os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/join.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2002 May 24
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.
sl@0
    12
#
sl@0
    13
# This file implements tests for joins, including outer joins.
sl@0
    14
#
sl@0
    15
# $Id: join.test,v 1.25 2008/08/14 00:19:49 drh Exp $
sl@0
    16
sl@0
    17
set testdir [file dirname $argv0]
sl@0
    18
source $testdir/tester.tcl
sl@0
    19
sl@0
    20
do_test join-1.1 {
sl@0
    21
  execsql {
sl@0
    22
    CREATE TABLE t1(a,b,c);
sl@0
    23
    INSERT INTO t1 VALUES(1,2,3);
sl@0
    24
    INSERT INTO t1 VALUES(2,3,4);
sl@0
    25
    INSERT INTO t1 VALUES(3,4,5);
sl@0
    26
    SELECT * FROM t1;
sl@0
    27
  }  
sl@0
    28
} {1 2 3 2 3 4 3 4 5}
sl@0
    29
do_test join-1.2 {
sl@0
    30
  execsql {
sl@0
    31
    CREATE TABLE t2(b,c,d);
sl@0
    32
    INSERT INTO t2 VALUES(1,2,3);
sl@0
    33
    INSERT INTO t2 VALUES(2,3,4);
sl@0
    34
    INSERT INTO t2 VALUES(3,4,5);
sl@0
    35
    SELECT * FROM t2;
sl@0
    36
  }  
sl@0
    37
} {1 2 3 2 3 4 3 4 5}
sl@0
    38
sl@0
    39
