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

Add global counter to subqueries #3430

Merged
merged 3 commits into from
Oct 22, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions dbms/src/Interpreters/QueryAliasesVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ void QueryAliasesVisitor::getNodeAlias(const ASTPtr & ast, Aliases & aliases, co
}
else if (auto subquery = typeid_cast<ASTSubquery *>(ast.get()))
{
/// Set unique aliases for all subqueries. This is needed, because content of subqueries could change after recursive analysis,
/// and auto-generated column names could become incorrect.
/// Set unique aliases for all subqueries. This is needed, because:
/// 1) content of subqueries could change after recursive analysis, and auto-generated column names could become incorrect
/// 2) result of different scalar subqueries can be cached inside expressions compilation cache and must have different names

if (subquery->alias.empty())
{
size_t subquery_index = 1;
static std::atomic_uint64_t subquery_index = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing comment.

while (true)
{
alias = "_subquery" + toString(subquery_index);
if (!aliases.count("_subquery" + toString(subquery_index)))
alias = "_subquery" + std::to_string(subquery_index++);
if (!aliases.count(alias))
break;
++subquery_index;
}

subquery->setAlias(alias);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SELECT * FROM (SELECT 1 AS id UNION ALL SELECT 2) WHERE id = 1;
SELECT * FROM (SELECT arrayJoin([1, 2, 3]) AS id) WHERE id = 1;
SELECT id FROM (SELECT arrayJoin([1, 2, 3]) AS id) WHERE id = 1;

SELECT * FROM (SELECT 1 AS id, (SELECT 1)) WHERE _subquery1 = 1;
SELECT * FROM (SELECT 1 AS id, (SELECT 1) as subquery) WHERE subquery = 1;
SELECT * FROM (SELECT toUInt64(b) AS a, sum(id) AS b FROM test.test) WHERE a = 3;
SELECT * FROM (SELECT toUInt64(b), sum(id) AS b FROM test.test) WHERE `toUInt64(sum(id))` = 3;
SELECT date, id, name, value FROM (SELECT date, name, value, min(id) AS id FROM test.test GROUP BY date, name, value) WHERE id = 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
0
23 changes: 23 additions & 0 deletions dbms/tests/queries/0_stateless/00745_compile_scalar_subquery.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
SET compile_expressions = 1;
SET min_count_to_compile = 1;
SET optimize_move_to_prewhere = 0;
SET enable_optimize_predicate_expression=0;

DROP TABLE IF EXISTS test.dt;
DROP TABLE IF EXISTS test.testx;

CREATE TABLE test.dt(tkey Int32) ENGINE = MergeTree order by tuple();
INSERT INTO test.dt VALUES (300000);
CREATE TABLE test.testx(t Int32, a UInt8) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO test.testx VALUES (100000, 0);

SELECT COUNT(*) FROM test.testx WHERE NOT a AND t < (SELECT tkey FROM test.dt);

DROP TABLE test.dt;
CREATE TABLE test.dt(tkey Int32) ENGINE = MergeTree order by tuple();
INSERT INTO test.dt VALUES (0);

SELECT COUNT(*) FROM test.testx WHERE NOT a AND t < (SELECT tkey FROM test.dt);

DROP TABLE IF EXISTS test.dt;
DROP TABLE IF EXISTS test.testx;