The AddColumnsStream, SortStream, and requisite yacc/lex grammar were originally added to a new branch, off of development, which is on net8. I forgot to change it to net7 when I cherry-picked the commits for the regular development branch. It's wasn't quite as simple as flipping net8 -> net7 in csproj files, because I was eager to use new language features only available in net8! Namely, primary constructors and collection expressions.
53 lines
2.0 KiB
C#
53 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
|
|
using Bts.ColumnExpressions;
|
|
|
|
namespace BCG;
|
|
|
|
/// <summary>
|
|
/// Provides methods to add columns to the schema
|
|
/// </summary>
|
|
/// <param name="columnsExpression">
|
|
/// String represting the columns to add to the output table
|
|
/// </param>
|
|
public class ColumnAdder : IStreamMapper {
|
|
// Store tuples of (column, index) to preserve order
|
|
protected readonly IDictionary<string, (ColumnExpression column, int index)> myNewColumns;
|
|
protected IBlueTableSchema? mySchema;
|
|
|
|
public ColumnAdder(string columnsExpression) =>
|
|
myNewColumns = (ColumnExpressionInterpreter.Evaluate(columnsExpression) ??
|
|
throw new Exception("Invalid columnn expression: " + columnsExpression))
|
|
.Select((c, i) => (c, i))
|
|
.ToDictionary(c => c.c.Name, c => (c.c, c.i));
|
|
|
|
[MemberNotNull(nameof(mySchema))]
|
|
public virtual IBlueRow map(IBlueRow row) {
|
|
// Only rebuild schema if necessary (i.e., if it's null)
|
|
mySchema ??= buildSchema(row.schema, myNewColumns);
|
|
return new AddColumnsRow(mySchema, row);
|
|
}
|
|
|
|
[MemberNotNull(nameof(mySchema))]
|
|
public IBlueTableSchema getOutputSchema(IBlueTableSchema inputSchema) =>
|
|
// Always rebuild schema (even if it's already non-null)
|
|
mySchema = buildSchema(inputSchema, myNewColumns);
|
|
|
|
[MemberNotNull(nameof(mySchema))]
|
|
static BlueTableSchema buildSchema(
|
|
IBlueTableSchema schema,
|
|
IDictionary<string, (ColumnExpression column, int index)> newColumns
|
|
) {
|
|
ColumnInfo[] cis = new ColumnInfo[schema.columnNames.Length + newColumns.Count];
|
|
schema.columnInfo.CopyTo(cis, 0);
|
|
foreach (var (col, index) in newColumns.Values) {
|
|
var schemaIdx = schema.columnInfo.Length + index;
|
|
cis[schemaIdx] = new ColumnInfo(col.Name, schemaIdx, col.Type, col.Value);
|
|
}
|
|
return new BlueTableSchema(cis, schema.tableName);
|
|
}
|
|
}
|