xref: /sqlite-3.40.0/test/offset1.test (revision bffd5c1e)
19f1ef45fSdrh# 2015-10-06
29f1ef45fSdrh#
39f1ef45fSdrh# The author disclaims copyright to this source code.  In place of
49f1ef45fSdrh# a legal notice, here is a blessing:
59f1ef45fSdrh#
69f1ef45fSdrh#    May you do good and not evil.
79f1ef45fSdrh#    May you find forgiveness for yourself and forgive others.
89f1ef45fSdrh#    May you share freely, never taking more than you give.
99f1ef45fSdrh#
109f1ef45fSdrh#***********************************************************************
119f1ef45fSdrh#
129f1ef45fSdrh# This file implements test cases for the [b65cb2c8d91f6685841d7d1e13b6]
139f1ef45fSdrh# bug:  Correct handling of LIMIT and OFFSET on a UNION ALL query where
149f1ef45fSdrh# the right-hand SELECT contains an ORDER BY in a subquery.
159f1ef45fSdrh#
169f1ef45fSdrh
179f1ef45fSdrhset testdir [file dirname $argv0]
189f1ef45fSdrhsource $testdir/tester.tcl
199f1ef45fSdrh
209f1ef45fSdrhifcapable !compound {
219f1ef45fSdrh  finish_test
229f1ef45fSdrh  return
239f1ef45fSdrh}
249f1ef45fSdrh
259f1ef45fSdrhdo_execsql_test offset1-1.1 {
269f1ef45fSdrh  CREATE TABLE t1(a,b);
279f1ef45fSdrh  INSERT INTO t1 VALUES(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e');
289f1ef45fSdrh  CREATE TABLE t2(x,y);
299f1ef45fSdrh  INSERT INTO t2 VALUES(8,'y'),(9,'z'),(6,'w'),(7,'x');
309f1ef45fSdrh  SELECT count(*) FROM t1, t2;
319f1ef45fSdrh} {20}
329f1ef45fSdrh
339f1ef45fSdrhdo_execsql_test offset1-1.2.0 {
349f1ef45fSdrh  SELECT a, b FROM t1
359f1ef45fSdrh  UNION ALL
369f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
379f1ef45fSdrh  LIMIT 3 OFFSET 0;
389f1ef45fSdrh} {1 a 2 b 3 c}
399f1ef45fSdrhdo_execsql_test offset1-1.2.1 {
409f1ef45fSdrh  SELECT a, b FROM t1
419f1ef45fSdrh  UNION ALL
429f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
439f1ef45fSdrh  LIMIT 3 OFFSET 1;
449f1ef45fSdrh} {2 b 3 c 4 d}
459f1ef45fSdrhdo_execsql_test offset1-1.2.2 {
469f1ef45fSdrh  SELECT a, b FROM t1
479f1ef45fSdrh  UNION ALL
489f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
499f1ef45fSdrh  LIMIT 3 OFFSET 2;
509f1ef45fSdrh} {3 c 4 d 5 e}
519f1ef45fSdrhdo_execsql_test offset1-1.2.3 {
529f1ef45fSdrh  SELECT a, b FROM t1
539f1ef45fSdrh  UNION ALL
549f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
559f1ef45fSdrh  LIMIT 3 OFFSET 3;
569f1ef45fSdrh} {4 d 5 e 6 w}
579f1ef45fSdrhdo_execsql_test offset1-1.2.4 {
589f1ef45fSdrh  SELECT a, b FROM t1
599f1ef45fSdrh  UNION ALL
609f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
619f1ef45fSdrh  LIMIT 3 OFFSET 4;
629f1ef45fSdrh} {5 e 6 w 7 x}
639f1ef45fSdrhdo_execsql_test offset1-1.2.5 {
649f1ef45fSdrh  SELECT a, b FROM t1
659f1ef45fSdrh  UNION ALL
669f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
679f1ef45fSdrh  LIMIT 3 OFFSET 5;
689f1ef45fSdrh} {6 w 7 x 8 y}
699f1ef45fSdrhdo_execsql_test offset1-1.2.6 {
709f1ef45fSdrh  SELECT a, b FROM t1
719f1ef45fSdrh  UNION ALL
729f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
739f1ef45fSdrh  LIMIT 3 OFFSET 6;
749f1ef45fSdrh} {7 x 8 y 9 z}
759f1ef45fSdrhdo_execsql_test offset1-1.2.7 {
769f1ef45fSdrh  SELECT a, b FROM t1
779f1ef45fSdrh  UNION ALL
789f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
799f1ef45fSdrh  LIMIT 3 OFFSET 7;
809f1ef45fSdrh} {8 y 9 z}
819f1ef45fSdrhdo_execsql_test offset1-1.2.8 {
829f1ef45fSdrh  SELECT a, b FROM t1
839f1ef45fSdrh  UNION ALL
849f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
859f1ef45fSdrh  LIMIT 3 OFFSET 8;
869f1ef45fSdrh} {9 z}
879f1ef45fSdrhdo_execsql_test offset1-1.2.9 {
889f1ef45fSdrh  SELECT a, b FROM t1
899f1ef45fSdrh  UNION ALL
909f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
919f1ef45fSdrh  LIMIT 3 OFFSET 9;
929f1ef45fSdrh} {}
939f1ef45fSdrh
949f1ef45fSdrhdo_execsql_test offset1-1.3.0 {
959f1ef45fSdrh  SELECT * FROM t1 LIMIT 0;
969f1ef45fSdrh} {}
979f1ef45fSdrh
989f1ef45fSdrhdo_execsql_test offset1-1.4.0 {
999f1ef45fSdrh  SELECT a, b FROM t1
1009f1ef45fSdrh  UNION ALL
1019f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
1029f1ef45fSdrh  LIMIT 0 OFFSET 1;
1039f1ef45fSdrh} {}
1049f1ef45fSdrhdo_execsql_test offset1-1.4.1 {
1059f1ef45fSdrh  SELECT a, b FROM t1
1069f1ef45fSdrh  UNION ALL
1079f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
1089f1ef45fSdrh  LIMIT 1 OFFSET 1;
1099f1ef45fSdrh} {2 b}
1109f1ef45fSdrhdo_execsql_test offset1-1.4.2 {
1119f1ef45fSdrh  SELECT a, b FROM t1
1129f1ef45fSdrh  UNION ALL
1139f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
1149f1ef45fSdrh  LIMIT 2 OFFSET 1;
1159f1ef45fSdrh} {2 b 3 c}
1169f1ef45fSdrhdo_execsql_test offset1-1.4.3 {
1179f1ef45fSdrh  SELECT a, b FROM t1
1189f1ef45fSdrh  UNION ALL
1199f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
1209f1ef45fSdrh  LIMIT 3 OFFSET 1;
1219f1ef45fSdrh} {2 b 3 c 4 d}
1229f1ef45fSdrhdo_execsql_test offset1-1.4.4 {
1239f1ef45fSdrh  SELECT a, b FROM t1
1249f1ef45fSdrh  UNION ALL
1259f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
1269f1ef45fSdrh  LIMIT 4 OFFSET 1;
1279f1ef45fSdrh} {2 b 3 c 4 d 5 e}
1289f1ef45fSdrhdo_execsql_test offset1-1.4.5 {
1299f1ef45fSdrh  SELECT a, b FROM t1
1309f1ef45fSdrh  UNION ALL
1319f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
1329f1ef45fSdrh  LIMIT 5 OFFSET 1;
1339f1ef45fSdrh} {2 b 3 c 4 d 5 e 6 w}
1349f1ef45fSdrhdo_execsql_test offset1-1.4.6 {
1359f1ef45fSdrh  SELECT a, b FROM t1
1369f1ef45fSdrh  UNION ALL
1379f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
1389f1ef45fSdrh  LIMIT 6 OFFSET 1;
1399f1ef45fSdrh} {2 b 3 c 4 d 5 e 6 w 7 x}
1409f1ef45fSdrhdo_execsql_test offset1-1.4.7 {
1419f1ef45fSdrh  SELECT a, b FROM t1
1429f1ef45fSdrh  UNION ALL
1439f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
1449f1ef45fSdrh  LIMIT 7 OFFSET 1;
1459f1ef45fSdrh} {2 b 3 c 4 d 5 e 6 w 7 x 8 y}
1469f1ef45fSdrhdo_execsql_test offset1-1.4.8 {
1479f1ef45fSdrh  SELECT a, b FROM t1
1489f1ef45fSdrh  UNION ALL
1499f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
1509f1ef45fSdrh  LIMIT 8 OFFSET 1;
1519f1ef45fSdrh} {2 b 3 c 4 d 5 e 6 w 7 x 8 y 9 z}
1529f1ef45fSdrhdo_execsql_test offset1-1.4.9 {
1539f1ef45fSdrh  SELECT a, b FROM t1
1549f1ef45fSdrh  UNION ALL
1559f1ef45fSdrh  SELECT * FROM (SELECT x, y FROM t2 ORDER BY y)
1569f1ef45fSdrh  LIMIT 9 OFFSET 1;
1579f1ef45fSdrh} {2 b 3 c 4 d 5 e 6 w 7 x 8 y 9 z}
1589f1ef45fSdrh
159*bffd5c1eSdrh# 2022-08-04
160*bffd5c1eSdrh# https://sqlite.org/forum/forumpost/6b5e9188f0657616
161*bffd5c1eSdrh#
162*bffd5c1eSdrhdo_execsql_test offset1-2.0 {
163*bffd5c1eSdrh  CREATE TABLE employees (
164*bffd5c1eSdrh    id integer primary key,
165*bffd5c1eSdrh    name text,
166*bffd5c1eSdrh    city text,
167*bffd5c1eSdrh    department text,
168*bffd5c1eSdrh    salary integer
169*bffd5c1eSdrh  );
170*bffd5c1eSdrh  INSERT INTO employees VALUES
171*bffd5c1eSdrh    (11,'Diane','London','hr',70),
172*bffd5c1eSdrh    (12,'Bob','London','hr',78),
173*bffd5c1eSdrh    (21,'Emma','London','it',84),
174*bffd5c1eSdrh    (22,'Grace','Berlin','it',90),
175*bffd5c1eSdrh    (23,'Henry','London','it',104),
176*bffd5c1eSdrh    (24,'Irene','Berlin','it',104),
177*bffd5c1eSdrh    (25,'Frank','Berlin','it',120),
178*bffd5c1eSdrh    (31,'Cindy','Berlin','sales',96),
179*bffd5c1eSdrh    (32,'Dave','London','sales',96),
180*bffd5c1eSdrh    (33,'Alice','Berlin','sales',100);
181*bffd5c1eSdrh  CREATE VIEW v AS
182*bffd5c1eSdrh    SELECT * FROM (
183*bffd5c1eSdrh      SELECT * FROM employees
184*bffd5c1eSdrh       WHERE salary < 100
185*bffd5c1eSdrh       ORDER BY salary desc)
186*bffd5c1eSdrh     UNION ALL
187*bffd5c1eSdrh     SELECT * FROM (
188*bffd5c1eSdrh       SELECT * FROM employees
189*bffd5c1eSdrh       WHERE salary >= 100
190*bffd5c1eSdrh       ORDER BY salary asc);
191*bffd5c1eSdrh} {}
192*bffd5c1eSdrhdo_execsql_test offset1-2.1 {
193*bffd5c1eSdrh  SELECT * FROM v LIMIT 5 OFFSET 2;
194*bffd5c1eSdrh} {
195*bffd5c1eSdrh  22  Grace  Berlin  it      90
196*bffd5c1eSdrh  21  Emma   London  it      84
197*bffd5c1eSdrh  12  Bob    London  hr      78
198*bffd5c1eSdrh  11  Diane  London  hr      70
199*bffd5c1eSdrh  33  Alice  Berlin  sales   100
200*bffd5c1eSdrh}
2019f1ef45fSdrh
2029f1ef45fSdrhfinish_test
203