SQLite Forum

Assertion failure in Indexed Expression (2)
Login

Assertion failure in Indexed Expression (2)

(1) By Song Liu (songliu) on 2023-03-25 19:27:33 [source]

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

CREATE TABLE v1 ( c1, c2 );
CREATE UNIQUE INDEX i ON v1 (c1, c1+c2);
SELECT count( a1.c1+a1.c2 ) FROM v1 AS a1 RIGHT JOIN v1 GROUP BY a1.c1;

Here are the outputs:

sqlite3: sqlite3.c:110129: sqlite3ExprCodeTarget: Assertion `pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn' failed.
[1]    73351 abort      ./sqlite3 < poc

We discussed this problem (forum post: edbded9c8c), here is the patch 76b90f267c5cc676. It seems the patch is incomplete and there is another case that can trigger this assertion failure (not the same location).

Here is the result of bisecting:

 11 BAD     2023-03-04 15:36:51 e06973876993926f CURRENT
 10 GOOD    2023-03-04 12:57:07 35f10a06ba81b8a5

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

CREATE TABLE v1 ( c1, c2 );
CREATE UNIQUE INDEX i ON v1 (c1, c1+c2);

.testctrl optimizations 0x01000000;
.print '-- disable optimization of SQLITE_IndexedExpr'
SELECT count( a1.c1+a1.c2 ) FROM v1 AS a1 RIGHT JOIN v1 GROUP BY a1.c1;

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-25 23:59:06 in reply to 1 [link] [source]

(3) By Song Liu (songliu) on 2023-03-26 01:11:15 in reply to 2 [link] [source]

Thanks for your work!