You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2023/01/06 16:33:04 UTC
[doris] branch master updated: [feature](Nereids) support variable type expression (#15659)
This is an automated email from the ASF dual-hosted git repository.
morrysnow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 9c8fcd805c [feature](Nereids) support variable type expression (#15659)
9c8fcd805c is described below
commit 9c8fcd805cccb5f37e033326eb478e825c6e1867
Author: 谢健 <ji...@gmail.com>
AuthorDate: Sat Jan 7 00:32:57 2023 +0800
[feature](Nereids) support variable type expression (#15659)
---
.../antlr4/org/apache/doris/nereids/DorisLexer.g4 | 3 +
.../antlr4/org/apache/doris/nereids/DorisParser.g4 | 2 +
.../doris/nereids/parser/LogicalPlanBuilder.java | 24 +++++
.../rewrite/rules/FoldConstantRuleOnFE.java | 11 +++
.../nereids/trees/expressions/VariableDesc.java | 104 +++++++++++++++++++++
.../expressions/visitor/ExpressionVisitor.java | 5 +
.../main/java/org/apache/doris/qe/VariableMgr.java | 26 ++++--
.../{set_var.groovy => system_var.groovy} | 8 +-
8 files changed, 176 insertions(+), 7 deletions(-)
diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4
index db2d1440ca..b4c7eb18a3 100644
--- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4
+++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4
@@ -325,6 +325,7 @@ SEMI: 'SEMI';
SEPARATED: 'SEPARATED';
SERDE: 'SERDE';
SERDEPROPERTIES: 'SERDEPROPERTIES';
+SESSION: 'SESSION';
SESSION_USER: 'SESSION_USER';
SET: 'SET';
SETMINUS: 'MINUS';
@@ -421,6 +422,8 @@ COLON: ':';
ARROW: '->';
HINT_START: '/*+';
HINT_END: '*/';
+ATSIGN: '@';
+DOUBLEATSIGN: '@@';
STRING
: '\'' ( ~('\''|'\\') | ('\\' .) )* '\''
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 e594e100a5..6494bceaa2 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
@@ -300,6 +300,8 @@ primaryExpression
| identifier LEFT_PAREN ((DISTINCT|ALL)? arguments+=expression
(COMMA arguments+=expression)*)? RIGHT_PAREN #functionCall
| LEFT_PAREN query RIGHT_PAREN #subqueryExpression
+ | ATSIGN identifier #userVariable
+ | DOUBLEATSIGN (kind=(GLOBAL | SESSION) DOT)? identifier #systemVariable
| identifier #columnReference
| base=primaryExpression DOT fieldName=identifier #dereference
| LEFT_PAREN expression RIGHT_PAREN #parenthesizedExpression
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 01fa86ad79..1e29b80bd2 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
@@ -18,6 +18,7 @@
package org.apache.doris.nereids.parser;
import org.apache.doris.analysis.ArithmeticExpr.Operator;
+import org.apache.doris.analysis.SetType;
import org.apache.doris.common.Pair;
import org.apache.doris.nereids.DorisParser;
import org.apache.doris.nereids.DorisParser.AggClauseContext;
@@ -84,6 +85,7 @@ import org.apache.doris.nereids.DorisParser.StatementDefaultContext;
import org.apache.doris.nereids.DorisParser.StringLiteralContext;
import org.apache.doris.nereids.DorisParser.SubqueryContext;
import org.apache.doris.nereids.DorisParser.SubqueryExpressionContext;
+import org.apache.doris.nereids.DorisParser.SystemVariableContext;
import org.apache.doris.nereids.DorisParser.TableAliasContext;
import org.apache.doris.nereids.DorisParser.TableNameContext;
import org.apache.doris.nereids.DorisParser.TableValuedFunctionContext;
@@ -92,6 +94,7 @@ import org.apache.doris.nereids.DorisParser.TvfPropertyContext;
import org.apache.doris.nereids.DorisParser.TvfPropertyItemContext;
import org.apache.doris.nereids.DorisParser.TypeConstructorContext;
import org.apache.doris.nereids.DorisParser.UnitIdentifierContext;
+import org.apache.doris.nereids.DorisParser.UserVariableContext;
import org.apache.doris.nereids.DorisParser.WhereClauseContext;
import org.apache.doris.nereids.DorisParserBaseVisitor;
import org.apache.doris.nereids.StatementContext;
@@ -139,6 +142,7 @@ import org.apache.doris.nereids.trees.expressions.ScalarSubquery;
import org.apache.doris.nereids.trees.expressions.Subtract;
import org.apache.doris.nereids.trees.expressions.TVFProperties;
import org.apache.doris.nereids.trees.expressions.TimestampArithmetic;
+import org.apache.doris.nereids.trees.expressions.VariableDesc;
import org.apache.doris.nereids.trees.expressions.WhenClause;
import org.apache.doris.nereids.trees.expressions.functions.Function;
import org.apache.doris.nereids.trees.expressions.functions.agg.Count;
@@ -517,6 +521,26 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> {
});
}
+ @Override
+ public Expression visitSystemVariable(SystemVariableContext ctx) {
+ if (ctx.kind == null) {
+ return new VariableDesc(SetType.SESSION, ctx.identifier().getText());
+ }
+ switch (ctx.kind.getType()) {
+ case DorisParser.GLOBAL:
+ return new VariableDesc(SetType.GLOBAL, ctx.identifier().getText());
+ case DorisParser.SESSION:
+ return new VariableDesc(SetType.SESSION, ctx.identifier().getText());
+ default:
+ throw new ParseException("Unsupported system variable: " + ctx.getText(), ctx);
+ }
+ }
+
+ @Override
+ public Expression visitUserVariable(UserVariableContext ctx) {
+ throw new ParseException("Unsupported user variable :" + ctx.getText(), ctx);
+ }
+
/**
* Create a comparison expression. This compares two expressions. The following comparison
* operators are supported:
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/rules/FoldConstantRuleOnFE.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/rules/FoldConstantRuleOnFE.java
index 0130a6f567..a1fffa85b1 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/rules/FoldConstantRuleOnFE.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/rules/FoldConstantRuleOnFE.java
@@ -40,6 +40,7 @@ import org.apache.doris.nereids.trees.expressions.NullSafeEqual;
import org.apache.doris.nereids.trees.expressions.Or;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.TimestampArithmetic;
+import org.apache.doris.nereids.trees.expressions.VariableDesc;
import org.apache.doris.nereids.trees.expressions.WhenClause;
import org.apache.doris.nereids.trees.expressions.functions.BoundFunction;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
@@ -52,9 +53,12 @@ import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
import org.apache.doris.nereids.trees.expressions.literal.Literal;
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
import org.apache.doris.nereids.util.ExpressionUtils;
+import org.apache.doris.qe.VariableMgr;
+import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
@@ -334,6 +338,13 @@ public class FoldConstantRuleOnFE extends AbstractExpressionRewriteRule {
return Literal.of(isNull.child().nullable());
}
+ @Override
+ public Expression visitVariableDesc(VariableDesc variableDesc, ExpressionRewriteContext context) {
+ Preconditions.checkArgument(variableDesc.isSystemVariable());
+ return new StringLiteral(VariableMgr.getValue(context.connectContext.getSessionVariable(), variableDesc));
+
+ }
+
@Override
public Expression visitTimestampArithmetic(TimestampArithmetic arithmetic, ExpressionRewriteContext context) {
return ExpressionEvaluator.INSTANCE.eval(arithmetic);
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/VariableDesc.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/VariableDesc.java
new file mode 100644
index 0000000000..1afceaba0d
--- /dev/null
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/VariableDesc.java
@@ -0,0 +1,104 @@
+// 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.
+
+package org.apache.doris.nereids.trees.expressions;
+
+import org.apache.doris.analysis.SetType;
+import org.apache.doris.nereids.exceptions.UnboundException;
+import org.apache.doris.nereids.trees.expressions.shape.LeafExpression;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
+
+import java.util.Objects;
+
+/**
+ * Variable class
+ */
+public class VariableDesc extends Expression implements LeafExpression {
+ final boolean isSystemVariable;
+ final SetType setType;
+ final String name;
+
+ public VariableDesc(boolean isSystemVariable, SetType setType, String name) {
+ this.isSystemVariable = isSystemVariable;
+ this.setType = setType;
+ this.name = name;
+ }
+
+ public VariableDesc(SetType setType, String name) {
+ //Construct system variable
+ this(true, setType, name);
+ }
+
+ public VariableDesc(String name) {
+ //Construct user variable
+ this(false, SetType.DEFAULT, name);
+ }
+
+ public boolean isSystemVariable() {
+ return isSystemVariable;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public SetType getSetType() {
+ return setType;
+ }
+
+ @Override
+ public String toSql() {
+ return toString();
+ }
+
+ @Override
+ public boolean nullable() throws UnboundException {
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ if (isSystemVariable) {
+ String s = setType.equals(SetType.GLOBAL) ? "global." : "session.";
+ return "@@" + s + name;
+ } else {
+ return "@" + name;
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(isSystemVariable, setType, name);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ VariableDesc other = (VariableDesc) o;
+ return other.hashCode() == this.hashCode();
+ }
+
+ @Override
+ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
+ return visitor.visitVariableDesc(this, context);
+ }
+}
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ExpressionVisitor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ExpressionVisitor.java
index 5fc68638c7..bf7dc5a6da 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ExpressionVisitor.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ExpressionVisitor.java
@@ -69,6 +69,7 @@ import org.apache.doris.nereids.trees.expressions.TVFProperties;
import org.apache.doris.nereids.trees.expressions.TimestampArithmetic;
import org.apache.doris.nereids.trees.expressions.UnaryArithmetic;
import org.apache.doris.nereids.trees.expressions.UnaryOperator;
+import org.apache.doris.nereids.trees.expressions.VariableDesc;
import org.apache.doris.nereids.trees.expressions.VirtualSlotReference;
import org.apache.doris.nereids.trees.expressions.WhenClause;
import org.apache.doris.nereids.trees.expressions.functions.BoundFunction;
@@ -380,6 +381,10 @@ public abstract class ExpressionVisitor<R, C>
return visit(virtualSlotReference, context);
}
+ public R visitVariableDesc(VariableDesc variableDesc, C context) {
+ return visit(variableDesc, context);
+ }
+
public R visitTVFProperties(TVFProperties tvfProperties, C context) {
return visit(tvfProperties, context);
}
diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/VariableMgr.java b/fe/fe-core/src/main/java/org/apache/doris/qe/VariableMgr.java
index 090af9f695..b66a9b1df6 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/qe/VariableMgr.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/qe/VariableMgr.java
@@ -27,6 +27,7 @@ import org.apache.doris.common.DdlException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.PatternMatcher;
+import org.apache.doris.nereids.trees.expressions.VariableDesc;
import org.apache.doris.persist.GlobalVarPersistInfo;
import com.google.common.base.Preconditions;
@@ -416,15 +417,13 @@ public class VariableMgr {
}
}
- // Get variable value through variable name, used to satisfy statement like `SELECT @@comment_version`
- // For test only
- public static String getValue(SessionVariable var, SysVariableDesc desc) throws AnalysisException {
- VarContext ctx = ctxByVarName.get(desc.getName());
+ private static String getValue(SessionVariable var, String name, SetType setType) throws AnalysisException {
+ VarContext ctx = ctxByVarName.get(name);
if (ctx == null) {
- ErrorReport.reportAnalysisException(ErrorCode.ERR_UNKNOWN_SYSTEM_VARIABLE, desc.getName());
+ ErrorReport.reportAnalysisException(ErrorCode.ERR_UNKNOWN_SYSTEM_VARIABLE, name);
}
- if (desc.getSetType() == SetType.GLOBAL) {
+ if (setType == SetType.GLOBAL) {
rlock.lock();
try {
return getValue(ctx.getObj(), ctx.getField());
@@ -436,6 +435,21 @@ public class VariableMgr {
}
}
+ // Get variable value through variable name, used to satisfy statement like `SELECT @@comment_version`
+ // For test only
+ public static String getValue(SessionVariable var, SysVariableDesc desc) throws AnalysisException {
+ return getValue(var, desc.getName(), desc.getSetType());
+ }
+
+ // For Nereids optimizer
+ public static String getValue(SessionVariable var, VariableDesc desc) throws RuntimeException {
+ try {
+ return getValue(var, desc.getName(), desc.getSetType());
+ } catch (AnalysisException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
private static String getValue(Object obj, Field field) {
try {
switch (field.getType().getSimpleName()) {
diff --git a/regression-test/suites/nereids_syntax_p0/set_var.groovy b/regression-test/suites/nereids_syntax_p0/system_var.groovy
similarity index 90%
rename from regression-test/suites/nereids_syntax_p0/set_var.groovy
rename to regression-test/suites/nereids_syntax_p0/system_var.groovy
index 3156b9f27f..3ae1580b8a 100644
--- a/regression-test/suites/nereids_syntax_p0/set_var.groovy
+++ b/regression-test/suites/nereids_syntax_p0/system_var.groovy
@@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.
-suite("set_var") {
+suite("nereids_sys_var") {
sql "SET enable_nereids_planner=true"
sql "SET enable_fallback_to_original_planner=false"
@@ -40,4 +40,10 @@ suite("set_var") {
sql "select /*+SET_VAR(runtime_filter_type=10000)*/ * from supplier limit 10"
exception "Unexpected exception: Can not set session variable"
}
+
+ sql "select @@session.time_zone"
+ test {
+ sql "select @@session.enable_nereids_planner"
+ result ([["true"]])
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org