Enforce affinity on materialized tables for subqueries and views. Also,

do not allow UNION ALL flattening if the affinity of a result column varies
between different arms of the compound.  This is a fix for
ticket [57c47526c34f01e8].

FossilOrigin-Name: 88a05141c28e5ff1357c3c599493e4ffb8f3821bab04be80244deac62e49135c
This commit is contained in:
drh 2022-11-01 12:10:39 +00:00
commit be44e188e3
8 changed files with 155 additions and 36 deletions

View File

@ -1,5 +1,5 @@
C Minor\sinternal\scleanups\sin\sthe\sjs\spieces.
D 2022-11-01T11:09:34.309
C Enforce\saffinity\son\smaterialized\stables\sfor\ssubqueries\sand\sviews.\s\sAlso,\ndo\snot\sallow\sUNION\sALL\sflattening\sif\sthe\saffinity\sof\sa\sresult\scolumn\svaries\nbetween\sdifferent\sarms\sof\sthe\scompound.\s\sThis\sis\sa\sfix\sfor\nticket\s[57c47526c34f01e8].
D 2022-11-01T12:10:39.967
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -595,7 +595,7 @@ F src/hash.c 8d7dda241d0ebdafb6ffdeda3149a412d7df75102cecfc1021c98d6219823b19
F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51
F src/hwtime.h cb1d7e3e1ed94b7aa6fde95ae2c2daccc3df826be26fc9ed7fd90d1750ae6144
F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
F src/insert.c b972b1642cd0f7d5ae4964d713b65158a7bfa6a6e8786a9e8e57bd9c0b4a896d
F src/insert.c 90a32bc7faa755cd5292ade21d2b3c6edba8fd1d70754a364caccabfde2c3bb2
F src/json.c 7749b98c62f691697c7ee536b570c744c0583cab4a89200fdd0fc2aa8cc8cbd6
F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
F src/loadext.c 8086232d10e51e183a7f64199815bad1c579896354db69435347665f62f481e9
@ -636,17 +636,17 @@ F src/printf.c e99ee9741e79ae3873458146f59644276657340385ade4e76a5f5d1c25793764
F src/random.c 546d6feb15ec69c1aafe9bb351a277cbb498fd5410e646add673acb805714960
F src/resolve.c efea4e5fbecfd6d0a9071b0be0d952620991673391b6ffaaf4c277b0bb674633
F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92
F src/select.c 8a709a8e19b32d2c411eaae1a10e2ee214cf740288859a9f9a362fcb2f50eaf1
F src/select.c 12cb5162e606290354f9512ff0c30fc6dc4d7a77a92b6c5b581395f9c4edd047
F src/shell.c.in bde69ad6a9887396c87d089da84c0fe1e432957faebd9206b9185be735341104
F src/sqlite.h.in d9c8a6243fc0a1c270d69db33758e34b810af3462f9bc5b4af113b347e07c69d
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
F src/sqlite3ext.h 5336beea1868d99d2f62e628dbea55e97267dbff8193291ab175e960c5df9141
F src/sqliteInt.h 2449c9ea2936e077dfba0f116178d8be2b908cf1ed8fa8eaabe73540a4df4331
F src/sqliteInt.h 2c24ba38f78e32fe5d7ec136321a6ad827698b33ca98664970a8b7274d69ef7c
F src/sqliteLimit.h d7323ffea5208c6af2734574bae933ca8ed2ab728083caa117c9738581a31657
F src/status.c 160c445d7d28c984a0eae38c144f6419311ed3eace59b44ac6dafc20db4af749
F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1
F src/tclsqlite.c 4e64ba300a5a26e0f1170e09032429faeb65e45e8f3d1a7833e8edb69fc2979e
F src/test1.c 790c8cd24d1ac39b8cf4e2f618ce16ecc028accfcde60660f7bd983e0f4c7d28
F src/test1.c 40c9a40975512985762f87b83d0c63e4904833a9fe78cbcca664a37095301b1d
F src/test2.c 3efb99ab7f1fc8d154933e02ae1378bac9637da5
F src/test3.c 61798bb0d38b915067a8c8e03f5a534b431181f802659a6616f9b4ff7d872644
F src/test4.c 4533b76419e7feb41b40582554663ed3cd77aaa54e135cf76b3205098cd6e664
@ -853,7 +853,7 @@ F test/capi3c.test 54e2dc0c8fd7c34ad1590d1be6864397da2438c95a9f5aee2f8fbc60c112e
F test/capi3d.test 8b778794af891b0dca3d900bd345fbc8ebd2aa2aae425a9dccdd10d5233dfbde
F test/capi3e.test 3d49c01ef2a1a55f41d73cba2b23b5059ec460fe
F test/carray01.test d55d57bf66b1af1c7ac55fae66ff4910884a8f5d21a90a18797ce386212a2634
F test/cast.test 336fa21989b5170ebcaf90c24266be22dd97b3e23d1fad5ecf6ad4efb04c4423
F test/cast.test 6064022ba9af31a8a2ff7bb345e5bd0e74172ffad85bdab5898a42d8227c7585
F test/cffault.test 9d6b20606afe712374952eec4f8fd74b1a8097ef
F test/changes.test 9dd8e597d84072122fc8a4fcdea837f4a54a461e6e536053ea984303e8ca937b
F test/changes2.test d222c0cbf5ab0ac4d7c180594e486c1bf20b2098d33e56ce33b8e12eba6823b9
@ -1769,7 +1769,7 @@ F test/tt3_vacuum.c 71b254cde1fc49d6c8c44efd54f4668f3e57d7b3a8f4601ade069f75a999
F test/types.test bf816ce73c7dfcfe26b700c19f97ef4050d194ff
F test/types2.test 1aeb81976841a91eef292723649b5c4fe3bc3cac
F test/types3.test 99e009491a54f4dc02c06bdbc0c5eea56ae3e25a
F test/unionall.test fee6e6adebe5446938475432ae8b042835a286c7e2efdc1b186d2182dfb93ffb
F test/unionall.test bfeeea6c18c09a46f7e8bed69173e31c71336152e6253fb37c7257c76509f0e4
F test/unionall2.test 71e8fa08d5699d50dc9f9dc0c9799c2e7a6bb7931a330d369307a4df7f157fa1
F test/unionallfault.test 652bfbb630e6c43135965dc1e8f0a9a791da83aec885d626a632fe1909c56f73
F test/unionvtab.test e1704ab1b4c1bb3ffc9da4681f8e85a0b909fd80b937984fc94b27415ac8e5a4
@ -2054,8 +2054,9 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 86a341d7e061f946b39e8647ddd4743013b851b33ae9e6e755d8dbc53fba5286
R c124f1782fbda1f872463378ecd48f84
U stephan
Z b4351b5e7b55104b1aa36757ad482931
P 271391b4e32220ab4c32d69f579ecd2b03eb99da898955a1ef8fffc27216719d a3a500127d4752ee05c183e9ac97a2bc27768bf7063b04b5c1405cd3fd3931f5
R 60e75a792dff45d600a3259961a93e92
T +closed a3a500127d4752ee05c183e9ac97a2bc27768bf7063b04b5c1405cd3fd3931f5
U drh
Z b380e750784c0847900a6a6007ece443
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
271391b4e32220ab4c32d69f579ecd2b03eb99da898955a1ef8fffc27216719d
88a05141c28e5ff1357c3c599493e4ffb8f3821bab04be80244deac62e49135c

