Merge pull request #50430 from hanfei1991/hanfei/fix-crossjoin-filter-pushdown

make filter push down through cross join
This commit is contained in:
Alexey Milovidov 2023-06-02 00:35:34 +03:00 committed by GitHub
commit 413b9c776a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 3 deletions

View File

@ -272,7 +272,7 @@ size_t tryPushDownFilter(QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes
{
/// If totals step has HAVING expression, skip it for now.
/// TODO:
/// We can merge HAVING expression with current filer.
/// We can merge HAVING expression with current filter.
/// Also, we can push down part of HAVING which depend only on aggregation keys.
if (totals_having->getActions())
return 0;
@ -323,9 +323,9 @@ size_t tryPushDownFilter(QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes
{
const auto & table_join = join ? join->getJoin()->getTableJoin() : filled_join->getJoin()->getTableJoin();
/// Only inner and left(/right) join are supported. Other types may generate default values for left table keys.
/// Only inner, cross and left(/right) join are supported. Other types may generate default values for left table keys.
/// So, if we push down a condition like `key != 0`, not all rows may be filtered.
if (table_join.kind() != JoinKind::Inner && table_join.kind() != kind)
if (table_join.kind() != JoinKind::Inner && table_join.kind() != JoinKind::Cross && table_join.kind() != kind)
return 0;
bool is_left = kind == JoinKind::Left;

View File

@ -6,3 +6,22 @@ String1_0 String2_0 String3_0 String4_0 1
String1_0 String2_0 String3_0 String4_0 1
1 [0,1,2]
1
Expression ((Projection + Before ORDER BY))
Filter (WHERE)
Join (JOIN FillRightFirst)
Filter (( + Before JOIN))
ReadFromMergeTree (default.t1)
Indexes:
PrimaryKey
Keys:
id
Condition: (id in [101, 101])
Parts: 1/1
Granules: 1/1
Expression ((Joined actions + (Rename joined columns + (Projection + Before ORDER BY))))
ReadFromMergeTree (default.t2)
Indexes:
PrimaryKey
Condition: true
Parts: 1/1
Granules: 1/1

View File

@ -38,6 +38,25 @@ DROP TABLE IF EXISTS Test;
select x, y from (select [0, 1, 2] as y, 1 as a, 2 as b) array join y as x where a = 1 and b = 2 and (x = 1 or x != 1) and x = 1;
DROP TABLE IF EXISTS t;
create table t(a UInt8) engine=MergeTree order by a;
insert into t select * from numbers(2);
select a from t t1 join t t2 on t1.a = t2.a where t1.a;
DROP TABLE IF EXISTS t;
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;
CREATE TABLE t1 (id Int64, create_time DateTime) ENGINE = MergeTree ORDER BY id;
CREATE TABLE t2 (delete_time DateTime) ENGINE = MergeTree ORDER BY delete_time;
insert into t1 values (101, '2023-05-28 00:00:00'), (102, '2023-05-28 00:00:00');
insert into t2 values ('2023-05-31 00:00:00');
EXPLAIN indexes=1 SELECT id, delete_time FROM t1
CROSS JOIN (
SELECT delete_time
FROM t2
) AS d WHERE create_time < delete_time AND id = 101;
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;