FrankChen021 commented on code in PR #20090:
URL: https://github.com/apache/druid/pull/20090#discussion_r4082475974


##########
processing/src/main/java/org/apache/druid/segment/data/ObjectStrategy.java:
##########
@@ -52,6 +52,25 @@ public interface ObjectStrategy<T> extends Comparator<T>
   @Nullable
   byte[] toBytes(@Nullable T val);
 
+  /**
+   * Converts an Object to bytes, handling the case where the input is already 
a byte[].
+   * This is needed when complex types are read from realtime segments, where
+   * {@link org.apache.druid.segment.ColumnValueSelector#getObject()} returns 
the already-serialized byte[] form
+   * rather than the deserialized object form. In that case, we can return the 
byte[] directly instead of
+   * trying to cast it to {@code T} and re-serialize it, which would cause a 
{@link ClassCastException}.
+   *
+   * @see <a href="https://github.com/apache/druid/issues/18340";>GitHub issue 
#18340</a>
+   */
+  @Nullable
+  default byte[] objectToBytes(@Nullable Object val)
+  {
+    if (val instanceof byte[]) {

Review Comment:
   [P2] Preserve byte[] object-strategy semantics
   
   **Finding:** This unconditional byte[] shortcut is now used by every 
ComplexMetricSerde, so a custom complex type whose legitimate intermediate 
object is byte[] (or whose strategy accepts byte[] and adds an envelope, 
compression, or type marker) silently skips its toBytes implementation. 
ComplexFieldWriter and ComplexFrameColumnWriter will persist the raw array, and 
the matching fromByteBuffer implementation can then fail or read a different 
value, breaking existing extension types.
   
   **Suggestion:** Make the already-serialized path explicit or guard it using 
the strategy's declared object class, and add a regression test for a 
byte[]-backed strategy whose toBytes method transforms the input.



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