From 97601244e2760e942b55a573414221a97022ee4b Mon Sep 17 00:00:00 2001 From: drh <> Date: Mon, 15 Dec 2025 17:32:56 +0000 Subject: [PATCH] Enhance the vtablog extension so that it shows the value of sqlite3_vtab_distinct() in xBestIndex, and so that provides the new consume_order_by option that can cause xBestIndex to set the orderByConsumed flag. FossilOrigin-Name: b158fe929929ae209f2603b11a2c4f44ad9147f6b2ce09a10ec1f92429402631 --- ext/misc/vtablog.c | 54 ++++++++++++++++++++++++++++++++++++++++------ manifest | 12 +++++------ manifest.uuid | 2 +- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/ext/misc/vtablog.c b/ext/misc/vtablog.c index 2b3e303559..1e28495dea 100644 --- a/ext/misc/vtablog.c +++ b/ext/misc/vtablog.c @@ -14,6 +14,8 @@ ** on stdout when its key interfaces are called. This is intended for ** interactive analysis and debugging of virtual table interfaces. ** +** HOW TO COMPILE: +** ** To build this extension as a separately loaded shared library or ** DLL, use compiler command-lines similar to the following: ** @@ -21,7 +23,7 @@ ** (mac) clang -fPIC -dynamiclib vtablog.c -o vtablog.dylib ** (windows) cl vtablog.c -link -dll -out:vtablog.dll ** -** Usage example: +** USAGE EXAMPLE: ** ** .load ./vtablog ** CREATE VIRTUAL TABLE temp.log USING vtablog( @@ -29,6 +31,23 @@ ** rows=25 ** ); ** SELECT * FROM log; +** +** ARGUMENTS TO CREATE VIRTUAL TABLE: +** +** In "CREATE VIRTUAL TABLE temp.log AS vtablog(ARGS....)" statement, the +** ARGS argument is a list of key-value pairs that can be any of the +** following. +** +** schema=TEXT Text is a CREATE TABLE statement that defines +** the schema of the new virtual table. +** +** rows=N The table as N rows. +** +** consume_order_by=N If the left-most ORDER BY terms is ASC and +** against column N (where the leftmost column +** is #1) then set the orderByConsumed=1 flag in +** xBestIndex. Or if the left-most ORDER BY is +** DESC and against column -N, do likewise. */ #include "sqlite3ext.h" SQLITE_EXTENSION_INIT1 @@ -49,6 +68,8 @@ struct vtablog_vtab { char *zName; /* Table name. argv[2] of xConnect/xCreate */ int nRow; /* Number of rows in the table */ int nCursor; /* Number of cursors created */ + int iConsumeOB; /* Consume the ORDER BY clause if on column N-th + ** and consumeOB=N or consumeOB=(-N) and DESC */ }; /* vtablog_cursor is a subclass of sqlite3_vtab_cursor which will @@ -180,6 +201,7 @@ static int vtablogConnectCreate( int rc; char *zSchema = 0; char *zNRow = 0; + char *zConsumeOB = 0; printf("%s.%s.%s():\n", argv[1], argv[2], isCreate ? "xCreate" : "xConnect"); @@ -203,6 +225,10 @@ static int vtablogConnectCreate( rc = SQLITE_ERROR; goto vtablog_end_connect; } + if( vtablog_string_parameter(pzErr, "consume_order_by", z, &zConsumeOB) ){ + rc = SQLITE_ERROR; + goto vtablog_end_connect; + } } if( zSchema==0 ){ zSchema = sqlite3_mprintf("%s","CREATE TABLE x(a,b);"); @@ -221,6 +247,10 @@ static int vtablogConnectCreate( pNew->nRow = 10; if( zNRow ) pNew->nRow = atoi(zNRow); printf(" nrow = %d\n", pNew->nRow); + if( zConsumeOB ) pNew->iConsumeOB = atoi(zConsumeOB); + if( pNew->iConsumeOB ){ + printf(" consume_order_by = %d\n", pNew->iConsumeOB); + } pNew->zDb = sqlite3_mprintf("%s", argv[1]); pNew->zName = sqlite3_mprintf("%s", argv[2]); } @@ -228,6 +258,7 @@ static int vtablogConnectCreate( vtablog_end_connect: sqlite3_free(zSchema); sqlite3_free(zNRow); + sqlite3_free(zConsumeOB); return rc; } static int vtablogCreate( @@ -514,16 +545,27 @@ static int vtablogBestIndex( } } printf(" nOrderBy: %d\n", p->nOrderBy); - for(i=0; inOrderBy; i++){ - printf(" orderby[%d]: col=%d desc=%d\n", - i, - p->aOrderBy[i].iColumn, - p->aOrderBy[i].desc); + if( p->nOrderBy ){ + for(i=0; inOrderBy; i++){ + printf(" orderby[%d]: col=%d desc=%d\n", + i, + p->aOrderBy[i].iColumn, + p->aOrderBy[i].desc); + } + if( pTab->iConsumeOB ){ + int N = p->aOrderBy[0].iColumn+1; + if( (p->aOrderBy[0].desc && N==-pTab->iConsumeOB) + || (!p->aOrderBy[0].desc && N==pTab->iConsumeOB) + ){ + p->orderByConsumed = 1; + } + } } p->estimatedCost = (double)500; p->estimatedRows = 500; printf(" idxNum=%d\n", p->idxNum); printf(" idxStr=NULL\n"); + printf(" sqlite3_vtab_distinct()=%d\n", sqlite3_vtab_distinct(p)); printf(" orderByConsumed=%d\n", p->orderByConsumed); printf(" estimatedCost=%g\n", p->estimatedCost); printf(" estimatedRows=%lld\n", p->estimatedRows); diff --git a/manifest b/manifest index 8ab80233db..d2c6733baf 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Clarify\sthe\saffect\sof\snCharLimit,\snLineLimit,\sand\snTitleLimit\son\sthe\nxRender\soutput\sfrom\sQRF. -D 2025-12-15T13:58:41.145 +C Enhance\sthe\svtablog\sextension\sso\sthat\sit\sshows\sthe\svalue\sof\nsqlite3_vtab_distinct()\sin\sxBestIndex,\sand\sso\sthat\sprovides\sthe\snew\nconsume_order_by\soption\sthat\scan\scause\sxBestIndex\sto\sset\sthe\norderByConsumed\sflag. +D 2025-12-15T17:32:56.417 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea @@ -410,7 +410,7 @@ F ext/misc/uuid.c 5bb2264c1b64d163efa46509544fd7500cb8769cb7c16dd52052da8d961505 F ext/misc/vfslog.c 3932ab932eeb2601dbc4447cb14d445aaa9fbe43b863ef5f014401c3420afd20 F ext/misc/vfsstat.c 0b23c0a69a2b63dc0ef0af44f9c1fc977300c480a1f7a9814500369d8211f56e F ext/misc/vfstrace.c 0e4b8b17ac0675ea90f6d168d8214687e06ca3efbc0060aad4814994d82b41fb -F ext/misc/vtablog.c 2d04386c2f5a3bb93bc9ae978f0b7dcd5a264e126abd640dd6d82aa9067cbd48 +F ext/misc/vtablog.c 402496fb38add7dd2c50f2a0ad20f83a9916ceab48dcd31e62ad621e663c83ac F ext/misc/vtshim.c e5bce24ab8c532f4fdc600148718fe1802cb6ed57417f1c1032d8961f72b0e8f F ext/misc/wholenumber.c 0fa0c082676b7868bf2fa918e911133f2b349bcdceabd1198bba5f65b4fc0668 F ext/misc/windirent.h 02211ce51f3034c675f2dbf4d228194d51b3ee05734678bad5106fff6292e60c @@ -2184,8 +2184,8 @@ F tool/version-info.c 33d0390ef484b3b1cb685d59362be891ea162123cea181cb8e6d2cf6dd F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 F tool/warnings.sh d924598cf2f55a4ecbc2aeb055c10bd5f48114793e7ba25f9585435da29e7e98 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 5cf9e99ff8279530cb73a24794107591f8dab755fe78acd6c11429e532f3d1bf -R 61f1b2124f9d7a4acd1b4a3d4268eab3 +P 881534858de8bccffca53e7256a725378a98a354e792374d972b120880c2ae78 +R 516b4041a52e0f6a1c6a30026e823eb8 U drh -Z d5b44ebdba76bbdfb15b0fa610193449 +Z 2dd134e29e053422231bfb06d9c85c41 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 67f294f401..b1dafb2f7a 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -881534858de8bccffca53e7256a725378a98a354e792374d972b120880c2ae78 +b158fe929929ae209f2603b11a2c4f44ad9147f6b2ce09a10ec1f92429402631 -- 2.47.3