Skip to content

Commit 77d3bc7

Browse files
committed
wip: add RANDOM ROWID table option
With the new RANDOM ROWID keywords, a table can explicitly state that it wants its rowid to be generated randomly, not consecutively, without having to previously create a sentinel record with rowid == max(i64). Fixes #12
1 parent 83b01ee commit 77d3bc7

6 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/build.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2708,6 +2708,15 @@ void sqlite3EndTable(
27082708
p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
27092709
convertToWithoutRowidTable(pParse, p);
27102710
}
2711+
if( tabOpts & TF_RandomRowid ){
2712+
assert( (p->tabFlags & TF_WithoutRowid) == 0 );
2713+
if( (p->tabFlags & TF_Autoincrement) ){
2714+
sqlite3ErrorMsg(pParse,
2715+
"AUTOINCREMENT not allowed on RANDOM ROWID tables");
2716+
return;
2717+
}
2718+
p->tabFlags |= TF_RandomRowid;
2719+
}
27112720
iDb = sqlite3SchemaToIndex(db, p->pSchema);
27122721

27132722
#ifndef SQLITE_OMIT_CHECK

src/insert.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,10 @@ void sqlite3Insert(
818818
** sqlite_sequence table and store it in memory cell regAutoinc.
819819
*/
820820
regAutoinc = autoIncBegin(pParse, iDb, pTab);
821+
if (pTab->tabFlags & TF_RandomRowid) {
822+
fprintf(stderr, "Random rowid table detected: %s\n", pTab->zName);
823+
regAutoinc = 0xffffffff; // libSQL
824+
}
821825

822826
/* Allocate a block registers to hold the rowid and the values
823827
** for all columns of the new row.

src/parse.y

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ table_option(A) ::= WITHOUT nm(X). {
216216
sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
217217
}
218218
}
219+
table_option(A) ::= RANDOM nm(X). {
220+
if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
221+
A = TF_RandomRowid;
222+
}else{
223+
A = 0;
224+
sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
225+
}
226+
}
219227
table_option(A) ::= nm(X). {
220228
if( X.n==6 && sqlite3_strnicmp(X.z,"strict",6)==0 ){
221229
A = TF_Strict;
@@ -248,7 +256,7 @@ columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,A,Y);}
248256
CONFLICT DATABASE DEFERRED DESC DETACH DO
249257
EACH END EXCLUSIVE EXPLAIN FAIL FOR
250258
IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
251-
QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
259+
QUERY KEY OF OFFSET PRAGMA RAISE RANDOM RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
252260
ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
253261
NULLS FIRST LAST
254262
%ifdef SQLITE_OMIT_COMPOUND_SELECT

src/sqliteInt.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,6 +2341,8 @@ struct Table {
23412341
#define TF_Ephemeral 0x00004000 /* An ephemeral table */
23422342
#define TF_Eponymous 0x00008000 /* An eponymous virtual table */
23432343
#define TF_Strict 0x00010000 /* STRICT mode */
2344+
/* libSQL extension */
2345+
#define TF_RandomRowid 0x01000000 /* Random rowid */
23442346

23452347
/*
23462348
** Allowed values for Table.eTabType

src/vdbe.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5419,6 +5419,12 @@ case OP_NewRowid: { /* out2 */
54195419
}
54205420
}
54215421

5422+
if ( pOp->p3 == 0xffffffff) {
5423+
fprintf(stderr, "assigning random rowid\n");
5424+
pC->useRandomRowid = 1;
5425+
pOp->p3 = 0;
5426+
}
5427+
54225428
#ifndef SQLITE_OMIT_AUTOINCREMENT
54235429
if( pOp->p3 ){
54245430
/* Assert that P3 is a valid memory cell. */

tool/mkkeywordhash.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ static Keyword aKeywordTable[] = {
278278
{ "PRIMARY", "TK_PRIMARY", ALWAYS, 1 },
279279
{ "QUERY", "TK_QUERY", EXPLAIN, 0 },
280280
{ "RAISE", "TK_RAISE", TRIGGER, 1 },
281+
{ "RANDOM", "TK_RANDOM", ALWAYS, 1 },
281282
{ "RANGE", "TK_RANGE", WINDOWFUNC, 3 },
282283
{ "RECURSIVE", "TK_RECURSIVE", CTE, 3 },
283284
{ "REFERENCES", "TK_REFERENCES", FKEY, 1 },

0 commit comments

Comments
 (0)