summaryrefslogtreecommitdiffstats
path: root/test/join7.test
blob: 42cc357418a8098de6fcd2e0792d80a02f101b82 (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# 2022-04-09
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.
#
# This file implements tests for RIGHT and FULL OUTER JOINs.

set testdir [file dirname $argv0]
source $testdir/tester.tcl

foreach {id schema} {
  1 {
    CREATE TABLE t1(a INT, b INT);
    INSERT INTO t1 VALUES(1,2),(1,3),(1,4);
    CREATE INDEX t1a ON t1(a);
    CREATE TABLE t2(c INT, d INT);
    INSERT INTO t2 VALUES(3,33),(4,44),(5,55);
    CREATE INDEX t2c ON t2(c);
    CREATE VIEW dual(dummy) AS VALUES('x');
  }
  2 {
    CREATE TABLE t1(a INT, b INT);
    INSERT INTO t1 VALUES(1,2),(1,3),(1,4);
    CREATE INDEX t1ab ON t1(a,b);
    CREATE TABLE t2(c INT, d INT);
    INSERT INTO t2 VALUES(3,33),(4,44),(5,55);
    CREATE INDEX t2cd ON t2(c,d);
    CREATE VIEW dual(dummy) AS VALUES('x');
  }
  3 {
    CREATE TABLE t1(a INT, b INT);
    INSERT INTO t1 VALUES(1,2),(1,3),(1,4);
    CREATE INDEX t1a ON t1(a);
    CREATE TABLE t2(c INT, d INT PRIMARY KEY) WITHOUT ROWID;
    INSERT INTO t2 VALUES(3,33),(4,44),(5,55);
    CREATE INDEX t2c ON t2(c);
    CREATE VIEW dual(dummy) AS VALUES('x');
  }
  4 {
    CREATE TABLE t1(a INT, b INT);
    INSERT INTO t1 VALUES(1,2),(1,3),(1,4);
    CREATE TABLE t2(c INTEGER PRIMARY KEY, d INT);
    INSERT INTO t2 VALUES(3,33),(4,44),(5,55);
    CREATE VIEW dual(dummy) AS VALUES('x');
  }
  5 {
    CREATE TABLE t1(a INT, b INT);
    INSERT INTO t1 VALUES(1,2),(1,3),(1,4);
    CREATE TABLE t2(c INT PRIMARY KEY, d INT) WITHOUT ROWID;
    INSERT INTO t2 VALUES(3,33),(4,44),(5,55);
    CREATE VIEW dual(dummy) AS VALUES('x');
  }
  6 {
    CREATE TABLE t1(a INT, b INT);
    INSERT INTO t1 VALUES(1,2),(1,3),(1,4);
    CREATE VIEW t2(c,d) AS VALUES(3,33),(4,44),(5,55);
    CREATE VIEW dual(dummy) AS VALUES('x');
  }
  7 {
    CREATE VIEW t1(a,b) AS VALUES(1,2),(1,3),(1,4);
    CREATE TABLE t2(c INTEGER PRIMARY KEY, d INT);
    INSERT INTO t2 VALUES(3,33),(4,44),(5,55);
    CREATE VIEW dual(dummy) AS VALUES('x');
  }
  8 {
    CREATE TABLE t1(a INT, b INT);
    INSERT INTO t1 VALUES(1,2),(1,3),(1,4);
    CREATE TABLE t2(c INT, d INT);
    INSERT INTO t2 VALUES(3,33),(4,44),(5,55);
    CREATE VIEW dual(dummy) AS VALUES('x');
  }
  9 {
    CREATE TABLE t1(a INT, b INT);
    INSERT INTO t1 VALUES(1,2),(1,3),(1,4);
    CREATE TABLE t2a(c INTEGER PRIMARY KEY, i1 INT);
    CREATE TABLE t2b(i1 INTEGER PRIMARY KEY, d INT);
    CREATE VIEW t2(c,d) AS SELECT c, d FROM t2a NATURAL JOIN t2b;
    INSERT INTO t2a VALUES(3,93),(4,94),(5,95),(6,96),(7,97);
    INSERT INTO t2b VALUES(91,11),(92,22),(93,33),(94,44),(95,55);
    CREATE TABLE dual(dummy TEXT);
    INSERT INTO dual(dummy) VALUES('x');
  }
  10 {
    CREATE TABLE t1(a INT, b INT, PRIMARY KEY(a,b)) WITHOUT ROWID;
    INSERT INTO t1 VALUES(1,2),(1,3),(1,4);
    CREATE TABLE t2a(c INTEGER PRIMARY KEY, i1 INT);
    CREATE TABLE t2b(i1 INTEGER PRIMARY KEY, d INT);
    CREATE VIEW t2(c,d) AS SELECT c, d FROM t2a NATURAL JOIN t2b;
    INSERT INTO t2a VALUES(3,93),(4,94),(5,95),(6,96),(7,97);
    INSERT INTO t2b VALUES(91,11),(92,22),(93,33),(94,44),(95,55);
    CREATE TABLE dual(dummy TEXT);
    INSERT INTO dual(dummy) VALUES('x');
  }
} {
  reset_db
  db nullvalue NULL
  do_execsql_test join7-$id.setup $schema {}

  # Verified against PG-14 for case 1
  do_execsql_test join7-$id.10 {
    SELECT b, d FROM t1 FULL OUTER JOIN t2 ON b=c ORDER BY +b;
  } {
    NULL 55
    2    NULL
    3    33
    4    44
  }

  # Verified against PG-14 for case 1
  do_execsql_test join7-$id.20 {
    SELECT a, c FROM t1 FULL OUTER JOIN t2 ON b=c ORDER BY +b;
  } {
    NULL  5
    1     NULL
    1     3
    1     4
  }

  do_execsql_test join7-$id.30 {
    SELECT * FROM t1 FULL OUTER JOIN t2 ON b=c ORDER BY +b;
  } {
    NULL NULL 5    55
    1    2    NULL NULL
    1    3    3    33
    1    4    4    44
  }
  do_execsql_test join7-$id.31 {
    SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c ORDER BY +b;
  } {
    NULL NULL 5    55
    1    2    NULL NULL
    1    3    3    33
    1    4    4    44
  }
  do_execsql_test join7-$id.32 {
    SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c
     WHERE b=c
     ORDER BY +b;
  } {
    1    3    3    33
    1    4    4    44
  }
  do_execsql_test join7-$id.33 {
    SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c
     WHERE b>0
     ORDER BY +b;
  } {
    1    2    NULL NULL
    1    3    3    33
    1    4    4    44
  }
  do_execsql_test join7-$id.34 {
    SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c
     WHERE b>0 OR b IS NULL
     ORDER BY +b;
  } {
    NULL NULL 5    55
    1    2    NULL NULL
    1    3    3    33
    1    4    4    44
  }
  do_execsql_test join7-$id.35 {
    SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c AND b>3 AND c>4
     ORDER BY coalesce(b,c,0);
  } {
    1    2    NULL NULL
    NULL NULL 3    33
    1    3    NULL NULL
    NULL NULL 4    44
    1    4    NULL NULL
    NULL NULL 5    55
  }
  do_execsql_test join7-$id.36 {
    SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c AND b>3 WHERE c>4
     ORDER BY coalesce(b,c,0);
  } {
    NULL NULL 5    55
  }
  do_execsql_test join7-$id.37 {
    SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c WHERE b>3 AND c>4
     ORDER BY coalesce(b,c,0);
  } {
  }
  do_execsql_test join7-$id.38 {
    SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c WHERE b>3 OR c>4
     ORDER BY coalesce(b,c,0);
  } {
    1    4    4    44
    NULL NULL 5    55
  }
  do_execsql_test join7-$id.39 {
    SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c AND (b>3 OR c>4)
     ORDER BY coalesce(b,c,0);
  } {
    1    2    NULL NULL
    NULL NULL 3    33
    1    3    NULL NULL
    1    4    4    44
    NULL NULL 5    55
  }
  do_execsql_test join7-$id.40 {
    SELECT * FROM t1 RIGHT OUTER JOIN t2 ON b=c ORDER BY +b;
  } {
    NULL NULL 5    55
    1    3    3    33
    1    4    4    44
  }
  do_execsql_test join7-$id.50 {
    SELECT t1.*, t2.* FROM t2 LEFT OUTER JOIN t1 ON b=c ORDER BY +b;
  } {
    NULL NULL 5    55
    1    3    3    33
    1    4    4    44
  }
  do_execsql_test join7-$id.60 {
    SELECT * FROM dual JOIN t1 ON true RIGHT OUTER JOIN t2 ON b=c ORDER BY +b;
  } {
    NULL NULL NULL 5    55
    x    1    3    3    33
    x    1    4    4    44
  }
  do_execsql_test join7-$id.70 {
    SELECT t1.*, t2.* 
      FROM t2 LEFT JOIN (dual JOIN t1 ON true) ON b=c ORDER BY +b;
  } {
    NULL NULL 5    55
    1    3    3    33
    1    4    4    44
  }
  do_execsql_test join7-$id.80 {
    SELECT * FROM dual CROSS JOIN t1 RIGHT OUTER JOIN t2 ON b=c ORDER BY +b;
  } {
    NULL NULL NULL 5    55
    x    1    3    3    33
    x    1    4    4    44
  }
  do_execsql_test join7-$id.81 {
    SELECT dual.*, t1.*, t2.*
      FROM t1 CROSS JOIN dual RIGHT OUTER JOIN t2 ON b=c ORDER BY +b;
  } {
    NULL NULL NULL 5    55
    x    1    3    3    33
    x    1    4    4    44
  }
  do_execsql_test join7-$id.90 {
    SELECT * FROM t1 LEFT OUTER JOIN t2 ON b=c ORDER BY +b;
  } {
    1    2    NULL NULL
    1    3    3    33
    1    4    4    44
  }
  do_execsql_test join7-$id.100 {
    SELECT * FROM t1 FULL OUTER JOIN t2 ON b=c AND a=1 ORDER BY +b;
  } {
    NULL NULL 5    55
    1    2    NULL NULL
    1    3    3    33
    1    4    4    44
  }
  do_execsql_test join7-$id.101 {
    SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c AND a=1 ORDER BY +b;
  } {
    NULL NULL 5    55
    1    2    NULL NULL
    1    3    3    33
    1    4    4    44
  }

  # Verified against PG-14 for case 1
  do_execsql_test join7-$id.110 {
    SELECT * FROM t1 FULL OUTER JOIN t2 ON b=c WHERE a=1 ORDER BY +b;
  } {
    1    2    NULL NULL
    1    3    3    33
    1    4    4    44
  }

  do_execsql_test join7-$id.111 {
    SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c WHERE a=1 ORDER BY +b;
  } {
    1    2    NULL NULL
    1    3    3    33
    1    4    4    44
  }

  # Verified against PG-14 for case 1
  do_execsql_test join7-$id.115 {
    SELECT * FROM t1 FULL OUTER JOIN t2 ON b=c
     WHERE a=1 OR a IS NULL ORDER BY +b;
  } {
    NULL NULL 5    55
    1    2    NULL NULL
    1    3    3    33
    1    4    4    44
  }

  do_execsql_test join7-$id.116 {
    SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c
     WHERE a=1 OR a IS NULL ORDER BY +b;
  } {
    NULL NULL 5    55
    1    2    NULL NULL
    1    3    3    33
    1    4    4    44
  }

  # Verified against PG-14 for case 1:
  do_execsql_test join7-$id.120 {
    SELECT * FROM t1 FULL OUTER JOIN t2 ON b=c WHERE a IS NULL ORDER BY +d;
  } {
    NULL NULL 5    55
  }

  # Verified against PG-14 for case 1:
  do_execsql_test join7-$id.130 {
    SELECT * FROM t1 FULL OUTER JOIN t2 ON b=c AND d<=0 ORDER BY +b, +d;
  } {
    NULL NULL 3    33
    NULL NULL 4    44
    NULL NULL 5    55
    1    2    NULL NULL
    1    3    NULL NULL
    1    4    NULL NULL
  }

  # Verified against PG-14 for case 1:
  do_execsql_test join7-$id.140 {
    SELECT a, b, c, d
      FROM t2 FULL OUTER JOIN t1 ON b=c AND d<=0 ORDER BY +b, +d;
  } {
    NULL NULL 3    33
    NULL NULL 4    44
    NULL NULL 5    55
    1    2    NULL NULL
    1    3    NULL NULL
    1    4    NULL NULL
  }

  do_execsql_test join7-$id.141 {
    SELECT a, b, c, d
      FROM t2 FULL OUTER JOIN t1 ON b=c AND d<=0
     ORDER BY +b, +d LIMIT 2 OFFSET 2
  } {
    NULL NULL 5    55
    1    2    NULL NULL
  }
}  
finish_test