]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Throw an error if the second argument passed to nth_value() is not a positive
authordan <dan@noemail.net>
Mon, 9 Jul 2018 13:31:18 +0000 (13:31 +0000)
committerdan <dan@noemail.net>
Mon, 9 Jul 2018 13:31:18 +0000 (13:31 +0000)
integer.

FossilOrigin-Name: 1a06e57a0b4279fa580c7ff4f152645f005794aaf86eeabf694637b7da11f763

manifest
manifest.uuid
src/window.c
test/window4.tcl
test/window4.test
test/window6.test

index db50739a1171d5acd380ed291ed70ac28abafa66..16d44b2759113ce25fe4584ac396180a75f684da 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sa\sbad\sassert()\sin\swindow.c.
-D 2018-07-09T06:51:36.138
+C Throw\san\serror\sif\sthe\ssecond\sargument\spassed\sto\snth_value()\sis\snot\sa\spositive\ninteger.
+D 2018-07-09T13:31:18.482
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F Makefile.in 0a3a6c81e6fcb969ff9106e882f0a08547014ba463cb6beca4c4efaecc924ee6
@@ -584,7 +584,7 @@ F src/where.c 0bcbf9e191ca07f9ea2008aa80e70ded46bcdffd26560c83397da501f00aece6
 F src/whereInt.h b90ef9b9707ef750eab2a7a080c48fb4900315033274689def32d0cf5a81ebe4
 F src/wherecode.c 3317f2b083a66d3e65a03edf316ade4ccb0a99c9956273282ebb579b95d4ba96
 F src/whereexpr.c 571618c67a3eb5ce0f1158c2792c1aee9b4a4a264392fc4fb1b35467f80abf9a
-F src/window.c fcc7f6febe4f49df2399b09fd4db5418072424085d409bcb4e5e83c94e50c594
+F src/window.c a3f2de2ff893e0f972d0fbc5addc5a5ab97ab900946001ce652aaded9e15eb65
 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
 F test/affinity2.test a6d901b436328bd67a79b41bb0ac2663918fe3bd
 F test/affinity3.test 6a101af2fc945ce2912f6fe54dd646018551710d
@@ -1624,10 +1624,10 @@ F test/window2.tcl 9bfa842d8a62b0d36dc8c1b5972206393c43847433c6d75940b87fec93ce3
 F test/window2.test 8e6d2a1b9f54dfebee1cde961c8590cd87b4db45c50f44947a211e1b63c2a05e
 F test/window3.tcl 577a3b1ff913208e5248c04dab9df17fd760ce159a752789e26d0cb4a5f91823
 F test/window3.test 87fb18021903fc4d1659b8b2092aea55d611a9606cfa7272686234e5197c4b31
-F test/window4.tcl 9491acccf705d0e0f44177dc20478f978bd0bd4812a6ddd6a350881cc7781876
-F test/window4.test b43a22ad5b748c37925e64c93ed0958ebae1c8d2eca59a79350c22c2e6f609a0
+F test/window4.tcl 871364059b7d320d556ec6ef804d604a4e8cc1547a3102c5d56067371bb200af
+F test/window4.test 323b118eb592932036388643ca6dcaead87f699bbea2984bbca49ba4ad6c2509
 F test/window5.test 8187f46597c90b73e8f96659e893353cbda337479cc582f7a488eab351ba08d3
-F test/window6.test 8370c02744297fcb9214c084779182bb95df1c87709906dccbe2643a0346ccdf
+F test/window6.test 63a7176e01aef5cc202563bbd754a88131404bf1aa455d7232f29a4740e3ddde
 F test/windowfault.test a44baa1b0bdf339d7792623ad3a6ebe057b1a6d7a7fd7ae880c8b744cfc0dafb
 F test/with1.test 58475190cd8caaeebea8cfeb2a264ec97a0c492b8ffe9ad20cefbb23df462f96
 F test/with2.test e0030e2f0267a910d6c0e4f46f2dfe941c1cc0d4f659ba69b3597728e7e8f1ab
@@ -1745,7 +1745,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 8f21d778450e1e5d2bc7f109d614fe018eb3d228b0bfc91cd3c6a5204415998f
-R c99424e74d45d4c034c17713bab90e29
+P fe8aaf0c806413965f50a03e34b5fdfaaa8b09dc0af73f41e830f7b664bd1ced
+R 36a1b3786e652c733a23427414a09e4c
 U dan
-Z 08afb33e1e6d6af7e7e8db9039bcb259
+Z fa36ac2705a3a1bf25d5a3791daaa70e
index 0fd521cf4d8885de9e67fe8b4f32c5ed73817143..0803b15bf123e0b60c45e739c778816443e93790 100644 (file)
@@ -1 +1 @@
-fe8aaf0c806413965f50a03e34b5fdfaaa8b09dc0af73f41e830f7b664bd1ced
\ No newline at end of file
+1a06e57a0b4279fa580c7ff4f152645f005794aaf86eeabf694637b7da11f763
\ No newline at end of file
index cd09f92ca73e1ca4846a240e4aaf1918ae38dd49..816ccc2b065760d22cd96fce6bd08815ebf24396 100644 (file)
@@ -1002,25 +1002,29 @@ void sqlite3WindowCodeInit(Parse *pParse, Window *pMWin){
 }
 
 /*
-** A "PRECEDING <expr>" (bEnd==0) or "FOLLOWING <expr>" (bEnd==1) has just 
-** been evaluated and the result left in register reg. This function generates
-** VM code to check that the value is a non-negative integer and throws
-** an exception if it is not.
+** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
+** value of the second argument to nth_value() (eCond==2) has just been
+** evaluated and the result left in register reg. This function generates VM
+** code to check that the value is a non-negative integer and throws an
+** exception if it is not.
 */
