siddharthteotia commented on code in PR #8583:
URL: https://github.com/apache/pinot/pull/8583#discussion_r867252040


##########
pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/DataBlockBuilder.java:
##########
@@ -0,0 +1,406 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.blocks;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.ObjectSerDeUtils;
+import org.apache.pinot.core.common.datatable.DataTableUtils;
+import org.apache.pinot.spi.utils.ArrayCopyUtils;
+import org.apache.pinot.spi.utils.BigDecimalUtils;
+import org.apache.pinot.spi.utils.ByteArray;
+
+
+public class DataBlockBuilder {
+  private final DataSchema _dataSchema;
+  private final BaseDataBlock.Type _blockType;
+  private final DataSchema.ColumnDataType[] _columnDataType;
+
+  private int[] _columnOffsets;
+  private int _rowSizeInBytes;
+  private int[] _cumulativeColumnOffsetSizeInBytes;
+  private int[] _columnSizeInBytes;
+
+  private int _numRows;
+  private int _numColumns;
+
+  private final Map<String, Map<String, Integer>> _dictionaryMap = new 
HashMap<>();
+  private final Map<String, Map<Integer, String>> _reverseDictionaryMap = new 
HashMap<>();
+  private final ByteArrayOutputStream _fixedSizeDataByteArrayOutputStream = 
new ByteArrayOutputStream();
+  private final ByteArrayOutputStream _variableSizeDataByteArrayOutputStream = 
new ByteArrayOutputStream();
+  private final DataOutputStream _variableSizeDataOutputStream =
+      new DataOutputStream(_variableSizeDataByteArrayOutputStream);
+
+
+  private ByteBuffer _currentRowDataByteBuffer;
+
+  private DataBlockBuilder(DataSchema dataSchema, BaseDataBlock.Type 
blockType) {
+    _dataSchema = dataSchema;
+    _columnDataType = dataSchema.getStoredColumnDataTypes();
+    _blockType = blockType;
+    _numColumns = dataSchema.size();
+    if (_blockType == BaseDataBlock.Type.COLUMNAR) {
+      _cumulativeColumnOffsetSizeInBytes = new int[_numColumns];
+      _columnSizeInBytes = new int[_numColumns];
+      DataBlockUtils.computeColumnSizeInBytes(_dataSchema, _columnSizeInBytes);
+      int cumulativeColumnOffset = 0;
+      for (int i = 0; i < _numColumns; i++) {
+        _cumulativeColumnOffsetSizeInBytes[i] = cumulativeColumnOffset;
+        cumulativeColumnOffset += _columnSizeInBytes[i] * _numRows;
+      }
+    } else if (_blockType == BaseDataBlock.Type.ROW) {
+      _columnOffsets = new int[_numColumns];
+      _rowSizeInBytes = DataTableUtils.computeColumnOffsets(dataSchema, 
_columnOffsets);
+    }
+  }
+
+  public static RowDataBlock buildFromRows(List<Object[]> rows, DataSchema 
dataSchema)
+      throws IOException {
+    DataBlockBuilder rowBuilder = new DataBlockBuilder(dataSchema, 
BaseDataBlock.Type.ROW);
+    rowBuilder._numRows = rows.size();
+    for (Object[] row : rows) {
+      ByteBuffer byteBuffer = ByteBuffer.allocate(rowBuilder._rowSizeInBytes);
+      for (int i = 0; i < rowBuilder._numColumns; i++) {
+        Object value = row[i];
+        switch (rowBuilder._columnDataType[i]) {
+          // Single-value column
+          case INT:
+            byteBuffer.putInt(((Number) value).intValue());
+            break;
+          case LONG:
+            byteBuffer.putLong(((Number) value).longValue());
+            break;
+          case FLOAT:
+            byteBuffer.putFloat(((Number) value).floatValue());
+            break;
+          case DOUBLE:
+            byteBuffer.putDouble(((Number) value).doubleValue());
+            break;
+          case BIG_DECIMAL:
+            setColumn(rowBuilder, byteBuffer, (BigDecimal) value);
+            break;
+          case STRING:
+            setColumn(rowBuilder, byteBuffer, i, (String) value);
+            break;
+          case BYTES:
+            setColumn(rowBuilder, byteBuffer, (ByteArray) value);
+            break;
+          case OBJECT:
+            setColumn(rowBuilder, byteBuffer, value);
+            break;
+          // Multi-value column
+          case BOOLEAN_ARRAY:
+          case INT_ARRAY:
+            setColumn(rowBuilder, byteBuffer, (int[]) value);
+            break;
+          case TIMESTAMP_ARRAY:
+          case LONG_ARRAY:
+            // LONG_ARRAY type covers INT_ARRAY and LONG_ARRAY
+            if (value instanceof int[]) {
+              int[] ints = (int[]) value;
+              int length = ints.length;
+              long[] longs = new long[length];
+              ArrayCopyUtils.copy(ints, longs, length);
+              setColumn(rowBuilder, byteBuffer, longs);
+            } else {
+              setColumn(rowBuilder, byteBuffer, (long[]) value);
+            }
+            break;
+          case FLOAT_ARRAY:
+            setColumn(rowBuilder, byteBuffer, (float[]) value);
+            break;
+          case DOUBLE_ARRAY:

Review Comment:
   I guess this is dictated by the root operator in the current stage that is 
outputting data to be transferred. So, it knows the type and that is what we 
should use here in the schema when building the `DataBlock`. So, why do we need 
to handle int/long/float if we know the final type after processing the current 
stage is DOUBLE ?



-- 
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.

To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org

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

Reply via email to