using Bts.ColumnMapExpressions;
using Spark.Core.Diagnostics;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace BCG
{
///
/// Provides the place holder for the int array that maps aliased column indices to the original
/// ones (and the algorithms to compute them)
///
internal class ProjectHelper
{
internal int[] colMap;
internal IBlueTableSchema schema;
internal string[] relevantOrigCols;
///
///
/// original(unprojected) schema
/// expression defining the mapping
/// table name of the new schema
internal ProjectHelper (IBlueTableSchema isch, string exp, string? tableName)
: this (isch, myGetDictionary (isch, exp), tableName)
{}
///
///
/// original (unprojected) schema
/// dictionary mapping unprojected names to their projected names, dropped cols should not be there
/// table name of the new schema
internal ProjectHelper (
IBlueTableSchema isch, IDictionary origToAlias, string? tableName)
{
ArrayList colMapAl = new ArrayList (),
colInfoAl = new ArrayList ();
relevantOrigCols = new string[origToAlias.Count-1];
int c = 0;
ArrayList iColNames = new ArrayList (isch.columnNames);
iColNames.Sort ();
foreach (string colName in iColNames)
{
if (origToAlias.TryGetValue(colName, out var colAlias))
{
ColumnInfo ci = isch[colName];
colInfoAl.Add (new ColumnInfo (colAlias,
colInfoAl.Count,
ci.colType)
);
colMapAl.Add (ci.colNo);
if (ci.colName != "id")
relevantOrigCols[c++] = ci.colName;
}
}
schema = new BlueTableSchema (tableName == null ? isch.tableName : tableName,
colInfoAl.ToArray (typeof(ColumnInfo)) as ColumnInfo[]);
colMap = (int[])colMapAl.ToArray(typeof(int));
}
///
/// Gets an IDictionary mapping the original names to their aliased names. (The columns
/// dropped do not appear in the returned value)
///
///
///
///
private static IDictionary myGetDictionary (IBlueTableSchema isch, string exp)
{
IDictionary origToAlias = new Dictionary();
var e = ColumnMapExpression.Parse(exp)
?? throw new ArgumentException("Failed to parse ColumnMapExpression", nameof(exp));
if (e.IsInclusive) {
origToAlias = e.Columns.ToDictionary();
origToAlias["id"] = "id";
} else {
origToAlias = isch.columnInfo.ToDictionary(
ci => ci.colName, ci => e.Columns.GetValueOrDefault(ci.colName) ?? ci.colName);
}
return origToAlias;
}
///
/// Returns a new dictionary with the keys and values interchanging positions
///
///
///
internal static IDictionary invertDic (IDictionary dic)
{
IDictionary retVal = new Hashtable (dic.Count);
foreach (DictionaryEntry de in dic)
retVal.Add(ExceptionAssert.NotNull(de.Value), de.Key);
return retVal;
}
}
}