2 ** 2015-08-18, 2023-04-28
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
13 ** This file demonstrates how to create a table-valued-function using
14 ** a virtual table. This demo implements the generate_series() function
15 ** which gives the same results as the eponymous function in PostgreSQL,
16 ** within the limitation that its arguments are signed 64-bit integers.
18 ** Considering its equivalents to generate_series(start,stop,step): A
19 ** value V[n] sequence is produced for integer n ascending from 0 where
20 ** ( V[n] == start + n * step && sgn(V[n] - stop) * sgn(step) >= 0 )
21 ** for each produced value (independent of production time ordering.)
23 ** All parameters must be either integer or convertable to integer.
24 ** The start parameter is required.
25 ** The stop parameter defaults to (1<<32)-1 (aka 4294967295 or 0xffffffff)
26 ** The step parameter defaults to 1 and 0 is treated as 1.
30 ** SELECT * FROM generate_series(0,100,5);
32 ** The query above returns integers from 0 through 100 counting by steps
35 ** SELECT * FROM generate_series(0,100);
37 ** Integers from 0 through 100 with a step size of 1.
39 ** SELECT * FROM generate_series(20) LIMIT 10;
41 ** Integers 20 through 29.
43 ** SELECT * FROM generate_series(0,-100,-5);
45 ** Integers 0 -5 -10 ... -100.
47 ** SELECT * FROM generate_series(0,-1);
53 ** The generate_series "function" is really a virtual table with the
56 ** CREATE TABLE generate_series(
63 ** The virtual table also has a rowid, logically equivalent to n+1 where
64 ** "n" is the ascending integer in the aforesaid production definition.
66 ** Function arguments in queries against this virtual table are translated
67 ** into equality constraints against successive hidden columns. In other
68 ** words, the following pairs of queries are equivalent to each other:
70 ** SELECT * FROM generate_series(0,100,5);
71 ** SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5;
73 ** SELECT * FROM generate_series(0,100);
74 ** SELECT * FROM generate_series WHERE start=0 AND stop=100;
76 ** SELECT * FROM generate_series(20) LIMIT 10;
77 ** SELECT * FROM generate_series WHERE start=20 LIMIT 10;
79 ** The generate_series virtual table implementation leaves the xCreate method
80 ** set to NULL. This means that it is not possible to do a CREATE VIRTUAL
81 ** TABLE command with "generate_series" as the USING argument. Instead, there
82 ** is a single generate_series virtual table that is always available without
83 ** having to be created first.
85 ** The xBestIndex method looks for equality constraints against the hidden
86 ** start, stop, and step columns, and if present, it uses those constraints
87 ** to bound the sequence of generated values. If the equality constraints
88 ** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step.
89 ** xBestIndex returns a small cost when both start and stop are available,
90 ** and a very large cost if either start or stop are unavailable. This
91 ** encourages the query planner to order joins such that the bounds of the
92 ** series are well-defined.
94 #include "sqlite3ext.h"
95 SQLITE_EXTENSION_INIT1
100 #ifndef SQLITE_OMIT_VIRTUALTABLE
102 ** Return that member of a generate_series(...) sequence whose 0-based
103 ** index is ix. The 0th member is given by smBase. The sequence members
104 ** progress per ix increment by smStep.
106 static sqlite3_int64
genSeqMember(sqlite3_int64 smBase
,
107 sqlite3_int64 smStep
,
109 if( ix
>=(sqlite3_uint64
)LLONG_MAX
){
110 /* Get ix into signed i64 range. */
111 ix
-= (sqlite3_uint64
)LLONG_MAX
;
112 /* With 2's complement ALU, this next can be 1 step, but is split into
113 * 2 for UBSAN's satisfaction (and hypothetical 1's complement ALUs.) */
114 smBase
+= (LLONG_MAX
/2) * smStep
;
115 smBase
+= (LLONG_MAX
- LLONG_MAX
/2) * smStep
;
117 /* Under UBSAN (or on 1's complement machines), must do this last term
118 * in steps to avoid the dreaded (and harmless) signed multiply overlow. */
120 sqlite3_int64 ix2
= (sqlite3_int64
)ix
/2;
121 smBase
+= ix2
*smStep
;
124 return smBase
+ ((sqlite3_int64
)ix
)*smStep
;
127 typedef unsigned char u8
;
129 typedef struct SequenceSpec
{
130 sqlite3_int64 iBase
; /* Starting value ("start") */
131 sqlite3_int64 iTerm
; /* Given terminal value ("stop") */
132 sqlite3_int64 iStep
; /* Increment ("step") */
133 sqlite3_uint64 uSeqIndexMax
; /* maximum sequence index (aka "n") */
134 sqlite3_uint64 uSeqIndexNow
; /* Current index during generation */
135 sqlite3_int64 iValueNow
; /* Current value during generation */
136 u8 isNotEOF
; /* Sequence generation not exhausted */
137 u8 isReversing
; /* Sequence is being reverse generated */
141 ** Prepare a SequenceSpec for use in generating an integer series
142 ** given initialized iBase, iTerm and iStep values. Sequence is
143 ** initialized per given isReversing. Other members are computed.
145 static void setupSequence( SequenceSpec
*pss
){
147 pss
->uSeqIndexMax
= 0;
149 bSameSigns
= (pss
->iBase
< 0)==(pss
->iTerm
< 0);
150 if( pss
->iTerm
< pss
->iBase
){
151 sqlite3_uint64 nuspan
= 0;
153 nuspan
= (sqlite3_uint64
)(pss
->iBase
- pss
->iTerm
);
155 /* Under UBSAN (or on 1's complement machines), must do this in steps.
156 * In this clause, iBase>=0 and iTerm<0 . */
158 nuspan
+= pss
->iBase
;
159 nuspan
+= -(pss
->iTerm
+1);
163 if( nuspan
==ULONG_MAX
){
164 pss
->uSeqIndexMax
= ( pss
->iStep
>LLONG_MIN
)? nuspan
/-pss
->iStep
: 1;
165 }else if( pss
->iStep
>LLONG_MIN
){
166 pss
->uSeqIndexMax
= nuspan
/-pss
->iStep
;
169 }else if( pss
->iTerm
> pss
->iBase
){
170 sqlite3_uint64 puspan
= 0;
172 puspan
= (sqlite3_uint64
)(pss
->iTerm
- pss
->iBase
);
174 /* Under UBSAN (or on 1's complement machines), must do this in steps.
175 * In this clause, iTerm>=0 and iBase<0 . */
177 puspan
+= pss
->iTerm
;
178 puspan
+= -(pss
->iBase
+1);
182 pss
->uSeqIndexMax
= puspan
/pss
->iStep
;
184 }else if( pss
->iTerm
== pss
->iBase
){
186 pss
->uSeqIndexMax
= 0;
188 pss
->uSeqIndexNow
= (pss
->isReversing
)? pss
->uSeqIndexMax
: 0;
189 pss
->iValueNow
= (pss
->isReversing
)
190 ? genSeqMember(pss
->iBase
, pss
->iStep
, pss
->uSeqIndexMax
)
195 ** Progress sequence generator to yield next value, if any.
196 ** Leave its state to either yield next value or be at EOF.
197 ** Return whether there is a next value, or 0 at EOF.
199 static int progressSequence( SequenceSpec
*pss
){
200 if( !pss
->isNotEOF
) return 0;
201 if( pss
->isReversing
){
202 if( pss
->uSeqIndexNow
> 0 ){
204 pss
->iValueNow
-= pss
->iStep
;
209 if( pss
->uSeqIndexNow
< pss
->uSeqIndexMax
){
211 pss
->iValueNow
+= pss
->iStep
;
216 return pss
->isNotEOF
;
219 /* series_cursor is a subclass of sqlite3_vtab_cursor which will
220 ** serve as the underlying representation of a cursor that scans
221 ** over rows of the result
223 typedef struct series_cursor series_cursor
;
224 struct series_cursor
{
225 sqlite3_vtab_cursor base
; /* Base class - must be first */
226 SequenceSpec ss
; /* (this) Derived class data */
230 ** The seriesConnect() method is invoked to create a new
231 ** series_vtab that describes the generate_series virtual table.
233 ** Think of this routine as the constructor for series_vtab objects.
235 ** All this routine needs to do is:
237 ** (1) Allocate the series_vtab object and initialize all fields.
239 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
240 ** result set of queries against generate_series will look like.
242 static int seriesConnect(
245 int argcUnused
, const char *const*argvUnused
,
246 sqlite3_vtab
**ppVtab
,
253 #define SERIES_COLUMN_VALUE 0
254 #define SERIES_COLUMN_START 1
255 #define SERIES_COLUMN_STOP 2
256 #define SERIES_COLUMN_STEP 3
262 rc
= sqlite3_declare_vtab(db
,
263 "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");
265 pNew
= *ppVtab
= sqlite3_malloc( sizeof(*pNew
) );
266 if( pNew
==0 ) return SQLITE_NOMEM
;
267 memset(pNew
, 0, sizeof(*pNew
));
268 sqlite3_vtab_config(db
, SQLITE_VTAB_INNOCUOUS
);
274 ** This method is the destructor for series_cursor objects.
276 static int seriesDisconnect(sqlite3_vtab
*pVtab
){
282 ** Constructor for a new series_cursor object.
284 static int seriesOpen(sqlite3_vtab
*pUnused
, sqlite3_vtab_cursor
**ppCursor
){
287 pCur
= sqlite3_malloc( sizeof(*pCur
) );
288 if( pCur
==0 ) return SQLITE_NOMEM
;
289 memset(pCur
, 0, sizeof(*pCur
));
290 *ppCursor
= &pCur
->base
;
295 ** Destructor for a series_cursor.
297 static int seriesClose(sqlite3_vtab_cursor
*cur
){
304 ** Advance a series_cursor to its next row of output.
306 static int seriesNext(sqlite3_vtab_cursor
*cur
){
307 series_cursor
*pCur
= (series_cursor
*)cur
;
308 progressSequence( & pCur
->ss
);
313 ** Return values of columns for the row at which the series_cursor
314 ** is currently pointing.
316 static int seriesColumn(
317 sqlite3_vtab_cursor
*cur
, /* The cursor */
318 sqlite3_context
*ctx
, /* First argument to sqlite3_result_...() */
319 int i
/* Which column to return */
321 series_cursor
*pCur
= (series_cursor
*)cur
;
324 case SERIES_COLUMN_START
: x
= pCur
->ss
.iBase
; break;
325 case SERIES_COLUMN_STOP
: x
= pCur
->ss
.iTerm
; break;
326 case SERIES_COLUMN_STEP
: x
= pCur
->ss
.iStep
; break;
327 default: x
= pCur
->ss
.iValueNow
; break;
329 sqlite3_result_int64(ctx
, x
);
334 ** Return the rowid for the current row, logically equivalent to n+1 where
335 ** "n" is the ascending integer in the aforesaid production definition.
337 static int seriesRowid(sqlite3_vtab_cursor
*cur
, sqlite_int64
*pRowid
){
338 series_cursor
*pCur
= (series_cursor
*)cur
;
339 sqlite3_uint64 n
= pCur
->ss
.uSeqIndexNow
;
340 *pRowid
= (sqlite3_int64
)((n
<0xffffffffffffffff)? n
+1 : 0);
345 ** Return TRUE if the cursor has been moved off of the last
348 static int seriesEof(sqlite3_vtab_cursor
*cur
){
349 series_cursor
*pCur
= (series_cursor
*)cur
;
350 return !pCur
->ss
.isNotEOF
;
353 /* True to cause run-time checking of the start=, stop=, and/or step=
354 ** parameters. The only reason to do this is for testing the
355 ** constraint checking logic for virtual tables in the SQLite core.
357 #ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
358 # define SQLITE_SERIES_CONSTRAINT_VERIFY 0
362 ** This method is called to "rewind" the series_cursor object back
363 ** to the first row of output. This method is always called at least
364 ** once prior to any call to seriesColumn() or seriesRowid() or
367 ** The query plan selected by seriesBestIndex is passed in the idxNum
368 ** parameter. (idxStr is not used in this implementation.) idxNum
369 ** is a bitmask showing which constraints are available:
375 ** Also, if bit 8 is set, that means that the series should be output
376 ** in descending order rather than in ascending order. If bit 16 is
377 ** set, then output must appear in ascending order.
379 ** This routine should initialize the cursor and position it so that it
380 ** is pointing at the first row, or pointing off the end of the table
381 ** (so that seriesEof() will return true) if the table is empty.
383 static int seriesFilter(
384 sqlite3_vtab_cursor
*pVtabCursor
,
385 int idxNum
, const char *idxStrUnused
,
386 int argc
, sqlite3_value
**argv
388 series_cursor
*pCur
= (series_cursor
*)pVtabCursor
;
392 pCur
->ss
.iBase
= sqlite3_value_int64(argv
[i
++]);
397 pCur
->ss
.iTerm
= sqlite3_value_int64(argv
[i
++]);
399 pCur
->ss
.iTerm
= 0xffffffff;
402 pCur
->ss
.iStep
= sqlite3_value_int64(argv
[i
++]);
403 if( pCur
->ss
.iStep
==0 ){
405 }else if( pCur
->ss
.iStep
<0 ){
406 if( (idxNum
& 16)==0 ) idxNum
|= 8;
411 for(i
=0; i
<argc
; i
++){
412 if( sqlite3_value_type(argv
[i
])==SQLITE_NULL
){
413 /* If any of the constraints have a NULL value, then return no rows.
414 ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
422 pCur
->ss
.isReversing
= pCur
->ss
.iStep
> 0;
424 pCur
->ss
.isReversing
= pCur
->ss
.iStep
< 0;
426 setupSequence( &pCur
->ss
);
431 ** SQLite will invoke this method one or more times while planning a query
432 ** that uses the generate_series virtual table. This routine needs to create
433 ** a query plan for each invocation and compute an estimated cost for that
436 ** In this implementation idxNum is used to represent the
437 ** query plan. idxStr is unused.
439 ** The query plan is represented by bits in idxNum:
441 ** (1) start = $value -- constraint exists
442 ** (2) stop = $value -- constraint exists
443 ** (4) step = $value -- constraint exists
444 ** (8) output in descending order
446 static int seriesBestIndex(
448 sqlite3_index_info
*pIdxInfo
450 int i
, j
; /* Loop over constraints */
451 int idxNum
= 0; /* The query plan bitmask */
452 int bStartSeen
= 0; /* EQ constraint seen on the START column */
453 int unusableMask
= 0; /* Mask of unusable constraints */
454 int nArg
= 0; /* Number of arguments that seriesFilter() expects */
455 int aIdx
[3]; /* Constraints on start, stop, and step */
456 const struct sqlite3_index_constraint
*pConstraint
;
458 /* This implementation assumes that the start, stop, and step columns
459 ** are the last three columns in the virtual table. */
460 assert( SERIES_COLUMN_STOP
== SERIES_COLUMN_START
+1 );
461 assert( SERIES_COLUMN_STEP
== SERIES_COLUMN_START
+2 );
463 aIdx
[0] = aIdx
[1] = aIdx
[2] = -1;
464 pConstraint
= pIdxInfo
->aConstraint
;
465 for(i
=0; i
<pIdxInfo
->nConstraint
; i
++, pConstraint
++){
466 int iCol
; /* 0 for start, 1 for stop, 2 for step */
467 int iMask
; /* bitmask for those column */
468 if( pConstraint
->iColumn
<SERIES_COLUMN_START
) continue;
469 iCol
= pConstraint
->iColumn
- SERIES_COLUMN_START
;
470 assert( iCol
>=0 && iCol
<=2 );
472 if( iCol
==0 ) bStartSeen
= 1;
473 if( pConstraint
->usable
==0 ){
474 unusableMask
|= iMask
;
476 }else if( pConstraint
->op
==SQLITE_INDEX_CONSTRAINT_EQ
){
482 if( (j
= aIdx
[i
])>=0 ){
483 pIdxInfo
->aConstraintUsage
[j
].argvIndex
= ++nArg
;
484 pIdxInfo
->aConstraintUsage
[j
].omit
= !SQLITE_SERIES_CONSTRAINT_VERIFY
;
487 /* The current generate_column() implementation requires at least one
488 ** argument (the START value). Legacy versions assumed START=0 if the
489 ** first argument was omitted. Compile with -DZERO_ARGUMENT_GENERATE_SERIES
490 ** to obtain the legacy behavior */
491 #ifndef ZERO_ARGUMENT_GENERATE_SERIES
493 sqlite3_free(pVTab
->zErrMsg
);
494 pVTab
->zErrMsg
= sqlite3_mprintf(
495 "first argument to \"generate_series()\" missing or unusable");
499 if( (unusableMask
& ~idxNum
)!=0 ){
500 /* The start, stop, and step columns are inputs. Therefore if there
501 ** are unusable constraints on any of start, stop, or step then
502 ** this plan is unusable */
503 return SQLITE_CONSTRAINT
;
505 if( (idxNum
& 3)==3 ){
506 /* Both start= and stop= boundaries are available. This is the
507 ** the preferred case */
508 pIdxInfo
->estimatedCost
= (double)(2 - ((idxNum
&4)!=0));
509 pIdxInfo
->estimatedRows
= 1000;
510 if( pIdxInfo
->nOrderBy
>=1 && pIdxInfo
->aOrderBy
[0].iColumn
==0 ){
511 if( pIdxInfo
->aOrderBy
[0].desc
){
516 pIdxInfo
->orderByConsumed
= 1;
519 /* If either boundary is missing, we have to generate a huge span
520 ** of numbers. Make this case very expensive so that the query
521 ** planner will work hard to avoid it. */
522 pIdxInfo
->estimatedRows
= 2147483647;
524 pIdxInfo
->idxNum
= idxNum
;
529 ** This following structure defines all the methods for the
530 ** generate_series virtual table.
532 static sqlite3_module seriesModule
= {
535 seriesConnect
, /* xConnect */
536 seriesBestIndex
, /* xBestIndex */
537 seriesDisconnect
, /* xDisconnect */
539 seriesOpen
, /* xOpen - open a cursor */
540 seriesClose
, /* xClose - close a cursor */
541 seriesFilter
, /* xFilter - configure scan constraints */
542 seriesNext
, /* xNext - advance a cursor */
543 seriesEof
, /* xEof - check for end of scan */
544 seriesColumn
, /* xColumn - read data */
545 seriesRowid
, /* xRowid - read data */
559 #endif /* SQLITE_OMIT_VIRTUALTABLE */
562 __declspec(dllexport
)
564 int sqlite3_series_init(
567 const sqlite3_api_routines
*pApi
570 SQLITE_EXTENSION_INIT2(pApi
);
571 #ifndef SQLITE_OMIT_VIRTUALTABLE
572 if( sqlite3_libversion_number()<3008012 && pzErrMsg
!=0 ){
573 *pzErrMsg
= sqlite3_mprintf(
574 "generate_series() requires SQLite 3.8.12 or later");
577 rc
= sqlite3_create_module(db
, "generate_series", &seriesModule
, 0);