From 8a3f4bda62d76136057c8ae9029504b26ffd8826 Mon Sep 17 00:00:00 2001 From: vdimir Date: Mon, 30 May 2022 13:50:22 +0000 Subject: [PATCH] Fix columns number mismatch in cross join --- src/Interpreters/HashJoin.cpp | 9 +++++++-- src/Interpreters/TreeRewriter.cpp | 2 +- .../02313_cross_join_dup_col_names.reference | 2 ++ .../02313_cross_join_dup_col_names.sql | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 tests/queries/0_stateless/02313_cross_join_dup_col_names.reference create mode 100644 tests/queries/0_stateless/02313_cross_join_dup_col_names.sql diff --git a/src/Interpreters/HashJoin.cpp b/src/Interpreters/HashJoin.cpp index b58059312bd5..521fbe139e78 100644 --- a/src/Interpreters/HashJoin.cpp +++ b/src/Interpreters/HashJoin.cpp @@ -707,6 +707,13 @@ namespace void HashJoin::initRightBlockStructure(Block & saved_block_sample) { + if (isCrossOrComma(kind)) + { + /// cross join doesn't have keys, just add all columns + saved_block_sample = sample_block_with_columns_to_add.cloneEmpty(); + return; + } + bool multiple_disjuncts = !table_join->oneDisjunct(); /// We could remove key columns for LEFT | INNER HashJoin but we should keep them for JoinSwitcher (if any). bool save_key_columns = !table_join->forceHashJoin() || isRightOrFull(kind) || multiple_disjuncts; @@ -724,9 +731,7 @@ void HashJoin::initRightBlockStructure(Block & saved_block_sample) for (auto & column : sample_block_with_columns_to_add) { if (!saved_block_sample.findByName(column.name)) - { saved_block_sample.insert(column); - } } } diff --git a/src/Interpreters/TreeRewriter.cpp b/src/Interpreters/TreeRewriter.cpp index c90421d6f4f4..9865928f4eb3 100644 --- a/src/Interpreters/TreeRewriter.cpp +++ b/src/Interpreters/TreeRewriter.cpp @@ -1151,7 +1151,7 @@ TreeRewriterResultPtr TreeRewriter::analyzeSelect( if (remove_duplicates) renameDuplicatedColumns(select_query); - /// Perform it before analyzing JOINs, because it may change number of columns with names unique and break some login inside JOINs + /// Perform it before analyzing JOINs, because it may change number of columns with names unique and break some logic inside JOINs if (settings.optimize_normalize_count_variants) TreeOptimizer::optimizeCountConstantAndSumOne(query); diff --git a/tests/queries/0_stateless/02313_cross_join_dup_col_names.reference b/tests/queries/0_stateless/02313_cross_join_dup_col_names.reference new file mode 100644 index 000000000000..96e34d5a44c2 --- /dev/null +++ b/tests/queries/0_stateless/02313_cross_join_dup_col_names.reference @@ -0,0 +1,2 @@ +\N +\N diff --git a/tests/queries/0_stateless/02313_cross_join_dup_col_names.sql b/tests/queries/0_stateless/02313_cross_join_dup_col_names.sql new file mode 100644 index 000000000000..44a4797ae3c5 --- /dev/null +++ b/tests/queries/0_stateless/02313_cross_join_dup_col_names.sql @@ -0,0 +1,16 @@ +-- Tags: no-backward-compatibility-check + +-- https://github.com/ClickHouse/ClickHouse/issues/37561 + +SELECT NULL +FROM + (SELECT NULL) AS s1, + (SELECT count(2), count(1)) AS s2 +; + +SELECT NULL +FROM + (SELECT NULL) AS s1, + (SELECT count(2.), 9223372036854775806, count('-1'), NULL) AS s2, + (SELECT count('-2147483648')) AS any_query, (SELECT NULL) AS check_single_query +;