From 189eca7512ed1dd761b0b84c4f43fda7deb37388 Mon Sep 17 00:00:00 2001 From: morrySnow <101034200+morrySnow@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:45:07 +0800 Subject: [PATCH] [opt](Nereids) support no-key hint parameter (#37720) pick from master #37720 support hint use parameter without key, like: ```sql SELECT /*+ query_timeout(3000) */ * FROM t; ``` --- .../org/apache/doris/nereids/DorisParser.g4 | 1 + .../nereids/parser/LogicalPlanBuilder.java | 26 ++++++++++-------- .../suites/nereids_syntax_p0/hint.groovy | 27 +++++++++++++++++++ 3 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 regression-test/suites/nereids_syntax_p0/hint.groovy diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 index 2aa21c01f432e5..e9f6f094176bee 100644 --- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 +++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 @@ -197,6 +197,7 @@ hintStatement hintAssignment : key=identifierOrText (EQ (constantValue=constant | identifierValue=identifier))? + | constant ; updateAssignment diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java index 055d19a840efed..6e4f888c390bce 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java @@ -1796,24 +1796,28 @@ private LogicalPlan withSelectHint(LogicalPlan logicalPlan, SelectHintContext hi case "set_var": Map> parameters = Maps.newLinkedHashMap(); for (HintAssignmentContext kv : hintStatement.parameters) { - String parameterName = visitIdentifierOrText(kv.key); - Optional value = Optional.empty(); - if (kv.constantValue != null) { - Literal literal = (Literal) visit(kv.constantValue); - value = Optional.ofNullable(literal.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 (kv.key != null) { + String parameterName = visitIdentifierOrText(kv.key); + Optional value = Optional.empty(); + if (kv.constantValue != null) { + Literal literal = (Literal) visit(kv.constantValue); + value = Optional.ofNullable(literal.toLegacyLiteral().getStringValue()); + } else if (kv.identifierValue != null) { + // maybe we should throw exception when the identifierValue is quoted identifier + value = Optional.ofNullable(kv.identifierValue.getText()); + } + parameters.put(parameterName, value); } - parameters.put(parameterName, value); } hints.put(hintName, new SelectHintSetVar(hintName, parameters)); break; case "leading": List leadingParameters = new ArrayList(); for (HintAssignmentContext kv : hintStatement.parameters) { - String parameterName = visitIdentifierOrText(kv.key); - leadingParameters.add(parameterName); + if (kv.key != null) { + String parameterName = visitIdentifierOrText(kv.key); + leadingParameters.add(parameterName); + } } hints.put(hintName, new SelectHintLeading(hintName, leadingParameters)); break; diff --git a/regression-test/suites/nereids_syntax_p0/hint.groovy b/regression-test/suites/nereids_syntax_p0/hint.groovy new file mode 100644 index 00000000000000..88c8f0c2163489 --- /dev/null +++ b/regression-test/suites/nereids_syntax_p0/hint.groovy @@ -0,0 +1,27 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("hint") { + sql """select /*+ func(k=v) */ 1""" + sql """select /*+ func('k'=v) */ 1""" + sql """select /*+ func("k"=v) */ 1""" + sql """select /*+ func(k) */ 1""" + sql """select /*+ func('k') */ 1""" + sql """select /*+ func("k") */ 1""" + sql """select /*+ func(1) */ 1""" +} +