xref: /sqlite-3.40.0/test/upfrom1.test (revision ec2f689b)
169887c99Sdan# 2020 April 22
269887c99Sdan#
369887c99Sdan# The author disclaims copyright to this source code.  In place of
469887c99Sdan# a legal notice, here is a blessing:
569887c99Sdan#
669887c99Sdan#    May you do good and not evil.
769887c99Sdan#    May you find forgiveness for yourself and forgive others.
869887c99Sdan#    May you share freely, never taking more than you give.
969887c99Sdan#
1069887c99Sdan#***********************************************************************
1169887c99Sdan# This file implements regression tests for SQLite library.
1269887c99Sdan#
1369887c99Sdan
1469887c99Sdan####################################################
1569887c99Sdan# DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED!
1669887c99Sdan####################################################
1769887c99Sdan
1869887c99Sdanset testdir [file dirname $argv0]
1969887c99Sdansource $testdir/tester.tcl
2069887c99Sdanset testprefix upfrom1
2169887c99Sdan
2269887c99Sdando_execsql_test 1.1.0 {
2369887c99Sdan  DROP TABLE IF EXISTS t2;
2469887c99Sdan  CREATE TABLE t2(a INTEGER PRIMARY KEY, b INTEGER, c INTEGER) WITHOUT ROWID;
2569887c99Sdan  INSERT INTO t2 VALUES(1, 2, 3);
2669887c99Sdan  INSERT INTO t2 VALUES(4, 5, 6);
2769887c99Sdan  INSERT INTO t2 VALUES(7, 8, 9);
2869887c99Sdan
2969887c99Sdan  DROP TABLE IF EXISTS chng;
3069887c99Sdan  CREATE TABLE chng(a INTEGER, b INTEGER, c INTEGER);
3169887c99Sdan  INSERT INTO chng VALUES(1, 100, 1000);
3269887c99Sdan  INSERT INTO chng VALUES(7, 700, 7000);
3369887c99Sdan} {}
3469887c99Sdan
3569887c99Sdando_execsql_test 1.1.1 {
3669887c99Sdan  SELECT * FROM t2;
3769887c99Sdan} {1 2 3   4 5 6   7 8 9}
3869887c99Sdan
3969887c99Sdando_execsql_test 1.1.2 {
4069887c99Sdan  UPDATE t2 SET b = chng.b, c = chng.c FROM chng WHERE chng.a = t2.a;
4169887c99Sdan  SELECT * FROM t2 ORDER BY a;
4269887c99Sdan} {1 100 1000   4 5 6   7 700 7000}
4369887c99Sdan
4469887c99Sdando_execsql_test 1.1.3 {
4569887c99Sdan  DELETE FROM t2;
4669887c99Sdan  INSERT INTO t2 VALUES(1, 2, 3);
4769887c99Sdan  INSERT INTO t2 VALUES(4, 5, 6);
4869887c99Sdan  INSERT INTO t2 VALUES(7, 8, 9);
4969887c99Sdan} {}
5069887c99Sdan
5169887c99Sdando_execsql_test 1.1.4 {
5269887c99Sdan  UPDATE t2 SET (b, c) = (SELECT b, c FROM chng WHERE a=t2.a)
5369887c99Sdan    WHERE a IN (SELECT a FROM chng);
5469887c99Sdan  SELECT * FROM t2 ORDER BY a;
5569887c99Sdan} {1 100 1000   4 5 6   7 700 7000}
5669887c99Sdan
5769887c99Sdando_execsql_test 1.1.5 {
5869887c99Sdan  DROP TABLE IF EXISTS t3;
5969887c99Sdan  CREATE TABLE t3(a INTEGER PRIMARY KEY, b INTEGER, c TEXT) WITHOUT ROWID;
6069887c99Sdan  INSERT INTO t3 VALUES(1, 1, 'one');
6169887c99Sdan  INSERT INTO t3 VALUES(2, 2, 'two');
6269887c99Sdan  INSERT INTO t3 VALUES(3, 3, 'three');
6369887c99Sdan
6469887c99Sdan  DROP TABLE IF EXISTS t4;
6569887c99Sdan  CREATE TABLE t4(x TEXT);
6669887c99Sdan  INSERT INTO t4 VALUES('five');
6769887c99Sdan
6869887c99Sdan  SELECT * FROM t3 ORDER BY a;
6969887c99Sdan} {1 1 one   2 2 two   3 3 three}
7069887c99Sdan
7169887c99Sdando_execsql_test 1.1.6 {
7269887c99Sdan  UPDATE t3 SET c=x FROM t4;
7369887c99Sdan  SELECT * FROM t3 ORDER BY a;
7469887c99Sdan} {1 1 five   2 2 five   3 3 five}
7569887c99Sdan
7669887c99Sdando_execsql_test 1.2.0 {
7769887c99Sdan  DROP TABLE IF EXISTS t2;
7869887c99Sdan  CREATE TABLE t2(a INTEGER PRIMARY KEY, b INTEGER, c INTEGER) ;
7969887c99Sdan  INSERT INTO t2 VALUES(1, 2, 3);
8069887c99Sdan  INSERT INTO t2 VALUES(4, 5, 6);
8169887c99Sdan  INSERT INTO t2 VALUES(7, 8, 9);
8269887c99Sdan
8369887c99Sdan  DROP TABLE IF EXISTS chng;
8469887c99Sdan  CREATE TABLE chng(a INTEGER, b INTEGER, c INTEGER);
8569887c99Sdan  INSERT INTO chng VALUES(1, 100, 1000);
8669887c99Sdan  INSERT INTO chng VALUES(7, 700, 7000);
8769887c99Sdan} {}
8869887c99Sdan
8969887c99Sdando_execsql_test 1.2.1 {
9069887c99Sdan  SELECT * FROM t2;
9169887c99Sdan} {1 2 3   4 5 6   7 8 9}
9269887c99Sdan
9369887c99Sdando_execsql_test 1.2.2 {
9469887c99Sdan  UPDATE t2 SET b = chng.b, c = chng.c FROM chng WHERE chng.a = t2.a;
9569887c99Sdan  SELECT * FROM t2 ORDER BY a;
9669887c99Sdan} {1 100 1000   4 5 6   7 700 7000}
9769887c99Sdan
9869887c99Sdando_execsql_test 1.2.3 {
9969887c99Sdan  DELETE FROM t2;
10069887c99Sdan  INSERT INTO t2 VALUES(1, 2, 3);
10169887c99Sdan  INSERT INTO t2 VALUES(4, 5, 6);
10269887c99Sdan  INSERT INTO t2 VALUES(7, 8, 9);
10369887c99Sdan} {}
10469887c99Sdan
10569887c99Sdando_execsql_test 1.2.4 {
10669887c99Sdan  UPDATE t2 SET (b, c) = (SELECT b, c FROM chng WHERE a=t2.a)
10769887c99Sdan    WHERE a IN (SELECT a FROM chng);
10869887c99Sdan  SELECT * FROM t2 ORDER BY a;
10969887c99Sdan} {1 100 1000   4 5 6   7 700 7000}
11069887c99Sdan
11169887c99Sdando_execsql_test 1.2.5 {
11269887c99Sdan  DROP TABLE IF EXISTS t3;
11369887c99Sdan  CREATE TABLE t3(a INTEGER PRIMARY KEY, b INTEGER, c TEXT) ;
11469887c99Sdan  INSERT INTO t3 VALUES(1, 1, 'one');
11569887c99Sdan  INSERT INTO t3 VALUES(2, 2, 'two');
11669887c99Sdan  INSERT INTO t3 VALUES(3, 3, 'three');
11769887c99Sdan
11869887c99Sdan  DROP TABLE IF EXISTS t4;
11969887c99Sdan  CREATE TABLE t4(x TEXT);
12069887c99Sdan  INSERT INTO t4 VALUES('five');
12169887c99Sdan
12269887c99Sdan  SELECT * FROM t3 ORDER BY a;
12369887c99Sdan} {1 1 one   2 2 two   3 3 three}
12469887c99Sdan
12569887c99Sdando_execsql_test 1.2.6 {
12669887c99Sdan  UPDATE t3 SET c=x FROM t4;
12769887c99Sdan  SELECT * FROM t3 ORDER BY a;
12869887c99Sdan} {1 1 five   2 2 five   3 3 five}
12969887c99Sdan
130f067ae8eSdando_execsql_test 2.1 {
131f067ae8eSdan  DROP TABLE IF EXISTS t5;
132f067ae8eSdan  DROP TABLE IF EXISTS m1;
133f067ae8eSdan  DROP TABLE IF EXISTS m2;
134f067ae8eSdan  CREATE TABLE t5(a INTEGER PRIMARY KEY, b TEXT, c TEXT);
135f067ae8eSdan  CREATE TABLE m1(x INTEGER PRIMARY KEY, y TEXT);
136f067ae8eSdan  CREATE TABLE m2(u INTEGER PRIMARY KEY, v TEXT);
137f067ae8eSdan
138f067ae8eSdan  INSERT INTO t5 VALUES(1, 'one', 'ONE');
139f067ae8eSdan  INSERT INTO t5 VALUES(2, 'two', 'TWO');
140f067ae8eSdan  INSERT INTO t5 VALUES(3, 'three', 'THREE');
141f067ae8eSdan  INSERT INTO t5 VALUES(4, 'four', 'FOUR');
142f067ae8eSdan
143f067ae8eSdan  INSERT INTO m1 VALUES(1, 'i');
144f067ae8eSdan  INSERT INTO m1 VALUES(2, 'ii');
145f067ae8eSdan  INSERT INTO m1 VALUES(3, 'iii');
146f067ae8eSdan
147f067ae8eSdan  INSERT INTO m2 VALUES(1, 'I');
148f067ae8eSdan  INSERT INTO m2 VALUES(3, 'II');
149f067ae8eSdan  INSERT INTO m2 VALUES(4, 'III');
150f067ae8eSdan} {}
151f067ae8eSdan
152f067ae8eSdando_execsql_test 2.2 {
153f067ae8eSdan  UPDATE t5 SET b=y, c=v FROM m1 LEFT JOIN m2 ON (x=u) WHERE x=a;
154f067ae8eSdan  SELECT * FROM t5 ORDER BY a;
155f067ae8eSdan} {1 i I   2 ii {}   3 iii II   4 four FOUR}
156f067ae8eSdan
15707ca7d61Sdan# PG says ERROR:  table name "t5" specified more than once
15807ca7d61Sdando_test 2.3.1 { catch { execsql {
15907ca7d61Sdan  UPDATE t5 SET b=1 FROM t5;
16007ca7d61Sdan} } } 1
16107ca7d61Sdan
16207ca7d61Sdan# PG says ERROR:  table name "apples" specified more than once
16307ca7d61Sdando_test 2.3.2 { catch { execsql {
16407ca7d61Sdan  UPDATE t5 AS apples SET b=1 FROM t5 AS apples;
16507ca7d61Sdan} } } 1
16607ca7d61Sdan
16709cf5692Sdrh# Problem found by OSSFuzz on 2020-07-20
16809cf5692Sdrh# https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=24282
16909cf5692Sdrh#
17009cf5692Sdrhreset_db
17109cf5692Sdrhdo_execsql_test 3.1 {
17209cf5692Sdrh  CREATE TABLE t0(a);
17309cf5692Sdrh  CREATE TABLE t1(b);
17409cf5692Sdrh  UPDATE t1 SET b=sum(a) FROM t0;
17509cf5692Sdrh  SELECT * FROM t0, t1;
17609cf5692Sdrh} {}
17709cf5692Sdrh
178cd1499f4Sdrh# Problem described by forum post https://sqlite.org/forum/forumpost/a274248080
179cd1499f4Sdrh#
180cd1499f4Sdrhreset_db
181cd1499f4Sdrhdo_execsql_test 4.1 {
182cd1499f4Sdrh  CREATE TABLE t1(x INT);  INSERT INTO t1 VALUES(1);
183cd1499f4Sdrh  CREATE TABLE t2(y INT);  INSERT INTO t2 VALUES(2);
184cd1499f4Sdrh  WITH t1 AS (SELECT y+100 AS x FROM t2)
185cd1499f4Sdrh    UPDATE t1 SET x=(SELECT x FROM t1);
186cd1499f4Sdrh  SELECT x, y FROM t1, t2;
187cd1499f4Sdrh} {102 2}
188cd1499f4Sdrhdo_execsql_test 4.2 {
189cd1499f4Sdrh  WITH t1 AS (SELECT y+100 AS x FROM t2)
190cd1499f4Sdrh    UPDATE t1 SET x=x+y FROM t2;
191cd1499f4Sdrh  SELECT x, y FROM t1, t2;
192cd1499f4Sdrh} {104 2}
193cd1499f4Sdrh
194*ec2f689bSdrh# 2021-05-20
195*ec2f689bSdrh# Forum https://sqlite.org/forum/forumpost/339f487de5 by Yu Liang
196*ec2f689bSdrh# A bad assert()
197*ec2f689bSdrh#
198*ec2f689bSdrhreset_db
199*ec2f689bSdrhdo_execsql_test 5.1 {
200*ec2f689bSdrh  CREATE TABLE t1(a);
201*ec2f689bSdrh  INSERT INTO t1(a) VALUES(5);
202*ec2f689bSdrh  CREATE VIEW t2 AS SELECT a FROM t1 UNION ALL SELECT a FROM t1;
203*ec2f689bSdrh  CREATE TABLE t3(b,c);
204*ec2f689bSdrh  INSERT INTO t3(b,c) VALUES(1,2);
205*ec2f689bSdrh  UPDATE t3 SET (c,b) = (SELECT 3,4) FROM t1, t2;
206*ec2f689bSdrh  SELECT * FROM t3;
207*ec2f689bSdrh} {4 3}
208*ec2f689bSdrh
209*ec2f689bSdrh
21069887c99Sdanfinish_test
211