morrySnow commented on code in PR #64366:
URL: https://github.com/apache/doris/pull/64366#discussion_r3518728493


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java:
##########
@@ -547,16 +566,97 @@ public Expression visitTryCast(TryCast cast, 
ExpressionRewriteContext context) {
         }
     }
 
+    @Override
+    public Expression visitNow(Now now, ExpressionRewriteContext context) {
+        now = rewriteChildren(now, context);
+        Optional<Expression> checkedExpr = preProcess(now);
+        if (checkedExpr.isPresent()) {
+            return checkedExpr.get();
+        }
+        Optional<Instant> statementStartTime = getStatementStartTime(context);
+        if (!statementStartTime.isPresent()) {
+            return now;
+        }
+        int precision = now.arity() == 0 ? 0
+                : ((IntegerLiteral) now.child(0)).getValue();
+        return DateTimeV2Literal.fromJavaDateType(
+                LocalDateTime.ofInstant(statementStartTime.get(), 
DateUtils.getTimeZone()),
+                precision);
+    }
+
     @Override
     public Expression visitBoundFunction(BoundFunction boundFunction, 
ExpressionRewriteContext context) {
         boundFunction = rewriteChildren(boundFunction, context);
         Optional<Expression> checkedExpr = preProcess(boundFunction);
         if (checkedExpr.isPresent()) {
             return checkedExpr.get();
         }
+        Optional<Expression> statementTimeExpression = 
evaluateStatementTimeFunction(boundFunction, context);
+        if (statementTimeExpression.isPresent()) {
+            return statementTimeExpression.get();
+        }
         return ExpressionEvaluator.INSTANCE.eval(boundFunction);
     }
 
+    private Optional<Expression> evaluateStatementTimeFunction(BoundFunction 
function,
+            ExpressionRewriteContext context) {
+        if (!isStatementTimeFunction(function)) {
+            return Optional.empty();
+        }
+        Optional<Instant> statementStartTime = getStatementStartTime(context);
+        if (!statementStartTime.isPresent()) {
+            return Optional.of(function);
+        }
+
+        Instant instant = statementStartTime.get();
+        if (function instanceof CurrentDate) {
+            return Optional.of(DateV2Literal.fromJavaDateType(
+                    LocalDateTime.ofInstant(instant, 
DateUtils.getTimeZone())));
+        } else if (function instanceof CurrentTime) {
+            LocalDateTime dateTime = LocalDateTime.ofInstant(instant, 
DateUtils.getTimeZone());
+            if (function.arity() == 0) {
+                return Optional.of(TimeV2Literal.fromJavaDateType(dateTime));
+            }
+            return Optional.of(TimeV2Literal.fromJavaDateType(dateTime,
+                    ((TinyIntLiteral) function.child(0)).getValue()));

Review Comment:
   Fixed in 3bd0f4ebf16. I changed FE folding to read precision only when the 
child is a supported literal. For `CurrentTime`, non-`TinyIntLiteral` precision 
arguments such as `NullLiteral(TINYINT)` now keep the expression unfolded 
instead of casting and throwing. I also added a regression test for 
`current_time(NULL)` / `curtime(NULL)` style precision handling.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/util/DateUtils.java:
##########
@@ -404,9 +405,9 @@ public static int getHourOrDefault(final TemporalAccessor 
accessor) {
 
     public static ZoneId getTimeZone() {
         if (ConnectContext.get() == null || 
ConnectContext.get().getSessionVariable() == null) {
-            return ZoneId.systemDefault();
+            return TimeUtils.getSystemTimeZone().toZoneId();
         }
-        return 
ZoneId.of(ConnectContext.get().getSessionVariable().getTimeZone());
+        return TimeUtils.getTimeZone().toZoneId();

Review Comment:
   Fixed in 3bd0f4ebf16. I restricted FE statement-time timezone resolution to 
the aliases BE can evaluate consistently. `DateUtils.getTimeZone()` now 
resolves the session timezone with a BE-compatible alias map instead of Java 
`SHORT_IDS`; unsupported aliases such as `PST` make FE statement-time folding 
return the original expression, so BE remains the single evaluator. I added a 
test that verifies `now()` and `current_time()` are not FE-folded under `PST`.



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