Skip to content

Commit

Permalink
Fix the table name aliasing on INSERT so that it occurs before the co…
Browse files Browse the repository at this point in the history
…lumn list

rather than afterwards, just as it does for PostgreSQL.  Add table name
aliasing to UPDATE and DELETE.

FossilOrigin-Name: 861a2e2a4895f96a5d8e1730e744983b2ac4311d0c2cf201c0e59f409030d5d7
  • Loading branch information
drh committed Apr 19, 2018
1 parent dedff6b commit 5e3a6eb
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 30 deletions.
23 changes: 11 additions & 12 deletions manifest
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
C Add\sthe\s"sorter-reference"\soptimization,\sallowing\sSQLite\sto\sbe\sconfigured\sso\nthat\ssome\srequired\svalues\smay\sbe\sloaded\sfrom\sthe\sdatabase\safter\sexternal\nsorting\soccurs\sfor\sSELECT\sstatements\swith\sORDER\sBY\sclauses\sthat\sare\snot\nsatisfied\sby\sdatabase\sindexes.
D 2018-04-18T19:56:14.234
C Fix\sthe\stable\sname\saliasing\son\sINSERT\sso\sthat\sit\soccurs\sbefore\sthe\scolumn\slist\nrather\sthan\safterwards,\sjust\sas\sit\sdoes\sfor\sPostgreSQL.\s\sAdd\stable\sname\naliasing\sto\sUPDATE\sand\sDELETE.
D 2018-04-19T11:45:16.962
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F Makefile.in 5ce9343cba9c189046f1afe6d2bcc1f68079439febc05267b98aec6ecc752439
Expand Down Expand Up @@ -480,7 +480,7 @@ F src/os_win.c eb03c6d52f893bcd7fdd4c6006674c13c1b5e49543fec98d605201af2997171c
F src/os_win.h 7b073010f1451abe501be30d12f6bc599824944a
F src/pager.c 1bb6a57fa0465296a4d6109a1a64610a0e7adde1f3acf3ef539a9d972908ce8f
F src/pager.h c571b064df842ec8f2e90855dead9acf4cbe0d1b2c05afe0ef0d0145f7fd0388
F src/parse.y 5419576595156433738781705c6a5a6534a470c763a6d36b8fb4b7d5ea6cae99
F src/parse.y 1095d04ac7bc6d2cc69167daf6969c1276970c5d14578e259561d2a5477312f3
F src/pcache.c 135ef0bc6fb2e3b7178d49ab5c9176254c8a691832c1bceb1156b2fbdd0869bd
F src/pcache.h 072f94d29281cffd99e46c1539849f248c4b56ae7684c1f36626797fee375170
F src/pcache1.c 716975564c15eb6679e97f734cec1bfd6c16ac3d4010f05f1f8e509fc7d19880
Expand Down Expand Up @@ -747,7 +747,7 @@ F test/dbpage.test dbf50a4d361f9e45a979432c727506065113124478a7d2db12074fa655e65
F test/dbstatus.test c15fa97f743dac7ce996814c84b56317e138895ee15ce27f15b608aa6924c90a
F test/dbstatus2.test f5fe0afed3fa45e57cfa70d1147606c20d2ba23feac78e9a172f2fe8ab5b78ef
F test/default.test 0cb49b1c315a0d81c81d775e407f66906a2a604d
F test/delete.test acc38fca8ee4851467705b1c2cfea64cd26667e5
F test/delete.test 31832b0c45ecb51a54348c68db173be462985901e6ed7f403d6d7a8f70ab4ef0
F test/delete2.test 3a03f2cca1f9a67ec469915cb8babd6485db43fa
F test/delete3.test 555e84a00a99230b7d049d477a324a631126a6ab
F test/delete4.test 21d2113217eeaacac2d99defe14fe6611615ae86
Expand Down Expand Up @@ -1506,11 +1506,11 @@ F test/unique.test 93f8b2ef5ea51b9495f8d6493429b1fd0f465264
F test/unique2.test 3674e9f2a3f1fbbfd4772ac74b7a97090d0f77d2
F test/unixexcl.test d936ba2b06794018e136418addd59a2354eeae97
F test/unordered.test ca7adce0419e4ca0c50f039885e76ed2c531eda8
F test/update.test 6c68446b8a0a33d522a7c72b320934596a2d7d32
F test/update.test 17f93464d311f4b9e396be9a26f70372b380f7af8b0df309d0c2f03ea206933c
F test/update2.test 5e67667e1c54017d964e626db765cf8bedcf87483c184f4c575bdb8c1dd2313e
F test/upsert1.test 3b4e8e5932516115bfffb2269a44c416c5c26d0d57cc7dd16954d0b77fbc4cd9
F test/upsert2.test 82ed5cbd4a13ff74240ece8ec97b6afd07446eac554ad5e93c12b42197844e0b
F test/upsert3.test 46ee74a7d7ee797ca0ab79d44e78572d6778b7520e4ebb8f8c1553765b4eae2c
F test/upsert2.test 9c3cdbb1a890227f6504ce4b0e3de68f4cdfa16bb21d8641208a9239896c5a09
F test/upsert3.test 88d7d590a1948a9cb6eac1b54b0642f67a9f35a1fc0f19b200e97d5d39e3179c
F test/upsert4.test 83b37a92f315217c9319f320966c044ddccf8bc525501fa09e2ee47779e3920b
F test/uri.test 3481026f00ade6dfe8adb7acb6e1e47b04369568
F test/uri2.test 9d3ba7a53ee167572d53a298ee4a5d38ec4a8fb7
Expand Down Expand Up @@ -1723,8 +1723,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 0cb83c84d10b89ef7a5504862566a609307c63e7571dd711d8b9f995d29e5a8c 413015c029d850d4ce7e66be1f59b57f291254240a958856378a62f5ac4a5092
R 4678faec743cd6b53d517fb16a4da20a
T +closed 413015c029d850d4ce7e66be1f59b57f291254240a958856378a62f5ac4a5092
U dan
Z 97f82718fcc75c5ef29e50fdc72006e4
P ef74090a40ceaef2fd93a7613ec99a191ce986811c852e96f4a19719f18af4f0
R b8ecc539094b35008277eb5c4de80d78
U drh
Z 8fcf02099940e8c7360c9b4125c9a140
2 changes: 1 addition & 1 deletion manifest.uuid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ef74090a40ceaef2fd93a7613ec99a191ce986811c852e96f4a19719f18af4f0
861a2e2a4895f96a5d8e1730e744983b2ac4311d0c2cf201c0e59f409030d5d7
35 changes: 21 additions & 14 deletions src/parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,21 @@ fullname(A) ::= nm(X).
fullname(A) ::= nm(X) DOT nm(Y).
{A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/}

