hello-stephen commented on code in PR #71190:
URL: https://github.com/apache/doris/pull/71190#discussion_r4752306379
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java:
##########
@@ -1908,6 +1918,179 @@ 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();
+ 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 (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 (!(kv.constantValue instanceof
DorisParser.NullLiteralContext
Review Comment:
[P2] Preserve ordinary SELECT hint literal compatibility
This whitelist also runs when `withHints` calls the helper with
`strict=false`, so it rejects valid existing SELECT hints. For example, `SELECT
/*+ SET_VAR(query_timeout=X'3630') */ 1` previously visited
`VarbinaryLiteralContext`, converted its bytes through
`VarBinaryLiteral.toLegacyLiteral().getStringValue()` to `"60"`, and applied a
valid timeout. The new check excludes that context and throws during parsing,
making an ordinary SELECT fail because of the REFRESH feature. Scope the
additional literal restrictions to strict REFRESH parsing, or retain existing
scalar literal kinds, and add a SELECT compatibility case.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java:
##########
@@ -1908,6 +1918,179 @@ 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();
+ 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 (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 (!(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 (!(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 + "'");
+ }
+ parameters.put(parameterName, value);
Review Comment:
[P2] Preserve the final assignment's position within one hint
For `REFRESH /*+ SET_VAR(query_timeout=10, QUERY_TIMEOUT=20,
query_timeout=30) */ MATERIALIZED VIEW mv COMPLETE`, this `LinkedHashMap.put`
replaces the first lowercase entry in place, producing the order
`{query_timeout=30, QUERY_TIMEOUT=20}`. `extractRefreshSetVarHints` then merges
case-insensitively in that order and discards 30, so the refresh receives a
20-second timeout despite the final assignment requesting 30. The same ordering
loss affects aliases and coupled setters. Preserve source order while
deduplicating individual assignments, and extend
`testParseRefreshWithMultipleSetVarHintsLastWins` to cover repeats within a
single block.
--
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]