120 lines
3.9 KiB
C#
120 lines
3.9 KiB
C#
namespace bts.BlueOrb;
|
|
|
|
using BCG;
|
|
|
|
using bts.collections;
|
|
|
|
using Bts.DataStreamCore;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
public class CummulativeSumTable : IBlueRowCollection {
|
|
readonly IBlueRowCollection input;
|
|
readonly IList<object?>[] columns;
|
|
readonly StringBehavior stringBehavior;
|
|
|
|
public CummulativeSumTable(IBlueRowCollection input, StringBehavior stringBehavior = StringBehavior.LastValue) {
|
|
this.input = input;
|
|
var colInfos = input.schema.columnInfo.OrderBy(c => c.colNo).ToArray();
|
|
if (colInfos.Length > 0) {
|
|
Log.assert(colInfos[0].colNo == 0 && colInfos[^1].colNo == colInfos.Length - 1);
|
|
}
|
|
columns = colInfos.WrapSelector(c => GetColumn(c.colNo), null).ToArray();
|
|
}
|
|
|
|
/// <summary>
|
|
/// By default, we take the last value for strings intstead of adding (concatenating) them.
|
|
/// Otherwise it would be too easy to create massive concatenated strings by mistake.
|
|
/// </summary>
|
|
public enum StringBehavior {
|
|
LastValue,
|
|
Concatenate
|
|
}
|
|
|
|
public IBlueRow this[int id] =>
|
|
id == 1 ? GetRow(0) : throw new KeyNotFoundException();
|
|
|
|
public IBlueTableSchema schema => input.schema;
|
|
|
|
public int Count => 1;
|
|
|
|
public bool containsID(int id) =>
|
|
id == 1;
|
|
|
|
public IList<object?> GetColumn(int colIndex) {
|
|
var count = input.Count;
|
|
var colInfo = input.schema.columnInfo[colIndex];
|
|
if (colInfo.colName == "id") {
|
|
return new object?[] { 1 };
|
|
}
|
|
object? res = null;
|
|
var colVals = input.GetColumn(colIndex);
|
|
if (colInfo.colType == typeof(TimeSpan)) {
|
|
// TimeSpan is a special case because it doesn't have a TypeCode.
|
|
res = TimeSpanImpl(colVals);
|
|
} else {
|
|
var typeCode = colInfo.type_code();
|
|
const int MAX_STRING_LENGTH = 1024;
|
|
res = typeCode switch {
|
|
TypeCode.Int32 => colVals.Cast<int>().Sum(),
|
|
TypeCode.Int64 => colVals.Cast<long>().Sum(),
|
|
TypeCode.UInt32 => colVals.Cast<uint>().Sum(),
|
|
TypeCode.UInt64 => colVals.Cast<ulong>().Aggregate(0UL, (s, v) => s + v),
|
|
TypeCode.Single => colVals.Cast<float>().Sum(),
|
|
TypeCode.Double => colVals.Cast<double>().Sum(),
|
|
TypeCode.DateTime => colVals.Cast<DateTime>().Aggregate(DateTime.MinValue, (s, v) => new DateTime(s.Ticks + v.Ticks)),
|
|
TypeCode.String => stringBehavior switch {
|
|
StringBehavior.Concatenate => colVals.Cast<string>().Aggregate(string.Empty, (s, v) => {
|
|
if (s.Length + v.Length > MAX_STRING_LENGTH) {
|
|
throw new InvalidOperationException("Cannot sum strings because the result is too long");
|
|
}
|
|
return s + v;
|
|
}),
|
|
_ => count == 0 ? null : colVals[^1] as string,
|
|
},
|
|
_ => null,
|
|
};
|
|
}
|
|
return new object?[] { res };
|
|
|
|
static object? TimeSpanImpl(IList<object?> colVals) {
|
|
var ticks = colVals.Cast<TimeSpan>().Select(ts => ts.Ticks).Sum();
|
|
return TimeSpan.FromTicks(ticks);
|
|
}
|
|
}
|
|
|
|
public IEnumerator<IBlueRow> GetEnumerator() {
|
|
yield return GetRow(0);
|
|
}
|
|
|
|
public IBlueRow GetRow(int index) =>
|
|
new BlueRowColumnWrapper(input.schema, index, columns);
|
|
|
|
public int IndexOf(IBlueRow item) {
|
|
int id = item.Id();
|
|
var inputRow = input[id];
|
|
int idx = input.IndexOf(inputRow);
|
|
if (idx != -1) {
|
|
var row = GetRow(idx);
|
|
if (BlueRowEqualityComparer.Equals(item, row)) {
|
|
return idx;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
public class CummulativeSumTableEvaluator : IFunctionEvaluator {
|
|
public object? Evaluate(string functionId, IEnumerable<StreamArgument> args) {
|
|
if (args.TryGetFirst(out var a)) {
|
|
var table = a.Value as IBlueRowCollection;
|
|
if (table is not null) {
|
|
return new CummulativeSumTable(table);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|