Skip to content

Commit

Permalink
wip: add RANDOM ROWID table option
Browse files Browse the repository at this point in the history
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 tursodatabase#12
  • Loading branch information
psarna committed Oct 27, 2022
1 parent 83b01ee commit ffe9707
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/build.c
Original file line number Diff line number Diff line change
Expand Up @@ -2708,6 +2708,10 @@ void sqlite3EndTable(
p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
convertToWithoutRowidTable(pParse, p);
}
if( tabOpts & TF_RandomRowid ){
assert( (p->tabFlags & TF_WithoutRowid) == 0 );
p->tabFlags |= TF_RandomRowid;
}
iDb = sqlite3SchemaToIndex(db, p->pSchema);

#ifndef SQLITE_OMIT_CHECK
Expand Down
4 changes: 4 additions & 0 deletions src/insert.c
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,10 @@ void sqlite3Insert(
** sqlite_sequence table and store it in memory cell regAutoinc.
*/
regAutoinc = autoIncBegin(pParse, iDb, pTab);
if (pTab->tabFlags & TF_RandomRowid) {
fprintf(stderr, "Random rowid table detected: %s\n", pTab->zName);
regAutoinc = 0xffffffff; // libSQL
}

/* Allocate a block registers to hold the rowid and the values
** for all columns of the new row.
Expand Down
10 changes: 9 additions & 1 deletion src/parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ table_option(A) ::= WITHOUT nm(X). {
sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
}
}
table_option(A) ::= RANDOM nm(X). {
if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
A = TF_RandomRowid;
}else{
A = 0;
sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
}
}
table_option(A) ::= nm(X). {
if( X.n==6 && sqlite3_strnicmp(X.z,"strict",6)==0 ){
A = TF_Strict;
Expand Down Expand Up @@ -248,7 +256,7 @@ columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,A,Y);}
CONFLICT DATABASE DEFERRED DESC DETACH DO
EACH END EXCLUSIVE EXPLAIN FAIL FOR
IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
QUERY KEY OF OFFSET PRAGMA RAISE RANDOM RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
NULLS FIRST LAST
%ifdef SQLITE_OMIT_COMPOUND_SELECT
Expand Down
2 changes: 2 additions & 0 deletions src/sqliteInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -2341,6 +2341,8 @@ struct Table {
#define TF_Ephemeral 0x00004000 /* An ephemeral table */
#define TF_Eponymous 0x00008000 /* An eponymous virtual table */
#define TF_Strict 0x00010000 /* STRICT mode */
/* libSQL extension */
#define TF_RandomRowid 0x01000000 /* Random rowid */

/*
** Allowed values for Table.eTabType
Expand Down
6 changes: 6 additions & 0 deletions src/vdbe.c
Original file line number Diff line number Diff line change
Expand Up @@ -5419,6 +5419,12 @@ case OP_NewRowid: { /* out2 */
}
}

if ( pOp->p3 == 0xffffffff) {
fprintf(stderr, "assigning random rowid\n");
pC->useRandomRowid = 1;
pOp->p3 = 0;
}

#ifndef SQLITE_OMIT_AUTOINCREMENT
if( pOp->p3 ){
/* Assert that P3 is a valid memory cell. */
Expand Down
1 change: 1 addition & 0 deletions tool/mkkeywordhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ static Keyword aKeywordTable[] = {
{ "PRIMARY", "TK_PRIMARY", ALWAYS, 1 },
{ "QUERY", "TK_QUERY", EXPLAIN, 0 },
{ "RAISE", "TK_RAISE", TRIGGER, 1 },
{ "RANDOM", "TK_RANDOM", ALWAYS, 1 },
{ "RANGE", "TK_RANGE", WINDOWFUNC, 3 },
{ "RECURSIVE", "TK_RECURSIVE", CTE, 3 },
{ "REFERENCES", "TK_REFERENCES", FKEY, 1 },
Expand Down

0 comments on commit ffe9707

Please sign in to comment.