Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect JOIN plan optimization with partially materialized normal projection #57196

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ bool optimizeUseNormalProjections(Stack & stack, QueryPlan::Nodes & nodes)
}
else
{
const auto & main_stream = iter->node->children.front()->step->getOutputStream();
const auto & main_stream = iter->node->children[iter->next_child - 1]->step->getOutputStream();
const auto * proj_stream = &next_node->step->getOutputStream();

if (auto materializing = makeMaterializingDAG(proj_stream->header, main_stream.header))
Expand All @@ -284,7 +284,7 @@ bool optimizeUseNormalProjections(Stack & stack, QueryPlan::Nodes & nodes)
auto & union_node = nodes.emplace_back();
DataStreams input_streams = {main_stream, *proj_stream};
union_node.step = std::make_unique<UnionStep>(std::move(input_streams));
union_node.children = {iter->node->children.front(), next_node};
union_node.children = {iter->node->children[iter->next_child - 1], next_node};
iter->node->children[iter->next_child - 1] = &union_node;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;

CREATE TABLE t1 (id UInt32, s String) Engine = MergeTree ORDER BY id;
CREATE TABLE t2 (id1 UInt32, id2 UInt32) Engine = MergeTree ORDER BY id1 SETTINGS index_granularity = 1;
INSERT INTO t2 SELECT number, number from numbers(100);
ALTER TABLE t2 ADD PROJECTION proj (SELECT id2 ORDER BY id2);
INSERT INTO t2 SELECT number, number from numbers(100);

SELECT s FROM t1 as lhs LEFT JOIN (SELECT * FROM t2 WHERE id2 = 2) as rhs ON lhs.id = rhs.id2;

DROP TABLE t1;
DROP TABLE t2;