mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-10 01:25:21 +00:00
Cover with tests how identifiers in SELECT queries are resolved
Add queries from https://github.com/ClickHouse/ClickHouse/issues/23194
This commit is contained in:
parent
ea9dc2e2b4
commit
baf9ca5fa9
@ -0,0 +1,11 @@
|
||||
0 2 1
|
||||
---
|
||||
1
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
---
|
||||
1
|
||||
2
|
||||
3
|
53
tests/queries/0_stateless/03101_analyzer_identifiers_1.sql
Normal file
53
tests/queries/0_stateless/03101_analyzer_identifiers_1.sql
Normal file
@ -0,0 +1,53 @@
|
||||
-- https://github.com/ClickHouse/ClickHouse/issues/23194
|
||||
-- This test add query-templates for fuzzer
|
||||
SET allow_experimental_analyzer = 1;
|
||||
|
||||
DROP DATABASE IF EXISTS {CLICKHOUSE_DATABASE:Identifier};
|
||||
CREATE DATABASE {CLICKHOUSE_DATABASE:Identifier};
|
||||
USE {CLICKHOUSE_DATABASE:Identifier};
|
||||
|
||||
CREATE TABLE table (
|
||||
column UInt64,
|
||||
nest Nested
|
||||
(
|
||||
key Nested (
|
||||
subkey UInt16
|
||||
)
|
||||
)
|
||||
) ENGINE = Memory();
|
||||
|
||||
|
||||
SELECT t.column FROM table AS t;
|
||||
|
||||
USE default;
|
||||
SELECT column FROM {CLICKHOUSE_DATABASE:Identifier}.table;
|
||||
USE {CLICKHOUSE_DATABASE:Identifier};
|
||||
|
||||
|
||||
SELECT {CLICKHOUSE_DATABASE:Identifier}.table.column FROM table;
|
||||
|
||||
--
|
||||
|
||||
SELECT t1.x, t2.x, y FROM
|
||||
(SELECT x, y FROM VALUES ('x UInt16, y UInt16', (0,1))) AS t1,
|
||||
(SELECT x, z FROM VALUES ('x UInt16, z UInt16', (2,3))) AS t2;
|
||||
|
||||
SELECT '---';
|
||||
|
||||
SELECT 1;
|
||||
SELECT dummy;
|
||||
SELECT one.dummy;
|
||||
SELECT system.one.dummy;
|
||||
|
||||
SELECT *;
|
||||
|
||||
--
|
||||
|
||||
SELECT nest.key.subkey FROM table;
|
||||
SELECT table.nest FROM table ARRAY JOIN nest;
|
||||
|
||||
SELECT '---';
|
||||
|
||||
SELECT * FROM (SELECT [1, 2, 3] AS arr) ARRAY JOIN arr;
|
||||
|
||||
SELECT * FROM table ARRAY JOIN [1, 2, 3] AS arr;
|
@ -0,0 +1,23 @@
|
||||
1 1 2
|
||||
1 2 1
|
||||
3 6
|
||||
---
|
||||
123
|
||||
---
|
||||
123 123
|
||||
123 1
|
||||
---
|
||||
555
|
||||
---
|
||||
99
|
||||
---
|
||||
1
|
||||
---
|
||||
1
|
||||
---
|
||||
2 2
|
||||
2 2
|
||||
---
|
||||
[2,3]
|
||||
1 [5,14]
|
||||
1 [5,14]
|
85
tests/queries/0_stateless/03101_analyzer_identifiers_2.sql
Normal file
85
tests/queries/0_stateless/03101_analyzer_identifiers_2.sql
Normal file
@ -0,0 +1,85 @@
|
||||
-- https://github.com/ClickHouse/ClickHouse/issues/23194
|
||||
SET allow_experimental_analyzer = 1;
|
||||
|
||||
CREATE TEMPORARY TABLE test1 (a String, nest Nested(x String, y String));
|
||||
|
||||
SELECT a, nest.* FROM test1 ARRAY JOIN nest;
|
||||
SELECT a, n.* FROM test1 ARRAY JOIN nest AS n;
|
||||
|
||||
CREATE TEMPORARY TABLE test2 (a String, nest Array(Tuple(x String, y String)));
|
||||
|
||||
SELECT a, nest.* FROM test2 ARRAY JOIN nest;
|
||||
SELECT a, n.* FROM test2 ARRAY JOIN nest AS n;
|
||||
|
||||
|
||||
SELECT 1 AS x, x, x + 1;
|
||||
SELECT x, x + 1, 1 AS x;
|
||||
SELECT x, 1 + (2 + (3 AS x));
|
||||
|
||||
SELECT '---';
|
||||
|
||||
SELECT 123 AS x FROM (SELECT a, x FROM (SELECT 1 AS a, 2 AS b));
|
||||
|
||||
SELECT '---';
|
||||
|
||||
SELECT 123 AS x, (SELECT x) AS y;
|
||||
SELECT 123 AS x, 123 IN (SELECT x);
|
||||
|
||||
SELECT '---';
|
||||
|
||||
WITH 123 AS x SELECT 555 FROM (SELECT a, x FROM (SELECT 1 AS a, 2 AS b));
|
||||
|
||||
SELECT '---';
|
||||
|
||||
-- here we refer to table `test1` (defined as subquery) three times, one of them inside another scalar subquery.
|
||||
WITH t AS (SELECT 1) SELECT t, (SELECT * FROM t) FROM t; -- { serverError UNKNOWN_IDENTIFIER }
|
||||
|
||||
-- throws, because x is not visible outside.
|
||||
SELECT x FROM (SELECT y FROM VALUES ('y UInt16', (2)) WHERE (1 AS x) = y) AS t; -- { serverError UNKNOWN_IDENTIFIER }
|
||||
|
||||
-- throws, because the table name `t` is not visible outside
|
||||
SELECT t.x FROM (SELECT * FROM (SELECT 1 AS x) AS t); -- { serverError UNKNOWN_IDENTIFIER }
|
||||
SELECT x FROM (SELECT * FROM (SELECT 99 AS x) AS t);
|
||||
|
||||
SELECT '---';
|
||||
|
||||
SELECT t.x FROM (SELECT 1 AS x) AS t;
|
||||
SELECT t.a FROM (SELECT a FROM test1) AS t;
|
||||
SELECT a FROM (SELECT a FROM test1) AS t;
|
||||
|
||||
SELECT '---';
|
||||
|
||||
-- this is wrong, the `tbl` name is not exported
|
||||
SELECT test1.a FROM (SELECT a FROM test1) AS t; -- { serverError UNKNOWN_IDENTIFIER }
|
||||
-- this is also wrong, the `t2` alias is not exported
|
||||
SELECT test1.a FROM (SELECT a FROM test1 AS t2) AS t; -- { serverError UNKNOWN_IDENTIFIER }
|
||||
|
||||
|
||||
-- does not work, `x` is not visible;
|
||||
SELECT x, (SELECT 1 AS x); -- { serverError UNKNOWN_IDENTIFIER }
|
||||
-- does not work either;
|
||||
SELECT x IN (SELECT 1 AS x); -- { serverError UNKNOWN_IDENTIFIER }
|
||||
-- this will work, but keep in mind that there are two different `x`.
|
||||
SELECT x IN (SELECT 1 AS x) FROM (SELECT 1 AS x);
|
||||
|
||||
SELECT '---';
|
||||
|
||||
SELECT x + 1 AS x, x FROM (SELECT 1 AS x);
|
||||
SELECT x, x + 1 AS x FROM (SELECT 1 AS x);
|
||||
SELECT 1 AS x, 2 AS x; -- { serverError MULTIPLE_EXPRESSIONS_FOR_ALIAS }
|
||||
|
||||
SELECT '---';
|
||||
|
||||
|
||||
SELECT arrayMap(x -> x + 1, [1, 2]);
|
||||
|
||||
SELECT x, arrayMap((x, y) -> x[1] + y + arrayFirst(x -> x != y, x), arr) FROM (SELECT 1 AS x, [([1, 2], 3), ([4, 5], 6)] AS arr);
|
||||
|
||||
SELECT x1, arrayMap((x2, y2) -> x2[1] + y2 + arrayFirst(x3 -> x3 != y2, x2), arr) FROM (SELECT 1 AS x1, [([1, 2], 3), ([4, 5], 6)] AS arr);
|
||||
|
||||
SELECT arrayMap(x -> [y * 2, (x + 1) AS y, 1 AS z], [1, 2]), y; -- { serverError UNKNOWN_IDENTIFIER }
|
||||
|
||||
-- TODO: this must work
|
||||
--SELECT arrayMap(x -> [y * 2, (x + 1) AS y, 1 AS z], [1, 2]), z;
|
||||
|
||||
SELECT arrayMap(x -> (x + 1) AS y, [3, 5]), arrayMap(x -> (x || 'hello') AS y, ['qq', 'ww']); -- { serverError MULTIPLE_EXPRESSIONS_FOR_ALIAS }
|
@ -0,0 +1,13 @@
|
||||
0
|
||||
2
|
||||
20
|
||||
---
|
||||
0
|
||||
0
|
||||
---
|
||||
1 2
|
||||
1 2
|
||||
---
|
||||
1 1
|
||||
1 1
|
||||
---
|
90
tests/queries/0_stateless/03101_analyzer_identifiers_3.sql
Normal file
90
tests/queries/0_stateless/03101_analyzer_identifiers_3.sql
Normal file
@ -0,0 +1,90 @@
|
||||
-- https://github.com/ClickHouse/ClickHouse/issues/23194
|
||||
SET allow_experimental_analyzer = 1;
|
||||
|
||||
DROP DATABASE IF EXISTS db1_03101;
|
||||
DROP DATABASE IF EXISTS db2_03101;
|
||||
CREATE DATABASE db1_03101;
|
||||
CREATE DATABASE db2_03101;
|
||||
USE db1_03101;
|
||||
|
||||
CREATE TABLE db1_03101.tbl
|
||||
(
|
||||
col String,
|
||||
db1_03101 Nested
|
||||
(
|
||||
tbl Nested
|
||||
(
|
||||
col String
|
||||
)
|
||||
)
|
||||
)
|
||||
ENGINE = Memory;
|
||||
|
||||
SELECT db1_03101.tbl.col FROM db1_03101.tbl;
|
||||
|
||||
|
||||
SELECT db1_03101.* FROM tbl;
|
||||
SELECT db1_03101 FROM tbl;
|
||||
|
||||
|
||||
SELECT * FROM tbl;
|
||||
SELECT count(*) FROM tbl;
|
||||
SELECT * + * FROM VALUES('a UInt16', 1, 10);
|
||||
|
||||
SELECT '---';
|
||||
|
||||
SELECT * GROUP BY *;
|
||||
-- not ok as every component of ORDER BY may contain ASC/DESC and COLLATE; though can be supported in some sense
|
||||
-- but it works
|
||||
SELECT * ORDER BY *;
|
||||
SELECT * WHERE *; -- { serverError UNSUPPORTED_METHOD }
|
||||
|
||||
SELECT '---';
|
||||
|
||||
SELECT * FROM (SELECT 1 AS a) AS t, (SELECT 2 AS b) AS u;
|
||||
-- equivalent to:
|
||||
SELECT a, b FROM (SELECT 1 AS a) AS t, (SELECT 2 AS b) AS u;
|
||||
|
||||
SELECT '---';
|
||||
|
||||
SELECT * FROM (SELECT 1 AS a) AS t, (SELECT 1 AS a) AS u;
|
||||
-- equivalent to:
|
||||
SELECT t.a, u.a FROM (SELECT 1 AS a) AS t, (SELECT 1 AS a) AS u;
|
||||
|
||||
SELECT '---';
|
||||
|
||||
---- TODO: think about it
|
||||
--CREATE TABLE db1_03101.t
|
||||
--(
|
||||
-- a UInt16
|
||||
--)
|
||||
--ENGINE = Memory;
|
||||
--
|
||||
--CREATE TABLE db2_03101.t
|
||||
--(
|
||||
-- a UInt16
|
||||
--)
|
||||
--ENGINE = Memory;
|
||||
--
|
||||
--SELECT * FROM (SELECT 1 AS a) AS db2_03101.t, (SELECT 1 AS a) AS db1_03101.t;
|
||||
---- equivalent to:
|
||||
--SELECT db2_03101.t.a, db1_03101.t.a FROM (SELECT 1 AS a) AS db2_03101.t, (SELECT 1 AS a) AS db1_03101.t;
|
||||
|
||||
|
||||
CREATE TABLE t
|
||||
(
|
||||
x String,
|
||||
nest Nested
|
||||
(
|
||||
a String,
|
||||
b String
|
||||
)
|
||||
) ENGINE = Memory;
|
||||
|
||||
SELECT * FROM t;
|
||||
|
||||
-- equivalent to:
|
||||
SELECT x, nest.* FROM t;
|
||||
|
||||
-- equivalent to:
|
||||
SELECT x, nest.a, nest.b FROM t;
|
@ -0,0 +1,25 @@
|
||||
1
|
||||
('hello',1) hello
|
||||
0
|
||||
---
|
||||
0
|
||||
hello 1
|
||||
---
|
||||
0
|
||||
0
|
||||
---
|
||||
1 1
|
||||
1
|
||||
1
|
||||
---
|
||||
3
|
||||
---
|
||||
5
|
||||
5
|
||||
5
|
||||
5
|
||||
5
|
||||
---
|
||||
11
|
||||
---
|
||||
10 12 11
|
103
tests/queries/0_stateless/03101_analyzer_identifiers_4.sql
Normal file
103
tests/queries/0_stateless/03101_analyzer_identifiers_4.sql
Normal file
@ -0,0 +1,103 @@
|
||||
-- https://github.com/ClickHouse/ClickHouse/issues/23194
|
||||
SET allow_experimental_analyzer = 1;
|
||||
|
||||
DROP DATABASE IF EXISTS {CLICKHOUSE_DATABASE:Identifier};
|
||||
CREATE DATABASE {CLICKHOUSE_DATABASE:Identifier};
|
||||
USE {CLICKHOUSE_DATABASE:Identifier};
|
||||
|
||||
-- simple tuple access operator
|
||||
SELECT tuple(1, 'a').1;
|
||||
-- named tuple or complex column access operator - can be applied to Nested type as well as Array of named Tuple
|
||||
SELECT CAST(('hello', 1) AS Tuple(hello String, count UInt32)) AS t, t.hello;
|
||||
-- TODO: this doesn't work
|
||||
-- https://github.com/ClickHouse/ClickHouse/issues/57361
|
||||
-- SELECT CAST(('hello', 1) AS Tuple(hello String, count UInt32)).hello;
|
||||
|
||||
-- expansion of a tuple or complex column with asterisk
|
||||
SELECT tuple(1, 'a').*;
|
||||
|
||||
SELECT '---';
|
||||
|
||||
SELECT CAST(('hello', 1) AS Tuple(name String, count UInt32)).*;
|
||||
|
||||
SELECT untuple(CAST(('hello', 1) AS Tuple(name String, count UInt32))); -- will give two columns `name` and `count`.
|
||||
|
||||
SELECT '---';
|
||||
|
||||
CREATE TABLE {CLICKHOUSE_DATABASE:Identifier}.t
|
||||
(
|
||||
col String,
|
||||
hello String,
|
||||
world String
|
||||
)
|
||||
ENGINE = Memory;
|
||||
|
||||
CREATE TABLE {CLICKHOUSE_DATABASE:Identifier}.u
|
||||
(
|
||||
cc String
|
||||
)
|
||||
ENGINE = Memory;
|
||||
|
||||
SELECT * EXCEPT('hello|world');
|
||||
-- TODO: Qualified matcher t.* EXCEPT 'hello|world' does not find table.
|
||||
-- SELECT t.* EXCEPT(hello, world);
|
||||
-- SELECT {CLICKHOUSE_DATABASE:Identifier}.t.* REPLACE(x + 1 AS x);
|
||||
|
||||
|
||||
SELECT * EXCEPT(hello) REPLACE(x + 1 AS x);
|
||||
|
||||
SELECT COLUMNS('^c') FROM t;
|
||||
SELECT t.COLUMNS('^c') FROM t, u;
|
||||
SELECT t.COLUMNS('^c') EXCEPT (test_hello, test_world) FROM t, u;
|
||||
|
||||
SELECT '---';
|
||||
|
||||
SELECT * FROM (SELECT x, x FROM (SELECT 1 AS x));
|
||||
SELECT x FROM (SELECT x, x FROM (SELECT 1 AS x));
|
||||
SELECT 1 FROM (SELECT x, x FROM (SELECT 1 AS x));
|
||||
|
||||
SELECT '---';
|
||||
|
||||
SELECT `plus(1, 2)` FROM (SELECT 1 + 2);
|
||||
|
||||
-- Lambda expressions can be aliased. (proposal)
|
||||
--SELECT arrayMap(plus, [1, 2], [10, 20]);
|
||||
--SELECT x -> x + 1 AS fun;
|
||||
|
||||
SELECT '---';
|
||||
|
||||
SELECT x FROM numbers(5 AS x);
|
||||
|
||||
|
||||
SELECT '---';
|
||||
|
||||
CREATE TEMPORARY TABLE aliased
|
||||
(
|
||||
x UInt8 DEFAULT 0,
|
||||
y ALIAS x + 1
|
||||
);
|
||||
|
||||
INSERT INTO aliased VALUES (10);
|
||||
|
||||
SELECT y FROM aliased;
|
||||
|
||||
CREATE TEMPORARY TABLE aliased2
|
||||
(
|
||||
x UInt8,
|
||||
y ALIAS ((x + 1) AS z) + 1
|
||||
);
|
||||
|
||||
SELECT x, y, z FROM aliased2; -- { serverError UNKNOWN_IDENTIFIER }
|
||||
|
||||
|
||||
SELECT '---';
|
||||
|
||||
CREATE TEMPORARY TABLE aliased3
|
||||
(
|
||||
x UInt8,
|
||||
y ALIAS z + 1,
|
||||
z ALIAS x + 1
|
||||
);
|
||||
INSERT INTO aliased3 VALUES (10);
|
||||
|
||||
SELECT x, y, z FROM aliased3;
|
Loading…
Reference in New Issue
Block a user