90 lines
4.4 KiB
C#
90 lines
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace BCG {
|
|
public class NullMapper : IStreamMapper {
|
|
private Dictionary<string, bool> _valid_cols = new Dictionary<string, bool>();
|
|
|
|
public NullMapper(string[] valid_cols) {
|
|
foreach (string c in valid_cols)
|
|
_valid_cols[c] = true;
|
|
_valid_cols["id"] = true;
|
|
}
|
|
|
|
public IBlueTableSchema getOutputSchema(IBlueTableSchema input_schema) { return input_schema; }
|
|
|
|
public IBlueRow map(IBlueRow row) {
|
|
MutableBlueRow out_row = new MutableBlueRow(row);
|
|
foreach (string c in row.schema.columnNames)
|
|
if (!_valid_cols.ContainsKey(c))
|
|
out_row.setValue(c, DBNull.Value);
|
|
return out_row;
|
|
}
|
|
}
|
|
|
|
public class SumStream : ViewStream {
|
|
public SumStream(ViewStream stream, string spaceSeparatedOutputCols, string spaceSeparatedGroupByCols) {
|
|
string[] outputCols = spaceSeparatedOutputCols.Split(' ');
|
|
string[] groupByCols = spaceSeparatedGroupByCols.Split(' ');
|
|
mySetRealSource(new GroupByStream(stream,
|
|
outputCols.Length == 1 ? new SumGroupByEvaluator(outputCols[0], groupByCols) as IGroupByEvaluator :
|
|
new MultiColumnSumGroupByEvaluator(spaceSeparatedOutputCols, spaceSeparatedGroupByCols)));
|
|
}
|
|
|
|
public SumStream(ViewStream stream, string[] output_columns) {
|
|
ViewStream rs = new AddColumnsStream(stream, "fake_col=0");
|
|
rs = new SumStream(rs, output_columns, new string[] { "fake_col" }, false);
|
|
rs = new ProjectStream(rs, "!fake_col");
|
|
mySetRealSource(rs);
|
|
}
|
|
|
|
public SumStream(ViewStream stream, string space_seperated_output_columns, string space_seperated_groupby_columns, bool force_null_into_other_columns) {
|
|
string[] output_columns = space_seperated_output_columns.Split(' ');
|
|
string[] groupby_columns = String.IsNullOrEmpty(space_seperated_groupby_columns) ? new string[] { } : space_seperated_groupby_columns.Split(' ');
|
|
mySetRealSource(new SumStream(stream, output_columns, groupby_columns, force_null_into_other_columns));
|
|
}
|
|
|
|
public SumStream(ViewStream stream, string[] output_columns, string[] groupby_columns, bool force_null_into_other_columns) {
|
|
ViewStream rs = null;
|
|
groupby_columns = groupby_columns ?? new string[] { };
|
|
if (groupby_columns.Length == 0) {
|
|
// rs = new AddColumnsStream(stream, "fake_col=0");
|
|
// rs = new GroupByStream(rs, new MultiColumnSumGroupByEvaluator(output_columns, new string[] { "fake_col" }));
|
|
// rs = new ProjectStream(rs, "!fake_col");
|
|
} else {
|
|
rs = new GroupByStream(stream, new MultiColumnSumGroupByEvaluator(output_columns, groupby_columns));
|
|
}
|
|
if (force_null_into_other_columns) {
|
|
List<string> valid_cols = new List<string>(output_columns);
|
|
valid_cols.AddRange(groupby_columns);
|
|
rs = new MapStream(rs, new NullMapper(valid_cols.ToArray()));
|
|
}
|
|
mySetRealSource(rs);
|
|
}
|
|
|
|
public SumStream(ViewStream stream, string outputCol)
|
|
: this(new AddColumnsStream(stream, "fakeCol=0"), outputCol, "fakeCol") {
|
|
}
|
|
}
|
|
|
|
public class IntSumStream : ViewStream {
|
|
public IntSumStream(ViewStream stream, string spaceSeparatedOutputCols, string spaceSeparatedGroupByCols) {
|
|
string[] outputCols = spaceSeparatedOutputCols.Split(' ');
|
|
string[] groupByCols = spaceSeparatedGroupByCols.Split(' ');
|
|
mySetRealSource(new GroupByStream(stream,
|
|
outputCols.Length == 1 ? new SumGroupByEvaluator<int>(outputCols[0], groupByCols) as IGroupByEvaluator :
|
|
new MultiColumnSumGroupByEvaluator<int>(spaceSeparatedOutputCols, spaceSeparatedGroupByCols)));
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public class SumStream<T> : ViewStream {
|
|
public SumStream(ViewStream stream, string outputCol, string spaceSeparatedInputCols) {
|
|
string[] inputCols = spaceSeparatedInputCols.Split(' ');
|
|
mySetRealSource(new GroupByStream(stream,
|
|
new SumGroupByEvaluator<T>(outputCol, inputCols)));
|
|
}
|
|
}
|
|
}
|