do_test join-1.3 {
sl@0
    40
  execsql2 {
sl@0
    41
    SELECT * FROM t1 NATURAL JOIN t2;
sl@0
    42
  }
sl@0
    43
} {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
sl@0
    44
do_test join-1.3.1 {
sl@0
    45
  execsql2 {
sl@0
    46
    SELECT * FROM t2 NATURAL JOIN t1;
sl@0
    47
  }
sl@0
    48
} {b 2 c 3 d 4 a 1 b 3 c 4 d 5 a 2}
sl@0
    49
do_test join-1.3.2 {
sl@0
    50
  execsql2 {
sl@0
    51
    SELECT * FROM t2 AS x NATURAL JOIN t1;
sl@0
    52
  }
sl@0
    53
} {b 2 c 3 d 4 a 1 b 3 c 4 d 5 a 2}
sl@0
    54
do_test join-1.3.3 {
sl@0
    55
  execsql2 {
sl@0
    56
    SELECT * FROM t2 NATURAL JOIN t1 AS y;
sl@0
    57
  }
sl@0
    58
} {b 2 c 3 d 4 a 1 b 3 c 4 d 5 a 2}
sl@0
    59
do_test join-1.3.4 {
sl@0
    60
  execsql {
sl@0
    61
    SELECT b FROM t1 NATURAL JOIN t2;
sl@0
    62
  }
sl@0
    63
} {2 3}
sl@0
    64
do_test join-1.4.1 {
sl@0
    65
  execsql2 {
sl@0
    66
    SELECT * FROM t1 INNER JOIN t2 USING(b,c);
sl@0
    67
  }
sl@0
    68
} {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
sl@0
    69
do_test join-1.4.2 {
sl@0
    70
  execsql2 {
sl@0
    71
    SELECT * FROM t1 AS x INNER JOIN t2 USING(b,c);
sl@0
    72
  }
sl@0
    73
} {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
sl@0
    74
do_test join-1.4.3 {
sl@0
    75
  execsql2 {
sl@0
    76
    SELECT * FROM t1 INNER JOIN t2 AS y USING(b,c);
sl@0
    77
  }
sl@0
    78
} {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
sl@0
    79
do_test join-1.4.4 {
sl@0
    80
  execsql2 {
sl@0
    81
    SELECT * FROM t1 AS x INNER JOIN t2 AS y USING(b,c);
sl@0
    82
  }
sl@0
    83
} {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
sl@0
    84
do_test join-1.4.5 {
sl@0
    85
  execsql {
sl@0
    86
    SELECT b FROM t1 JOIN t2 USING(b);
sl@0
    87
  }
sl@0
    88
} {2 3}
sl@0
    89
do_test join-1.5 {
sl@0
    90
  execsql2 {
sl@0
    91
    SELECT * FROM t1 INNER JOIN t2 USING(b);
sl@0
    92
  }
sl@0
    93
} {a 1 b 2 c 3 c 3 d 4 a 2 b 3 c 4 c 4 d 5}
sl@0
    94
do_test join-1.6 {
sl@0
    95
  execsql2 {
sl@0
    96
    SELECT * FROM t1 INNER JOIN t2 USING(c);
sl@0
    97
  }
sl@0
    98
} {a 1 b 2 c 3 b 2 d 4 a 2 b 3 c 4 b 3 d 5}
sl@0
    99
do_test join-1.7 {
sl@0
   100
  execsql2 {
sl@0
   101
    SELECT * FROM t1 INNER JOIN t2 USING(c,b);
sl@0
   102
  }
sl@0
   103
} {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
sl@0
   104
sl@0
   105
do_test join-1.8 {
sl@0
   106
  execsql {
sl@0
   107
    SELECT * FROM t1 NATURAL CROSS JOIN t2;
sl@0
   108
  }
sl@0
   109
} {1 2 3 4 2 3 4 5}
sl@0
   110
do_test join-1.9 {
sl@0
   111
  execsql {
sl@0
   112
    SELECT * FROM t1 CROSS JOIN t2 USING(b,c);
sl@0
   113
  }
sl@0
   114
} {1 2 3 4 2 3 4 5}
sl@0
   115
do_test join-1.10 {
sl@0
   116
  execsql {
sl@0
   117
    SELECT * FROM t1 NATURAL INNER JOIN t2;
sl@0
   118
  }
sl@0
   119
} {1 2 3 4 2 3 4 5}
sl@0
   120
do_test join-1.11 {
sl@0
   121
  execsql {
sl@0
   122
    SELECT * FROM t1 INNER JOIN t2 USING(b,c);
sl@0
   123
  }
sl@0
   124
} {1 2 3 4 2 3 4 5}
sl@0
   125
do_test join-1.12 {
sl@0
   126
  execsql {
sl@0
   127
    SELECT * FROM t1 natural inner join t2;
sl@0
   128
  }
sl@0
   129
} {1 2 3 4 2 3 4 5}
sl@0
   130
sl@0
   131
ifcapable subquery {
sl@0
   132
  do_test join-1.13 {
sl@0
   133
    execsql2 {
sl@0
   134
      SELECT * FROM t1 NATURAL JOIN 
sl@0
   135
        (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as t3
sl@0
   136
    }
sl@0
   137
  } {a 1 b 2 c 3 d 4 e 5}
sl@0
   138
  do_test join-1.14 {
sl@0
   139
    execsql2 {
sl@0
   140
      SELECT * FROM (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as 'tx'
sl@0
   141
          NATURAL JOIN t1
sl@0
   142
    }
sl@0
   143
  } {c 3 d 4 e 5 a 1 b 2}
sl@0
   144
}
sl@0
   145
sl@0
   146
do_test join-1.15 {
sl@0
   147
  execsql {
sl@0
   148
    CREATE TABLE t3(c,d,e);
sl@0
   149
    INSERT INTO t3 VALUES(2,3,4);
sl@0
   150
    INSERT INTO t3 VALUES(3,4,5);
sl@0
   151
    INSERT INTO t3 VALUES(4,5,6);
sl@0
   152
    SELECT * FROM t3;
sl@0
   153
  }  
sl@0
   154
} {2 3 4 3 4 5 4 5 6}
sl@0
   155
do_test join-1.16 {
sl@0
   156
  execsql {
sl@0
   157
    SELECT * FROM t1 natural join t2 natural join t3;
sl@0
   158
  }
sl@0
   159
} {1 2 3 4 5 2 3 4 5 6}
sl@0
   160
do_test join-1.17 {
sl@0
   161
  execsql2 {
sl@0
   162
    SELECT * FROM t1 natural join t2 natural join t3;
sl@0
   163
  }
sl@0
   164
} {a 1 b 2 c 3 d 4 e 5 a 2 b 3 c 4 d 5 e 6}
sl@0
   165
do_test join-1.18 {
sl@0
   166
  execsql {
sl@0
   167
    CREATE TABLE t4(d,e,f);
sl@0
   168
    INSERT INTO t4 VALUES(2,3,4);
sl@0
   169
    INSERT INTO t4 VALUES(3,4,5);
sl@0
   170
    INSERT INTO t4 VALUES(4,5,6);
sl@0
   171
    SELECT * FROM t4;
sl@0
   172
  }  
sl@0
   173
} {2 3 4 3 4 5 4 5 6}
sl@0
   174
do_test join-1.19.1 {
sl@0
   175
  execsql {
sl@0
   176
    SELECT * FROM t1 natural join t2 natural join t4;
sl@0
   177
  }
sl@0
   178
} {1 2 3 4 5 6}
sl@0
   179
do_test join-1.19.2 {
sl@0
   180
  execsql2 {
sl@0
   181
    SELECT * FROM t1 natural join t2 natural join t4;
sl@0
   182
  }
sl@0
   183
} {a 1 b 2 c 3 d 4 e 5 f 6}
sl@0
   184
do_test join-1.20 {
sl@0
   185
  execsql {
sl@0
   186
    SELECT * FROM t1 natural join t2 natural join t3 WHERE t1.a=1
sl@0
   187
  }
sl@0
   188
} {1 2 3 4 5}
sl@0
   189
sl@0
   190
do_test join-2.1 {
sl@0
   191
  execsql {
sl@0
   192
    SELECT * FROM t1 NATURAL LEFT JOIN t2;
sl@0
   193
  }
sl@0
   194
} {1 2 3 4 2 3 4 5 3 4 5 {}}
sl@0
   195
do_test join-2.2 {
sl@0
   196
  execsql {
sl@0
   197
    SELECT * FROM t2 NATURAL LEFT OUTER JOIN t1;
sl@0
   198
  }
sl@0
   199
} {1 2 3 {} 2 3 4 1 3 4 5 2}
sl@0
   200
do_test join-2.3 {
sl@0
   201
  catchsql {
sl@0
   202
    SELECT * FROM t1 NATURAL RIGHT OUTER JOIN t2;
sl@0
   203
  }
sl@0
   204
} {1 {RIGHT and FULL OUTER JOINs are not currently supported}}
sl@0
   205
do_test join-2.4 {
sl@0
   206
  execsql {
sl@0
   207
    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d
sl@0
   208
  }
sl@0
   209
} {1 2 3 {} {} {} 2 3 4 {} {} {} 3 4 5 1 2 3}
sl@0
   210
do_test join-2.5 {
sl@0
   211
  execsql {
sl@0
   212
    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t1.a>1
sl@0
   213
  }
sl@0
   214
} {2 3 4 {} {} {} 3 4 5 1 2 3}
sl@0
   215
do_test join-2.6 {
sl@0
   216
  execsql {
sl@0
   217
    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t2.b IS NULL OR t2.b>1
sl@0
   218
  }
sl@0
   219
} {1 2 3 {} {} {} 2 3 4 {} {} {}}
sl@0
   220
sl@0
   221
do_test join-3.1 {
sl@0
   222
  catchsql {
sl@0
   223
    SELECT * FROM t1 NATURAL JOIN t2 ON t1.a=t2.b;
sl@0
   224
  }
sl@0
   225
} {1 {a NATURAL join may not have an ON or USING clause}}
sl@0
   226
do_test join-3.2 {
sl@0
   227
  catchsql {
sl@0
   228
    SELECT * FROM t1 NATURAL JOIN t2 USING(b);
sl@0
   229
  }
sl@0
   230
} {1 {a NATURAL join may not have an ON or USING clause}}
sl@0
   231
do_test join-3.3 {
sl@0
   232
  catchsql {
sl@0
   233
    SELECT * FROM t1 JOIN t2 ON t1.a=t2.b USING(b);
sl@0
   234
  }
sl@0
   235
} {1 {cannot have both ON and USING clauses in the same join}}
sl@0
   236
do_test join-3.4.1 {
sl@0
   237
  catchsql {
sl@0
   238
    SELECT * FROM t1 JOIN t2 USING(a);
sl@0
   239
  }
sl@0
   240
} {1 {cannot join using column a - column not present in both tables}}
sl@0
   241
do_test join-3.4.2 {
sl@0
   242
  catchsql {
sl@0
   243
    SELECT * FROM t1 JOIN t2 USING(d);
sl@0
   244
  }
sl@0
   245
} {1 {cannot join using column d - column not present in both tables}}
sl@0
   246
do_test join-3.5 {
sl@0
   247
  catchsql {
sl@0
   248
    SELECT * FROM t1 USING(a);
sl@0
   249
  }
sl@0
   250
} {0 {1 2 3 2 3 4 3 4 5}}
sl@0
   251
do_test join-3.6 {
sl@0
   252
  catchsql {
sl@0
   253
    SELECT * FROM t1 JOIN t2 ON t3.a=t2.b;
sl@0
   254
  }
sl@0
   255
} {1 {no such column: t3.a}}
sl@0
   256
do_test join-3.7 {
sl@0
   257
  catchsql {
sl@0
   258
    SELECT * FROM t1 INNER OUTER JOIN t2;
sl@0
   259
  }
sl@0
   260
} {1 {unknown or unsupported join type: INNER OUTER}}
sl@0
   261
do_test join-3.8 {
sl@0
   262
  catchsql {
sl@0
   263
    SELECT * FROM t1 INNER OUTER CROSS JOIN t2;
sl@0
   264
  }
sl@0
   265
} {1 {unknown or unsupported join type: INNER OUTER CROSS}}
sl@0
   266
do_test join-3.9 {
sl@0
   267
  catchsql {
sl@0
   268
    SELECT * FROM t1 OUTER NATURAL INNER JOIN t2;
sl@0
   269
  }
sl@0
   270
} {1 {unknown or unsupported join type: OUTER NATURAL INNER}}
sl@0
   271
do_test join-3.10 {
sl@0
   272
  catchsql {
sl@0
   273
    SELECT * FROM t1 LEFT BOGUS JOIN t2;
sl@0
   274
  }
sl@0
   275
} {1 {unknown or unsupported join type: LEFT BOGUS}}
sl@0
   276
do_test join-3.11 {
sl@0
   277
  catchsql {
sl@0
   278
    SELECT * FROM t1 INNER BOGUS CROSS JOIN t2;
sl@0
   279
  }
sl@0
   280
} {1 {unknown or unsupported join type: INNER BOGUS CROSS}}
sl@0
   281
do_test join-3.12 {
sl@0
   282
  catchsql {
sl@0
   283
    SELECT * FROM t1 NATURAL AWK SED JOIN t2;
sl@0
   284
  }
sl@0
   285
} {1 {unknown or unsupported join type: NATURAL AWK SED}}
sl@0
   286
sl@0
   287
do_test join-4.1 {
sl@0
   288
  execsql {
sl@0
   289
    BEGIN;
sl@0
   290
    CREATE TABLE t5(a INTEGER PRIMARY KEY);
sl@0
   291
    CREATE TABLE t6(a INTEGER);
sl@0
   292
    INSERT INTO t6 VALUES(NULL);
sl@0
   293
    INSERT INTO t6 VALUES(NULL);
sl@0
   294
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   295
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   296
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   297
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   298
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   299
    INSERT INTO t6 SELECT * FROM t6;
sl@0
   300
    COMMIT;
sl@0
   301
  }
sl@0
   302
  execsql {
sl@0
   303
    SELECT * FROM t6 NATURAL JOIN t5;
sl@0
   304
  }
sl@0
   305
} {}
sl@0
   306
do_test join-4.2 {
sl@0
   307
  execsql {
sl@0
   308
    SELECT * FROM t6, t5 WHERE t6.a<t5.a;
sl@0
   309
  }
sl@0
   310
} {}
sl@0
   311
do_test join-4.3 {
sl@0
   312
  execsql {
sl@0
   313
    SELECT * FROM t6, t5 WHERE t6.a>t5.a;
sl@0
   314
  }
sl@0
   315
} {}
sl@0
   316
do_test join-4.4 {
sl@0
   317
  execsql {
sl@0
   318
    UPDATE t6 SET a='xyz';
sl@0
   319
    SELECT * FROM t6 NATURAL JOIN t5;
sl@0
   320
  }
sl@0
   321
} {}
sl@0
   322
do_test join-4.6 {
sl@0
   323
  execsql {
sl@0
   324
    SELECT * FROM t6, t5 WHERE t6.a<t5.a;
sl@0
   325
  }
sl@0
   326
} {}
sl@0
   327
do_test join-4.7 {
sl@0
   328
  execsql {
sl@0
   329
    SELECT * FROM t6, t5 WHERE t6.a>t5.a;
sl@0
   330
  }
sl@0
   331
} {}
sl@0
   332
do_test join-4.8 {
sl@0
   333
  execsql {
sl@0
   334
    UPDATE t6 SET a=1;
sl@0
   335
    SELECT * FROM t6 NATURAL JOIN t5;
sl@0
   336
  }
sl@0
   337
} {}
sl@0
   338
do_test join-4.9 {
sl@0
   339
  execsql {
sl@0
   340
    SELECT * FROM t6, t5 WHERE t6.a<t5.a;
sl@0
   341
  }
sl@0
   342
} {}
sl@0
   343
do_test join-4.10 {
sl@0
   344
  execsql {
sl@0
   345
    SELECT * FROM t6, t5 WHERE t6.a>t5.a;
sl@0
   346
  }
sl@0
   347
} {}
sl@0
   348
sl@0
   349
do_test join-5.1 {
sl@0
   350
  execsql {
sl@0
   351
    BEGIN;
sl@0
   352
    create table centros (id integer primary key, centro);
sl@0
   353
    INSERT INTO centros VALUES(1,'xxx');
sl@0
   354
    create table usuarios (id integer primary key, nombre, apellidos,
sl@0
   355
    idcentro integer);
sl@0
   356
    INSERT INTO usuarios VALUES(1,'a','aa',1);
sl@0
   357
    INSERT INTO usuarios VALUES(2,'b','bb',1);
sl@0
   358
    INSERT INTO usuarios VALUES(3,'c','cc',NULL);
sl@0
   359
    create index idcentro on usuarios (idcentro);
sl@0
   360
    END;
sl@0
   361
    select usuarios.id, usuarios.nombre, centros.centro from
sl@0
   362
    usuarios left outer join centros on usuarios.idcentro = centros.id;
sl@0
   363
  }
sl@0
   364
} {1 a xxx 2 b xxx 3 c {}}
sl@0
   365
sl@0
   366
# A test for ticket #247.
sl@0
   367
#
sl@0
   368
do_test join-7.1 {
sl@0
   369
  execsql {
sl@0
   370
    CREATE TABLE t7 (x, y);
sl@0
   371
    INSERT INTO t7 VALUES ("pa1", 1);
sl@0
   372
    INSERT INTO t7 VALUES ("pa2", NULL);
sl@0
   373
    INSERT INTO t7 VALUES ("pa3", NULL);
sl@0
   374
    INSERT INTO t7 VALUES ("pa4", 2);
sl@0
   375
    INSERT INTO t7 VALUES ("pa30", 131);
sl@0
   376
    INSERT INTO t7 VALUES ("pa31", 130);
sl@0
   377
    INSERT INTO t7 VALUES ("pa28", NULL);
sl@0
   378
sl@0
   379
    CREATE TABLE t8 (a integer primary key, b);
sl@0
   380
    INSERT INTO t8 VALUES (1, "pa1");
sl@0
   381
    INSERT INTO t8 VALUES (2, "pa4");
sl@0
   382
    INSERT INTO t8 VALUES (3, NULL);
sl@0
   383
    INSERT INTO t8 VALUES (4, NULL);
sl@0
   384
    INSERT INTO t8 VALUES (130, "pa31");
sl@0
   385
    INSERT INTO t8 VALUES (131, "pa30");
sl@0
   386
sl@0
   387
    SELECT coalesce(t8.a,999) from t7 LEFT JOIN t8 on y=a;
sl@0
   388
  }
sl@0
   389
} {1 999 999 2 131 130 999}
sl@0
   390
sl@0
   391
# Make sure a left join where the right table is really a view that
sl@0
   392
# is itself a join works right.  Ticket #306.
sl@0
   393
#
sl@0
   394
ifcapable view {
sl@0
   395
do_test join-8.1 {
sl@0
   396
  execsql {
sl@0
   397
    BEGIN;
sl@0
   398
    CREATE TABLE t9(a INTEGER PRIMARY KEY, b);
sl@0
   399
    INSERT INTO t9 VALUES(1,11);
sl@0
   400
    INSERT INTO t9 VALUES(2,22);
sl@0
   401
    CREATE TABLE t10(x INTEGER PRIMARY KEY, y);
sl@0
   402
    INSERT INTO t10 VALUES(1,2);
sl@0
   403
    INSERT INTO t10 VALUES(3,3);    
sl@0
   404
    CREATE TABLE t11(p INTEGER PRIMARY KEY, q);
sl@0
   405
    INSERT INTO t11 VALUES(2,111);
sl@0
   406
    INSERT INTO t11 VALUES(3,333);    
sl@0
   407
    CREATE VIEW v10_11 AS SELECT x, q FROM t10, t11 WHERE t10.y=t11.p;
sl@0
   408
    COMMIT;
sl@0
   409
    SELECT * FROM t9 LEFT JOIN v10_11 ON( a=x );
sl@0
   410
  }
sl@0
   411
} {1 11 1 111 2 22 {} {}}
sl@0
   412
ifcapable subquery {
sl@0
   413
  do_test join-8.2 {
sl@0
   414
    execsql {
sl@0
   415
      SELECT * FROM t9 LEFT JOIN (SELECT x, q FROM t10, t11 WHERE t10.y=t11.p)
sl@0
   416
           ON( a=x);
sl@0
   417
    }
sl@0
   418
  } {1 11 1 111 2 22 {} {}}
sl@0
   419
}
sl@0
   420
do_test join-8.3 {
sl@0
   421
  execsql {
sl@0
   422
    SELECT * FROM v10_11 LEFT JOIN t9 ON( a=x );
sl@0
   423
  }
sl@0
   424
} {1 111 1 11 3 333 {} {}}
sl@0
   425
ifcapable subquery {
sl@0
   426
  # Constant expressions in a subquery that is the right element of a
sl@0
   427
  # LEFT JOIN evaluate to NULL for rows where the LEFT JOIN does not
sl@0
   428
  # match.  Ticket #3300
sl@0
   429
  do_test join-8.4 {
sl@0
   430
    execsql {
sl@0
   431
      SELECT * FROM t9 LEFT JOIN (SELECT 44, p, q FROM t11) AS sub1 ON p=a
sl@0
   432
    }
sl@0
   433
  } {1 11 {} {} {} 2 22 44 2 111}
sl@0
   434
}
sl@0
   435
} ;# ifcapable view
sl@0
   436
sl@0
   437
# Ticket #350 describes a scenario where LEFT OUTER JOIN does not
sl@0
   438
# function correctly if the right table in the join is really
sl@0
   439
# subquery.
sl@0
   440
#
sl@0
   441
# To test the problem, we generate the same LEFT OUTER JOIN in two
sl@0
   442
# separate selects but with on using a subquery and the other calling
sl@0
   443
# the table directly.  Then connect the two SELECTs using an EXCEPT.
sl@0
   444
# Both queries should generate the same results so the answer should
sl@0
   445
# be an empty set.
sl@0
   446
#
sl@0
   447
ifcapable compound {
sl@0
   448
do_test join-9.1 {
sl@0
   449
  execsql {
sl@0
   450
    BEGIN;
sl@0
   451
    CREATE TABLE t12(a,b);
sl@0
   452
    INSERT INTO t12 VALUES(1,11);
sl@0
   453
    INSERT INTO t12 VALUES(2,22);
sl@0
   454
    CREATE TABLE t13(b,c);
sl@0
   455
    INSERT INTO t13 VALUES(22,222);
sl@0
   456
    COMMIT;
sl@0
   457
  }
sl@0
   458
} {}
sl@0
   459
sl@0
   460
ifcapable subquery {
sl@0
   461
  do_test join-9.1.1 {
sl@0
   462
    execsql {
sl@0
   463
      SELECT * FROM t12 NATURAL LEFT JOIN t13
sl@0
   464
      EXCEPT
sl@0
   465
      SELECT * FROM t12 NATURAL LEFT JOIN (SELECT * FROM t13 WHERE b>0);
sl@0
   466
    }
sl@0
   467
  } {}
sl@0
   468
}
sl@0
   469
ifcapable view {
sl@0
   470
  do_test join-9.2 {
sl@0
   471
    execsql {
sl@0
   472
      CREATE VIEW v13 AS SELECT * FROM t13 WHERE b>0;
sl@0
   473
      SELECT * FROM t12 NATURAL LEFT JOIN t13
sl@0
   474
        EXCEPT
sl@0
   475
        SELECT * FROM t12 NATURAL LEFT JOIN v13;
sl@0
   476
    }
sl@0
   477
  } {}
sl@0
   478
} ;# ifcapable view
sl@0
   479
} ;# ifcapable compound
sl@0
   480
sl@0
   481
ifcapable subquery {
sl@0
   482
  # Ticket #1697:  Left Join WHERE clause terms that contain an
sl@0
   483
  # aggregate subquery.
sl@0
   484
  #
sl@0
   485
  do_test join-10.1 {
sl@0
   486
    execsql {
sl@0
   487
      CREATE TABLE t21(a,b,c);
sl@0
   488
      CREATE TABLE t22(p,q);
sl@0
   489
      CREATE INDEX i22 ON t22(q);
sl@0
   490
      SELECT a FROM t21 LEFT JOIN t22 ON b=p WHERE q=
sl@0
   491
         (SELECT max(m.q) FROM t22 m JOIN t21 n ON n.b=m.p WHERE n.c=1);
sl@0
   492
    }  
sl@0
   493
  } {}
sl@0
   494
sl@0
   495
  # Test a LEFT JOIN when the right-hand side of hte join is an empty
sl@0
   496
  # sub-query. Seems fine.
sl@0
   497
  #
sl@0
   498
  do_test join-10.2 {
sl@0
   499
    execsql {
sl@0
   500
      CREATE TABLE t23(a, b, c);
sl@0
   501
      CREATE TABLE t24(a, b, c);
sl@0
   502
      INSERT INTO t23 VALUES(1, 2, 3);
sl@0
   503
    }
sl@0
   504
    execsql {
sl@0
   505
      SELECT * FROM t23 LEFT JOIN t24;
sl@0
   506
    }
sl@0
   507
  } {1 2 3 {} {} {}}
sl@0
   508
  do_test join-10.3 {
sl@0
   509
    execsql {
sl@0
   510
      SELECT * FROM t23 LEFT JOIN (SELECT * FROM t24);
sl@0
   511
    }
sl@0
   512
  } {1 2 3 {} {} {}}
sl@0
   513
sl@0
   514
} ;# ifcapable subquery
sl@0
   515
sl@0
   516
finish_test