]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
RIGHT JOIN USING now appears to work the same as PG-14. Legacy tests pass.
authordrh <>
Sat, 16 Apr 2022 12:40:52 +0000 (12:40 +0000)
committerdrh <>
Sat, 16 Apr 2022 12:40:52 +0000 (12:40 +0000)
FossilOrigin-Name: 5bfb862419541de955dae35fc91f6e9fc70e2744bb786d49fa26bede80b9091d

manifest
manifest.uuid
src/resolve.c
src/select.c
test/join9.test [new file with mode: 0644]

index fed1873bb694734e3da1c3e612c8dd6cc2cee5bf..a8f3238cc574ccb5cae01492ff9ad17857892ff2 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Revamp\sthe\sUSING\sclause\sprocessing\sso\sthat\sit\sworks\s(mostly)\swith\sRIGHT\nand\sFULL\sJOIN.
-D 2022-04-15T19:53:35.253
+C RIGHT\sJOIN\sUSING\snow\sappears\sto\swork\sthe\ssame\sas\sPG-14.\s\sLegacy\stests\spass.
+D 2022-04-16T12:40:52.845
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -550,9 +550,9 @@ F src/pragma.h e690a356c18e98414d2e870ea791c1be1545a714ba623719deb63f7f226d8bb7
 F src/prepare.c fd940149c691684e7c1073c3787a7170e44852b02d1275d2e30a5b58e89cfcaf
 F src/printf.c 05d8dfd2018bc4fc3ddb8b37eb97ccef7abf985643fa1caebdcf2916ca90fa32
 F src/random.c 097dc8b31b8fba5a9aca1697aeb9fd82078ec91be734c16bffda620ced7ab83c
-F src/resolve.c ecd1623bf1737c755eedea91761f715c7fd7fb9fe75f8852a59c69ed7eabe23d
+F src/resolve.c ed18f81f424c473e37b6ab7dcd3d0a0e2198590a4342d4f2b2a999e94ff0c97b
 F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92
-F src/select.c d36cf25d63f1a2f2dd342cf4ade0d8b14e9ade92b3617627ecc7609d3cf7b624
+F src/select.c d273657c2958eb5eee34d68920796b6e0214034dd3a2a88dc8e6dc92a4804271
 F src/shell.c.in eb7f10d5e2c47bd014d92ec5db1def21fcc1ed56ffaaa4ee715b6c37c370b47f
 F src/sqlite.h.in 2a35f62185eb5e7ecc64a2f68442b538ce9be74f80f28a00abc24837edcf1c17
 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
@@ -1148,6 +1148,7 @@ F test/join5.test c4df54e2e204d7f1417bfbdd21ca324b4b07415c647595cc47798eacfddc96
 F test/join6.test f809c025fa253f9e150c0e9afd4cef8813257bceeb6f46e04041228c9403cc2c
 F test/join7.test e5c9b1b729d7e1d0b4195e99833e0ff0cf2d88e7fdd32b49af1044f4c76f72d9
 F test/join8.test 9731b16dbb4309a980ba4adcb4ec31277626230300f819296ffc753c564b2011
+F test/join9.test ab637b0983877c802b02c7cba71c8fc849bcff55edb819bea31ad8aade3e7dbc
 F test/journal1.test c7b768041b7f494471531e17abc2f4f5ebf9e5096984f43ed17c4eb80ba34497
 F test/journal2.test 9dac6b4ba0ca79c3b21446bbae993a462c2397c4
 F test/journal3.test 7c3cf23ffc77db06601c1fcfc9743de8441cb77db9d1aa931863d94f5ffa140e
@@ -1947,9 +1948,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 8378e1e0d289627fb294ccd3f5865ef49df3a42b8a5aa211e21be1b42d9da753 b8e717663957dfd84e580bf21bc8e8881b33fdb94a2cc0c664a230e54d5e5076
-R 1c50cd468ee936498b2b4c9dc93da894
-T +closed b8e717663957dfd84e580bf21bc8e8881b33fdb94a2cc0c664a230e54d5e5076
+P 719c8d920b435b0f234d47467a2abe2941ab7d55ecca886e211cfce577ae648c
+R c3fdd17a4836973d7440bcccabeca593
 U drh
-Z e84867876e1dda583c8d47e448336a7d
+Z da7116bcca24d6303c79a8dd758b8403
 # Remove this line to create a well-formed Fossil manifest.
index 2a8e66f8f7046c373ea7c3f25610fd3e772e318c..a977f88626a91d08f1d7a86749631ca4cf9e6100 100644 (file)
@@ -1 +1 @@
-719c8d920b435b0f234d47467a2abe2941ab7d55ecca886e211cfce577ae648c
\ No newline at end of file
+5bfb862419541de955dae35fc91f6e9fc70e2744bb786d49fa26bede80b9091d
\ No newline at end of file
index 2047328506be0876c3b3c33e2a746a1c627a1840..d1698b127f07bb2b7677d7f13b25b962025768b8 100644 (file)
@@ -342,6 +342,10 @@ static int lookupName(
                 ){
                   sqlite3ExprListDelete(db, pFJMatch);
                   pFJMatch = 0;
+                  if( pItem->fg.jointype & JT_LTORJ ){
+                    cnt++;
+                    continue;
+                  }
                 }else
                 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
                   /* An INNER or LEFT JOIN.  Use the left-most table */