View File

@ -110,6 +110,28 @@ const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
return pIdx->zColAff;
}
/*
** Compute an affinity string for a table. Space is obtained
** from sqlite3DbMalloc(). The caller is responsible for freeing
** the space when done.
*/
char *sqlite3TableAffinityStr(sqlite3 *db, const Table *pTab){
char *zColAff;
zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1);
if( zColAff ){
int i, j;
for(i=j=0; i<pTab->nCol; i++){
if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
zColAff[j++] = pTab->aCol[i].affinity;
}
}
do{
zColAff[j--] = 0;
}while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB );
}
return zColAff;
}
/*
** Make changes to the evolving bytecode to do affinity transformations
** of values that are about to be gathered into a row for table pTab.
@ -151,7 +173,7 @@ const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
** Apply the type checking to that array of registers.
*/
void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
int i, j;
int i;
char *zColAff;
if( pTab->tabFlags & TF_Strict ){
if( iReg==0 ){
@ -174,22 +196,11 @@ void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
}
zColAff = pTab->zColAff;
if( zColAff==0 ){
sqlite3 *db = sqlite3VdbeDb(v);
zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
zColAff = sqlite3TableAffinityStr(0, pTab);
if( !zColAff ){
sqlite3OomFault(db);
sqlite3OomFault(sqlite3VdbeDb(v));
return;
}
for(i=j=0; i<pTab->nCol; i++){
assert( pTab->aCol[i].affinity!=0 || sqlite3VdbeParser(v)->nErr>0 );
if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
zColAff[j++] = pTab->aCol[i].affinity;
}
}
do{
zColAff[j--] = 0;
}while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB );
pTab->zColAff = zColAff;
}
assert( zColAff!=0 );

