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


##########
processing/src/main/java/org/apache/druid/query/operator/window/value/WindowValueProcessorBase.java:
##########
@@ -108,4 +112,59 @@ public List<String> getOutputColumnNames()
   {
     return Collections.singletonList(outputColumn);
   }
+
+  /**
+   * Computes first or last value for each row respecting the given window 
frame. For each row, selects
+   * either the first or last element of the frame. Entries for rows where the 
frame is empty are left as null.
+   *
+   * @param first if true, selects the first element of the frame; if false, 
selects the last
+   */
+  static void computeFirstOrLastValueFramed(
+      final RowsAndColumns rac,
+      final ColumnAccessor accessor,
+      final WindowFrame frame,
+      final Object[] results,
+      final boolean first
+  )
+  {
+    final int numRows = accessor.numRows();
+    final WindowFrame.Rows rowsFrame = frame.unwrap(WindowFrame.Rows.class);
+    if (rowsFrame != null) {
+      final int lower = rowsFrame.getLowerOffsetClamped(numRows);
+      final int upper = rowsFrame.getUpperOffsetClamped(numRows);
+      for (int i = 0; i < numRows; i++) {
+        final int frameLower = Math.max(0, i + lower);
+        final int frameUpper = Math.min(numRows - 1, i + upper);
+        if (frameLower <= frameUpper) {
+          results[i] = accessor.getObject(first ? frameLower : frameUpper);
+        }
+      }
+      return;
+    }
+
+    final WindowFrame.Groups groupsFrame = 
frame.unwrap(WindowFrame.Groups.class);

Review Comment:
   Processing won't get this far due to validation. There's a test for it in 
`CalciteQueryTest#testUnSupportedRangeBounds`, expecting an error message like:
   
   > Order By with RANGE clause currently supports only UNBOUNDED or CURRENT 
ROW.
   
   I added a comment here about that.



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