mihaibudiu commented on code in PR #5178:
URL: https://github.com/apache/calcite/pull/5178#discussion_r3792594696


##########
core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java:
##########
@@ -2491,12 +2490,107 @@ protected FirstLastValueImplementor(SeekType seekType) 
{
         AggResultContext result) {
       WinAggResultContext winResult = (WinAggResultContext) result;
 
+      final boolean ignoreNulls =
+          info instanceof WinAggContext && ((WinAggContext) 
info).ignoreNulls();
+      if (ignoreNulls) {
+        return implementResultIgnoreNulls(info, winResult);
+      }
+
       return Expressions.condition(winResult.hasRows(),
           winResult.rowTranslator(
               winResult.computeIndex(Expressions.constant(0), seekType))
               .translate(winResult.rexArguments().get(0), info.returnType()),
           getDefaultValue(info.returnType()));
     }
+
+    /**
+     * Implements FIRST_VALUE / LAST_VALUE with IGNORE NULLS by scanning the
+     * frame (forward for FIRST_VALUE, backward for LAST_VALUE) and returning
+     * the first non-null argument value, or null if all rows in the frame are
+     * null (or the frame is empty).
+     *
+     * <p>Generated code (for FIRST_VALUE; LAST_VALUE scans backward):
+     * <pre>{@code
+     *   BoxType res = null;
+     *   if (hasRows) {
+     *     for (int seekIdx = startIndex; seekIdx <= endIndex; seekIdx++) {
+     *       BoxType seekValue = rowTranslator.translate(arg, boxType);
+     *       if (seekValue != null) {
+     *         res = seekValue;
+     *         break;
+     *       }
+     *     }
+     *   }
+     *   return res;
+     * }</pre>
+     */
+    private Expression implementResultIgnoreNulls(AggContext info,
+        WinAggResultContext winResult) {
+      final Type returnType = info.returnType();
+      final RexNode arg = winResult.rexArguments().get(0);
+
+      // Use a boxed type internally so that a NULL comparison is always valid,
+      // even when the (frame-guaranteed non-empty) return type is a primitive.
+      // The surrounding window implementation converts the result back to the
+      // declared return type.
+      final Type boxType = Types.box(returnType);
+
+      final ParameterExpression res =
+          Expressions.parameter(0, boxType,
+              winResult.currentBlock().newName(
+                  seekType == SeekType.START ? "first_value" : "last_value"));
+      // res = null
+      winResult.currentBlock().add(Expressions.declare(0, res, NULL_EXPR));
+
+      final ParameterExpression idx =
+          Expressions.parameter(int.class,
+              winResult.currentBlock().newName("seekIdx"));
+
+      // Scan direction: FIRST_VALUE walks from start to end, LAST_VALUE walks
+      // from end back to start.
+      final boolean forward = seekType == SeekType.START;
+      final Expression from =
+          forward ? winResult.startIndex() : winResult.endIndex();
+      final Expression to =
+          forward ? winResult.endIndex() : winResult.startIndex();
+      final Expression condition =
+          forward
+              ? Expressions.lessThanOrEqual(idx, to)
+              : Expressions.greaterThanOrEqual(idx, to);
+      final Expression post =
+          forward
+              ? Expressions.postIncrementAssign(idx)
+              : Expressions.postDecrementAssign(idx);
+
+      // Build the loop body:
+      //   BoxType seekValue = rowTranslator.translate(arg, boxType);
+      //   if (seekValue != null) {
+      //     res = seekValue;
+      //     break;
+      //   }
+      final BlockBuilder loopBody = winResult.nestBlock();
+      final Expression value =
+          winResult.rowTranslator(idx).translate(arg, boxType);
+      final ParameterExpression valueVar =
+          Expressions.parameter(0, boxType, loopBody.newName("seekValue"));
+      loopBody.add(Expressions.declare(0, valueVar, value));
+      loopBody.add(
+          Expressions.ifThen(
+              Expressions.notEqual(valueVar, NULL_EXPR),
+              Expressions.block(
+                  Expressions.statement(Expressions.assign(res, valueVar)),
+                  Expressions.break_(null))));
+      winResult.exitBlock();
+      final BlockStatement loopBodyBlock = loopBody.toBlock();
+
+      // Wrap the scan in: if (hasRows) { for (...) { ... } }
+      winResult.currentBlock().add(
+          Expressions.ifThen(winResult.hasRows(),
+              Expressions.for_(
+                  Expressions.declare(0, idx, from),

Review Comment:
   I was curious about what this for loop iterates over for unbounded ranges. 
What are 'from', 'condition' and 'post' for unbounded ranges?



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

Reply via email to