-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BugFix] Fix partition resolve bug for materialized view (#34880)
Signed-off-by: shuming.li <ming.moriarty@gmail.com>
- Loading branch information
Showing
6 changed files
with
274 additions
and
152 deletions.
There are no files selected for viewing
This file contains 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 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
165 changes: 165 additions & 0 deletions
165
fe/fe-core/src/main/java/com/starrocks/sql/analyzer/SlotRefResolver.java
This file contains 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,165 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// Licensed 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 | ||
// | ||
// https://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 com.starrocks.sql.analyzer; | ||
|
||
import com.starrocks.analysis.Expr; | ||
import com.starrocks.analysis.SlotRef; | ||
import com.starrocks.analysis.TableName; | ||
import com.starrocks.sql.ast.AstVisitor; | ||
import com.starrocks.sql.ast.FieldReference; | ||
import com.starrocks.sql.ast.JoinRelation; | ||
import com.starrocks.sql.ast.QueryStatement; | ||
import com.starrocks.sql.ast.Relation; | ||
import com.starrocks.sql.ast.SelectListItem; | ||
import com.starrocks.sql.ast.SelectRelation; | ||
import com.starrocks.sql.ast.SetOperationRelation; | ||
import com.starrocks.sql.ast.SubqueryRelation; | ||
import com.starrocks.sql.ast.TableRelation; | ||
import com.starrocks.sql.ast.ValuesRelation; | ||
import com.starrocks.sql.ast.ViewRelation; | ||
|
||
/** | ||
* Resolve the expression(eg: partition column expression) through join/sub-query/view/set operator fo find the slot ref | ||
* which it comes from. | ||
*/ | ||
public class SlotRefResolver { | ||
private static final AstVisitor<Expr, Relation> EXPR_SHUTTLE = new AstVisitor<Expr, Relation>() { | ||
@Override | ||
public Expr visitExpression(Expr expr, Relation node) { | ||
expr = expr.clone(); | ||
for (int i = 0; i < expr.getChildren().size(); i++) { | ||
Expr child = expr.getChild(i); | ||
expr.setChild(i, child.accept(this, node)); | ||
} | ||
return expr; | ||
} | ||
|
||
@Override | ||
public Expr visitSlot(SlotRef slotRef, Relation node) { | ||
String tableName = slotRef.getTblNameWithoutAnalyzed().getTbl(); | ||
if (node.getAlias() != null && !node.getAlias().getTbl().equalsIgnoreCase(tableName)) { | ||
return slotRef; | ||
} | ||
return node.accept(SLOT_REF_RESOLVER, slotRef); | ||
} | ||
|
||
@Override | ||
public Expr visitFieldReference(FieldReference fieldReference, Relation node) { | ||
Field field = node.getScope().getRelationFields() | ||
.getFieldByIndex(fieldReference.getFieldIndex()); | ||
return new SlotRef(field.getRelationAlias(), field.getName()); | ||
} | ||
}; | ||
|
||
private static final AstVisitor<Expr, SlotRef> SLOT_REF_RESOLVER = new AstVisitor<Expr, SlotRef>() { | ||
@Override | ||
public Expr visitSelect(SelectRelation node, SlotRef slot) { | ||
for (SelectListItem selectListItem : node.getSelectList().getItems()) { | ||
TableName tableName = slot.getTblNameWithoutAnalyzed(); | ||
if (selectListItem.getAlias() == null) { | ||
if (selectListItem.getExpr() instanceof SlotRef) { | ||
SlotRef result = (SlotRef) selectListItem.getExpr(); | ||
if (result.getColumnName().equalsIgnoreCase(slot.getColumnName()) | ||
&& (tableName == null || tableName.equals(result.getTblNameWithoutAnalyzed()))) { | ||
return selectListItem.getExpr().accept(EXPR_SHUTTLE, node.getRelation()); | ||
} | ||
} | ||
} else { | ||
if (tableName != null && tableName.isFullyQualified()) { | ||
continue; | ||
} | ||
if (selectListItem.getAlias().equalsIgnoreCase(slot.getColumnName())) { | ||
return selectListItem.getExpr().accept(EXPR_SHUTTLE, node.getRelation()); | ||
} | ||
} | ||
} | ||
return node.getRelation().accept(this, slot); | ||
} | ||
|
||
@Override | ||
public Expr visitSubquery(SubqueryRelation node, SlotRef slot) { | ||
String tableName = slot.getTblNameWithoutAnalyzed().getTbl(); | ||
if (!node.getAlias().getTbl().equalsIgnoreCase(tableName)) { | ||
return null; | ||
} | ||
slot = (SlotRef) slot.clone(); | ||
slot.setTblName(null); //clear table name here, not check it inside | ||
return node.getQueryStatement().getQueryRelation().accept(this, slot); | ||
} | ||
|
||
@Override | ||
public Expr visitTable(TableRelation node, SlotRef slot) { | ||
TableName tableName = slot.getTblNameWithoutAnalyzed(); | ||
if (node.getName().equals(tableName)) { | ||
return slot; | ||
} | ||
if (tableName != null && !node.getResolveTableName().equals(tableName)) { | ||
return null; | ||
} | ||
slot = (SlotRef) slot.clone(); | ||
slot.setTblName(node.getName()); | ||
return slot; | ||
} | ||
|
||
@Override | ||
public Expr visitView(ViewRelation node, SlotRef slot) { | ||
TableName tableName = slot.getTblNameWithoutAnalyzed(); | ||
if (tableName != null && !node.getResolveTableName().equals(tableName)) { | ||
return null; | ||
} | ||
slot = (SlotRef) slot.clone(); | ||
slot.setTblName(null); //clear table name here, not check it inside | ||
return node.getQueryStatement().getQueryRelation().accept(this, slot); | ||
} | ||
|
||
@Override | ||
public Expr visitJoin(JoinRelation node, SlotRef slot) { | ||
Relation leftRelation = node.getLeft(); | ||
Expr leftExpr = leftRelation.accept(this, slot); | ||
if (leftExpr != null) { | ||
return leftExpr; | ||
} | ||
Relation rightRelation = node.getRight(); | ||
Expr rightExpr = rightRelation.accept(this, slot); | ||
if (rightExpr != null) { | ||
return rightExpr; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Expr visitSetOp(SetOperationRelation node, SlotRef slot) { | ||
for (Relation relation : node.getRelations()) { | ||
Expr resolved = relation.accept(this, slot); | ||
if (resolved != null) { | ||
return resolved; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public SlotRef visitValues(ValuesRelation node, SlotRef slot) { | ||
return null; | ||
} | ||
}; | ||
|
||
/** | ||
* Resolve the expression through join/sub-query/view/set operator fo find the slot ref | ||
* which it comes from. | ||
*/ | ||
public static Expr resolveExpr(Expr expr, QueryStatement queryStatement) { | ||
return expr.accept(EXPR_SHUTTLE, queryStatement.getQueryRelation()); | ||
} | ||
} |
Oops, something went wrong.