SQLite

Check-in [2f09b51b]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:When a table has an INTEGER PRIMARY KEY ON CONFLICT REPLACE and some other uniqueness constraint, and it participates in an upsert on that other constraint, ensure that code that checks for conflicts on the INTEGER PRIMARY KEY is well-formed. Fix for the problem reported by forum post 06b16b8b29f8c8c3.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 2f09b51b1ff37bf98f958564cc1224968caa7e33147e6666b5c1ba20b2a7813b
User & Date: drh 2021-12-30 02:38:43
Context
2021-12-30
17:36
When constructing the sqlite3_index_info object for the xBestIndex method of a virtual table, omit constant trims from the ORDER BY clause, as they will always be in the correct order. (check-in: 524c2b87 user: drh tags: trunk)
03:12
When a table has an INTEGER PRIMARY KEY ON CONFLICT REPLACE and some other uniqueness constraint, and it participates in an upsert on that other constraint, ensure that code that checks for conflicts on the INTEGER PRIMARY KEY is well-formed. Fix for the problem reported by forum post 06b16b8b29f8c8c3. (check-in: 1f1e0b4e user: drh tags: branch-3.37)
02:38
When a table has an INTEGER PRIMARY KEY ON CONFLICT REPLACE and some other uniqueness constraint, and it participates in an upsert on that other constraint, ensure that code that checks for conflicts on the INTEGER PRIMARY KEY is well-formed. Fix for the problem reported by forum post 06b16b8b29f8c8c3. (check-in: 2f09b51b user: drh tags: trunk)
00:37
When implementing a multi-way compound SELECT using merge, try to balance the merge tree. (check-in: bb8522fe user: drh tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/insert.c.
2003
2004
2005
2006
2007
2008
2009

2010
2011
2012
2013
2014
2015
2016
    ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
    ** to defer the running of the rowid conflict checking until after
    ** the UNIQUE constraints have run.
    */
    if( onError==OE_Replace      /* IPK rule is REPLACE */
     && onError!=overrideError   /* Rules for other constraints are different */
     && pTab->pIndex             /* There exist other constraints */

    ){
      ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
      VdbeComment((v, "defer IPK REPLACE until last"));
    }

    if( isUpdate ){
      /* pkChng!=0 does not mean that the rowid has changed, only that







>







2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
    ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
    ** to defer the running of the rowid conflict checking until after
    ** the UNIQUE constraints have run.
    */
    if( onError==OE_Replace      /* IPK rule is REPLACE */
     && onError!=overrideError   /* Rules for other constraints are different */
     && pTab->pIndex             /* There exist other constraints */
     && !upsertIpkDelay          /* IPK check already deferred by UPSERT */
    ){
      ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
      VdbeComment((v, "defer IPK REPLACE until last"));
    }

    if( isUpdate ){
      /* pkChng!=0 does not mean that the rowid has changed, only that
2411
2412
2413
2414
2415
2416
2417

2418
2419
2420
2421
2422
2423
2424
    }
  }

  /* If the IPK constraint is a REPLACE, run it last */
  if( ipkTop ){
    sqlite3VdbeGoto(v, ipkTop);
    VdbeComment((v, "Do IPK REPLACE"));

    sqlite3VdbeJumpHere(v, ipkBottom);
  }

  /* Recheck all uniqueness constraints after replace triggers have run */
  testcase( regTrigCnt!=0 && nReplaceTrig==0 );
  assert( regTrigCnt!=0 || nReplaceTrig==0 );
  if( nReplaceTrig ){







>







2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
    }
  }

  /* If the IPK constraint is a REPLACE, run it last */
  if( ipkTop ){
    sqlite3VdbeGoto(v, ipkTop);
    VdbeComment((v, "Do IPK REPLACE"));
    assert( ipkBottom>0 );
    sqlite3VdbeJumpHere(v, ipkBottom);
  }

  /* Recheck all uniqueness constraints after replace triggers have run */
  testcase( regTrigCnt!=0 && nReplaceTrig==0 );
  assert( regTrigCnt!=0 || nReplaceTrig==0 );
  if( nReplaceTrig ){
Changes to test/upsert1.test.
236
237
238
239
240
241
242














243
244
# 2019-12-26 ticket 7c13db5c3bf74001
reset_db
do_catchsql_test upsert1-1000 {
  CREATE TABLE t0(c0 PRIMARY KEY, c1, c2 UNIQUE) WITHOUT ROWID;
  INSERT OR FAIL INTO t0(c2) VALUES (0), (NULL)
    ON CONFLICT(c2) DO UPDATE SET c1 = c0;
} {1 {NOT NULL constraint failed: t0.c0}}















finish_test







>
>
>
>
>
>
>
>
>
>
>
>
>
>


236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# 2019-12-26 ticket 7c13db5c3bf74001
reset_db
do_catchsql_test upsert1-1000 {
  CREATE TABLE t0(c0 PRIMARY KEY, c1, c2 UNIQUE) WITHOUT ROWID;
  INSERT OR FAIL INTO t0(c2) VALUES (0), (NULL)
    ON CONFLICT(c2) DO UPDATE SET c1 = c0;
} {1 {NOT NULL constraint failed: t0.c0}}

# 2021-12-29 forum post https://sqlite.org/forum/forumpost/06b16b8b29f8c8c3
# By Jingzhou Fu. When there is both an INTEGER PRIMARY KEY ON CONFLICT REPLACE
# and an upsert on a constraint other than the INTEGER PRIMARY KEY, the
# constraint checking logic generates invalid bytecode which might result
# in a NULL pointer dereference.
#
reset_db
do_execsql_test upsert1-1100 {
  CREATE TABLE t1(a INTEGER PRIMARY KEY ON CONFLICT REPLACE, b UNIQUE);
  INSERT INTO t1(b) VALUES(22);
  INSERT INTO t1 VALUES(2,22) ON CONFLICT (b) DO NOTHING;
  SELECT * FROM t1;
} {1 22}

finish_test