%type xfullname {SrcList*}
%destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);}
xfullname(A) ::= nm(X).
{A = sqlite3SrcListAppend(pParse->db,0,&X,0); /*A-overwrites-X*/}
xfullname(A) ::= nm(X) DOT nm(Y).
{A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/}
xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). {
A = sqlite3SrcListAppend(pParse->db,0,&X,&Y); /*A-overwrites-X*/
if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
}
xfullname(A) ::= nm(X) AS nm(Z). {
A = sqlite3SrcListAppend(pParse->db,0,&X,0); /*A-overwrites-X*/
if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z);
}

%type joinop {int}
joinop(X) ::= COMMA|JOIN. { X = JT_INNER; }
joinop(X) ::= JOIN_KW(A) JOIN.
Expand Down Expand Up @@ -800,14 +815,14 @@ limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y).
/////////////////////////// The DELETE statement /////////////////////////////
//
%ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
cmd ::= with DELETE FROM fullname(X) indexed_opt(I) where_opt(W)
cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W)
orderby_opt(O) limit_opt(L). {
sqlite3SrcListIndexedBy(pParse, X, &I);
sqlite3DeleteFrom(pParse,X,W,O,L);
}
%endif
%ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
cmd ::= with DELETE FROM fullname(X) indexed_opt(I) where_opt(W). {
cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt(W). {
sqlite3SrcListIndexedBy(pParse, X, &I);
sqlite3DeleteFrom(pParse,X,W,0,0);
}
Expand All @@ -822,15 +837,15 @@ where_opt(A) ::= WHERE expr(X). {A = X;}
////////////////////////// The UPDATE command ////////////////////////////////
//
%ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
cmd ::= with UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y)
where_opt(W) orderby_opt(O) limit_opt(L). {
sqlite3SrcListIndexedBy(pParse, X, &I);
sqlite3ExprListCheckLength(pParse,Y,"set list");
sqlite3Update(pParse,X,Y,W,R,O,L,0);
}
%endif
%ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
cmd ::= with UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y)
cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y)
where_opt(W). {
sqlite3SrcListIndexedBy(pParse, X, &I);
sqlite3ExprListCheckLength(pParse,Y,"set list");
Expand Down Expand Up @@ -858,19 +873,11 @@ setlist(A) ::= LP idlist(X) RP EQ expr(Y). {

////////////////////////// The INSERT command /////////////////////////////////
//
cmd ::= with insert_cmd(R) INTO fullname(X) idlist_opt(F) select(S)
upsert(U). {
sqlite3Insert(pParse, X, S, F, R, U);
}
cmd ::= with insert_cmd(R) INTO fullname(X) idlist_opt(F) AS nm(A) select(S)
cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S)
upsert(U). {
/* X could only be NULL following a OOM, but an OOM would have been detected
** and stopped the parse long before this rule reduces. */
assert( X!=0 );
X->a[0].zAlias = sqlite3NameFromToken(pParse->db, &A);
sqlite3Insert(pParse, X, S, F, R, U);
}
cmd ::= with insert_cmd(R) INTO fullname(X) idlist_opt(F) DEFAULT VALUES.
cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES.
{
sqlite3Insert(pParse, X, 0, F, R, 0);
}
Expand Down
12 changes: 12 additions & 0 deletions test/delete.test
Original file line number Diff line number Diff line change
Expand Up @@ -403,5 +403,17 @@ do_execsql_test delete-10.2 {
SELECT * FROM t1 WHERE a='1' AND b='2';
}

do_execsql_test delete-11.0 {
CREATE TABLE t11(a INTEGER PRIMARY KEY, b INT);
WITH RECURSIVE cnt(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM cnt WHERE x<20)
INSERT INTO t11(a,b) SELECT x, (x*17)%100 FROM cnt;
SELECT * FROM t11;
} {1 17 2 34 3 51 4 68 5 85 6 2 7 19 8 36 9 53 10 70 11 87 12 4 13 21 14 38 15 55 16 72 17 89 18 6 19 23 20 40}
do_execsql_test delete-11.1 {
DELETE FROM t11 AS xyz
WHERE EXISTS(SELECT 1 FROM t11 WHERE t11.a>xyz.a AND t11.b<=xyz.b);
SELECT * FROM t11;
} {6 2 12 4 18 6 19 23 20 40}


finish_test
12 changes: 12 additions & 0 deletions test/update.test
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,18 @@ ifcapable subquery {
SELECT a,e FROM t1;
}
} {1 15 2 8}
do_test update-11.3 {
execsql {
UPDATE t1 AS xyz SET e=e+1 WHERE xyz.a IN (SELECT a FROM t1);
SELECT a,e FROM t1;
}
} {1 16 2 9}
do_test update-11.4 {
execsql {
UPDATE t1 AS xyz SET e=e+1 WHERE EXISTS(SELECT 1 FROM t1 WHERE t1.a<xyz.a);
SELECT a,e FROM t1;
}
} {1 16 2 10}
}

