Merge pull request #3430 from yandex/fix_subquery_global_counter

Add global counter to subqueries
This commit is contained in:
alexey-milovidov 2018-10-22 14:56:31 +03:00 committed by GitHub
commit 28fc99c556
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 7 deletions

View File

@ -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;
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);

View File

@ -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;

View File

@ -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;