xiaokang commented on code in PR #28484:
URL: https://github.com/apache/doris/pull/28484#discussion_r1433396674
##########
fe/fe-core/src/main/java/org/apache/doris/rewrite/ExprRewriter.java:
##########
@@ -202,6 +202,25 @@ public void rewriteConstant(Map<String, Expr> exprMap,
Analyzer analyzer, TQuery
}
}
+ public Expr rewriteElementAtToSlot(Expr inputExpr, Analyzer analyzer)
Review Comment:
Why is it different from rewriteConstant?
1. the function signature
2. call rewrite instead of apply
##########
fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java:
##########
@@ -1130,7 +1130,9 @@ private void analyzeAndGenerateQueryPlan(TQueryOptions
tQueryOptions) throws Use
if (context.getSessionVariable().isEnableFoldConstantByBe()) {
// fold constant expr
parsedStmt.foldConstant(rewriter, tQueryOptions);
-
+ }
+ if (context.getSessionVariable().isEnableRewriteElementAtToSlot())
{
+ parsedStmt.rewriteElementAtToSlot(rewriter, tQueryOptions);
Review Comment:
Is there any disadvantage for normal query without variant column? If no,
the session variable is not necessary.
##########
fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java:
##########
@@ -412,6 +413,7 @@ private static class GlobalState {
private final Map<SlotId, Analyzer> blockBySlot = Maps.newHashMap();
// Expr rewriter for normalizing and rewriting expressions.
+
Review Comment:
useless blank line
##########
fe/fe-core/src/main/java/org/apache/doris/rewrite/ElementAtToSlotRefRule.java:
##########
@@ -0,0 +1,135 @@
+// 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.rewrite;
+
+import org.apache.doris.analysis.Analyzer;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.FunctionCallExpr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotRef;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.rewrite.ExprRewriter.ClauseType;
+
+import com.google.common.collect.Lists;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Transform element_at function to SlotReference for variant sub-column
access.
+ * This optimization will help query engine to prune as many sub columns as
possible
+ * to speed up query.
+ * eg: element_at(element_at(v, "a"), "b") -> SlotReference(column=v,
subColLabels=["a", "b"])
+ */
+
+public class ElementAtToSlotRefRule implements ExprRewriteRule {
+ public static final ElementAtToSlotRefRule INSTANCE = new
ElementAtToSlotRefRule();
+
+ @Override
+ public Expr apply(Expr expr, Analyzer analyzer, ClauseType clauseType)
throws AnalysisException {
+ // Only check element at of variant all rewrited to slots
+ List<Expr> elementAtFunctions = Lists.newArrayList();
+ getElementAtFunction(expr, elementAtFunctions);
+ if (!elementAtFunctions.isEmpty()) {
+ throw new AnalysisException("element_at should not appear in
common rewrite stage");
+ }
+ return expr;
+ }
+
+ private Expr apply(Expr expr, Analyzer analyzer) throws AnalysisException
{
+ if (!(isElementAtOfVariantType(expr))) {
+ return expr;
+ }
+ List<SlotRef> slotRefs = Lists.newArrayList();
+ expr.collect(SlotRef.class, slotRefs);
+ SlotRef slot = slotRefs.get(0);
+ List<Expr> pathsExpr = Lists.newArrayList();
+ expr.collect(Expr::isLiteral, pathsExpr);
+ List<String> fullPaths = pathsExpr.stream()
+ .map(node -> ((LiteralExpr) node).getStringValue())
+ .collect(Collectors.toList());
+ slot.setSubColPath(fullPaths);
+ slot.analyzeImpl(analyzer);
+ return slot;
+ }
+
+ private static boolean isElementAtOfVariantType(Expr expr) {
+ if (!(expr instanceof FunctionCallExpr)) {
+ return false;
+ }
+ FunctionCallExpr functionCallExpr = (FunctionCallExpr) expr;
+ return
functionCallExpr.getFnName().getFunction().equalsIgnoreCase("element_at")
+ && functionCallExpr.getType() == Type.VARIANT;
Review Comment:
functionCallExpr.getType() is return type of function. I think the type of
the first arg should be checked instead.
##########
fe/fe-core/src/main/java/org/apache/doris/rewrite/ElementAtToSlotRefRule.java:
##########
@@ -0,0 +1,135 @@
+// 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.rewrite;
+
+import org.apache.doris.analysis.Analyzer;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.FunctionCallExpr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotRef;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.rewrite.ExprRewriter.ClauseType;
+
+import com.google.common.collect.Lists;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Transform element_at function to SlotReference for variant sub-column
access.
+ * This optimization will help query engine to prune as many sub columns as
possible
+ * to speed up query.
+ * eg: element_at(element_at(v, "a"), "b") -> SlotReference(column=v,
subColLabels=["a", "b"])
+ */
+
+public class ElementAtToSlotRefRule implements ExprRewriteRule {
+ public static final ElementAtToSlotRefRule INSTANCE = new
ElementAtToSlotRefRule();
+
+ @Override
+ public Expr apply(Expr expr, Analyzer analyzer, ClauseType clauseType)
throws AnalysisException {
+ // Only check element at of variant all rewrited to slots
+ List<Expr> elementAtFunctions = Lists.newArrayList();
+ getElementAtFunction(expr, elementAtFunctions);
+ if (!elementAtFunctions.isEmpty()) {
+ throw new AnalysisException("element_at should not appear in
common rewrite stage");
+ }
+ return expr;
+ }
+
+ private Expr apply(Expr expr, Analyzer analyzer) throws AnalysisException
{
+ if (!(isElementAtOfVariantType(expr))) {
+ return expr;
+ }
+ List<SlotRef> slotRefs = Lists.newArrayList();
+ expr.collect(SlotRef.class, slotRefs);
+ SlotRef slot = slotRefs.get(0);
+ List<Expr> pathsExpr = Lists.newArrayList();
+ expr.collect(Expr::isLiteral, pathsExpr);
+ List<String> fullPaths = pathsExpr.stream()
Review Comment:
Add an example comment to explain here.
##########
fe/fe-core/src/main/java/org/apache/doris/rewrite/ElementAtToSlotRefRule.java:
##########
@@ -0,0 +1,135 @@
+// 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.rewrite;
+
+import org.apache.doris.analysis.Analyzer;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.FunctionCallExpr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotRef;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.rewrite.ExprRewriter.ClauseType;
+
+import com.google.common.collect.Lists;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Transform element_at function to SlotReference for variant sub-column
access.
+ * This optimization will help query engine to prune as many sub columns as
possible
+ * to speed up query.
+ * eg: element_at(element_at(v, "a"), "b") -> SlotReference(column=v,
subColLabels=["a", "b"])
+ */
+
+public class ElementAtToSlotRefRule implements ExprRewriteRule {
+ public static final ElementAtToSlotRefRule INSTANCE = new
ElementAtToSlotRefRule();
+
+ @Override
+ public Expr apply(Expr expr, Analyzer analyzer, ClauseType clauseType)
throws AnalysisException {
+ // Only check element at of variant all rewrited to slots
+ List<Expr> elementAtFunctions = Lists.newArrayList();
+ getElementAtFunction(expr, elementAtFunctions);
+ if (!elementAtFunctions.isEmpty()) {
+ throw new AnalysisException("element_at should not appear in
common rewrite stage");
+ }
+ return expr;
+ }
+
+ private Expr apply(Expr expr, Analyzer analyzer) throws AnalysisException
{
+ if (!(isElementAtOfVariantType(expr))) {
+ return expr;
+ }
+ List<SlotRef> slotRefs = Lists.newArrayList();
+ expr.collect(SlotRef.class, slotRefs);
+ SlotRef slot = slotRefs.get(0);
+ List<Expr> pathsExpr = Lists.newArrayList();
+ expr.collect(Expr::isLiteral, pathsExpr);
+ List<String> fullPaths = pathsExpr.stream()
Review Comment:
Do you mean this transformation: v['a']['b']['c'] => element_at(v, 'a', 'b',
'c') => SlotRef(v, ['a', 'b', 'c']) ?
I assume that v['a']['b']['c'] is analyzed to
element_at(element_at(element_at(v, 'a'), 'b'), 'c') originally.
##########
fe/fe-core/src/main/java/org/apache/doris/rewrite/ElementAtToSlotRefRule.java:
##########
@@ -0,0 +1,135 @@
+// 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.rewrite;
+
+import org.apache.doris.analysis.Analyzer;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.FunctionCallExpr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotRef;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.rewrite.ExprRewriter.ClauseType;
+
+import com.google.common.collect.Lists;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Transform element_at function to SlotReference for variant sub-column
access.
+ * This optimization will help query engine to prune as many sub columns as
possible
+ * to speed up query.
+ * eg: element_at(element_at(v, "a"), "b") -> SlotReference(column=v,
subColLabels=["a", "b"])
+ */
+
+public class ElementAtToSlotRefRule implements ExprRewriteRule {
+ public static final ElementAtToSlotRefRule INSTANCE = new
ElementAtToSlotRefRule();
+
+ @Override
+ public Expr apply(Expr expr, Analyzer analyzer, ClauseType clauseType)
throws AnalysisException {
+ // Only check element at of variant all rewrited to slots
+ List<Expr> elementAtFunctions = Lists.newArrayList();
+ getElementAtFunction(expr, elementAtFunctions);
+ if (!elementAtFunctions.isEmpty()) {
+ throw new AnalysisException("element_at should not appear in
common rewrite stage");
+ }
+ return expr;
+ }
+
+ private Expr apply(Expr expr, Analyzer analyzer) throws AnalysisException
{
+ if (!(isElementAtOfVariantType(expr))) {
+ return expr;
+ }
+ List<SlotRef> slotRefs = Lists.newArrayList();
+ expr.collect(SlotRef.class, slotRefs);
+ SlotRef slot = slotRefs.get(0);
+ List<Expr> pathsExpr = Lists.newArrayList();
+ expr.collect(Expr::isLiteral, pathsExpr);
+ List<String> fullPaths = pathsExpr.stream()
+ .map(node -> ((LiteralExpr) node).getStringValue())
+ .collect(Collectors.toList());
+ slot.setSubColPath(fullPaths);
+ slot.analyzeImpl(analyzer);
+ return slot;
+ }
+
+ private static boolean isElementAtOfVariantType(Expr expr) {
+ if (!(expr instanceof FunctionCallExpr)) {
+ return false;
+ }
+ FunctionCallExpr functionCallExpr = (FunctionCallExpr) expr;
+ return
functionCallExpr.getFnName().getFunction().equalsIgnoreCase("element_at")
+ && functionCallExpr.getType() == Type.VARIANT;
+ }
+
+ public static boolean containsElementAtFunction(Expr expr) {
+ List<Expr> result = Lists.newArrayList();
+ getElementAtFunction(expr, result);
+ return !result.isEmpty();
+ }
+
+ private static void getElementAtFunction(Expr expr, List<Expr> result) {
+ if (isElementAtOfVariantType(expr)) {
+ result.add(expr);
+ return;
+ }
+ for (Expr child : expr.getChildren()) {
+ if (isElementAtOfVariantType(expr)) {
Review Comment:
The following recusive call is enough.
##########
fe/fe-core/src/main/java/org/apache/doris/rewrite/ElementAtToSlotRefRule.java:
##########
@@ -0,0 +1,135 @@
+// 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.rewrite;
+
+import org.apache.doris.analysis.Analyzer;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.FunctionCallExpr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotRef;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.rewrite.ExprRewriter.ClauseType;
+
+import com.google.common.collect.Lists;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Transform element_at function to SlotReference for variant sub-column
access.
+ * This optimization will help query engine to prune as many sub columns as
possible
+ * to speed up query.
+ * eg: element_at(element_at(v, "a"), "b") -> SlotReference(column=v,
subColLabels=["a", "b"])
+ */
+
+public class ElementAtToSlotRefRule implements ExprRewriteRule {
+ public static final ElementAtToSlotRefRule INSTANCE = new
ElementAtToSlotRefRule();
+
+ @Override
+ public Expr apply(Expr expr, Analyzer analyzer, ClauseType clauseType)
throws AnalysisException {
+ // Only check element at of variant all rewrited to slots
+ List<Expr> elementAtFunctions = Lists.newArrayList();
+ getElementAtFunction(expr, elementAtFunctions);
+ if (!elementAtFunctions.isEmpty()) {
+ throw new AnalysisException("element_at should not appear in
common rewrite stage");
+ }
+ return expr;
+ }
+
+ private Expr apply(Expr expr, Analyzer analyzer) throws AnalysisException
{
+ if (!(isElementAtOfVariantType(expr))) {
+ return expr;
+ }
+ List<SlotRef> slotRefs = Lists.newArrayList();
+ expr.collect(SlotRef.class, slotRefs);
+ SlotRef slot = slotRefs.get(0);
+ List<Expr> pathsExpr = Lists.newArrayList();
+ expr.collect(Expr::isLiteral, pathsExpr);
+ List<String> fullPaths = pathsExpr.stream()
+ .map(node -> ((LiteralExpr) node).getStringValue())
+ .collect(Collectors.toList());
+ slot.setSubColPath(fullPaths);
+ slot.analyzeImpl(analyzer);
+ return slot;
+ }
+
+ private static boolean isElementAtOfVariantType(Expr expr) {
+ if (!(expr instanceof FunctionCallExpr)) {
+ return false;
+ }
+ FunctionCallExpr functionCallExpr = (FunctionCallExpr) expr;
+ return
functionCallExpr.getFnName().getFunction().equalsIgnoreCase("element_at")
+ && functionCallExpr.getType() == Type.VARIANT;
+ }
+
+ public static boolean containsElementAtFunction(Expr expr) {
+ List<Expr> result = Lists.newArrayList();
+ getElementAtFunction(expr, result);
+ return !result.isEmpty();
+ }
+
+ private static void getElementAtFunction(Expr expr, List<Expr> result) {
+ if (isElementAtOfVariantType(expr)) {
+ result.add(expr);
+ return;
+ }
+ for (Expr child : expr.getChildren()) {
+ if (isElementAtOfVariantType(expr)) {
+ // get the top level element at function, ignore its child
+ result.add(child);
+ continue;
+ }
+ getElementAtFunction(child, result);
+ }
+ }
+
+ public Expr rewrite(Expr inputExpr, Analyzer analyzer)
+ throws AnalysisException {
+ List<Expr> originalFunctionElementAtExprs = Lists.newArrayList();
+ Expr newExpr = null;
+ getElementAtFunction(inputExpr, originalFunctionElementAtExprs);
+ for (Expr expr : originalFunctionElementAtExprs) {
+ Expr rewriteExpr = apply(expr, analyzer);
+ if (rewriteExpr != expr) {
+ if (newExpr == null) {
+ newExpr = inputExpr.clone();
+ }
+ newExpr = replaceExpr(newExpr, expr.getId().toString(),
rewriteExpr);
Review Comment:
Why not just use expr, but create newExpr?
--
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]