@@ -392,6 +396,10 @@ static int lookupName(
               ){
                 sqlite3ExprListDelete(db, pFJMatch);
                 pFJMatch = 0;
+                if( pItem->fg.jointype & JT_LTORJ ){
+                  cnt++;
+                  continue;
+                }
               }else
               if( (pItem->fg.jointype & JT_RIGHT)==0 ){
                 /* An INNER or LEFT JOIN.  Use the left-most table */
@@ -671,10 +679,17 @@ static int lookupName(
   ** be multiple matches for a NATURAL LEFT JOIN or a LEFT JOIN USING.
   */
   assert( pFJMatch==0 || cnt>0 );
+  assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) );
   if( cnt!=1 ){
     const char *zErr;
     if( pFJMatch ){
       if( pFJMatch->nExpr==cnt-1 ){
+        if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
+          sqlite3ExprDelete(db, pExpr->pLeft);
+          pExpr->pLeft = 0;
+          sqlite3ExprDelete(db, pExpr->pRight);
+          pExpr->pRight = 0;
+        }
         extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
         pExpr->op = TK_FUNCTION;
         pExpr->u.zToken = "coalesce";
@@ -699,6 +714,15 @@ static int lookupName(
   }
   assert( pFJMatch==0 );
 
+  /* Remove all substructure from pExpr */
+  if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
+    sqlite3ExprDelete(db, pExpr->pLeft);
+    pExpr->pLeft = 0;
+    sqlite3ExprDelete(db, pExpr->pRight);
+    pExpr->pRight = 0;
+    ExprSetProperty(pExpr, EP_Leaf);
+  }
+
   /* If a column from a table in pSrcList is referenced, then record
   ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
   ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  Bit 63 is
@@ -721,13 +745,6 @@ static int lookupName(
 lookupname_end:
   if( cnt==1 ){
     assert( pNC!=0 );
-    if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
-      sqlite3ExprDelete(db, pExpr->pLeft);
-      pExpr->pLeft = 0;
-      sqlite3ExprDelete(db, pExpr->pRight);
-      pExpr->pRight = 0;
-      ExprSetProperty(pExpr, EP_Leaf);
-    }
 #ifndef SQLITE_OMIT_AUTHORIZATION
     if( pParse->db->xAuth
      && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER)
index 8c01b01aa5964ba4809b8b9de7a03a8a083aec6c..245b9cf5df7af1a9dd119caab71badea410631ab 100644 (file)
@@ -5829,7 +5829,8 @@ static int selectExpander(Walker *pWalker, Select *p){
               zToFree = 0;
             }
             if( pTabList->nSrc>1
-             && !inAnyUsingClause(zName,pFrom,pTabList->nSrc-i-1)
+             && (  (pFrom->fg.jointype & JT_LTORJ)==0
+                 || !inAnyUsingClause(zName,pFrom,pTabList->nSrc-i-1) )
             ){
               Expr *pLeft;
               pLeft = sqlite3Expr(db, TK_ID, zTabName);
diff --git a/test/join9.test b/test/join9.test
new file mode 100644 (file)
index 0000000..fde5436
--- /dev/null
@@ -0,0 +1,69 @@
+# 2022-04-16
+#
+# The author disclaims copyright to this source code.  In place of
+# a legal notice, here is a blessing:
+#
+#    May you do good and not evil.
+#    May you find forgiveness for yourself and forgive others.
+#    May you share freely, never taking more than you give.
+#
+#***********************************************************************
+# This file implements regression tests for SQLite library.
+#
+# This file implements tests for RIGHT and FULL OUTER JOINs.
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+foreach {id schema} {
+  1 {
+    CREATE TABLE t4(id INTEGER PRIMARY KEY, x TEXT);
+    CREATE TABLE t5(id INTEGER PRIMARY KEY, y TEXT);
+    CREATE TABLE t6(id INTEGER PRIMARY KEY, z INT);
+    INSERT INTO t4(id,x) VALUES(2,'alice'),(4,'bob'),(6,'cindy'),(8,'dave');
+    INSERT INTO t5(id,y) VALUES(1,'red'),(2,'orange'),(3,'yellow'),(4,'green'),
+                               (5,'blue');
+    INSERT INTO t6(id,z) VALUES(3,333),(4,444),(5,555),(0,1000),(9,999);
+  }
+} {
+  reset_db
+  db nullvalue -
+  do_execsql_test join9-$id.setup $schema {}
+  do_execsql_test join9-$id.10 {
+    SELECT *, t4.id, t5.id, t6.id
+      FROM t4 NATURAL LEFT JOIN t5 NATURAL LEFT JOIN t6
+     ORDER BY id;
+  } {
+    2   alice  orange  -    2   2   - 
+    4   bob    green   444  4   4   4 
+    6   cindy  -       -    6   -   - 
+    8   dave   -       -    8   -   - 
+  }
+  do_execsql_test join9-$id.20 {
+    SELECT *, t4.id, t5.id, t6.id
+      FROM t4 NATURAL RIGHT JOIN t5 NATURAL RIGHT JOIN t6
+     ORDER BY id;
+  } {
+    0   -    -       1000  -   -   0 
+    3   -    yellow  333   -   3   3 
+    4   bob  green   444   4   4   4 
+    5   -    blue    555   -   5   5 
+    9   -    -       999   -   -   9 
+  }
+  do_execsql_test join9-$id.30 {
+    SELECT *, t4.id, t5.id, t6.id
+      FROM t4 NATURAL FULL JOIN t5 NATURAL FULL JOIN t6
+     ORDER BY id;
+  } {
+    0    -      -       1000  -   -   0 
+    1    -      red     -     -   1   - 
+    2    alice  orange  -     2   2   - 
+    3    -      yellow  333   -   3   3 
+    4    bob    green   444   4   4   4 
+    5    -      blue    555   -   5   5 
+    6    cindy  -       -     6   -   - 
+    8    dave   -       -     8   -   - 
+    9    -      -       999   -   -   9 
+  }
+}  
+finish_test