FrankChen021 commented on code in PR #19386:
URL: https://github.com/apache/druid/pull/19386#discussion_r3189389262
##########
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, the selector constant-folding path I called out is fixed now:
`now()` carries a non-deterministic analysis flag and `ExpressionSelectors`
skips `ConstantExprEvalSelector` for it. I think two related constant/cache
paths still need the same treatment, though. `Expr.asBitmapColumnIndex` still
treats any expression with no required bindings as a constant bitmap index, so
an expression filter around `now()` can still be evaluated once while building
the bitmap. Also, `ExpressionDimFilter` and `ExpressionVirtualColumn` cache
keys still serialize the stable expression cache key (`now()` stringifies the
same way every time), so cached query results can be reused across wall-clock
changes. Could you either make non-deterministic expressions non-cacheable for
these query components, or include a non-stable cache key / disable the
affected optimizations when `analysis.isNonDeterministic()` is true?
--
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]