Fix complex queries with GLOBAL IN and UNION ALL (#CLICKHOUSE-3356) (#1339)

* TEST only: why initQueryAnalyzer ?

* Better tests

* missing file

* Missing file

* Add test

* Test fixes

* Fixed FREEZE PARTITION: using only active data parts; acquire snapshot of parts [#CLICKHOUSE-3369].

* Removed tcp_ssl_port by default [#CLICKHOUSE-2].

* Better tests

* comment

* clean

* REmove wrong code

* clean
This commit is contained in:
proller 2017-10-13 00:28:24 +03:00 committed by alexey-milovidov
parent 7bc58340b0
commit 1e961cc69d
5 changed files with 71 additions and 23 deletions

View File

@ -100,30 +100,11 @@ void InterpreterSelectQuery::init(const BlockInputStreamPtr & input, const Names
}
}
if (is_first_select_inside_union_all && (hasAsterisk() || hasAggregation(query)))
{
basicInit(input);
renameColumns();
if (!required_column_names.empty())
rewriteExpressionList(required_column_names);
// We execute this code here, because otherwise the following kind of query would not work
// SELECT X FROM (SELECT * FROM (SELECT 1 AS X, 2 AS Y) UNION ALL SELECT 3, 4)
// because the asterisk is replaced with columns only when query_analyzer objects are created in basicInit().
renameColumns();
if (!required_column_names.empty() && (table_column_names.size() != required_column_names.size()))
{
rewriteExpressionList(required_column_names);
/// Now there is obsolete information to execute the query. We update this information.
initQueryAnalyzer();
}
}
else
{
renameColumns();
if (!required_column_names.empty())
rewriteExpressionList(required_column_names);
basicInit(input);
}
basicInit(input);
}
bool InterpreterSelectQuery::hasAggregation(const ASTSelectQuery & query_ptr)

View File

@ -0,0 +1,10 @@
1
3
34
NOW okay =========================:
34
34
NOW BAD ==========================:
34
34
finish ===========================;

View File

@ -0,0 +1,17 @@
SELECT X FROM (SELECT * FROM (SELECT 1 AS X, 2 AS Y) UNION ALL SELECT 3, 4) ORDER BY X;
DROP TABLE IF EXISTS test.globalin;
CREATE TABLE test.globalin (CounterID UInt32, StartDate Date ) ENGINE = Memory;
INSERT INTO test.globalin VALUES (34, toDate('2017-10-02')), (42, toDate('2017-10-02')), (55, toDate('2017-10-01'));
SELECT * FROM ( SELECT CounterID FROM remote('localhost', 'test', 'globalin') WHERE (CounterID GLOBAL IN ( SELECT toUInt32(34))) GROUP BY CounterID);
SELECT 'NOW okay =========================:';
SELECT CounterID FROM remote('127.0.0.1', 'test', 'globalin') WHERE (CounterID GLOBAL IN ( SELECT toUInt32(34) )) GROUP BY CounterID UNION ALL SELECT CounterID FROM remote('localhost', 'test', 'globalin') WHERE (CounterID GLOBAL IN ( SELECT toUInt32(34))) GROUP BY CounterID;
SELECT 'NOW BAD ==========================:';
SELECT * FROM ( SELECT CounterID FROM remote('127.0.0.1', 'test', 'globalin') WHERE (CounterID GLOBAL IN ( SELECT toUInt32(34) )) GROUP BY CounterID UNION ALL SELECT CounterID FROM remote('localhost', 'test', 'globalin') WHERE (CounterID GLOBAL IN ( SELECT toUInt32(34))) GROUP BY CounterID);
SELECT 'finish ===========================;';
DROP TABLE test.globalin;

View File

@ -0,0 +1,16 @@
3 8
23 48
33 68
13 28
3 8
23 48
33 68
13 28
3 8
23 48
33 68
13 28
3 8
23 48
33 68
13 28

View File

@ -0,0 +1,24 @@
-- https://github.com/yandex/ClickHouse/issues/1059
DROP TABLE IF EXISTS test.union1;
DROP TABLE IF EXISTS test.union2;
DROP TABLE IF EXISTS test.union3;
CREATE TABLE test.union1 ( date Date, a Int32, b Int32, c Int32, d Int32) ENGINE = MergeTree(date, (a, date), 8192);
CREATE TABLE test.union2 ( date Date, a Int32, b Int32, c Int32, d Int32) ENGINE = Distributed(test_shard_localhost, 'test', 'union1');
CREATE TABLE test.union3 ( date Date, a Int32, b Int32, c Int32, d Int32) ENGINE = Distributed(test_shard_localhost, 'test', 'union2');
INSERT INTO test.union1 VALUES (1, 2, 3, 4, 5);
INSERT INTO test.union1 VALUES (11,12,13,14,15);
INSERT INTO test.union2 VALUES (21,22,23,24,25);
INSERT INTO test.union3 VALUES (31,32,33,34,35);
select b, sum(c) from ( select a, b, sum(c) as c from test.union2 where a>1 group by a,b UNION ALL select a, b, sum(c) as c from test.union2 where b>1 group by a, b ) as a group by b;
select b, sum(c) from ( select a, b, sum(c) as c from test.union1 where a>1 group by a,b UNION ALL select a, b, sum(c) as c from test.union2 where b>1 group by a, b ) as a group by b;
select b, sum(c) from ( select a, b, sum(c) as c from test.union1 where a>1 group by a,b UNION ALL select a, b, sum(c) as c from test.union1 where b>1 group by a, b ) as a group by b;
select b, sum(c) from ( select a, b, sum(c) as c from test.union2 where a>1 group by a,b UNION ALL select a, b, sum(c) as c from test.union3 where b>1 group by a, b ) as a group by b;
DROP TABLE test.union1;
DROP TABLE test.union2;
DROP TABLE test.union3;