Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.9k
[feature](nereids) Support dereference expression#57532
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4 fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.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
22 changes: 11 additions & 11 deletions
22 fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.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
156 changes: 142 additions & 14 deletions
156 fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzer.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 |
|---|---|---|
| @@ -50,6 +50,7 @@ | ||
| import org.apache.doris.nereids.trees.expressions.Cast; | ||
| import org.apache.doris.nereids.trees.expressions.ComparisonPredicate; | ||
| import org.apache.doris.nereids.trees.expressions.CompoundPredicate; | ||
| import org.apache.doris.nereids.trees.expressions.DereferenceExpression; | ||
| import org.apache.doris.nereids.trees.expressions.Divide; | ||
| import org.apache.doris.nereids.trees.expressions.EqualTo; | ||
| import org.apache.doris.nereids.trees.expressions.ExprId; | ||
| @@ -75,7 +76,9 @@ | ||
| import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction; | ||
| import org.apache.doris.nereids.trees.expressions.functions.agg.NullableAggregateFunction; | ||
| import org.apache.doris.nereids.trees.expressions.functions.agg.SupportMultiDistinct; | ||
| import org.apache.doris.nereids.trees.expressions.functions.scalar.ElementAt; | ||
| import org.apache.doris.nereids.trees.expressions.functions.scalar.Lambda; | ||
| import org.apache.doris.nereids.trees.expressions.functions.scalar.StructElement; | ||
| import org.apache.doris.nereids.trees.expressions.functions.udf.AliasUdfBuilder; | ||
| import org.apache.doris.nereids.trees.expressions.functions.udf.JavaUdaf; | ||
| import org.apache.doris.nereids.trees.expressions.functions.udf.JavaUdf; | ||
| @@ -95,6 +98,8 @@ | ||
| import org.apache.doris.nereids.types.DataType; | ||
| import org.apache.doris.nereids.types.NestedColumnPrunable; | ||
| import org.apache.doris.nereids.types.StringType; | ||
| import org.apache.doris.nereids.types.StructField; | ||
| import org.apache.doris.nereids.types.StructType; | ||
| import org.apache.doris.nereids.types.TinyIntType; | ||
| import org.apache.doris.nereids.util.ExpressionUtils; | ||
| import org.apache.doris.nereids.util.TypeCoercionUtils; | ||
| @@ -257,6 +262,25 @@ public Expression visitUnboundAlias(UnboundAlias unboundAlias, ExpressionRewrite | ||
| } | ||
| } | ||
| @Override | ||
| public Expression visitDereferenceExpression(DereferenceExpression dereferenceExpression, | ||
| ExpressionRewriteContext context) { | ||
| Expression expression = dereferenceExpression.child(0).accept(this, context); | ||
| DataType dataType = expression.getDataType(); | ||
| if (dataType.isStructType()) { | ||
| StructType structType = (StructType) dataType; | ||
| StructField field = structType.getField(dereferenceExpression.fieldName); | ||
| if (field != null) { | ||
| return new StructElement(expression, dereferenceExpression.child(1)); | ||
| } | ||
| } else if (dataType.isMapType()) { | ||
| return new ElementAt(expression, dereferenceExpression.child(1)); | ||
| } else if (dataType.isVariantType()) { | ||
| return new ElementAt(expression, dereferenceExpression.child(1)); | ||
| } | ||
| throw new AnalysisException("Can not dereference field: " + dereferenceExpression.fieldName); | ||
| } | ||
| @Override | ||
| public Expression visitUnboundSlot(UnboundSlot unboundSlot, ExpressionRewriteContext context) { | ||
| Optional<Scope> outerScope = getScope().getOuterScope(); | ||
| @@ -947,13 +971,13 @@ protected List<? extends Expression> bindSlotByThisScope(UnboundSlot unboundSlot | ||
| return bindSlotByScope(unboundSlot, getScope()); | ||
| } | ||
| protected List<Slot> bindExactSlotsByThisScope(UnboundSlot unboundSlot, Scope scope) { | ||
| List<Slot> candidates = bindSlotByScope(unboundSlot, scope); | ||
| protected List<Expression> bindExactSlotsByThisScope(UnboundSlot unboundSlot, Scope scope) { | ||
| List<Expression> candidates = bindSlotByScope(unboundSlot, scope); | ||
| if (candidates.size() == 1) { | ||
| return candidates; | ||
| } | ||
| List<Slot> extractSlots = Utils.filterImmutableList(candidates, bound -> | ||
| unboundSlot.getNameParts().size() == bound.getQualifier().size() + 1 | ||
| List<Expression> extractSlots = Utils.filterImmutableList(candidates, bound -> | ||
| bound instanceof Slot && unboundSlot.getNameParts().size() == ((Slot) bound).getQualifier().size() + 1 | ||
| ); | ||
| // we should return origin candidates slots if extract slots is empty, | ||
| // and then throw an ambiguous exception | ||
| @@ -972,33 +996,137 @@ private List<Slot> addSqlIndexInfo(List<Slot> slots, Optional<Pair<Integer, Inte | ||
| } | ||
| /** bindSlotByScope */ | ||
| public List<Slot> bindSlotByScope(UnboundSlot unboundSlot, Scope scope) { | ||
| public List<Expression> bindSlotByScope(UnboundSlot unboundSlot, Scope scope) { | ||
| List<String> nameParts = unboundSlot.getNameParts(); | ||
| Optional<Pair<Integer, Integer>> idxInSql = unboundSlot.getIndexInSqlString(); | ||
| int namePartSize = nameParts.size(); | ||
| switch (namePartSize) { | ||
| // column | ||
| case 1: { | ||
| return addSqlIndexInfo(bindSingleSlotByName(nameParts.get(0), scope), idxInSql); | ||
| return (List<Expression>) bindExpressionByColumn(unboundSlot, nameParts, idxInSql, scope); | ||
| } | ||
| // table.column | ||
| case 2: { | ||
| return addSqlIndexInfo(bindSingleSlotByTable(nameParts.get(0), nameParts.get(1), scope), idxInSql); | ||
| return (List<Expression>) bindExpressionByTableColumn(unboundSlot, nameParts, idxInSql, scope); | ||
| } | ||
| // db.table.column | ||
| case 3: { | ||
| return addSqlIndexInfo(bindSingleSlotByDb(nameParts.get(0), nameParts.get(1), nameParts.get(2), scope), | ||
| idxInSql); | ||
| return (List<Expression>) bindExpressionByDbTableColumn(unboundSlot, nameParts, idxInSql, scope); | ||
| } | ||
| // catalog.db.table.column | ||
| case 4: { | ||
| return addSqlIndexInfo(bindSingleSlotByCatalog( | ||
| nameParts.get(0), nameParts.get(1), nameParts.get(2), nameParts.get(3), scope), idxInSql); | ||
| } | ||
| default: { | ||
| throw new AnalysisException("Not supported name: " + StringUtils.join(nameParts, ".")); | ||
| return (List<Expression>) bindExpressionByCatalogDbTableColumn(unboundSlot, nameParts, idxInSql, scope); | ||
| } | ||
| } | ||
| } | ||
| private List<? extends Expression> bindExpressionByCatalogDbTableColumn( | ||
924060929 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| UnboundSlot unboundSlot, List<String> nameParts, Optional<Pair<Integer, Integer>> idxInSql, Scope scope) { | ||
| List<Slot> slots = addSqlIndexInfo(bindSingleSlotByCatalog( | ||
| nameParts.get(0), nameParts.get(1), nameParts.get(2), nameParts.get(3), scope), idxInSql); | ||
| if (slots.isEmpty()) { | ||
| return bindExpressionByDbTableColumn(unboundSlot, nameParts, idxInSql, scope); | ||
| } else if (slots.size() > 1) { | ||
| return slots; | ||
| } | ||
| if (nameParts.size() == 4) { | ||
| return slots; | ||
| } | ||
| Optional<Expression> expression = bindNestedFields( | ||
| unboundSlot, slots.get(0), nameParts.subList(4, nameParts.size()) | ||
| ); | ||
| if (!expression.isPresent()) { | ||
| return slots; | ||
| } | ||
| return ImmutableList.of(expression.get()); | ||
| } | ||
| private List<? extends Expression> bindExpressionByDbTableColumn( | ||
| UnboundSlot unboundSlot, List<String> nameParts, Optional<Pair<Integer, Integer>> idxInSql, Scope scope) { | ||
| List<Slot> slots = addSqlIndexInfo( | ||
| bindSingleSlotByDb(nameParts.get(0), nameParts.get(1), nameParts.get(2), scope), idxInSql); | ||
| if (slots.isEmpty()) { | ||
| return bindExpressionByTableColumn(unboundSlot, nameParts, idxInSql, scope); | ||
| } else if (slots.size() > 1) { | ||
| return slots; | ||
| } | ||
| if (nameParts.size() == 3) { | ||
| return slots; | ||
| } | ||
| Optional<Expression> expression = bindNestedFields( | ||
| unboundSlot, slots.get(0), nameParts.subList(3, nameParts.size()) | ||
| ); | ||
| if (!expression.isPresent()) { | ||
| return slots; | ||
| } | ||
| return ImmutableList.of(expression.get()); | ||
| } | ||
| private List<? extends Expression> bindExpressionByTableColumn( | ||
| UnboundSlot unboundSlot, List<String> nameParts, Optional<Pair<Integer, Integer>> idxInSql, Scope scope) { | ||
| List<Slot> slots = addSqlIndexInfo(bindSingleSlotByTable(nameParts.get(0), nameParts.get(1), scope), idxInSql); | ||
| if (slots.isEmpty()) { | ||
| return bindExpressionByColumn(unboundSlot, nameParts, idxInSql, scope); | ||
| } else if (slots.size() > 1) { | ||
| return slots; | ||
| } | ||
| if (nameParts.size() == 2) { | ||
| return slots; | ||
| } | ||
| Optional<Expression> expression = bindNestedFields( | ||
| unboundSlot, slots.get(0), nameParts.subList(2, nameParts.size()) | ||
| ); | ||
| if (!expression.isPresent()) { | ||
| return slots; | ||
| } | ||
| return ImmutableList.of(expression.get()); | ||
| } | ||
| private List<? extends Expression> bindExpressionByColumn( | ||
| UnboundSlot unboundSlot, List<String> nameParts, Optional<Pair<Integer, Integer>> idxInSql, Scope scope) { | ||
| List<Slot> slots = addSqlIndexInfo(bindSingleSlotByName(nameParts.get(0), scope), idxInSql); | ||
| if (slots.size() != 1) { | ||
| return slots; | ||
| } | ||
| if (nameParts.size() == 1) { | ||
| return slots; | ||
| } | ||
| Optional<Expression> expression = bindNestedFields( | ||
| unboundSlot, slots.get(0), nameParts.subList(1, nameParts.size()) | ||
| ); | ||
| if (!expression.isPresent()) { | ||
| return slots; | ||
| } | ||
| return ImmutableList.of(expression.get()); | ||
| } | ||
| private Optional<Expression> bindNestedFields(UnboundSlot unboundSlot, Slot slot, List<String> fieldNames) { | ||
| Expression expression = slot; | ||
| String lastFieldName = slot.getName(); | ||
| for (String fieldName : fieldNames) { | ||
| DataType dataType = expression.getDataType(); | ||
| if (dataType.isStructType()) { | ||
| StructType structType = (StructType) dataType; | ||
| StructField field = structType.getField(fieldName); | ||
| if (field == null) { | ||
| throw new AnalysisException("No such struct field '" + fieldName + "' in '" + lastFieldName + "'"); | ||
| } | ||
| lastFieldName = fieldName; | ||
| expression = new StructElement(expression, new StringLiteral(fieldName)); | ||
| continue; | ||
| } else if (dataType.isMapType()) { | ||
| expression = new ElementAt(expression, new StringLiteral(fieldName)); | ||
| continue; | ||
| } else if (dataType.isVariantType()) { | ||
| expression = new ElementAt(expression, new StringLiteral(fieldName)); | ||
| continue; | ||
| } | ||
| throw new AnalysisException("No such field '" + fieldName + "' in '" + lastFieldName + "'"); | ||
| } | ||
| return Optional.of(new Alias(expression)); | ||
| } | ||
| public static boolean sameTableName(String boundSlot, String unboundSlot) { | ||
41 changes: 41 additions & 0 deletions
41 ...-core/src/main/java/org/apache/doris/nereids/trees/expressions/DereferenceExpression.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,41 @@ | ||
| // 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.nereids.analyzer.Unbound; | ||
| import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable; | ||
| import org.apache.doris.nereids.trees.expressions.literal.StringLiteral; | ||
| import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression; | ||
| import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; | ||
| import com.google.common.collect.ImmutableList; | ||
| /** DereferenceExpression*/ | ||
| public class DereferenceExpression extends Expression implements BinaryExpression, PropagateNullable, Unbound { | ||
| public final String fieldName; | ||
| public DereferenceExpression(Expression expression, StringLiteral fieldName) { | ||
| super(ImmutableList.of(expression, fieldName)); | ||
| this.fieldName = fieldName.getValue(); | ||
| } | ||
| @Override | ||
| public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) { | ||
| return visitor.visitDereferenceExpression(this, context); | ||
| } | ||
| } |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.