yujun777 commented on code in PR #71190:
URL: https://github.com/apache/doris/pull/71190#discussion_r4752646604
##########
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:
Fixed in 8d927790a46. In `LogicalPlanBuilder.buildSelectHintSetVar`, prior
entries are now removed before inserting (case-insensitively when
`strict=true`, exact key otherwise) so `LinkedHashMap` moves repeated keys to
their final source position, preserving last-wins order across mixed-case
repeats and coupled variables within a single `SET_VAR` block. Extended
`RefreshMTMVCommandTest.testParseRefreshWithMultipleSetVarHintsLastWins` to
cover both cases.
##########
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:
Fixed in 8d927790a46. Scoped the additional scalar-literal context whitelist
and skew check in `LogicalPlanBuilder.buildSelectHintSetVar` to `strict=true`
(REFRESH parsing) so ordinary `SELECT` hints (including binary literals like
`X'3630'`) preserve existing compatibility. Added
`RefreshMTMVCommandTest.testOrdinarySelectSetVarBinaryLiteralCompatibility`.
--
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]