1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
# name: test/sql/select/test_multi_column_reference.test
# description: Test multi column reference
# group: [select]
statement ok
PRAGMA enable_verification
# column names can have an arbitrary amount of dots
# here is how the resolution works:
# no dots (i.e. "part1")
# -> part1 refers to a column
# one dot (i.e. "part1.part2")
# EITHER:
# -> part1 is a table, part2 is a column
# -> part1 is a column, part2 is a property of that column (i.e. struct_extract)
# two or more dots (i.e. "part1.part2.part3.part4...")
# -> part1 is a schema, part2 is a table, part3 is a column name, part4 and beyond are struct fields
# -> part1 is a table, part2 is a column name, part3 and beyond are struct fields
# -> part1 is a column, part2 and beyond are struct fields
# we always prefer the most top-level view
# i.e. in case of multiple resolution options, we resolve in order:
# -> 1. resolve "part1" as a schema
# -> 2. resolve "part1" as a table
# -> 3. resolve "part1" as a column
# schema -> table -> column reference
statement ok
CREATE SCHEMA test
statement ok
CREATE TABLE test.tbl(col INTEGER);
statement ok
INSERT INTO test.tbl VALUES (1), (2), (3);
query I
SELECT test.tbl.col FROM test.tbl;
----
1
2
3
# schema name with alias does not work
statement error
SELECT test.t.col FROM test.tbl t;
----
<REGEX>:.*Binder Error.*table.*not found.*
statement error
SELECT test.tbl.col FROM test.tbl t;
----
<REGEX>:.*Binder Error.*table.*not found.*
# check how ties are resolved
# we create a table called "t" in a schema called "t" with a column called "t" that has a field called "t"
statement ok
CREATE SCHEMA t
statement ok
CREATE TABLE t.t(t ROW(t INTEGER));
statement ok
INSERT INTO t.t VALUES ({'t': 42});
# "t" selects the column
query I
SELECT t FROM t.t;
----
{'t': 42}
# "t.t" also selects the column
query I
SELECT t.t FROM t.t;
----
{'t': 42}
# t.t.t also selects the column
query I
SELECT t.t.t FROM t.t;
----
{'t': 42}
# t.t.t.t selects the field
query I
SELECT t.t.t.t FROM t.t;
----
42
statement ok
DROP SCHEMA t CASCADE;
# test long nested struct
statement ok
CREATE SCHEMA t
statement ok
CREATE TABLE t.t AS SELECT {'t': {'t': {'t': {'t': {'t': 42}}}}} t
query I
SELECT t.t.t.t.t.t.t.t FROM t.t;
----
42
query I
SELECT t.t.t.t.t.t.t FROM t.t;
----
{'t': 42}
query I
SELECT t.t.t.t.t.t FROM t.t;
----
{'t': {'t': 42}}
query I
SELECT t.t.t.t.t FROM t.t;
----
{'t': {'t': {'t': 42}}}
query I
SELECT t.t.t.t FROM t.t;
----
{'t': {'t': {'t': {'t': 42}}}}
query I
SELECT t.t.t FROM t.t;
----
{'t': {'t': {'t': {'t': {'t': 42}}}}}
query I
SELECT t.t FROM t.t;
----
{'t': {'t': {'t': {'t': {'t': 42}}}}}
query I
SELECT t FROM t.t;
----
{'t': {'t': {'t': {'t': {'t': 42}}}}}
statement ok
DROP SCHEMA t CASCADE
# test multiple tables with the same name but a different schema
statement ok
CREATE SCHEMA s1
statement ok
CREATE SCHEMA s2
statement ok
CREATE TABLE s1.t1 AS SELECT 42 t
statement ok
CREATE TABLE s2.t1 AS SELECT 84 t
query I
SELECT s1.t1.t FROM s1.t1, s2.t1
----
42
query I
SELECT * EXCLUDE (s1.t1.t) FROM s1.t1, s2.t1
----
84
query I
SELECT * EXCLUDE (S1.T1.T) FROM s1.t1, s2.t1
----
84
query I
SELECT s2.t1.t FROM s1.t1, s2.t1
----
84
# test various failures
statement error
SELECT testX.tbl.col FROM test.tbl;
----
<REGEX>:.*Binder Error.*table.*not found.*
statement error
SELECT test.tblX.col FROM test.tbl;
----
<REGEX>:.*Binder Error.*table.*not found.*
statement error
SELECT test.tbl.colX FROM test.tbl;
----
<REGEX>:.*Binder Error.*does not have a column.*
|