View File

@ -1287,6 +1287,9 @@ static void selectInnerLoop(
testcase( eDest==SRT_Fifo );
testcase( eDest==SRT_DistFifo );
sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
if( pDest->zAffSdst ){
sqlite3VdbeChangeP4(v, -1, pDest->zAffSdst, nResultCol);
}
#ifndef SQLITE_OMIT_CTE
if( eDest==SRT_DistFifo ){
/* If the destination is DistFifo, then cursor (iParm+1) is open
@ -4149,6 +4152,8 @@ static ExprList *findLeftmostExprlist(Select *pSel){
** (17g) either the subquery is the first element of the outer
** query or there are no RIGHT or FULL JOINs in any arm
** of the subquery. (This is a duplicate of condition (27b).)
** (17h) The corresponding result set expressions in all arms of the
** compound must have the same affinity.
**
** The parent and sub-query may contain WHERE clauses. Subject to
** rules (11), (13) and (14), they may also contain ORDER BY,
@ -4325,6 +4330,7 @@ static int flattenSubquery(
** queries.
*/
if( pSub->pPrior ){
int ii;
if( pSub->pOrderBy ){
return 0; /* Restriction (20) */
}
@ -4357,7 +4363,6 @@ static int flattenSubquery(
/* Restriction (18). */
if( p->pOrderBy ){
int ii;
for(ii=0; ii<p->pOrderBy->nExpr; ii++){
if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
}
@ -4366,6 +4371,21 @@ static int flattenSubquery(
/* Restriction (23) */
if( (p->selFlags & SF_Recursive) ) return 0;
/* Restriction (17h) */
for(ii=0; ii<pSub->pEList->nExpr; ii++){
char aff;
assert( pSub->pEList->a[ii].pExpr!=0 );
aff = sqlite3ExprAffinity(pSub->pEList->a[ii].pExpr);
for(pSub1=pSub->pPrior; pSub1; pSub1=pSub1->pPrior){
assert( pSub1->pEList!=0 );
assert( pSub1->pEList->nExpr>ii );
assert( pSub1->pEList->a[ii].pExpr!=0 );
if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
return 0;
}
}
}
if( pSrc->nSrc>1 ){
if( pParse->nSelect>500 ) return 0;
if( OptimizationDisabled(db, SQLITE_FlttnUnionAll) ) return 0;
@ -7071,7 +7091,10 @@ int sqlite3Select(
}
sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
ExplainQueryPlan((pParse, 1, "MATERIALIZE %!S", pItem));
dest.zAffSdst = sqlite3TableAffinityStr(db, pItem->pTab);
sqlite3Select(pParse, pSub, &dest);
sqlite3DbFree(db, dest.zAffSdst);
dest.zAffSdst = 0;
pItem->pTab->nRowLogEst = pSub->nSelectRow;
if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1);

View File

@ -3511,7 +3511,7 @@ struct SelectDest {
int iSDParm2; /* A second parameter for the eDest disposal method */
int iSdst; /* Base register where results are written */
int nSdst; /* Number of registers allocated */
char *zAffSdst; /* Affinity used when eDest==SRT_Set */
char *zAffSdst; /* Affinity used for SRT_Set, SRT_Table, and similar */
ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
};
@ -4981,6 +4981,7 @@ int sqlite3VarintLen(u64 v);
const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
char *sqlite3TableAffinityStr(sqlite3*,const Table*);
void sqlite3TableAffinity(Vdbe*, Table*, int);
char sqlite3CompareAffinity(const Expr *pExpr, char aff2);
int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity);

View File

@ -7629,7 +7629,12 @@ static int SQLITE_TCLAPI win32_rmdir(
**
** Enable or disable query optimizations using the sqlite3_test_control()
** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true.
** OPT is the name of the optimization to be disabled.
** OPT is the name of the optimization to be disabled. OPT can also be a
** list or optimizations names, in which case all optimizations named are
** enabled or disabled.
**
** Each invocation of this control overrides all prior invocations. The
** changes are not cumulative.
*/
static int SQLITE_TCLAPI optimization_control(
void * clientData,
@ -7642,6 +7647,7 @@ static int SQLITE_TCLAPI optimization_control(
const char *zOpt;
int onoff;
int mask = 0;
int cnt = 0;
static const struct {
const char *zOptName;
int mask;
@ -7661,7 +7667,6 @@ static int SQLITE_TCLAPI optimization_control(
{ "push-down", SQLITE_PushDown },
{ "balanced-merge", SQLITE_BalancedMerge },
{ "propagate-const", SQLITE_PropagateConst },
{ 0, 0 }
};
if( objc!=4 ){
@ -7672,13 +7677,13 @@ static int SQLITE_TCLAPI optimization_control(
if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR;
zOpt = Tcl_GetString(objv[2]);
for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
if( strcmp(zOpt, aOpt[i].zOptName)==0 ){
mask = aOpt[i].mask;
break;
if( strstr(zOpt, aOpt[i].zOptName)!=0 ){
mask |= aOpt[i].mask;
cnt++;
}
}
if( onoff ) mask = ~mask;
if( i>=sizeof(aOpt)/sizeof(aOpt[0]) ){
if( cnt==0 ){
Tcl_AppendResult(interp, "unknown optimization - should be one of:",
(char*)0);
for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){
@ -7687,6 +7692,7 @@ static int SQLITE_TCLAPI optimization_control(
return TCL_ERROR;
}
sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask);
Tcl_SetObjResult(interp, Tcl_NewIntObj(mask));
return TCL_OK;
}

View File

@ -481,7 +481,7 @@ do_execsql_test cast-9.0 {
CREATE VIEW v1(c0, c1) AS
SELECT CAST(0.0 AS NUMERIC), COUNT(*) OVER () FROM t0;
SELECT v1.c0 FROM v1, t0 WHERE v1.c0=0;
} {0.0}
} {0}
finish_test

View File

@ -55,6 +55,27 @@ do_execsql_test 1.3 {
} {1 one 2 two 5 five 6 six}
# 2022-10-31 part of ticket 57c47526c34f01e8
# The queries below were causing an assertion fault in
# the comparison operators of the VDBE.
#
reset_db
database_never_corrupt
optimization_control db all 0
do_execsql_test 1.10 {
CREATE TABLE t0(c0 INT);
INSERT INTO t0 VALUES(0);
CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT);
INSERT INTO t1_a VALUES(1,'one');
CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT);
INSERT INTO t1_b VALUES(2,'two');
CREATE VIEW t1 AS SELECT a, b FROM t1_a UNION ALL SELECT c, c FROM t1_b;
SELECT * FROM (SELECT t1.a, t1.b AS b, t0.c0 FROM t0, t1);
} {1 one 0 2 2 0}
do_execsql_test 1.11 {
SELECT * FROM (SELECT t1.a, t1.b AS b, t0.c0 FROM t0, t1) WHERE b=2;
} {2 2 0}
#-------------------------------------------------------------------------
reset_db
@ -364,4 +385,60 @@ do_execsql_test 7.1 {
ORDER BY x, y;
} {0 1 | 0 101 | 0 102 | 1 1 | 1 101 | 1 102 | 100 1 | 100 101 | 100 102 | 101 1 | 101 101 | 101 102 |}
# 2022-10-31 ticket https://sqlite.org/src/info/57c47526c34f01e8
# dbsqlfuzz 37230460b46b3b6049f0d768eb801f3428189382
# UNION ALL subqueries or views which have arms with different
# affinities should not be flattened.
#
reset_db
do_execsql_test 8.1 {
CREATE TABLE t0(c0 INT);
INSERT INTO t0 VALUES(0);
CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT);
INSERT INTO t1_a VALUES(1,'one');
INSERT INTO t1_a VALUES(4,'four');
CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT);
INSERT INTO t1_b VALUES(2,'two');
INSERT INTO t1_b VALUES(5,'five');
CREATE TABLE t1_c(e INTEGER PRIMARY KEY, f TEXT);
INSERT INTO t1_c VALUES(3,'three');
INSERT INTO t1_c VALUES(6,'six');
CREATE VIEW v0(c0) AS SELECT CAST(t0.c0 AS INTEGER) FROM t0;
CREATE VIEW t1 AS
SELECT a, b FROM t1_a UNION ALL
SELECT c, c FROM t1_b UNION ALL
SELECT e, f FROM t1_c;
}
optimization_control db all 1
do_execsql_test 8.2 {
SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2;
} {2 2 0 {}}
do_execsql_test 8.3 {
SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2.0;
} {}
do_execsql_test 8.4 {
SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b='2';
} {2 2 0 {}}
optimization_control db query-flattener,push-down 0
do_execsql_test 8.5 {
SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2;
} {2 2 0 {}}
do_execsql_test 8.6 {
SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2.0;
} {}
do_execsql_test 8.7 {
SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b='2';
} {2 2 0 {}}
optimization_control db all 0
do_execsql_test 8.8 {
SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2;
} {2 2 0 {}}
do_execsql_test 8.9 {
SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2.0;
} {}
do_execsql_test 8.10 {
SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b='2';
} {2 2 0 {}}
finish_test