-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Feature](Variant) Implement variant new sub column access method #28484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3d39df6
[Feature](Variant) Implement variant new sub column access method
eldenmoon 3fb128b
remove useless code
eldenmoon 2a1e54f
fix rewrite
eldenmoon cfdb891
fix rewrite
eldenmoon d373cd1
add session var
eldenmoon 8d7a61d
fix stable
eldenmoon 53bc222
comment some sql
eldenmoon 7649dd1
refine
eldenmoon 221f392
fix
eldenmoon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
fe/fe-core/src/main/java/org/apache/doris/rewrite/ElementAtToSlotRefRule.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| // 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.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); | ||
| if (slotRefs.size() != 1) { | ||
| throw new AnalysisException("only support syntax like v[\"a\"][\"b\"][\"c\"]"); | ||
| } | ||
| SlotRef slot = slotRefs.get(0); | ||
| List<Expr> pathsExpr = Lists.newArrayList(); | ||
| // Traverse the expression tree to gather literals. | ||
| // For instance, consider the expression v["a"]["b"]["c"], where it's represented as | ||
| // element_at(element_at(element_at(v, 'a'), 'b'), 'c').The pathsExpr will contain | ||
| // literals ['a', 'b', 'c'] representing the sequence of keys in the structure. | ||
| expr.collect(Expr::isLiteral, pathsExpr); | ||
| List<String> fullPaths = pathsExpr.stream() | ||
eldenmoon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .map(node -> ((LiteralExpr) node).getStringValue()) | ||
| .collect(Collectors.toList()); | ||
| slot.setSubColPath(fullPaths); | ||
| slot.analyzeImpl(analyzer); | ||
| return slot; | ||
| } | ||
|
|
||
| // check if expr is element_at with variant slot type | ||
| private static boolean isElementAtOfVariantType(Expr expr) { | ||
| if (!(expr instanceof FunctionCallExpr)) { | ||
| return false; | ||
| } | ||
| FunctionCallExpr functionCallExpr = (FunctionCallExpr) expr; | ||
| List<SlotRef> slotRefs = Lists.newArrayList(); | ||
| expr.collect(SlotRef.class, slotRefs); | ||
| if (slotRefs.size() != 1) { | ||
| return false; | ||
| } | ||
| return functionCallExpr.getFnName().getFunction().equalsIgnoreCase("element_at") | ||
| && slotRefs.get(0).getType().isVariantType(); | ||
| } | ||
|
|
||
| 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()) { | ||
| getElementAtFunction(child, result); | ||
| } | ||
| } | ||
|
|
||
| public Expr rewrite(Expr inputExpr, Analyzer analyzer) | ||
| throws AnalysisException { | ||
| List<Expr> originalFunctionElementAtExprs = Lists.newArrayList(); | ||
| boolean changed = false; | ||
| Expr newExpr = inputExpr.clone(); | ||
| getElementAtFunction(inputExpr, originalFunctionElementAtExprs); | ||
| for (Expr expr : originalFunctionElementAtExprs) { | ||
| Expr rewriteExpr = apply(expr, analyzer); | ||
| if (inputExpr.getId().equals(expr.getId())) { | ||
| return rewriteExpr; | ||
| } | ||
| if (rewriteExpr != expr) { | ||
| changed = true; | ||
| replaceChildExpr(newExpr, expr.getId().toString(), rewriteExpr); | ||
| } | ||
| } | ||
| return changed ? newExpr : inputExpr; | ||
| } | ||
|
|
||
| // Find child expr which id matches key and replace this child expr | ||
| // with replacExpr and set replacExpr with same expr id. | ||
| private void replaceChildExpr(Expr expr, String key, Expr replacExpr) { | ||
| // ATTN: make sure the child order of expr keep unchanged | ||
| for (int i = 0; i < expr.getChildren().size(); i++) { | ||
| Expr child = expr.getChild(i); | ||
| if ((isElementAtOfVariantType(child)) && key.equals(child.getId().toString())) { | ||
| replacExpr.setId(child.getId()); | ||
| expr.setChild(i, replacExpr); | ||
| break; | ||
| } | ||
| replaceChildExpr(child, key, replacExpr); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any disadvantage for normal query without variant column? If no, the session variable is not necessary.