github-actions[bot] commented on code in PR #66795:
URL: https://github.com/apache/doris/pull/66795#discussion_r4638155791


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SessionVarGuardRewriter.java:
##########
@@ -17,29 +17,69 @@
 
 package org.apache.doris.nereids.rules.analysis;
 
+import org.apache.doris.analysis.BinaryPredicate;
+import org.apache.doris.analysis.CastExpr;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.FunctionCallExpr;
+import org.apache.doris.analysis.IsNullPredicate;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotRef;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.common.util.DigitalVersion;
 import org.apache.doris.nereids.CascadesContext;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.exceptions.UnboundException;
 import org.apache.doris.nereids.pattern.Pattern;
 import org.apache.doris.nereids.rules.Rule;
-import org.apache.doris.nereids.rules.expression.ExpressionPatternMatcher;
-import org.apache.doris.nereids.rules.expression.ExpressionPatternRuleFactory;
 import org.apache.doris.nereids.rules.expression.ExpressionRewrite;
+import org.apache.doris.nereids.rules.expression.ExpressionRewriteContext;
+import org.apache.doris.nereids.rules.expression.ExpressionRewriteRule;
 import org.apache.doris.nereids.rules.expression.ExpressionRuleExecutor;
-import org.apache.doris.nereids.rules.expression.ExpressionRuleType;
-import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.And;
+import org.apache.doris.nereids.trees.expressions.Between;
+import org.apache.doris.nereids.trees.expressions.ComparisonPredicate;
 import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.InPredicate;
+import org.apache.doris.nereids.trees.expressions.IsNull;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
 import org.apache.doris.nereids.trees.expressions.NeedSessionVarGuard;
+import org.apache.doris.nereids.trees.expressions.Not;
+import org.apache.doris.nereids.trees.expressions.Or;
+import org.apache.doris.nereids.trees.expressions.OrderExpression;
 import org.apache.doris.nereids.trees.expressions.SessionVarGuardExpr;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.WindowExpression;
+import org.apache.doris.nereids.trees.expressions.WindowFrame;
+import 
org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
+import 
org.apache.doris.nereids.trees.expressions.functions.generator.TableGeneratingFunction;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.Lambda;

Review Comment:
   [P1] Guard offset-free TIMESTAMPTZ literals in predicates. Type coercion 
builds a TIMESTAMPTZ literal with TimestampTzLiteral.fromSessionTimeZone, so an 
offset-free bound such as ts < '2024-01-01 00:00:00' becomes a different UTC 
instant in each query session. The new isTimeZoneSensitive() returns false for 
every Literal and skips ComparisonPredicate, so an MTMV cached in +08:00 can 
reuse its creation-zone row set for a UTC query instead of evaluating the UTC 
bound. Distinguish literals parsed with an explicit offset from session-local 
literals and guard the latter, with a cross-zone predicate regression.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SessionVarGuardRewriter.java:
##########
@@ -74,55 +162,205 @@ private Plan rewritePlanNode(Plan plan) {
         return plan;
     }
 
