gianm commented on code in PR #16863:
URL: https://github.com/apache/druid/pull/16863#discussion_r1730233325


##########
processing/src/main/java/org/apache/druid/segment/data/CompressedBlockReader.java:
##########
@@ -169,16 +177,21 @@ public ByteBuffer getRange(long startOffset, int size)
     if (size == 0) {
       return NULL_VALUE;
     }
-
     final int startBlockOffset = loadBlock(startOffset);
     final int startBlockNumber = currentBlockNumber;
     decompressedDataBuffer.position(startBlockOffset);
-    // patch together value from n underlying compressed pages
+    // possibly patch together value from n underlying compressed pages
     if (size < decompressedDataBuffer.remaining()) {
-      // sweet, same buffer, we can slice out a view directly to the value
-      final ByteBuffer dupe = 
decompressedDataBuffer.duplicate().order(byteOrder);
-      dupe.position(startBlockOffset).limit(startBlockOffset + size);
-      return dupe.slice().order(byteOrder);
+      // sweet, same buffer, we can return the buffer directly with position 
and limit set

Review Comment:
   this comment appears to no longer be 100% accurate



##########
processing/src/main/java/org/apache/druid/segment/serde/ComplexMetricSerde.java:
##########
@@ -142,14 +120,100 @@ public Object fromBytes(byte[] data, int start, int 
numBytes)
   }
 
   /**
-   * This method provides the ability for a ComplexMetricSerde to control its 
own serialization.
-   * For large column (i.e columns greater than {@link Integer#MAX_VALUE}) use
-   * {@link LargeColumnSupportedComplexColumnSerializer}
+   * Deserializes a ByteBuffer and adds it to the ColumnBuilder.  This method 
allows for the ComplexMetricSerde
+   * to implement it's own versioning scheme to allow for changes of binary 
format in a forward-compatible manner.
    *
-   * @return an instance of GenericColumnSerializer used for serialization.
+   * @param buffer  the buffer to deserialize
+   * @param builder ColumnBuilder to add the column to
+   * @param columnConfig ColumnConfiguration used during deserialization
+   */
+  public void deserializeColumn(
+      ByteBuffer buffer,
+      ColumnBuilder builder,
+      ColumnConfig columnConfig
+  )
+  {
+    deserializeColumn(buffer, builder);
+  }
+
+
+  /**
+   * {@link ComplexMetricSerde#deserializeColumn(ByteBuffer, ColumnBuilder, 
ColumnConfig)} should be used instead of this.
+   * This method is left for backward compatibility.
    */
