FrankChen021 commented on code in PR #19386:
URL: https://github.com/apache/druid/pull/19386#discussion_r3161336330
##########
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:
[P2] Prevent now() from being planned as constant
Returning an empty BindingAnalysis makes ExpressionPlanner mark now() and
expressions that only depend on now() as CONSTANT. Query-time expression
selectors then build a ConstantExprEvalSelector and evaluate now() once when
the selector is created, and expression filters/virtual columns also get stable
cache keys, so native queries can reuse stale timestamps instead of evaluating
at processing time. Add explicit non-deterministic handling, or otherwise keep
now() out of constant selector/index/cache optimizations.
--
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]