-    private static class ReplaceRule implements ExpressionPatternRuleFactory {
+    /**
+     * Applies {@link AddSessionVarGuardRewriter} to the whole expression 
tree, so that non-Alias
+     * expressions (e.g. filter predicates, join conjuncts) are guarded as 
well as alias children.
+     */
+    private static class AddGuardExpressionRewriteRule implements 
ExpressionRewriteRule<ExpressionRewriteContext> {
         private final AddSessionVarGuardRewriter addGuardRewriter;
 
-        private ReplaceRule(AddSessionVarGuardRewriter guard) {
-            this.addGuardRewriter = guard;
+        private AddGuardExpressionRewriteRule(AddSessionVarGuardRewriter 
addGuardRewriter) {
+            this.addGuardRewriter = addGuardRewriter;
         }
 
         @Override
-        public List<ExpressionPatternMatcher<? extends Expression>> 
buildRules() {
-            return ImmutableList.of(
-                    matchesType(Alias.class).thenApply(ctx -> {
-                        Alias alias = ctx.expr;
-                        Expression aliasChild = 
alias.child().accept(addGuardRewriter, Boolean.FALSE);
-                        return 
alias.withChildren(ImmutableList.of(aliasChild));
-                    }).toRule(ExpressionRuleType.ADD_SESSION_VAR_GUARD)
-            );
+        public Expression rewrite(Expression expr, ExpressionRewriteContext 
ctx) {
+            return expr.accept(addGuardRewriter, Boolean.FALSE);
         }
     }
 
-    /** This ensures that all expressions implementing NeedSessionVarGuard are
-     * wrapped in a SessionVarGuardExpr layer.
-     * e.g. (a+b)*c -> guard(guard(a+b)*c)
-     * */
+    /**
+     * Wraps expressions whose value depends on session variables (or on the 
session time zone) in a
+     * {@link SessionVarGuardExpr} when the relevant session variables differ 
from the ones persisted on
+     * the object (view / materialized view / generated column) being 
processed.
+     */
     public static class AddSessionVarGuardRewriter extends 
DefaultExpressionRewriter<Boolean> {
         private final Map<String, String> sessionVar;
+        // Whether the time-zone family (time-zone sensitive expressions) must 
be guarded: the creation
+        // time zone differs from the current one, or the persisted map does 
not carry time_zone at all
+        // (pre-time_zone metadata), so the creation zone is unknown and must 
be treated as different.
+        private final boolean timeZoneDiffersOrUnknown;
+        // Whether the "other" guard family (NeedSessionVarGuard expressions, 
e.g. decimal256 dependent)
+        // must be guarded: some affectQueryResult session variable other than 
time_zone differs.
+        private final boolean otherSessionVarsDiffer;
+        // True when the guards are added to a shared materialized-view 
rewrite cache (MTMVCache.from).
+        // Cache-mismatch guards must stay structurally distinct from the 
guards BindRelation adds when
+        // expanding a persisted object into the query, so that pre-RBO 
expression matching never equates a
+        // query-side nested-object guard with the cache guard of an MTMV 
materialized in another zone.
+        private final boolean cacheGuard;
 
+        /**
+         * Creates a guard rewriter that guards both dependency families 
unconditionally for the persisted
+         * variables {@code var}. The guard decision must not depend on the 
current thread-local session:
+         * the only production caller builds this rewriter inside an {@code 
AutoCloseSessionVariable} scope
+         * where the current session already equals {@code var} (so deriving 
the decision from the
+         * thread-local session would add no guard at all), while the wrapped 
expression is later
+         * translated/executed in a different (load) session.
+         *
+         * @param var the persisted session variables of the object being 
processed
+         */
         public AddSessionVarGuardRewriter(Map<String, String> var) {
-            sessionVar = var;
+            this(var, true, true, false);
+        }
+
+        /**
+         * Creates a guard rewriter for the persisted session variables {@code 
var} against the current
+         * query session variables {@code currentVars}.
+         *
+         * @param var the persisted session variables of the object being 
processed
+         * @param currentVars the current query session's 
affectQueryResultInPlan variables
+         */
+        public AddSessionVarGuardRewriter(Map<String, String> var, Map<String, 
String> currentVars) {
+            this(var,
+                    // The creation zone is unknown when the persisted map is 
empty (pre-change metadata)
+                    // or has no time_zone key (metadata written before 
time_zone became a plan variable);
+                    // treat it as different so time-zone sensitive 
expressions are always guarded
+                    // (compatibility fence). The empty captured map is 
transparent at execution.
+                    var != null && (var.isEmpty() || 
!var.containsKey(SessionVariable.TIME_ZONE)
+                            || 
!timeZonesEquivalent(var.get(SessionVariable.TIME_ZONE),
+                                    currentVars == null ? null : 
currentVars.get(SessionVariable.TIME_ZONE))),
+                    // An empty map records no variable at all, so the 
creation session's other variables are
+                    // unknown as well and their dependency family has to be 
guarded too; a map that only lacks
+                    // time_zone still carries them and can be compared 
exactly.
+                    var != null && (var.isEmpty() || 
differsInNonTimeZoneVars(currentVars, var)),
+                    false);
+        }
+
+        /**
+         * Creates a guard rewriter that guards the families selected by 
{@code guardMask} unconditionally.
+         * Used when building a shared rewrite cache: the guarded cache must 
contain the guards regardless
+         * of the session it is generated in, so a cache built in the creation 
zone (e.g. by a background
+         * refresh task) is still effective for a query in a different zone. 
The produced guards are cache
+         * guards (see {@link #cacheGuard}) so they never equal a query-side 
nested-object guard.
+         *
+         * @param var the persisted session variables of the object being 
processed
+         * @param guardMask combination of {@link #GUARD_TIME_ZONE} and {@link 
#GUARD_OTHER}
+         */
+        public AddSessionVarGuardRewriter(Map<String, String> var, int 
guardMask) {

Review Comment:
   [P1] Fail closed for registered frontends with unknown versions. 
Frontend.version is populated by heartbeat, so a newly registered or down FE 
can be null while checkClusterSupportsPlanTimeZoneFence treats it as safe. The 
current FE can then persist time_zone/fence metadata that an older binary later 
replays and serves without guards. Count null/unparseable registered members as 
incompatible (or use an explicit capability) before allowing fenced DDL.



##########
fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVPartitionExprDateTrunc.java:
##########
@@ -342,9 +343,18 @@ private String dateTimeToStr(DateTimeV2Literal literal,
             // Emit an explicit +00:00 suffix so that downstream consumers
             // (PartitionKey.createPartitionKey -> 
TimestampTzLiteral.fromSessionTimeZone)
             // interpret the value as UTC rather than session-local time.
-            return String.format(PartitionExprUtil.DATETIME_FORMATTER,
+            // The fractional-second digits must match 
DateLiteral#getStringValue() for TIMESTAMPTZ,
+            // otherwise the roll-up partition key desc is not string-equal to 
the desc of the

Review Comment:
   [P1] Preserve legacy TIMESTAMPTZ partition-bound representations during 
refresh validation. Older MTMVs store bounds such as ...00+00:00, while this 
formatter now regenerates ...00.000000+00:00 for TIMESTAMPTZ(6). 
PartitionKeyDesc equality compares those raw strings, and RefreshMTMVInfo 
rejects an explicit request for the real legacy physical name when its 
descriptor is absent from the regenerated set, even though the base partition 
still exists. Normalize bounds by typed PartitionKey semantics or 
migrate/accept the legacy descriptor before the stale-name check, and add an 
upgrade-style manual-refresh regression.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SessionVarGuardRewriter.java:
##########
@@ -74,55 +162,205 @@ private Plan rewritePlanNode(Plan plan) {
         return plan;
     }
 
-    private static class ReplaceRule implements ExpressionPatternRuleFactory {
+    /**
+     * Applies {@link AddSessionVarGuardRewriter} to the whole expression 
tree, so that non-Alias
+     * expressions (e.g. filter predicates, join conjuncts) are guarded as 
well as alias children.
+     */
+    private static class AddGuardExpressionRewriteRule implements 
ExpressionRewriteRule<ExpressionRewriteContext> {
         private final AddSessionVarGuardRewriter addGuardRewriter;
 
-        private ReplaceRule(AddSessionVarGuardRewriter guard) {
-            this.addGuardRewriter = guard;
+        private AddGuardExpressionRewriteRule(AddSessionVarGuardRewriter 
addGuardRewriter) {
+            this.addGuardRewriter = addGuardRewriter;
         }
 
         @Override
-        public List<ExpressionPatternMatcher<? extends Expression>> 
buildRules() {
-            return ImmutableList.of(
-                    matchesType(Alias.class).thenApply(ctx -> {
-                        Alias alias = ctx.expr;
-                        Expression aliasChild = 
alias.child().accept(addGuardRewriter, Boolean.FALSE);
-                        return 
alias.withChildren(ImmutableList.of(aliasChild));
-                    }).toRule(ExpressionRuleType.ADD_SESSION_VAR_GUARD)
-            );
+        public Expression rewrite(Expression expr, ExpressionRewriteContext 
ctx) {
+            return expr.accept(addGuardRewriter, Boolean.FALSE);
         }
     }
 
-    /** This ensures that all expressions implementing NeedSessionVarGuard are
-     * wrapped in a SessionVarGuardExpr layer.
-     * e.g. (a+b)*c -> guard(guard(a+b)*c)
-     * */
+    /**
+     * Wraps expressions whose value depends on session variables (or on the 
session time zone) in a
+     * {@link SessionVarGuardExpr} when the relevant session variables differ 
from the ones persisted on
+     * the object (view / materialized view / generated column) being 
processed.
+     */
     public static class AddSessionVarGuardRewriter extends 
DefaultExpressionRewriter<Boolean> {
         private final Map<String, String> sessionVar;
+        // Whether the time-zone family (time-zone sensitive expressions) must 
be guarded: the creation
+        // time zone differs from the current one, or the persisted map does 
not carry time_zone at all
+        // (pre-time_zone metadata), so the creation zone is unknown and must 
be treated as different.
+        private final boolean timeZoneDiffersOrUnknown;
+        // Whether the "other" guard family (NeedSessionVarGuard expressions, 
e.g. decimal256 dependent)
+        // must be guarded: some affectQueryResult session variable other than 
time_zone differs.
+        private final boolean otherSessionVarsDiffer;
+        // True when the guards are added to a shared materialized-view 
rewrite cache (MTMVCache.from).
+        // Cache-mismatch guards must stay structurally distinct from the 
guards BindRelation adds when
+        // expanding a persisted object into the query, so that pre-RBO 
expression matching never equates a
+        // query-side nested-object guard with the cache guard of an MTMV 
materialized in another zone.
+        private final boolean cacheGuard;
 
+        /**
+         * Creates a guard rewriter that guards both dependency families 
unconditionally for the persisted
+         * variables {@code var}. The guard decision must not depend on the 
current thread-local session:
+         * the only production caller builds this rewriter inside an {@code 
AutoCloseSessionVariable} scope
+         * where the current session already equals {@code var} (so deriving 
the decision from the
+         * thread-local session would add no guard at all), while the wrapped 
expression is later
+         * translated/executed in a different (load) session.
+         *
+         * @param var the persisted session variables of the object being 
processed
+         */
         public AddSessionVarGuardRewriter(Map<String, String> var) {
-            sessionVar = var;
+            this(var, true, true, false);
+        }
+
+        /**
+         * Creates a guard rewriter for the persisted session variables {@code 
var} against the current
+         * query session variables {@code currentVars}.
+         *
+         * @param var the persisted session variables of the object being 
processed
+         * @param currentVars the current query session's 
affectQueryResultInPlan variables
+         */
+        public AddSessionVarGuardRewriter(Map<String, String> var, Map<String, 
String> currentVars) {
+            this(var,
+                    // The creation zone is unknown when the persisted map is 
empty (pre-change metadata)
+                    // or has no time_zone key (metadata written before 
time_zone became a plan variable);
+                    // treat it as different so time-zone sensitive 
expressions are always guarded
+                    // (compatibility fence). The empty captured map is 
transparent at execution.
+                    var != null && (var.isEmpty() || 
!var.containsKey(SessionVariable.TIME_ZONE)
+                            || 
!timeZonesEquivalent(var.get(SessionVariable.TIME_ZONE),
+                                    currentVars == null ? null : 
currentVars.get(SessionVariable.TIME_ZONE))),
+                    // An empty map records no variable at all, so the 
creation session's other variables are
+                    // unknown as well and their dependency family has to be 
guarded too; a map that only lacks
+                    // time_zone still carries them and can be compared 
exactly.
+                    var != null && (var.isEmpty() || 
differsInNonTimeZoneVars(currentVars, var)),
+                    false);
+        }
+
+        /**
+         * Creates a guard rewriter that guards the families selected by 
{@code guardMask} unconditionally.
+         * Used when building a shared rewrite cache: the guarded cache must 
contain the guards regardless
+         * of the session it is generated in, so a cache built in the creation 
zone (e.g. by a background
+         * refresh task) is still effective for a query in a different zone. 
The produced guards are cache
+         * guards (see {@link #cacheGuard}) so they never equal a query-side 
nested-object guard.
+         *
+         * @param var the persisted session variables of the object being 
processed
+         * @param guardMask combination of {@link #GUARD_TIME_ZONE} and {@link 
#GUARD_OTHER}
+         */
+        public AddSessionVarGuardRewriter(Map<String, String> var, int 
guardMask) {
+            this(var, (guardMask & GUARD_TIME_ZONE) != 0, (guardMask & 
GUARD_OTHER) != 0, true);
+        }
+
+        private AddSessionVarGuardRewriter(Map<String, String> var, boolean 
timeZoneDiffersOrUnknown,
+                boolean otherSessionVarsDiffer, boolean cacheGuard) {
+            this.sessionVar = var;
+            this.timeZoneDiffersOrUnknown = timeZoneDiffersOrUnknown;
+            this.otherSessionVarsDiffer = otherSessionVarsDiffer;
+            this.cacheGuard = cacheGuard;
         }
 
         @Override
         public Expression visit(Expression expr, Boolean insideGuard) {
             Expression rewritten = rewriteChildren(this, expr, Boolean.FALSE);
-            if (rewritten instanceof NeedSessionVarGuard && 
!Boolean.TRUE.equals(insideGuard)) {
+            if (needsSessionVarGuard(rewritten) && 
!Boolean.TRUE.equals(insideGuard)) {
                 if (sessionVar == null) {
                     return expr;
                 }
-                return new SessionVarGuardExpr(rewritten, sessionVar);
+                return new SessionVarGuardExpr(rewritten, sessionVar, 
cacheGuard);
             }
             return rewritten;
         }
 
         @Override
         public Expression visitSessionVarGuardExpr(SessionVarGuardExpr expr, 
Boolean context) {
             Expression child = expr.child().accept(this, Boolean.TRUE);
-            if (child != expr.child()) {
-                return expr.withChildren(ImmutableList.of(child));
+            Expression guarded = child != expr.child() ? 
expr.withChildren(ImmutableList.of(child)) : expr;
+            // A cache-building rewriter must keep a cache-mismatch marker 
around an existing non-cache
+            // guard, but ONLY for the guard family the cache is built for: 
the existing guard's dependency
+            // family is re-derived from the expression it wraps (a 
NeedSessionVarGuard expression is the
+            // "other" family, a time-zone sensitive one is the time-zone 
family), and an outer marker for a
+            // family the cache does not guard (e.g. an "other"-family decimal 
guard under a time-zone-only
+            // cache mask) would reject a safe cross-zone nested-view rewrite 
whose decimal semantics agree
+            // through the view guard. Without the family scope an MTMV over a 
view carries the view's
+            // query-side guard (cacheGuard=false) in its definition plan and 
the isCacheGuard() rejection
+            // in AbstractMaterializedViewRule would never fire for the family 
that actually differs.
+            if (cacheGuard && !expr.isCacheGuard() && sessionVar != null
+                    && needsSessionVarGuard(guarded.child(0))) {
+                return new SessionVarGuardExpr(guarded, sessionVar, true);
             }
-            return expr;
+            return guarded;
+        }
+
+        private boolean needsSessionVarGuard(Expression expr) {
+            if (expr instanceof NeedSessionVarGuard) {
+                return otherSessionVarsDiffer;
+            }
+            return timeZoneDiffersOrUnknown && isTimeZoneSensitive(expr);
+        }
+
+        /**
+         * An expression is time-zone sensitive when its value is a 
session-time-zone dependent rendering of
+         * a TIMESTAMPTZ value (stored as UTC), e.g. date_trunc/cast/floor on 
a timestamptz column, or a
+         * string conversion of a TIMESTAMPTZ nested in a complex type 
(ARRAY&lt;timestamptz&gt;, MAP, STRUCT).
+         * Zone-invariant operations must NOT be guarded so that safe rewrites 
keep working: plain
+         * slot/literal passthroughs (a TIMESTAMPTZ slot holds the UTC 
instant), named expressions (their
+         * children are guarded individually), aggregate functions such as 
COUNT/MIN/MAX (they preserve the
+         * instant), IS (NOT) NULL checks, and comparisons / boolean 
combinations of them (equality,
+         * ordering, BETWEEN and IN over instants compare the UTC instants and 
do not depend on the session
+         * zone; any zone-dependent operand, e.g. a cast of an offset-free 
string into TIMESTAMPTZ, is
+         * guarded on its own by the child visit).
+         */
+        private static boolean isTimeZoneSensitive(Expression expr) {
+            if (expr instanceof Slot || expr instanceof Literal || expr 
instanceof NamedExpression
+                    || expr instanceof AggregateFunction || expr instanceof 
IsNull
+                    || expr instanceof ComparisonPredicate || expr instanceof 
Between
+                    || expr instanceof InPredicate || expr instanceof And || 
expr instanceof Or
+                    || expr instanceof Not
+                    // Structural / subtype-constrained expressions must not 
be replaced by a guard: their
+                    // owners rebuild them by casting (e.g. 
WindowExpression.withChildren casts ORDER BY keys
+                    // back to OrderExpression and the frame to WindowFrame; 
GenerateExpressionRewrite casts
+                    // the generator root back to Function). Wrapping them 
would throw a ClassCastException.
+                    // Their value-producing children are still guarded 
individually by the visitor. A whole
+                    // window expression is not guarded either: only the 
expressions it contains (partition
+                    // keys, order keys, frame bounds) are visited, so a 
zone-invariant window function such
+                    // as row_number() OVER (ORDER BY ts) keeps rewriting 
across zones.

Review Comment:
   [P1] Reject epoch-to-calendar functions in stored expressions. The 
classifier only special-cases zero-argument clock names and otherwise sees the 
BIGINT epoch argument of FROM_UNIXTIME/HOUR_FROM_UNIXTIME, so it accepts 
expressions such as DATETIME GENERATED ALWAYS AS (from_unixtime(epoch)). Both 
implementations convert through DateUtils.getTimeZone; identical rows loaded in 
UTC and +08:00 therefore materialize different values. Classify these 
session-zone conversions as sensitive and add a cross-zone generated-column or 
sync-MV regression.



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