From e44601d1b130e0968fc04d0775629ede07131b73 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 5 Jun 2024 15:35:36 +0000 Subject: [PATCH] fix merge: Bring back few fns that SchemaGuesserStream needs * BlueDataUtility.get_typed_schema * BlueDataUtility.get_typed_table --- .../BlueData/BlueDataUtility.cs | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs index 9422271aa..a9b65f3a6 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs @@ -348,6 +348,64 @@ namespace BCG { return new BlueTableSchema(table_name, cis.ToArray()); } + public static IBlueTableSchema get_typed_schema(IBlueRowCollection rows) { + // just use one row to guess + List cis = new List(); + foreach (IBlueRow r in rows) { + foreach (var ci in r.schema.columnInfo) { + if (ci.colType != typeof(System.String)) { + cis.Add(ci); + continue; + } + var (type, _) = guess_type(r[ci.colName] as string); + cis.Add(new ColumnInfo(ci.colName, ci.colNo, type)); + } + return new BlueTableSchema(cis.ToArray(), rows.schema.tableName); + } + return null; + } + + // converts a row where all columns are string types to a schema where the best types are guesed + public static IBlueRowCollection get_typed_table(IBlueRowCollection string_rows) { + IBlueTableSchema typed_schema = get_typed_schema(string_rows); + var rv = new BlueTable(typed_schema); + foreach (IBlueRow r in string_rows) { + var typed_row = get_typed_row(r, typed_schema); + rv.addRow(typed_row); + } + return rv; + + static IBlueRow get_typed_row(IBlueRow string_row, IBlueTableSchema typed_schema) { + var rv = new MutableBlueRow(typed_schema); + foreach (var ci in typed_schema.columnInfo) { + var typed_val = convert(ci.colType, string_row[ci.colName].ToString()); + rv.setValue(ci.colName, typed_val); + } + return rv; + } + + static object convert(Type t, string value) { + // Attempt to convert the string to the specified type + try { + return Convert.ChangeType(value, t); + } catch (Exception) { + if (t == typeof(double)) { + // some doubles come with currency and ',' we clean that up here (there might be better ways to do this) + string cleaned_val = new string(value.Where(c => c != '$' && c != ',').ToArray()); + cleaned_val = cleaned_val.Replace('−', '-'); + double rv_d; + if (Double.TryParse(cleaned_val, out rv_d)) + return rv_d; + else + return null; + } + return null; + // Handle conversion failure gracefully + // throw new ArgumentException("Failed to convert the string to the specified type."); + } + } + } + /// /// Gets the Hashcode of the row with the boolean indicating whether the order of the rows should /// be alphabetic or not