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


##########
processing/src/main/java/org/apache/druid/math/expr/BuiltInExprMacros.java:
##########
@@ -214,4 +214,95 @@ public Object getLiteralValue()
     }
   }
 
+  /**
+   * Expression macro for now() function that returns current system timestamp.
+   * Implemented as a macro to prevent constant folding optimization.
+   */
+  public static class NowExprMacro implements ExprMacroTable.ExprMacro
+  {
+    public static final String NAME = "now";
+
+    @Override
+    public String name()
+    {
+      return NAME;
+    }
+
+    @Override
+    public Expr apply(List<Expr> args)
+    {
+      validationHelperCheckArgumentCount(args, 0);
+      return new NowExpression();
+    }
+
+    static final class NowExpression implements Expr
+    {
+      @Override
+      public ExprEval eval(ObjectBinding bindings)
+      {
+        return ExprEval.ofLong(System.currentTimeMillis());
+      }
+
+      @Override
+      public String stringify()
+      {
+        return "now()";
+      }
+
+      @Override
+      public Expr visit(Shuttle shuttle)
+      {
+        return shuttle.visit(this);
+      }
+
+      @Override
+      public BindingAnalysis analyzeInputs()
+      {
+        // Return analysis indicating this is NOT constant
+        // by pretending we have a free variable
+        return new BindingAnalysis();

Review Comment:
   Thanks, this fixes the `ExpressionPlan` constant trait, but I think the 
selector path can still fold `now()` once. `NowExpression.analyzeInputs()` now 
reports only the synthetic `__dummy__` binding; for normal segments that column 
has no capabilities and its selector is nil, so 
`ExpressionSelectors.createBindings` drops it and returns 
`InputBindings.nilBindings()`. `makeExprEvalSelector` then treats those nil 
bindings as a constant expression and returns `new 
ConstantExprEvalSelector(plan.getExpression().eval(bindings))`, so `now()` is 
still evaluated when the selector is created instead of at row/query processing 
time. The new tests cover the planner only; could you add selector-level 
coverage and avoid using a missing dummy input as the non-determinism marker?



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