-
Notifications
You must be signed in to change notification settings - Fork 52
/
unique_keys.test
178 lines (153 loc) · 6.09 KB
/
unique_keys.test
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
statement ok
create table testkeys (a int primary key, b int unique, c int, d int, unique(c, d))
statement ok rowcount 3
insert into testkeys values (1,1,1,1),(2,2,2,2),(3,3,3,3)
# The following joins can be converted into semijoins
query T nosort
plan select y from (values (1),(2),(cast(3 as int))) y(y) inner join testkeys on y.y = testkeys.a
----
project (
| semijoin (
| | [ [ int(32) "1", int(32) "2", int(32) "3" ] as "y"."y" ],
| | table("sys"."testkeys") [ "testkeys"."a" NOT NULL UNIQUE HASHCOL ]
| ) [ ("y"."y" NOT NULL) = ("testkeys"."a" NOT NULL UNIQUE HASHCOL ) ]
) [ "y"."y" NOT NULL ]
query T nosort
plan select y from (values (1),(2),(cast(3 as int))) y(y) inner join testkeys on y.y = testkeys.b
----
project (
| semijoin (
| | [ [ int(32) "1", int(32) "2", int(32) "3" ] as "y"."y" ],
| | table("sys"."testkeys") [ "testkeys"."b" UNIQUE HASHCOL ]
| ) [ ("y"."y" NOT NULL) = ("testkeys"."b" UNIQUE HASHCOL ) ]
) [ "y"."y" NOT NULL ]
query T nosort
plan select y from (values (1),(2),(cast(3 as int))) y(y) inner join testkeys on y.y = testkeys.a or (y.y is null and testkeys.a is null)
----
project (
| semijoin (
| | [ [ int(32) "1", int(32) "2", int(32) "3" ] as "y"."y" ],
| | table("sys"."testkeys") [ "testkeys"."a" NOT NULL UNIQUE HASHCOL ]
| ) [ ("y"."y" NOT NULL) = ("testkeys"."a" NOT NULL UNIQUE HASHCOL ) ]
) [ "y"."y" NOT NULL ]
# The following joins cannot be converted into semijoins
query T nosort
plan select y from (values (NULL),(1),(2),(cast(3 as int))) y(y) inner join testkeys on y.y = testkeys.b or (y.y is null and testkeys.b is null)
----
project (
| join (
| | [ [ int(32) NULL, int(32) "1", int(32) "2", int(32) "3" ] as "y"."y" ],
| | table("sys"."testkeys") [ "testkeys"."b" UNIQUE HASHCOL ]
| ) [ ("y"."y") * = ("testkeys"."b" UNIQUE HASHCOL ) ]
) [ "y"."y" ]
query T nosort
plan select y from (values (1),(2),(cast(3 as int))) y(y) inner join testkeys on y.y = testkeys.c or (y.y is null and testkeys.c is null)
----
project (
| join (
| | [ [ int(32) "1", int(32) "2", int(32) "3" ] as "y"."y" ],
| | table("sys"."testkeys") [ "testkeys"."c" UNIQUE ]
| ) [ ("y"."y" NOT NULL) * = ("testkeys"."c" UNIQUE) ]
) [ "y"."y" NOT NULL ]
statement ok
start transaction
statement ok
create table othertable (a int, b int, c int, d int)
statement ok rowcount 3
insert into othertable values (1,1,1,1),(2,2,2,2),(3,3,3,3)
# here 'othertable.a = 2' can be pushed up
query T nosort
plan select othertable.a from testkeys inner join othertable on testkeys.a = othertable.a where testkeys.a = 1 and othertable.a = 1
----
project (
| select (
| | join (
| | | select (
| | | | table("sys"."testkeys") [ "testkeys"."a" NOT NULL UNIQUE HASHCOL ]
| | | ) [ ("testkeys"."a" NOT NULL UNIQUE HASHCOL ) = (int(32) "1") ],
| | | table("sys"."othertable") [ "othertable"."a" ]
| | ) [ ("testkeys"."a" NOT NULL UNIQUE HASHCOL ) = ("othertable"."a" NOT NULL) ]
| ) [ ("othertable"."a") = (int(32) "1") ]
) [ "othertable"."a" NOT NULL ]
# here 'othertable.a between 2 and 5' can be pushed up too
query T nosort
plan select testkeys.a from testkeys inner join othertable on testkeys.a = othertable.a where testkeys.b = 1 and othertable.a between 2 and 5
----
project (
| select (
| | join (
| | | select (
| | | | table("sys"."testkeys") [ "testkeys"."a" NOT NULL UNIQUE HASHCOL , "testkeys"."b" UNIQUE HASHCOL ]
| | | ) [ ("testkeys"."b" UNIQUE HASHCOL ) = (int(32) "1") ],
| | | table("sys"."othertable") [ "othertable"."a" UNIQUE ]
| | ) [ ("testkeys"."a" NOT NULL UNIQUE HASHCOL ) = ("othertable"."a" UNIQUE) ]
| ) [ (int(32) "2") <= ("othertable"."a" UNIQUE) <= (int(32) "5") ]
) [ "testkeys"."a" NOT NULL UNIQUE HASHCOL ]
# here 'othertable.a > 1' cannot be pushed up because 'testkeys.b is null' may pass multiple values
query T nosort
plan select testkeys.a from testkeys inner join othertable on testkeys.a = othertable.a where testkeys.b is null and othertable.a > 1
----
project (
| join (
| | select (
| | | table("sys"."testkeys") [ "testkeys"."a" NOT NULL UNIQUE HASHCOL , "testkeys"."b" UNIQUE HASHCOL ]
| | ) [ ("testkeys"."b" UNIQUE HASHCOL ) * = (int(32) NULL) ],
| | select (
| | | table("sys"."othertable") [ "othertable"."a" UNIQUE ]
| | ) [ ("othertable"."a" UNIQUE) > (int(32) "1") ]
| ) [ ("testkeys"."a" NOT NULL UNIQUE HASHCOL ) = ("othertable"."a" NOT NULL UNIQUE) ]
) [ "testkeys"."a" NOT NULL UNIQUE HASHCOL ]
statement ok
rollback
statement ok
alter table testkeys drop constraint testkeys_a_pkey
statement ok
alter table testkeys drop constraint testkeys_b_unique
# No more unique properties
query T nosort
plan select y from (values (1),(2),(cast(3 as int))) y(y) inner join testkeys on y.y = testkeys.a
----
project (
| join (
| | [ [ int(32) "1", int(32) "2", int(32) "3" ] as "y"."y" ],
| | table("sys"."testkeys") [ "testkeys"."a" NOT NULL UNIQUE ]
| ) [ ("y"."y" NOT NULL) = ("testkeys"."a" NOT NULL UNIQUE) ]
) [ "y"."y" NOT NULL ]
query T nosort
plan select y from (values (1),(2),(cast(3 as int))) y(y) inner join testkeys on y.y = testkeys.b
----
project (
| join (
| | [ [ int(32) "1", int(32) "2", int(32) "3" ] as "y"."y" ],
| | table("sys"."testkeys") [ "testkeys"."b" UNIQUE ]
| ) [ ("y"."y" NOT NULL) = ("testkeys"."b" UNIQUE) ]
) [ "y"."y" NOT NULL ]
statement ok
start transaction
statement ok
alter table testkeys add constraint testkeys_a_pkey primary key (a)
statement ok
alter table testkeys add constraint testkeys_b_unique unique (b)
statement ok
rollback
# The unique properties should not return
query T nosort
plan select y from (values (1),(2),(cast(3 as int))) y(y) inner join testkeys on y.y = testkeys.a
----
project (
| join (
| | [ [ int(32) "1", int(32) "2", int(32) "3" ] as "y"."y" ],
| | table("sys"."testkeys") [ "testkeys"."a" NOT NULL UNIQUE ]
| ) [ ("y"."y" NOT NULL) = ("testkeys"."a" NOT NULL UNIQUE) ]
) [ "y"."y" NOT NULL ]
query T nosort
plan select y from (values (1),(2),(cast(3 as int))) y(y) inner join testkeys on y.y = testkeys.b
----
project (
| join (
| | [ [ int(32) "1", int(32) "2", int(32) "3" ] as "y"."y" ],
| | table("sys"."testkeys") [ "testkeys"."b" UNIQUE ]
| ) [ ("y"."y" NOT NULL) = ("testkeys"."b" UNIQUE) ]
) [ "y"."y" NOT NULL ]
statement ok
DROP TABLE testkeys