From 8103b7d2b7fa06f42494cb8d22c75e67d4acd4cb Mon Sep 17 00:00:00 2001 From: drh Date: Sat, 24 Feb 2007 13:23:51 +0000 Subject: [PATCH] Make sure the INSERT xfer optimization does not trigger if the CHECK constraints on the two tables are not identical. Ticket #2252. (CVS 3660) FossilOrigin-Name: 6fc18275230563437f2985eac3795e4dfe8eb9de --- manifest | 15 +++++----- manifest.uuid | 2 +- src/insert.c | 16 +++++----- src/select.c | 3 +- test/insert4.test | 75 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 17 deletions(-) create mode 100644 test/insert4.test diff --git a/manifest b/manifest index f6ac3764ae..7a40efb515 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Make\ssure\sthat\sinteger\svalues\sare\sconverted\sto\sreal\swhen\spulled\sfrom\na\sREAL\stable\scolumn\sby\sGROUP\sBY.\s\sTicket\s#2251.\s\sAlso\smake\ssure\sdefault\nvalues\sare\scorrectly\sexpanded.\s\sThere\smay\sbe\sother\splaces\sin\sthe\scode\nwhere\sthis\sissue\scomes\sup\s-\swe\sneed\sto\slook\sfurther.\s(CVS\s3659) -D 2007-02-24T11:52:53 +C Make\ssure\sthe\sINSERT\sxfer\soptimization\sdoes\snot\strigger\sif\sthe\sCHECK\nconstraints\son\sthe\stwo\stables\sare\snot\sidentical.\s\sTicket\s#2252.\s(CVS\s3660) +D 2007-02-24T13:23:52 F Makefile.in 1fe3d0b46e40fd684e1e61f8e8056cefed16de9f F Makefile.linux-gcc 2d8574d1ba75f129aba2019f0b959db380a90935 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 @@ -69,7 +69,7 @@ F src/expr.c 7815bdb27152e0b9025a089608403f7fa5d279ec F src/func.c b7e1e220a6795ecae7649815145ea5f8644dfa5f F src/hash.c 449f3d6620193aa557f5d86cbc5cc6b87702b185 F src/hash.h 1b3f7e2609141fd571f62199fc38687d262e9564 -F src/insert.c c7cb4894ec15ae1c66a33f3e7df9761794604241 +F src/insert.c e1398c4167299704b30a037bbab6194137f34049 F src/legacy.c 2631df6a861f830d6b1c0fe92b9fdd745b2c0cd6 F src/loadext.c bbfdbf452c71b6f2723375478a365788498ec3cd F src/main.c 33c32014da3a1471e8869d2eba32b2c4314c39ce @@ -92,7 +92,7 @@ F src/pragma.c 5091300911670ddaa552bfa12c45cbca1bb7e7d6 F src/prepare.c 484389c6811415b8f23d259ac9c029613e1c72c3 F src/printf.c aade23a789d7cc88b397ec0d33a0a01a33a7a9c1 F src/random.c 6119474a6f6917f708c1dee25b9a8e519a620e88 -F src/select.c 7808a4079c3756ac946ee8c43c7e61f06134cb2e +F src/select.c 6a090150d6866a94f719eda57e58207105f4a26d F src/server.c 087b92a39d883e3fa113cae259d64e4c7438bc96 F src/shell.c d13ca007cd18192c07a668aeddcdd6a9fe639be9 F src/sqlite.h.in 6b7383baf76070214f6381f603328ca9b22a7fae @@ -228,6 +228,7 @@ F test/index3.test f66718cd92ce1216819d47e6a156755e4b2c4ca1 F test/insert.test 42e26d9192f36859938765e6817fb957cf19532b F test/insert2.test 5a20e1ace5fa0800b58d28284212290189b49aed F test/insert3.test 09a532d5b6f3a788d91be0d4d368462f522685d1 +F test/insert4.test d733a33437657cc31d65b70249e46f464f886a4a F test/interrupt.test c38b7f7c17914f0cd6a119beed5d03bc3f47f9eb F test/intpkey.test af4fd826c4784ec5c93b444de07adea0254d0d30 F test/ioerr.test 565f1a47c6af6bb75dc0ff633213cec63470c19c @@ -432,7 +433,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513 -P 908daaa9ab86e0bd1da6d0807d6aaba240c3cee0 -R eee73bc2a03f9bdc2ebbcd27a41cf46b +P e11bbf174c5a2fa75e3d1dd450c8b2a18f40e4da +R 0e22889d42f0395ba317d6639c075c27 U drh -Z 81fcb798214e4a4eaf239b3e14455f12 +Z da32718baf840d5b2e61d121b53eedac diff --git a/manifest.uuid b/manifest.uuid index e3c94210a0..c233047935 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -e11bbf174c5a2fa75e3d1dd450c8b2a18f40e4da \ No newline at end of file +6fc18275230563437f2985eac3795e4dfe8eb9de \ No newline at end of file diff --git a/src/insert.c b/src/insert.c index d96780663b..6823a72cb9 100644 --- a/src/insert.c +++ b/src/insert.c @@ -12,7 +12,7 @@ ** This file contains C code routines that are called by the parser ** to handle INSERT statements in SQLite. ** -** $Id: insert.c,v 1.173 2007/02/13 15:01:11 drh Exp $ +** $Id: insert.c,v 1.174 2007/02/24 13:23:52 drh Exp $ */ #include "sqliteInt.h" @@ -1305,7 +1305,7 @@ static int xferCompatibleIndex(Index *pDest, Index *pSrc){ ** This optimization is only attempted if ** ** (1) tab1 and tab2 have identical schemas including all the -** same indices +** same indices and constraints ** ** (2) tab1 and tab2 are different tables ** @@ -1382,18 +1382,15 @@ static int xferOptimization( if( pSelect->pOrderBy ){ return 0; /* SELECT may not have an ORDER BY clause */ } - if( pSelect->pHaving ){ - return 0; /* SELECT may not have a HAVING clause */ - } + /* Do not need to test for a HAVING clause. If HAVING is present but + ** there is no ORDER BY, we will get an error. */ if( pSelect->pGroupBy ){ return 0; /* SELECT may not have a GROUP BY clause */ } if( pSelect->pLimit ){ return 0; /* SELECT may not have a LIMIT clause */ } - if( pSelect->pOffset ){ - return 0; /* SELECT may not have an OFFSET clause */ - } + assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */ if( pSelect->pPrior ){ return 0; /* SELECT may not be a compound query */ } @@ -1455,6 +1452,9 @@ static int xferOptimization( return 0; /* pDestIdx has no corresponding index in pSrc */ } } + if( !sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){ + return 0; /* Tables have different CHECK constraints. Ticket #2252 */ + } /* If we get this far, it means either: ** diff --git a/src/select.c b/src/select.c index 84e054658a..c8f6556873 100644 --- a/src/select.c +++ b/src/select.c @@ -12,7 +12,7 @@ ** This file contains C code routines that are called by the parser ** to handle SELECT statements in SQLite. ** -** $Id: select.c,v 1.328 2007/02/24 11:52:54 drh Exp $ +** $Id: select.c,v 1.329 2007/02/24 13:23:53 drh Exp $ */ #include "sqliteInt.h" @@ -68,6 +68,7 @@ Select *sqlite3SelectNew( pNew->pOrderBy = pOrderBy; pNew->isDistinct = isDistinct; pNew->op = TK_SELECT; + assert( pOffset==0 || pLimit!=0 ); pNew->pLimit = pLimit; pNew->pOffset = pOffset; pNew->iLimit = -1; diff --git a/test/insert4.test b/test/insert4.test new file mode 100644 index 0000000000..8ae891e4b6 --- /dev/null +++ b/test/insert4.test @@ -0,0 +1,75 @@ +# 2007 January 24 +# +# 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. The +# focus of this file is testing corner cases of the INSERT statement. +# +# $Id: insert4.test,v 1.1 2007/02/24 13:23:53 drh Exp $ + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# Ticket #2252. Make sure the an INSERT from identical tables +# does not violate constraints. +# +do_test insert4-1.1 { + execsql { + CREATE TABLE t1(a int, b int, check(b>a)); + CREATE TABLE t2(x int, y int); + INSERT INTO t2 VALUES(9,1); + } + catchsql { + INSERT INTO t1 SELECT * FROM t2; + } +} {1 {constraint failed}} +do_test insert4-1.2 { + execsql { + SELECT * FROM t1; + } +} {} + +# Other coverage tests for the INSERT transfer optimization. +# +do_test insert4-2.1 { + execsql { + INSERT INTO t1 SELECT 4, 8; + SELECT * FROM t1; + } +} {4 8} +do_test insert4-2.2.1 { + execsql { + CREATE TABLE t3(a int, b int); + INSERT INTO t2 SELECT y, x FROM t2; + INSERT INTO t3 SELECT * FROM t2 LIMIT 1; + SELECT * FROM t3; + } +} {9 1} +do_test insert4-2.2.2 { + catchsql { + DELETE FROM t1; + INSERT INTO t1 SELECT * FROM t2 LIMIT 1; + SELECT * FROM t1; + } +} {1 {constraint failed}} +do_test insert4-2.3.1 { + execsql { + DELETE FROM t3; + INSERT INTO t3 SELECT DISTINCT * FROM t2; + SELECT * FROM t3; + } +} {9 1 1 9} +do_test insert4-2.3.2 { + catchsql { + DELETE FROM t1; + INSERT INTO t1 SELECT DISTINCT * FROM t2; + } +} {1 {constraint failed}} + +finish_test