LakshSingla commented on code in PR #16707:
URL: https://github.com/apache/druid/pull/16707#discussion_r1695467166


##########
processing/src/main/java/org/apache/druid/frame/field/LongFieldReader.java:
##########
@@ -99,4 +107,135 @@ public boolean isNull()
       return super._isNull();
     }
   }
+
+  @Override
+  public Column makeRACColumn(Frame frame, RowSignature signature, String 
columnName)
+  {
+    return new LongFieldReaderColumn(frame, signature.indexOf(columnName), 
signature.size());
+  }
+
+  private class LongFieldReaderColumn implements Column
+  {
+    private final Frame frame;
+    private final Memory dataRegion;
+    private final FieldPositionHelper coach;
+
+    public LongFieldReaderColumn(Frame frame, int columnIndex, int numFields)
+    {
+      this.frame = frame;
+      this.dataRegion = frame.region(RowBasedFrameWriter.ROW_DATA_REGION);
+
+      this.coach = new FieldPositionHelper(
+          frame,
+          frame.region(RowBasedFrameWriter.ROW_OFFSET_REGION),
+          dataRegion,
+          columnIndex,
+          numFields
+      );
+    }
+
+    @Nonnull
+    @Override
+    public ColumnAccessor toAccessor()
+    {
+      return new ColumnAccessor()
+      {
+        @Override
+        public ColumnType getType()
+        {
+          return ColumnType.LONG;
+        }
+
+        @Override
+        public int numRows()
+        {
+          return frame.numRows();
+        }
+
+        @Override
+        public boolean isNull(int rowNum)
+        {
+          final long fieldPosition = coach.computeFieldPosition(rowNum);
+          return dataRegion.getByte(fieldPosition) == getNullIndicatorByte();
+        }
+
+        @Nullable
+        @Override
+        public Object getObject(int rowNum)
+        {
+          final long fieldPosition = coach.computeFieldPosition(rowNum);
+
+          if (dataRegion.getByte(fieldPosition) == getNullIndicatorByte()) {
+            return null;
+          } else {
+            return getLongAtPosition(fieldPosition);
+          }
+        }
+
+        @Override
+        public double getDouble(int rowNum)
+        {
+          return getLong(rowNum);
+        }
+
+        @Override
+        public float getFloat(int rowNum)
+        {
+          return getLong(rowNum);
+        }
+
+        @Override
+        public long getLong(int rowNum)
+        {
+          final long fieldPosition = coach.computeFieldPosition(rowNum);
+
+          if (dataRegion.getByte(fieldPosition) == getNullIndicatorByte()) {
+            return 0L;
+          } else {
+            return getLongAtPosition(fieldPosition);
+          }
+        }
+
+        @Override
+        public int getInt(int rowNum)
+        {
+          return (int) getLong(rowNum);
+        }
+
+        @Override
+        public int compareRows(int lhsRowNum, int rhsRowNum)
+        {
+          long lhsPosition = coach.computeFieldPosition(lhsRowNum);
+          long rhsPosition = coach.computeFieldPosition(rhsRowNum);
+
+          final byte nullIndicatorByte = getNullIndicatorByte();
+          if (dataRegion.getByte(lhsPosition) == nullIndicatorByte) {
+            if (dataRegion.getByte(rhsPosition) == nullIndicatorByte) {
+              return 0;
+            } else {
+              return -1;
+            }
+          } else {
+            if (dataRegion.getByte(rhsPosition) == nullIndicatorByte) {
+              return 1;
+            } else {
+              return Long.compare(getLongAtPosition(lhsPosition), 
getLongAtPosition(rhsPosition));
+            }
+          }
+        }

Review Comment:
   If two fields of the same type are written in the frame - a represented as 
[a0...aM] and b represented as [b0..bN] then if (a > b), then the bytewise 
comparison of a's representation will be lexicographically greater than the 
bytewise representation of b. Therefore, if a > b, a0 = b0, a1 == b1, ... ai == 
bi, a(i+1) > b(i+1)
   Note, this only applies to primitive types and arrays of primitives.
   
   We use this property in multiple places, check out one of the 
implementations here - 
https://github.com/apache/druid/blob/master/processing/src/main/java/org/apache/druid/frame/read/FrameReaderUtils.java#L173-L173.
   
   in the given code, you can compare using something like:
   ```java
    for (int i = 0; i < Math.min(fieldLength1, fieldLength2); ++i) {
      int cmp = compareBytesUnsigned(dataRegion.getByte(fieldPosition1 + i), 
dataRegion.getByte(fieldPosition2 + i);
      if (cmp != 0) return cmp;
    }
   ```
   `fieldLength1` & `fieldLength2` is something you can compute by subtracting 
the starting position of current field from the starting position of the next 
field (or the row end).  There are pre-existing utility methods that can 
achieve this - checkout the classes `ReadableFieldPointer` and 
`RowMemoryFieldPointer`. You can check those as well, I think some of the work 
done by the `FieldPositionHelper` is duplicated in those classes, albeit in a 
non-roundabout way. 
   
   LMK if there's more I can explain up on. 



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