os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/select6.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
# 2001 September 15
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.  The
sl@0
    12
# focus of this file is testing SELECT statements that contain
sl@0
    13
# subqueries in their FROM clause.
sl@0
    14
#
sl@0
    15
# $Id: select6.test,v 1.28 2008/08/20 16:35:10 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
# Omit this whole file if the library is build without subquery support.
sl@0
    21
ifcapable !subquery {
sl@0
    22
  finish_test
sl@0
    23
  return
sl@0
    24
}
sl@0
    25
sl@0
    26
do_test select6-1.0 {
sl@0
    27
  execsql {
sl@0
    28
    BEGIN;
sl@0
    29
    CREATE TABLE t1(x, y);
sl@0
    30
    INSERT INTO t1 VALUES(1,1);
sl@0
    31
    INSERT INTO t1 VALUES(2,2);
sl@0
    32
    INSERT INTO t1 VALUES(3,2);
sl@0
    33
    INSERT INTO t1 VALUES(4,3);
sl@0
    34
    INSERT INTO t1 VALUES(5,3);
sl@0
    35
    INSERT INTO t1 VALUES(6,3);
sl@0
    36
    INSERT INTO t1 VALUES(7,3);
sl@0
    37
    INSERT INTO t1 VALUES(8,4);
sl@0
    38
    INSERT INTO t1 VALUES(9,4);
sl@0
    39
    INSERT INTO t1 VALUES(10,4);
sl@0
    40
    INSERT INTO t1 VALUES(11,4);
sl@0
    41
    INSERT INTO t1 VALUES(12,4);
sl@0
    42
    INSERT INTO t1 VALUES(13,4);
sl@0
    43
    INSERT INTO t1 VALUES(14,4);
sl@0
    44
    INSERT INTO t1 VALUES(15,4);
sl@0
    45
    INSERT INTO t1 VALUES(16,5);
sl@0
    46
    INSERT INTO t1 VALUES(17,5);
sl@0
    47
    INSERT INTO t1 VALUES(18,5);
sl@0
    48
    INSERT INTO t1 VALUES(19,5);
sl@0
    49
    INSERT INTO t1 VALUES(20,5);
sl@0
    50
    COMMIT;
sl@0
    51
    SELECT DISTINCT y FROM t1 ORDER BY y;
sl@0
    52
  }
sl@0
    53
} {1 2 3 4 5}
sl@0
    54
sl@0
    55
do_test select6-1.1 {
sl@0
    56
  execsql2 {SELECT * FROM (SELECT x, y FROM t1 WHERE x<2)}
sl@0
    57
} {x 1 y 1}
sl@0
    58
do_test select6-1.2 {
sl@0
    59
  execsql {SELECT count(*) FROM (SELECT y FROM t1)}
sl@0
    60
} {20}
sl@0
    61
do_test select6-1.3 {
sl@0
    62
  execsql {SELECT count(*) FROM (SELECT DISTINCT y FROM t1)}
sl@0
    63
} {5}
sl@0
    64
do_test select6-1.4 {
sl@0
    65
  execsql {SELECT count(*) FROM (SELECT DISTINCT * FROM (SELECT y FROM t1))}
sl@0
    66
} {5}
sl@0
    67
do_test select6-1.5 {
sl@0
    68
  execsql {SELECT count(*) FROM (SELECT * FROM (SELECT DISTINCT y FROM t1))}
sl@0
    69
} {5}
sl@0
    70
sl@0
    71
do_test select6-1.6 {
sl@0
    72
  execsql {
sl@0
    73
    SELECT * 
sl@0
    74
    FROM (SELECT count(*),y FROM t1 GROUP BY y) AS a,
sl@0
    75
         (SELECT max(x),y FROM t1 GROUP BY y) as b
sl@0
    76
    WHERE a.y=b.y ORDER BY a.y
sl@0
    77
  }
sl@0
    78
} {1 1 1 1 2 2 3 2 4 3 7 3 8 4 15 4 5 5 20 5}
sl@0
    79
do_test select6-1.7 {
sl@0
    80
  execsql {
sl@0
    81
    SELECT a.y, a.[count(*)], [max(x)], [count(*)]
sl@0
    82
    FROM (SELECT count(*),y FROM t1 GROUP BY y) AS a,
sl@0
    83
         (SELECT max(x),y FROM t1 GROUP BY y) as b
sl@0
    84
    WHERE a.y=b.y ORDER BY a.y
sl@0
    85
  }
sl@0
    86
} {1 1 1 1 2 2 3 2 3 4 7 4 4 8 15 8 5 5 20 5}
sl@0
    87
