Merge 'fix/fix_prune_sorting_info' into 'cnch-dev'

fix(optimizer@m-5358993151): fix PruneSortingInfo log wrong warning if order by don't match in table order keys cnch-dev

See merge request: !25876
This commit is contained in:
李开希 2024-10-18 09:50:59 +00:00 committed by Fred Wang
parent af3173ffd9
commit fdbd1128f7
3 changed files with 126 additions and 39 deletions

View File

@ -237,29 +237,17 @@ PlanNodePtr PruneSortingInfoRewriter::visitTableScanNode(TableScanNode & node, S
// prune unused read order columns
// eg, select * from table(order by a,b,c) where a = 'x' and d = 'y' order by b,d
// required sort columns may be: b,d; read order columns should be a,b
// required sort columns: b,d; read order columns should be a,b
// eg2, select * from table(order by c,d,e) where a = 'x' order by a, b
// required sort columns: a, read order columns is empty
auto read_order = step->getReadOrder();
auto it = std::find_if(read_order.rbegin(), read_order.rend(), [&](const SortColumnDescription & sort_column) {
return required_columns.contains(sort_column.column_name);
});
SortDescription pruned_read_order(read_order.begin(), read_order.begin() + std::distance(it, read_order.rend()));
if (!required.sort_desc.empty() && pruned_read_order.empty())
{
// do nothing if all columns in required don't exist in table
if (logger->error())
{
Names names;
for (const auto & desc : required.sort_desc)
names.emplace_back(desc.column_name);
LOG_WARNING(logger, "unkown required sorting: {}", fmt::format("{}", fmt::join(names, ", ")));
}
}
else
{
node.getStep()->setReadOrder(pruned_read_order);
}
node.getStep()->setReadOrder(pruned_read_order);
return node.shared_from_this();
}

View File

