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


##########
processing/src/main/java/org/apache/druid/query/rowsandcols/RowsAndColumns.java:
##########
@@ -110,6 +129,77 @@ static AppendableRowsAndColumns 
expectAppendable(RowsAndColumns input)
    * @return A concrete implementation of the interface, or null if there is 
no meaningful optimization to be had
    * through a local implementation of the interface.
    */
+  @SuppressWarnings("unchecked")
   @Nullable
-  <T> T as(Class<T> clazz);
+  default <T> T as(Class<T> clazz)
+  {
+    if (Arrays.asList(getClass().getInterfaces()).contains(clazz)) {
+      return (T) this;
+    }
+    Class<?> superClass = this.getClass().getSuperclass();
+    while (superClass != null) {
+      if (superClass.equals(clazz)) {
+        return (T) this;
+      }
+      superClass = superClass.getSuperclass();
+    }
+    return null;
+  }
+
+  /**
+   * 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, null, 
ByteTracker.unboundedTracker());
+
+      jsonGenerator.writeBinary(baos.toByteArray());
+    }
+  }
+
+  /**
+   * Deserializer for {@link RowsAndColumns} returning as an instance of 
{@link FrameRowsAndColumns}
+   */
+  class RowsAndColumnsDeserializer extends StdDeserializer<RowsAndColumns>
+  {
+    public RowsAndColumnsDeserializer()
+    {
+      super(RowsAndColumns.class);
+    }
+
+    @Override
+    public FrameRowsAndColumns deserialize(JsonParser jsonParser, 
DeserializationContext deserializationContext)
+        throws IOException
+    {
+      RowSignature sig = jsonParser.readValueAs(RowSignature.class);
+      jsonParser.nextValue();
+
+      ByteArrayOutputStream baos = new ByteArrayOutputStream();
+      jsonParser.readBinaryValue(baos);
+      Frame frame = Frame.wrap(baos.toByteArray());
+      return (frame.type() == FrameType.COLUMNAR)
+             ? new 
ColumnBasedFrameRowsAndColumns(Frame.wrap(baos.toByteArray()), sig)
+             : new RowBasedFrameRowsAndColumns(Frame.wrap(baos.toByteArray()), 
sig);

Review Comment:
    `Frame.wrap(baos.toByteArray())` is repeated 2 times; and executed 2 times 
if CSE does not eliminate it
   
   nit: use a full if as that's more readable



##########
processing/src/main/java/org/apache/druid/query/rowsandcols/RowsAndColumns.java:
##########
@@ -110,6 +129,77 @@ static AppendableRowsAndColumns 
expectAppendable(RowsAndColumns input)
    * @return A concrete implementation of the interface, or null if there is 
no meaningful optimization to be had
    * through a local implementation of the interface.
    */
+  @SuppressWarnings("unchecked")
   @Nullable
-  <T> T as(Class<T> clazz);
+  default <T> T as(Class<T> clazz)
+  {
+    if (Arrays.asList(getClass().getInterfaces()).contains(clazz)) {
+      return (T) this;
+    }
+    Class<?> superClass = this.getClass().getSuperclass();
+    while (superClass != null) {
+      if (superClass.equals(clazz)) {
+        return (T) this;
+      }
+      superClass = superClass.getSuperclass();
+    }
+    return null;
+  }
+
+  /**
+   * 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, null, 
ByteTracker.unboundedTracker());
+
+      jsonGenerator.writeBinary(baos.toByteArray());
+    }
+  }
+
+  /**
+   * Deserializer for {@link RowsAndColumns} returning as an instance of 
{@link FrameRowsAndColumns}
+   */
+  class RowsAndColumnsDeserializer extends StdDeserializer<RowsAndColumns>
+  {
+    public RowsAndColumnsDeserializer()
+    {
+      super(RowsAndColumns.class);
+    }
+
+    @Override
+    public FrameRowsAndColumns deserialize(JsonParser jsonParser, 
DeserializationContext deserializationContext)
+        throws IOException
+    {
+      RowSignature sig = jsonParser.readValueAs(RowSignature.class);
+      jsonParser.nextValue();
+
+      ByteArrayOutputStream baos = new ByteArrayOutputStream();
+      jsonParser.readBinaryValue(baos);
+      Frame frame = Frame.wrap(baos.toByteArray());
+      return (frame.type() == FrameType.COLUMNAR)
+             ? new 
ColumnBasedFrameRowsAndColumns(Frame.wrap(baos.toByteArray()), sig)
+             : new RowBasedFrameRowsAndColumns(Frame.wrap(baos.toByteArray()), 
sig);

Review Comment:
    `Frame.wrap(baos.toByteArray())` is repeated 2 times; and executed 2 times 
if CSE does not eliminate it
   
   nit: could use a full if as that's more readable



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