Skip to content

Commit

Permalink
Fix columns number mismatch in cross join
Browse files Browse the repository at this point in the history
  • Loading branch information
vdimir committed May 30, 2022
1 parent 55f1faf commit 8a3f4bd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/Interpreters/HashJoin.cpp
Expand Up @@ -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;
Expand All @@ -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);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Interpreters/TreeRewriter.cpp
Expand Up @@ -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);

Expand Down
@@ -0,0 +1,2 @@
\N
\N
16 changes: 16 additions & 0 deletions 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
;

0 comments on commit 8a3f4bd

Please sign in to comment.