@ -1,7 +1,7 @@
-- { echoOn }
explain select * from test order by a, b, c, d;
Projection Est. ? rows
│ Expressions: [a, b, c, d]
│ Expressions: [a, b, c, d, e]
└─ Sorting Est. ? rows
│ Order by: {a ASC NULLS LAST, b ASC NULLS LAST, c ASC NULLS LAST, d ASC NULLS LAST}
└─ Gather Exchange Est. ? rows
@ -10,10 +10,10 @@ Projection Est. ? rows
│ Prefix Order: {a, b, c}
└─ TableScan default.test Est. ? rows
Input Order Info: {a ASC ANY, b ASC ANY, c ASC ANY}
Outputs: [a, b, c, d]
Outputs: [a, b, c, d, e]
explain select * from test order by a desc, b desc, c desc, d desc;
Projection Est. ? rows
│ Expressions: [a, b, c, d]
│ Expressions: [a, b, c, d, e]
└─ Sorting Est. ? rows
│ Order by: {a DESC NULLS LAST, b DESC NULLS LAST, c DESC NULLS LAST, d DESC NULLS LAST}
└─ Gather Exchange Est. ? rows
@ -22,10 +22,10 @@ Projection Est. ? rows
│ Prefix Order: {a, b, c}
└─ TableScan default.test Est. ? rows
Input Order Info: {a DESC ANY, b DESC ANY, c DESC ANY}
Outputs: [a, b, c, d]
Outputs: [a, b, c, d, e]
explain select * from test order by a, b desc, c;
Projection Est. ? rows
│ Expressions: [a, b, c, d]
│ Expressions: [a, b, c, d, e]
└─ Sorting Est. ? rows
│ Order by: {a ASC NULLS LAST, b DESC NULLS LAST, c ASC NULLS LAST}
└─ Gather Exchange Est. ? rows
@ -34,10 +34,10 @@ Projection Est. ? rows
│ Prefix Order: {a}
└─ TableScan default.test Est. ? rows
Input Order Info: {a ASC ANY}
Outputs: [a, b, c, d]
Outputs: [a, b, c, d, e]
explain select * from test order by a desc, b, c desc;
Projection Est. ? rows
│ Expressions: [a, b, c, d]
│ Expressions: [a, b, c, d, e]
└─ Sorting Est. ? rows
│ Order by: {a DESC NULLS LAST, b ASC NULLS LAST, c DESC NULLS LAST}
└─ Gather Exchange Est. ? rows
@ -46,7 +46,7 @@ Projection Est. ? rows
│ Prefix Order: {a}
└─ TableScan default.test Est. ? rows
Input Order Info: {a DESC ANY}
Outputs: [a, b, c, d]
Outputs: [a, b, c, d, e]
explain select a, b, concat(c, d) as e from test order by a, b, e;
Projection Est. ? rows
│ Expressions: [a, b], e:=`expr#concat(c, d)`
@ -91,7 +91,7 @@ Projection Est. ? rows
Outputs: [a, b, c, d]
explain select * from test where a = 'x' order by b, c, d;
Projection Est. ? rows
│ Expressions: [a, b, c, d]
│ Expressions: [a, b, c, d, e]
└─ Sorting Est. ? rows
│ Order by: {b ASC NULLS LAST, c ASC NULLS LAST, d ASC NULLS LAST}
└─ Gather Exchange Est. ? rows
@ -103,10 +103,10 @@ Projection Est. ? rows
└─ TableScan default.test Est. ? rows
Input Order Info: {a ASC ANY, b ASC ANY, c ASC ANY}
Where: a = \'x\'
Outputs: [a, b, c, d]
Outputs: [a, b, c, d, e]
explain select * from test where a = 'x' order by b desc, c desc, d desc;
Projection Est. ? rows
│ Expressions: [a, b, c, d]
│ Expressions: [a, b, c, d, e]
└─ Sorting Est. ? rows
│ Order by: {b DESC NULLS LAST, c DESC NULLS LAST, d DESC NULLS LAST}
└─ Gather Exchange Est. ? rows
@ -118,10 +118,10 @@ Projection Est. ? rows
└─ TableScan default.test Est. ? rows
Input Order Info: {a DESC ANY, b DESC ANY, c DESC ANY}
Where: a = \'x\'
Outputs: [a, b, c, d]
Outputs: [a, b, c, d, e]
explain select * from test where a = 'x' and d = 'z' order by d, b, c;
Projection Est. ? rows
│ Expressions: [a, b, c, d]
│ Expressions: [a, b, c, d, e]
└─ Sorting Est. ? rows
│ Order by: {d ASC NULLS LAST, b ASC NULLS LAST, c ASC NULLS LAST}
└─ Gather Exchange Est. ? rows
@ -133,10 +133,10 @@ Projection Est. ? rows
└─ TableScan default.test Est. ? rows
Input Order Info: {a ASC ANY, b ASC ANY, c ASC ANY}
Where: (a = \'x\') AND (d = \'z\')
Outputs: [a, b, c, d]
Outputs: [a, b, c, d, e]
explain select * from test where a = 'x' and d = 'z' order by d desc, b desc, c desc;
Projection Est. ? rows
│ Expressions: [a, b, c, d]
│ Expressions: [a, b, c, d, e]
└─ Sorting Est. ? rows
│ Order by: {d DESC NULLS LAST, b DESC NULLS LAST, c DESC NULLS LAST}
└─ Gather Exchange Est. ? rows
@ -148,7 +148,7 @@ Projection Est. ? rows
└─ TableScan default.test Est. ? rows
Input Order Info: {a DESC ANY, b DESC ANY, c DESC ANY}
Where: (a = \'x\') AND (d = \'z\')
Outputs: [a, b, c, d]
Outputs: [a, b, c, d, e]
explain pipeline select * from test where a = 'x' order by b, c limit 10;
Segment[ 1 ] :
@ -228,6 +228,91 @@ Projection Est. 10 rows, cost 7.400000e-01
Input Order Info: {a DESC ANY, b DESC ANY, c DESC ANY}
Where: (a = \'3\') AND (b = \'3\')
Outputs: a_1:=a, b_1:=b, c_1:=c
explain select a from test where d = 'x' order by d;
Projection Est. ? rows
│ Expressions: [a]
└─ Sorting Est. ? rows
│ Order by: {d ASC NULLS LAST}
└─ Gather Exchange Est. ? rows
└─ Sorting Est. ? rows
│ Order by: {d ASC NULLS LAST}
│ Prefix Order: {d}
└─ Filter Est. ? rows
│ Condition: d = \'x\'
└─ TableScan default.test Est. ? rows
Where: d = \'x\'
Outputs: [a, d]
explain select a from test where d = 'x' order by d, e;
Projection Est. ? rows
│ Expressions: [a]
└─ Sorting Est. ? rows
│ Order by: {d ASC NULLS LAST, e ASC NULLS LAST}
└─ Gather Exchange Est. ? rows
└─ Sorting Est. ? rows
│ Order by: {d ASC NULLS LAST, e ASC NULLS LAST}
│ Prefix Order: {d}
└─ Filter Est. ? rows
│ Condition: d = \'x\'
└─ TableScan default.test Est. ? rows
Where: d = \'x\'
Outputs: [a, d, e]
explain select a from test where d = 'x' and e = 'z' order by d, e;
Projection Est. ? rows
│ Expressions: [a]
└─ Sorting Est. ? rows
│ Order by: {d ASC NULLS LAST, e ASC NULLS LAST}
└─ Gather Exchange Est. ? rows
└─ Sorting Est. ? rows
│ Order by: {d ASC NULLS LAST, e ASC NULLS LAST}
│ Prefix Order: {d, e}
└─ Filter Est. ? rows
│ Condition: (d = \'x\') AND (e = \'z\')
└─ TableScan default.test Est. ? rows
Where: (d = \'x\') AND (e = \'z\')
Outputs: [a, d, e]
explain select a from test order by a || c, a || b;
Projection Est. ? rows
│ Expressions: [a]
└─ Sorting Est. ? rows
│ Order by: {expr#concat(a, c) ASC NULLS LAST, expr#concat(a, b) ASC NULLS LAST}
└─ Gather Exchange Est. ? rows
└─ Sorting Est. ? rows
│ Order by: {expr#concat(a, c) ASC NULLS LAST, expr#concat(a, b) ASC NULLS LAST}
└─ Projection Est. ? rows
│ Expressions: [a], expr#concat(a, b):=concat(a, b), expr#concat(a, c):=concat(a, c)
└─ TableScan default.test Est. ? rows
Outputs: [a, b, c]
explain select a from test order by a, a || b, b;
Projection Est. ? rows
│ Expressions: [a]
└─ Sorting Est. ? rows
│ Order by: {a ASC NULLS LAST, expr#concat(a, b) ASC NULLS LAST, b ASC NULLS LAST}
└─ Gather Exchange Est. ? rows
└─ Sorting Est. ? rows
│ Order by: {a ASC NULLS LAST, expr#concat(a, b) ASC NULLS LAST, b ASC NULLS LAST}
│ Prefix Order: {a}
└─ Projection Est. ? rows
│ Expressions: [a, b], expr#concat(a, b):=concat(a, b)
└─ TableScan default.test Est. ? rows
Input Order Info: {a ASC ANY}
Outputs: [a, b]
explain select a from test where (a || b) = 'xy' order by a, a || b, b;
Projection Est. ? rows
│ Expressions: [a]
└─ Sorting Est. ? rows
│ Order by: {a ASC NULLS LAST, expr#concat(a, b) ASC NULLS LAST, b ASC NULLS LAST}
└─ Gather Exchange Est. ? rows
└─ Sorting Est. ? rows
│ Order by: {a ASC NULLS LAST, expr#concat(a, b) ASC NULLS LAST, b ASC NULLS LAST}
│ Prefix Order: {a}
└─ Projection Est. ? rows
│ Expressions: [a, b], expr#concat(a, b):=concat(a, b)
└─ Filter Est. ? rows
│ Condition: concat(a, b) = \'xy\'
└─ TableScan default.test Est. ? rows
Input Order Info: {a ASC ANY}
Where: concat(a, b) = \'xy\'
Outputs: [a, b]
-- { echoOn }
select a as e, b, concat(c, d) as f from test order by e desc, b desc, f desc;
3 3 44

View File

@ -3,7 +3,8 @@ CREATE TABLE test
`a` String,
`b` String,
`c` String,
`d` Nullable(String)
`d` Nullable(String),
`e` String
)
ENGINE = CnchMergeTree
ORDER BY (a, b, c)
@ -11,6 +12,7 @@ SETTINGS storage_policy = 'cnch_default_hdfs', index_granularity = 8192;
set enable_optimizer = 1;
set enable_sorting_property = 1;
set send_logs_level='warning';
-- { echoOn }
explain select * from test order by a, b, c, d;
@ -41,12 +43,24 @@ explain select a, b, c from (select a, b, c from test union all select a, b, c f
explain select a, b, c from (select a, b, c from test where a = '1' and b = '1' union all select a, b, c from test where a = '3' and b = '3') order by c desc limit 10;
-- { echoOff }
insert into test values ('1', '1', '1', '1');
insert into test values ('1', '1', '2', '2');
explain select a from test where d = 'x' order by d;
insert into test values ('3', '3', '3', '3');
insert into test values ('3', '3', '4', '4');
explain select a from test where d = 'x' order by d, e;
explain select a from test where d = 'x' and e = 'z' order by d, e;
explain select a from test order by a || c, a || b;
explain select a from test order by a, a || b, b;
explain select a from test where (a || b) = 'xy' order by a, a || b, b;
-- { echoOff }
insert into test values ('1', '1', '1', '1', 1);
insert into test values ('1', '1', '2', '2', 2);
insert into test values ('3', '3', '3', '3', 3);
insert into test values ('3', '3', '4', '4', 4);
-- { echoOn }
select a as e, b, concat(c, d) as f from test order by e desc, b desc, f desc;