[Fix](topn opt) double check plan From OriginalPlanner to make sure optimized SQL is a general topn query (#16848)

From the original logic, query like `select * from a where exists (select * from b order by 1) order by 1 limit 1` is a query contains subquery,
but the top query will pass `checkEnableTwoPhaseRead` and set `isTwoPhaseOptEnabled=true`.So check the double plan is a general topn query plan is needed, and rollback the needMaterialize flag setted by the previous `analyze`.
This commit is contained in:
lihangyu
2023-02-17 10:59:35 +08:00
committed by GitHub
parent 630865a32f
commit 6acee1ce88
7 changed files with 36 additions and 6 deletions

View File

@ -118,6 +118,10 @@ public class DescriptorTable {
return tupleDescs.get(id);
}
public HashMap<SlotId, SlotDescriptor> getSlotDescs() {
return slotDescs;
}
/**
* Return all tuple desc by idList.
*/

View File

@ -660,7 +660,7 @@ public class SelectStmt extends QueryStmt {
// reset slots need to do fetch column
for (SlotRef slot : resultSlots) {
// invalid slots will be pruned from reading from ScanNode
slot.setInvalid();
slot.setNeedMaterialize(false);
}
LOG.debug("resultsSlots {}", resultSlots);

View File

@ -111,8 +111,8 @@ public class SlotDescriptor {
return isAgg;
}
public void setInvalid() {
this.needMaterialize = false;
public void setNeedMaterialize(boolean needMaterialize) {
this.needMaterialize = needMaterialize;
}
public boolean isInvalid() {

View File

@ -124,8 +124,8 @@ public class SlotRef extends Expr {
return desc.getId();
}
public void setInvalid() {
this.desc.setInvalid();
public void setNeedMaterialize(boolean needMaterialize) {
this.desc.setNeedMaterialize(needMaterialize);
}
public boolean isInvalid() {

View File

@ -283,7 +283,17 @@ public class OriginalPlanner extends Planner {
}
} else if (selectStmt.isTwoPhaseReadOptEnabled()) {
// Optimize query like `SELECT ... FROM <tbl> WHERE ... ORDER BY ... LIMIT ...`
injectRowIdColumnSlot();
if (singleNodePlan instanceof SortNode
&& singleNodePlan.getChildren().size() == 1
&& ((SortNode) singleNodePlan).getChild(0) instanceof OlapScanNode) {
// Double check this plan to ensure it's a general topn query
injectRowIdColumnSlot();
} else {
// This is not a general topn query, rollback needMaterialize flag
for (SlotDescriptor slot : analyzer.getDescTbl().getSlotDescs().values()) {
slot.setNeedMaterialize(true);
}
}
}
}
}

View File

@ -15,3 +15,15 @@
-- !sql4 --
1
-- !sql5 --
\N \N \N \N \N \N \N \N \N \N \N \N \N \N
false 1 1989 1001 11011902 123.123 true 1989-03-21 1989-03-21T13:00 wangjuoo4 0.1 6.333 string12345 170141183460469231731687303715884105727
false 2 1986 1001 11011903 1243.500 false 1901-12-31 1989-03-21T13:00 wangynnsf 20.268 789.25 string12345 -170141183460469231731687303715884105727
false 3 1989 1002 11011905 24453.325 false 2012-03-14 2000-01-01T00:00 yunlj8@nk 78945.0 3654.0 string12345 0
false 4 1991 3021 -11011907 243243.325 false 3124-10-10 2015-03-13T10:30 yanvjldjlll 2.06 -0.001 string12345 20220101
false 5 1985 5014 -11011903 243.325 true 2015-01-01 2015-03-13T12:36:38 du3lnvl -0.0 -365.0 string12345 20220102
false 6 32767 3021 123456 604587.000 true 2014-11-11 2015-03-13T12:36:38 yanavnd 0.1 80699.0 string12345 20220104
false 7 -32767 1002 7210457 3.141 false 1988-03-21 1901-01-01T00:00 jiw3n4 0.0 6058.0 string12345 -20220101
true 8 255 2147483647 11011920 -0.123 true 1989-03-21 9999-11-11T12:12 wangjuoo5 987456.123 12.14 string12345 -2022
true 9 1991 -2147483647 11011902 -654.654 true 1991-08-11 1989-03-21T13:11 wangjuoo4 0.0 69.123 string12345 11011903

View File

@ -41,4 +41,8 @@ suite("test_subquery") {
select /*+SET_VAR(enable_projection=false) */
count() from (select k2, k1 from test_query_db.baseall order by k1 limit 1) a;
"""
qt_sql5 """
select * from test_query_db.bigtable where exists (select k2, k1 from test_query_db.baseall order by k1) order by k1, k2, k3, k4 limit 10;
"""
}