+  @Deprecated
+  public void deserializeColumn(ByteBuffer buffer, ColumnBuilder builder)
+  {
+    // default implementation to match default serializer implementation
+    final int position = buffer.position();
+    final byte version = buffer.get();
+    if (version == CompressedComplexColumnSerializer.IS_COMPRESSED) {
+      CompressedComplexColumnSupplier supplier = 
CompressedComplexColumnSupplier.read(
+          buffer,
+          builder,
+          getTypeName(),
+          getObjectStrategy()
+      );
+      builder.setComplexColumnSupplier(supplier);
+      builder.setNullValueIndexSupplier(supplier.getNullValues());
+      builder.setHasNulls(!supplier.getNullValues().isEmpty());
+    } else {
+      buffer.position(position);
+      builder.setComplexColumnSupplier(
+          new ComplexColumnPartSupplier(
+              getTypeName(),
+              GenericIndexed.read(buffer, getObjectStrategy(), 
builder.getFileMapper())
+          )
+      );
+    }
+  }
+
+  /**
+   * {@link ComplexMetricSerde#getSerializer(SegmentWriteOutMedium, String, 
IndexSpec)} should be used instead of this.
+   * This method is left for backward compatibility.
+   */
+  @Nullable
+  @Deprecated
   public GenericColumnSerializer getSerializer(SegmentWriteOutMedium 
segmentWriteOutMedium, String column)
   {
-    return ComplexColumnSerializer.create(segmentWriteOutMedium, column, 
this.getObjectStrategy());
+    return null;
+  }
+
+  /**
+   * This method provides the ability for a ComplexMetricSerde to control its 
own serialization.
+   * Default implementation uses {@link CompressedComplexColumnSerializer} if 
{@link IndexSpec#complexMetricCompression}
+   * is not null or uncompressed/none, or {@link 
LargeColumnSupportedComplexColumnSerializer} if no compression is
+   * specified.
+   *
+   * @return an instance of {@link GenericColumnSerializer} used for 
serialization.
+   */
+  public GenericColumnSerializer getSerializer(
+      SegmentWriteOutMedium segmentWriteOutMedium,
+      String column,
+      IndexSpec indexSpec
+  )
+  {
+    // backwards compatibility, if defined use it
+    final GenericColumnSerializer serializer = 
getSerializer(segmentWriteOutMedium, column);
+    if (serializer != null) {
+      return serializer;
+    }
+
+    // otherwise, use compressed or generic indexed based serializer
+    CompressionStrategy strategy = indexSpec.getComplexMetricCompression();
+    if (strategy == null || CompressionStrategy.NONE == strategy || 
CompressionStrategy.UNCOMPRESSED == strategy) {
+      return LargeColumnSupportedComplexColumnSerializer.create(

Review Comment:
   The old code had a default of `ComplexColumnSerializer`. Why change that? 
(Is it strictly better? Are there compatibility concerns?)



##########
processing/src/main/java/org/apache/druid/segment/data/ObjectStrategy.java:
##########
@@ -59,6 +60,19 @@ default boolean canCompare()
     return true;
   }
 
+  /**
+   * Whether the {@link #fromByteBuffer(ByteBuffer, int)}, {@link 
#fromByteBufferWithSize(ByteBuffer)}, and
+   * {@link #fromByteBufferSafe(ByteBuffer, int)} methods return an object 
that may retain a reference to the provided
+   * {@link ByteBuffer}. If a reference is sometimes retained, this method 
returns true. It returns false if, and only

Review Comment:
   Should clarify: does this mean "retains a reference to the specific 
ByteBuffer object" or "retains a reference to the same underlying memory"?
   
   i.e., if an ObjectStrategy calls `duplicate()` on the buf and retains the 
duplicate, should it return `true` or `false` from this method?



##########
processing/src/main/java/org/apache/druid/segment/serde/CompressedComplexColumnSerializer.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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.segment.serde;
+
+import com.google.common.base.Preconditions;
+import org.apache.druid.collections.bitmap.ImmutableBitmap;
+import org.apache.druid.collections.bitmap.MutableBitmap;
+import org.apache.druid.java.util.common.io.smoosh.FileSmoosher;
+import org.apache.druid.segment.ColumnValueSelector;
+import org.apache.druid.segment.GenericColumnSerializer;
+import org.apache.druid.segment.IndexMerger;
+import org.apache.druid.segment.IndexSpec;
+import org.apache.druid.segment.data.ByteBufferWriter;
+import 
org.apache.druid.segment.data.CompressedVariableSizedBlobColumnSerializer;
+import org.apache.druid.segment.data.ObjectStrategy;
+import org.apache.druid.segment.nested.NestedCommonFormatColumnSerializer;
+import org.apache.druid.segment.nested.NestedDataColumnMetadata;
+import org.apache.druid.segment.nested.NestedDataComplexTypeSerde;
+import org.apache.druid.segment.writeout.SegmentWriteOutMedium;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.channels.WritableByteChannel;
+
+public class CompressedComplexColumnSerializer<T> implements 
GenericColumnSerializer<T>
+{
+  public static final byte IS_COMPRESSED = Byte.MAX_VALUE;
+  public static final byte V0 = 0x00;
+  public static final String FILE_NAME = "__complexColumn";
+
+  public static GenericColumnSerializer create(
+      SegmentWriteOutMedium segmentWriteOutMedium,
+      String column,
+      IndexSpec indexSpec,
+      ObjectStrategy objectStrategy
+  )
+  {
+    return new CompressedComplexColumnSerializer(column, 
segmentWriteOutMedium, indexSpec, objectStrategy);
+  }
+
+  public CompressedComplexColumnSerializer(
+      String name,
+      SegmentWriteOutMedium segmentWriteOutMedium,
+      IndexSpec indexSpec,
+      ObjectStrategy<T> strategy
+  )
+  {
+    this.name = name;
+    this.segmentWriteOutMedium = segmentWriteOutMedium;
+    this.indexSpec = indexSpec;
+    this.strategy = strategy;
+  }
+
+  private final String name;
+  private final SegmentWriteOutMedium segmentWriteOutMedium;
+  private final IndexSpec indexSpec;
+  private final ObjectStrategy<T> strategy;
+  private CompressedVariableSizedBlobColumnSerializer writer;
+  private ByteBufferWriter<ImmutableBitmap> nullBitmapWriter;
+  private MutableBitmap nullRowsBitmap;
+
+  private int rowCount = 0;
+  private boolean closedForWrite = false;
+  private byte[] metadataBytes;
+
+  @Override
+  public void open() throws IOException
+  {
+    writer = new CompressedVariableSizedBlobColumnSerializer(
+        NestedCommonFormatColumnSerializer.getInternalFileName(name, 
FILE_NAME),
+        segmentWriteOutMedium,
+        indexSpec.getComplexMetricCompression()
+    );
+    writer.open();
+
+    nullBitmapWriter = new ByteBufferWriter<>(
+        segmentWriteOutMedium,
+        indexSpec.getBitmapSerdeFactory().getObjectStrategy()
+    );
+    nullBitmapWriter.open();
+
+    nullRowsBitmap = 
indexSpec.getBitmapSerdeFactory().getBitmapFactory().makeEmptyMutableBitmap();
+  }
+
+  @Override
+  public void serialize(ColumnValueSelector<? extends T> selector) throws 
IOException
+  {
+    final T data = selector.getObject();
+    if (data == null) {
+      nullRowsBitmap.add(rowCount);
+    }
+    rowCount++;
+    final byte[] bytes = strategy.toBytes(data);
+    writer.addValue(bytes);
+  }
+
+  @Override
+  public long getSerializedSize() throws IOException
+  {
+    closeForWrite();
+    // COMPRESSED_BYTE + V0 + metadata
+    return 1 + 1 + metadataBytes.length;
+  }
+
+  @Override
+  public void writeTo(WritableByteChannel channel, FileSmoosher smoosher) 
throws IOException
+  {
+    Preconditions.checkState(closedForWrite, "Not closed yet!");
+
+    channel.write(ByteBuffer.wrap(new byte[]{IS_COMPRESSED}));
+    channel.write(ByteBuffer.wrap(new byte[]{V0}));
+    channel.write(ByteBuffer.wrap(metadataBytes));
+
+    NestedCommonFormatColumnSerializer.writeInternal(smoosher, writer, name, 
FILE_NAME);

Review Comment:
   perhaps move that `writeInternal` to a more neutral helper class? It seems 
odd for the complex column serializer to call into an internal method of nested 
column serializer. I mean, it's fine, just a little odd.



##########
processing/src/main/java/org/apache/druid/segment/nested/NestedDataColumnSerializer.java:
##########
@@ -394,18 +392,6 @@ private void closeForWrite() throws IOException
   {
     if (!closedForWrite) {
       closedForWrite = true;
-      ByteArrayOutputStream baos = new ByteArrayOutputStream();

Review Comment:
   was this dead code?



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