SQLite User Forum

Assertion failure in sqlite3WhereEnd function
Login

Assertion failure in sqlite3WhereEnd function

(1.1) By Song Liu (songliu) on 2023-03-27 22:12:22 edited from 1.0 [source]

I found an assertion failure while SQLite (latest, 4fc1904b8e18c7d4) executes the following queries.

CREATE TABLE v0 (c0);
CREATE VIEW v1 AS SELECT * FROM v0 JOIN v0 A ORDER BY sum(0) OVER(PARTITION BY(SELECT 0 WINDOW y AS (PARTITION BY(0))));
UPDATE v1 SET c0=nth_value(0,0) OVER() FROM(SELECT 0 FROM v0) WHERE c0 IS 0 RETURNING 0;

Here are the outputs:

sqlite3: sqlite3.c:164032: sqlite3WhereEnd: Assertion `(pLoop->wsFlags & WHERE_IDX_ONLY)==0 || cursorIsOpen(v,pOp->p1,k) || pOp->opcode==OP_Offset' failed.
[1]    3773025 abort      ./sqlite3 < poc

Here is the result of bisecting:

 13 BAD     2021-02-03 13:08:09 416c898bfb8ff963
 14 GOOD    2021-02-03 12:35:51 06b15b17be38c804 CURRENT

The assertion failure may be caused by the optimizations. SQLite crashes by default with all optimizations enabled. If I disable the SQLITE_Coroutines optimization, SQLite works well.

CREATE TABLE v0 (c0);
CREATE VIEW v1 AS SELECT * FROM v0 JOIN v0 A ORDER BY sum(0) OVER(PARTITION BY(SELECT 0 WINDOW y AS (PARTITION BY(0))));

.testctrl optimizations 0x02000000;
.print '-- disable optimization of SQLITE_Coroutines'
UPDATE v1 SET c0=nth_value(0,0) OVER() FROM(SELECT 0 FROM v0) WHERE c0 IS 0 RETURNING 0;

My compilation flags:

    export CFLAGS="-g -O0 -DSQLITE_DEBUG 
                -DSQLITE_ENABLE_TREETRACE 
                -DSQLITE_ENABLE_WHERETRACE
                -DSQLITE_ENABLE_CURSOR_HINTS 
                -DSQLITE_COUNTOFVIEW_OPTIMIZATION 
                -DSQLITE_ENABLE_STAT4" 

(2) By Richard Hipp (drh) on 2023-03-28 11:24:54 in reply to 1.1 [link] [source]

This issue should now be resolved on both trunk and branch-3.41.

(3) By Song Liu (songliu) on 2023-03-28 12:24:41 in reply to 2 [link] [source]

Thanks for your work!