do_test select6-1.8 {
sl@0
    88
  execsql {
sl@0
    89
    SELECT q, p, r
sl@0
    90
    FROM (SELECT count(*) as p , y as q FROM t1 GROUP BY y) AS a,
sl@0
    91
         (SELECT max(x) as r, y as s FROM t1 GROUP BY y) as b
sl@0
    92
    WHERE q=s ORDER BY s
sl@0
    93
  }
sl@0
    94
} {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
sl@0
    95
do_test select6-1.9 {
sl@0
    96
  execsql {
sl@0
    97
    SELECT q, p, r, b.[min(x)+y]
sl@0
    98
    FROM (SELECT count(*) as p , y as q FROM t1 GROUP BY y) AS a,
sl@0
    99
         (SELECT max(x) as r, y as s, min(x)+y FROM t1 GROUP BY y) as b
sl@0
   100
    WHERE q=s ORDER BY s
sl@0
   101
  }
sl@0
   102
} {1 1 1 2 2 2 3 4 3 4 7 7 4 8 15 12 5 5 20 21}
sl@0
   103
sl@0
   104
do_test select6-2.0 {
sl@0
   105
  execsql {
sl@0
   106
    CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
sl@0
   107
    INSERT INTO t2 SELECT * FROM t1;
sl@0
   108
    SELECT DISTINCT b FROM t2 ORDER BY b;
sl@0
   109
  }
sl@0
   110
} {1 2 3 4 5}
sl@0
   111
do_test select6-2.1 {
sl@0
   112
  execsql2 {SELECT * FROM (SELECT a, b FROM t2 WHERE a<2)}
sl@0
   113
} {a 1 b 1}
sl@0
   114
do_test select6-2.2 {
sl@0
   115
  execsql {SELECT count(*) FROM (SELECT b FROM t2)}
sl@0
   116
} {20}
sl@0
   117
do_test select6-2.3 {
sl@0
   118
  execsql {SELECT count(*) FROM (SELECT DISTINCT b FROM t2)}
sl@0
   119
} {5}
sl@0
   120
do_test select6-2.4 {
sl@0
   121
  execsql {SELECT count(*) FROM (SELECT DISTINCT * FROM (SELECT b FROM t2))}
sl@0
   122
} {5}
sl@0
   123
do_test select6-2.5 {
sl@0
   124
  execsql {SELECT count(*) FROM (SELECT * FROM (SELECT DISTINCT b FROM t2))}
sl@0
   125
} {5}
sl@0
   126
sl@0
   127
do_test select6-2.6 {
sl@0
   128
  execsql {
sl@0
   129
    SELECT * 
sl@0
   130
    FROM (SELECT count(*),b FROM t2 GROUP BY b) AS a,
sl@0
   131
         (SELECT max(a),b FROM t2 GROUP BY b) as b
sl@0
   132
    WHERE a.b=b.b ORDER BY a.b
sl@0
   133
  }
sl@0
   134
} {1 1 1 1 2 2 3 2 4 3 7 3 8 4 15 4 5 5 20 5}
sl@0
   135
do_test select6-2.7 {
sl@0
   136
  execsql {
sl@0
   137
    SELECT a.b, a.[count(*)], [max(a)], [count(*)]
sl@0
   138
    FROM (SELECT count(*),b FROM t2 GROUP BY b) AS a,
sl@0
   139
         (SELECT max(a),b FROM t2 GROUP BY b) as b
sl@0
   140
    WHERE a.b=b.b ORDER BY a.b
sl@0
   141
  }
sl@0
   142
} {1 1 1 1 2 2 3 2 3 4 7 4 4 8 15 8 5 5 20 5}
sl@0
   143
do_test select6-2.8 {
sl@0
   144
  execsql {
sl@0
   145
    SELECT q, p, r
sl@0
   146
    FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY b) AS a,
sl@0
   147
         (SELECT max(a) as r, b as s FROM t2 GROUP BY b) as b
sl@0
   148
    WHERE q=s ORDER BY s
sl@0
   149
  }
sl@0
   150
} {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
sl@0
   151
do_test select6-2.9 {
sl@0
   152
  execsql {
sl@0
   153
    SELECT a.q, a.p, b.r
sl@0
   154
    FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY q) AS a,
sl@0
   155
         (SELECT max(a) as r, b as s FROM t2 GROUP BY s) as b
sl@0
   156
    WHERE a.q=b.s ORDER BY a.q
sl@0
   157
  }
sl@0
   158
} {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
sl@0
   159
sl@0
   160
do_test select6-3.1 {
sl@0
   161
  execsql2 {
sl@0
   162
    SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE x=3));
sl@0
   163
  }
