Files
spark/src/DataStreamSuite/DataStream/Primitives/NullToConstant/NullToConstantMapper.cs
T
2023-09-04 16:25:40 -04:00

111 lines
2.7 KiB
C#

using System;
using System.Collections;
using BCG;
using BCG.Utilities;
using System.Collections.Generic;
using mbts;
namespace BCG
{
public class NullToConstantMapper : IStreamMapper
{
public NullToConstantMapper(string field, object constant)
{
myField = field;
myConstant = constant;
}
#region IStreamMapper Members
public IBlueTableSchema getOutputSchema(IBlueTableSchema inputSchema)
{
return inputSchema;
}
#endregion
#region IDynamicStreamMapper Members
public IBlueRow map(IBlueRow row)
{
if(row[myField].Equals(DBNull.Value))
{
Hashtable newRow = new Hashtable(row);
newRow[myField]= myConstant;
return new BlueRow(row.schema, newRow);
}
else
return row;
}
#endregion
private string myField;
private object myConstant;
}
public class AllNullToConstantMapper : IStreamMapper
{
public AllNullToConstantMapper(object constant)
{
myConstant = constant;
}
#region IStreamMapper Members
public IBlueTableSchema getOutputSchema(IBlueTableSchema inputSchema)
{
return inputSchema;
}
#endregion
#region IDynamicStreamMapper Members
public IBlueRow map(IBlueRow row)
{
bool change = false;
Hashtable newRow = new Hashtable(row);
foreach(ColumnInfo info in row.schema.columnInfo)
{
if(row[info.colName].Equals(DBNull.Value))
{
change = true;
newRow[info.colName] = Convert.ChangeType(myConstant, info.colType);
}
}
if(change)
return new BlueRow(row.schema, newRow);
else
return row;
}
#endregion
private object myConstant;
}
public class SpecifiedNullToConstantMapper : IStreamMapper
{
private Pair<string, object>[] _col_val_pair_list;
public SpecifiedNullToConstantMapper(params Pair<string, object>[] col_val_pair_list)
{
_col_val_pair_list = col_val_pair_list;
}
public IBlueTableSchema getOutputSchema(IBlueTableSchema s) {return s;}
public IBlueRow map(IBlueRow r)
{
MutableBlueRow rv = new MutableBlueRow(getOutputSchema(r.schema), r);
foreach (Pair<string, object> p in _col_val_pair_list)
{
object v = BlueDataUtility.get_row_value<object>(r, p.first, DBNull.Value);
if (v == null || v == DBNull.Value)
rv.setValue(p.first, p.second);
}
return rv;
}
}
}