Skip to content

Commit 51e0892

Browse files
committed
{173518307} UPSERT: Skip unnecessary constraint checks done on the replicant
Signed-off-by: Nirbhay Choubey <nchoubey@bloomberg.net>
1 parent 002182d commit 51e0892

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

sqlite/src/insert.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,9 +1367,13 @@ void sqlite3GenerateConstraintChecks(
13671367
sqlite3 *db; /* Database connection */
13681368
int i; /* loop counter */
13691369
int ix; /* Index loop counter */
1370+
#if !defined(SQLITE_BUILDING_FOR_COMDB2)
13701371
int nCol; /* Number of columns */
1372+
#endif /* !defined(SQLITE_BUILDING_FOR_COMDB2) */
13711373
int onError; /* Conflict resolution strategy */
1374+
#if !defined(SQLITE_BUILDING_FOR_COMDB2)
13721375
int addr1; /* Address of jump instruction */
1376+
#endif /* !defined(SQLITE_BUILDING_FOR_COMDB2) */
13731377
int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
13741378
int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
13751379
Index *pUpIdx = 0; /* Index to which to apply the upsert */
@@ -1391,8 +1395,10 @@ void sqlite3GenerateConstraintChecks(
13911395
v = sqlite3GetVdbe(pParse);
13921396
assert( v!=0 );
13931397
assert( pTab->pSelect==0 ); /* This table is not a VIEW */
1398+
#if !defined(SQLITE_BUILDING_FOR_COMDB2)
13941399
nCol = pTab->nCol;
1395-
1400+
#endif /* !defined(SQLITE_BUILDING_FOR_COMDB2) */
1401+
13961402
/* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
13971403
** normal rowid tables. nPkField is the number of key fields in the
13981404
** pPk index or 1 for a rowid table. In other words, nPkField is the
@@ -1409,6 +1415,7 @@ void sqlite3GenerateConstraintChecks(
14091415
VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
14101416
iDataCur, iIdxCur, regNewData, regOldData, pkChng));
14111417

1418+
#if !defined(SQLITE_BUILDING_FOR_COMDB2)
14121419
/* Test all NOT NULL constraints.
14131420
*/
14141421
for(i=0; i<nCol; i++){
@@ -1504,6 +1511,7 @@ void sqlite3GenerateConstraintChecks(
15041511
pParse->iSelfTab = 0;
15051512
}
15061513
#endif /* !defined(SQLITE_OMIT_CHECK) */
1514+
#endif /* !defined(SQLITE_BUILDING_FOR_COMDB2) */
15071515

15081516
/* UNIQUE and PRIMARY KEY constraints should be handled in the following
15091517
** order:
@@ -1719,7 +1727,6 @@ void sqlite3GenerateConstraintChecks(
17191727
VdbeNoopComment((v, "uniqueness check for %s", pIdx->zName));
17201728
iThisCur = iIdxCur+ix;
17211729

1722-
17231730
/* Skip partial indices for which the WHERE clause is not true */
17241731
if( pIdx->pPartIdxWhere ){
17251732
sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
@@ -1768,6 +1775,12 @@ void sqlite3GenerateConstraintChecks(
17681775
continue;
17691776
}
17701777

1778+
/* Skip non-target indices */
1779+
if( pUpIdx && pUpIdx!=pIdx ) {
1780+
sqlite3VdbeResolveLabel(v, addrUniqueOk);
1781+
continue;
1782+
}
1783+
17711784
/* Find out what action to take in case there is a uniqueness conflict */
17721785
onError = pIdx->onError;
17731786
#if defined(SQLITE_BUILDING_FOR_COMDB2)

tests/upsert.test/t00_upsert.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,6 @@ keys
215215
(rows inserted=1)
216216
[INSERT INTO t1 VALUES(2,1) ON CONFLICT(i) DO NOTHING] failed with rc 299 add key constraint duplicate key '$KEY_1E727833' on table 't1' index 1
217217
(i=1, j=1)
218+
(test='--- Test with 2 unique indices ----')
219+
(rows inserted=2)
220+
[INSERT INTO t1 VALUES(3,2) ON CONFLICT(i) DO UPDATE SET j=j+2] failed with rc 2

tests/upsert.test/t00_upsert.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,10 @@ INSERT INTO t1 VALUES(2,1) ON CONFLICT(i) DO NOTHING;
319319
SELECT * FROM t1 ORDER BY 1;
320320

321321
DROP TABLE t1;
322+
323+
SELECT '--- Test with 2 unique indices ----' as test;
324+
CREATE TABLE t1(i INT UNIQUE, j INT UNIQUE)$$
325+
INSERT INTO t1 VALUES(1,3), (2,2);
326+
INSERT INTO t1 VALUES(3,2) ON CONFLICT(i) DO UPDATE SET j=j+2;
327+
DROP TABLE t1;
328+

0 commit comments

Comments
 (0)