ClickHouse/tests/queries/0_stateless/02989_join_using_parent_scope.reference

137 lines
4.5 KiB
Plaintext

-- { echoOn }
SELECT 1 AS a FROM tb JOIN tabc USING (a) ORDER BY ALL;
1
1
1
1
SELECT a + 2 AS b FROM ta JOIN tabc USING (b) ORDER BY ALL;
2
3
4
SELECT b + 2 AS a FROM tb JOIN tabc USING (a) ORDER BY ALL;
2
3
SELECT a + 2 AS c FROM ta JOIN tabc USING (c) ORDER BY ALL;
2
3
4
5
SELECT b AS a, a FROM tb JOIN tabc USING (a) ORDER BY ALL;
0 0
1 1
2 2
3 3
SELECT 1 AS b FROM tb JOIN ta USING (b); -- { serverError UNKNOWN_IDENTIFIER }
-- SELECT * returns all columns from both tables in new analyzer
SELECT 3 AS a, a, * FROM tb FULL JOIN tabc USING (a) ORDER BY ALL SETTINGS allow_experimental_analyzer = 1;
3 3 0 3 abc3
3 3 1 3 abc3
3 3 2 3 abc3
3 3 3 3 abc3
3 3 \N 0 abc0
3 3 \N 1 abc1
3 3 \N 2 abc2
SELECT b + 1 AS a, * FROM tb JOIN tabc USING (a) ORDER BY ALL SETTINGS allow_experimental_analyzer = 1;
1 0 1 abc1
2 1 2 abc2
3 2 3 abc3
SELECT b + 1 AS a, * FROM tb JOIN tabc USING (a) ORDER BY ALL SETTINGS allow_experimental_analyzer = 1;
1 0 1 abc1
2 1 2 abc2
3 2 3 abc3
SELECT b + 1 AS a, * FROM tb LEFT JOIN tabc USING (a) ORDER BY ALL SETTINGS allow_experimental_analyzer = 1;
1 0 1 abc1
2 1 2 abc2
3 2 3 abc3
4 3 \N \N
SELECT b + 1 AS a, * FROM tb RIGHT JOIN tabc USING (a) ORDER BY ALL SETTINGS allow_experimental_analyzer = 1;
1 0 1 abc1
2 1 2 abc2
3 2 3 abc3
\N \N 0 abc0
SELECT b + 1 AS a, * FROM tb FULL JOIN tabc USING (a) ORDER BY ALL SETTINGS allow_experimental_analyzer = 1;
1 0 1 abc1
2 1 2 abc2
3 2 3 abc3
4 3 \N \N
\N \N 0 abc0
SELECT b + 1 AS a, * FROM tb FULL JOIN tabc USING (a) ORDER BY ALL SETTINGS asterisk_include_alias_columns = 1, allow_experimental_analyzer = 1;
1 0 1 abc1 2 3
2 1 2 abc2 3 4
3 2 3 abc3 4 5
4 3 \N \N \N \N
\N \N 0 abc0 1 2
SELECT b + 1 AS a, * FROM (SELECT b FROM tb) t1 JOIN (SELECT a, b FROM tabc) t2 USING (a) ORDER BY ALL SETTINGS allow_experimental_analyzer = 1;
1 0 1 2
2 1 2 3
3 2 3 4
SELECT b + 1 AS a, * FROM (SELECT b FROM tb) t1 LEFT JOIN (SELECT a, b FROM tabc) t2 USING (a) ORDER BY ALL SETTINGS allow_experimental_analyzer = 1;
1 0 1 2
2 1 2 3
3 2 3 4
4 3 \N \N
SELECT b + 1 AS a, * FROM (SELECT b FROM tb) t1 RIGHT JOIN (SELECT a, b FROM tabc) t2 USING (a) ORDER BY ALL SETTINGS allow_experimental_analyzer = 1;
1 0 1 2
2 1 2 3
3 2 3 4
\N \N 0 1
SELECT b + 1 AS a, * FROM (SELECT b FROM tb) t1 FULL JOIN (SELECT a, b FROM tabc) t2 USING (a) ORDER BY ALL SETTINGS allow_experimental_analyzer = 1;
1 0 1 2
2 1 2 3
3 2 3 4
4 3 \N \N
\N \N 0 1
SELECT b + 1 AS a, s FROM tb FULL OUTER JOIN tabc USING (a) PREWHERE a > 2 ORDER BY ALL SETTINGS allow_experimental_analyzer = 1;
3 abc3
4 \N
\N abc0
\N abc1
\N abc2
-- It's a default behavior for old analyzer and new with analyzer_compatibility_join_using_top_level_identifier
-- Column `b` actually exists in left table, but `b` from USING is resoled to `a + 2` and `a` is not in left table
-- so we get UNKNOWN_IDENTIFIER error.
SELECT a + 2 AS b FROM tb JOIN tabc USING (b) ORDER BY ALL
SETTINGS analyzer_compatibility_join_using_top_level_identifier = 1; -- { serverError UNKNOWN_IDENTIFIER }
-- In new analyzer with `analyzer_compatibility_join_using_top_level_identifier = 0` we get `b` from left table
SELECT a + 2 AS b FROM tb JOIN tabc USING (b) ORDER BY ALL
SETTINGS analyzer_compatibility_join_using_top_level_identifier = 0, allow_experimental_analyzer = 1;
2
3
4
-- This is example where query may return different results with different `analyzer_compatibility_join_using_top_level_identifier`
DROP TABLE IF EXISTS users;
CREATE TABLE users (uid Int16, name String, spouse_name String) ENGINE=Memory;
INSERT INTO users VALUES (1231, 'John', 'Ksenia');
INSERT INTO users VALUES (6666, 'Ksenia', '');
SELECT u1.uid, u1.spouse_name as name, u2.uid, u2.name
FROM users u1 JOIN users u2 USING (name)
ORDER BY u1.uid
FORMAT TSVWithNamesAndTypes
SETTINGS allow_experimental_analyzer = 1, analyzer_compatibility_join_using_top_level_identifier = 1;
uid name u2.uid u2.name
Int16 String Int16 String
1231 Ksenia 6666 Ksenia
SELECT u1.uid, u1.spouse_name as name, u2.uid, u2.name
FROM users u1 JOIN users u2 USING (name)
ORDER BY u1.uid
FORMAT TSVWithNamesAndTypes
SETTINGS allow_experimental_analyzer = 1, analyzer_compatibility_join_using_top_level_identifier = 0;
uid name u2.uid u2.name
Int16 String Int16 String
1231 Ksenia 1231 John
6666 6666 Ksenia
SELECT u1.uid, u1.spouse_name as name, u2.uid, u2.name
FROM users u1 JOIN users u2 USING (name)
ORDER BY u1.uid
FORMAT TSVWithNamesAndTypes
SETTINGS allow_experimental_analyzer = 0;
uid name u2.uid u2.name
Int16 String Int16 String
1231 Ksenia 6666 Ksenia
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS tabc;
DROP TABLE IF EXISTS ta;
DROP TABLE IF EXISTS tb;
DROP TABLE IF EXISTS tc;