Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Better handle WHERE terms that are common to two or more OR branches when planning virtual table queries. Fix for ticket [6e3ace1270f29930]. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
1976c3f7e1fe77cf3367710e8ada230a |
User & Date: | dan 2024-06-04 17:26:15 |
Original Comment: | Better handle WHERE terms that are common to two or more OR branches when planning virtual table queries. |
Context
2024-06-04
| ||
19:21 | Fix harmless compiler warnings seen with MSVC. (check-in: 93ac8573 user: mistachkin tags: trunk) | |
17:26 | Better handle WHERE terms that are common to two or more OR branches when planning virtual table queries. Fix for ticket [6e3ace1270f29930]. (check-in: 1976c3f7 user: dan tags: trunk) | |
15:07 | Fix a couple of memory leaks in the shell tool code that could occur when processing errors. (check-in: e84f09d4 user: dan tags: trunk) | |
2024-06-02
| ||
10:52 | Be more aggressive about freeing memory associated with the sqlite3_index_info.idxStr field. (Closed-Leaf check-in: 85dcd0a8 user: drh tags: xbestindex-or-terms) | |
Changes
Changes to ext/fts5/fts5_main.c.
︙ | ︙ | |||
529 530 531 532 533 534 535 | ** Equality constraint against the rowid: "=" ** A < or <= against the rowid: "<" ** A > or >= against the rowid: ">" ** ** This function ensures that there is at most one "r" or "=". And that if ** there exists an "=" then there is no "<" or ">". ** | | > < | | 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 | ** Equality constraint against the rowid: "=" ** A < or <= against the rowid: "<" ** A > or >= against the rowid: ">" ** ** This function ensures that there is at most one "r" or "=". And that if ** there exists an "=" then there is no "<" or ">". ** ** If an unusable MATCH operator is present in the WHERE clause, then ** SQLITE_CONSTRAINT is returned. ** ** Costs are assigned as follows: ** ** a) If a MATCH operator is present, the cost depends on the other ** constraints also present. As follows: ** ** * No other constraints: cost=1000.0 ** * One rowid range constraint: cost=750.0 ** * Both rowid range constraints: cost=500.0 |
︙ | ︙ | |||
565 566 567 568 569 570 571 | char *idxStr; int iIdxStr = 0; int iCons = 0; int bSeenEq = 0; int bSeenGt = 0; int bSeenLt = 0; | | | 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | char *idxStr; int iIdxStr = 0; int iCons = 0; int bSeenEq = 0; int bSeenGt = 0; int bSeenLt = 0; int nSeenMatch = 0; int bSeenRank = 0; assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH ); assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH ); assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH ); assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH ); |
︙ | ︙ | |||
596 597 598 599 600 601 602 | int iCol = p->iColumn; if( p->op==SQLITE_INDEX_CONSTRAINT_MATCH || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol>=nCol) ){ /* A MATCH operator or equivalent */ if( p->usable==0 || iCol<0 ){ /* As there exists an unusable MATCH constraint this is an | | < < < | | | | 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 | int iCol = p->iColumn; if( p->op==SQLITE_INDEX_CONSTRAINT_MATCH || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol>=nCol) ){ /* A MATCH operator or equivalent */ if( p->usable==0 || iCol<0 ){ /* As there exists an unusable MATCH constraint this is an ** unusable plan. Return SQLITE_CONSTRAINT. */ return SQLITE_CONSTRAINT; }else{ if( iCol==nCol+1 ){ if( bSeenRank ) continue; idxStr[iIdxStr++] = 'r'; bSeenRank = 1; }else if( iCol>=0 ){ nSeenMatch++; idxStr[iIdxStr++] = 'M'; sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol); idxStr += strlen(&idxStr[iIdxStr]); assert( idxStr[iIdxStr]=='\0' ); } pInfo->aConstraintUsage[i].argvIndex = ++iCons; pInfo->aConstraintUsage[i].omit = 1; } }else if( p->usable ){ if( iCol>=0 && iCol<nCol && fts5UsePatternMatch(pConfig, p) ){ assert( p->op==FTS5_PATTERN_LIKE || p->op==FTS5_PATTERN_GLOB ); idxStr[iIdxStr++] = p->op==FTS5_PATTERN_LIKE ? 'L' : 'G'; sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol); idxStr += strlen(&idxStr[iIdxStr]); pInfo->aConstraintUsage[i].argvIndex = ++iCons; assert( idxStr[iIdxStr]=='\0' ); nSeenMatch++; }else if( bSeenEq==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol<0 ){ idxStr[iIdxStr++] = '='; bSeenEq = 1; pInfo->aConstraintUsage[i].argvIndex = ++iCons; } } } |
︙ | ︙ | |||
661 662 663 664 665 666 667 | /* Set idxFlags flags for the ORDER BY clause ** ** Note that tokendata=1 tables cannot currently handle "ORDER BY rowid DESC". */ if( pInfo->nOrderBy==1 ){ int iSort = pInfo->aOrderBy[0].iColumn; | | | | | | > > > | | 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 | /* Set idxFlags flags for the ORDER BY clause ** ** Note that tokendata=1 tables cannot currently handle "ORDER BY rowid DESC". */ if( pInfo->nOrderBy==1 ){ int iSort = pInfo->aOrderBy[0].iColumn; if( iSort==(pConfig->nCol+1) && nSeenMatch>0 ){ idxFlags |= FTS5_BI_ORDER_RANK; }else if( iSort==-1 && (!pInfo->aOrderBy[0].desc || !pConfig->bTokendata) ){ idxFlags |= FTS5_BI_ORDER_ROWID; } if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID) ){ pInfo->orderByConsumed = 1; if( pInfo->aOrderBy[0].desc ){ idxFlags |= FTS5_BI_ORDER_DESC; } } } /* Calculate the estimated cost based on the flags set in idxFlags. */ if( bSeenEq ){ pInfo->estimatedCost = nSeenMatch ? 1000.0 : 10.0; if( nSeenMatch==0 ) fts5SetUniqueFlag(pInfo); }else if( bSeenLt && bSeenGt ){ pInfo->estimatedCost = nSeenMatch ? 5000.0 : 250000.0; }else if( bSeenLt || bSeenGt ){ pInfo->estimatedCost = nSeenMatch ? 7500.0 : 750000.0; }else{ pInfo->estimatedCost = nSeenMatch ? 10000.0 : 1000000.0; } for(i=1; i<nSeenMatch; i++){ pInfo->estimatedCost *= 0.4; } pInfo->idxNum = idxFlags; return SQLITE_OK; } static int fts5NewTransaction(Fts5FullTable *pTab){ |
︙ | ︙ |
Changes to ext/fts5/test/fts5colset.test.
︙ | ︙ | |||
75 76 77 78 79 80 81 | do_execsql_test 3.$tn " SELECT rowid FROM t1 WHERE $w " $res } do_catchsql_test 4.1 { SELECT * FROM t1 WHERE rowid MATCH 'a' | | | 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | do_execsql_test 3.$tn " SELECT rowid FROM t1 WHERE $w " $res } do_catchsql_test 4.1 { SELECT * FROM t1 WHERE rowid MATCH 'a' } {1 {no query solution}} } #------------------------------------------------------------------------- # Confirm that the expression parser creates the same expression tree # for: # # {a b} : (abc AND def) |
︙ | ︙ |
Changes to ext/fts5/test/fts5misc.test.
︙ | ︙ | |||
530 531 532 533 534 535 536 537 538 539 | SAVEPOINT one; INSERT INTO t2 VALUES('one'); INSERT INTO t2 VALUES('two'); INSERT INTO t2 VALUES('three'); ROLLBACK TO one; COMMIT; } finish_test | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 | SAVEPOINT one; INSERT INTO t2 VALUES('one'); INSERT INTO t2 VALUES('two'); INSERT INTO t2 VALUES('three'); ROLLBACK TO one; COMMIT; } #------------------------------------------------------------------------- reset_db do_execsql_test 20.0 { CREATE VIRTUAL TABLE x1 USING fts5(a); INSERT INTO x1(rowid, a) VALUES (1, 'a b c d'), (2, 'x b c d'), (3, 'x y z d'), (4, 'a y c x'); } do_execsql_test 20.1 { SELECT rowid FROM x1 WHERE x1 MATCH 'a' AND x1 MATCH 'b'; } {1} do_execsql_test 20.2 { SELECT rowid FROM x1 WHERE x1 MATCH 'a' AND x1 MATCH 'y'; } {4} do_execsql_test 20.3 { SELECT rowid FROM x1 WHERE x1 MATCH 'a' OR x1 MATCH 'y'; } {1 4 3} do_execsql_test 20.4 { SELECT rowid FROM x1 WHERE x1 MATCH 'a' OR (x1 MATCH 'y' AND x1 MATCH 'd'); } {1 4 3} do_execsql_test 20.5 { SELECT rowid FROM x1 WHERE x1 MATCH 'z' OR (x1 MATCH 'a' AND x1 MATCH 'd'); } {3 1} finish_test |
Changes to src/test_bestindex.c.
︙ | ︙ | |||
301 302 303 304 305 306 307 | int ii; int rc; pScript = Tcl_DuplicateObj(pTab->pCmd); Tcl_IncrRefCount(pScript); Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xFilter", -1)); Tcl_ListObjAppendElement(interp, pScript, Tcl_NewIntObj(idxNum)); | < | < | < > | 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | int ii; int rc; pScript = Tcl_DuplicateObj(pTab->pCmd); Tcl_IncrRefCount(pScript); Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xFilter", -1)); Tcl_ListObjAppendElement(interp, pScript, Tcl_NewIntObj(idxNum)); Tcl_ListObjAppendElement( interp, pScript, Tcl_NewStringObj(idxStr ? idxStr : "", -1) ); pArg = Tcl_NewObj(); Tcl_IncrRefCount(pArg); for(ii=0; ii<argc; ii++){ const char *zVal = (const char*)sqlite3_value_text(argv[ii]); Tcl_Obj *pVal; if( zVal==0 ){ |
︙ | ︙ | |||
526 527 528 529 530 531 532 533 534 535 536 537 538 539 | const char *azSub[] = { "constraints", /* 0 */ "orderby", /* 1 */ "mask", /* 2 */ "distinct", /* 3 */ "in", /* 4 */ "rhs_value", /* 5 */ 0 }; int ii; sqlite3_index_info *pIdxInfo = (sqlite3_index_info*)clientData; if( objc<2 ){ Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND"); | > | 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | const char *azSub[] = { "constraints", /* 0 */ "orderby", /* 1 */ "mask", /* 2 */ "distinct", /* 3 */ "in", /* 4 */ "rhs_value", /* 5 */ "collation", /* 6 */ 0 }; int ii; sqlite3_index_info *pIdxInfo = (sqlite3_index_info*)clientData; if( objc<2 ){ Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND"); |
︙ | ︙ | |||
606 607 608 609 610 611 612 613 614 615 616 617 618 619 | zVal = (const char*)sqlite3_value_text(pVal); }else if( objc==4 ){ zVal = Tcl_GetString(objv[3]); } Tcl_SetObjResult(interp, Tcl_NewStringObj(zVal, -1)); break; } } return TCL_OK; } static int tclBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ tcl_vtab *pTab = (tcl_vtab*)tab; | > > > > > > > > > > > | 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 | zVal = (const char*)sqlite3_value_text(pVal); }else if( objc==4 ){ zVal = Tcl_GetString(objv[3]); } Tcl_SetObjResult(interp, Tcl_NewStringObj(zVal, -1)); break; } case 6: assert( sqlite3_stricmp(azSub[ii], "collation")==0 ); { int iCons = 0; const char *zColl = ""; if( Tcl_GetIntFromObj(interp, objv[2], &iCons) ){ return TCL_ERROR; } zColl = sqlite3_vtab_collation(pIdxInfo, iCons); Tcl_SetObjResult(interp, Tcl_NewStringObj(zColl, -1)); break; } } return TCL_OK; } static int tclBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ tcl_vtab *pTab = (tcl_vtab*)tab; |
︙ | ︙ | |||
696 697 698 699 700 701 702 703 704 705 706 707 708 709 | pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %d", iCons); }else{ int bOmit = (zCmd[0]=='o' || zCmd[0]=='O'); pIdxInfo->aConstraintUsage[iCons].argvIndex = iArgv++; pIdxInfo->aConstraintUsage[iCons].omit = bOmit; } } }else{ rc = SQLITE_ERROR; pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zCmd); } if( rc!=SQLITE_OK && pTab->base.zErrMsg==0 ){ const char *zErr = Tcl_GetStringResult(interp); pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr); | > > > > | 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 | pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %d", iCons); }else{ int bOmit = (zCmd[0]=='o' || zCmd[0]=='O'); pIdxInfo->aConstraintUsage[iCons].argvIndex = iArgv++; pIdxInfo->aConstraintUsage[iCons].omit = bOmit; } } }else if( sqlite3_stricmp("constraint", zCmd)==0 ){ rc = SQLITE_CONSTRAINT; pTab->base.zErrMsg = sqlite3_mprintf("%s", Tcl_GetString(p)); }else{ rc = SQLITE_ERROR; pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zCmd); } if( rc!=SQLITE_OK && pTab->base.zErrMsg==0 ){ const char *zErr = Tcl_GetStringResult(interp); pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr); |
︙ | ︙ |
Changes to src/where.c.
︙ | ︙ | |||
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 | sqlite3VdbeJumpHere(v, addrOnce); pParse->pIdxEpr = saved_pIdxEpr; pParse->pIdxPartExpr = saved_pIdxPartExpr; } #ifndef SQLITE_OMIT_VIRTUALTABLE /* ** Allocate and populate an sqlite3_index_info structure. It is the ** responsibility of the caller to eventually release the structure ** by passing the pointer returned by this function to freeIndexInfo(). */ static sqlite3_index_info *allocateIndexInfo( WhereInfo *pWInfo, /* The WHERE clause */ | > > > > > > > > > > > > > > | 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 | sqlite3VdbeJumpHere(v, addrOnce); pParse->pIdxEpr = saved_pIdxEpr; pParse->pIdxPartExpr = saved_pIdxPartExpr; } #ifndef SQLITE_OMIT_VIRTUALTABLE /* ** Return term iTerm of the WhereClause passed as the first argument. Terms ** are numbered from 0 upwards, starting with the terms in pWC->a[], then ** those in pWC->pOuter->a[] (if any), and so on. */ static WhereTerm *termFromWhereClause(WhereClause *pWC, int iTerm){ WhereClause *p; for(p=pWC; p; p=p->pOuter){ if( iTerm<p->nTerm ) return &p->a[iTerm]; iTerm -= p->nTerm; } return 0; } /* ** Allocate and populate an sqlite3_index_info structure. It is the ** responsibility of the caller to eventually release the structure ** by passing the pointer returned by this function to freeIndexInfo(). */ static sqlite3_index_info *allocateIndexInfo( WhereInfo *pWInfo, /* The WHERE clause */ |
︙ | ︙ | |||
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 | WhereTerm *pTerm; int nOrderBy; sqlite3_index_info *pIdxInfo; u16 mNoOmit = 0; const Table *pTab; int eDistinct = 0; ExprList *pOrderBy = pWInfo->pOrderBy; assert( pSrc!=0 ); pTab = pSrc->pTab; assert( pTab!=0 ); assert( IsVirtual(pTab) ); /* Find all WHERE clause constraints referring to this virtual table. ** Mark each term with the TERM_OK flag. Set nTerm to the number of ** terms found. */ | > > | | | | | | | | | | | | | | | | | | | | | > | 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 | WhereTerm *pTerm; int nOrderBy; sqlite3_index_info *pIdxInfo; u16 mNoOmit = 0; const Table *pTab; int eDistinct = 0; ExprList *pOrderBy = pWInfo->pOrderBy; WhereClause *p; assert( pSrc!=0 ); pTab = pSrc->pTab; assert( pTab!=0 ); assert( IsVirtual(pTab) ); /* Find all WHERE clause constraints referring to this virtual table. ** Mark each term with the TERM_OK flag. Set nTerm to the number of ** terms found. */ for(p=pWC, nTerm=0; p; p=p->pOuter){ for(i=0, pTerm=p->a; i<p->nTerm; i++, pTerm++){ pTerm->wtFlags &= ~TERM_OK; if( pTerm->leftCursor != pSrc->iCursor ) continue; if( pTerm->prereqRight & mUnusable ) continue; assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); testcase( pTerm->eOperator & WO_IN ); testcase( pTerm->eOperator & WO_ISNULL ); testcase( pTerm->eOperator & WO_IS ); testcase( pTerm->eOperator & WO_ALL ); if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue; if( pTerm->wtFlags & TERM_VNULL ) continue; assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); assert( pTerm->u.x.leftColumn>=XN_ROWID ); assert( pTerm->u.x.leftColumn<pTab->nCol ); if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 && !constraintCompatibleWithOuterJoin(pTerm,pSrc) ){ continue; } nTerm++; pTerm->wtFlags |= TERM_OK; } } /* If the ORDER BY clause contains only columns in the current ** virtual table then allocate space for the aOrderBy part of ** the sqlite3_index_info structure. */ nOrderBy = 0; |
︙ | ︙ | |||
1478 1479 1480 1481 1482 1483 1484 | pIdxInfo->aConstraint = pIdxCons; pIdxInfo->aOrderBy = pIdxOrderBy; pIdxInfo->aConstraintUsage = pUsage; pHidden->pWC = pWC; pHidden->pParse = pParse; pHidden->eDistinct = eDistinct; pHidden->mIn = 0; | > > | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | > > > > > > > > > > > > > | 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 | pIdxInfo->aConstraint = pIdxCons; pIdxInfo->aOrderBy = pIdxOrderBy; pIdxInfo->aConstraintUsage = pUsage; pHidden->pWC = pWC; pHidden->pParse = pParse; pHidden->eDistinct = eDistinct; pHidden->mIn = 0; for(p=pWC, i=j=0; p; p=p->pOuter){ int nLast = i+p->nTerm;; for(pTerm=p->a; i<nLast; i++, pTerm++){ u16 op; if( (pTerm->wtFlags & TERM_OK)==0 ) continue; pIdxCons[j].iColumn = pTerm->u.x.leftColumn; pIdxCons[j].iTermOffset = i; op = pTerm->eOperator & WO_ALL; if( op==WO_IN ){ if( (pTerm->wtFlags & TERM_SLICE)==0 ){ pHidden->mIn |= SMASKBIT32(j); } op = WO_EQ; } if( op==WO_AUX ){ pIdxCons[j].op = pTerm->eMatchOp; }else if( op & (WO_ISNULL|WO_IS) ){ if( op==WO_ISNULL ){ pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL; }else{ pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_IS; } }else{ pIdxCons[j].op = (u8)op; /* The direct assignment in the previous line is possible only because ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The ** following asserts verify this fact. */ assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) ); if( op & (WO_LT|WO_LE|WO_GT|WO_GE) && sqlite3ExprIsVector(pTerm->pExpr->pRight) ){ testcase( j!=i ); if( j<16 ) mNoOmit |= (1 << j); if( op==WO_LT ) pIdxCons[j].op = WO_LE; if( op==WO_GT ) pIdxCons[j].op = WO_GE; } } j++; } } assert( j==nTerm ); pIdxInfo->nConstraint = j; for(i=j=0; i<nOrderBy; i++){ Expr *pExpr = pOrderBy->a[i].pExpr; if( sqlite3ExprIsConstant(0, pExpr) ) continue; assert( pExpr->op==TK_COLUMN || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN && pExpr->iColumn==pExpr->pLeft->iColumn) ); pIdxOrderBy[j].iColumn = pExpr->iColumn; pIdxOrderBy[j].desc = pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC; j++; } pIdxInfo->nOrderBy = j; *pmNoOmit = mNoOmit; return pIdxInfo; } /* ** Free and zero the sqlite3_index_info.idxStr value if needed. */ static void freeIdxStr(sqlite3_index_info *pIdxInfo){ if( pIdxInfo->needToFreeIdxStr ){ sqlite3_free(pIdxInfo->idxStr); pIdxInfo->idxStr = 0; pIdxInfo->needToFreeIdxStr = 0; } } /* ** Free an sqlite3_index_info structure allocated by allocateIndexInfo() ** and possibly modified by xBestIndex methods. */ static void freeIndexInfo(sqlite3 *db, sqlite3_index_info *pIdxInfo){ HiddenIndexInfo *pHidden; int i; assert( pIdxInfo!=0 ); pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; assert( pHidden->pParse!=0 ); assert( pHidden->pParse->db==db ); for(i=0; i<pIdxInfo->nConstraint; i++){ sqlite3ValueFree(pHidden->aRhs[i]); /* IMP: R-14553-25174 */ pHidden->aRhs[i] = 0; } freeIdxStr(pIdxInfo); sqlite3DbFree(db, pIdxInfo); } /* ** The table object reference passed as the second argument to this function ** must represent a virtual table. This function invokes the xBestIndex() ** method of the virtual table with the sqlite3_index_info object that |
︙ | ︙ | |||
4155 4156 4157 4158 4159 4160 4161 | *pbIn = 0; pNew->prereq = mPrereq; /* Set the usable flag on the subset of constraints identified by ** arguments mUsable and mExclude. */ pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; for(i=0; i<nConstraint; i++, pIdxCons++){ | | | 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 | *pbIn = 0; pNew->prereq = mPrereq; /* Set the usable flag on the subset of constraints identified by ** arguments mUsable and mExclude. */ pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; for(i=0; i<nConstraint; i++, pIdxCons++){ WhereTerm *pTerm = termFromWhereClause(pWC, pIdxCons->iTermOffset); pIdxCons->usable = 0; if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight && (pTerm->eOperator & mExclude)==0 && (pbRetryLimit || !isLimitTerm(pTerm)) ){ pIdxCons->usable = 1; } |
︙ | ︙ | |||
4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 | if( rc ){ if( rc==SQLITE_CONSTRAINT ){ /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means ** that the particular combination of parameters provided is unusable. ** Make no entries in the loop table. */ WHERETRACE(0xffffffff, (" ^^^^--- non-viable plan rejected!\n")); return SQLITE_OK; } return rc; } mxTerm = -1; assert( pNew->nLSlot>=nConstraint ); memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint ); memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab)); pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; for(i=0; i<nConstraint; i++, pIdxCons++){ int iTerm; if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ WhereTerm *pTerm; int j = pIdxCons->iTermOffset; if( iTerm>=nConstraint || j<0 | > | | < | 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 | if( rc ){ if( rc==SQLITE_CONSTRAINT ){ /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means ** that the particular combination of parameters provided is unusable. ** Make no entries in the loop table. */ WHERETRACE(0xffffffff, (" ^^^^--- non-viable plan rejected!\n")); freeIdxStr(pIdxInfo); return SQLITE_OK; } return rc; } mxTerm = -1; assert( pNew->nLSlot>=nConstraint ); memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint ); memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab)); pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; for(i=0; i<nConstraint; i++, pIdxCons++){ int iTerm; if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ WhereTerm *pTerm; int j = pIdxCons->iTermOffset; if( iTerm>=nConstraint || j<0 || (pTerm = termFromWhereClause(pWC, j))==0 || pNew->aLTerm[iTerm]!=0 || pIdxCons->usable==0 ){ sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName); freeIdxStr(pIdxInfo); return SQLITE_ERROR; } testcase( iTerm==nConstraint-1 ); testcase( j==0 ); testcase( j==pWC->nTerm-1 ); pNew->prereq |= pTerm->prereqRight; assert( iTerm<pNew->nLSlot ); pNew->aLTerm[iTerm] = pTerm; if( iTerm>mxTerm ) mxTerm = iTerm; testcase( iTerm==15 ); testcase( iTerm==16 ); if( pUsage[i].omit ){ |
︙ | ︙ | |||
4259 4260 4261 4262 4263 4264 4265 | /* If there is an IN(...) term handled as an == (separate call to ** xFilter for each value on the RHS of the IN) and a LIMIT or ** OFFSET term handled as well, the plan is unusable. Similarly, ** if there is a LIMIT/OFFSET and there are other unused terms, ** the plan cannot be used. In these cases set variable *pbRetryLimit ** to true to tell the caller to retry with LIMIT and OFFSET ** disabled. */ | < < | < < | | 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 | /* If there is an IN(...) term handled as an == (separate call to ** xFilter for each value on the RHS of the IN) and a LIMIT or ** OFFSET term handled as well, the plan is unusable. Similarly, ** if there is a LIMIT/OFFSET and there are other unused terms, ** the plan cannot be used. In these cases set variable *pbRetryLimit ** to true to tell the caller to retry with LIMIT and OFFSET ** disabled. */ freeIdxStr(pIdxInfo); *pbRetryLimit = 1; return SQLITE_OK; } } } pNew->nLTerm = mxTerm+1; for(i=0; i<=mxTerm; i++){ if( pNew->aLTerm[i]==0 ){ /* The non-zero argvIdx values must be contiguous. Raise an ** error if they are not */ sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName); freeIdxStr(pIdxInfo); return SQLITE_ERROR; } } assert( pNew->nLTerm<=pNew->nLSlot ); pNew->u.vtab.idxNum = pIdxInfo->idxNum; pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; pIdxInfo->needToFreeIdxStr = 0; |
︙ | ︙ | |||
4331 4332 4333 4334 4335 4336 4337 | */ const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){ HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; const char *zRet = 0; if( iCons>=0 && iCons<pIdxInfo->nConstraint ){ CollSeq *pC = 0; int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset; | | | 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 | */ const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){ HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; const char *zRet = 0; if( iCons>=0 && iCons<pIdxInfo->nConstraint ){ CollSeq *pC = 0; int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset; Expr *pX = termFromWhereClause(pHidden->pWC, iTerm)->pExpr; if( pX->pLeft ){ pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX); } zRet = (pC ? pC->zName : sqlite3StrBINARY); } return zRet; } |
︙ | ︙ | |||
4377 4378 4379 4380 4381 4382 4383 | HiddenIndexInfo *pH = (HiddenIndexInfo*)&pIdxInfo[1]; sqlite3_value *pVal = 0; int rc = SQLITE_OK; if( iCons<0 || iCons>=pIdxInfo->nConstraint ){ rc = SQLITE_MISUSE_BKPT; /* EV: R-30545-25046 */ }else{ if( pH->aRhs[iCons]==0 ){ | > | > | 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 | HiddenIndexInfo *pH = (HiddenIndexInfo*)&pIdxInfo[1]; sqlite3_value *pVal = 0; int rc = SQLITE_OK; if( iCons<0 || iCons>=pIdxInfo->nConstraint ){ rc = SQLITE_MISUSE_BKPT; /* EV: R-30545-25046 */ }else{ if( pH->aRhs[iCons]==0 ){ WhereTerm *pTerm = termFromWhereClause( pH->pWC, pIdxInfo->aConstraint[iCons].iTermOffset ); rc = sqlite3ValueFromExpr( pH->pParse->db, pTerm->pExpr->pRight, ENC(pH->pParse->db), SQLITE_AFF_BLOB, &pH->aRhs[iCons] ); testcase( rc!=SQLITE_OK ); } pVal = pH->aRhs[iCons]; |
︙ | ︙ | |||
4533 4534 4535 4536 4537 4538 4539 | /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq) ** in the set of terms that apply to the current virtual table. */ while( rc==SQLITE_OK ){ int i; Bitmask mNext = ALLBITS; assert( mNext>0 ); for(i=0; i<nConstraint; i++){ | < | < > | 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 | /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq) ** in the set of terms that apply to the current virtual table. */ while( rc==SQLITE_OK ){ int i; Bitmask mNext = ALLBITS; assert( mNext>0 ); for(i=0; i<nConstraint; i++){ int iTerm = p->aConstraint[i].iTermOffset; Bitmask mThis = termFromWhereClause(pWC, iTerm)->prereqRight & ~mPrereq; if( mThis>mPrev && mThis<mNext ) mNext = mThis; } mPrev = mNext; if( mNext==ALLBITS ) break; if( mNext==mBest || mNext==mBestNoIn ) continue; WHERETRACE(0x800, (" VirtualOne: mPrev=%04llx mNext=%04llx\n", (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext)); |
︙ | ︙ | |||
4571 4572 4573 4574 4575 4576 4577 | if( rc==SQLITE_OK && seenZeroNoIN==0 ){ WHERETRACE(0x800, (" VirtualOne: all disabled and w/o IN\n")); rc = whereLoopAddVirtualOne( pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn, 0); } } | < | 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 | if( rc==SQLITE_OK && seenZeroNoIN==0 ){ WHERETRACE(0x800, (" VirtualOne: all disabled and w/o IN\n")); rc = whereLoopAddVirtualOne( pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn, 0); } } freeIndexInfo(pParse->db, p); WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc)); return rc; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ /* |
︙ | ︙ |
Changes to test/bestindexC.test.
︙ | ︙ | |||
205 206 207 208 209 210 211 212 213 | } {1 {declare_vtab: syntax error}} do_catchsql_test 4.3 { CREATE VIRTUAL TABLE y1 USING tcl(vtab_command "CREATE TABLE x1("); } {1 {declare_vtab: incomplete input}} do_catchsql_test 4.4 { CREATE VIRTUAL TABLE y1 USING tcl(vtab_command "CREATE TABLE x1(insert)"); } {1 {declare_vtab: near "insert": syntax error}} finish_test | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | } {1 {declare_vtab: syntax error}} do_catchsql_test 4.3 { CREATE VIRTUAL TABLE y1 USING tcl(vtab_command "CREATE TABLE x1("); } {1 {declare_vtab: incomplete input}} do_catchsql_test 4.4 { CREATE VIRTUAL TABLE y1 USING tcl(vtab_command "CREATE TABLE x1(insert)"); } {1 {declare_vtab: near "insert": syntax error}} #------------------------------------------------------------------------- reset_db register_tcl_module db proc quote {str} { return "'[string map {' ''} $str]'" } proc vtab_command {lVal method args} { switch -- $method { xConnect { return "CREATE TABLE t1(a, b, c, d)" } xBestIndex { set hdl [lindex $args 0] set clist [$hdl constraints] set res [list] set idx 0 set idxnum 0 set cols(0) a set cols(1) b set cols(2) c set lCons [list] foreach c $clist { array set a $c if {$a(usable)==0} continue if {($a(op)=="eq" || $a(op)=="is") && [info exists cols($a(column))]} { lappend res omit $idx set coll [$hdl collation $idx] lappend lCons "$cols($a(column)) = %[llength $lCons]% COLLATE $coll" set idxnum [expr {$idx + (1 << $a(column))}] catch { unset cols($a(column)) } } incr idx } if {[llength [array names cols]]>0} { set missing [list] for {set i 0} {$i < 3} {incr i} { catch { lappend missing $cols($i) } } set msg "missing required constraints: [join $missing ,]" return [list constraint $msg] } set idxstr [join $lCons " AND "] return "cost 1000 rows 1000 idxnum $idxnum $res idxstr {$idxstr}" } xFilter { foreach {idxnum idxstr lArg} $args {} set i 0 set where $idxstr foreach a $lArg { set where [string map [list %$i% [quote $a]] $where] incr i } # puts $where return [list sql "SELECT rowid, * FROM $lVal WHERE $where"] } } return {} } do_execsql_test 5.1 { CREATE VIRTUAL TABLE x1 USING tcl(vtab_command t1); CREATE TABLE t1(a, b, c, d); } foreach {tn where ok} { 0 "WHERE a=? AND b=? AND c=? AND c=?" 1 1 "WHERE a=? AND b=? AND c=?" 1 2 "WHERE a=? AND b=? AND (c=? OR c=?)" 1 3 "WHERE a=? AND b=? AND (c=? OR c=? OR c=?)" 1 4 "WHERE a=? AND b=? AND (c IS ? OR c IS ?)" 1 5 "WHERE a=? AND ((b=? AND c=?) OR (c=? AND b=?))" 1 6 "WHERE a=? AND ((b=? AND c=?) OR (c=?))" 0 } { do_test 5.2.$tn { catch { execsql "SELECT * FROM x1 $::where" } msg # if {$tn==0 || $tn==2 || $tn==3} { puts "MSG: $msg" } } [expr !$ok] } do_execsql_test 5.3 { SELECT * FROM x1 WHERE (a, b, c) = (?, ?, ?); } do_execsql_test 5.4 { INSERT INTO t1(rowid, a, b, c, d) VALUES(1, 'x', 'y', 'z', 'one'); INSERT INTO t1(rowid, a, b, c, d) VALUES(2, 'X', 'Y', 'Z', 'two'); SELECT * FROM x1 WHERE (a, b, c) = ('X', 'Y', 'Z'); } {X Y Z two} do_execsql_test 5.5 { SELECT * FROM x1 WHERE a='x' AND b='y' AND c='z'; } {x y z one} do_execsql_test 5.6 { SELECT * FROM x1 WHERE a='x' COLLATE nocase AND b='y' COLLATE nocase AND c='z'COLLATE nocase; } {x y z one X Y Z two} do_execsql_test 5.7 { DELETE FROM t1; INSERT INTO t1(rowid, a, b, c, d) VALUES(0, 'x', 'y', 'z', 'zero'); INSERT INTO t1(rowid, a, b, c, d) VALUES(1, 'x', 'y', 'Z', 'one'); INSERT INTO t1(rowid, a, b, c, d) VALUES(2, 'x', 'Y', 'z', 'two'); INSERT INTO t1(rowid, a, b, c, d) VALUES(3, 'x', 'Y', 'Z', 'three'); INSERT INTO t1(rowid, a, b, c, d) VALUES(4, 'X', 'y', 'z', 'four'); INSERT INTO t1(rowid, a, b, c, d) VALUES(5, 'X', 'y', 'Z', 'five'); INSERT INTO t1(rowid, a, b, c, d) VALUES(6, 'X', 'Y', 'z', 'six'); INSERT INTO t1(rowid, a, b, c, d) VALUES(7, 'X', 'Y', 'z', 'seven'); } do_execsql_test 5.8 { SELECT d FROM x1 WHERE a='x' AND ((b='y' AND c='z') OR (b='Y' AND c='z' COLLATE nocase)) } { zero two three } do_execsql_test 5.9 { SELECT d FROM x1 WHERE a='x' COLLATE nocase AND ((b='y' AND c='z') OR (b='Y' AND c='z' COLLATE nocase)) } { zero four two three six seven } finish_test |