From 2bceda01ae83178b75377580b12caee44349dd4b Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Mon, 22 Jan 2024 16:28:23 -0500 Subject: [PATCH] Added code in BlueDataUtility to create schema from data --- .../BlueData/BlueDataUtility.cs | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs index a29191e2a..1a4762ffa 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs @@ -48,6 +48,12 @@ namespace BCG { rv.Add(r.get(col)); return rv; } + public static IEnumerable get_colvalues(IBlueRowCollection rows, string col) { + List rv = new List(); + foreach (IBlueRow r in rows) + rv.Add(r.get(col)); + return rv; + } public static IDictionary> group_by(IBlueRowCollection rows, string gp_col) { Dictionary> rv = new Dictionary>(); @@ -241,6 +247,68 @@ namespace BCG { return new BlueTableSchema(table_name, cis.ToArray()); } + public static TypeCode convert_column_type(IEnumerable col_vals) { + // bool + bool success = true; + foreach (var val in col_vals) { + bool parsed_val; + success = success && Boolean.TryParse(val, out parsed_val); + if (!success) + break; + } + if (success) + return TypeCode.Boolean; + // int + success = true; + foreach (var val in col_vals) { + int parsed_val; + success = success && Int32.TryParse(val, out parsed_val); + if (!success) + break; + } + if (success) + return TypeCode.Int32; + // try double + success = true; + foreach (var val in col_vals) { + double parsed_val; + success = success && Double.TryParse(val, out parsed_val); + if (!success) + break; + } + if (success) + return TypeCode.Double; + // try DateTime + success = true; + foreach (var val in col_vals) { + DateTime parsed_val; + success = success && DateTime.TryParse(val, out parsed_val); + if (!success) + break; + } + if (success) + return TypeCode.DateTime; + bts.Log.assert(false); + return TypeCode.Empty; + } + + public static IBlueTableSchema to_typed_schema(IBlueRowCollection rows) { + List cis = new List(); + foreach (var col in rows.schema.columnInfo) { + var text_vals = BlueDataUtility.get_colvalues(rows, col.colName); + TypeCode type_code = convert_column_type(text_vals); + cis.Add(new ColumnInfo(col.colName, cis.Count, _typecode_to_type[type_code])); + } + return new BlueTableSchema(cis.ToArray(), rows.schema.tableName); + } + + // public static IBlueRow convert_string_row(IBlueRow r, IBlueTableSchema out_schema) { + // foreach (var ci in out_schema.columnInfo){ + + // } + // } + + /// /// Gets the Hashcode of the row with the boolean indicating whether the order of the rows should /// be alphabetic or not