yujun777 commented on code in PR #71190:
URL: https://github.com/apache/doris/pull/71190#discussion_r4800484035


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java:
##########
@@ -1911,6 +1921,191 @@ public LogicalPlan 
visitRefreshMTMVDryRun(RefreshMTMVDryRunContext ctx) {
         return new RefreshMTMVCommand(refreshMTMVInfo, false, true, 
dryRunLimit);
     }
 
+    private Map<String, String> extractRefreshSetVarHints(ParserRuleContext 
stmtCtx,
+            TerminalNode refreshToken, TerminalNode materializedToken) {
+        if (selectHintMap == null || selectHintMap.isEmpty()) {
+            return ImmutableMap.of();
+        }
+        Pair<Integer, Integer> statementBounds = 
getStatementHintBounds(stmtCtx);
+        int stmtLowerBound = statementBounds.first;
+        int stmtUpperBound = statementBounds.second;
+        int refreshStopIndex = refreshToken.getSymbol().getStopIndex();
+        int materializedStartIndex = 
materializedToken.getSymbol().getStartIndex();
+
+        List<Map.Entry<Integer, ParserRuleContext>> sortedHints = new 
ArrayList<>(selectHintMap.entrySet());
+        sortedHints.sort(Map.Entry.comparingByKey());
+
+        Map<String, String> sessionVariables = Maps.newLinkedHashMap();
+        Map<String, String> originalKeyByLower = Maps.newHashMap();
+        for (Map.Entry<Integer, ParserRuleContext> entry : sortedHints) {
+            int hintStartIndex = entry.getKey();
+            if (hintStartIndex <= stmtLowerBound || hintStartIndex >= 
stmtUpperBound) {
+                continue;
+            }
+            if (hintStartIndex <= refreshStopIndex || hintStartIndex >= 
materializedStartIndex) {
+                throw new AnalysisException(
+                        "Hint must be placed right after REFRESH in REFRESH 
MATERIALIZED VIEW");
+            }
+            ParserRuleContext hintCtx = entry.getValue();
+            if (!(hintCtx instanceof SelectHintContext)) {
+                throw new AnalysisException("Invalid hint syntax in REFRESH 
MATERIALIZED VIEW");
+            }
+            SelectHintContext selectHintContext = (SelectHintContext) hintCtx;
+            if (hasParseError(selectHintContext) || 
selectHintContext.HINT_END() == null
+                    || selectHintContext.hintStatements.isEmpty()) {
+                throw new AnalysisException("Invalid hint syntax in REFRESH 
MATERIALIZED VIEW");
+            }
+            for (HintStatementContext hintStatement : 
selectHintContext.hintStatements) {
+                if (hintStatement.USE_MV() != null || 
hintStatement.NO_USE_MV() != null
+                        || hintStatement.hintName() == null
+                        || 
!hintStatement.hintName().getText().equalsIgnoreCase("set_var")) {
+                    throw new AnalysisException(
+                            "Only SET_VAR hint is supported in REFRESH 
MATERIALIZED VIEW");
+                }
+                SelectHintSetVar setVar = buildSelectHintSetVar(hintStatement, 
true);
+                for (Map.Entry<String, Optional<String>> kv : 
setVar.getParameters().entrySet()) {
+                    String parameterName = kv.getKey();
+                    String lowerKey = parameterName.toLowerCase(Locale.ROOT);
+                    String prevKey = originalKeyByLower.put(lowerKey, 
parameterName);
+                    if (prevKey != null) {
+                        sessionVariables.remove(prevKey);
+                    }
+                    sessionVariables.put(parameterName, kv.getValue().get());
+                }
+            }
+        }
+        return sessionVariables;
+    }
+
+    /**
+     * Parse and construct a {@link SelectHintSetVar} without applying it to 
the current statement.
+     * Callers that want statement-level application invoke {@link 
SelectHintSetVar#setVarOnceInSql}
+     * separately. When {@code strict} is true (used by REFRESH MATERIALIZED 
VIEW), empty parameter
+     * lists, keyless constants, missing values, and null values are rejected 
explicitly.
+     */
+    private SelectHintSetVar buildSelectHintSetVar(HintStatementContext 
hintStatement, boolean strict) {
+        if (strict && hintStatement.parameters.isEmpty()) {
+            throw new AnalysisException(
+                    "SET_VAR hint requires at least one parameter in REFRESH 
MATERIALIZED VIEW");
+        }
+        String hintName = 
hintStatement.hintName().getText().toLowerCase(Locale.ROOT);
+        Map<String, Optional<String>> parameters = Maps.newLinkedHashMap();
+        Map<String, String> originalKeyByLower = strict ? Maps.newHashMap() : 
null;
+        for (HintAssignmentContext kv : hintStatement.parameters) {
+            if (kv.key == null) {
+                if (strict) {
+                    throw new AnalysisException(
+                            "Invalid SET_VAR hint parameter in REFRESH 
MATERIALIZED VIEW");
+                }
+                continue;
+            }
+            if (strict && kv.skew != null) {
+                throw new AnalysisException(
+                        "Invalid SET_VAR hint parameter: skew is not supported 
in SET_VAR");
+            }
+            if (strict && kv.constantValue == null && kv.identifierValue == 
null) {
+                throw new AnalysisException(
+                        "Invalid SET_VAR hint parameter in REFRESH 
MATERIALIZED VIEW");
+            }
+            String parameterName = visitIdentifierOrText(kv.key);
+            Optional<String> value = Optional.empty();
+            if (kv.constantValue != null) {
+                if (strict && !(kv.constantValue instanceof 
DorisParser.NullLiteralContext
+                        || kv.constantValue instanceof 
DorisParser.TypeConstructorContext
+                        || kv.constantValue instanceof 
DorisParser.NumericLiteralContext
+                        || kv.constantValue instanceof 
DorisParser.BooleanLiteralContext
+                        || kv.constantValue instanceof 
DorisParser.StringLiteralContext)) {
+                    throw new AnalysisException(
+                            "Invalid SET_VAR hint parameter value for '" + 
parameterName + "'");
+                }
+                Object visited = visit(kv.constantValue);
+                if (strict && !(visited instanceof Literal)) {
+                    throw new AnalysisException(
+                            "Invalid SET_VAR hint parameter value for '" + 
parameterName + "'");
+                }
+                value = Optional.ofNullable(((Literal) 
visited).toLegacyLiteral().getStringValue());
+            } else if (kv.identifierValue != null) {
+                // maybe we should throw exception when the identifierValue is 
quoted identifier
+                value = Optional.ofNullable(kv.identifierValue.getText());
+            }
+            if (strict && !value.isPresent()) {
+                throw new AnalysisException(
+                        "Invalid SET_VAR hint parameter value for '" + 
parameterName + "'");
+            }
+            // Remove any prior entry before putting so LinkedHashMap moves 
the repeated key to
+            // its final source position (preserving last-wins order across 
case variants/aliases).
+            if (strict) {
+                String lowerKey = parameterName.toLowerCase(Locale.ROOT);
+                String prevKey = originalKeyByLower.put(lowerKey, 
parameterName);
+                if (prevKey != null) {
+                    parameters.remove(prevKey);
+                }
+            } else {
+                parameters.remove(parameterName);

Review Comment:
   Fixed in b68766fdefc. Removed the non-strict 
`parameters.remove(parameterName)` call in 
`LogicalPlanBuilder.buildSelectHintSetVar` so ordinary `SELECT` hints retain 
their historical `LinkedHashMap` in-place assignment order, while keeping 
case-insensitive last-wins repositioning for strict `REFRESH` parsing. Added 
coverage for the coupled `SELECT` setters in 
`RefreshMTMVCommandTest.testOrdinarySelectSetVarBinaryLiteralCompatibility`.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java:
##########
@@ -1911,6 +1921,191 @@ public LogicalPlan 
visitRefreshMTMVDryRun(RefreshMTMVDryRunContext ctx) {
         return new RefreshMTMVCommand(refreshMTMVInfo, false, true, 
dryRunLimit);
     }
 
+    private Map<String, String> extractRefreshSetVarHints(ParserRuleContext 
stmtCtx,
+            TerminalNode refreshToken, TerminalNode materializedToken) {
+        if (selectHintMap == null || selectHintMap.isEmpty()) {
+            return ImmutableMap.of();
+        }
+        Pair<Integer, Integer> statementBounds = 
getStatementHintBounds(stmtCtx);
+        int stmtLowerBound = statementBounds.first;
+        int stmtUpperBound = statementBounds.second;
+        int refreshStopIndex = refreshToken.getSymbol().getStopIndex();
+        int materializedStartIndex = 
materializedToken.getSymbol().getStartIndex();
+
+        List<Map.Entry<Integer, ParserRuleContext>> sortedHints = new 
ArrayList<>(selectHintMap.entrySet());
+        sortedHints.sort(Map.Entry.comparingByKey());
+
+        Map<String, String> sessionVariables = Maps.newLinkedHashMap();
+        Map<String, String> originalKeyByLower = Maps.newHashMap();
+        for (Map.Entry<Integer, ParserRuleContext> entry : sortedHints) {
+            int hintStartIndex = entry.getKey();
+            if (hintStartIndex <= stmtLowerBound || hintStartIndex >= 
stmtUpperBound) {
+                continue;
+            }
+            if (hintStartIndex <= refreshStopIndex || hintStartIndex >= 
materializedStartIndex) {
+                throw new AnalysisException(
+                        "Hint must be placed right after REFRESH in REFRESH 
MATERIALIZED VIEW");
+            }
+            ParserRuleContext hintCtx = entry.getValue();
+            if (!(hintCtx instanceof SelectHintContext)) {
+                throw new AnalysisException("Invalid hint syntax in REFRESH 
MATERIALIZED VIEW");
+            }
+            SelectHintContext selectHintContext = (SelectHintContext) hintCtx;
+            if (hasParseError(selectHintContext) || 
selectHintContext.HINT_END() == null
+                    || selectHintContext.hintStatements.isEmpty()) {
+                throw new AnalysisException("Invalid hint syntax in REFRESH 
MATERIALIZED VIEW");
+            }
+            for (HintStatementContext hintStatement : 
selectHintContext.hintStatements) {
+                if (hintStatement.USE_MV() != null || 
hintStatement.NO_USE_MV() != null
+                        || hintStatement.hintName() == null
+                        || 
!hintStatement.hintName().getText().equalsIgnoreCase("set_var")) {
+                    throw new AnalysisException(
+                            "Only SET_VAR hint is supported in REFRESH 
MATERIALIZED VIEW");
+                }
+                SelectHintSetVar setVar = buildSelectHintSetVar(hintStatement, 
true);
+                for (Map.Entry<String, Optional<String>> kv : 
setVar.getParameters().entrySet()) {
+                    String parameterName = kv.getKey();
+                    String lowerKey = parameterName.toLowerCase(Locale.ROOT);
+                    String prevKey = originalKeyByLower.put(lowerKey, 
parameterName);
+                    if (prevKey != null) {
+                        sessionVariables.remove(prevKey);
+                    }
+                    sessionVariables.put(parameterName, kv.getValue().get());
+                }
+            }
+        }
+        return sessionVariables;
+    }
+
+    /**
+     * Parse and construct a {@link SelectHintSetVar} without applying it to 
the current statement.
+     * Callers that want statement-level application invoke {@link 
SelectHintSetVar#setVarOnceInSql}
+     * separately. When {@code strict} is true (used by REFRESH MATERIALIZED 
VIEW), empty parameter
+     * lists, keyless constants, missing values, and null values are rejected 
explicitly.
+     */
+    private SelectHintSetVar buildSelectHintSetVar(HintStatementContext 
hintStatement, boolean strict) {
+        if (strict && hintStatement.parameters.isEmpty()) {
+            throw new AnalysisException(
+                    "SET_VAR hint requires at least one parameter in REFRESH 
MATERIALIZED VIEW");
+        }
+        String hintName = 
hintStatement.hintName().getText().toLowerCase(Locale.ROOT);
+        Map<String, Optional<String>> parameters = Maps.newLinkedHashMap();
+        Map<String, String> originalKeyByLower = strict ? Maps.newHashMap() : 
null;
+        for (HintAssignmentContext kv : hintStatement.parameters) {
+            if (kv.key == null) {
+                if (strict) {
+                    throw new AnalysisException(
+                            "Invalid SET_VAR hint parameter in REFRESH 
MATERIALIZED VIEW");
+                }
+                continue;
+            }
+            if (strict && kv.skew != null) {
+                throw new AnalysisException(
+                        "Invalid SET_VAR hint parameter: skew is not supported 
in SET_VAR");
+            }
+            if (strict && kv.constantValue == null && kv.identifierValue == 
null) {
+                throw new AnalysisException(
+                        "Invalid SET_VAR hint parameter in REFRESH 
MATERIALIZED VIEW");
+            }
+            String parameterName = visitIdentifierOrText(kv.key);
+            Optional<String> value = Optional.empty();
+            if (kv.constantValue != null) {
+                if (strict && !(kv.constantValue instanceof 
DorisParser.NullLiteralContext

Review Comment:
   Fixed in b68766fdefc. Removed `NullLiteralContext` from the strict `REFRESH` 
scalar-literal whitelist in `LogicalPlanBuilder.buildSelectHintSetVar` so 
unquoted `NULL` fails fast during parsing instead of passing `"NULL"` to 
string-valued settings. Added `REFRESH /*+ SET_VAR(disable_nereids_rules=NULL) 
*/` coverage in `RefreshMTMVCommandTest`.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java:
##########
@@ -1911,6 +1921,191 @@ public LogicalPlan 
visitRefreshMTMVDryRun(RefreshMTMVDryRunContext ctx) {
         return new RefreshMTMVCommand(refreshMTMVInfo, false, true, 
dryRunLimit);
     }
 
+    private Map<String, String> extractRefreshSetVarHints(ParserRuleContext 
stmtCtx,
+            TerminalNode refreshToken, TerminalNode materializedToken) {
+        if (selectHintMap == null || selectHintMap.isEmpty()) {
+            return ImmutableMap.of();
+        }
+        Pair<Integer, Integer> statementBounds = 
getStatementHintBounds(stmtCtx);
+        int stmtLowerBound = statementBounds.first;
+        int stmtUpperBound = statementBounds.second;
+        int refreshStopIndex = refreshToken.getSymbol().getStopIndex();
+        int materializedStartIndex = 
materializedToken.getSymbol().getStartIndex();
+
+        List<Map.Entry<Integer, ParserRuleContext>> sortedHints = new 
ArrayList<>(selectHintMap.entrySet());
+        sortedHints.sort(Map.Entry.comparingByKey());
+
+        Map<String, String> sessionVariables = Maps.newLinkedHashMap();
+        Map<String, String> originalKeyByLower = Maps.newHashMap();
+        for (Map.Entry<Integer, ParserRuleContext> entry : sortedHints) {
+            int hintStartIndex = entry.getKey();
+            if (hintStartIndex <= stmtLowerBound || hintStartIndex >= 
stmtUpperBound) {
+                continue;
+            }
+            if (hintStartIndex <= refreshStopIndex || hintStartIndex >= 
materializedStartIndex) {
+                throw new AnalysisException(

Review Comment:
   This is a pre-existing general behavior of 
`ConnectProcessor.parseWithFallback` for any multi-statement request where an 
earlier `SELECT /*+ SET_VAR(...) */` mutates session variables during parsing 
and a later statement throws a parse-time `AnalysisException` (since 
`parseWithFallback` currently only calls `revertSessionValue` for audit-policy 
rejections). Keeping `ConnectProcessor` unchanged to stay scoped to MTMV 
refresh hints.



##########
fe/fe-core/src/main/java/org/apache/doris/job/extensions/mtmv/MTMVTask.java:
##########
@@ -1009,8 +1010,8 @@ private IvmIncrRefreshResult 
executeSingleIvmAttempt(MTMVRefreshContext refreshC
         try {
             ivmResult = executeWithRetry(() -> {
                 signatureRef.set(null);
-                ConnectContext ivmConnectContext = 
MTMVPlanUtil.createMTMVContext(mtmv,
-                        MTMVPlanUtil.DISABLE_RULES_WHEN_RUN_MTMV_TASK);
+                ConnectContext ivmConnectContext = 
MTMVPlanUtil.createMTMVRefreshContext(mtmv,
+                        MTMVPlanUtil.DISABLE_RULES_WHEN_RUN_MTMV_TASK, 
taskContext.getSessionVariables());

Review Comment:
   By design (consistent with the policy on `dry_run_query`): we intentionally 
do not maintain a custom allowlist/blacklist or per-rule policing layer for 
`disable_nereids_rules` in MTMV. Explicitly disabling internal optimizer/IVM 
rules such as `IVM_INCR_REFRESH_MTMV` via `SET_VAR` is caller responsibility, 
just like explicitly disabling core Nereids rules on ordinary queries or DML.



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