1# 2022-05-31 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 6# May you do good and not evil. 7# May you find forgiveness for yourself and forgive others. 8# May you share freely, never taking more than you give. 9# 10#*********************************************************************** 11# 12# This file implements tests for JOINs 13# 14# The test case output is (mostly) all generated by PostgreSQL 14. This 15# test module was created as follows: 16# 17# 1. Run a TCL script (included at the bottom of this file) that 18# generates an input script for "psql" that will run man 19# diverse tests on joins. 20# 21# 2. Run the script from step (1) through psql and collect the 22# output. 23# 24# 3. Make a few minor global search-and-replace operations to convert 25# the psql output into a form suitable for this test module. 26# 27# 4. Add this header, and the script content at the footer. 28# 29# A few extra tests that were not generated from postgresql output are 30# added at the end. 31# 32set testdir [file dirname $argv0] 33source $testdir/tester.tcl 34db nullvalue - 35db eval { 36 CREATE TABLE t1(x INT); 37 CREATE TABLE t2(y INT); 38 CREATE TABLE t3(z INT); 39 CREATE TABLE t4(w INT); 40 INSERT INTO t1 VALUES(10); 41 INSERT INTO t3 VALUES(20),(30); 42 INSERT INTO t4 VALUES(50); 43} 44do_execsql_test joinF-1 { 45 SELECT * 46 FROM t1 INNER JOIN t2 ON true 47 INNER JOIN t3 ON t2.y IS NOT NULL 48 INNER JOIN t4 ON true 49 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 50} { 51} 52do_execsql_test joinF-2 { 53 SELECT * 54 FROM t1 INNER JOIN t2 ON true 55 INNER JOIN t3 ON t2.y IS NOT NULL 56 INNER JOIN t4 ON true 57 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 58 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 59} { 60} 61do_execsql_test joinF-3 { 62 SELECT * 63 FROM t1 INNER JOIN t2 ON true 64 INNER JOIN t3 ON t2.y IS NOT NULL 65 LEFT JOIN t4 ON true 66 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 67} { 68} 69do_execsql_test joinF-4 { 70 SELECT * 71 FROM t1 INNER JOIN t2 ON true 72 INNER JOIN t3 ON t2.y IS NOT NULL 73 LEFT JOIN t4 ON true 74 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 75 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 76} { 77} 78do_execsql_test joinF-5 { 79 SELECT * 80 FROM t1 INNER JOIN t2 ON true 81 INNER JOIN t3 ON t2.y IS NOT NULL 82 RIGHT JOIN t4 ON true 83 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 84} { 85 - - - 50 86} 87do_execsql_test joinF-6 { 88 SELECT * 89 FROM t1 INNER JOIN t2 ON true 90 INNER JOIN t3 ON t2.y IS NOT NULL 91 RIGHT JOIN t4 ON true 92 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 93 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 94} { 95} 96do_execsql_test joinF-7 { 97 SELECT * 98 FROM t1 INNER JOIN t2 ON true 99 LEFT JOIN t3 ON t2.y IS NOT NULL 100 INNER JOIN t4 ON true 101 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 102} { 103} 104do_execsql_test joinF-8 { 105 SELECT * 106 FROM t1 INNER JOIN t2 ON true 107 LEFT JOIN t3 ON t2.y IS NOT NULL 108 INNER JOIN t4 ON true 109 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 110 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 111} { 112} 113do_execsql_test joinF-9 { 114 SELECT * 115 FROM t1 INNER JOIN t2 ON true 116 LEFT JOIN t3 ON t2.y IS NOT NULL 117 LEFT JOIN t4 ON true 118 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 119} { 120} 121do_execsql_test joinF-10 { 122 SELECT * 123 FROM t1 INNER JOIN t2 ON true 124 LEFT JOIN t3 ON t2.y IS NOT NULL 125 LEFT JOIN t4 ON true 126 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 127 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 128} { 129} 130do_execsql_test joinF-11 { 131 SELECT * 132 FROM t1 INNER JOIN t2 ON true 133 LEFT JOIN t3 ON t2.y IS NOT NULL 134 RIGHT JOIN t4 ON true 135 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 136} { 137 - - - 50 138} 139do_execsql_test joinF-12 { 140 SELECT * 141 FROM t1 INNER JOIN t2 ON true 142 LEFT JOIN t3 ON t2.y IS NOT NULL 143 RIGHT JOIN t4 ON true 144 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 145 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 146} { 147} 148do_execsql_test joinF-13 { 149 SELECT * 150 FROM t1 INNER JOIN t2 ON true 151 RIGHT JOIN t3 ON t2.y IS NOT NULL 152 INNER JOIN t4 ON true 153 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 154} { 155 - - 20 50 156 - - 30 50 157} 158do_execsql_test joinF-14 { 159 SELECT * 160 FROM t1 INNER JOIN t2 ON true 161 RIGHT JOIN t3 ON t2.y IS NOT NULL 162 INNER JOIN t4 ON true 163 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 164 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 165} { 166 - - 20 50 167 - - 30 50 168} 169do_execsql_test joinF-15 { 170 SELECT * 171 FROM t1 INNER JOIN t2 ON true 172 RIGHT JOIN t3 ON t2.y IS NOT NULL 173 LEFT JOIN t4 ON true 174 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 175} { 176 - - 20 50 177 - - 30 50 178} 179do_execsql_test joinF-16 { 180 SELECT * 181 FROM t1 INNER JOIN t2 ON true 182 RIGHT JOIN t3 ON t2.y IS NOT NULL 183 LEFT JOIN t4 ON true 184 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 185 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 186} { 187 - - 20 50 188 - - 30 50 189} 190do_execsql_test joinF-17 { 191 SELECT * 192 FROM t1 INNER JOIN t2 ON true 193 RIGHT JOIN t3 ON t2.y IS NOT NULL 194 RIGHT JOIN t4 ON true 195 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 196} { 197 - - 20 50 198 - - 30 50 199} 200do_execsql_test joinF-18 { 201 SELECT * 202 FROM t1 INNER JOIN t2 ON true 203 RIGHT JOIN t3 ON t2.y IS NOT NULL 204 RIGHT JOIN t4 ON true 205 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 206 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 207} { 208 - - 20 50 209 - - 30 50 210} 211do_execsql_test joinF-19 { 212 SELECT * 213 FROM t1 LEFT JOIN t2 ON true 214 INNER JOIN t3 ON t2.y IS NOT NULL 215 INNER JOIN t4 ON true 216 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 217} { 218} 219do_execsql_test joinF-20 { 220 SELECT * 221 FROM t1 LEFT JOIN t2 ON true 222 INNER JOIN t3 ON t2.y IS NOT NULL 223 INNER JOIN t4 ON true 224 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 225 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 226} { 227} 228do_execsql_test joinF-21 { 229 SELECT * 230 FROM t1 LEFT JOIN t2 ON true 231 INNER JOIN t3 ON t2.y IS NOT NULL 232 LEFT JOIN t4 ON true 233 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 234} { 235} 236do_execsql_test joinF-22 { 237 SELECT * 238 FROM t1 LEFT JOIN t2 ON true 239 INNER JOIN t3 ON t2.y IS NOT NULL 240 LEFT JOIN t4 ON true 241 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 242 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 243} { 244} 245do_execsql_test joinF-23 { 246 SELECT * 247 FROM t1 LEFT JOIN t2 ON true 248 INNER JOIN t3 ON t2.y IS NOT NULL 249 RIGHT JOIN t4 ON true 250 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 251} { 252 - - - 50 253} 254do_execsql_test joinF-24 { 255 SELECT * 256 FROM t1 LEFT JOIN t2 ON true 257 INNER JOIN t3 ON t2.y IS NOT NULL 258 RIGHT JOIN t4 ON true 259 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 260 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 261} { 262} 263do_execsql_test joinF-25 { 264 SELECT * 265 FROM t1 LEFT JOIN t2 ON true 266 LEFT JOIN t3 ON t2.y IS NOT NULL 267 INNER JOIN t4 ON true 268 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 269} { 270 10 - - 50 271} 272do_execsql_test joinF-26 { 273 SELECT * 274 FROM t1 LEFT JOIN t2 ON true 275 LEFT JOIN t3 ON t2.y IS NOT NULL 276 INNER JOIN t4 ON true 277 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 278 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 279} { 280} 281do_execsql_test joinF-27 { 282 SELECT * 283 FROM t1 LEFT JOIN t2 ON true 284 LEFT JOIN t3 ON t2.y IS NOT NULL 285 LEFT JOIN t4 ON true 286 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 287} { 288 10 - - 50 289} 290do_execsql_test joinF-28 { 291 SELECT * 292 FROM t1 LEFT JOIN t2 ON true 293 LEFT JOIN t3 ON t2.y IS NOT NULL 294 LEFT JOIN t4 ON true 295 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 296 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 297} { 298} 299do_execsql_test joinF-29 { 300 SELECT * 301 FROM t1 LEFT JOIN t2 ON true 302 LEFT JOIN t3 ON t2.y IS NOT NULL 303 RIGHT JOIN t4 ON true 304 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 305} { 306 10 - - 50 307} 308do_execsql_test joinF-30 { 309 SELECT * 310 FROM t1 LEFT JOIN t2 ON true 311 LEFT JOIN t3 ON t2.y IS NOT NULL 312 RIGHT JOIN t4 ON true 313 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 314 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 315} { 316} 317do_execsql_test joinF-31 { 318 SELECT * 319 FROM t1 LEFT JOIN t2 ON true 320 RIGHT JOIN t3 ON t2.y IS NOT NULL 321 INNER JOIN t4 ON true 322 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 323} { 324 - - 20 50 325 - - 30 50 326} 327do_execsql_test joinF-32 { 328 SELECT * 329 FROM t1 LEFT JOIN t2 ON true 330 RIGHT JOIN t3 ON t2.y IS NOT NULL 331 INNER JOIN t4 ON true 332 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 333 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 334} { 335 - - 20 50 336 - - 30 50 337} 338do_execsql_test joinF-33 { 339 SELECT * 340 FROM t1 LEFT JOIN t2 ON true 341 RIGHT JOIN t3 ON t2.y IS NOT NULL 342 LEFT JOIN t4 ON true 343 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 344} { 345 - - 20 50 346 - - 30 50 347} 348do_execsql_test joinF-34 { 349 SELECT * 350 FROM t1 LEFT JOIN t2 ON true 351 RIGHT JOIN t3 ON t2.y IS NOT NULL 352 LEFT JOIN t4 ON true 353 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 354 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 355} { 356 - - 20 50 357 - - 30 50 358} 359do_execsql_test joinF-35 { 360 SELECT * 361 FROM t1 LEFT JOIN t2 ON true 362 RIGHT JOIN t3 ON t2.y IS NOT NULL 363 RIGHT JOIN t4 ON true 364 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 365} { 366 - - 20 50 367 - - 30 50 368} 369do_execsql_test joinF-36 { 370 SELECT * 371 FROM t1 LEFT JOIN t2 ON true 372 RIGHT JOIN t3 ON t2.y IS NOT NULL 373 RIGHT JOIN t4 ON true 374 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 375 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 376} { 377 - - 20 50 378 - - 30 50 379} 380do_execsql_test joinF-37 { 381 SELECT * 382 FROM t1 RIGHT JOIN t2 ON true 383 INNER JOIN t3 ON t2.y IS NOT NULL 384 INNER JOIN t4 ON true 385 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 386} { 387} 388do_execsql_test joinF-38 { 389 SELECT * 390 FROM t1 RIGHT JOIN t2 ON true 391 INNER JOIN t3 ON t2.y IS NOT NULL 392 INNER JOIN t4 ON true 393 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 394 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 395} { 396} 397do_execsql_test joinF-39 { 398 SELECT * 399 FROM t1 RIGHT JOIN t2 ON true 400 INNER JOIN t3 ON t2.y IS NOT NULL 401 LEFT JOIN t4 ON true 402 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 403} { 404} 405do_execsql_test joinF-40 { 406 SELECT * 407 FROM t1 RIGHT JOIN t2 ON true 408 INNER JOIN t3 ON t2.y IS NOT NULL 409 LEFT JOIN t4 ON true 410 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 411 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 412} { 413} 414do_execsql_test joinF-41 { 415 SELECT * 416 FROM t1 RIGHT JOIN t2 ON true 417 INNER JOIN t3 ON t2.y IS NOT NULL 418 RIGHT JOIN t4 ON true 419 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 420} { 421 - - - 50 422} 423do_execsql_test joinF-42 { 424 SELECT * 425 FROM t1 RIGHT JOIN t2 ON true 426 INNER JOIN t3 ON t2.y IS NOT NULL 427 RIGHT JOIN t4 ON true 428 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 429 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 430} { 431} 432do_execsql_test joinF-43 { 433 SELECT * 434 FROM t1 RIGHT JOIN t2 ON true 435 LEFT JOIN t3 ON t2.y IS NOT NULL 436 INNER JOIN t4 ON true 437 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 438} { 439} 440do_execsql_test joinF-44 { 441 SELECT * 442 FROM t1 RIGHT JOIN t2 ON true 443 LEFT JOIN t3 ON t2.y IS NOT NULL 444 INNER JOIN t4 ON true 445 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 446 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 447} { 448} 449do_execsql_test joinF-45 { 450 SELECT * 451 FROM t1 RIGHT JOIN t2 ON true 452 LEFT JOIN t3 ON t2.y IS NOT NULL 453 LEFT JOIN t4 ON true 454 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 455} { 456} 457do_execsql_test joinF-46 { 458 SELECT * 459 FROM t1 RIGHT JOIN t2 ON true 460 LEFT JOIN t3 ON t2.y IS NOT NULL 461 LEFT JOIN t4 ON true 462 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 463 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 464} { 465} 466do_execsql_test joinF-47 { 467 SELECT * 468 FROM t1 RIGHT JOIN t2 ON true 469 LEFT JOIN t3 ON t2.y IS NOT NULL 470 RIGHT JOIN t4 ON true 471 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 472} { 473 - - - 50 474} 475do_execsql_test joinF-48 { 476 SELECT * 477 FROM t1 RIGHT JOIN t2 ON true 478 LEFT JOIN t3 ON t2.y IS NOT NULL 479 RIGHT JOIN t4 ON true 480 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 481 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 482} { 483} 484do_execsql_test joinF-49 { 485 SELECT * 486 FROM t1 RIGHT JOIN t2 ON true 487 RIGHT JOIN t3 ON t2.y IS NOT NULL 488 INNER JOIN t4 ON true 489 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 490} { 491 - - 20 50 492 - - 30 50 493} 494do_execsql_test joinF-50 { 495 SELECT * 496 FROM t1 RIGHT JOIN t2 ON true 497 RIGHT JOIN t3 ON t2.y IS NOT NULL 498 INNER JOIN t4 ON true 499 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 500 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 501} { 502 - - 20 50 503 - - 30 50 504} 505do_execsql_test joinF-51 { 506 SELECT * 507 FROM t1 RIGHT JOIN t2 ON true 508 RIGHT JOIN t3 ON t2.y IS NOT NULL 509 LEFT JOIN t4 ON true 510 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 511} { 512 - - 20 50 513 - - 30 50 514} 515do_execsql_test joinF-52 { 516 SELECT * 517 FROM t1 RIGHT JOIN t2 ON true 518 RIGHT JOIN t3 ON t2.y IS NOT NULL 519 LEFT JOIN t4 ON true 520 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 521 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 522} { 523 - - 20 50 524 - - 30 50 525} 526do_execsql_test joinF-53 { 527 SELECT * 528 FROM t1 RIGHT JOIN t2 ON true 529 RIGHT JOIN t3 ON t2.y IS NOT NULL 530 RIGHT JOIN t4 ON true 531 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 532} { 533 - - 20 50 534 - - 30 50 535} 536do_execsql_test joinF-54 { 537 SELECT * 538 FROM t1 RIGHT JOIN t2 ON true 539 RIGHT JOIN t3 ON t2.y IS NOT NULL 540 RIGHT JOIN t4 ON true 541 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) 542 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); 543} { 544 - - 20 50 545 - - 30 50 546} 547finish_test 548 549############################################################################ 550# This is the TCL script used to generate the psql script that generated 551# the data above. 552# 553# puts " 554# \\pset border off 555# \\pset tuples_only on 556# \\pset null - 557# 558# DROP TABLE IF EXISTS t1; 559# DROP TABLE IF EXISTS t2; 560# DROP TABLE IF EXISTS t3; 561# DROP TABLE IF EXISTS t4; 562# CREATE TABLE t1(x INT); 563# CREATE TABLE t2(y INT); 564# CREATE TABLE t3(z INT); 565# CREATE TABLE t4(w INT); 566# INSERT INTO t1 VALUES(10); 567# INSERT INTO t3 VALUES(20),(30); 568# INSERT INTO t4 VALUES(50); 569# " 570# 571# proc echo {prefix txt} { 572# regsub -all {\n} $txt \n$prefix txt 573# puts "$prefix$txt" 574# } 575# 576# set n 0 577# foreach j1 {INNER LEFT RIGHT} { 578# foreach j2 {INNER LEFT RIGHT} { 579# foreach j3 {INNER LEFT RIGHT} { 580# 581# incr n 582# set q1 "" 583# append q1 "SELECT *\n" 584# append q1 " FROM t1 $j1 JOIN t2 ON true\n" 585# append q1 " $j2 JOIN t3 ON t2.y IS NOT NULL\n" 586# append q1 " $j3 JOIN t4 ON true\n" 587# append q1 " ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);" 588# 589# echo "\\qecho " "do_execsql_test joinF-$n \{" 590# echo "\\qecho X " $q1 591# echo "\\qecho " "\} \{" 592# puts $q1 593# echo "\\qecho " "\}" 594# 595# incr n 596# set q1 "" 597# append q1 "SELECT *\n" 598# append q1 " FROM t1 $j1 JOIN t2 ON true\n" 599# append q1 " $j2 JOIN t3 ON t2.y IS NOT NULL\n" 600# append q1 " $j3 JOIN t4 ON true\n" 601# append q1 " WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)\n" 602# append q1 " ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);" 603# 604# echo "\\qecho " "do_execsql_test joinF-$n \{" 605# echo "\\qecho X " $q1 606# echo "\\qecho " "\} \{" 607# puts $q1 608# echo "\\qecho " "\}" 609# 610# } 611# } 612# } 613# 614