From 4690c978b4309b2785ffeaa5c8a6d5f382e2292a Mon Sep 17 00:00:00 2001 From: Zhan Lu <51200935+lausannel@users.noreply.github.com> Date: Tue, 19 Nov 2024 09:49:04 +0800 Subject: [PATCH] fix: incorrect column order in results returned by SELECT statements (#23) * fix: wrong column order in SessionDataSet * fix: .net 5.0 does not support `[]` init --- .../SessionPoolTest.Record.cs | 16 +++++ .../DataStructure/SessionDataSet.cs | 63 ++++++++----------- 2 files changed, 43 insertions(+), 36 deletions(-) diff --git a/samples/Apache.IoTDB.Samples/SessionPoolTest.Record.cs b/samples/Apache.IoTDB.Samples/SessionPoolTest.Record.cs index 44c0c98..a113480 100644 --- a/samples/Apache.IoTDB.Samples/SessionPoolTest.Record.cs +++ b/samples/Apache.IoTDB.Samples/SessionPoolTest.Record.cs @@ -295,6 +295,22 @@ public async Task TestInsertRecords() Console.WriteLine(res_count + " " + fetch_size * processed_size); System.Diagnostics.Debug.Assert(res_count == record_count); System.Diagnostics.Debug.Assert(status == 0); + + string sql = string.Format("select {0}, {1}, {2} from ", test_measurements[3], test_measurements[1], test_measurements[2]) + string.Format("{0}.{1}", test_group_name, test_device); + res = await session_pool.ExecuteQueryStatementAsync(sql); + res.ShowTableNames(); + RowRecord row = null; + while (res.HasNext()) + { + row = res.Next(); + break; + } + + Console.WriteLine($"{test_group_name}.{test_device}.{row.Measurements[0]} {test_measurements[3]}"); + System.Diagnostics.Debug.Assert($"{test_group_name}.{test_device}.{test_measurements[3]}" == row.Measurements[0]); + System.Diagnostics.Debug.Assert($"{test_group_name}.{test_device}.{test_measurements[1]}" == row.Measurements[1]); + System.Diagnostics.Debug.Assert($"{test_group_name}.{test_device}.{test_measurements[2]}" == row.Measurements[2]); + status = await session_pool.DeleteStorageGroupAsync(test_group_name); System.Diagnostics.Debug.Assert(status == 0); await session_pool.Close(); diff --git a/src/Apache.IoTDB/DataStructure/SessionDataSet.cs b/src/Apache.IoTDB/DataStructure/SessionDataSet.cs index b9f7261..fcdf265 100644 --- a/src/Apache.IoTDB/DataStructure/SessionDataSet.cs +++ b/src/Apache.IoTDB/DataStructure/SessionDataSet.cs @@ -63,6 +63,7 @@ public SessionDataSet(string sql, TSExecuteStatementResp resp, ConcurrentClientQ _currentBitmap = new byte[_columnSize]; _columnNames = new List(); _timeBuffer = new ByteBuffer(_queryDataset.Time); + // column name -> column location _columnNameIndexMap = new Dictionary(); _columnTypeLst = new List(); _duplicateLocation = new Dictionary(); @@ -72,43 +73,31 @@ public SessionDataSet(string sql, TSExecuteStatementResp resp, ConcurrentClientQ _hasCatchedResult = false; _rowIndex = 0; RowCount = _queryDataset.Time.Length / sizeof(long); - if (resp.ColumnNameIndexMap != null) - { - for (var index = 0; index < resp.Columns.Count; index++) - { - _columnNames.Add(""); - _columnTypeLst.Add(""); - } - - for (var index = 0; index < resp.Columns.Count; index++) - { - var name = resp.Columns[index]; - _columnNames[resp.ColumnNameIndexMap[name]] = name; - _columnTypeLst[resp.ColumnNameIndexMap[name]] = resp.DataTypeList[index]; - } - } - else - { - _columnNames = resp.Columns; - _columnTypeLst = resp.DataTypeList; - } - for (int index = 0; index < _columnNames.Count; index++) - { - var columnName = _columnNames[index]; - if (_columnNameIndexMap.ContainsKey(columnName)) - { - _duplicateLocation[index] = _columnNameIndexMap[columnName]; - } - else - { - _columnNameIndexMap[columnName] = index; + _columnNames = resp.Columns; + _columnTypeLst = resp.DataTypeList; + + int deduplicateIdx = 0; + Dictionary columnToFirstIndexMap = new Dictionary(); + for(var i = 0; i < _columnSize; i++){ + var columnName = _columnNames[i]; + if(_columnNameIndexMap.ContainsKey(columnName)){ + _duplicateLocation[i] = columnToFirstIndexMap[columnName]; + } else { + columnToFirstIndexMap[columnName] = i; + if(resp.ColumnNameIndexMap != null) { + int valueIndex = resp.ColumnNameIndexMap[columnName]; + _columnNameIndexMap[columnName] = valueIndex; + _valueBufferLst.Add(new ByteBuffer(_queryDataset.ValueList[valueIndex])); + _bitmapBufferLst.Add(new ByteBuffer(_queryDataset.BitmapList[valueIndex])); + } else { + _columnNameIndexMap[columnName] = deduplicateIdx; + _valueBufferLst.Add(new ByteBuffer(_queryDataset.ValueList[deduplicateIdx])); + _bitmapBufferLst.Add(new ByteBuffer(_queryDataset.BitmapList[deduplicateIdx])); + } + deduplicateIdx++; } - - _valueBufferLst.Add(new ByteBuffer(_queryDataset.ValueList[index])); - _bitmapBufferLst.Add(new ByteBuffer(_queryDataset.BitmapList[index])); } - } public List ColumnNames => _columnNames; @@ -282,8 +271,10 @@ private bool FetchResults() _bitmapBufferLst = new List(); for (int index = 0; index < _queryDataset.ValueList.Count; index++) { - _valueBufferLst.Add(new ByteBuffer(_queryDataset.ValueList[index])); - _bitmapBufferLst.Add(new ByteBuffer(_queryDataset.BitmapList[index])); + string columnName = _columnNames[index]; + int valueIndex = _columnNameIndexMap[columnName]; + _valueBufferLst.Add(new ByteBuffer(_queryDataset.ValueList[valueIndex])); + _bitmapBufferLst.Add(new ByteBuffer(_queryDataset.BitmapList[valueIndex])); } // reset row index