Skip to content
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

[Fix](Nereids) fix leading with multi level of brace pairs (#34169) #35606

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
public class LeadingHint extends Hint {
private String originalString = "";
private final List<String> tablelist = new ArrayList<>();
private final List<Integer> levellist = new ArrayList<>();
private final List<Integer> levelList = new ArrayList<>();

private final Map<RelationId, LogicalPlan> relationIdToScanMap = Maps.newLinkedHashMap();

Expand Down Expand Up @@ -101,18 +101,46 @@ public LeadingHint(String hintName, List<String> parameters, String originalStri
}
} else {
tablelist.add(parameter);
levellist.add(level);
levelList.add(level);
}
lastParameter = parameter;
}
normalizeLevelList();
}

private void removeGap(int left, int right, int gap) {
for (int i = left; i <= right; i++) {
levelList.set(i, levelList.get(i) - (gap - 1));
}
}

// when we write leading like: leading(t1 {{t2 t3} {t4 t5}} t6)
// levelList would like 0 2 2 3 3 0, it could be reduced to 0 1 1 2 2 0 like leading(t1 {t2 t3 {t4 t5}} t6)
// gap is like 0 to 2 or 3 to 0 in upper example, and this function is to remove gap when we use a lot of braces
private void normalizeLevelList() {
int leftIndex = 0;
// at lease two tables were needed
for (int i = 1; i < levelList.size(); i++) {
if ((levelList.get(i) - levelList.get(leftIndex)) > 1) {
int rightIndex = i;
for (int j = i; j < levelList.size(); j++) {
if ((levelList.get(rightIndex) - levelList.get(j)) > 1) {
removeGap(i, rightIndex, Math.min(levelList.get(i) - levelList.get(leftIndex),
levelList.get(rightIndex) - levelList.get(j)));
}
rightIndex = j;
}
}
leftIndex = i;
}
}

public List<String> getTablelist() {
return tablelist;
}

public List<Integer> getLevellist() {
return levellist;
public List<Integer> getLevelList() {
return levelList;
}

public Map<RelationId, LogicalPlan> getRelationIdToScanMap() {
Expand Down Expand Up @@ -450,10 +478,10 @@ public Plan generateLeadingJoinPlan() {
}
logicalPlan = makeFilterPlanIfExist(getFilters(), logicalPlan);
assert (logicalPlan != null);
stack.push(Pair.of(getLevellist().get(index), logicalPlan));
int stackTopLevel = getLevellist().get(index++);
stack.push(Pair.of(getLevelList().get(index), logicalPlan));
int stackTopLevel = getLevelList().get(index++);
while (index < getTablelist().size()) {
int currentLevel = getLevellist().get(index);
int currentLevel = getLevelList().get(index);
if (currentLevel == stackTopLevel) {
// should return error if can not found table
logicalPlan = getLogicalPlanByName(getTablelist().get(index++));
Expand Down Expand Up @@ -492,7 +520,7 @@ public Plan generateLeadingJoinPlan() {
logicalJoin.setBitmap(LongBitmap.or(getBitmap(newStackTop.second), getBitmap(logicalPlan)));
if (stackTopLevel > 0) {
if (index < getTablelist().size()) {
if (stackTopLevel > getLevellist().get(index)) {
if (stackTopLevel > getLevelList().get(index)) {
stackTopLevel--;
}
} else {
Expand Down
36 changes: 19 additions & 17 deletions regression-test/data/nereids_p0/hint/fix_leading.out
Original file line number Diff line number Diff line change
Expand Up @@ -251,28 +251,30 @@ Used: leading(t1 t2 t3 )
UnUsed:
SyntaxError:

-- !select5_1 --
-- !select6_1 --
PhysicalResultSink
--hashAgg[GLOBAL]
----PhysicalDistribute[DistributionSpecGather]
------hashAgg[LOCAL]
--------PhysicalProject
----------NestedLoopJoin[LEFT_OUTER_JOIN](t3.c3 > 500)
------------PhysicalProject
--------------PhysicalOlapScan[t3]
------------PhysicalDistribute[DistributionSpecReplicated]
--------------PhysicalProject
----------------NestedLoopJoin[LEFT_OUTER_JOIN](t1.c1 > 500)
------------------PhysicalProject
--------------------filter((t1.c1 < 200))
----------------------PhysicalOlapScan[t1]
------------------PhysicalDistribute[DistributionSpecReplicated]
--------------------PhysicalProject
----------------------filter((t2.c2 > 500))
------------------------PhysicalOlapScan[t2]
--------hashJoin[INNER_JOIN] hashCondition=((t1.c1 = t6.c6)) otherCondition=()
----------hashJoin[INNER_JOIN] hashCondition=((t1.c1 = t2.c2) and (t1.c1 = t3.c3) and (t1.c1 = t4.c4) and (t1.c1 = t5.c5)) otherCondition=()
------------PhysicalOlapScan[t1]
------------PhysicalDistribute[DistributionSpecHash]
--------------hashJoin[INNER_JOIN] hashCondition=((t2.c2 = t4.c4) and (t2.c2 = t5.c5) and (t3.c3 = t4.c4) and (t3.c3 = t5.c5)) otherCondition=()
----------------hashJoin[INNER_JOIN] hashCondition=((t2.c2 = t3.c3)) otherCondition=()
------------------PhysicalOlapScan[t2]
------------------PhysicalDistribute[DistributionSpecHash]
--------------------PhysicalOlapScan[t3]
----------------PhysicalDistribute[DistributionSpecHash]
------------------hashJoin[INNER_JOIN] hashCondition=((t4.c4 = t5.c5)) otherCondition=()
--------------------PhysicalOlapScan[t4]
--------------------PhysicalDistribute[DistributionSpecHash]
----------------------PhysicalOlapScan[t5]
----------PhysicalDistribute[DistributionSpecHash]
------------PhysicalOlapScan[t6]

Hint log:
Used:
Used: leading(t1 { { t2 t3 } { t4 t5 } } t6 )
UnUsed:
SyntaxError: leading(t1 t2) Msg:leading should have all tables in query block, missing tables: t3
SyntaxError:

Loading
Loading