integrity_check update-12.1
Expand Down
4 changes: 2 additions & 2 deletions test/upsert2.test
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ do_execsql_test upsert2-201 {
DELETE FROM t1;
INSERT INTO t1(a,b) VALUES(1,2),(3,4);
WITH nx(a,b) AS (VALUES(1,8),(2,11),(3,1),(2,15),(1,4),(1,99))
INSERT INTO t1(a,b) AS t2 SELECT a, b FROM nx WHERE true
INSERT INTO main.t1 AS t2(a,b) SELECT a, b FROM nx WHERE true
ON CONFLICT(a) DO UPDATE SET b=excluded.b, c=t2.c+1 WHERE t2.b<excluded.b;
SELECT *, 'x' FROM t1 ORDER BY a;
} {1 99 2 x 2 15 1 x 3 4 0 x}
do_catchsql_test upsert2-202 {
WITH nx(a,b) AS (VALUES(1,8),(2,11),(3,1),(2,15),(1,4),(1,99))
INSERT INTO t1(a,b) AS t2 SELECT a, b FROM nx WHERE true
INSERT INTO t1 AS t2(a,b) SELECT a, b FROM nx WHERE true
ON CONFLICT(a) DO UPDATE SET b=excluded.b, c=t1.c+1 WHERE t1.b<excluded.b;
} {1 {no such column: t1.c}}
do_execsql_test upsert2-210 {
Expand Down
2 changes: 1 addition & 1 deletion test/upsert3.test
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ do_execsql_test upsert3-200 {
SELECT *, 'x' FROM excluded ORDER BY a;
} {1 2 2 x 3 4 1 x 5 6 0 x}
do_execsql_test upsert3-210 {
INSERT INTO excluded(a,b,c) AS base VALUES(1,2,8),(1,2,3)
INSERT INTO excluded AS base(a,b,c) VALUES(1,2,8),(1,2,3)
ON CONFLICT(b,a) DO UPDATE SET c=excluded.c+1 WHERE base.c<excluded.c;
SELECT *, 'x' FROM excluded ORDER BY a;
} {1 2 9 x 3 4 1 x 5 6 0 x}
Expand Down

0 comments on commit 5e3a6eb

Please sign in to comment.