Skip to content

Commit

Permalink
Merge "Expose SQL string to virtual tables" from Lucas C. Villa Real (#…
Browse files Browse the repository at this point in the history
…87)

"SQLite provides an interface to expose different data sources (such as
files or other database systems) as virtual tables. There are several
callbacks one must implement to enable e.g., connection handling and
pagination, but one key attribute is missing on all callbacks: the query
string being executed by the application.

The query string is especially relevant when virtual tables are used to
mirror data from external databases hosted on different machines. A
virtual table implementation that knows about the SQL string is able to
reduce the amount of data transferred over the network when an
application is only concerned about a subset of the columns.

This patch adds a new opcode VPrepareSql that's called immediately
before VFilter when dispatching queries to a virtual table. The new
opcode triggers the execution of the new xPrepareSql method which
provides the SQL text to the virtual table implementation."
  • Loading branch information
penberg authored Dec 21, 2022
2 parents 273a494 + 25e2204 commit 7beaa40
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/sqlite.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -7107,6 +7107,9 @@ struct sqlite3_module {
/* The methods above are in versions 1 and 2 of the sqlite_module object.
** Those below are for version 3 and greater. */
int (*xShadowName)(const char*);
/* The methods below relate to features contributed by the community and
** are available for version 700 and greater. */
int (*xPreparedSql)(sqlite3_vtab_cursor*, const char*);
};

/*
Expand Down
17 changes: 15 additions & 2 deletions src/test8.c
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,17 @@ static int echoRollbackTo(sqlite3_vtab *pVTab, int iSavepoint){
return SQLITE_OK;
}

static int echoShadowName(const char *name){
assert( name );
return SQLITE_OK;
}

static int echoPreparedSql(sqlite3_vtab_cursor *cur, const char *sql){
assert( cur );
assert( sql );
return SQLITE_OK;
}

/*
** A virtual table module that merely "echos" the contents of another
** table (like an SQL VIEW).
Expand Down Expand Up @@ -1321,7 +1332,7 @@ static sqlite3_module echoModule = {
};

static sqlite3_module echoModuleV2 = {
2, /* iVersion */
700, /* iVersion */
echoCreate,
echoConnect,
echoBestIndex,
Expand All @@ -1343,7 +1354,9 @@ static sqlite3_module echoModuleV2 = {
echoRename, /* xRename - rename the table */
echoSavepoint,
echoRelease,
echoRollbackTo
echoRollbackTo,
echoShadowName,
echoPreparedSql,
};

/*
Expand Down
33 changes: 33 additions & 0 deletions src/vdbe.c
Original file line number Diff line number Diff line change
Expand Up @@ -8055,6 +8055,39 @@ case OP_VInitIn: { /* out2, ncycle */
}
#endif /* SQLITE_OMIT_VIRTUALTABLE */

#ifndef SQLITE_OMIT_VIRTUALTABLE
/* Opcode: VPreparedSql P1 * * * *
**
** P1 is a cursor opened using VOpen.
**
** This opcode invokes the xPreparedSql method on the virtual table specified
** by P1. The SQL text parameter to xPreparedSql is obtained from Vdbe* `p`.
**
*/
case OP_VPreparedSql: {
const sqlite3_module *pModule;
sqlite3_vtab_cursor *pVCur;
sqlite3_vtab *pVtab;
VdbeCursor *pCur;

pCur = p->apCsr[pOp->p1];
assert( pCur!=0 );
assert( pCur->eCurType==CURTYPE_VTAB );
pVCur = pCur->uc.pVCur;
pVtab = pVCur->pVtab;
pModule = pVtab->pModule;

/* Invoke the xPreparedSql method */
if( pModule->iVersion>=700 ){
if( pModule->xPreparedSql && p->zSql ){
rc = pModule->xPreparedSql(pVCur, p->zSql);
if( rc ) goto abort_due_to_error;
}
}

break;
}
#endif /* SQLITE_OMIT_VIRTUALTABLE */

#ifndef SQLITE_OMIT_VIRTUALTABLE
/* Opcode: VFilter P1 P2 P3 P4 *
Expand Down
2 changes: 2 additions & 0 deletions src/wherecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,8 @@ Bitmask sqlite3WhereCodeOneLoopStart(
int addrNotFound;
int nConstraint = pLoop->nLTerm;

sqlite3VdbeAddOp1(v, OP_VPreparedSql, iCur);

iReg = sqlite3GetTempRange(pParse, nConstraint+2);
addrNotFound = pLevel->addrBrk;
for(j=0; j<nConstraint; j++){
Expand Down
40 changes: 40 additions & 0 deletions test/rowvaluevtab.test
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ ifcapable !vtab {

register_echo_module db

#############
# Test echo
#############

do_execsql_test 1.0 {
CREATE TABLE t1(a, b, c);
CREATE INDEX t1b ON t1(b);
Expand Down Expand Up @@ -92,4 +96,40 @@ do_vfilter4_test 1.4f {
SELECT a FROM e1 WHERE (b, c) IN ( VALUES(2, 2) )
} {{SELECT rowid, a, b, c FROM 't1' WHERE b = ?}}

#######################################################################
# Test echo_v2. We simply want to ensure that OP_VPreparedSql executes
#######################################################################

do_execsql_test 2.0 {
CREATE TABLE t2(a, b, c);
CREATE INDEX t2b ON t2(b);
INSERT INTO t2 VALUES('one', 1, 1);
INSERT INTO t2 VALUES('two', 1, 2);
INSERT INTO t2 VALUES('three', 1, 3);

WITH s(i) AS (
SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10000
) INSERT INTO t2 SELECT NULL, NULL, NULL FROM s;
CREATE VIRTUAL TABLE e2 USING echo_v2(t2);
}

proc do_vpreparedsql1_test {tn sql expected} {
set rc -1
db eval "explain $sql" {
if {$opcode=="VPreparedSql"} {
set rc 0
}
}
if {$rc != $expected} {
error "Unexpected result $rc, was hoping for $expected"
}
}

do_execsql_test 2.1 {
SELECT a FROM e2 WHERE (b, c) IN ( VALUES(1, 3) )
} {three}
do_vpreparedsql1_test 2.1f {
SELECT a FROM e2 WHERE (b, c) IN ( VALUES(2, 2) )
} {0}

finish_test

0 comments on commit 7beaa40

Please sign in to comment.