From 84e838d038f8b56807cecd4ca2cecee84352b7af Mon Sep 17 00:00:00 2001 From: vdimir Date: Thu, 1 Jun 2023 10:52:04 +0000 Subject: [PATCH] Fix 02382_join_and_filtering_set Copy missing corresponding code from Interpreter, adjust test (removed with cube) --- src/Planner/PlannerJoinTree.cpp | 21 ++++++++++++++++++- tests/analyzer_tech_debt.txt | 1 - .../02382_join_and_filtering_set.reference | 3 --- .../02382_join_and_filtering_set.sql | 6 +++--- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index c95671da6be..597de16cf04 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -1248,7 +1248,26 @@ JoinTreeQueryPlan buildQueryPlanForJoinNode(const QueryTreeNodePtr & join_table_ const auto & join_clause = table_join->getOnlyClause(); bool kind_allows_filtering = isInner(join_kind) || isLeft(join_kind) || isRight(join_kind); - if (settings.max_rows_in_set_to_optimize_join > 0 && kind_allows_filtering) + + auto has_non_const = [](const Block & block, const auto & keys) + { + for (const auto & key : keys) + { + const auto & column = block.getByName(key).column; + if (column && !isColumnConst(*column)) + return true; + } + return false; + }; + + /// This optimization relies on the sorting that should buffer the whole stream before emitting any rows. + /// It doesn't hold such a guarantee for streams with const keys. + /// Note: it's also doesn't work with the read-in-order optimization. + /// No checks here because read in order is not applied if we have `CreateSetAndFilterOnTheFlyStep` in the pipeline between the reading and sorting steps. + bool has_non_const_keys = has_non_const(left_plan.getCurrentDataStream().header, join_clause.key_names_left) + && has_non_const(right_plan.getCurrentDataStream().header, join_clause.key_names_right); + + if (settings.max_rows_in_set_to_optimize_join > 0 && kind_allows_filtering && has_non_const_keys) { auto * left_set = add_create_set(left_plan, join_clause.key_names_left, JoinTableSide::Left); auto * right_set = add_create_set(right_plan, join_clause.key_names_right, JoinTableSide::Right); diff --git a/tests/analyzer_tech_debt.txt b/tests/analyzer_tech_debt.txt index 95898bc70b0..a100210d36c 100644 --- a/tests/analyzer_tech_debt.txt +++ b/tests/analyzer_tech_debt.txt @@ -82,7 +82,6 @@ 02354_annoy 02366_union_decimal_conversion 02375_rocksdb_with_filters -02382_join_and_filtering_set 02402_merge_engine_with_view 02404_memory_bound_merging 02426_orc_bug diff --git a/tests/queries/0_stateless/02382_join_and_filtering_set.reference b/tests/queries/0_stateless/02382_join_and_filtering_set.reference index f81cf99bba7..8d68a1e392e 100644 --- a/tests/queries/0_stateless/02382_join_and_filtering_set.reference +++ b/tests/queries/0_stateless/02382_join_and_filtering_set.reference @@ -7,8 +7,5 @@ 10 bug with constant columns in join keys a a -a a -a a -a a 1 1 diff --git a/tests/queries/0_stateless/02382_join_and_filtering_set.sql b/tests/queries/0_stateless/02382_join_and_filtering_set.sql index b9dd44721dd..69bb8e7c222 100644 --- a/tests/queries/0_stateless/02382_join_and_filtering_set.sql +++ b/tests/queries/0_stateless/02382_join_and_filtering_set.sql @@ -22,20 +22,20 @@ SELECT count() FROM t1 JOIN t2 ON t1.x = t2.x WHERE t1.x % 2 == 0 AND t2.x % 2 = SELECT 'bug with constant columns in join keys'; SELECT * FROM ( SELECT 'a' AS key ) AS t1 -INNER JOIN ( SELECT 'a' AS key GROUP BY ignore(1), ignore(2) WITH CUBE ) AS t2 +INNER JOIN ( SELECT 'a' AS key ) AS t2 ON t1.key = t2.key ; SELECT count() > 1 FROM (EXPLAIN PIPELINE SELECT * FROM ( SELECT materialize('a') AS key ) AS t1 - INNER JOIN ( SELECT materialize('a') AS key GROUP BY ignore(1), ignore(2) WITH CUBE ) AS t2 + INNER JOIN ( SELECT materialize('a') AS key ) AS t2 ON t1.key = t2.key ) WHERE explain ilike '%FilterBySetOnTheFlyTransform%' ; SELECT count() == 0 FROM (EXPLAIN PIPELINE SELECT * FROM ( SELECT 'a' AS key ) AS t1 - INNER JOIN ( SELECT 'a' AS key GROUP BY ignore(1), ignore(2) WITH CUBE ) AS t2 + INNER JOIN ( SELECT 'a' AS key ) AS t2 ON t1.key = t2.key ) WHERE explain ilike '%FilterBySetOnTheFlyTransform%' ;