Jackie-Jiang commented on a change in pull request #5605: URL: https://github.com/apache/incubator-pinot/pull/5605#discussion_r445181570
########## File path: pinot-core/src/main/java/org/apache/pinot/core/query/reduce/GroupByDataTableReducer.java ########## @@ -171,106 +160,81 @@ public void reduceAndSetResults(String tableName, DataSchema dataSchema, */ private void setSQLGroupByInResultTable(BrokerResponseNative brokerResponseNative, DataSchema dataSchema, Collection<DataTable> dataTables) { - + DataSchema resultTableSchema = getSQLResultTableSchema(dataSchema); IndexedTable indexedTable = getIndexedTable(dataSchema, dataTables); - - int[] finalSchemaMapIdx = null; - if (_sqlSelectionList != null) { - finalSchemaMapIdx = getFinalSchemaMapIdx(); - } - List<Object[]> rows = new ArrayList<>(); Iterator<Record> sortedIterator = indexedTable.iterator(); - int numRows = 0; - while (numRows < _groupBy.getTopN() && sortedIterator.hasNext()) { - Record nextRecord = sortedIterator.next(); - Object[] values = nextRecord.getValues(); - - int index = _numGroupBy; - int aggNum = 0; - while (index < _numColumns) { - values[index] = AggregationFunctionUtils - .getSerializableValue(_aggregationFunctions[aggNum++].extractFinalResult(values[index])); - index++; + int limit = _queryContext.getLimit(); + List<Object[]> rows = new ArrayList<>(limit); + + if (_sqlQuery) { + // NOTE: For SQL query, need to reorder the columns in the data table based on the select expressions. + + int[] selectExpressionIndexMap = getSelectExpressionIndexMap(); + int numSelectExpressions = selectExpressionIndexMap.length; + String[] columnNames = resultTableSchema.getColumnNames(); + DataSchema.ColumnDataType[] columnDataTypes = resultTableSchema.getColumnDataTypes(); + String[] reorderedColumnNames = new String[numSelectExpressions]; + DataSchema.ColumnDataType[] reorderedColumnDataTypes = new DataSchema.ColumnDataType[numSelectExpressions]; + resultTableSchema = new DataSchema(reorderedColumnNames, reorderedColumnDataTypes); + for (int i = 0; i < numSelectExpressions; i++) { + reorderedColumnNames[i] = columnNames[selectExpressionIndexMap[i]]; + reorderedColumnDataTypes[i] = columnDataTypes[selectExpressionIndexMap[i]]; } - if (_sqlSelectionList != null) { Review comment: I don't think so. The existing logic have the same if condition in 3 places (line 178, 195, 208), and this one is even in a while loop. Instead, the change makes it one time branching, which is much more clear IMO. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org