sl@0
   164
} {x 3 y 2}
sl@0
   165
do_test select6-3.2 {
sl@0
   166
  execsql {
sl@0
   167
    SELECT * FROM
sl@0
   168
      (SELECT a.q, a.p, b.r
sl@0
   169
       FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY q) AS a,
sl@0
   170
            (SELECT max(a) as r, b as s FROM t2 GROUP BY s) as b
sl@0
   171
       WHERE a.q=b.s ORDER BY a.q)
sl@0
   172
    ORDER BY "a.q"
sl@0
   173
  }
sl@0
   174
} {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
sl@0
   175
do_test select6-3.3 {
sl@0
   176
  execsql {
sl@0
   177
    SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
sl@0
   178
  }
sl@0
   179
} {10.5 3.7 14.2}
sl@0
   180
do_test select6-3.4 {
sl@0
   181
  execsql {
sl@0
   182
    SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
sl@0
   183
  }
sl@0
   184
} {11.5 4.0 15.5}
sl@0
   185
do_test select6-3.5 {
sl@0
   186
  execsql {
sl@0
   187
    SELECT x,y,x+y FROM (SELECT avg(a) as 'x', avg(b) as 'y' FROM t2 WHERE a=4)
sl@0
   188
  }
sl@0
   189
} {4.0 3.0 7.0}
sl@0
   190
do_test select6-3.6 {
sl@0
   191
  execsql {
sl@0
   192
    SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
sl@0
   193
    WHERE a>10
sl@0
   194
  }
sl@0
   195
} {10.5 3.7 14.2}
sl@0
   196
do_test select6-3.7 {
sl@0
   197
  execsql {
sl@0
   198
    SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
sl@0
   199
    WHERE a<10
sl@0
   200
  }
sl@0
   201
} {}
sl@0
   202
do_test select6-3.8 {
sl@0
   203
  execsql {
sl@0
   204
    SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
sl@0
   205
    WHERE a>10
sl@0
   206
  }
sl@0
   207
} {11.5 4.0 15.5}
sl@0
   208
do_test select6-3.9 {
sl@0
   209
  execsql {
sl@0
   210
    SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
sl@0
   211
    WHERE a<10
sl@0
   212
  }
sl@0
   213
} {}
sl@0
   214
do_test select6-3.10 {
sl@0
   215
  execsql {
sl@0
   216
    SELECT a,b,a+b FROM (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
sl@0
   217
    ORDER BY a
sl@0
   218
  }
sl@0
   219
} {1.0 1 2.0 2.5 2 4.5 5.5 3 8.5 11.5 4 15.5 18.0 5 23.0}
sl@0
   220
do_test select6-3.11 {
sl@0
   221
  execsql {
sl@0
   222
    SELECT a,b,a+b FROM 
sl@0
   223
       (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
sl@0
   224
    WHERE b<4 ORDER BY a
sl@0
   225
  }
sl@0
   226
} {1.0 1 2.0 2.5 2 4.5 5.5 3 8.5}
sl@0
   227
do_test select6-3.12 {
sl@0
   228
  execsql {
sl@0
   229
    SELECT a,b,a+b FROM 
sl@0
   230
       (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
sl@0
   231
    WHERE b<4 ORDER BY a
sl@0
   232
  }
sl@0
   233
} {2.5 2 4.5 5.5 3 8.5}
sl@0
   234
do_test select6-3.13 {
sl@0
   235
  execsql {
sl@0
   236
    SELECT a,b,a+b FROM 
sl@0
   237
       (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
sl@0
   238
    ORDER BY a
sl@0
   239
  }
sl@0
   240
} {2.5 2 4.5 5.5 3 8.5 11.5 4 15.5 18.0 5 23.0}
sl@0
   241
do_test select6-3.14 {
sl@0
   242
  execsql {
sl@0
   243
    SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
sl@0
   244
    ORDER BY [count(*)]
sl@0
   245
  }
sl@0
   246
} {1 1 2 2 4 3 5 5 8 4}
sl@0
   247
do_test select6-3.15 {
sl@0
   248
  execsql {
sl@0
   249
    SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
sl@0
   250
    ORDER BY y
sl@0
   251
  }
sl@0
   252
} {1 1 2 2 4 3 8 4 5 5}
sl@0
   253
sl@0
   254
do_test select6-4.1 {
sl@0
   255
  execsql {
sl@0
   256
    SELECT a,b,c FROM 
sl@0
   257
      (SELECT x AS 'a', y AS 'b', x+y AS 'c' FROM t1 WHERE y=4)
sl@0
   258
    WHERE a<10 ORDER BY a;
sl@0
   259
  }
sl@0
   260
} {8 4 12 9 4 13}
sl@0
   261
do_test select6-4.2 {
sl@0
   262
  execsql {
sl@0
   263
    SELECT y FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
sl@0
   264
  }
sl@0
   265
} {1 2 3 4}
sl@0
   266
do_test select6-4.3 {
sl@0
   267
  execsql {
sl@0
   268
    SELECT DISTINCT y FROM (SELECT y FROM t1) WHERE y<5 ORDER BY y
sl@0
   269
  }
sl@0
   270
} {1 2 3 4}
sl@0
   271
do_test select6-4.4 {
sl@0
   272
  execsql {
sl@0
   273
    SELECT avg(y) FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
sl@0
   274
  }
sl@0
   275
} {2.5}
sl@0
   276
do_test select6-4.5 {
sl@0
   277
  execsql {
sl@0
   278
    SELECT avg(y) FROM (SELECT DISTINCT y FROM t1 WHERE y<5) ORDER BY y
sl@0
   279
  }
sl@0
   280
} {2.5}
sl@0
   281
sl@0
   282
do_test select6-5.1 {
sl@0
   283
  execsql {
sl@0
   284
    SELECT a,x,b FROM
sl@0
   285
      (SELECT x+3 AS 'a', x FROM t1 WHERE y=3) AS 'p',
sl@0
   286
      (SELECT x AS 'b' FROM t1 WHERE y=4) AS 'q'
sl@0
   287
    WHERE a=b
sl@0
   288
    ORDER BY a
sl@0
   289
  }
sl@0
   290
} {8 5 8 9 6 9 10 7 10}
sl@0
   291
do_test select6-5.2 {
sl@0
   292
  execsql {
sl@0
   293
    SELECT a,x,b FROM
sl@0
   294
      (SELECT x+3 AS 'a', x FROM t1 WHERE y=3),
sl@0
   295
      (SELECT x AS 'b' FROM t1 WHERE y=4)
sl@0
   296
    WHERE a=b
sl@0
   297
    ORDER BY a
sl@0
   298
  }
sl@0
   299
} {8 5 8 9 6 9 10 7 10}
sl@0
   300
sl@0
   301
# Tests of compound sub-selects
sl@0
   302
#
sl@0
   303
do_test select6-6.1 {
sl@0
   304
  execsql {
sl@0
   305
    DELETE FROM t1 WHERE x>4;
sl@0
   306
    SELECT * FROM t1
sl@0
   307
  }
sl@0
   308
} {1 1 2 2 3 2 4 3}
sl@0
   309
ifcapable compound {
sl@0
   310
  do_test select6-6.2 {
sl@0
   311
    execsql {
sl@0
   312
      SELECT * FROM (
sl@0
   313
        SELECT x AS 'a' FROM t1 UNION ALL SELECT x+10 AS 'a' FROM t1
sl@0
   314
      ) ORDER BY a;
sl@0
   315
    }
sl@0
   316
  } {1 2 3 4 11 12 13 14}
sl@0
   317
  do_test select6-6.3 {
sl@0
   318
    execsql {
sl@0
   319
      SELECT * FROM (
sl@0
   320
        SELECT x AS 'a' FROM t1 UNION ALL SELECT x+1 AS 'a' FROM t1
sl@0
   321
      ) ORDER BY a;
sl@0
   322
    }
sl@0
   323
  } {1 2 2 3 3 4 4 5}
sl@0
   324
  do_test select6-6.4 {
sl@0
   325
    execsql {
sl@0
   326
      SELECT * FROM (
sl@0
   327
        SELECT x AS 'a' FROM t1 UNION SELECT x+1 AS 'a' FROM t1
sl@0
   328
      ) ORDER BY a;
sl@0
   329
    }
sl@0
   330
  } {1 2 3 4 5}
sl@0
   331
  do_test select6-6.5 {
sl@0
   332
    execsql {
sl@0
   333
      SELECT * FROM (
sl@0
   334
        SELECT x AS 'a' FROM t1 INTERSECT SELECT x+1 AS 'a' FROM t1
sl@0
   335
      ) ORDER BY a;
sl@0
   336
    }
sl@0
   337
  } {2 3 4}
sl@0
   338
  do_test select6-6.6 {
sl@0
   339
    execsql {
sl@0
   340
      SELECT * FROM (
sl@0
   341
        SELECT x AS 'a' FROM t1 EXCEPT SELECT x*2 AS 'a' FROM t1
sl@0
   342
      ) ORDER BY a;
sl@0
   343
    }
sl@0
   344
  } {1 3}
sl@0
   345
} ;# ifcapable compound
sl@0
   346
sl@0
   347
# Subselects with no FROM clause
sl@0
   348
#
sl@0
   349
do_test select6-7.1 {
sl@0
   350
  execsql {
sl@0
   351
    SELECT * FROM (SELECT 1)
sl@0
   352
  }
sl@0
   353
} {1}
sl@0
   354
do_test select6-7.2 {
sl@0
   355
  execsql {
sl@0
   356
    SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c')
sl@0
   357
  }
sl@0
   358
} {abc 2 1 1 2 abc}
sl@0
   359
do_test select6-7.3 {
sl@0
   360
  execsql {
sl@0
   361
    SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 0)
sl@0
   362
  }
sl@0
   363
} {}
sl@0
   364
do_test select6-7.4 {
sl@0
   365
  execsql2 {
sl@0
   366
    SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 1)
sl@0
   367
  }
sl@0
   368
} {c abc b 2 a 1 a 1 b 2 c abc}
sl@0
   369
sl@0
   370
# The remaining tests in this file depend on the EXPLAIN keyword.
sl@0
   371
# Skip these tests if EXPLAIN is disabled in the current build.
sl@0
   372
#
sl@0
   373
ifcapable {!explain} {
sl@0
   374
  finish_test
sl@0
   375
  return
sl@0
   376
}
sl@0
   377
sl@0
   378
# The following procedure compiles the SQL given as an argument and returns
sl@0
   379
# TRUE if that SQL uses any transient tables and returns FALSE if no
sl@0
   380
# transient tables are used.  This is used to make sure that the
sl@0
   381
# sqliteFlattenSubquery() routine in select.c is doing its job.
sl@0
   382
#
sl@0
   383
proc is_flat {sql} {
sl@0
   384
  return [expr 0>[lsearch [execsql "EXPLAIN $sql"] OpenEphemeral]]
sl@0
   385
}
sl@0
   386
sl@0
   387
# Check that the flattener works correctly for deeply nested subqueries
sl@0
   388
# involving joins.
sl@0
   389
#
sl@0
   390
do_test select6-8.1 {
sl@0
   391
  execsql {
sl@0
   392
    BEGIN;
sl@0
   393
    CREATE TABLE t3(p,q);
sl@0
   394
    INSERT INTO t3 VALUES(1,11);
sl@0
   395
    INSERT INTO t3 VALUES(2,22);
sl@0
   396
    CREATE TABLE t4(q,r);
sl@0
   397
    INSERT INTO t4 VALUES(11,111);
sl@0
   398
    INSERT INTO t4 VALUES(22,222);
sl@0
   399
    COMMIT;
sl@0
   400
    SELECT * FROM t3 NATURAL JOIN t4;
sl@0
   401
  }
sl@0
   402
} {1 11 111 2 22 222}
sl@0
   403
do_test select6-8.2 {
sl@0
   404
  execsql {
sl@0
   405
    SELECT y, p, q, r FROM
sl@0
   406
       (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
sl@0
   407
       (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
sl@0
   408
    WHERE  y=p
sl@0
   409
  }
sl@0
   410
} {1 1 11 111 2 2 22 222 2 2 22 222}
sl@0
   411
# If view support is omitted from the build, then so is the query 
sl@0
   412
# "flattener". So omit this test and test select6-8.6 in that case.
sl@0
   413
ifcapable view {
sl@0
   414
do_test select6-8.3 {
sl@0
   415
  is_flat {
sl@0
   416
    SELECT y, p, q, r FROM
sl@0
   417
       (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
sl@0
   418
       (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
sl@0
   419
    WHERE  y=p
sl@0
   420
  }
sl@0
   421
} {1}
sl@0
   422
} ;# ifcapable view
sl@0
   423
do_test select6-8.4 {
sl@0
   424
  execsql {
sl@0
   425
    SELECT DISTINCT y, p, q, r FROM
sl@0
   426
       (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
sl@0
   427
       (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
sl@0
   428
    WHERE  y=p
sl@0
   429
  }
sl@0
   430
} {1 1 11 111 2 2 22 222}
sl@0
   431
do_test select6-8.5 {
sl@0
   432
  execsql {
sl@0
   433
    SELECT * FROM 
sl@0
   434
      (SELECT y, p, q, r FROM
sl@0
   435
         (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
sl@0
   436
         (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
sl@0
   437
      WHERE  y=p) AS e,
sl@0
   438
      (SELECT r AS z FROM t4 WHERE q=11) AS f
sl@0
   439
    WHERE e.r=f.z
sl@0
   440
  }
sl@0
   441
} {1 1 11 111 111}
sl@0
   442
ifcapable view {
sl@0
   443
do_test select6-8.6 {
sl@0
   444
  is_flat {
sl@0
   445
    SELECT * FROM 
sl@0
   446
      (SELECT y, p, q, r FROM
sl@0
   447
         (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
sl@0
   448
         (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
sl@0
   449
      WHERE  y=p) AS e,
sl@0
   450
      (SELECT r AS z FROM t4 WHERE q=11) AS f
sl@0
   451
    WHERE e.r=f.z
sl@0
   452
  }
sl@0
   453
} {1}
sl@0
   454
} ;# ifcapable view
sl@0
   455
sl@0
   456
# Ticket #1634
sl@0
   457
#
sl@0
   458
do_test select6-9.1 {
sl@0
   459
  execsql {
sl@0
   460
    SELECT a.x, b.x FROM t1 AS a, (SELECT x FROM t1 LIMIT 2) AS b
sl@0
   461
  }
sl@0
   462
} {1 1 1 2 2 1 2 2 3 1 3 2 4 1 4 2}
sl@0
   463
do_test select6-9.2 {
sl@0
   464
  execsql {
sl@0
   465
    SELECT x FROM (SELECT x FROM t1 LIMIT 2);
sl@0
   466
  }
sl@0
   467
} {1 2}
sl@0
   468
do_test select6-9.3 {
sl@0
   469
  execsql {
sl@0
   470
    SELECT x FROM (SELECT x FROM t1 LIMIT 2 OFFSET 1);
sl@0
   471
  }
sl@0
   472
} {2 3}
sl@0
   473
do_test select6-9.4 {
sl@0
   474
  execsql {
sl@0
   475
    SELECT x FROM (SELECT x FROM t1) LIMIT 2;
sl@0
   476
  }
sl@0
   477
} {1 2}
sl@0
   478
do_test select6-9.5 {
sl@0
   479
  execsql {
sl@0
   480
    SELECT x FROM (SELECT x FROM t1) LIMIT 2 OFFSET 1;
sl@0
   481
  }
sl@0
   482
} {2 3}
sl@0
   483
do_test select6-9.6 {
sl@0
   484
  execsql {
sl@0
   485
    SELECT x FROM (SELECT x FROM t1 LIMIT 2) LIMIT 3;
sl@0
   486
  }
sl@0
   487
} {1 2}
sl@0
   488
do_test select6-9.7 {
sl@0
   489
  execsql {
sl@0
   490
    SELECT x FROM (SELECT x FROM t1 LIMIT -1) LIMIT 3;
sl@0
   491
  }
sl@0
   492
} {1 2 3}
sl@0
   493
do_test select6-9.8 {
sl@0
   494
  execsql {
sl@0
   495
    SELECT x FROM (SELECT x FROM t1 LIMIT -1);
sl@0
   496
  }
sl@0
   497
} {1 2 3 4}
sl@0
   498
do_test select6-9.9 {
sl@0
   499
  execsql {
sl@0
   500
    SELECT x FROM (SELECT x FROM t1 LIMIT -1 OFFSET 1);
sl@0
   501
  }
sl@0
   502
} {2 3 4}
sl@0
   503
sl@0
   504
sl@0
   505
sl@0
   506
finish_test