-static void windowCheckFrameOffset(Parse *pParse, int reg, int bEnd){
+static void windowCheckIntValue(Parse *pParse, int reg, int eCond){
   static const char *azErr[] = {
     "frame starting offset must be a non-negative integer",
-    "frame ending offset must be a non-negative integer"
+    "frame ending offset must be a non-negative integer",
+    "second argument to nth_value must be a positive integer"
   };
+  static int aOp[] = { OP_Ge, OP_Ge, OP_Gt };
   Vdbe *v = sqlite3GetVdbe(pParse);
   int regZero = sqlite3GetTempReg(pParse);
+  assert( eCond==0 || eCond==1 || eCond==2 );
   sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero);
   sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
   VdbeCoverage(v);
-  sqlite3VdbeAddOp3(v, OP_Ge, regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
+  sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
   VdbeCoverage(v);
   sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort);
-  sqlite3VdbeAppendP4(v, (void*)azErr[bEnd], P4_STATIC);
+  sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC);
   sqlite3ReleaseTempReg(pParse, regZero);
 }
 
@@ -1278,6 +1282,7 @@ static void windowReturnOneRow(
 
       if( pFunc->zName==nth_valueName ){
         sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+1,tmpReg);
+        windowCheckIntValue(pParse, tmpReg, 2);
       }else{
         sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
       }
@@ -1576,11 +1581,11 @@ static void windowCodeRowExprStep(
   ** an exception.  */
   if( pMWin->pStart ){
     sqlite3ExprCode(pParse, pMWin->pStart, regStart);
-    windowCheckFrameOffset(pParse, regStart, 0);
+    windowCheckIntValue(pParse, regStart, 0);
   }
   if( pMWin->pEnd ){
     sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
-    windowCheckFrameOffset(pParse, regEnd, 1);
+    windowCheckIntValue(pParse, regEnd, 1);
   }
 
   /* If this is "ROWS <expr1> FOLLOWING AND ROWS <expr2> FOLLOWING", do:
index 011671b66dbe5500dc97c63caab455f167b27e83..ea36735a09f51b4393662d0648ef98de608378d4 100644 (file)
@@ -351,6 +351,14 @@ execsql_test 10.1 {
   SELECT id, min(b) OVER (PARTITION BY a ORDER BY id) FROM t7;
 }
 
+execsql_test 10.2 {
+  SELECT id, lead(b, -1) OVER (PARTITION BY a ORDER BY id) FROM t7;
+}
+execsql_test 10.3 {
+  SELECT id, lag(b, -1) OVER (PARTITION BY a ORDER BY id) FROM t7;
+}
+
+
 
 finish_test
 
index d8026fed8b3bd63e67f80b346c21c775d2dc38c9..1933a32d094672c0202d8c0a2f48f1b11fb5a842 100644 (file)
@@ -1261,4 +1261,12 @@ do_execsql_test 10.1 {
   SELECT id, min(b) OVER (PARTITION BY a ORDER BY id) FROM t7;
 } {1 2   2 2   3 2   4 {}   5 8   6 1}
 
+do_execsql_test 10.2 {
+  SELECT id, lead(b, -1) OVER (PARTITION BY a ORDER BY id) FROM t7;
+} {1 {}   2 2   3 {}   4 {}   5 {}   6 8}
+
+do_execsql_test 10.3 {
+  SELECT id, lag(b, -1) OVER (PARTITION BY a ORDER BY id) FROM t7;
+} {1 {}   2 4   3 {}   4 8   5 1   6 {}}
+
 finish_test
index effebea30f56889200fd067749e20da1a97de06d..1f2294e10a13961ca87aaa077d3fd4090bb52e8d 100644 (file)
@@ -283,4 +283,31 @@ do_execsql_test 10.0 {
     WINDOW w1 AS (ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING);
 } {1}
 
+foreach {tn stmt} {
+  1 "SELECT nth_value(b, 0) OVER (ORDER BY a) FROM t1"
+  2 "SELECT nth_value(b, -1) OVER (ORDER BY a) FROM t1"
+  3 "SELECT nth_value(b, '4ab') OVER (ORDER BY a) FROM t1"
+  4 "SELECT nth_value(b, NULL) OVER (ORDER BY a) FROM t1"
+  5 "SELECT nth_value(b, 8.5) OVER (ORDER BY a) FROM t1"
+} {
+  do_catchsql_test 10.1.$tn "
+    WITH t1(a,b) AS ( VALUES(1, 2), (2, 3), (3, 4) )
+    $stmt
+  " {1 {second argument to nth_value must be a positive integer}}
+}
+
+foreach {tn stmt res} {
+  1 "SELECT nth_value(b, 1) OVER (ORDER BY a) FROM t1"         {2 2 2}
+  2 "SELECT nth_value(b, 2) OVER (ORDER BY a) FROM t1"         {{} 3 3}
+  3 "SELECT nth_value(b, '2') OVER (ORDER BY a) FROM t1"       {{} 3 3}
+  4 "SELECT nth_value(b, 2.0) OVER (ORDER BY a) FROM t1"       {{} 3 3}
+  5 "SELECT nth_value(b, '2.0') OVER (ORDER BY a) FROM t1"     {{} 3 3}
+  6 "SELECT nth_value(b, 10000000) OVER (ORDER BY a) FROM t1"  {{} {} {}}
+} {
+  do_execsql_test 10.2.$tn "
+    WITH t1(a,b) AS ( VALUES(1, 2), (2, 3), (3, 4) )
+    $stmt
+  " $res
+}
+
 finish_test