clintropolis commented on code in PR #16885:
URL: https://github.com/apache/druid/pull/16885#discussion_r1734014991


##########
processing/src/main/java/org/apache/druid/frame/read/columnar/StringArrayFrameColumnReader.java:
##########
@@ -0,0 +1,386 @@
+/*
+ * 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.druid.frame.read.columnar;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.primitives.Ints;
+import it.unimi.dsi.fastutil.objects.ObjectArrays;
+import org.apache.datasketches.memory.Memory;
+import org.apache.druid.common.config.NullHandling;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.frame.Frame;
+import org.apache.druid.frame.read.FrameReaderUtils;
+import org.apache.druid.frame.write.FrameWriterUtils;
+import org.apache.druid.frame.write.columnar.FrameColumnWriters;
+import org.apache.druid.frame.write.columnar.StringFrameColumnWriter;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector;
+import org.apache.druid.query.rowsandcols.column.Column;
+import org.apache.druid.query.rowsandcols.column.ColumnAccessorBasedColumn;
+import 
org.apache.druid.query.rowsandcols.column.accessor.ObjectColumnAccessorBase;
+import org.apache.druid.segment.ColumnValueSelector;
+import org.apache.druid.segment.ObjectColumnSelector;
+import org.apache.druid.segment.column.BaseColumn;
+import org.apache.druid.segment.column.ColumnCapabilitiesImpl;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.data.ReadableOffset;
+import org.apache.druid.segment.vector.ReadableVectorInspector;
+import org.apache.druid.segment.vector.ReadableVectorOffset;
+import org.apache.druid.segment.vector.VectorObjectSelector;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+import java.util.Comparator;
+
+/**
+ * Reader for {@link ColumnType#STRING_ARRAY}.

Review Comment:
   might be nice to callout in javadocs that there is similarity with 
multi-value string format



##########
processing/src/main/java/org/apache/druid/frame/read/columnar/StringArrayFrameColumnReader.java:
##########
@@ -0,0 +1,386 @@
+/*
+ * 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.druid.frame.read.columnar;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.primitives.Ints;
+import it.unimi.dsi.fastutil.objects.ObjectArrays;
+import org.apache.datasketches.memory.Memory;
+import org.apache.druid.common.config.NullHandling;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.frame.Frame;
+import org.apache.druid.frame.read.FrameReaderUtils;
+import org.apache.druid.frame.write.FrameWriterUtils;
+import org.apache.druid.frame.write.columnar.FrameColumnWriters;
+import org.apache.druid.frame.write.columnar.StringFrameColumnWriter;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector;
+import org.apache.druid.query.rowsandcols.column.Column;
+import org.apache.druid.query.rowsandcols.column.ColumnAccessorBasedColumn;
+import 
org.apache.druid.query.rowsandcols.column.accessor.ObjectColumnAccessorBase;
+import org.apache.druid.segment.ColumnValueSelector;
+import org.apache.druid.segment.ObjectColumnSelector;
+import org.apache.druid.segment.column.BaseColumn;
+import org.apache.druid.segment.column.ColumnCapabilitiesImpl;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.data.ReadableOffset;
+import org.apache.druid.segment.vector.ReadableVectorInspector;
+import org.apache.druid.segment.vector.ReadableVectorOffset;
+import org.apache.druid.segment.vector.VectorObjectSelector;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+import java.util.Comparator;
+
+/**
+ * Reader for {@link ColumnType#STRING_ARRAY}.
+ */
+public class StringArrayFrameColumnReader implements FrameColumnReader
+{
+  private final int columnNumber;
+
+  /**
+   * Create a new reader.
+   *
+   * @param columnNumber column number
+   */
+  StringArrayFrameColumnReader(int columnNumber)
+  {
+    this.columnNumber = columnNumber;
+  }
+
+  @Override
+  public Column readRACColumn(Frame frame)
+  {
+    final Memory memory = frame.region(columnNumber);
+    validate(memory);
+
+    final long positionOfLengths = 
getStartOfStringLengthSection(frame.numRows());
+    final long positionOfPayloads = getStartOfStringDataSection(memory, 
frame.numRows());
+
+    StringArrayFrameColumn frameCol = new StringArrayFrameColumn(
+        frame,
+        memory,
+        positionOfLengths,
+        positionOfPayloads
+    );
+
+    return new ColumnAccessorBasedColumn(frameCol);
+  }
+
+  @Override
+  public ColumnPlus readColumn(final Frame frame)
+  {
+    final Memory memory = frame.region(columnNumber);
+    validate(memory);
+
+    final long startOfStringLengthSection = 
getStartOfStringLengthSection(frame.numRows());
+    final long startOfStringDataSection = getStartOfStringDataSection(memory, 
frame.numRows());
+
+    final BaseColumn baseColumn = new StringArrayFrameColumn(
+        frame,
+        memory,
+        startOfStringLengthSection,
+        startOfStringDataSection
+    );
+
+    return new ColumnPlus(
+        baseColumn,
+        new ColumnCapabilitiesImpl().setType(ColumnType.STRING_ARRAY)
+                                    .setHasMultipleValues(false)
+                                    .setDictionaryEncoded(false),
+        frame.numRows()
+    );
+  }
+
+  private void validate(final Memory region)
+  {
+    // Check if column is big enough for a header
+    if (region.getCapacity() < StringFrameColumnWriter.DATA_OFFSET) {
+      throw DruidException.defensive("Column[%s] is not big enough for a 
header", columnNumber);
+    }
+
+    final byte typeCode = region.getByte(0);
+    if (typeCode != FrameColumnWriters.TYPE_STRING_ARRAY) {
+      throw DruidException.defensive(
+          "Column[%s] does not have the correct type code; expected[%s], 
got[%s]",
+          columnNumber,
+          FrameColumnWriters.TYPE_STRING_ARRAY,
+          typeCode
+      );
+    }
+  }
+
+  private static long getStartOfCumulativeLengthSection()
+  {
+    return StringFrameColumnWriter.DATA_OFFSET;
+  }
+
+  private static long getStartOfStringLengthSection(final int numRows)
+  {
+    return StringFrameColumnWriter.DATA_OFFSET + (long) Integer.BYTES * 
numRows;
+  }
+
+  private long getStartOfStringDataSection(
+      final Memory memory,
+      final int numRows
+  )
+  {
+    if (numRows < 0) {
+      throw DruidException.defensive("Encountered -ve numRows [%s] while 
reading frame", numRows);
+    }
+    final int totalNumValues = 
FrameColumnReaderUtils.getAdjustedCumulativeRowLength(
+        memory,
+        getStartOfCumulativeLengthSection(),
+        numRows - 1
+    );
+
+    return getStartOfStringLengthSection(numRows) + (long) Integer.BYTES * 
totalNumValues;
+  }
+
+  @VisibleForTesting
+  private static class StringArrayFrameColumn extends ObjectColumnAccessorBase 
implements BaseColumn
+  {
+    private final Frame frame;
+    private final Memory memory;
+    private final long startOfStringLengthSection;
+    private final long startOfStringDataSection;
+
+    private StringArrayFrameColumn(
+        Frame frame,
+        Memory memory,
+        long startOfStringLengthSection,
+        long startOfStringDataSection
+    )
+    {
+      this.frame = frame;
+      this.memory = memory;
+      this.startOfStringLengthSection = startOfStringLengthSection;
+      this.startOfStringDataSection = startOfStringDataSection;
+    }
+
+    @Override
+    public ColumnValueSelector<?> makeColumnValueSelector(ReadableOffset 
offset)
+    {
+      return new ObjectColumnSelector<Object>()
+      {
+        @Override
+        public void inspectRuntimeShape(RuntimeShapeInspector inspector)
+        {
+          // Do nothing.
+        }
+
+        @Nullable
+        @Override
+        public Object getObject()
+        {
+          return getRowAsObject(frame.physicalRow(offset.getOffset()), true);
+        }
+
+        @Override
+        public Class<?> classOfObject()
+        {
+          return String[].class;

Review Comment:
   this should probably be `Object[].class` to be consistent with the others, 
though i'm not sure it matters either



##########
processing/src/main/java/org/apache/druid/frame/read/columnar/StringFrameColumnReader.java:
##########
@@ -92,18 +85,17 @@ public Column readRACColumn(Frame frame)
     final Memory memory = frame.region(columnNumber);
     validate(memory);
 
-    final long positionOfLengths = 
getStartOfStringLengthSection(frame.numRows(), false);
-    final long positionOfPayloads = getStartOfStringDataSection(memory, 
frame.numRows(), false);
-
-    StringFrameColumn frameCol =
-        new StringFrameColumn(
-            frame,
-            false,
-            memory,
-            positionOfLengths,
-            positionOfPayloads,
-            asArray || isMultiValue(memory) // Read MVDs as String arrays
-        );
+    final boolean multiValue = isMultiValue(memory);
+    final long positionOfLengths = 
getStartOfStringLengthSection(frame.numRows(), multiValue);
+    final long positionOfPayloads = getStartOfStringDataSection(memory, 
frame.numRows(), multiValue);
+
+    StringFrameColumn frameCol = new StringFrameColumn(
+        frame,
+        multiValue,
+        memory,
+        positionOfLengths,
+        positionOfPayloads
+    );

Review Comment:
   is this cool? I sort of remember the rows and columns stuff not handling 
multi-value strings, which is maybe why it was trying to force it to read as a 
string array here, though idk that implicit casting like this is good either. 
Are there any tests that try to feed a multi-value string into window stuff?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to