kgyrtkirk commented on code in PR #16658:
URL: https://github.com/apache/druid/pull/16658#discussion_r1745278652


##########
processing/src/main/java/org/apache/druid/query/rowsandcols/concrete/FrameRowsAndColumns.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.query.rowsandcols.concrete;
+
+import com.google.common.base.Objects;
+import org.apache.druid.frame.Frame;
+import org.apache.druid.frame.read.FrameReader;
+import org.apache.druid.frame.segment.FrameStorageAdapter;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.query.rowsandcols.RowsAndColumns;
+import org.apache.druid.query.rowsandcols.column.Column;
+import org.apache.druid.segment.CloseableShapeshifter;
+import org.apache.druid.segment.StorageAdapter;
+import org.apache.druid.segment.column.RowSignature;
+
+import javax.annotation.Nullable;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+
+public abstract class FrameRowsAndColumns implements RowsAndColumns, 
AutoCloseable, CloseableShapeshifter
+{
+  final Frame frame;
+  final RowSignature signature;
+  final LinkedHashMap<String, Column> colCache = new LinkedHashMap<>();
+
+  public FrameRowsAndColumns(Frame frame, RowSignature signature)
+  {
+    this.frame = frame;
+    this.signature = signature;
+  }
+
+  public Frame getFrame()
+  {
+    return frame;
+  }
+
+  public RowSignature getSignature()
+  {
+    return signature;
+  }
+
+  @Override
+  public Collection<String> getColumnNames()
+  {
+    return signature.getColumnNames();
+  }
+
+  @Override
+  public int numRows()
+  {
+    return frame.numRows();
+  }
+
+  @SuppressWarnings("unchecked")
+  @Nullable
+  @Override
+  public <T> T as(Class<T> clazz)
+  {
+    if (StorageAdapter.class.equals(clazz)) {
+      return (T) new FrameStorageAdapter(frame, FrameReader.create(signature), 
Intervals.ETERNITY);
+    }
+    if (FrameRowsAndColumns.class.equals(clazz)) {
+      return (T) this;
+    }
+    return null;

Review Comment:
   note: the default implementation of `as` could be something like:
   ```
   if(getClass().getInterfaces().contains(clazz)) return (T)this;
   ```
   so that instead of this; and the `return null` => `return super.as(clazz)` 
could be used to invoke that logic



##########
processing/src/main/java/org/apache/druid/jackson/DruidDefaultSerializersModule.java:
##########
@@ -189,20 +188,7 @@ public ByteOrder deserialize(JsonParser jp, 
DeserializationContext ctxt) throws
     );
     addDeserializer(ResponseContext.class, new ResponseContextDeserializer());
 
-    addSerializer(RowsAndColumns.class, new JsonSerializer<RowsAndColumns>()
-    {
-      @Override
-      public void serialize(
-          RowsAndColumns value,
-          JsonGenerator gen,
-          SerializerProvider serializers
-      ) throws IOException
-      {
-        // It would be really cool if jackson offered an output stream that 
would allow us to push bytes
-        // through, but it doesn't right now, so we have to build a byte[] 
instead.  Maybe something to contribute
-        // back to Jackson at some point.
-        gen.writeBinary(WireTransferable.fromRAC(value).bytesToTransfer());

Review Comment:
   can we retire `WireTransferable` ?
   with these changes it will be only used in 
[WindowOperatorQueryQueryRunnerFactory](https://github.com/apache/druid/blob/40f38f0191dcd03b755224c2fb604ad732c7a8c7/processing/src/main/java/org/apache/druid/query/operator/WindowOperatorQueryQueryRunnerFactory.java#L107-L115)
 
   but the usecase there seem to closely match 
`rac.as(FrameRowsAndColumns.class)` or not?



##########
processing/src/main/java/org/apache/druid/query/rowsandcols/RowsAndColumns.java:
##########
@@ -112,4 +131,66 @@ static AppendableRowsAndColumns 
expectAppendable(RowsAndColumns input)
    */
   @Nullable
   <T> T as(Class<T> clazz);
+
+  /**
+   * Serializer for {@link RowsAndColumns} by converting the instance to 
{@link FrameRowsAndColumns}
+   */
+  class RowsAndColumnsSerializer extends StdSerializer<RowsAndColumns>
+  {
+    public RowsAndColumnsSerializer()
+    {
+      super(RowsAndColumns.class);
+    }
+
+    @Override
+    public void serialize(
+        RowsAndColumns rac,
+        JsonGenerator jsonGenerator,
+        SerializerProvider serializerProvider
+    ) throws IOException
+    {
+      FrameRowsAndColumns frameRAC = rac.as(FrameRowsAndColumns.class);
+      if (frameRAC == null) {
+        throw DruidException.defensive("Unable to serialize RAC");
+      }
+      JacksonUtils.writeObjectUsingSerializerProvider(jsonGenerator, 
serializerProvider, frameRAC.getSignature());
+
+      Frame frame = frameRAC.getFrame();
+      final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+      frame.writeTo(
+          Channels.newChannel(baos),
+          false,
+          ByteBuffer.allocate(Frame.compressionBufferSize((int) 
frame.numBytes())),

Review Comment:
   `compression` is false; meanwhile a `compressionBuffer` is being allocated 
at every call - is that required?
   if its needed - would it be possible to reuse the buffer later? 



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