Fossil SCM

Update to the latest SQLite amalgamation, for the purpose of testing recent changes in SQLite.

drh 2012-09-28 10:18 trunk
Commit c0f245de25a61fcbd3924a5e05169e711ab1f762
3 files changed +32 -5 +2199 -1993 +4 -1
+32 -5
--- src/shell.c
+++ src/shell.c
@@ -694,28 +694,33 @@
694694
if( i<ArraySize(p->colWidth) ){
695695
w = p->colWidth[i];
696696
}else{
697697
w = 0;
698698
}
699
- if( w<=0 ){
699
+ if( w==0 ){
700700
w = strlen30(azCol[i] ? azCol[i] : "");
701701
if( w<10 ) w = 10;
702702
n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue);
703703
if( w<n ) w = n;
704704
}
705705
if( i<ArraySize(p->actualWidth) ){
706706
p->actualWidth[i] = w;
707707
}
708708
if( p->showHeader ){
709
- fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
709
+ if( w<0 ){
710
+ fprintf(p->out,"%*.*s%s",-w,-w,azCol[i], i==nArg-1 ? "\n": " ");
711
+ }else{
712
+ fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
713
+ }
710714
}
711715
}
712716
if( p->showHeader ){
713717
for(i=0; i<nArg; i++){
714718
int w;
715719
if( i<ArraySize(p->actualWidth) ){
716720
w = p->actualWidth[i];
721
+ if( w<0 ) w = -w;
717722
}else{
718723
w = 10;
719724
}
720725
fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
721726
"----------------------------------------------------------",
@@ -733,12 +738,17 @@
733738
}
734739
if( p->mode==MODE_Explain && azArg[i] &&
735740
strlen30(azArg[i])>w ){
736741
w = strlen30(azArg[i]);
737742
}
738
- fprintf(p->out,"%-*.*s%s",w,w,
739
- azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
743
+ if( w<0 ){
744
+ fprintf(p->out,"%*.*s%s",-w,-w,
745
+ azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
746
+ }else{
747
+ fprintf(p->out,"%-*.*s%s",w,w,
748
+ azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
749
+ }
740750
}
741751
break;
742752
}
743753
case MODE_Semi:
744754
case MODE_List: {
@@ -1414,13 +1424,14 @@
14141424
" insert SQL insert statements for TABLE\n"
14151425
" line One value per line\n"
14161426
" list Values delimited by .separator string\n"
14171427
" tabs Tab-separated values\n"
14181428
" tcl TCL list elements\n"
1419
- ".nullvalue STRING Print STRING in place of NULL values\n"
1429
+ ".nullvalue STRING Use STRING in place of NULL values\n"
14201430
".output FILENAME Send output to FILENAME\n"
14211431
".output stdout Send output to the screen\n"
1432
+ ".print STRING... Print literal STRING\n"
14221433
".prompt MAIN CONTINUE Replace the standard prompts\n"
14231434
".quit Exit this program\n"
14241435
".read FILENAME Execute SQL in FILENAME\n"
14251436
".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
14261437
".schema ?TABLE? Show the CREATE statements\n"
@@ -2067,10 +2078,19 @@
20672078
} else {
20682079
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
20692080
}
20702081
}
20712082
}else
2083
+
2084
+ if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
2085
+ int i;
2086
+ for(i=1; i<nArg; i++){
2087
+ if( i>1 ) fprintf(p->out, " ");
2088
+ fprintf(p->out, "%s", azArg[i]);
2089
+ }
2090
+ fprintf(p->out, "\n");
2091
+ }else
20722092
20732093
if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
20742094
if( nArg >= 2) {
20752095
strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
20762096
}
@@ -2490,10 +2510,17 @@
24902510
printf("%s\n", zVfsName);
24912511
sqlite3_free(zVfsName);
24922512
}
24932513
}
24942514
}else
2515
+
2516
+#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2517
+ if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
2518
+ extern int sqlite3WhereTrace;
2519
+ sqlite3WhereTrace = atoi(azArg[1]);
2520
+ }else
2521
+#endif
24952522
24962523
if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){
24972524
int j;
24982525
assert( nArg<=ArraySize(azArg) );
24992526
for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
25002527
--- src/shell.c
+++ src/shell.c
@@ -694,28 +694,33 @@
694 if( i<ArraySize(p->colWidth) ){
695 w = p->colWidth[i];
696 }else{
697 w = 0;
698 }
699 if( w<=0 ){
700 w = strlen30(azCol[i] ? azCol[i] : "");
701 if( w<10 ) w = 10;
702 n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue);
703 if( w<n ) w = n;
704 }
705 if( i<ArraySize(p->actualWidth) ){
706 p->actualWidth[i] = w;
707 }
708 if( p->showHeader ){
709 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
 
 
 
 
710 }
711 }
712 if( p->showHeader ){
713 for(i=0; i<nArg; i++){
714 int w;
715 if( i<ArraySize(p->actualWidth) ){
716 w = p->actualWidth[i];
 
717 }else{
718 w = 10;
719 }
720 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
721 "----------------------------------------------------------",
@@ -733,12 +738,17 @@
733 }
734 if( p->mode==MODE_Explain && azArg[i] &&
735 strlen30(azArg[i])>w ){
736 w = strlen30(azArg[i]);
737 }
738 fprintf(p->out,"%-*.*s%s",w,w,
739 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
 
 
 
 
 
740 }
741 break;
742 }
743 case MODE_Semi:
744 case MODE_List: {
@@ -1414,13 +1424,14 @@
1414 " insert SQL insert statements for TABLE\n"
1415 " line One value per line\n"
1416 " list Values delimited by .separator string\n"
1417 " tabs Tab-separated values\n"
1418 " tcl TCL list elements\n"
1419 ".nullvalue STRING Print STRING in place of NULL values\n"
1420 ".output FILENAME Send output to FILENAME\n"
1421 ".output stdout Send output to the screen\n"
 
1422 ".prompt MAIN CONTINUE Replace the standard prompts\n"
1423 ".quit Exit this program\n"
1424 ".read FILENAME Execute SQL in FILENAME\n"
1425 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
1426 ".schema ?TABLE? Show the CREATE statements\n"
@@ -2067,10 +2078,19 @@
2067 } else {
2068 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
2069 }
2070 }
2071 }else
 
 
 
 
 
 
 
 
 
2072
2073 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
2074 if( nArg >= 2) {
2075 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
2076 }
@@ -2490,10 +2510,17 @@
2490 printf("%s\n", zVfsName);
2491 sqlite3_free(zVfsName);
2492 }
2493 }
2494 }else
 
 
 
 
 
 
 
2495
2496 if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){
2497 int j;
2498 assert( nArg<=ArraySize(azArg) );
2499 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
2500
--- src/shell.c
+++ src/shell.c
@@ -694,28 +694,33 @@
694 if( i<ArraySize(p->colWidth) ){
695 w = p->colWidth[i];
696 }else{
697 w = 0;
698 }
699 if( w==0 ){
700 w = strlen30(azCol[i] ? azCol[i] : "");
701 if( w<10 ) w = 10;
702 n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue);
703 if( w<n ) w = n;
704 }
705 if( i<ArraySize(p->actualWidth) ){
706 p->actualWidth[i] = w;
707 }
708 if( p->showHeader ){
709 if( w<0 ){
710 fprintf(p->out,"%*.*s%s",-w,-w,azCol[i], i==nArg-1 ? "\n": " ");
711 }else{
712 fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
713 }
714 }
715 }
716 if( p->showHeader ){
717 for(i=0; i<nArg; i++){
718 int w;
719 if( i<ArraySize(p->actualWidth) ){
720 w = p->actualWidth[i];
721 if( w<0 ) w = -w;
722 }else{
723 w = 10;
724 }
725 fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
726 "----------------------------------------------------------",
@@ -733,12 +738,17 @@
738 }
739 if( p->mode==MODE_Explain && azArg[i] &&
740 strlen30(azArg[i])>w ){
741 w = strlen30(azArg[i]);
742 }
743 if( w<0 ){
744 fprintf(p->out,"%*.*s%s",-w,-w,
745 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
746 }else{
747 fprintf(p->out,"%-*.*s%s",w,w,
748 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
749 }
750 }
751 break;
752 }
753 case MODE_Semi:
754 case MODE_List: {
@@ -1414,13 +1424,14 @@
1424 " insert SQL insert statements for TABLE\n"
1425 " line One value per line\n"
1426 " list Values delimited by .separator string\n"
1427 " tabs Tab-separated values\n"
1428 " tcl TCL list elements\n"
1429 ".nullvalue STRING Use STRING in place of NULL values\n"
1430 ".output FILENAME Send output to FILENAME\n"
1431 ".output stdout Send output to the screen\n"
1432 ".print STRING... Print literal STRING\n"
1433 ".prompt MAIN CONTINUE Replace the standard prompts\n"
1434 ".quit Exit this program\n"
1435 ".read FILENAME Execute SQL in FILENAME\n"
1436 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
1437 ".schema ?TABLE? Show the CREATE statements\n"
@@ -2067,10 +2078,19 @@
2078 } else {
2079 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
2080 }
2081 }
2082 }else
2083
2084 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
2085 int i;
2086 for(i=1; i<nArg; i++){
2087 if( i>1 ) fprintf(p->out, " ");
2088 fprintf(p->out, "%s", azArg[i]);
2089 }
2090 fprintf(p->out, "\n");
2091 }else
2092
2093 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
2094 if( nArg >= 2) {
2095 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
2096 }
@@ -2490,10 +2510,17 @@
2510 printf("%s\n", zVfsName);
2511 sqlite3_free(zVfsName);
2512 }
2513 }
2514 }else
2515
2516 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2517 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
2518 extern int sqlite3WhereTrace;
2519 sqlite3WhereTrace = atoi(azArg[1]);
2520 }else
2521 #endif
2522
2523 if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){
2524 int j;
2525 assert( nArg<=ArraySize(azArg) );
2526 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
2527
+2199 -1993
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -673,11 +673,11 @@
673673
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
674674
** [sqlite_version()] and [sqlite_source_id()].
675675
*/
676676
#define SQLITE_VERSION "3.7.15"
677677
#define SQLITE_VERSION_NUMBER 3007015
678
-#define SQLITE_SOURCE_ID "2012-09-17 21:24:01 698b2a28004a9a2f0eabaadf36d833da4400b2bf"
678
+#define SQLITE_SOURCE_ID "2012-09-28 00:44:28 1e874629d7cf568368b912b295bd3001147d0b52"
679679
680680
/*
681681
** CAPI3REF: Run-Time Library Version Numbers
682682
** KEYWORDS: sqlite3_version, sqlite3_sourceid
683683
**
@@ -5315,10 +5315,13 @@
53155315
** successfully. An [error code] is returned otherwise.)^
53165316
**
53175317
** ^Shared cache is disabled by default. But this might change in
53185318
** future releases of SQLite. Applications that care about shared
53195319
** cache setting should set it explicitly.
5320
+**
5321
+** This interface is threadsafe on processors where writing a
5322
+** 32-bit integer is atomic.
53205323
**
53215324
** See Also: [SQLite Shared-Cache Mode]
53225325
*/
53235326
SQLITE_API int sqlite3_enable_shared_cache(int);
53245327
@@ -8281,10 +8284,11 @@
82818284
typedef struct NameContext NameContext;
82828285
typedef struct Parse Parse;
82838286
typedef struct RowSet RowSet;
82848287
typedef struct Savepoint Savepoint;
82858288
typedef struct Select Select;
8289
+typedef struct SelectDest SelectDest;
82868290
typedef struct SrcList SrcList;
82878291
typedef struct StrAccum StrAccum;
82888292
typedef struct Table Table;
82898293
typedef struct TableLock TableLock;
82908294
typedef struct Token Token;
@@ -8887,11 +8891,11 @@
88878891
#define OPFLG_IN3 0x0010 /* in3: P3 is an input */
88888892
#define OPFLG_OUT2 0x0020 /* out2: P2 is an output */
88898893
#define OPFLG_OUT3 0x0040 /* out3: P3 is an output */
88908894
#define OPFLG_INITIALIZER {\
88918895
/* 0 */ 0x00, 0x01, 0x01, 0x04, 0x04, 0x10, 0x00, 0x02,\
8892
-/* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
8896
+/* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x24,\
88938897
/* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
88948898
/* 24 */ 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00,\
88958899
/* 32 */ 0x02, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,\
88968900
/* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11,\
88978901
/* 48 */ 0x11, 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02,\
@@ -9843,10 +9847,11 @@
98439847
int flags; /* Miscellaneous flags. See below */
98449848
i64 lastRowid; /* ROWID of most recent insert (see above) */
98459849
unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
98469850
int errCode; /* Most recent error code (SQLITE_*) */
98479851
int errMask; /* & result codes with this before returning */
9852
+ u8 dbOptFlags; /* Flags to enable/disable optimizations */
98489853
u8 autoCommit; /* The auto-commit flag. */
98499854
u8 temp_store; /* 1: file 2: memory 0: default */
98509855
u8 mallocFailed; /* True if we have seen a malloc failure */
98519856
u8 dfltLockMode; /* Default locking-mode for attached dbs */
98529857
signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
@@ -9947,50 +9952,62 @@
99479952
#define ENC(db) ((db)->aDb[0].pSchema->enc)
99489953
99499954
/*
99509955
** Possible values for the sqlite3.flags.
99519956
*/
9952
-#define SQLITE_VdbeTrace 0x00000100 /* True to trace VDBE execution */
9953
-#define SQLITE_InternChanges 0x00000200 /* Uncommitted Hash table changes */
9954
-#define SQLITE_FullColNames 0x00000400 /* Show full column names on SELECT */
9955
-#define SQLITE_ShortColNames 0x00000800 /* Show short columns names */
9956
-#define SQLITE_CountRows 0x00001000 /* Count rows changed by INSERT, */
9957
+#define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */
9958
+#define SQLITE_InternChanges 0x00000002 /* Uncommitted Hash table changes */
9959
+#define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */
9960
+#define SQLITE_ShortColNames 0x00000008 /* Show short columns names */
9961
+#define SQLITE_CountRows 0x00000010 /* Count rows changed by INSERT, */
99579962
/* DELETE, or UPDATE and return */
99589963
/* the count using a callback. */
9959
-#define SQLITE_NullCallback 0x00002000 /* Invoke the callback once if the */
9964
+#define SQLITE_NullCallback 0x00000020 /* Invoke the callback once if the */
99609965
/* result set is empty */
9961
-#define SQLITE_SqlTrace 0x00004000 /* Debug print SQL as it executes */
9962
-#define SQLITE_VdbeListing 0x00008000 /* Debug listings of VDBE programs */
9963
-#define SQLITE_WriteSchema 0x00010000 /* OK to update SQLITE_MASTER */
9964
- /* 0x00020000 Unused */
9965
-#define SQLITE_IgnoreChecks 0x00040000 /* Do not enforce check constraints */
9966
-#define SQLITE_ReadUncommitted 0x0080000 /* For shared-cache mode */
9967
-#define SQLITE_LegacyFileFmt 0x00100000 /* Create new databases in format 1 */
9968
-#define SQLITE_FullFSync 0x00200000 /* Use full fsync on the backend */
9969
-#define SQLITE_CkptFullFSync 0x00400000 /* Use full fsync for checkpoint */
9970
-#define SQLITE_RecoveryMode 0x00800000 /* Ignore schema errors */
9971
-#define SQLITE_ReverseOrder 0x01000000 /* Reverse unordered SELECTs */
9972
-#define SQLITE_RecTriggers 0x02000000 /* Enable recursive triggers */
9973
-#define SQLITE_ForeignKeys 0x04000000 /* Enforce foreign key constraints */
9974
-#define SQLITE_AutoIndex 0x08000000 /* Enable automatic indexes */
9975
-#define SQLITE_PreferBuiltin 0x10000000 /* Preference to built-in funcs */
9976
-#define SQLITE_LoadExtension 0x20000000 /* Enable load_extension */
9977
-#define SQLITE_EnableTrigger 0x40000000 /* True to enable triggers */
9966
+#define SQLITE_SqlTrace 0x00000040 /* Debug print SQL as it executes */
9967
+#define SQLITE_VdbeListing 0x00000080 /* Debug listings of VDBE programs */
9968
+#define SQLITE_WriteSchema 0x00000100 /* OK to update SQLITE_MASTER */
9969
+ /* 0x00000200 Unused */
9970
+#define SQLITE_IgnoreChecks 0x00000400 /* Do not enforce check constraints */
9971
+#define SQLITE_ReadUncommitted 0x0000800 /* For shared-cache mode */
9972
+#define SQLITE_LegacyFileFmt 0x00001000 /* Create new databases in format 1 */
9973
+#define SQLITE_FullFSync 0x00002000 /* Use full fsync on the backend */
9974
+#define SQLITE_CkptFullFSync 0x00004000 /* Use full fsync for checkpoint */
9975
+#define SQLITE_RecoveryMode 0x00008000 /* Ignore schema errors */
9976
+#define SQLITE_ReverseOrder 0x00010000 /* Reverse unordered SELECTs */
9977
+#define SQLITE_RecTriggers 0x00020000 /* Enable recursive triggers */
9978
+#define SQLITE_ForeignKeys 0x00040000 /* Enforce foreign key constraints */
9979
+#define SQLITE_AutoIndex 0x00080000 /* Enable automatic indexes */
9980
+#define SQLITE_PreferBuiltin 0x00100000 /* Preference to built-in funcs */
9981
+#define SQLITE_LoadExtension 0x00200000 /* Enable load_extension */
9982
+#define SQLITE_EnableTrigger 0x00400000 /* True to enable triggers */
99789983
99799984
/*
9980
-** Bits of the sqlite3.flags field that are used by the
9981
-** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
9982
-** These must be the low-order bits of the flags field.
9985
+** Bits of the sqlite3.dbOptFlags field that are used by the
9986
+** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
9987
+** selectively disable various optimizations.
99839988
*/
9984
-#define SQLITE_QueryFlattener 0x01 /* Disable query flattening */
9985
-#define SQLITE_ColumnCache 0x02 /* Disable the column cache */
9986
-#define SQLITE_GroupByOrder 0x04 /* Disable GROUPBY cover of ORDERBY */
9987
-#define SQLITE_FactorOutConst 0x08 /* Disable factoring out constants */
9988
-#define SQLITE_IdxRealAsInt 0x10 /* Store REAL as INT in indices */
9989
-#define SQLITE_DistinctOpt 0x20 /* DISTINCT using indexes */
9990
-#define SQLITE_CoverIdxScan 0x40 /* Disable covering index scans */
9991
-#define SQLITE_OptMask 0xff /* Mask of all disablable opts */
9989
+#define SQLITE_QueryFlattener 0x0001 /* Query flattening */
9990
+#define SQLITE_ColumnCache 0x0002 /* Column cache */
9991
+#define SQLITE_GroupByOrder 0x0004 /* GROUPBY cover of ORDERBY */
9992
+#define SQLITE_FactorOutConst 0x0008 /* Constant factoring */
9993
+#define SQLITE_IdxRealAsInt 0x0010 /* Store REAL as INT in indices */
9994
+#define SQLITE_DistinctOpt 0x0020 /* DISTINCT using indexes */
9995
+#define SQLITE_CoverIdxScan 0x0040 /* Covering index scans */
9996
+#define SQLITE_OrderByIdxJoin 0x0080 /* ORDER BY of joins via index */
9997
+#define SQLITE_AllOpts 0x00ff /* All optimizations */
9998
+
9999
+/*
10000
+** Macros for testing whether or not optimizations are enabled or disabled.
10001
+*/
10002
+#ifndef SQLITE_OMIT_BUILTIN_TEST
10003
+#define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
10004
+#define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
10005
+#else
10006
+#define OptimizationDisabled(db, mask) 0
10007
+#define OptimizationEnabled(db, mask) 1
10008
+#endif
999210009
999310010
/*
999410011
** Possible values for the sqlite.magic field.
999510012
** The numbers are obtained at random and have no special meaning, other
999610013
** than being distinct from one another.
@@ -10922,11 +10939,12 @@
1092210939
** is only used when wsFlags&WHERE_VIRTUALTABLE is true. It is never the
1092310940
** case that more than one of these conditions is true.
1092410941
*/
1092510942
struct WherePlan {
1092610943
u32 wsFlags; /* WHERE_* flags that describe the strategy */
10927
- u32 nEq; /* Number of == constraints */
10944
+ u16 nEq; /* Number of == constraints */
10945
+ u16 nOBSat; /* Number of ORDER BY terms satisfied */
1092810946
double nRow; /* Estimated number of rows (for EQP) */
1092910947
union {
1093010948
Index *pIdx; /* Index when WHERE_INDEXED is true */
1093110949
struct WhereTerm *pTerm; /* WHERE clause term for OR-search */
1093210950
sqlite3_index_info *pVtabIdx; /* Virtual table index to use */
@@ -10998,28 +11016,32 @@
1099811016
** half does the tail of the WHERE loop. An instance of
1099911017
** this structure is returned by the first half and passed
1100011018
** into the second half to give some continuity.
1100111019
*/
1100211020
struct WhereInfo {
11003
- Parse *pParse; /* Parsing and code generating context */
11004
- u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
11005
- u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE or DELETE */
11006
- u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
11007
- u8 eDistinct;
11008
- SrcList *pTabList; /* List of tables in the join */
11009
- int iTop; /* The very beginning of the WHERE loop */
11010
- int iContinue; /* Jump here to continue with next record */
11011
- int iBreak; /* Jump here to break out of the loop */
11012
- int nLevel; /* Number of nested loop */
11013
- struct WhereClause *pWC; /* Decomposition of the WHERE clause */
11014
- double savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
11015
- double nRowOut; /* Estimated number of output rows */
11016
- WhereLevel a[1]; /* Information about each nest loop in WHERE */
11021
+ Parse *pParse; /* Parsing and code generating context */
11022
+ SrcList *pTabList; /* List of tables in the join */
11023
+ u16 nOBSat; /* Number of ORDER BY terms satisfied by indices */
11024
+ u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
11025
+ u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */
11026
+ u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
11027
+ u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */
11028
+ int iTop; /* The very beginning of the WHERE loop */
11029
+ int iContinue; /* Jump here to continue with next record */
11030
+ int iBreak; /* Jump here to break out of the loop */
11031
+ int nLevel; /* Number of nested loop */
11032
+ struct WhereClause *pWC; /* Decomposition of the WHERE clause */
11033
+ double savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
11034
+ double nRowOut; /* Estimated number of output rows */
11035
+ WhereLevel a[1]; /* Information about each nest loop in WHERE */
1101711036
};
1101811037
11019
-#define WHERE_DISTINCT_UNIQUE 1
11020
-#define WHERE_DISTINCT_ORDERED 2
11038
+/* Allowed values for WhereInfo.eDistinct and DistinctCtx.eTnctType */
11039
+#define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
11040
+#define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
11041
+#define WHERE_DISTINCT_ORDERED 2 /* All duplicates are adjacent */
11042
+#define WHERE_DISTINCT_UNORDERED 3 /* Duplicates are scattered */
1102111043
1102211044
/*
1102311045
** A NameContext defines a context in which to resolve table and column
1102411046
** names. The context consists of a list of tables (the pSrcList) field and
1102511047
** a list of named expression (pEList). The named expression list may
@@ -11074,17 +11096,16 @@
1107411096
** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
1107511097
** the number of columns in P2 can be computed at the same time
1107611098
** as the OP_OpenEphm instruction is coded because not
1107711099
** enough information about the compound query is known at that point.
1107811100
** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
11079
-** for the result set. The KeyInfo for addrOpenTran[2] contains collating
11101
+** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
1108011102
** sequences for the ORDER BY clause.
1108111103
*/
1108211104
struct Select {
1108311105
ExprList *pEList; /* The fields of the result */
1108411106
u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
11085
- char affinity; /* MakeRecord with this affinity for SRT_Set */
1108611107
u16 selFlags; /* Various SF_* values */
1108711108
int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
1108811109
int addrOpenEphm[3]; /* OP_OpenEphem opcodes related to this select */
1108911110
double nSelectRow; /* Estimated number of result rows */
1109011111
SrcList *pSrc; /* The FROM clause */
@@ -11131,17 +11152,16 @@
1113111152
#define SRT_Table 8 /* Store result as data with an automatic rowid */
1113211153
#define SRT_EphemTab 9 /* Create transient tab and store like SRT_Table */
1113311154
#define SRT_Coroutine 10 /* Generate a single row of result */
1113411155
1113511156
/*
11136
-** A structure used to customize the behavior of sqlite3Select(). See
11137
-** comments above sqlite3Select() for details.
11157
+** An instance of this object describes where to put of the results of
11158
+** a SELECT statement.
1113811159
*/
11139
-typedef struct SelectDest SelectDest;
1114011160
struct SelectDest {
11141
- u8 eDest; /* How to dispose of the results */
11142
- u8 affSdst; /* Affinity used when eDest==SRT_Set */
11161
+ u8 eDest; /* How to dispose of the results. On of SRT_* above. */
11162
+ char affSdst; /* Affinity used when eDest==SRT_Set */
1114311163
int iSDParm; /* A parameter used by the eDest disposal method */
1114411164
int iSdst; /* Base register where results are written */
1114511165
int nSdst; /* Number of registers allocated */
1114611166
};
1114711167
@@ -11826,17 +11846,15 @@
1182611846
#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
1182711847
SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
1182811848
#endif
1182911849
SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
1183011850
SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
11831
-SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
11832
- Parse*,SrcList*,Expr*,ExprList**,ExprList*,u16,int);
11851
+SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
1183311852
SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
1183411853
SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
1183511854
SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
1183611855
SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
11837
-SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, int, int, int);
1183811856
SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
1183911857
SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
1184011858
SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
1184111859
SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
1184211860
SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
@@ -13178,11 +13196,13 @@
1317813196
#define MEM_Real 0x0008 /* Value is a real number */
1317913197
#define MEM_Blob 0x0010 /* Value is a BLOB */
1318013198
#define MEM_RowSet 0x0020 /* Value is a RowSet object */
1318113199
#define MEM_Frame 0x0040 /* Value is a VdbeFrame object */
1318213200
#define MEM_Invalid 0x0080 /* Value is undefined */
13183
-#define MEM_TypeMask 0x00ff /* Mask of type bits */
13201
+#define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */
13202
+#define MEM_TypeMask 0x01ff /* Mask of type bits */
13203
+
1318413204
1318513205
/* Whenever Mem contains a valid string or blob representation, one of
1318613206
** the following flags must be set to determine the memory management
1318713207
** policy for Mem.z. The MEM_Term flag tells us whether or not the
1318813208
** string is \000 or \u0000 terminated
@@ -63693,10 +63713,11 @@
6369363713
struct OP_Yield_stack_vars {
6369463714
int pcDest;
6369563715
} aa;
6369663716
struct OP_Null_stack_vars {
6369763717
int cnt;
63718
+ u16 nullFlag;
6369863719
} ab;
6369963720
struct OP_Variable_stack_vars {
6370063721
Mem *pVar; /* Value being transferred */
6370163722
} ac;
6370263723
struct OP_Move_stack_vars {
@@ -63703,60 +63724,63 @@
6370363724
char *zMalloc; /* Holding variable for allocated memory */
6370463725
int n; /* Number of registers left to copy */
6370563726
int p1; /* Register to copy from */
6370663727
int p2; /* Register to copy to */
6370763728
} ad;
63729
+ struct OP_Copy_stack_vars {
63730
+ int n;
63731
+ } ae;
6370863732
struct OP_ResultRow_stack_vars {
6370963733
Mem *pMem;
6371063734
int i;
63711
- } ae;
63735
+ } af;
6371263736
struct OP_Concat_stack_vars {
6371363737
i64 nByte;
63714
- } af;
63738
+ } ag;
6371563739
struct OP_Remainder_stack_vars {
6371663740
int flags; /* Combined MEM_* flags from both inputs */
6371763741
i64 iA; /* Integer value of left operand */
6371863742
i64 iB; /* Integer value of right operand */
6371963743
double rA; /* Real value of left operand */
6372063744
double rB; /* Real value of right operand */
63721
- } ag;
63745
+ } ah;
6372263746
struct OP_Function_stack_vars {
6372363747
int i;
6372463748
Mem *pArg;
6372563749
sqlite3_context ctx;
6372663750
sqlite3_value **apVal;
6372763751
int n;
63728
- } ah;
63752
+ } ai;
6372963753
struct OP_ShiftRight_stack_vars {
6373063754
i64 iA;
6373163755
u64 uA;
6373263756
i64 iB;
6373363757
u8 op;
63734
- } ai;
63758
+ } aj;
6373563759
struct OP_Ge_stack_vars {
6373663760
int res; /* Result of the comparison of pIn1 against pIn3 */
6373763761
char affinity; /* Affinity to use for comparison */
6373863762
u16 flags1; /* Copy of initial value of pIn1->flags */
6373963763
u16 flags3; /* Copy of initial value of pIn3->flags */
63740
- } aj;
63764
+ } ak;
6374163765
struct OP_Compare_stack_vars {
6374263766
int n;
6374363767
int i;
6374463768
int p1;
6374563769
int p2;
6374663770
const KeyInfo *pKeyInfo;
6374763771
int idx;
6374863772
CollSeq *pColl; /* Collating sequence to use on this term */
6374963773
int bRev; /* True for DESCENDING sort order */
63750
- } ak;
63774
+ } al;
6375163775
struct OP_Or_stack_vars {
6375263776
int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
6375363777
int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
63754
- } al;
63778
+ } am;
6375563779
struct OP_IfNot_stack_vars {
6375663780
int c;
63757
- } am;
63781
+ } an;
6375863782
struct OP_Column_stack_vars {
6375963783
u32 payloadSize; /* Number of bytes in the record */
6376063784
i64 payloadSize64; /* Number of bytes in the record */
6376163785
int p1; /* P1 value of the opcode */
6376263786
int p2; /* column number to retrieve */
@@ -63777,15 +63801,15 @@
6377763801
u32 szField; /* Number of bytes in the content of a field */
6377863802
int szHdr; /* Size of the header size field at start of record */
6377963803
int avail; /* Number of bytes of available data */
6378063804
u32 t; /* A type code from the record header */
6378163805
Mem *pReg; /* PseudoTable input register */
63782
- } an;
63806
+ } ao;
6378363807
struct OP_Affinity_stack_vars {
6378463808
const char *zAffinity; /* The affinity to be applied */
6378563809
char cAff; /* A single character of affinity */
63786
- } ao;
63810
+ } ap;
6378763811
struct OP_MakeRecord_stack_vars {
6378863812
u8 *zNewRecord; /* A buffer to hold the data for the new record */
6378963813
Mem *pRec; /* The new record */
6379063814
u64 nData; /* Number of bytes of data space */
6379163815
int nHdr; /* Number of bytes of header space */
@@ -63798,108 +63822,108 @@
6379863822
int nField; /* Number of fields in the record */
6379963823
char *zAffinity; /* The affinity string for the record */
6380063824
int file_format; /* File format to use for encoding */
6380163825
int i; /* Space used in zNewRecord[] */
6380263826
int len; /* Length of a field */
63803
- } ap;
63827
+ } aq;
6380463828
struct OP_Count_stack_vars {
6380563829
i64 nEntry;
6380663830
BtCursor *pCrsr;
63807
- } aq;
63831
+ } ar;
6380863832
struct OP_Savepoint_stack_vars {
6380963833
int p1; /* Value of P1 operand */
6381063834
char *zName; /* Name of savepoint */
6381163835
int nName;
6381263836
Savepoint *pNew;
6381363837
Savepoint *pSavepoint;
6381463838
Savepoint *pTmp;
6381563839
int iSavepoint;
6381663840
int ii;
63817
- } ar;
63841
+ } as;
6381863842
struct OP_AutoCommit_stack_vars {
6381963843
int desiredAutoCommit;
6382063844
int iRollback;
6382163845
int turnOnAC;
63822
- } as;
63846
+ } at;
6382363847
struct OP_Transaction_stack_vars {
6382463848
Btree *pBt;
63825
- } at;
63849
+ } au;
6382663850
struct OP_ReadCookie_stack_vars {
6382763851
int iMeta;
6382863852
int iDb;
6382963853
int iCookie;
63830
- } au;
63854
+ } av;
6383163855
struct OP_SetCookie_stack_vars {
6383263856
Db *pDb;
63833
- } av;
63857
+ } aw;
6383463858
struct OP_VerifyCookie_stack_vars {
6383563859
int iMeta;
6383663860
int iGen;
6383763861
Btree *pBt;
63838
- } aw;
63862
+ } ax;
6383963863
struct OP_OpenWrite_stack_vars {
6384063864
int nField;
6384163865
KeyInfo *pKeyInfo;
6384263866
int p2;
6384363867
int iDb;
6384463868
int wrFlag;
6384563869
Btree *pX;
6384663870
VdbeCursor *pCur;
6384763871
Db *pDb;
63848
- } ax;
63872
+ } ay;
6384963873
struct OP_OpenEphemeral_stack_vars {
6385063874
VdbeCursor *pCx;
63851
- } ay;
63875
+ } az;
6385263876
struct OP_SorterOpen_stack_vars {
6385363877
VdbeCursor *pCx;
63854
- } az;
63878
+ } ba;
6385563879
struct OP_OpenPseudo_stack_vars {
6385663880
VdbeCursor *pCx;
63857
- } ba;
63881
+ } bb;
6385863882
struct OP_SeekGt_stack_vars {
6385963883
int res;
6386063884
int oc;
6386163885
VdbeCursor *pC;
6386263886
UnpackedRecord r;
6386363887
int nField;
6386463888
i64 iKey; /* The rowid we are to seek to */
63865
- } bb;
63889
+ } bc;
6386663890
struct OP_Seek_stack_vars {
6386763891
VdbeCursor *pC;
63868
- } bc;
63892
+ } bd;
6386963893
struct OP_Found_stack_vars {
6387063894
int alreadyExists;
6387163895
VdbeCursor *pC;
6387263896
int res;
6387363897
char *pFree;
6387463898
UnpackedRecord *pIdxKey;
6387563899
UnpackedRecord r;
6387663900
char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
63877
- } bd;
63901
+ } be;
6387863902
struct OP_IsUnique_stack_vars {
6387963903
u16 ii;
6388063904
VdbeCursor *pCx;
6388163905
BtCursor *pCrsr;
6388263906
u16 nField;
6388363907
Mem *aMx;
6388463908
UnpackedRecord r; /* B-Tree index search key */
6388563909
i64 R; /* Rowid stored in register P3 */
63886
- } be;
63910
+ } bf;
6388763911
struct OP_NotExists_stack_vars {
6388863912
VdbeCursor *pC;
6388963913
BtCursor *pCrsr;
6389063914
int res;
6389163915
u64 iKey;
63892
- } bf;
63916
+ } bg;
6389363917
struct OP_NewRowid_stack_vars {
6389463918
i64 v; /* The new rowid */
6389563919
VdbeCursor *pC; /* Cursor of table to get the new rowid */
6389663920
int res; /* Result of an sqlite3BtreeLast() */
6389763921
int cnt; /* Counter to limit the number of searches */
6389863922
Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
6389963923
VdbeFrame *pFrame; /* Root frame of VDBE */
63900
- } bg;
63924
+ } bh;
6390163925
struct OP_InsertInt_stack_vars {
6390263926
Mem *pData; /* MEM cell holding data for the record to be inserted */
6390363927
Mem *pKey; /* MEM cell holding key for the record */
6390463928
i64 iKey; /* The integer ROWID or key for the record to be inserted */
6390563929
VdbeCursor *pC; /* Cursor to table into which insert is written */
@@ -63906,160 +63930,163 @@
6390663930
int nZero; /* Number of zero-bytes to append */
6390763931
int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
6390863932
const char *zDb; /* database name - used by the update hook */
6390963933
const char *zTbl; /* Table name - used by the opdate hook */
6391063934
int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
63911
- } bh;
63935
+ } bi;
6391263936
struct OP_Delete_stack_vars {
6391363937
i64 iKey;
6391463938
VdbeCursor *pC;
63915
- } bi;
63939
+ } bj;
6391663940
struct OP_SorterCompare_stack_vars {
6391763941
VdbeCursor *pC;
6391863942
int res;
63919
- } bj;
63943
+ } bk;
6392063944
struct OP_SorterData_stack_vars {
6392163945
VdbeCursor *pC;
63922
- } bk;
63946
+ } bl;
6392363947
struct OP_RowData_stack_vars {
6392463948
VdbeCursor *pC;
6392563949
BtCursor *pCrsr;
6392663950
u32 n;
6392763951
i64 n64;
63928
- } bl;
63952
+ } bm;
6392963953
struct OP_Rowid_stack_vars {
6393063954
VdbeCursor *pC;
6393163955
i64 v;
6393263956
sqlite3_vtab *pVtab;
6393363957
const sqlite3_module *pModule;
63934
- } bm;
63958
+ } bn;
6393563959
struct OP_NullRow_stack_vars {
6393663960
VdbeCursor *pC;
63937
- } bn;
63961
+ } bo;
6393863962
struct OP_Last_stack_vars {
6393963963
VdbeCursor *pC;
6394063964
BtCursor *pCrsr;
6394163965
int res;
63942
- } bo;
63966
+ } bp;
6394363967
struct OP_Rewind_stack_vars {
6394463968
VdbeCursor *pC;
6394563969
BtCursor *pCrsr;
6394663970
int res;
63947
- } bp;
63971
+ } bq;
6394863972
struct OP_Next_stack_vars {
6394963973
VdbeCursor *pC;
6395063974
int res;
63951
- } bq;
63975
+ } br;
6395263976
struct OP_IdxInsert_stack_vars {
6395363977
VdbeCursor *pC;
6395463978
BtCursor *pCrsr;
6395563979
int nKey;
6395663980
const char *zKey;
63957
- } br;
63981
+ } bs;
6395863982
struct OP_IdxDelete_stack_vars {
6395963983
VdbeCursor *pC;
6396063984
BtCursor *pCrsr;
6396163985
int res;
6396263986
UnpackedRecord r;
63963
- } bs;
63987
+ } bt;
6396463988
struct OP_IdxRowid_stack_vars {
6396563989
BtCursor *pCrsr;
6396663990
VdbeCursor *pC;
6396763991
i64 rowid;
63968
- } bt;
63992
+ } bu;
6396963993
struct OP_IdxGE_stack_vars {
6397063994
VdbeCursor *pC;
6397163995
int res;
6397263996
UnpackedRecord r;
63973
- } bu;
63997
+ } bv;
6397463998
struct OP_Destroy_stack_vars {
6397563999
int iMoved;
6397664000
int iCnt;
6397764001
Vdbe *pVdbe;
6397864002
int iDb;
63979
- } bv;
64003
+ } bw;
6398064004
struct OP_Clear_stack_vars {
6398164005
int nChange;
63982
- } bw;
64006
+ } bx;
6398364007
struct OP_CreateTable_stack_vars {
6398464008
int pgno;
6398564009
int flags;
6398664010
Db *pDb;
63987
- } bx;
64011
+ } by;
6398864012
struct OP_ParseSchema_stack_vars {
6398964013
int iDb;
6399064014
const char *zMaster;
6399164015
char *zSql;
6399264016
InitData initData;
63993
- } by;
64017
+ } bz;
6399464018
struct OP_IntegrityCk_stack_vars {
6399564019
int nRoot; /* Number of tables to check. (Number of root pages.) */
6399664020
int *aRoot; /* Array of rootpage numbers for tables to be checked */
6399764021
int j; /* Loop counter */
6399864022
int nErr; /* Number of errors reported */
6399964023
char *z; /* Text of the error report */
6400064024
Mem *pnErr; /* Register keeping track of errors remaining */
64001
- } bz;
64025
+ } ca;
6400264026
struct OP_RowSetRead_stack_vars {
6400364027
i64 val;
64004
- } ca;
64028
+ } cb;
6400564029
struct OP_RowSetTest_stack_vars {
6400664030
int iSet;
6400764031
int exists;
64008
- } cb;
64032
+ } cc;
6400964033
struct OP_Program_stack_vars {
6401064034
int nMem; /* Number of memory registers for sub-program */
6401164035
int nByte; /* Bytes of runtime space required for sub-program */
6401264036
Mem *pRt; /* Register to allocate runtime space */
6401364037
Mem *pMem; /* Used to iterate through memory cells */
6401464038
Mem *pEnd; /* Last memory cell in new array */
6401564039
VdbeFrame *pFrame; /* New vdbe frame to execute in */
6401664040
SubProgram *pProgram; /* Sub-program to execute */
6401764041
void *t; /* Token identifying trigger */
64018
- } cc;
64042
+ } cd;
6401964043
struct OP_Param_stack_vars {
6402064044
VdbeFrame *pFrame;
6402164045
Mem *pIn;
64022
- } cd;
64046
+ } ce;
6402364047
struct OP_MemMax_stack_vars {
6402464048
Mem *pIn1;
6402564049
VdbeFrame *pFrame;
64026
- } ce;
64050
+ } cf;
6402764051
struct OP_AggStep_stack_vars {
6402864052
int n;
6402964053
int i;
6403064054
Mem *pMem;
6403164055
Mem *pRec;
6403264056
sqlite3_context ctx;
6403364057
sqlite3_value **apVal;
64034
- } cf;
64058
+ } cg;
6403564059
struct OP_AggFinal_stack_vars {
6403664060
Mem *pMem;
64037
- } cg;
64061
+ } ch;
6403864062
struct OP_Checkpoint_stack_vars {
6403964063
int i; /* Loop counter */
6404064064
int aRes[3]; /* Results */
6404164065
Mem *pMem; /* Write results here */
64042
- } ch;
64066
+ } ci;
6404364067
struct OP_JournalMode_stack_vars {
6404464068
Btree *pBt; /* Btree to change journal mode of */
6404564069
Pager *pPager; /* Pager associated with pBt */
6404664070
int eNew; /* New journal mode */
6404764071
int eOld; /* The old journal mode */
64048
- } ci;
64072
+#ifndef SQLITE_OMIT_WAL
64073
+ const char *zFilename; /* Name of database file for pPager */
64074
+#endif
64075
+ } cj;
6404964076
struct OP_IncrVacuum_stack_vars {
6405064077
Btree *pBt;
64051
- } cj;
64078
+ } ck;
6405264079
struct OP_VBegin_stack_vars {
6405364080
VTable *pVTab;
64054
- } ck;
64081
+ } cl;
6405564082
struct OP_VOpen_stack_vars {
6405664083
VdbeCursor *pCur;
6405764084
sqlite3_vtab_cursor *pVtabCursor;
6405864085
sqlite3_vtab *pVtab;
6405964086
sqlite3_module *pModule;
64060
- } cl;
64087
+ } cm;
6406164088
struct OP_VFilter_stack_vars {
6406264089
int nArg;
6406364090
int iQuery;
6406464091
const sqlite3_module *pModule;
6406564092
Mem *pQuery;
@@ -64068,40 +64095,40 @@
6406864095
sqlite3_vtab *pVtab;
6406964096
VdbeCursor *pCur;
6407064097
int res;
6407164098
int i;
6407264099
Mem **apArg;
64073
- } cm;
64100
+ } cn;
6407464101
struct OP_VColumn_stack_vars {
6407564102
sqlite3_vtab *pVtab;
6407664103
const sqlite3_module *pModule;
6407764104
Mem *pDest;
6407864105
sqlite3_context sContext;
64079
- } cn;
64106
+ } co;
6408064107
struct OP_VNext_stack_vars {
6408164108
sqlite3_vtab *pVtab;
6408264109
const sqlite3_module *pModule;
6408364110
int res;
6408464111
VdbeCursor *pCur;
64085
- } co;
64112
+ } cp;
6408664113
struct OP_VRename_stack_vars {
6408764114
sqlite3_vtab *pVtab;
6408864115
Mem *pName;
64089
- } cp;
64116
+ } cq;
6409064117
struct OP_VUpdate_stack_vars {
6409164118
sqlite3_vtab *pVtab;
6409264119
sqlite3_module *pModule;
6409364120
int nArg;
6409464121
int i;
6409564122
sqlite_int64 rowid;
6409664123
Mem **apArg;
6409764124
Mem *pX;
64098
- } cq;
64125
+ } cr;
6409964126
struct OP_Trace_stack_vars {
6410064127
char *zTrace;
6410164128
char *z;
64102
- } cr;
64129
+ } cs;
6410364130
} u;
6410464131
/* End automatically generated code
6410564132
********************************************************************/
6410664133
6410764134
assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -64488,29 +64515,34 @@
6448864515
pOut->enc = encoding;
6448964516
UPDATE_MAX_BLOBSIZE(pOut);
6449064517
break;
6449164518
}
6449264519
64493
-/* Opcode: Null * P2 P3 * *
64520
+/* Opcode: Null P1 P2 P3 * *
6449464521
**
6449564522
** Write a NULL into registers P2. If P3 greater than P2, then also write
64496
-** NULL into register P3 and ever register in between P2 and P3. If P3
64523
+** NULL into register P3 and every register in between P2 and P3. If P3
6449764524
** is less than P2 (typically P3 is zero) then only register P2 is
64498
-** set to NULL
64525
+** set to NULL.
64526
+**
64527
+** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
64528
+** NULL values will not compare equal even if SQLITE_NULLEQ is set on
64529
+** OP_Ne or OP_Eq.
6449964530
*/
6450064531
case OP_Null: { /* out2-prerelease */
6450164532
#if 0 /* local variables moved into u.ab */
6450264533
int cnt;
64534
+ u16 nullFlag;
6450364535
#endif /* local variables moved into u.ab */
6450464536
u.ab.cnt = pOp->p3-pOp->p2;
6450564537
assert( pOp->p3<=p->nMem );
64506
- pOut->flags = MEM_Null;
64538
+ pOut->flags = u.ab.nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
6450764539
while( u.ab.cnt>0 ){
6450864540
pOut++;
6450964541
memAboutToChange(p, pOut);
6451064542
VdbeMemRelease(pOut);
64511
- pOut->flags = MEM_Null;
64543
+ pOut->flags = u.ab.nullFlag;
6451264544
u.ab.cnt--;
6451364545
}
6451464546
break;
6451564547
}
6451664548
@@ -64551,24 +64583,24 @@
6455164583
break;
6455264584
}
6455364585
6455464586
/* Opcode: Move P1 P2 P3 * *
6455564587
**
64556
-** Move the values in register P1..P1+P3-1 over into
64557
-** registers P2..P2+P3-1. Registers P1..P1+P1-1 are
64588
+** Move the values in register P1..P1+P3 over into
64589
+** registers P2..P2+P3. Registers P1..P1+P3 are
6455864590
** left holding a NULL. It is an error for register ranges
64559
-** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
64591
+** P1..P1+P3 and P2..P2+P3 to overlap.
6456064592
*/
6456164593
case OP_Move: {
6456264594
#if 0 /* local variables moved into u.ad */
6456364595
char *zMalloc; /* Holding variable for allocated memory */
6456464596
int n; /* Number of registers left to copy */
6456564597
int p1; /* Register to copy from */
6456664598
int p2; /* Register to copy to */
6456764599
#endif /* local variables moved into u.ad */
6456864600
64569
- u.ad.n = pOp->p3;
64601
+ u.ad.n = pOp->p3 + 1;
6457064602
u.ad.p1 = pOp->p1;
6457164603
u.ad.p2 = pOp->p2;
6457264604
assert( u.ad.n>0 && u.ad.p1>0 && u.ad.p2>0 );
6457364605
assert( u.ad.p1+u.ad.n<=u.ad.p2 || u.ad.p2+u.ad.n<=u.ad.p1 );
6457464606
@@ -64593,24 +64625,34 @@
6459364625
pOut++;
6459464626
}
6459564627
break;
6459664628
}
6459764629
64598
-/* Opcode: Copy P1 P2 * * *
64630
+/* Opcode: Copy P1 P2 P3 * *
6459964631
**
64600
-** Make a copy of register P1 into register P2.
64632
+** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
6460164633
**
6460264634
** This instruction makes a deep copy of the value. A duplicate
6460364635
** is made of any string or blob constant. See also OP_SCopy.
6460464636
*/
64605
-case OP_Copy: { /* in1, out2 */
64637
+case OP_Copy: {
64638
+#if 0 /* local variables moved into u.ae */
64639
+ int n;
64640
+#endif /* local variables moved into u.ae */
64641
+
64642
+ u.ae.n = pOp->p3;
6460664643
pIn1 = &aMem[pOp->p1];
6460764644
pOut = &aMem[pOp->p2];
6460864645
assert( pOut!=pIn1 );
64609
- sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
64610
- Deephemeralize(pOut);
64611
- REGISTER_TRACE(pOp->p2, pOut);
64646
+ while( 1 ){
64647
+ sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
64648
+ Deephemeralize(pOut);
64649
+ REGISTER_TRACE(pOp->p2+pOp->p3-u.ae.n, pOut);
64650
+ if( (u.ae.n--)==0 ) break;
64651
+ pOut++;
64652
+ pIn1++;
64653
+ }
6461264654
break;
6461364655
}
6461464656
6461564657
/* Opcode: SCopy P1 P2 * * *
6461664658
**
@@ -64643,14 +64685,14 @@
6464364685
** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
6464464686
** structure to provide access to the top P1 values as the result
6464564687
** row.
6464664688
*/
6464764689
case OP_ResultRow: {
64648
-#if 0 /* local variables moved into u.ae */
64690
+#if 0 /* local variables moved into u.af */
6464964691
Mem *pMem;
6465064692
int i;
64651
-#endif /* local variables moved into u.ae */
64693
+#endif /* local variables moved into u.af */
6465264694
assert( p->nResColumn==pOp->p2 );
6465364695
assert( pOp->p1>0 );
6465464696
assert( pOp->p1+pOp->p2<=p->nMem+1 );
6465564697
6465664698
/* If this statement has violated immediate foreign key constraints, do
@@ -64688,19 +64730,19 @@
6468864730
6468964731
/* Make sure the results of the current row are \000 terminated
6469064732
** and have an assigned type. The results are de-ephemeralized as
6469164733
** a side effect.
6469264734
*/
64693
- u.ae.pMem = p->pResultSet = &aMem[pOp->p1];
64694
- for(u.ae.i=0; u.ae.i<pOp->p2; u.ae.i++){
64695
- assert( memIsValid(&u.ae.pMem[u.ae.i]) );
64696
- Deephemeralize(&u.ae.pMem[u.ae.i]);
64697
- assert( (u.ae.pMem[u.ae.i].flags & MEM_Ephem)==0
64698
- || (u.ae.pMem[u.ae.i].flags & (MEM_Str|MEM_Blob))==0 );
64699
- sqlite3VdbeMemNulTerminate(&u.ae.pMem[u.ae.i]);
64700
- sqlite3VdbeMemStoreType(&u.ae.pMem[u.ae.i]);
64701
- REGISTER_TRACE(pOp->p1+u.ae.i, &u.ae.pMem[u.ae.i]);
64735
+ u.af.pMem = p->pResultSet = &aMem[pOp->p1];
64736
+ for(u.af.i=0; u.af.i<pOp->p2; u.af.i++){
64737
+ assert( memIsValid(&u.af.pMem[u.af.i]) );
64738
+ Deephemeralize(&u.af.pMem[u.af.i]);
64739
+ assert( (u.af.pMem[u.af.i].flags & MEM_Ephem)==0
64740
+ || (u.af.pMem[u.af.i].flags & (MEM_Str|MEM_Blob))==0 );
64741
+ sqlite3VdbeMemNulTerminate(&u.af.pMem[u.af.i]);
64742
+ sqlite3VdbeMemStoreType(&u.af.pMem[u.af.i]);
64743
+ REGISTER_TRACE(pOp->p1+u.af.i, &u.af.pMem[u.af.i]);
6470264744
}
6470364745
if( db->mallocFailed ) goto no_mem;
6470464746
6470564747
/* Return SQLITE_ROW
6470664748
*/
@@ -64720,13 +64762,13 @@
6472064762
** It is illegal for P1 and P3 to be the same register. Sometimes,
6472164763
** if P3 is the same register as P2, the implementation is able
6472264764
** to avoid a memcpy().
6472364765
*/
6472464766
case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
64725
-#if 0 /* local variables moved into u.af */
64767
+#if 0 /* local variables moved into u.ag */
6472664768
i64 nByte;
64727
-#endif /* local variables moved into u.af */
64769
+#endif /* local variables moved into u.ag */
6472864770
6472964771
pIn1 = &aMem[pOp->p1];
6473064772
pIn2 = &aMem[pOp->p2];
6473164773
pOut = &aMem[pOp->p3];
6473264774
assert( pIn1!=pOut );
@@ -64735,26 +64777,26 @@
6473564777
break;
6473664778
}
6473764779
if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
6473864780
Stringify(pIn1, encoding);
6473964781
Stringify(pIn2, encoding);
64740
- u.af.nByte = pIn1->n + pIn2->n;
64741
- if( u.af.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
64782
+ u.ag.nByte = pIn1->n + pIn2->n;
64783
+ if( u.ag.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
6474264784
goto too_big;
6474364785
}
6474464786
MemSetTypeFlag(pOut, MEM_Str);
64745
- if( sqlite3VdbeMemGrow(pOut, (int)u.af.nByte+2, pOut==pIn2) ){
64787
+ if( sqlite3VdbeMemGrow(pOut, (int)u.ag.nByte+2, pOut==pIn2) ){
6474664788
goto no_mem;
6474764789
}
6474864790
if( pOut!=pIn2 ){
6474964791
memcpy(pOut->z, pIn2->z, pIn2->n);
6475064792
}
6475164793
memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
64752
- pOut->z[u.af.nByte] = 0;
64753
- pOut->z[u.af.nByte+1] = 0;
64794
+ pOut->z[u.ag.nByte] = 0;
64795
+ pOut->z[u.ag.nByte+1] = 0;
6475464796
pOut->flags |= MEM_Term;
64755
- pOut->n = (int)u.af.nByte;
64797
+ pOut->n = (int)u.ag.nByte;
6475664798
pOut->enc = encoding;
6475764799
UPDATE_MAX_BLOBSIZE(pOut);
6475864800
break;
6475964801
}
6476064802
@@ -64794,80 +64836,80 @@
6479464836
case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
6479564837
case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
6479664838
case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
6479764839
case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
6479864840
case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
64799
-#if 0 /* local variables moved into u.ag */
64841
+#if 0 /* local variables moved into u.ah */
6480064842
int flags; /* Combined MEM_* flags from both inputs */
6480164843
i64 iA; /* Integer value of left operand */
6480264844
i64 iB; /* Integer value of right operand */
6480364845
double rA; /* Real value of left operand */
6480464846
double rB; /* Real value of right operand */
64805
-#endif /* local variables moved into u.ag */
64847
+#endif /* local variables moved into u.ah */
6480664848
6480764849
pIn1 = &aMem[pOp->p1];
6480864850
applyNumericAffinity(pIn1);
6480964851
pIn2 = &aMem[pOp->p2];
6481064852
applyNumericAffinity(pIn2);
6481164853
pOut = &aMem[pOp->p3];
64812
- u.ag.flags = pIn1->flags | pIn2->flags;
64813
- if( (u.ag.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
64854
+ u.ah.flags = pIn1->flags | pIn2->flags;
64855
+ if( (u.ah.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
6481464856
if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
64815
- u.ag.iA = pIn1->u.i;
64816
- u.ag.iB = pIn2->u.i;
64857
+ u.ah.iA = pIn1->u.i;
64858
+ u.ah.iB = pIn2->u.i;
6481764859
switch( pOp->opcode ){
64818
- case OP_Add: if( sqlite3AddInt64(&u.ag.iB,u.ag.iA) ) goto fp_math; break;
64819
- case OP_Subtract: if( sqlite3SubInt64(&u.ag.iB,u.ag.iA) ) goto fp_math; break;
64820
- case OP_Multiply: if( sqlite3MulInt64(&u.ag.iB,u.ag.iA) ) goto fp_math; break;
64860
+ case OP_Add: if( sqlite3AddInt64(&u.ah.iB,u.ah.iA) ) goto fp_math; break;
64861
+ case OP_Subtract: if( sqlite3SubInt64(&u.ah.iB,u.ah.iA) ) goto fp_math; break;
64862
+ case OP_Multiply: if( sqlite3MulInt64(&u.ah.iB,u.ah.iA) ) goto fp_math; break;
6482164863
case OP_Divide: {
64822
- if( u.ag.iA==0 ) goto arithmetic_result_is_null;
64823
- if( u.ag.iA==-1 && u.ag.iB==SMALLEST_INT64 ) goto fp_math;
64824
- u.ag.iB /= u.ag.iA;
64864
+ if( u.ah.iA==0 ) goto arithmetic_result_is_null;
64865
+ if( u.ah.iA==-1 && u.ah.iB==SMALLEST_INT64 ) goto fp_math;
64866
+ u.ah.iB /= u.ah.iA;
6482564867
break;
6482664868
}
6482764869
default: {
64828
- if( u.ag.iA==0 ) goto arithmetic_result_is_null;
64829
- if( u.ag.iA==-1 ) u.ag.iA = 1;
64830
- u.ag.iB %= u.ag.iA;
64870
+ if( u.ah.iA==0 ) goto arithmetic_result_is_null;
64871
+ if( u.ah.iA==-1 ) u.ah.iA = 1;
64872
+ u.ah.iB %= u.ah.iA;
6483164873
break;
6483264874
}
6483364875
}
64834
- pOut->u.i = u.ag.iB;
64876
+ pOut->u.i = u.ah.iB;
6483564877
MemSetTypeFlag(pOut, MEM_Int);
6483664878
}else{
6483764879
fp_math:
64838
- u.ag.rA = sqlite3VdbeRealValue(pIn1);
64839
- u.ag.rB = sqlite3VdbeRealValue(pIn2);
64880
+ u.ah.rA = sqlite3VdbeRealValue(pIn1);
64881
+ u.ah.rB = sqlite3VdbeRealValue(pIn2);
6484064882
switch( pOp->opcode ){
64841
- case OP_Add: u.ag.rB += u.ag.rA; break;
64842
- case OP_Subtract: u.ag.rB -= u.ag.rA; break;
64843
- case OP_Multiply: u.ag.rB *= u.ag.rA; break;
64883
+ case OP_Add: u.ah.rB += u.ah.rA; break;
64884
+ case OP_Subtract: u.ah.rB -= u.ah.rA; break;
64885
+ case OP_Multiply: u.ah.rB *= u.ah.rA; break;
6484464886
case OP_Divide: {
6484564887
/* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
64846
- if( u.ag.rA==(double)0 ) goto arithmetic_result_is_null;
64847
- u.ag.rB /= u.ag.rA;
64888
+ if( u.ah.rA==(double)0 ) goto arithmetic_result_is_null;
64889
+ u.ah.rB /= u.ah.rA;
6484864890
break;
6484964891
}
6485064892
default: {
64851
- u.ag.iA = (i64)u.ag.rA;
64852
- u.ag.iB = (i64)u.ag.rB;
64853
- if( u.ag.iA==0 ) goto arithmetic_result_is_null;
64854
- if( u.ag.iA==-1 ) u.ag.iA = 1;
64855
- u.ag.rB = (double)(u.ag.iB % u.ag.iA);
64893
+ u.ah.iA = (i64)u.ah.rA;
64894
+ u.ah.iB = (i64)u.ah.rB;
64895
+ if( u.ah.iA==0 ) goto arithmetic_result_is_null;
64896
+ if( u.ah.iA==-1 ) u.ah.iA = 1;
64897
+ u.ah.rB = (double)(u.ah.iB % u.ah.iA);
6485664898
break;
6485764899
}
6485864900
}
6485964901
#ifdef SQLITE_OMIT_FLOATING_POINT
64860
- pOut->u.i = u.ag.rB;
64902
+ pOut->u.i = u.ah.rB;
6486164903
MemSetTypeFlag(pOut, MEM_Int);
6486264904
#else
64863
- if( sqlite3IsNaN(u.ag.rB) ){
64905
+ if( sqlite3IsNaN(u.ah.rB) ){
6486464906
goto arithmetic_result_is_null;
6486564907
}
64866
- pOut->r = u.ag.rB;
64908
+ pOut->r = u.ah.rB;
6486764909
MemSetTypeFlag(pOut, MEM_Real);
64868
- if( (u.ag.flags & MEM_Real)==0 ){
64910
+ if( (u.ah.flags & MEM_Real)==0 ){
6486964911
sqlite3VdbeIntegerAffinity(pOut);
6487064912
}
6487164913
#endif
6487264914
}
6487364915
break;
@@ -64915,96 +64957,96 @@
6491564957
** invocation of this opcode.
6491664958
**
6491764959
** See also: AggStep and AggFinal
6491864960
*/
6491964961
case OP_Function: {
64920
-#if 0 /* local variables moved into u.ah */
64962
+#if 0 /* local variables moved into u.ai */
6492164963
int i;
6492264964
Mem *pArg;
6492364965
sqlite3_context ctx;
6492464966
sqlite3_value **apVal;
6492564967
int n;
64926
-#endif /* local variables moved into u.ah */
64968
+#endif /* local variables moved into u.ai */
6492764969
64928
- u.ah.n = pOp->p5;
64929
- u.ah.apVal = p->apArg;
64930
- assert( u.ah.apVal || u.ah.n==0 );
64970
+ u.ai.n = pOp->p5;
64971
+ u.ai.apVal = p->apArg;
64972
+ assert( u.ai.apVal || u.ai.n==0 );
6493164973
assert( pOp->p3>0 && pOp->p3<=p->nMem );
6493264974
pOut = &aMem[pOp->p3];
6493364975
memAboutToChange(p, pOut);
6493464976
64935
- assert( u.ah.n==0 || (pOp->p2>0 && pOp->p2+u.ah.n<=p->nMem+1) );
64936
- assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ah.n );
64937
- u.ah.pArg = &aMem[pOp->p2];
64938
- for(u.ah.i=0; u.ah.i<u.ah.n; u.ah.i++, u.ah.pArg++){
64939
- assert( memIsValid(u.ah.pArg) );
64940
- u.ah.apVal[u.ah.i] = u.ah.pArg;
64941
- Deephemeralize(u.ah.pArg);
64942
- sqlite3VdbeMemStoreType(u.ah.pArg);
64943
- REGISTER_TRACE(pOp->p2+u.ah.i, u.ah.pArg);
64977
+ assert( u.ai.n==0 || (pOp->p2>0 && pOp->p2+u.ai.n<=p->nMem+1) );
64978
+ assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ai.n );
64979
+ u.ai.pArg = &aMem[pOp->p2];
64980
+ for(u.ai.i=0; u.ai.i<u.ai.n; u.ai.i++, u.ai.pArg++){
64981
+ assert( memIsValid(u.ai.pArg) );
64982
+ u.ai.apVal[u.ai.i] = u.ai.pArg;
64983
+ Deephemeralize(u.ai.pArg);
64984
+ sqlite3VdbeMemStoreType(u.ai.pArg);
64985
+ REGISTER_TRACE(pOp->p2+u.ai.i, u.ai.pArg);
6494464986
}
6494564987
6494664988
assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
6494764989
if( pOp->p4type==P4_FUNCDEF ){
64948
- u.ah.ctx.pFunc = pOp->p4.pFunc;
64949
- u.ah.ctx.pVdbeFunc = 0;
64990
+ u.ai.ctx.pFunc = pOp->p4.pFunc;
64991
+ u.ai.ctx.pVdbeFunc = 0;
6495064992
}else{
64951
- u.ah.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
64952
- u.ah.ctx.pFunc = u.ah.ctx.pVdbeFunc->pFunc;
64993
+ u.ai.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
64994
+ u.ai.ctx.pFunc = u.ai.ctx.pVdbeFunc->pFunc;
6495364995
}
6495464996
64955
- u.ah.ctx.s.flags = MEM_Null;
64956
- u.ah.ctx.s.db = db;
64957
- u.ah.ctx.s.xDel = 0;
64958
- u.ah.ctx.s.zMalloc = 0;
64997
+ u.ai.ctx.s.flags = MEM_Null;
64998
+ u.ai.ctx.s.db = db;
64999
+ u.ai.ctx.s.xDel = 0;
65000
+ u.ai.ctx.s.zMalloc = 0;
6495965001
6496065002
/* The output cell may already have a buffer allocated. Move
64961
- ** the pointer to u.ah.ctx.s so in case the user-function can use
65003
+ ** the pointer to u.ai.ctx.s so in case the user-function can use
6496265004
** the already allocated buffer instead of allocating a new one.
6496365005
*/
64964
- sqlite3VdbeMemMove(&u.ah.ctx.s, pOut);
64965
- MemSetTypeFlag(&u.ah.ctx.s, MEM_Null);
65006
+ sqlite3VdbeMemMove(&u.ai.ctx.s, pOut);
65007
+ MemSetTypeFlag(&u.ai.ctx.s, MEM_Null);
6496665008
64967
- u.ah.ctx.isError = 0;
64968
- if( u.ah.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
65009
+ u.ai.ctx.isError = 0;
65010
+ if( u.ai.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
6496965011
assert( pOp>aOp );
6497065012
assert( pOp[-1].p4type==P4_COLLSEQ );
6497165013
assert( pOp[-1].opcode==OP_CollSeq );
64972
- u.ah.ctx.pColl = pOp[-1].p4.pColl;
65014
+ u.ai.ctx.pColl = pOp[-1].p4.pColl;
6497365015
}
6497465016
db->lastRowid = lastRowid;
64975
- (*u.ah.ctx.pFunc->xFunc)(&u.ah.ctx, u.ah.n, u.ah.apVal); /* IMP: R-24505-23230 */
65017
+ (*u.ai.ctx.pFunc->xFunc)(&u.ai.ctx, u.ai.n, u.ai.apVal); /* IMP: R-24505-23230 */
6497665018
lastRowid = db->lastRowid;
6497765019
6497865020
/* If any auxiliary data functions have been called by this user function,
6497965021
** immediately call the destructor for any non-static values.
6498065022
*/
64981
- if( u.ah.ctx.pVdbeFunc ){
64982
- sqlite3VdbeDeleteAuxData(u.ah.ctx.pVdbeFunc, pOp->p1);
64983
- pOp->p4.pVdbeFunc = u.ah.ctx.pVdbeFunc;
65023
+ if( u.ai.ctx.pVdbeFunc ){
65024
+ sqlite3VdbeDeleteAuxData(u.ai.ctx.pVdbeFunc, pOp->p1);
65025
+ pOp->p4.pVdbeFunc = u.ai.ctx.pVdbeFunc;
6498465026
pOp->p4type = P4_VDBEFUNC;
6498565027
}
6498665028
6498765029
if( db->mallocFailed ){
6498865030
/* Even though a malloc() has failed, the implementation of the
6498965031
** user function may have called an sqlite3_result_XXX() function
6499065032
** to return a value. The following call releases any resources
6499165033
** associated with such a value.
6499265034
*/
64993
- sqlite3VdbeMemRelease(&u.ah.ctx.s);
65035
+ sqlite3VdbeMemRelease(&u.ai.ctx.s);
6499465036
goto no_mem;
6499565037
}
6499665038
6499765039
/* If the function returned an error, throw an exception */
64998
- if( u.ah.ctx.isError ){
64999
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ah.ctx.s));
65000
- rc = u.ah.ctx.isError;
65040
+ if( u.ai.ctx.isError ){
65041
+ sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ai.ctx.s));
65042
+ rc = u.ai.ctx.isError;
6500165043
}
6500265044
6500365045
/* Copy the result of the function into register P3 */
65004
- sqlite3VdbeChangeEncoding(&u.ah.ctx.s, encoding);
65005
- sqlite3VdbeMemMove(pOut, &u.ah.ctx.s);
65046
+ sqlite3VdbeChangeEncoding(&u.ai.ctx.s, encoding);
65047
+ sqlite3VdbeMemMove(pOut, &u.ai.ctx.s);
6500665048
if( sqlite3VdbeMemTooBig(pOut) ){
6500765049
goto too_big;
6500865050
}
6500965051
6501065052
#if 0
@@ -65048,56 +65090,56 @@
6504865090
*/
6504965091
case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
6505065092
case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
6505165093
case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
6505265094
case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
65053
-#if 0 /* local variables moved into u.ai */
65095
+#if 0 /* local variables moved into u.aj */
6505465096
i64 iA;
6505565097
u64 uA;
6505665098
i64 iB;
6505765099
u8 op;
65058
-#endif /* local variables moved into u.ai */
65100
+#endif /* local variables moved into u.aj */
6505965101
6506065102
pIn1 = &aMem[pOp->p1];
6506165103
pIn2 = &aMem[pOp->p2];
6506265104
pOut = &aMem[pOp->p3];
6506365105
if( (pIn1->flags | pIn2->flags) & MEM_Null ){
6506465106
sqlite3VdbeMemSetNull(pOut);
6506565107
break;
6506665108
}
65067
- u.ai.iA = sqlite3VdbeIntValue(pIn2);
65068
- u.ai.iB = sqlite3VdbeIntValue(pIn1);
65069
- u.ai.op = pOp->opcode;
65070
- if( u.ai.op==OP_BitAnd ){
65071
- u.ai.iA &= u.ai.iB;
65072
- }else if( u.ai.op==OP_BitOr ){
65073
- u.ai.iA |= u.ai.iB;
65074
- }else if( u.ai.iB!=0 ){
65075
- assert( u.ai.op==OP_ShiftRight || u.ai.op==OP_ShiftLeft );
65109
+ u.aj.iA = sqlite3VdbeIntValue(pIn2);
65110
+ u.aj.iB = sqlite3VdbeIntValue(pIn1);
65111
+ u.aj.op = pOp->opcode;
65112
+ if( u.aj.op==OP_BitAnd ){
65113
+ u.aj.iA &= u.aj.iB;
65114
+ }else if( u.aj.op==OP_BitOr ){
65115
+ u.aj.iA |= u.aj.iB;
65116
+ }else if( u.aj.iB!=0 ){
65117
+ assert( u.aj.op==OP_ShiftRight || u.aj.op==OP_ShiftLeft );
6507665118
6507765119
/* If shifting by a negative amount, shift in the other direction */
65078
- if( u.ai.iB<0 ){
65120
+ if( u.aj.iB<0 ){
6507965121
assert( OP_ShiftRight==OP_ShiftLeft+1 );
65080
- u.ai.op = 2*OP_ShiftLeft + 1 - u.ai.op;
65081
- u.ai.iB = u.ai.iB>(-64) ? -u.ai.iB : 64;
65122
+ u.aj.op = 2*OP_ShiftLeft + 1 - u.aj.op;
65123
+ u.aj.iB = u.aj.iB>(-64) ? -u.aj.iB : 64;
6508265124
}
6508365125
65084
- if( u.ai.iB>=64 ){
65085
- u.ai.iA = (u.ai.iA>=0 || u.ai.op==OP_ShiftLeft) ? 0 : -1;
65086
- }else{
65087
- memcpy(&u.ai.uA, &u.ai.iA, sizeof(u.ai.uA));
65088
- if( u.ai.op==OP_ShiftLeft ){
65089
- u.ai.uA <<= u.ai.iB;
65090
- }else{
65091
- u.ai.uA >>= u.ai.iB;
65126
+ if( u.aj.iB>=64 ){
65127
+ u.aj.iA = (u.aj.iA>=0 || u.aj.op==OP_ShiftLeft) ? 0 : -1;
65128
+ }else{
65129
+ memcpy(&u.aj.uA, &u.aj.iA, sizeof(u.aj.uA));
65130
+ if( u.aj.op==OP_ShiftLeft ){
65131
+ u.aj.uA <<= u.aj.iB;
65132
+ }else{
65133
+ u.aj.uA >>= u.aj.iB;
6509265134
/* Sign-extend on a right shift of a negative number */
65093
- if( u.ai.iA<0 ) u.ai.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.ai.iB);
65135
+ if( u.aj.iA<0 ) u.aj.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.aj.iB);
6509465136
}
65095
- memcpy(&u.ai.iA, &u.ai.uA, sizeof(u.ai.iA));
65137
+ memcpy(&u.aj.iA, &u.aj.uA, sizeof(u.aj.iA));
6509665138
}
6509765139
}
65098
- pOut->u.i = u.ai.iA;
65140
+ pOut->u.i = u.aj.iA;
6509965141
MemSetTypeFlag(pOut, MEM_Int);
6510065142
break;
6510165143
}
6510265144
6510365145
/* Opcode: AddImm P1 P2 * * *
@@ -65285,10 +65327,14 @@
6528565327
** are of different types, then numbers are considered less than
6528665328
** strings and strings are considered less than blobs.
6528765329
**
6528865330
** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
6528965331
** store a boolean result (either 0, or 1, or NULL) in register P2.
65332
+**
65333
+** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
65334
+** equal to one another, provided that they do not have their MEM_Cleared
65335
+** bit set.
6529065336
*/
6529165337
/* Opcode: Ne P1 P2 P3 P4 P5
6529265338
**
6529365339
** This works just like the Lt opcode except that the jump is taken if
6529465340
** the operands in registers P1 and P3 are not equal. See the Lt opcode for
@@ -65334,30 +65380,38 @@
6533465380
case OP_Ne: /* same as TK_NE, jump, in1, in3 */
6533565381
case OP_Lt: /* same as TK_LT, jump, in1, in3 */
6533665382
case OP_Le: /* same as TK_LE, jump, in1, in3 */
6533765383
case OP_Gt: /* same as TK_GT, jump, in1, in3 */
6533865384
case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
65339
-#if 0 /* local variables moved into u.aj */
65385
+#if 0 /* local variables moved into u.ak */
6534065386
int res; /* Result of the comparison of pIn1 against pIn3 */
6534165387
char affinity; /* Affinity to use for comparison */
6534265388
u16 flags1; /* Copy of initial value of pIn1->flags */
6534365389
u16 flags3; /* Copy of initial value of pIn3->flags */
65344
-#endif /* local variables moved into u.aj */
65390
+#endif /* local variables moved into u.ak */
6534565391
6534665392
pIn1 = &aMem[pOp->p1];
6534765393
pIn3 = &aMem[pOp->p3];
65348
- u.aj.flags1 = pIn1->flags;
65349
- u.aj.flags3 = pIn3->flags;
65350
- if( (u.aj.flags1 | u.aj.flags3)&MEM_Null ){
65394
+ u.ak.flags1 = pIn1->flags;
65395
+ u.ak.flags3 = pIn3->flags;
65396
+ if( (u.ak.flags1 | u.ak.flags3)&MEM_Null ){
6535165397
/* One or both operands are NULL */
6535265398
if( pOp->p5 & SQLITE_NULLEQ ){
6535365399
/* If SQLITE_NULLEQ is set (which will only happen if the operator is
6535465400
** OP_Eq or OP_Ne) then take the jump or not depending on whether
6535565401
** or not both operands are null.
6535665402
*/
6535765403
assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
65358
- u.aj.res = (u.aj.flags1 & u.aj.flags3 & MEM_Null)==0;
65404
+ assert( (u.ak.flags1 & MEM_Cleared)==0 );
65405
+ if( (u.ak.flags1&MEM_Null)!=0
65406
+ && (u.ak.flags3&MEM_Null)!=0
65407
+ && (u.ak.flags3&MEM_Cleared)==0
65408
+ ){
65409
+ u.ak.res = 0; /* Results are equal */
65410
+ }else{
65411
+ u.ak.res = 1; /* Results are not equal */
65412
+ }
6535965413
}else{
6536065414
/* SQLITE_NULLEQ is clear and at least one operand is NULL,
6536165415
** then the result is always NULL.
6536265416
** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
6536365417
*/
@@ -65370,44 +65424,44 @@
6537065424
}
6537165425
break;
6537265426
}
6537365427
}else{
6537465428
/* Neither operand is NULL. Do a comparison. */
65375
- u.aj.affinity = pOp->p5 & SQLITE_AFF_MASK;
65376
- if( u.aj.affinity ){
65377
- applyAffinity(pIn1, u.aj.affinity, encoding);
65378
- applyAffinity(pIn3, u.aj.affinity, encoding);
65429
+ u.ak.affinity = pOp->p5 & SQLITE_AFF_MASK;
65430
+ if( u.ak.affinity ){
65431
+ applyAffinity(pIn1, u.ak.affinity, encoding);
65432
+ applyAffinity(pIn3, u.ak.affinity, encoding);
6537965433
if( db->mallocFailed ) goto no_mem;
6538065434
}
6538165435
6538265436
assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
6538365437
ExpandBlob(pIn1);
6538465438
ExpandBlob(pIn3);
65385
- u.aj.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
65439
+ u.ak.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
6538665440
}
6538765441
switch( pOp->opcode ){
65388
- case OP_Eq: u.aj.res = u.aj.res==0; break;
65389
- case OP_Ne: u.aj.res = u.aj.res!=0; break;
65390
- case OP_Lt: u.aj.res = u.aj.res<0; break;
65391
- case OP_Le: u.aj.res = u.aj.res<=0; break;
65392
- case OP_Gt: u.aj.res = u.aj.res>0; break;
65393
- default: u.aj.res = u.aj.res>=0; break;
65442
+ case OP_Eq: u.ak.res = u.ak.res==0; break;
65443
+ case OP_Ne: u.ak.res = u.ak.res!=0; break;
65444
+ case OP_Lt: u.ak.res = u.ak.res<0; break;
65445
+ case OP_Le: u.ak.res = u.ak.res<=0; break;
65446
+ case OP_Gt: u.ak.res = u.ak.res>0; break;
65447
+ default: u.ak.res = u.ak.res>=0; break;
6539465448
}
6539565449
6539665450
if( pOp->p5 & SQLITE_STOREP2 ){
6539765451
pOut = &aMem[pOp->p2];
6539865452
memAboutToChange(p, pOut);
6539965453
MemSetTypeFlag(pOut, MEM_Int);
65400
- pOut->u.i = u.aj.res;
65454
+ pOut->u.i = u.ak.res;
6540165455
REGISTER_TRACE(pOp->p2, pOut);
65402
- }else if( u.aj.res ){
65456
+ }else if( u.ak.res ){
6540365457
pc = pOp->p2-1;
6540465458
}
6540565459
6540665460
/* Undo any changes made by applyAffinity() to the input registers. */
65407
- pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.aj.flags1&MEM_TypeMask);
65408
- pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.aj.flags3&MEM_TypeMask);
65461
+ pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.ak.flags1&MEM_TypeMask);
65462
+ pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.ak.flags3&MEM_TypeMask);
6540965463
break;
6541065464
}
6541165465
6541265466
/* Opcode: Permutation * * * P4 *
6541365467
**
@@ -65438,50 +65492,50 @@
6543865492
** The comparison is a sort comparison, so NULLs compare equal,
6543965493
** NULLs are less than numbers, numbers are less than strings,
6544065494
** and strings are less than blobs.
6544165495
*/
6544265496
case OP_Compare: {
65443
-#if 0 /* local variables moved into u.ak */
65497
+#if 0 /* local variables moved into u.al */
6544465498
int n;
6544565499
int i;
6544665500
int p1;
6544765501
int p2;
6544865502
const KeyInfo *pKeyInfo;
6544965503
int idx;
6545065504
CollSeq *pColl; /* Collating sequence to use on this term */
6545165505
int bRev; /* True for DESCENDING sort order */
65452
-#endif /* local variables moved into u.ak */
65453
-
65454
- u.ak.n = pOp->p3;
65455
- u.ak.pKeyInfo = pOp->p4.pKeyInfo;
65456
- assert( u.ak.n>0 );
65457
- assert( u.ak.pKeyInfo!=0 );
65458
- u.ak.p1 = pOp->p1;
65459
- u.ak.p2 = pOp->p2;
65506
+#endif /* local variables moved into u.al */
65507
+
65508
+ u.al.n = pOp->p3;
65509
+ u.al.pKeyInfo = pOp->p4.pKeyInfo;
65510
+ assert( u.al.n>0 );
65511
+ assert( u.al.pKeyInfo!=0 );
65512
+ u.al.p1 = pOp->p1;
65513
+ u.al.p2 = pOp->p2;
6546065514
#if SQLITE_DEBUG
6546165515
if( aPermute ){
6546265516
int k, mx = 0;
65463
- for(k=0; k<u.ak.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
65464
- assert( u.ak.p1>0 && u.ak.p1+mx<=p->nMem+1 );
65465
- assert( u.ak.p2>0 && u.ak.p2+mx<=p->nMem+1 );
65517
+ for(k=0; k<u.al.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
65518
+ assert( u.al.p1>0 && u.al.p1+mx<=p->nMem+1 );
65519
+ assert( u.al.p2>0 && u.al.p2+mx<=p->nMem+1 );
6546665520
}else{
65467
- assert( u.ak.p1>0 && u.ak.p1+u.ak.n<=p->nMem+1 );
65468
- assert( u.ak.p2>0 && u.ak.p2+u.ak.n<=p->nMem+1 );
65521
+ assert( u.al.p1>0 && u.al.p1+u.al.n<=p->nMem+1 );
65522
+ assert( u.al.p2>0 && u.al.p2+u.al.n<=p->nMem+1 );
6546965523
}
6547065524
#endif /* SQLITE_DEBUG */
65471
- for(u.ak.i=0; u.ak.i<u.ak.n; u.ak.i++){
65472
- u.ak.idx = aPermute ? aPermute[u.ak.i] : u.ak.i;
65473
- assert( memIsValid(&aMem[u.ak.p1+u.ak.idx]) );
65474
- assert( memIsValid(&aMem[u.ak.p2+u.ak.idx]) );
65475
- REGISTER_TRACE(u.ak.p1+u.ak.idx, &aMem[u.ak.p1+u.ak.idx]);
65476
- REGISTER_TRACE(u.ak.p2+u.ak.idx, &aMem[u.ak.p2+u.ak.idx]);
65477
- assert( u.ak.i<u.ak.pKeyInfo->nField );
65478
- u.ak.pColl = u.ak.pKeyInfo->aColl[u.ak.i];
65479
- u.ak.bRev = u.ak.pKeyInfo->aSortOrder[u.ak.i];
65480
- iCompare = sqlite3MemCompare(&aMem[u.ak.p1+u.ak.idx], &aMem[u.ak.p2+u.ak.idx], u.ak.pColl);
65525
+ for(u.al.i=0; u.al.i<u.al.n; u.al.i++){
65526
+ u.al.idx = aPermute ? aPermute[u.al.i] : u.al.i;
65527
+ assert( memIsValid(&aMem[u.al.p1+u.al.idx]) );
65528
+ assert( memIsValid(&aMem[u.al.p2+u.al.idx]) );
65529
+ REGISTER_TRACE(u.al.p1+u.al.idx, &aMem[u.al.p1+u.al.idx]);
65530
+ REGISTER_TRACE(u.al.p2+u.al.idx, &aMem[u.al.p2+u.al.idx]);
65531
+ assert( u.al.i<u.al.pKeyInfo->nField );
65532
+ u.al.pColl = u.al.pKeyInfo->aColl[u.al.i];
65533
+ u.al.bRev = u.al.pKeyInfo->aSortOrder[u.al.i];
65534
+ iCompare = sqlite3MemCompare(&aMem[u.al.p1+u.al.idx], &aMem[u.al.p2+u.al.idx], u.al.pColl);
6548165535
if( iCompare ){
65482
- if( u.ak.bRev ) iCompare = -iCompare;
65536
+ if( u.al.bRev ) iCompare = -iCompare;
6548365537
break;
6548465538
}
6548565539
}
6548665540
aPermute = 0;
6548765541
break;
@@ -65522,39 +65576,39 @@
6552265576
** even if the other input is NULL. A NULL and false or two NULLs
6552365577
** give a NULL output.
6552465578
*/
6552565579
case OP_And: /* same as TK_AND, in1, in2, out3 */
6552665580
case OP_Or: { /* same as TK_OR, in1, in2, out3 */
65527
-#if 0 /* local variables moved into u.al */
65581
+#if 0 /* local variables moved into u.am */
6552865582
int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
6552965583
int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
65530
-#endif /* local variables moved into u.al */
65584
+#endif /* local variables moved into u.am */
6553165585
6553265586
pIn1 = &aMem[pOp->p1];
6553365587
if( pIn1->flags & MEM_Null ){
65534
- u.al.v1 = 2;
65588
+ u.am.v1 = 2;
6553565589
}else{
65536
- u.al.v1 = sqlite3VdbeIntValue(pIn1)!=0;
65590
+ u.am.v1 = sqlite3VdbeIntValue(pIn1)!=0;
6553765591
}
6553865592
pIn2 = &aMem[pOp->p2];
6553965593
if( pIn2->flags & MEM_Null ){
65540
- u.al.v2 = 2;
65594
+ u.am.v2 = 2;
6554165595
}else{
65542
- u.al.v2 = sqlite3VdbeIntValue(pIn2)!=0;
65596
+ u.am.v2 = sqlite3VdbeIntValue(pIn2)!=0;
6554365597
}
6554465598
if( pOp->opcode==OP_And ){
6554565599
static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
65546
- u.al.v1 = and_logic[u.al.v1*3+u.al.v2];
65600
+ u.am.v1 = and_logic[u.am.v1*3+u.am.v2];
6554765601
}else{
6554865602
static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
65549
- u.al.v1 = or_logic[u.al.v1*3+u.al.v2];
65603
+ u.am.v1 = or_logic[u.am.v1*3+u.am.v2];
6555065604
}
6555165605
pOut = &aMem[pOp->p3];
65552
- if( u.al.v1==2 ){
65606
+ if( u.am.v1==2 ){
6555365607
MemSetTypeFlag(pOut, MEM_Null);
6555465608
}else{
65555
- pOut->u.i = u.al.v1;
65609
+ pOut->u.i = u.am.v1;
6555665610
MemSetTypeFlag(pOut, MEM_Int);
6555765611
}
6555865612
break;
6555965613
}
6556065614
@@ -65621,25 +65675,25 @@
6562165675
** is considered false if it has a numeric value of zero. If the value
6562265676
** in P1 is NULL then take the jump if P3 is zero.
6562365677
*/
6562465678
case OP_If: /* jump, in1 */
6562565679
case OP_IfNot: { /* jump, in1 */
65626
-#if 0 /* local variables moved into u.am */
65680
+#if 0 /* local variables moved into u.an */
6562765681
int c;
65628
-#endif /* local variables moved into u.am */
65682
+#endif /* local variables moved into u.an */
6562965683
pIn1 = &aMem[pOp->p1];
6563065684
if( pIn1->flags & MEM_Null ){
65631
- u.am.c = pOp->p3;
65685
+ u.an.c = pOp->p3;
6563265686
}else{
6563365687
#ifdef SQLITE_OMIT_FLOATING_POINT
65634
- u.am.c = sqlite3VdbeIntValue(pIn1)!=0;
65688
+ u.an.c = sqlite3VdbeIntValue(pIn1)!=0;
6563565689
#else
65636
- u.am.c = sqlite3VdbeRealValue(pIn1)!=0.0;
65690
+ u.an.c = sqlite3VdbeRealValue(pIn1)!=0.0;
6563765691
#endif
65638
- if( pOp->opcode==OP_IfNot ) u.am.c = !u.am.c;
65692
+ if( pOp->opcode==OP_IfNot ) u.an.c = !u.an.c;
6563965693
}
65640
- if( u.am.c ){
65694
+ if( u.an.c ){
6564165695
pc = pOp->p2-1;
6564265696
}
6564365697
break;
6564465698
}
6564565699
@@ -65690,11 +65744,11 @@
6569065744
** the result is guaranteed to only be used as the argument of a length()
6569165745
** or typeof() function, respectively. The loading of large blobs can be
6569265746
** skipped for length() and all content loading can be skipped for typeof().
6569365747
*/
6569465748
case OP_Column: {
65695
-#if 0 /* local variables moved into u.an */
65749
+#if 0 /* local variables moved into u.ao */
6569665750
u32 payloadSize; /* Number of bytes in the record */
6569765751
i64 payloadSize64; /* Number of bytes in the record */
6569865752
int p1; /* P1 value of the opcode */
6569965753
int p2; /* column number to retrieve */
6570065754
VdbeCursor *pC; /* The VDBE cursor */
@@ -65714,130 +65768,130 @@
6571465768
u32 szField; /* Number of bytes in the content of a field */
6571565769
int szHdr; /* Size of the header size field at start of record */
6571665770
int avail; /* Number of bytes of available data */
6571765771
u32 t; /* A type code from the record header */
6571865772
Mem *pReg; /* PseudoTable input register */
65719
-#endif /* local variables moved into u.an */
65773
+#endif /* local variables moved into u.ao */
6572065774
6572165775
65722
- u.an.p1 = pOp->p1;
65723
- u.an.p2 = pOp->p2;
65724
- u.an.pC = 0;
65725
- memset(&u.an.sMem, 0, sizeof(u.an.sMem));
65726
- assert( u.an.p1<p->nCursor );
65776
+ u.ao.p1 = pOp->p1;
65777
+ u.ao.p2 = pOp->p2;
65778
+ u.ao.pC = 0;
65779
+ memset(&u.ao.sMem, 0, sizeof(u.ao.sMem));
65780
+ assert( u.ao.p1<p->nCursor );
6572765781
assert( pOp->p3>0 && pOp->p3<=p->nMem );
65728
- u.an.pDest = &aMem[pOp->p3];
65729
- memAboutToChange(p, u.an.pDest);
65730
- u.an.zRec = 0;
65782
+ u.ao.pDest = &aMem[pOp->p3];
65783
+ memAboutToChange(p, u.ao.pDest);
65784
+ u.ao.zRec = 0;
6573165785
65732
- /* This block sets the variable u.an.payloadSize to be the total number of
65786
+ /* This block sets the variable u.ao.payloadSize to be the total number of
6573365787
** bytes in the record.
6573465788
**
65735
- ** u.an.zRec is set to be the complete text of the record if it is available.
65789
+ ** u.ao.zRec is set to be the complete text of the record if it is available.
6573665790
** The complete record text is always available for pseudo-tables
6573765791
** If the record is stored in a cursor, the complete record text
65738
- ** might be available in the u.an.pC->aRow cache. Or it might not be.
65739
- ** If the data is unavailable, u.an.zRec is set to NULL.
65792
+ ** might be available in the u.ao.pC->aRow cache. Or it might not be.
65793
+ ** If the data is unavailable, u.ao.zRec is set to NULL.
6574065794
**
6574165795
** We also compute the number of columns in the record. For cursors,
6574265796
** the number of columns is stored in the VdbeCursor.nField element.
6574365797
*/
65744
- u.an.pC = p->apCsr[u.an.p1];
65745
- assert( u.an.pC!=0 );
65798
+ u.ao.pC = p->apCsr[u.ao.p1];
65799
+ assert( u.ao.pC!=0 );
6574665800
#ifndef SQLITE_OMIT_VIRTUALTABLE
65747
- assert( u.an.pC->pVtabCursor==0 );
65801
+ assert( u.ao.pC->pVtabCursor==0 );
6574865802
#endif
65749
- u.an.pCrsr = u.an.pC->pCursor;
65750
- if( u.an.pCrsr!=0 ){
65803
+ u.ao.pCrsr = u.ao.pC->pCursor;
65804
+ if( u.ao.pCrsr!=0 ){
6575165805
/* The record is stored in a B-Tree */
65752
- rc = sqlite3VdbeCursorMoveto(u.an.pC);
65806
+ rc = sqlite3VdbeCursorMoveto(u.ao.pC);
6575365807
if( rc ) goto abort_due_to_error;
65754
- if( u.an.pC->nullRow ){
65755
- u.an.payloadSize = 0;
65756
- }else if( u.an.pC->cacheStatus==p->cacheCtr ){
65757
- u.an.payloadSize = u.an.pC->payloadSize;
65758
- u.an.zRec = (char*)u.an.pC->aRow;
65759
- }else if( u.an.pC->isIndex ){
65760
- assert( sqlite3BtreeCursorIsValid(u.an.pCrsr) );
65761
- VVA_ONLY(rc =) sqlite3BtreeKeySize(u.an.pCrsr, &u.an.payloadSize64);
65808
+ if( u.ao.pC->nullRow ){
65809
+ u.ao.payloadSize = 0;
65810
+ }else if( u.ao.pC->cacheStatus==p->cacheCtr ){
65811
+ u.ao.payloadSize = u.ao.pC->payloadSize;
65812
+ u.ao.zRec = (char*)u.ao.pC->aRow;
65813
+ }else if( u.ao.pC->isIndex ){
65814
+ assert( sqlite3BtreeCursorIsValid(u.ao.pCrsr) );
65815
+ VVA_ONLY(rc =) sqlite3BtreeKeySize(u.ao.pCrsr, &u.ao.payloadSize64);
6576265816
assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
6576365817
/* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
65764
- ** payload size, so it is impossible for u.an.payloadSize64 to be
65818
+ ** payload size, so it is impossible for u.ao.payloadSize64 to be
6576565819
** larger than 32 bits. */
65766
- assert( (u.an.payloadSize64 & SQLITE_MAX_U32)==(u64)u.an.payloadSize64 );
65767
- u.an.payloadSize = (u32)u.an.payloadSize64;
65820
+ assert( (u.ao.payloadSize64 & SQLITE_MAX_U32)==(u64)u.ao.payloadSize64 );
65821
+ u.ao.payloadSize = (u32)u.ao.payloadSize64;
6576865822
}else{
65769
- assert( sqlite3BtreeCursorIsValid(u.an.pCrsr) );
65770
- VVA_ONLY(rc =) sqlite3BtreeDataSize(u.an.pCrsr, &u.an.payloadSize);
65823
+ assert( sqlite3BtreeCursorIsValid(u.ao.pCrsr) );
65824
+ VVA_ONLY(rc =) sqlite3BtreeDataSize(u.ao.pCrsr, &u.ao.payloadSize);
6577165825
assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
6577265826
}
65773
- }else if( ALWAYS(u.an.pC->pseudoTableReg>0) ){
65774
- u.an.pReg = &aMem[u.an.pC->pseudoTableReg];
65775
- assert( u.an.pReg->flags & MEM_Blob );
65776
- assert( memIsValid(u.an.pReg) );
65777
- u.an.payloadSize = u.an.pReg->n;
65778
- u.an.zRec = u.an.pReg->z;
65779
- u.an.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
65780
- assert( u.an.payloadSize==0 || u.an.zRec!=0 );
65827
+ }else if( ALWAYS(u.ao.pC->pseudoTableReg>0) ){
65828
+ u.ao.pReg = &aMem[u.ao.pC->pseudoTableReg];
65829
+ assert( u.ao.pReg->flags & MEM_Blob );
65830
+ assert( memIsValid(u.ao.pReg) );
65831
+ u.ao.payloadSize = u.ao.pReg->n;
65832
+ u.ao.zRec = u.ao.pReg->z;
65833
+ u.ao.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
65834
+ assert( u.ao.payloadSize==0 || u.ao.zRec!=0 );
6578165835
}else{
6578265836
/* Consider the row to be NULL */
65783
- u.an.payloadSize = 0;
65837
+ u.ao.payloadSize = 0;
6578465838
}
6578565839
65786
- /* If u.an.payloadSize is 0, then just store a NULL. This can happen because of
65840
+ /* If u.ao.payloadSize is 0, then just store a NULL. This can happen because of
6578765841
** nullRow or because of a corrupt database. */
65788
- if( u.an.payloadSize==0 ){
65789
- MemSetTypeFlag(u.an.pDest, MEM_Null);
65842
+ if( u.ao.payloadSize==0 ){
65843
+ MemSetTypeFlag(u.ao.pDest, MEM_Null);
6579065844
goto op_column_out;
6579165845
}
6579265846
assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
65793
- if( u.an.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
65847
+ if( u.ao.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
6579465848
goto too_big;
6579565849
}
6579665850
65797
- u.an.nField = u.an.pC->nField;
65798
- assert( u.an.p2<u.an.nField );
65851
+ u.ao.nField = u.ao.pC->nField;
65852
+ assert( u.ao.p2<u.ao.nField );
6579965853
6580065854
/* Read and parse the table header. Store the results of the parse
6580165855
** into the record header cache fields of the cursor.
6580265856
*/
65803
- u.an.aType = u.an.pC->aType;
65804
- if( u.an.pC->cacheStatus==p->cacheCtr ){
65805
- u.an.aOffset = u.an.pC->aOffset;
65857
+ u.ao.aType = u.ao.pC->aType;
65858
+ if( u.ao.pC->cacheStatus==p->cacheCtr ){
65859
+ u.ao.aOffset = u.ao.pC->aOffset;
6580665860
}else{
65807
- assert(u.an.aType);
65808
- u.an.avail = 0;
65809
- u.an.pC->aOffset = u.an.aOffset = &u.an.aType[u.an.nField];
65810
- u.an.pC->payloadSize = u.an.payloadSize;
65811
- u.an.pC->cacheStatus = p->cacheCtr;
65861
+ assert(u.ao.aType);
65862
+ u.ao.avail = 0;
65863
+ u.ao.pC->aOffset = u.ao.aOffset = &u.ao.aType[u.ao.nField];
65864
+ u.ao.pC->payloadSize = u.ao.payloadSize;
65865
+ u.ao.pC->cacheStatus = p->cacheCtr;
6581265866
6581365867
/* Figure out how many bytes are in the header */
65814
- if( u.an.zRec ){
65815
- u.an.zData = u.an.zRec;
65868
+ if( u.ao.zRec ){
65869
+ u.ao.zData = u.ao.zRec;
6581665870
}else{
65817
- if( u.an.pC->isIndex ){
65818
- u.an.zData = (char*)sqlite3BtreeKeyFetch(u.an.pCrsr, &u.an.avail);
65871
+ if( u.ao.pC->isIndex ){
65872
+ u.ao.zData = (char*)sqlite3BtreeKeyFetch(u.ao.pCrsr, &u.ao.avail);
6581965873
}else{
65820
- u.an.zData = (char*)sqlite3BtreeDataFetch(u.an.pCrsr, &u.an.avail);
65874
+ u.ao.zData = (char*)sqlite3BtreeDataFetch(u.ao.pCrsr, &u.ao.avail);
6582165875
}
6582265876
/* If KeyFetch()/DataFetch() managed to get the entire payload,
65823
- ** save the payload in the u.an.pC->aRow cache. That will save us from
65877
+ ** save the payload in the u.ao.pC->aRow cache. That will save us from
6582465878
** having to make additional calls to fetch the content portion of
6582565879
** the record.
6582665880
*/
65827
- assert( u.an.avail>=0 );
65828
- if( u.an.payloadSize <= (u32)u.an.avail ){
65829
- u.an.zRec = u.an.zData;
65830
- u.an.pC->aRow = (u8*)u.an.zData;
65881
+ assert( u.ao.avail>=0 );
65882
+ if( u.ao.payloadSize <= (u32)u.ao.avail ){
65883
+ u.ao.zRec = u.ao.zData;
65884
+ u.ao.pC->aRow = (u8*)u.ao.zData;
6583165885
}else{
65832
- u.an.pC->aRow = 0;
65886
+ u.ao.pC->aRow = 0;
6583365887
}
6583465888
}
6583565889
/* The following assert is true in all cases except when
6583665890
** the database file has been corrupted externally.
65837
- ** assert( u.an.zRec!=0 || u.an.avail>=u.an.payloadSize || u.an.avail>=9 ); */
65838
- u.an.szHdr = getVarint32((u8*)u.an.zData, u.an.offset);
65891
+ ** assert( u.ao.zRec!=0 || u.ao.avail>=u.ao.payloadSize || u.ao.avail>=9 ); */
65892
+ u.ao.szHdr = getVarint32((u8*)u.ao.zData, u.ao.offset);
6583965893
6584065894
/* Make sure a corrupt database has not given us an oversize header.
6584165895
** Do this now to avoid an oversize memory allocation.
6584265896
**
6584365897
** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
@@ -65844,161 +65898,161 @@
6584465898
** types use so much data space that there can only be 4096 and 32 of
6584565899
** them, respectively. So the maximum header length results from a
6584665900
** 3-byte type for each of the maximum of 32768 columns plus three
6584765901
** extra bytes for the header length itself. 32768*3 + 3 = 98307.
6584865902
*/
65849
- if( u.an.offset > 98307 ){
65903
+ if( u.ao.offset > 98307 ){
6585065904
rc = SQLITE_CORRUPT_BKPT;
6585165905
goto op_column_out;
6585265906
}
6585365907
65854
- /* Compute in u.an.len the number of bytes of data we need to read in order
65855
- ** to get u.an.nField type values. u.an.offset is an upper bound on this. But
65856
- ** u.an.nField might be significantly less than the true number of columns
65857
- ** in the table, and in that case, 5*u.an.nField+3 might be smaller than u.an.offset.
65858
- ** We want to minimize u.an.len in order to limit the size of the memory
65859
- ** allocation, especially if a corrupt database file has caused u.an.offset
65908
+ /* Compute in u.ao.len the number of bytes of data we need to read in order
65909
+ ** to get u.ao.nField type values. u.ao.offset is an upper bound on this. But
65910
+ ** u.ao.nField might be significantly less than the true number of columns
65911
+ ** in the table, and in that case, 5*u.ao.nField+3 might be smaller than u.ao.offset.
65912
+ ** We want to minimize u.ao.len in order to limit the size of the memory
65913
+ ** allocation, especially if a corrupt database file has caused u.ao.offset
6586065914
** to be oversized. Offset is limited to 98307 above. But 98307 might
6586165915
** still exceed Robson memory allocation limits on some configurations.
65862
- ** On systems that cannot tolerate large memory allocations, u.an.nField*5+3
65863
- ** will likely be much smaller since u.an.nField will likely be less than
65916
+ ** On systems that cannot tolerate large memory allocations, u.ao.nField*5+3
65917
+ ** will likely be much smaller since u.ao.nField will likely be less than
6586465918
** 20 or so. This insures that Robson memory allocation limits are
6586565919
** not exceeded even for corrupt database files.
6586665920
*/
65867
- u.an.len = u.an.nField*5 + 3;
65868
- if( u.an.len > (int)u.an.offset ) u.an.len = (int)u.an.offset;
65921
+ u.ao.len = u.ao.nField*5 + 3;
65922
+ if( u.ao.len > (int)u.ao.offset ) u.ao.len = (int)u.ao.offset;
6586965923
6587065924
/* The KeyFetch() or DataFetch() above are fast and will get the entire
6587165925
** record header in most cases. But they will fail to get the complete
6587265926
** record header if the record header does not fit on a single page
6587365927
** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
6587465928
** acquire the complete header text.
6587565929
*/
65876
- if( !u.an.zRec && u.an.avail<u.an.len ){
65877
- u.an.sMem.flags = 0;
65878
- u.an.sMem.db = 0;
65879
- rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, 0, u.an.len, u.an.pC->isIndex, &u.an.sMem);
65930
+ if( !u.ao.zRec && u.ao.avail<u.ao.len ){
65931
+ u.ao.sMem.flags = 0;
65932
+ u.ao.sMem.db = 0;
65933
+ rc = sqlite3VdbeMemFromBtree(u.ao.pCrsr, 0, u.ao.len, u.ao.pC->isIndex, &u.ao.sMem);
6588065934
if( rc!=SQLITE_OK ){
6588165935
goto op_column_out;
6588265936
}
65883
- u.an.zData = u.an.sMem.z;
65884
- }
65885
- u.an.zEndHdr = (u8 *)&u.an.zData[u.an.len];
65886
- u.an.zIdx = (u8 *)&u.an.zData[u.an.szHdr];
65887
-
65888
- /* Scan the header and use it to fill in the u.an.aType[] and u.an.aOffset[]
65889
- ** arrays. u.an.aType[u.an.i] will contain the type integer for the u.an.i-th
65890
- ** column and u.an.aOffset[u.an.i] will contain the u.an.offset from the beginning
65891
- ** of the record to the start of the data for the u.an.i-th column
65892
- */
65893
- for(u.an.i=0; u.an.i<u.an.nField; u.an.i++){
65894
- if( u.an.zIdx<u.an.zEndHdr ){
65895
- u.an.aOffset[u.an.i] = u.an.offset;
65896
- if( u.an.zIdx[0]<0x80 ){
65897
- u.an.t = u.an.zIdx[0];
65898
- u.an.zIdx++;
65937
+ u.ao.zData = u.ao.sMem.z;
65938
+ }
65939
+ u.ao.zEndHdr = (u8 *)&u.ao.zData[u.ao.len];
65940
+ u.ao.zIdx = (u8 *)&u.ao.zData[u.ao.szHdr];
65941
+
65942
+ /* Scan the header and use it to fill in the u.ao.aType[] and u.ao.aOffset[]
65943
+ ** arrays. u.ao.aType[u.ao.i] will contain the type integer for the u.ao.i-th
65944
+ ** column and u.ao.aOffset[u.ao.i] will contain the u.ao.offset from the beginning
65945
+ ** of the record to the start of the data for the u.ao.i-th column
65946
+ */
65947
+ for(u.ao.i=0; u.ao.i<u.ao.nField; u.ao.i++){
65948
+ if( u.ao.zIdx<u.ao.zEndHdr ){
65949
+ u.ao.aOffset[u.ao.i] = u.ao.offset;
65950
+ if( u.ao.zIdx[0]<0x80 ){
65951
+ u.ao.t = u.ao.zIdx[0];
65952
+ u.ao.zIdx++;
6589965953
}else{
65900
- u.an.zIdx += sqlite3GetVarint32(u.an.zIdx, &u.an.t);
65954
+ u.ao.zIdx += sqlite3GetVarint32(u.ao.zIdx, &u.ao.t);
6590165955
}
65902
- u.an.aType[u.an.i] = u.an.t;
65903
- u.an.szField = sqlite3VdbeSerialTypeLen(u.an.t);
65904
- u.an.offset += u.an.szField;
65905
- if( u.an.offset<u.an.szField ){ /* True if u.an.offset overflows */
65906
- u.an.zIdx = &u.an.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
65956
+ u.ao.aType[u.ao.i] = u.ao.t;
65957
+ u.ao.szField = sqlite3VdbeSerialTypeLen(u.ao.t);
65958
+ u.ao.offset += u.ao.szField;
65959
+ if( u.ao.offset<u.ao.szField ){ /* True if u.ao.offset overflows */
65960
+ u.ao.zIdx = &u.ao.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
6590765961
break;
6590865962
}
6590965963
}else{
65910
- /* If u.an.i is less that u.an.nField, then there are fewer fields in this
65964
+ /* If u.ao.i is less that u.ao.nField, then there are fewer fields in this
6591165965
** record than SetNumColumns indicated there are columns in the
65912
- ** table. Set the u.an.offset for any extra columns not present in
65966
+ ** table. Set the u.ao.offset for any extra columns not present in
6591365967
** the record to 0. This tells code below to store the default value
6591465968
** for the column instead of deserializing a value from the record.
6591565969
*/
65916
- u.an.aOffset[u.an.i] = 0;
65970
+ u.ao.aOffset[u.ao.i] = 0;
6591765971
}
6591865972
}
65919
- sqlite3VdbeMemRelease(&u.an.sMem);
65920
- u.an.sMem.flags = MEM_Null;
65973
+ sqlite3VdbeMemRelease(&u.ao.sMem);
65974
+ u.ao.sMem.flags = MEM_Null;
6592165975
6592265976
/* If we have read more header data than was contained in the header,
6592365977
** or if the end of the last field appears to be past the end of the
6592465978
** record, or if the end of the last field appears to be before the end
6592565979
** of the record (when all fields present), then we must be dealing
6592665980
** with a corrupt database.
6592765981
*/
65928
- if( (u.an.zIdx > u.an.zEndHdr) || (u.an.offset > u.an.payloadSize)
65929
- || (u.an.zIdx==u.an.zEndHdr && u.an.offset!=u.an.payloadSize) ){
65982
+ if( (u.ao.zIdx > u.ao.zEndHdr) || (u.ao.offset > u.ao.payloadSize)
65983
+ || (u.ao.zIdx==u.ao.zEndHdr && u.ao.offset!=u.ao.payloadSize) ){
6593065984
rc = SQLITE_CORRUPT_BKPT;
6593165985
goto op_column_out;
6593265986
}
6593365987
}
6593465988
65935
- /* Get the column information. If u.an.aOffset[u.an.p2] is non-zero, then
65936
- ** deserialize the value from the record. If u.an.aOffset[u.an.p2] is zero,
65989
+ /* Get the column information. If u.ao.aOffset[u.ao.p2] is non-zero, then
65990
+ ** deserialize the value from the record. If u.ao.aOffset[u.ao.p2] is zero,
6593765991
** then there are not enough fields in the record to satisfy the
6593865992
** request. In this case, set the value NULL or to P4 if P4 is
6593965993
** a pointer to a Mem object.
6594065994
*/
65941
- if( u.an.aOffset[u.an.p2] ){
65995
+ if( u.ao.aOffset[u.ao.p2] ){
6594265996
assert( rc==SQLITE_OK );
65943
- if( u.an.zRec ){
65997
+ if( u.ao.zRec ){
6594465998
/* This is the common case where the whole row fits on a single page */
65945
- VdbeMemRelease(u.an.pDest);
65946
- sqlite3VdbeSerialGet((u8 *)&u.an.zRec[u.an.aOffset[u.an.p2]], u.an.aType[u.an.p2], u.an.pDest);
65999
+ VdbeMemRelease(u.ao.pDest);
66000
+ sqlite3VdbeSerialGet((u8 *)&u.ao.zRec[u.ao.aOffset[u.ao.p2]], u.ao.aType[u.ao.p2], u.ao.pDest);
6594766001
}else{
6594866002
/* This branch happens only when the row overflows onto multiple pages */
65949
- u.an.t = u.an.aType[u.an.p2];
66003
+ u.ao.t = u.ao.aType[u.ao.p2];
6595066004
if( (pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
65951
- && ((u.an.t>=12 && (u.an.t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)
66005
+ && ((u.ao.t>=12 && (u.ao.t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)
6595266006
){
6595366007
/* Content is irrelevant for the typeof() function and for
6595466008
** the length(X) function if X is a blob. So we might as well use
6595566009
** bogus content rather than reading content from disk. NULL works
65956
- ** for text and blob and whatever is in the u.an.payloadSize64 variable
66010
+ ** for text and blob and whatever is in the u.ao.payloadSize64 variable
6595766011
** will work for everything else. */
65958
- u.an.zData = u.an.t<12 ? (char*)&u.an.payloadSize64 : 0;
66012
+ u.ao.zData = u.ao.t<12 ? (char*)&u.ao.payloadSize64 : 0;
6595966013
}else{
65960
- u.an.len = sqlite3VdbeSerialTypeLen(u.an.t);
65961
- sqlite3VdbeMemMove(&u.an.sMem, u.an.pDest);
65962
- rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, u.an.aOffset[u.an.p2], u.an.len, u.an.pC->isIndex,
65963
- &u.an.sMem);
66014
+ u.ao.len = sqlite3VdbeSerialTypeLen(u.ao.t);
66015
+ sqlite3VdbeMemMove(&u.ao.sMem, u.ao.pDest);
66016
+ rc = sqlite3VdbeMemFromBtree(u.ao.pCrsr, u.ao.aOffset[u.ao.p2], u.ao.len, u.ao.pC->isIndex,
66017
+ &u.ao.sMem);
6596466018
if( rc!=SQLITE_OK ){
6596566019
goto op_column_out;
6596666020
}
65967
- u.an.zData = u.an.sMem.z;
66021
+ u.ao.zData = u.ao.sMem.z;
6596866022
}
65969
- sqlite3VdbeSerialGet((u8*)u.an.zData, u.an.t, u.an.pDest);
66023
+ sqlite3VdbeSerialGet((u8*)u.ao.zData, u.ao.t, u.ao.pDest);
6597066024
}
65971
- u.an.pDest->enc = encoding;
66025
+ u.ao.pDest->enc = encoding;
6597266026
}else{
6597366027
if( pOp->p4type==P4_MEM ){
65974
- sqlite3VdbeMemShallowCopy(u.an.pDest, pOp->p4.pMem, MEM_Static);
66028
+ sqlite3VdbeMemShallowCopy(u.ao.pDest, pOp->p4.pMem, MEM_Static);
6597566029
}else{
65976
- MemSetTypeFlag(u.an.pDest, MEM_Null);
66030
+ MemSetTypeFlag(u.ao.pDest, MEM_Null);
6597766031
}
6597866032
}
6597966033
6598066034
/* If we dynamically allocated space to hold the data (in the
6598166035
** sqlite3VdbeMemFromBtree() call above) then transfer control of that
65982
- ** dynamically allocated space over to the u.an.pDest structure.
66036
+ ** dynamically allocated space over to the u.ao.pDest structure.
6598366037
** This prevents a memory copy.
6598466038
*/
65985
- if( u.an.sMem.zMalloc ){
65986
- assert( u.an.sMem.z==u.an.sMem.zMalloc );
65987
- assert( !(u.an.pDest->flags & MEM_Dyn) );
65988
- assert( !(u.an.pDest->flags & (MEM_Blob|MEM_Str)) || u.an.pDest->z==u.an.sMem.z );
65989
- u.an.pDest->flags &= ~(MEM_Ephem|MEM_Static);
65990
- u.an.pDest->flags |= MEM_Term;
65991
- u.an.pDest->z = u.an.sMem.z;
65992
- u.an.pDest->zMalloc = u.an.sMem.zMalloc;
66039
+ if( u.ao.sMem.zMalloc ){
66040
+ assert( u.ao.sMem.z==u.ao.sMem.zMalloc );
66041
+ assert( !(u.ao.pDest->flags & MEM_Dyn) );
66042
+ assert( !(u.ao.pDest->flags & (MEM_Blob|MEM_Str)) || u.ao.pDest->z==u.ao.sMem.z );
66043
+ u.ao.pDest->flags &= ~(MEM_Ephem|MEM_Static);
66044
+ u.ao.pDest->flags |= MEM_Term;
66045
+ u.ao.pDest->z = u.ao.sMem.z;
66046
+ u.ao.pDest->zMalloc = u.ao.sMem.zMalloc;
6599366047
}
6599466048
65995
- rc = sqlite3VdbeMemMakeWriteable(u.an.pDest);
66049
+ rc = sqlite3VdbeMemMakeWriteable(u.ao.pDest);
6599666050
6599766051
op_column_out:
65998
- UPDATE_MAX_BLOBSIZE(u.an.pDest);
65999
- REGISTER_TRACE(pOp->p3, u.an.pDest);
66052
+ UPDATE_MAX_BLOBSIZE(u.ao.pDest);
66053
+ REGISTER_TRACE(pOp->p3, u.ao.pDest);
6600066054
break;
6600166055
}
6600266056
6600366057
/* Opcode: Affinity P1 P2 * P4 *
6600466058
**
@@ -66007,24 +66061,24 @@
6600766061
** P4 is a string that is P2 characters long. The nth character of the
6600866062
** string indicates the column affinity that should be used for the nth
6600966063
** memory cell in the range.
6601066064
*/
6601166065
case OP_Affinity: {
66012
-#if 0 /* local variables moved into u.ao */
66066
+#if 0 /* local variables moved into u.ap */
6601366067
const char *zAffinity; /* The affinity to be applied */
6601466068
char cAff; /* A single character of affinity */
66015
-#endif /* local variables moved into u.ao */
66069
+#endif /* local variables moved into u.ap */
6601666070
66017
- u.ao.zAffinity = pOp->p4.z;
66018
- assert( u.ao.zAffinity!=0 );
66019
- assert( u.ao.zAffinity[pOp->p2]==0 );
66071
+ u.ap.zAffinity = pOp->p4.z;
66072
+ assert( u.ap.zAffinity!=0 );
66073
+ assert( u.ap.zAffinity[pOp->p2]==0 );
6602066074
pIn1 = &aMem[pOp->p1];
66021
- while( (u.ao.cAff = *(u.ao.zAffinity++))!=0 ){
66075
+ while( (u.ap.cAff = *(u.ap.zAffinity++))!=0 ){
6602266076
assert( pIn1 <= &p->aMem[p->nMem] );
6602366077
assert( memIsValid(pIn1) );
6602466078
ExpandBlob(pIn1);
66025
- applyAffinity(pIn1, u.ao.cAff, encoding);
66079
+ applyAffinity(pIn1, u.ap.cAff, encoding);
6602666080
pIn1++;
6602766081
}
6602866082
break;
6602966083
}
6603066084
@@ -66042,11 +66096,11 @@
6604266096
** macros defined in sqliteInt.h.
6604366097
**
6604466098
** If P4 is NULL then all index fields have the affinity NONE.
6604566099
*/
6604666100
case OP_MakeRecord: {
66047
-#if 0 /* local variables moved into u.ap */
66101
+#if 0 /* local variables moved into u.aq */
6604866102
u8 *zNewRecord; /* A buffer to hold the data for the new record */
6604966103
Mem *pRec; /* The new record */
6605066104
u64 nData; /* Number of bytes of data space */
6605166105
int nHdr; /* Number of bytes of header space */
6605266106
i64 nByte; /* Data space required for this record */
@@ -66058,11 +66112,11 @@
6605866112
int nField; /* Number of fields in the record */
6605966113
char *zAffinity; /* The affinity string for the record */
6606066114
int file_format; /* File format to use for encoding */
6606166115
int i; /* Space used in zNewRecord[] */
6606266116
int len; /* Length of a field */
66063
-#endif /* local variables moved into u.ap */
66117
+#endif /* local variables moved into u.aq */
6606466118
6606566119
/* Assuming the record contains N fields, the record format looks
6606666120
** like this:
6606766121
**
6606866122
** ------------------------------------------------------------------------
@@ -66075,87 +66129,87 @@
6607566129
** Each type field is a varint representing the serial type of the
6607666130
** corresponding data element (see sqlite3VdbeSerialType()). The
6607766131
** hdr-size field is also a varint which is the offset from the beginning
6607866132
** of the record to data0.
6607966133
*/
66080
- u.ap.nData = 0; /* Number of bytes of data space */
66081
- u.ap.nHdr = 0; /* Number of bytes of header space */
66082
- u.ap.nZero = 0; /* Number of zero bytes at the end of the record */
66083
- u.ap.nField = pOp->p1;
66084
- u.ap.zAffinity = pOp->p4.z;
66085
- assert( u.ap.nField>0 && pOp->p2>0 && pOp->p2+u.ap.nField<=p->nMem+1 );
66086
- u.ap.pData0 = &aMem[u.ap.nField];
66087
- u.ap.nField = pOp->p2;
66088
- u.ap.pLast = &u.ap.pData0[u.ap.nField-1];
66089
- u.ap.file_format = p->minWriteFileFormat;
66134
+ u.aq.nData = 0; /* Number of bytes of data space */
66135
+ u.aq.nHdr = 0; /* Number of bytes of header space */
66136
+ u.aq.nZero = 0; /* Number of zero bytes at the end of the record */
66137
+ u.aq.nField = pOp->p1;
66138
+ u.aq.zAffinity = pOp->p4.z;
66139
+ assert( u.aq.nField>0 && pOp->p2>0 && pOp->p2+u.aq.nField<=p->nMem+1 );
66140
+ u.aq.pData0 = &aMem[u.aq.nField];
66141
+ u.aq.nField = pOp->p2;
66142
+ u.aq.pLast = &u.aq.pData0[u.aq.nField-1];
66143
+ u.aq.file_format = p->minWriteFileFormat;
6609066144
6609166145
/* Identify the output register */
6609266146
assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
6609366147
pOut = &aMem[pOp->p3];
6609466148
memAboutToChange(p, pOut);
6609566149
6609666150
/* Loop through the elements that will make up the record to figure
6609766151
** out how much space is required for the new record.
6609866152
*/
66099
- for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
66100
- assert( memIsValid(u.ap.pRec) );
66101
- if( u.ap.zAffinity ){
66102
- applyAffinity(u.ap.pRec, u.ap.zAffinity[u.ap.pRec-u.ap.pData0], encoding);
66103
- }
66104
- if( u.ap.pRec->flags&MEM_Zero && u.ap.pRec->n>0 ){
66105
- sqlite3VdbeMemExpandBlob(u.ap.pRec);
66106
- }
66107
- u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
66108
- u.ap.len = sqlite3VdbeSerialTypeLen(u.ap.serial_type);
66109
- u.ap.nData += u.ap.len;
66110
- u.ap.nHdr += sqlite3VarintLen(u.ap.serial_type);
66111
- if( u.ap.pRec->flags & MEM_Zero ){
66153
+ for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){
66154
+ assert( memIsValid(u.aq.pRec) );
66155
+ if( u.aq.zAffinity ){
66156
+ applyAffinity(u.aq.pRec, u.aq.zAffinity[u.aq.pRec-u.aq.pData0], encoding);
66157
+ }
66158
+ if( u.aq.pRec->flags&MEM_Zero && u.aq.pRec->n>0 ){
66159
+ sqlite3VdbeMemExpandBlob(u.aq.pRec);
66160
+ }
66161
+ u.aq.serial_type = sqlite3VdbeSerialType(u.aq.pRec, u.aq.file_format);
66162
+ u.aq.len = sqlite3VdbeSerialTypeLen(u.aq.serial_type);
66163
+ u.aq.nData += u.aq.len;
66164
+ u.aq.nHdr += sqlite3VarintLen(u.aq.serial_type);
66165
+ if( u.aq.pRec->flags & MEM_Zero ){
6611266166
/* Only pure zero-filled BLOBs can be input to this Opcode.
6611366167
** We do not allow blobs with a prefix and a zero-filled tail. */
66114
- u.ap.nZero += u.ap.pRec->u.nZero;
66115
- }else if( u.ap.len ){
66116
- u.ap.nZero = 0;
66168
+ u.aq.nZero += u.aq.pRec->u.nZero;
66169
+ }else if( u.aq.len ){
66170
+ u.aq.nZero = 0;
6611766171
}
6611866172
}
6611966173
6612066174
/* Add the initial header varint and total the size */
66121
- u.ap.nHdr += u.ap.nVarint = sqlite3VarintLen(u.ap.nHdr);
66122
- if( u.ap.nVarint<sqlite3VarintLen(u.ap.nHdr) ){
66123
- u.ap.nHdr++;
66175
+ u.aq.nHdr += u.aq.nVarint = sqlite3VarintLen(u.aq.nHdr);
66176
+ if( u.aq.nVarint<sqlite3VarintLen(u.aq.nHdr) ){
66177
+ u.aq.nHdr++;
6612466178
}
66125
- u.ap.nByte = u.ap.nHdr+u.ap.nData-u.ap.nZero;
66126
- if( u.ap.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
66179
+ u.aq.nByte = u.aq.nHdr+u.aq.nData-u.aq.nZero;
66180
+ if( u.aq.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
6612766181
goto too_big;
6612866182
}
6612966183
6613066184
/* Make sure the output register has a buffer large enough to store
6613166185
** the new record. The output register (pOp->p3) is not allowed to
6613266186
** be one of the input registers (because the following call to
6613366187
** sqlite3VdbeMemGrow() could clobber the value before it is used).
6613466188
*/
66135
- if( sqlite3VdbeMemGrow(pOut, (int)u.ap.nByte, 0) ){
66189
+ if( sqlite3VdbeMemGrow(pOut, (int)u.aq.nByte, 0) ){
6613666190
goto no_mem;
6613766191
}
66138
- u.ap.zNewRecord = (u8 *)pOut->z;
66192
+ u.aq.zNewRecord = (u8 *)pOut->z;
6613966193
6614066194
/* Write the record */
66141
- u.ap.i = putVarint32(u.ap.zNewRecord, u.ap.nHdr);
66142
- for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
66143
- u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
66144
- u.ap.i += putVarint32(&u.ap.zNewRecord[u.ap.i], u.ap.serial_type); /* serial type */
66145
- }
66146
- for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){ /* serial data */
66147
- u.ap.i += sqlite3VdbeSerialPut(&u.ap.zNewRecord[u.ap.i], (int)(u.ap.nByte-u.ap.i), u.ap.pRec,u.ap.file_format);
66148
- }
66149
- assert( u.ap.i==u.ap.nByte );
66195
+ u.aq.i = putVarint32(u.aq.zNewRecord, u.aq.nHdr);
66196
+ for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){
66197
+ u.aq.serial_type = sqlite3VdbeSerialType(u.aq.pRec, u.aq.file_format);
66198
+ u.aq.i += putVarint32(&u.aq.zNewRecord[u.aq.i], u.aq.serial_type); /* serial type */
66199
+ }
66200
+ for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){ /* serial data */
66201
+ u.aq.i += sqlite3VdbeSerialPut(&u.aq.zNewRecord[u.aq.i], (int)(u.aq.nByte-u.aq.i), u.aq.pRec,u.aq.file_format);
66202
+ }
66203
+ assert( u.aq.i==u.aq.nByte );
6615066204
6615166205
assert( pOp->p3>0 && pOp->p3<=p->nMem );
66152
- pOut->n = (int)u.ap.nByte;
66206
+ pOut->n = (int)u.aq.nByte;
6615366207
pOut->flags = MEM_Blob | MEM_Dyn;
6615466208
pOut->xDel = 0;
66155
- if( u.ap.nZero ){
66156
- pOut->u.nZero = u.ap.nZero;
66209
+ if( u.aq.nZero ){
66210
+ pOut->u.nZero = u.aq.nZero;
6615766211
pOut->flags |= MEM_Zero;
6615866212
}
6615966213
pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
6616066214
REGISTER_TRACE(pOp->p3, pOut);
6616166215
UPDATE_MAX_BLOBSIZE(pOut);
@@ -66167,22 +66221,22 @@
6616766221
** Store the number of entries (an integer value) in the table or index
6616866222
** opened by cursor P1 in register P2
6616966223
*/
6617066224
#ifndef SQLITE_OMIT_BTREECOUNT
6617166225
case OP_Count: { /* out2-prerelease */
66172
-#if 0 /* local variables moved into u.aq */
66226
+#if 0 /* local variables moved into u.ar */
6617366227
i64 nEntry;
6617466228
BtCursor *pCrsr;
66175
-#endif /* local variables moved into u.aq */
66176
-
66177
- u.aq.pCrsr = p->apCsr[pOp->p1]->pCursor;
66178
- if( ALWAYS(u.aq.pCrsr) ){
66179
- rc = sqlite3BtreeCount(u.aq.pCrsr, &u.aq.nEntry);
66180
- }else{
66181
- u.aq.nEntry = 0;
66182
- }
66183
- pOut->u.i = u.aq.nEntry;
66229
+#endif /* local variables moved into u.ar */
66230
+
66231
+ u.ar.pCrsr = p->apCsr[pOp->p1]->pCursor;
66232
+ if( ALWAYS(u.ar.pCrsr) ){
66233
+ rc = sqlite3BtreeCount(u.ar.pCrsr, &u.ar.nEntry);
66234
+ }else{
66235
+ u.ar.nEntry = 0;
66236
+ }
66237
+ pOut->u.i = u.ar.nEntry;
6618466238
break;
6618566239
}
6618666240
#endif
6618766241
6618866242
/* Opcode: Savepoint P1 * * P4 *
@@ -66190,42 +66244,42 @@
6619066244
** Open, release or rollback the savepoint named by parameter P4, depending
6619166245
** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
6619266246
** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
6619366247
*/
6619466248
case OP_Savepoint: {
66195
-#if 0 /* local variables moved into u.ar */
66249
+#if 0 /* local variables moved into u.as */
6619666250
int p1; /* Value of P1 operand */
6619766251
char *zName; /* Name of savepoint */
6619866252
int nName;
6619966253
Savepoint *pNew;
6620066254
Savepoint *pSavepoint;
6620166255
Savepoint *pTmp;
6620266256
int iSavepoint;
6620366257
int ii;
66204
-#endif /* local variables moved into u.ar */
66258
+#endif /* local variables moved into u.as */
6620566259
66206
- u.ar.p1 = pOp->p1;
66207
- u.ar.zName = pOp->p4.z;
66260
+ u.as.p1 = pOp->p1;
66261
+ u.as.zName = pOp->p4.z;
6620866262
66209
- /* Assert that the u.ar.p1 parameter is valid. Also that if there is no open
66263
+ /* Assert that the u.as.p1 parameter is valid. Also that if there is no open
6621066264
** transaction, then there cannot be any savepoints.
6621166265
*/
6621266266
assert( db->pSavepoint==0 || db->autoCommit==0 );
66213
- assert( u.ar.p1==SAVEPOINT_BEGIN||u.ar.p1==SAVEPOINT_RELEASE||u.ar.p1==SAVEPOINT_ROLLBACK );
66267
+ assert( u.as.p1==SAVEPOINT_BEGIN||u.as.p1==SAVEPOINT_RELEASE||u.as.p1==SAVEPOINT_ROLLBACK );
6621466268
assert( db->pSavepoint || db->isTransactionSavepoint==0 );
6621566269
assert( checkSavepointCount(db) );
6621666270
66217
- if( u.ar.p1==SAVEPOINT_BEGIN ){
66271
+ if( u.as.p1==SAVEPOINT_BEGIN ){
6621866272
if( db->writeVdbeCnt>0 ){
6621966273
/* A new savepoint cannot be created if there are active write
6622066274
** statements (i.e. open read/write incremental blob handles).
6622166275
*/
6622266276
sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
6622366277
"SQL statements in progress");
6622466278
rc = SQLITE_BUSY;
6622566279
}else{
66226
- u.ar.nName = sqlite3Strlen30(u.ar.zName);
66280
+ u.as.nName = sqlite3Strlen30(u.as.zName);
6622766281
6622866282
#ifndef SQLITE_OMIT_VIRTUALTABLE
6622966283
/* This call is Ok even if this savepoint is actually a transaction
6623066284
** savepoint (and therefore should not prompt xSavepoint()) callbacks.
6623166285
** If this is a transaction savepoint being opened, it is guaranteed
@@ -66235,14 +66289,14 @@
6623566289
db->nStatement+db->nSavepoint);
6623666290
if( rc!=SQLITE_OK ) goto abort_due_to_error;
6623766291
#endif
6623866292
6623966293
/* Create a new savepoint structure. */
66240
- u.ar.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.ar.nName+1);
66241
- if( u.ar.pNew ){
66242
- u.ar.pNew->zName = (char *)&u.ar.pNew[1];
66243
- memcpy(u.ar.pNew->zName, u.ar.zName, u.ar.nName+1);
66294
+ u.as.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.as.nName+1);
66295
+ if( u.as.pNew ){
66296
+ u.as.pNew->zName = (char *)&u.as.pNew[1];
66297
+ memcpy(u.as.pNew->zName, u.as.zName, u.as.nName+1);
6624466298
6624566299
/* If there is no open transaction, then mark this as a special
6624666300
** "transaction savepoint". */
6624766301
if( db->autoCommit ){
6624866302
db->autoCommit = 0;
@@ -66250,31 +66304,31 @@
6625066304
}else{
6625166305
db->nSavepoint++;
6625266306
}
6625366307
6625466308
/* Link the new savepoint into the database handle's list. */
66255
- u.ar.pNew->pNext = db->pSavepoint;
66256
- db->pSavepoint = u.ar.pNew;
66257
- u.ar.pNew->nDeferredCons = db->nDeferredCons;
66309
+ u.as.pNew->pNext = db->pSavepoint;
66310
+ db->pSavepoint = u.as.pNew;
66311
+ u.as.pNew->nDeferredCons = db->nDeferredCons;
6625866312
}
6625966313
}
6626066314
}else{
66261
- u.ar.iSavepoint = 0;
66315
+ u.as.iSavepoint = 0;
6626266316
6626366317
/* Find the named savepoint. If there is no such savepoint, then an
6626466318
** an error is returned to the user. */
6626566319
for(
66266
- u.ar.pSavepoint = db->pSavepoint;
66267
- u.ar.pSavepoint && sqlite3StrICmp(u.ar.pSavepoint->zName, u.ar.zName);
66268
- u.ar.pSavepoint = u.ar.pSavepoint->pNext
66320
+ u.as.pSavepoint = db->pSavepoint;
66321
+ u.as.pSavepoint && sqlite3StrICmp(u.as.pSavepoint->zName, u.as.zName);
66322
+ u.as.pSavepoint = u.as.pSavepoint->pNext
6626966323
){
66270
- u.ar.iSavepoint++;
66324
+ u.as.iSavepoint++;
6627166325
}
66272
- if( !u.ar.pSavepoint ){
66273
- sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.ar.zName);
66326
+ if( !u.as.pSavepoint ){
66327
+ sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.as.zName);
6627466328
rc = SQLITE_ERROR;
66275
- }else if( db->writeVdbeCnt>0 && u.ar.p1==SAVEPOINT_RELEASE ){
66329
+ }else if( db->writeVdbeCnt>0 && u.as.p1==SAVEPOINT_RELEASE ){
6627666330
/* It is not possible to release (commit) a savepoint if there are
6627766331
** active write statements.
6627866332
*/
6627966333
sqlite3SetString(&p->zErrMsg, db,
6628066334
"cannot release savepoint - SQL statements in progress"
@@ -66284,12 +66338,12 @@
6628466338
6628566339
/* Determine whether or not this is a transaction savepoint. If so,
6628666340
** and this is a RELEASE command, then the current transaction
6628766341
** is committed.
6628866342
*/
66289
- int isTransaction = u.ar.pSavepoint->pNext==0 && db->isTransactionSavepoint;
66290
- if( isTransaction && u.ar.p1==SAVEPOINT_RELEASE ){
66343
+ int isTransaction = u.as.pSavepoint->pNext==0 && db->isTransactionSavepoint;
66344
+ if( isTransaction && u.as.p1==SAVEPOINT_RELEASE ){
6629166345
if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
6629266346
goto vdbe_return;
6629366347
}
6629466348
db->autoCommit = 1;
6629566349
if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
@@ -66299,55 +66353,55 @@
6629966353
goto vdbe_return;
6630066354
}
6630166355
db->isTransactionSavepoint = 0;
6630266356
rc = p->rc;
6630366357
}else{
66304
- u.ar.iSavepoint = db->nSavepoint - u.ar.iSavepoint - 1;
66305
- if( u.ar.p1==SAVEPOINT_ROLLBACK ){
66306
- for(u.ar.ii=0; u.ar.ii<db->nDb; u.ar.ii++){
66307
- sqlite3BtreeTripAllCursors(db->aDb[u.ar.ii].pBt, SQLITE_ABORT);
66358
+ u.as.iSavepoint = db->nSavepoint - u.as.iSavepoint - 1;
66359
+ if( u.as.p1==SAVEPOINT_ROLLBACK ){
66360
+ for(u.as.ii=0; u.as.ii<db->nDb; u.as.ii++){
66361
+ sqlite3BtreeTripAllCursors(db->aDb[u.as.ii].pBt, SQLITE_ABORT);
6630866362
}
6630966363
}
66310
- for(u.ar.ii=0; u.ar.ii<db->nDb; u.ar.ii++){
66311
- rc = sqlite3BtreeSavepoint(db->aDb[u.ar.ii].pBt, u.ar.p1, u.ar.iSavepoint);
66364
+ for(u.as.ii=0; u.as.ii<db->nDb; u.as.ii++){
66365
+ rc = sqlite3BtreeSavepoint(db->aDb[u.as.ii].pBt, u.as.p1, u.as.iSavepoint);
6631266366
if( rc!=SQLITE_OK ){
6631366367
goto abort_due_to_error;
6631466368
}
6631566369
}
66316
- if( u.ar.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
66370
+ if( u.as.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
6631766371
sqlite3ExpirePreparedStatements(db);
6631866372
sqlite3ResetAllSchemasOfConnection(db);
6631966373
db->flags = (db->flags | SQLITE_InternChanges);
6632066374
}
6632166375
}
6632266376
6632366377
/* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
6632466378
** savepoints nested inside of the savepoint being operated on. */
66325
- while( db->pSavepoint!=u.ar.pSavepoint ){
66326
- u.ar.pTmp = db->pSavepoint;
66327
- db->pSavepoint = u.ar.pTmp->pNext;
66328
- sqlite3DbFree(db, u.ar.pTmp);
66379
+ while( db->pSavepoint!=u.as.pSavepoint ){
66380
+ u.as.pTmp = db->pSavepoint;
66381
+ db->pSavepoint = u.as.pTmp->pNext;
66382
+ sqlite3DbFree(db, u.as.pTmp);
6632966383
db->nSavepoint--;
6633066384
}
6633166385
6633266386
/* If it is a RELEASE, then destroy the savepoint being operated on
6633366387
** too. If it is a ROLLBACK TO, then set the number of deferred
6633466388
** constraint violations present in the database to the value stored
6633566389
** when the savepoint was created. */
66336
- if( u.ar.p1==SAVEPOINT_RELEASE ){
66337
- assert( u.ar.pSavepoint==db->pSavepoint );
66338
- db->pSavepoint = u.ar.pSavepoint->pNext;
66339
- sqlite3DbFree(db, u.ar.pSavepoint);
66390
+ if( u.as.p1==SAVEPOINT_RELEASE ){
66391
+ assert( u.as.pSavepoint==db->pSavepoint );
66392
+ db->pSavepoint = u.as.pSavepoint->pNext;
66393
+ sqlite3DbFree(db, u.as.pSavepoint);
6634066394
if( !isTransaction ){
6634166395
db->nSavepoint--;
6634266396
}
6634366397
}else{
66344
- db->nDeferredCons = u.ar.pSavepoint->nDeferredCons;
66398
+ db->nDeferredCons = u.as.pSavepoint->nDeferredCons;
6634566399
}
6634666400
6634766401
if( !isTransaction ){
66348
- rc = sqlite3VtabSavepoint(db, u.ar.p1, u.ar.iSavepoint);
66402
+ rc = sqlite3VtabSavepoint(db, u.as.p1, u.as.iSavepoint);
6634966403
if( rc!=SQLITE_OK ) goto abort_due_to_error;
6635066404
}
6635166405
}
6635266406
}
6635366407
@@ -66362,53 +66416,53 @@
6636266416
** there are active writing VMs or active VMs that use shared cache.
6636366417
**
6636466418
** This instruction causes the VM to halt.
6636566419
*/
6636666420
case OP_AutoCommit: {
66367
-#if 0 /* local variables moved into u.as */
66421
+#if 0 /* local variables moved into u.at */
6636866422
int desiredAutoCommit;
6636966423
int iRollback;
6637066424
int turnOnAC;
66371
-#endif /* local variables moved into u.as */
66425
+#endif /* local variables moved into u.at */
6637266426
66373
- u.as.desiredAutoCommit = pOp->p1;
66374
- u.as.iRollback = pOp->p2;
66375
- u.as.turnOnAC = u.as.desiredAutoCommit && !db->autoCommit;
66376
- assert( u.as.desiredAutoCommit==1 || u.as.desiredAutoCommit==0 );
66377
- assert( u.as.desiredAutoCommit==1 || u.as.iRollback==0 );
66427
+ u.at.desiredAutoCommit = pOp->p1;
66428
+ u.at.iRollback = pOp->p2;
66429
+ u.at.turnOnAC = u.at.desiredAutoCommit && !db->autoCommit;
66430
+ assert( u.at.desiredAutoCommit==1 || u.at.desiredAutoCommit==0 );
66431
+ assert( u.at.desiredAutoCommit==1 || u.at.iRollback==0 );
6637866432
assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
6637966433
6638066434
#if 0
66381
- if( u.as.turnOnAC && u.as.iRollback && db->activeVdbeCnt>1 ){
66435
+ if( u.at.turnOnAC && u.at.iRollback && db->activeVdbeCnt>1 ){
6638266436
/* If this instruction implements a ROLLBACK and other VMs are
6638366437
** still running, and a transaction is active, return an error indicating
6638466438
** that the other VMs must complete first.
6638566439
*/
6638666440
sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
6638766441
"SQL statements in progress");
6638866442
rc = SQLITE_BUSY;
6638966443
}else
6639066444
#endif
66391
- if( u.as.turnOnAC && !u.as.iRollback && db->writeVdbeCnt>0 ){
66445
+ if( u.at.turnOnAC && !u.at.iRollback && db->writeVdbeCnt>0 ){
6639266446
/* If this instruction implements a COMMIT and other VMs are writing
6639366447
** return an error indicating that the other VMs must complete first.
6639466448
*/
6639566449
sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
6639666450
"SQL statements in progress");
6639766451
rc = SQLITE_BUSY;
66398
- }else if( u.as.desiredAutoCommit!=db->autoCommit ){
66399
- if( u.as.iRollback ){
66400
- assert( u.as.desiredAutoCommit==1 );
66452
+ }else if( u.at.desiredAutoCommit!=db->autoCommit ){
66453
+ if( u.at.iRollback ){
66454
+ assert( u.at.desiredAutoCommit==1 );
6640166455
sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
6640266456
db->autoCommit = 1;
6640366457
}else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
6640466458
goto vdbe_return;
6640566459
}else{
66406
- db->autoCommit = (u8)u.as.desiredAutoCommit;
66460
+ db->autoCommit = (u8)u.at.desiredAutoCommit;
6640766461
if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
6640866462
p->pc = pc;
66409
- db->autoCommit = (u8)(1-u.as.desiredAutoCommit);
66463
+ db->autoCommit = (u8)(1-u.at.desiredAutoCommit);
6641066464
p->rc = rc = SQLITE_BUSY;
6641166465
goto vdbe_return;
6641266466
}
6641366467
}
6641466468
assert( db->nStatement==0 );
@@ -66419,12 +66473,12 @@
6641966473
rc = SQLITE_ERROR;
6642066474
}
6642166475
goto vdbe_return;
6642266476
}else{
6642366477
sqlite3SetString(&p->zErrMsg, db,
66424
- (!u.as.desiredAutoCommit)?"cannot start a transaction within a transaction":(
66425
- (u.as.iRollback)?"cannot rollback - no transaction is active":
66478
+ (!u.at.desiredAutoCommit)?"cannot start a transaction within a transaction":(
66479
+ (u.at.iRollback)?"cannot rollback - no transaction is active":
6642666480
"cannot commit - no transaction is active"));
6642766481
6642866482
rc = SQLITE_ERROR;
6642966483
}
6643066484
break;
@@ -66460,20 +66514,20 @@
6646066514
** will automatically commit when the VDBE halts.
6646166515
**
6646266516
** If P2 is zero, then a read-lock is obtained on the database file.
6646366517
*/
6646466518
case OP_Transaction: {
66465
-#if 0 /* local variables moved into u.at */
66519
+#if 0 /* local variables moved into u.au */
6646666520
Btree *pBt;
66467
-#endif /* local variables moved into u.at */
66521
+#endif /* local variables moved into u.au */
6646866522
6646966523
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6647066524
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66471
- u.at.pBt = db->aDb[pOp->p1].pBt;
66525
+ u.au.pBt = db->aDb[pOp->p1].pBt;
6647266526
66473
- if( u.at.pBt ){
66474
- rc = sqlite3BtreeBeginTrans(u.at.pBt, pOp->p2);
66527
+ if( u.au.pBt ){
66528
+ rc = sqlite3BtreeBeginTrans(u.au.pBt, pOp->p2);
6647566529
if( rc==SQLITE_BUSY ){
6647666530
p->pc = pc;
6647766531
p->rc = rc = SQLITE_BUSY;
6647866532
goto vdbe_return;
6647966533
}
@@ -66482,20 +66536,20 @@
6648266536
}
6648366537
6648466538
if( pOp->p2 && p->usesStmtJournal
6648566539
&& (db->autoCommit==0 || db->activeVdbeCnt>1)
6648666540
){
66487
- assert( sqlite3BtreeIsInTrans(u.at.pBt) );
66541
+ assert( sqlite3BtreeIsInTrans(u.au.pBt) );
6648866542
if( p->iStatement==0 ){
6648966543
assert( db->nStatement>=0 && db->nSavepoint>=0 );
6649066544
db->nStatement++;
6649166545
p->iStatement = db->nSavepoint + db->nStatement;
6649266546
}
6649366547
6649466548
rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
6649566549
if( rc==SQLITE_OK ){
66496
- rc = sqlite3BtreeBeginStmt(u.at.pBt, p->iStatement);
66550
+ rc = sqlite3BtreeBeginStmt(u.au.pBt, p->iStatement);
6649766551
}
6649866552
6649966553
/* Store the current value of the database handles deferred constraint
6650066554
** counter. If the statement transaction needs to be rolled back,
6650166555
** the value of this counter needs to be restored too. */
@@ -66516,25 +66570,25 @@
6651666570
** There must be a read-lock on the database (either a transaction
6651766571
** must be started or there must be an open cursor) before
6651866572
** executing this instruction.
6651966573
*/
6652066574
case OP_ReadCookie: { /* out2-prerelease */
66521
-#if 0 /* local variables moved into u.au */
66575
+#if 0 /* local variables moved into u.av */
6652266576
int iMeta;
6652366577
int iDb;
6652466578
int iCookie;
66525
-#endif /* local variables moved into u.au */
66579
+#endif /* local variables moved into u.av */
6652666580
66527
- u.au.iDb = pOp->p1;
66528
- u.au.iCookie = pOp->p3;
66581
+ u.av.iDb = pOp->p1;
66582
+ u.av.iCookie = pOp->p3;
6652966583
assert( pOp->p3<SQLITE_N_BTREE_META );
66530
- assert( u.au.iDb>=0 && u.au.iDb<db->nDb );
66531
- assert( db->aDb[u.au.iDb].pBt!=0 );
66532
- assert( (p->btreeMask & (((yDbMask)1)<<u.au.iDb))!=0 );
66584
+ assert( u.av.iDb>=0 && u.av.iDb<db->nDb );
66585
+ assert( db->aDb[u.av.iDb].pBt!=0 );
66586
+ assert( (p->btreeMask & (((yDbMask)1)<<u.av.iDb))!=0 );
6653366587
66534
- sqlite3BtreeGetMeta(db->aDb[u.au.iDb].pBt, u.au.iCookie, (u32 *)&u.au.iMeta);
66535
- pOut->u.i = u.au.iMeta;
66588
+ sqlite3BtreeGetMeta(db->aDb[u.av.iDb].pBt, u.av.iCookie, (u32 *)&u.av.iMeta);
66589
+ pOut->u.i = u.av.iMeta;
6653666590
break;
6653766591
}
6653866592
6653966593
/* Opcode: SetCookie P1 P2 P3 * *
6654066594
**
@@ -66545,30 +66599,30 @@
6654566599
** database file used to store temporary tables.
6654666600
**
6654766601
** A transaction must be started before executing this opcode.
6654866602
*/
6654966603
case OP_SetCookie: { /* in3 */
66550
-#if 0 /* local variables moved into u.av */
66604
+#if 0 /* local variables moved into u.aw */
6655166605
Db *pDb;
66552
-#endif /* local variables moved into u.av */
66606
+#endif /* local variables moved into u.aw */
6655366607
assert( pOp->p2<SQLITE_N_BTREE_META );
6655466608
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6655566609
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66556
- u.av.pDb = &db->aDb[pOp->p1];
66557
- assert( u.av.pDb->pBt!=0 );
66610
+ u.aw.pDb = &db->aDb[pOp->p1];
66611
+ assert( u.aw.pDb->pBt!=0 );
6655866612
assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
6655966613
pIn3 = &aMem[pOp->p3];
6656066614
sqlite3VdbeMemIntegerify(pIn3);
6656166615
/* See note about index shifting on OP_ReadCookie */
66562
- rc = sqlite3BtreeUpdateMeta(u.av.pDb->pBt, pOp->p2, (int)pIn3->u.i);
66616
+ rc = sqlite3BtreeUpdateMeta(u.aw.pDb->pBt, pOp->p2, (int)pIn3->u.i);
6656366617
if( pOp->p2==BTREE_SCHEMA_VERSION ){
6656466618
/* When the schema cookie changes, record the new cookie internally */
66565
- u.av.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
66619
+ u.aw.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
6656666620
db->flags |= SQLITE_InternChanges;
6656766621
}else if( pOp->p2==BTREE_FILE_FORMAT ){
6656866622
/* Record changes in the file format */
66569
- u.av.pDb->pSchema->file_format = (u8)pIn3->u.i;
66623
+ u.aw.pDb->pSchema->file_format = (u8)pIn3->u.i;
6657066624
}
6657166625
if( pOp->p1==1 ){
6657266626
/* Invalidate all prepared statements whenever the TEMP database
6657366627
** schema is changed. Ticket #1644 */
6657466628
sqlite3ExpirePreparedStatements(db);
@@ -66594,27 +66648,27 @@
6659466648
** Either a transaction needs to have been started or an OP_Open needs
6659566649
** to be executed (to establish a read lock) before this opcode is
6659666650
** invoked.
6659766651
*/
6659866652
case OP_VerifyCookie: {
66599
-#if 0 /* local variables moved into u.aw */
66653
+#if 0 /* local variables moved into u.ax */
6660066654
int iMeta;
6660166655
int iGen;
6660266656
Btree *pBt;
66603
-#endif /* local variables moved into u.aw */
66657
+#endif /* local variables moved into u.ax */
6660466658
6660566659
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6660666660
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
6660766661
assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
66608
- u.aw.pBt = db->aDb[pOp->p1].pBt;
66609
- if( u.aw.pBt ){
66610
- sqlite3BtreeGetMeta(u.aw.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.aw.iMeta);
66611
- u.aw.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
66662
+ u.ax.pBt = db->aDb[pOp->p1].pBt;
66663
+ if( u.ax.pBt ){
66664
+ sqlite3BtreeGetMeta(u.ax.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.ax.iMeta);
66665
+ u.ax.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
6661266666
}else{
66613
- u.aw.iGen = u.aw.iMeta = 0;
66667
+ u.ax.iGen = u.ax.iMeta = 0;
6661466668
}
66615
- if( u.aw.iMeta!=pOp->p2 || u.aw.iGen!=pOp->p3 ){
66669
+ if( u.ax.iMeta!=pOp->p2 || u.ax.iGen!=pOp->p3 ){
6661666670
sqlite3DbFree(db, p->zErrMsg);
6661766671
p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
6661866672
/* If the schema-cookie from the database file matches the cookie
6661966673
** stored with the in-memory representation of the schema, do
6662066674
** not reload the schema from the database file.
@@ -66626,11 +66680,11 @@
6662666680
** discard the database schema, as the user code implementing the
6662766681
** v-table would have to be ready for the sqlite3_vtab structure itself
6662866682
** to be invalidated whenever sqlite3_step() is called from within
6662966683
** a v-table method.
6663066684
*/
66631
- if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.aw.iMeta ){
66685
+ if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.ax.iMeta ){
6663266686
sqlite3ResetOneSchema(db, pOp->p1);
6663366687
}
6663466688
6663566689
p->expired = 1;
6663666690
rc = SQLITE_SCHEMA;
@@ -66687,91 +66741,91 @@
6668766741
**
6668866742
** See also OpenRead.
6668966743
*/
6669066744
case OP_OpenRead:
6669166745
case OP_OpenWrite: {
66692
-#if 0 /* local variables moved into u.ax */
66746
+#if 0 /* local variables moved into u.ay */
6669366747
int nField;
6669466748
KeyInfo *pKeyInfo;
6669566749
int p2;
6669666750
int iDb;
6669766751
int wrFlag;
6669866752
Btree *pX;
6669966753
VdbeCursor *pCur;
6670066754
Db *pDb;
66701
-#endif /* local variables moved into u.ax */
66755
+#endif /* local variables moved into u.ay */
6670266756
6670366757
assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
6670466758
assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
6670566759
6670666760
if( p->expired ){
6670766761
rc = SQLITE_ABORT;
6670866762
break;
6670966763
}
6671066764
66711
- u.ax.nField = 0;
66712
- u.ax.pKeyInfo = 0;
66713
- u.ax.p2 = pOp->p2;
66714
- u.ax.iDb = pOp->p3;
66715
- assert( u.ax.iDb>=0 && u.ax.iDb<db->nDb );
66716
- assert( (p->btreeMask & (((yDbMask)1)<<u.ax.iDb))!=0 );
66717
- u.ax.pDb = &db->aDb[u.ax.iDb];
66718
- u.ax.pX = u.ax.pDb->pBt;
66719
- assert( u.ax.pX!=0 );
66765
+ u.ay.nField = 0;
66766
+ u.ay.pKeyInfo = 0;
66767
+ u.ay.p2 = pOp->p2;
66768
+ u.ay.iDb = pOp->p3;
66769
+ assert( u.ay.iDb>=0 && u.ay.iDb<db->nDb );
66770
+ assert( (p->btreeMask & (((yDbMask)1)<<u.ay.iDb))!=0 );
66771
+ u.ay.pDb = &db->aDb[u.ay.iDb];
66772
+ u.ay.pX = u.ay.pDb->pBt;
66773
+ assert( u.ay.pX!=0 );
6672066774
if( pOp->opcode==OP_OpenWrite ){
66721
- u.ax.wrFlag = 1;
66722
- assert( sqlite3SchemaMutexHeld(db, u.ax.iDb, 0) );
66723
- if( u.ax.pDb->pSchema->file_format < p->minWriteFileFormat ){
66724
- p->minWriteFileFormat = u.ax.pDb->pSchema->file_format;
66775
+ u.ay.wrFlag = 1;
66776
+ assert( sqlite3SchemaMutexHeld(db, u.ay.iDb, 0) );
66777
+ if( u.ay.pDb->pSchema->file_format < p->minWriteFileFormat ){
66778
+ p->minWriteFileFormat = u.ay.pDb->pSchema->file_format;
6672566779
}
6672666780
}else{
66727
- u.ax.wrFlag = 0;
66781
+ u.ay.wrFlag = 0;
6672866782
}
6672966783
if( pOp->p5 & OPFLAG_P2ISREG ){
66730
- assert( u.ax.p2>0 );
66731
- assert( u.ax.p2<=p->nMem );
66732
- pIn2 = &aMem[u.ax.p2];
66784
+ assert( u.ay.p2>0 );
66785
+ assert( u.ay.p2<=p->nMem );
66786
+ pIn2 = &aMem[u.ay.p2];
6673366787
assert( memIsValid(pIn2) );
6673466788
assert( (pIn2->flags & MEM_Int)!=0 );
6673566789
sqlite3VdbeMemIntegerify(pIn2);
66736
- u.ax.p2 = (int)pIn2->u.i;
66737
- /* The u.ax.p2 value always comes from a prior OP_CreateTable opcode and
66738
- ** that opcode will always set the u.ax.p2 value to 2 or more or else fail.
66790
+ u.ay.p2 = (int)pIn2->u.i;
66791
+ /* The u.ay.p2 value always comes from a prior OP_CreateTable opcode and
66792
+ ** that opcode will always set the u.ay.p2 value to 2 or more or else fail.
6673966793
** If there were a failure, the prepared statement would have halted
6674066794
** before reaching this instruction. */
66741
- if( NEVER(u.ax.p2<2) ) {
66795
+ if( NEVER(u.ay.p2<2) ) {
6674266796
rc = SQLITE_CORRUPT_BKPT;
6674366797
goto abort_due_to_error;
6674466798
}
6674566799
}
6674666800
if( pOp->p4type==P4_KEYINFO ){
66747
- u.ax.pKeyInfo = pOp->p4.pKeyInfo;
66748
- u.ax.pKeyInfo->enc = ENC(p->db);
66749
- u.ax.nField = u.ax.pKeyInfo->nField+1;
66801
+ u.ay.pKeyInfo = pOp->p4.pKeyInfo;
66802
+ u.ay.pKeyInfo->enc = ENC(p->db);
66803
+ u.ay.nField = u.ay.pKeyInfo->nField+1;
6675066804
}else if( pOp->p4type==P4_INT32 ){
66751
- u.ax.nField = pOp->p4.i;
66805
+ u.ay.nField = pOp->p4.i;
6675266806
}
6675366807
assert( pOp->p1>=0 );
66754
- u.ax.pCur = allocateCursor(p, pOp->p1, u.ax.nField, u.ax.iDb, 1);
66755
- if( u.ax.pCur==0 ) goto no_mem;
66756
- u.ax.pCur->nullRow = 1;
66757
- u.ax.pCur->isOrdered = 1;
66758
- rc = sqlite3BtreeCursor(u.ax.pX, u.ax.p2, u.ax.wrFlag, u.ax.pKeyInfo, u.ax.pCur->pCursor);
66759
- u.ax.pCur->pKeyInfo = u.ax.pKeyInfo;
66808
+ u.ay.pCur = allocateCursor(p, pOp->p1, u.ay.nField, u.ay.iDb, 1);
66809
+ if( u.ay.pCur==0 ) goto no_mem;
66810
+ u.ay.pCur->nullRow = 1;
66811
+ u.ay.pCur->isOrdered = 1;
66812
+ rc = sqlite3BtreeCursor(u.ay.pX, u.ay.p2, u.ay.wrFlag, u.ay.pKeyInfo, u.ay.pCur->pCursor);
66813
+ u.ay.pCur->pKeyInfo = u.ay.pKeyInfo;
6676066814
assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
66761
- sqlite3BtreeCursorHints(u.ax.pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
66815
+ sqlite3BtreeCursorHints(u.ay.pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
6676266816
6676366817
/* Since it performs no memory allocation or IO, the only value that
6676466818
** sqlite3BtreeCursor() may return is SQLITE_OK. */
6676566819
assert( rc==SQLITE_OK );
6676666820
6676766821
/* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
6676866822
** SQLite used to check if the root-page flags were sane at this point
6676966823
** and report database corruption if they were not, but this check has
6677066824
** since moved into the btree layer. */
66771
- u.ax.pCur->isTable = pOp->p4type!=P4_KEYINFO;
66772
- u.ax.pCur->isIndex = !u.ax.pCur->isTable;
66825
+ u.ay.pCur->isTable = pOp->p4type!=P4_KEYINFO;
66826
+ u.ay.pCur->isIndex = !u.ay.pCur->isTable;
6677366827
break;
6677466828
}
6677566829
6677666830
/* Opcode: OpenEphemeral P1 P2 * P4 P5
6677766831
**
@@ -66803,28 +66857,28 @@
6680366857
** by this opcode will be used for automatically created transient
6680466858
** indices in joins.
6680566859
*/
6680666860
case OP_OpenAutoindex:
6680766861
case OP_OpenEphemeral: {
66808
-#if 0 /* local variables moved into u.ay */
66862
+#if 0 /* local variables moved into u.az */
6680966863
VdbeCursor *pCx;
66810
-#endif /* local variables moved into u.ay */
66864
+#endif /* local variables moved into u.az */
6681166865
static const int vfsFlags =
6681266866
SQLITE_OPEN_READWRITE |
6681366867
SQLITE_OPEN_CREATE |
6681466868
SQLITE_OPEN_EXCLUSIVE |
6681566869
SQLITE_OPEN_DELETEONCLOSE |
6681666870
SQLITE_OPEN_TRANSIENT_DB;
6681766871
6681866872
assert( pOp->p1>=0 );
66819
- u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66820
- if( u.ay.pCx==0 ) goto no_mem;
66821
- u.ay.pCx->nullRow = 1;
66822
- rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ay.pCx->pBt,
66873
+ u.az.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66874
+ if( u.az.pCx==0 ) goto no_mem;
66875
+ u.az.pCx->nullRow = 1;
66876
+ rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.az.pCx->pBt,
6682366877
BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
6682466878
if( rc==SQLITE_OK ){
66825
- rc = sqlite3BtreeBeginTrans(u.ay.pCx->pBt, 1);
66879
+ rc = sqlite3BtreeBeginTrans(u.az.pCx->pBt, 1);
6682666880
}
6682766881
if( rc==SQLITE_OK ){
6682866882
/* If a transient index is required, create it by calling
6682966883
** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
6683066884
** opening it. If a transient table is required, just use the
@@ -66831,26 +66885,26 @@
6683166885
** automatically created table with root-page 1 (an BLOB_INTKEY table).
6683266886
*/
6683366887
if( pOp->p4.pKeyInfo ){
6683466888
int pgno;
6683566889
assert( pOp->p4type==P4_KEYINFO );
66836
- rc = sqlite3BtreeCreateTable(u.ay.pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
66890
+ rc = sqlite3BtreeCreateTable(u.az.pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
6683766891
if( rc==SQLITE_OK ){
6683866892
assert( pgno==MASTER_ROOT+1 );
66839
- rc = sqlite3BtreeCursor(u.ay.pCx->pBt, pgno, 1,
66840
- (KeyInfo*)pOp->p4.z, u.ay.pCx->pCursor);
66841
- u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66842
- u.ay.pCx->pKeyInfo->enc = ENC(p->db);
66843
- }
66844
- u.ay.pCx->isTable = 0;
66845
- }else{
66846
- rc = sqlite3BtreeCursor(u.ay.pCx->pBt, MASTER_ROOT, 1, 0, u.ay.pCx->pCursor);
66847
- u.ay.pCx->isTable = 1;
66893
+ rc = sqlite3BtreeCursor(u.az.pCx->pBt, pgno, 1,
66894
+ (KeyInfo*)pOp->p4.z, u.az.pCx->pCursor);
66895
+ u.az.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66896
+ u.az.pCx->pKeyInfo->enc = ENC(p->db);
66897
+ }
66898
+ u.az.pCx->isTable = 0;
66899
+ }else{
66900
+ rc = sqlite3BtreeCursor(u.az.pCx->pBt, MASTER_ROOT, 1, 0, u.az.pCx->pCursor);
66901
+ u.az.pCx->isTable = 1;
6684866902
}
6684966903
}
66850
- u.ay.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
66851
- u.ay.pCx->isIndex = !u.ay.pCx->isTable;
66904
+ u.az.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
66905
+ u.az.pCx->isIndex = !u.az.pCx->isTable;
6685266906
break;
6685366907
}
6685466908
6685566909
/* Opcode: OpenSorter P1 P2 * P4 *
6685666910
**
@@ -66857,20 +66911,21 @@
6685766911
** This opcode works like OP_OpenEphemeral except that it opens
6685866912
** a transient index that is specifically designed to sort large
6685966913
** tables using an external merge-sort algorithm.
6686066914
*/
6686166915
case OP_SorterOpen: {
66862
-#if 0 /* local variables moved into u.az */
66916
+#if 0 /* local variables moved into u.ba */
6686366917
VdbeCursor *pCx;
66864
-#endif /* local variables moved into u.az */
66918
+#endif /* local variables moved into u.ba */
66919
+
6686566920
#ifndef SQLITE_OMIT_MERGE_SORT
66866
- u.az.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66867
- if( u.az.pCx==0 ) goto no_mem;
66868
- u.az.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66869
- u.az.pCx->pKeyInfo->enc = ENC(p->db);
66870
- u.az.pCx->isSorter = 1;
66871
- rc = sqlite3VdbeSorterInit(db, u.az.pCx);
66921
+ u.ba.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66922
+ if( u.ba.pCx==0 ) goto no_mem;
66923
+ u.ba.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66924
+ u.ba.pCx->pKeyInfo->enc = ENC(p->db);
66925
+ u.ba.pCx->isSorter = 1;
66926
+ rc = sqlite3VdbeSorterInit(db, u.ba.pCx);
6687266927
#else
6687366928
pOp->opcode = OP_OpenEphemeral;
6687466929
pc--;
6687566930
#endif
6687666931
break;
@@ -66890,21 +66945,21 @@
6689066945
**
6689166946
** P3 is the number of fields in the records that will be stored by
6689266947
** the pseudo-table.
6689366948
*/
6689466949
case OP_OpenPseudo: {
66895
-#if 0 /* local variables moved into u.ba */
66950
+#if 0 /* local variables moved into u.bb */
6689666951
VdbeCursor *pCx;
66897
-#endif /* local variables moved into u.ba */
66952
+#endif /* local variables moved into u.bb */
6689866953
6689966954
assert( pOp->p1>=0 );
66900
- u.ba.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
66901
- if( u.ba.pCx==0 ) goto no_mem;
66902
- u.ba.pCx->nullRow = 1;
66903
- u.ba.pCx->pseudoTableReg = pOp->p2;
66904
- u.ba.pCx->isTable = 1;
66905
- u.ba.pCx->isIndex = 0;
66955
+ u.bb.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
66956
+ if( u.bb.pCx==0 ) goto no_mem;
66957
+ u.bb.pCx->nullRow = 1;
66958
+ u.bb.pCx->pseudoTableReg = pOp->p2;
66959
+ u.bb.pCx->isTable = 1;
66960
+ u.bb.pCx->isIndex = 0;
6690666961
break;
6690766962
}
6690866963
6690966964
/* Opcode: Close P1 * * * *
6691066965
**
@@ -66972,39 +67027,39 @@
6697267027
*/
6697367028
case OP_SeekLt: /* jump, in3 */
6697467029
case OP_SeekLe: /* jump, in3 */
6697567030
case OP_SeekGe: /* jump, in3 */
6697667031
case OP_SeekGt: { /* jump, in3 */
66977
-#if 0 /* local variables moved into u.bb */
67032
+#if 0 /* local variables moved into u.bc */
6697867033
int res;
6697967034
int oc;
6698067035
VdbeCursor *pC;
6698167036
UnpackedRecord r;
6698267037
int nField;
6698367038
i64 iKey; /* The rowid we are to seek to */
66984
-#endif /* local variables moved into u.bb */
67039
+#endif /* local variables moved into u.bc */
6698567040
6698667041
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6698767042
assert( pOp->p2!=0 );
66988
- u.bb.pC = p->apCsr[pOp->p1];
66989
- assert( u.bb.pC!=0 );
66990
- assert( u.bb.pC->pseudoTableReg==0 );
67043
+ u.bc.pC = p->apCsr[pOp->p1];
67044
+ assert( u.bc.pC!=0 );
67045
+ assert( u.bc.pC->pseudoTableReg==0 );
6699167046
assert( OP_SeekLe == OP_SeekLt+1 );
6699267047
assert( OP_SeekGe == OP_SeekLt+2 );
6699367048
assert( OP_SeekGt == OP_SeekLt+3 );
66994
- assert( u.bb.pC->isOrdered );
66995
- if( ALWAYS(u.bb.pC->pCursor!=0) ){
66996
- u.bb.oc = pOp->opcode;
66997
- u.bb.pC->nullRow = 0;
66998
- if( u.bb.pC->isTable ){
67049
+ assert( u.bc.pC->isOrdered );
67050
+ if( ALWAYS(u.bc.pC->pCursor!=0) ){
67051
+ u.bc.oc = pOp->opcode;
67052
+ u.bc.pC->nullRow = 0;
67053
+ if( u.bc.pC->isTable ){
6699967054
/* The input value in P3 might be of any type: integer, real, string,
6700067055
** blob, or NULL. But it needs to be an integer before we can do
6700167056
** the seek, so covert it. */
6700267057
pIn3 = &aMem[pOp->p3];
6700367058
applyNumericAffinity(pIn3);
67004
- u.bb.iKey = sqlite3VdbeIntValue(pIn3);
67005
- u.bb.pC->rowidIsValid = 0;
67059
+ u.bc.iKey = sqlite3VdbeIntValue(pIn3);
67060
+ u.bc.pC->rowidIsValid = 0;
6700667061
6700767062
/* If the P3 value could not be converted into an integer without
6700867063
** loss of information, then special processing is required... */
6700967064
if( (pIn3->flags & MEM_Int)==0 ){
6701067065
if( (pIn3->flags & MEM_Real)==0 ){
@@ -67015,105 +67070,105 @@
6701567070
}
6701667071
/* If we reach this point, then the P3 value must be a floating
6701767072
** point number. */
6701867073
assert( (pIn3->flags & MEM_Real)!=0 );
6701967074
67020
- if( u.bb.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.bb.iKey || pIn3->r>0) ){
67021
- /* The P3 value is too large in magnitude to be expressed as an
67022
- ** integer. */
67023
- u.bb.res = 1;
67024
- if( pIn3->r<0 ){
67025
- if( u.bb.oc>=OP_SeekGe ){ assert( u.bb.oc==OP_SeekGe || u.bb.oc==OP_SeekGt );
67026
- rc = sqlite3BtreeFirst(u.bb.pC->pCursor, &u.bb.res);
67027
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
67028
- }
67029
- }else{
67030
- if( u.bb.oc<=OP_SeekLe ){ assert( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekLe );
67031
- rc = sqlite3BtreeLast(u.bb.pC->pCursor, &u.bb.res);
67032
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
67033
- }
67034
- }
67035
- if( u.bb.res ){
67036
- pc = pOp->p2 - 1;
67037
- }
67038
- break;
67039
- }else if( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekGe ){
67040
- /* Use the ceiling() function to convert real->int */
67041
- if( pIn3->r > (double)u.bb.iKey ) u.bb.iKey++;
67042
- }else{
67043
- /* Use the floor() function to convert real->int */
67044
- assert( u.bb.oc==OP_SeekLe || u.bb.oc==OP_SeekGt );
67045
- if( pIn3->r < (double)u.bb.iKey ) u.bb.iKey--;
67046
- }
67047
- }
67048
- rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, 0, (u64)u.bb.iKey, 0, &u.bb.res);
67049
- if( rc!=SQLITE_OK ){
67050
- goto abort_due_to_error;
67051
- }
67052
- if( u.bb.res==0 ){
67053
- u.bb.pC->rowidIsValid = 1;
67054
- u.bb.pC->lastRowid = u.bb.iKey;
67055
- }
67056
- }else{
67057
- u.bb.nField = pOp->p4.i;
67058
- assert( pOp->p4type==P4_INT32 );
67059
- assert( u.bb.nField>0 );
67060
- u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo;
67061
- u.bb.r.nField = (u16)u.bb.nField;
67062
-
67063
- /* The next line of code computes as follows, only faster:
67064
- ** if( u.bb.oc==OP_SeekGt || u.bb.oc==OP_SeekLe ){
67065
- ** u.bb.r.flags = UNPACKED_INCRKEY;
67066
- ** }else{
67067
- ** u.bb.r.flags = 0;
67068
- ** }
67069
- */
67070
- u.bb.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.bb.oc - OP_SeekLt)));
67071
- assert( u.bb.oc!=OP_SeekGt || u.bb.r.flags==UNPACKED_INCRKEY );
67072
- assert( u.bb.oc!=OP_SeekLe || u.bb.r.flags==UNPACKED_INCRKEY );
67073
- assert( u.bb.oc!=OP_SeekGe || u.bb.r.flags==0 );
67074
- assert( u.bb.oc!=OP_SeekLt || u.bb.r.flags==0 );
67075
-
67076
- u.bb.r.aMem = &aMem[pOp->p3];
67077
-#ifdef SQLITE_DEBUG
67078
- { int i; for(i=0; i<u.bb.r.nField; i++) assert( memIsValid(&u.bb.r.aMem[i]) ); }
67079
-#endif
67080
- ExpandBlob(u.bb.r.aMem);
67081
- rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, &u.bb.r, 0, 0, &u.bb.res);
67082
- if( rc!=SQLITE_OK ){
67083
- goto abort_due_to_error;
67084
- }
67085
- u.bb.pC->rowidIsValid = 0;
67086
- }
67087
- u.bb.pC->deferredMoveto = 0;
67088
- u.bb.pC->cacheStatus = CACHE_STALE;
67089
-#ifdef SQLITE_TEST
67090
- sqlite3_search_count++;
67091
-#endif
67092
- if( u.bb.oc>=OP_SeekGe ){ assert( u.bb.oc==OP_SeekGe || u.bb.oc==OP_SeekGt );
67093
- if( u.bb.res<0 || (u.bb.res==0 && u.bb.oc==OP_SeekGt) ){
67094
- rc = sqlite3BtreeNext(u.bb.pC->pCursor, &u.bb.res);
67095
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
67096
- u.bb.pC->rowidIsValid = 0;
67097
- }else{
67098
- u.bb.res = 0;
67099
- }
67100
- }else{
67101
- assert( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekLe );
67102
- if( u.bb.res>0 || (u.bb.res==0 && u.bb.oc==OP_SeekLt) ){
67103
- rc = sqlite3BtreePrevious(u.bb.pC->pCursor, &u.bb.res);
67104
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
67105
- u.bb.pC->rowidIsValid = 0;
67106
- }else{
67107
- /* u.bb.res might be negative because the table is empty. Check to
67108
- ** see if this is the case.
67109
- */
67110
- u.bb.res = sqlite3BtreeEof(u.bb.pC->pCursor);
67111
- }
67112
- }
67113
- assert( pOp->p2>0 );
67114
- if( u.bb.res ){
67075
+ if( u.bc.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.bc.iKey || pIn3->r>0) ){
67076
+ /* The P3 value is too large in magnitude to be expressed as an
67077
+ ** integer. */
67078
+ u.bc.res = 1;
67079
+ if( pIn3->r<0 ){
67080
+ if( u.bc.oc>=OP_SeekGe ){ assert( u.bc.oc==OP_SeekGe || u.bc.oc==OP_SeekGt );
67081
+ rc = sqlite3BtreeFirst(u.bc.pC->pCursor, &u.bc.res);
67082
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
67083
+ }
67084
+ }else{
67085
+ if( u.bc.oc<=OP_SeekLe ){ assert( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekLe );
67086
+ rc = sqlite3BtreeLast(u.bc.pC->pCursor, &u.bc.res);
67087
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
67088
+ }
67089
+ }
67090
+ if( u.bc.res ){
67091
+ pc = pOp->p2 - 1;
67092
+ }
67093
+ break;
67094
+ }else if( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekGe ){
67095
+ /* Use the ceiling() function to convert real->int */
67096
+ if( pIn3->r > (double)u.bc.iKey ) u.bc.iKey++;
67097
+ }else{
67098
+ /* Use the floor() function to convert real->int */
67099
+ assert( u.bc.oc==OP_SeekLe || u.bc.oc==OP_SeekGt );
67100
+ if( pIn3->r < (double)u.bc.iKey ) u.bc.iKey--;
67101
+ }
67102
+ }
67103
+ rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, 0, (u64)u.bc.iKey, 0, &u.bc.res);
67104
+ if( rc!=SQLITE_OK ){
67105
+ goto abort_due_to_error;
67106
+ }
67107
+ if( u.bc.res==0 ){
67108
+ u.bc.pC->rowidIsValid = 1;
67109
+ u.bc.pC->lastRowid = u.bc.iKey;
67110
+ }
67111
+ }else{
67112
+ u.bc.nField = pOp->p4.i;
67113
+ assert( pOp->p4type==P4_INT32 );
67114
+ assert( u.bc.nField>0 );
67115
+ u.bc.r.pKeyInfo = u.bc.pC->pKeyInfo;
67116
+ u.bc.r.nField = (u16)u.bc.nField;
67117
+
67118
+ /* The next line of code computes as follows, only faster:
67119
+ ** if( u.bc.oc==OP_SeekGt || u.bc.oc==OP_SeekLe ){
67120
+ ** u.bc.r.flags = UNPACKED_INCRKEY;
67121
+ ** }else{
67122
+ ** u.bc.r.flags = 0;
67123
+ ** }
67124
+ */
67125
+ u.bc.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.bc.oc - OP_SeekLt)));
67126
+ assert( u.bc.oc!=OP_SeekGt || u.bc.r.flags==UNPACKED_INCRKEY );
67127
+ assert( u.bc.oc!=OP_SeekLe || u.bc.r.flags==UNPACKED_INCRKEY );
67128
+ assert( u.bc.oc!=OP_SeekGe || u.bc.r.flags==0 );
67129
+ assert( u.bc.oc!=OP_SeekLt || u.bc.r.flags==0 );
67130
+
67131
+ u.bc.r.aMem = &aMem[pOp->p3];
67132
+#ifdef SQLITE_DEBUG
67133
+ { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
67134
+#endif
67135
+ ExpandBlob(u.bc.r.aMem);
67136
+ rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, &u.bc.r, 0, 0, &u.bc.res);
67137
+ if( rc!=SQLITE_OK ){
67138
+ goto abort_due_to_error;
67139
+ }
67140
+ u.bc.pC->rowidIsValid = 0;
67141
+ }
67142
+ u.bc.pC->deferredMoveto = 0;
67143
+ u.bc.pC->cacheStatus = CACHE_STALE;
67144
+#ifdef SQLITE_TEST
67145
+ sqlite3_search_count++;
67146
+#endif
67147
+ if( u.bc.oc>=OP_SeekGe ){ assert( u.bc.oc==OP_SeekGe || u.bc.oc==OP_SeekGt );
67148
+ if( u.bc.res<0 || (u.bc.res==0 && u.bc.oc==OP_SeekGt) ){
67149
+ rc = sqlite3BtreeNext(u.bc.pC->pCursor, &u.bc.res);
67150
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
67151
+ u.bc.pC->rowidIsValid = 0;
67152
+ }else{
67153
+ u.bc.res = 0;
67154
+ }
67155
+ }else{
67156
+ assert( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekLe );
67157
+ if( u.bc.res>0 || (u.bc.res==0 && u.bc.oc==OP_SeekLt) ){
67158
+ rc = sqlite3BtreePrevious(u.bc.pC->pCursor, &u.bc.res);
67159
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
67160
+ u.bc.pC->rowidIsValid = 0;
67161
+ }else{
67162
+ /* u.bc.res might be negative because the table is empty. Check to
67163
+ ** see if this is the case.
67164
+ */
67165
+ u.bc.res = sqlite3BtreeEof(u.bc.pC->pCursor);
67166
+ }
67167
+ }
67168
+ assert( pOp->p2>0 );
67169
+ if( u.bc.res ){
6711567170
pc = pOp->p2 - 1;
6711667171
}
6711767172
}else{
6711867173
/* This happens when attempting to open the sqlite3_master table
6711967174
** for read access returns SQLITE_EMPTY. In this case always
@@ -67132,24 +67187,24 @@
6713267187
** This is actually a deferred seek. Nothing actually happens until
6713367188
** the cursor is used to read a record. That way, if no reads
6713467189
** occur, no unnecessary I/O happens.
6713567190
*/
6713667191
case OP_Seek: { /* in2 */
67137
-#if 0 /* local variables moved into u.bc */
67192
+#if 0 /* local variables moved into u.bd */
6713867193
VdbeCursor *pC;
67139
-#endif /* local variables moved into u.bc */
67194
+#endif /* local variables moved into u.bd */
6714067195
6714167196
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67142
- u.bc.pC = p->apCsr[pOp->p1];
67143
- assert( u.bc.pC!=0 );
67144
- if( ALWAYS(u.bc.pC->pCursor!=0) ){
67145
- assert( u.bc.pC->isTable );
67146
- u.bc.pC->nullRow = 0;
67197
+ u.bd.pC = p->apCsr[pOp->p1];
67198
+ assert( u.bd.pC!=0 );
67199
+ if( ALWAYS(u.bd.pC->pCursor!=0) ){
67200
+ assert( u.bd.pC->isTable );
67201
+ u.bd.pC->nullRow = 0;
6714767202
pIn2 = &aMem[pOp->p2];
67148
- u.bc.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
67149
- u.bc.pC->rowidIsValid = 0;
67150
- u.bc.pC->deferredMoveto = 1;
67203
+ u.bd.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
67204
+ u.bd.pC->rowidIsValid = 0;
67205
+ u.bd.pC->deferredMoveto = 1;
6715167206
}
6715267207
break;
6715367208
}
6715467209
6715567210
@@ -67177,67 +67232,67 @@
6717767232
**
6717867233
** See also: Found, NotExists, IsUnique
6717967234
*/
6718067235
case OP_NotFound: /* jump, in3 */
6718167236
case OP_Found: { /* jump, in3 */
67182
-#if 0 /* local variables moved into u.bd */
67237
+#if 0 /* local variables moved into u.be */
6718367238
int alreadyExists;
6718467239
VdbeCursor *pC;
6718567240
int res;
6718667241
char *pFree;
6718767242
UnpackedRecord *pIdxKey;
6718867243
UnpackedRecord r;
6718967244
char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
67190
-#endif /* local variables moved into u.bd */
67245
+#endif /* local variables moved into u.be */
6719167246
6719267247
#ifdef SQLITE_TEST
6719367248
sqlite3_found_count++;
6719467249
#endif
6719567250
67196
- u.bd.alreadyExists = 0;
67251
+ u.be.alreadyExists = 0;
6719767252
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6719867253
assert( pOp->p4type==P4_INT32 );
67199
- u.bd.pC = p->apCsr[pOp->p1];
67200
- assert( u.bd.pC!=0 );
67254
+ u.be.pC = p->apCsr[pOp->p1];
67255
+ assert( u.be.pC!=0 );
6720167256
pIn3 = &aMem[pOp->p3];
67202
- if( ALWAYS(u.bd.pC->pCursor!=0) ){
67257
+ if( ALWAYS(u.be.pC->pCursor!=0) ){
6720367258
67204
- assert( u.bd.pC->isTable==0 );
67259
+ assert( u.be.pC->isTable==0 );
6720567260
if( pOp->p4.i>0 ){
67206
- u.bd.r.pKeyInfo = u.bd.pC->pKeyInfo;
67207
- u.bd.r.nField = (u16)pOp->p4.i;
67208
- u.bd.r.aMem = pIn3;
67261
+ u.be.r.pKeyInfo = u.be.pC->pKeyInfo;
67262
+ u.be.r.nField = (u16)pOp->p4.i;
67263
+ u.be.r.aMem = pIn3;
6720967264
#ifdef SQLITE_DEBUG
67210
- { int i; for(i=0; i<u.bd.r.nField; i++) assert( memIsValid(&u.bd.r.aMem[i]) ); }
67265
+ { int i; for(i=0; i<u.be.r.nField; i++) assert( memIsValid(&u.be.r.aMem[i]) ); }
6721167266
#endif
67212
- u.bd.r.flags = UNPACKED_PREFIX_MATCH;
67213
- u.bd.pIdxKey = &u.bd.r;
67267
+ u.be.r.flags = UNPACKED_PREFIX_MATCH;
67268
+ u.be.pIdxKey = &u.be.r;
6721467269
}else{
67215
- u.bd.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
67216
- u.bd.pC->pKeyInfo, u.bd.aTempRec, sizeof(u.bd.aTempRec), &u.bd.pFree
67270
+ u.be.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
67271
+ u.be.pC->pKeyInfo, u.be.aTempRec, sizeof(u.be.aTempRec), &u.be.pFree
6721767272
);
67218
- if( u.bd.pIdxKey==0 ) goto no_mem;
67273
+ if( u.be.pIdxKey==0 ) goto no_mem;
6721967274
assert( pIn3->flags & MEM_Blob );
6722067275
assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
67221
- sqlite3VdbeRecordUnpack(u.bd.pC->pKeyInfo, pIn3->n, pIn3->z, u.bd.pIdxKey);
67222
- u.bd.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
67276
+ sqlite3VdbeRecordUnpack(u.be.pC->pKeyInfo, pIn3->n, pIn3->z, u.be.pIdxKey);
67277
+ u.be.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
6722367278
}
67224
- rc = sqlite3BtreeMovetoUnpacked(u.bd.pC->pCursor, u.bd.pIdxKey, 0, 0, &u.bd.res);
67279
+ rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, u.be.pIdxKey, 0, 0, &u.be.res);
6722567280
if( pOp->p4.i==0 ){
67226
- sqlite3DbFree(db, u.bd.pFree);
67281
+ sqlite3DbFree(db, u.be.pFree);
6722767282
}
6722867283
if( rc!=SQLITE_OK ){
6722967284
break;
6723067285
}
67231
- u.bd.alreadyExists = (u.bd.res==0);
67232
- u.bd.pC->deferredMoveto = 0;
67233
- u.bd.pC->cacheStatus = CACHE_STALE;
67286
+ u.be.alreadyExists = (u.be.res==0);
67287
+ u.be.pC->deferredMoveto = 0;
67288
+ u.be.pC->cacheStatus = CACHE_STALE;
6723467289
}
6723567290
if( pOp->opcode==OP_Found ){
67236
- if( u.bd.alreadyExists ) pc = pOp->p2 - 1;
67291
+ if( u.be.alreadyExists ) pc = pOp->p2 - 1;
6723767292
}else{
67238
- if( !u.bd.alreadyExists ) pc = pOp->p2 - 1;
67293
+ if( !u.be.alreadyExists ) pc = pOp->p2 - 1;
6723967294
}
6724067295
break;
6724167296
}
6724267297
6724367298
/* Opcode: IsUnique P1 P2 P3 P4 *
@@ -67265,67 +67320,67 @@
6726567320
** instruction.
6726667321
**
6726767322
** See also: NotFound, NotExists, Found
6726867323
*/
6726967324
case OP_IsUnique: { /* jump, in3 */
67270
-#if 0 /* local variables moved into u.be */
67325
+#if 0 /* local variables moved into u.bf */
6727167326
u16 ii;
6727267327
VdbeCursor *pCx;
6727367328
BtCursor *pCrsr;
6727467329
u16 nField;
6727567330
Mem *aMx;
6727667331
UnpackedRecord r; /* B-Tree index search key */
6727767332
i64 R; /* Rowid stored in register P3 */
67278
-#endif /* local variables moved into u.be */
67333
+#endif /* local variables moved into u.bf */
6727967334
6728067335
pIn3 = &aMem[pOp->p3];
67281
- u.be.aMx = &aMem[pOp->p4.i];
67336
+ u.bf.aMx = &aMem[pOp->p4.i];
6728267337
/* Assert that the values of parameters P1 and P4 are in range. */
6728367338
assert( pOp->p4type==P4_INT32 );
6728467339
assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
6728567340
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6728667341
6728767342
/* Find the index cursor. */
67288
- u.be.pCx = p->apCsr[pOp->p1];
67289
- assert( u.be.pCx->deferredMoveto==0 );
67290
- u.be.pCx->seekResult = 0;
67291
- u.be.pCx->cacheStatus = CACHE_STALE;
67292
- u.be.pCrsr = u.be.pCx->pCursor;
67343
+ u.bf.pCx = p->apCsr[pOp->p1];
67344
+ assert( u.bf.pCx->deferredMoveto==0 );
67345
+ u.bf.pCx->seekResult = 0;
67346
+ u.bf.pCx->cacheStatus = CACHE_STALE;
67347
+ u.bf.pCrsr = u.bf.pCx->pCursor;
6729367348
6729467349
/* If any of the values are NULL, take the jump. */
67295
- u.be.nField = u.be.pCx->pKeyInfo->nField;
67296
- for(u.be.ii=0; u.be.ii<u.be.nField; u.be.ii++){
67297
- if( u.be.aMx[u.be.ii].flags & MEM_Null ){
67350
+ u.bf.nField = u.bf.pCx->pKeyInfo->nField;
67351
+ for(u.bf.ii=0; u.bf.ii<u.bf.nField; u.bf.ii++){
67352
+ if( u.bf.aMx[u.bf.ii].flags & MEM_Null ){
6729867353
pc = pOp->p2 - 1;
67299
- u.be.pCrsr = 0;
67354
+ u.bf.pCrsr = 0;
6730067355
break;
6730167356
}
6730267357
}
67303
- assert( (u.be.aMx[u.be.nField].flags & MEM_Null)==0 );
67358
+ assert( (u.bf.aMx[u.bf.nField].flags & MEM_Null)==0 );
6730467359
67305
- if( u.be.pCrsr!=0 ){
67360
+ if( u.bf.pCrsr!=0 ){
6730667361
/* Populate the index search key. */
67307
- u.be.r.pKeyInfo = u.be.pCx->pKeyInfo;
67308
- u.be.r.nField = u.be.nField + 1;
67309
- u.be.r.flags = UNPACKED_PREFIX_SEARCH;
67310
- u.be.r.aMem = u.be.aMx;
67362
+ u.bf.r.pKeyInfo = u.bf.pCx->pKeyInfo;
67363
+ u.bf.r.nField = u.bf.nField + 1;
67364
+ u.bf.r.flags = UNPACKED_PREFIX_SEARCH;
67365
+ u.bf.r.aMem = u.bf.aMx;
6731167366
#ifdef SQLITE_DEBUG
67312
- { int i; for(i=0; i<u.be.r.nField; i++) assert( memIsValid(&u.be.r.aMem[i]) ); }
67367
+ { int i; for(i=0; i<u.bf.r.nField; i++) assert( memIsValid(&u.bf.r.aMem[i]) ); }
6731367368
#endif
6731467369
67315
- /* Extract the value of u.be.R from register P3. */
67370
+ /* Extract the value of u.bf.R from register P3. */
6731667371
sqlite3VdbeMemIntegerify(pIn3);
67317
- u.be.R = pIn3->u.i;
67372
+ u.bf.R = pIn3->u.i;
6731867373
6731967374
/* Search the B-Tree index. If no conflicting record is found, jump
6732067375
** to P2. Otherwise, copy the rowid of the conflicting record to
6732167376
** register P3 and fall through to the next instruction. */
67322
- rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, &u.be.r, 0, 0, &u.be.pCx->seekResult);
67323
- if( (u.be.r.flags & UNPACKED_PREFIX_SEARCH) || u.be.r.rowid==u.be.R ){
67377
+ rc = sqlite3BtreeMovetoUnpacked(u.bf.pCrsr, &u.bf.r, 0, 0, &u.bf.pCx->seekResult);
67378
+ if( (u.bf.r.flags & UNPACKED_PREFIX_SEARCH) || u.bf.r.rowid==u.bf.R ){
6732467379
pc = pOp->p2 - 1;
6732567380
}else{
67326
- pIn3->u.i = u.be.r.rowid;
67381
+ pIn3->u.i = u.bf.r.rowid;
6732767382
}
6732867383
}
6732967384
break;
6733067385
}
6733167386
@@ -67342,46 +67397,46 @@
6734267397
** P1 is an index.
6734367398
**
6734467399
** See also: Found, NotFound, IsUnique
6734567400
*/
6734667401
case OP_NotExists: { /* jump, in3 */
67347
-#if 0 /* local variables moved into u.bf */
67402
+#if 0 /* local variables moved into u.bg */
6734867403
VdbeCursor *pC;
6734967404
BtCursor *pCrsr;
6735067405
int res;
6735167406
u64 iKey;
67352
-#endif /* local variables moved into u.bf */
67407
+#endif /* local variables moved into u.bg */
6735367408
6735467409
pIn3 = &aMem[pOp->p3];
6735567410
assert( pIn3->flags & MEM_Int );
6735667411
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67357
- u.bf.pC = p->apCsr[pOp->p1];
67358
- assert( u.bf.pC!=0 );
67359
- assert( u.bf.pC->isTable );
67360
- assert( u.bf.pC->pseudoTableReg==0 );
67361
- u.bf.pCrsr = u.bf.pC->pCursor;
67362
- if( ALWAYS(u.bf.pCrsr!=0) ){
67363
- u.bf.res = 0;
67364
- u.bf.iKey = pIn3->u.i;
67365
- rc = sqlite3BtreeMovetoUnpacked(u.bf.pCrsr, 0, u.bf.iKey, 0, &u.bf.res);
67366
- u.bf.pC->lastRowid = pIn3->u.i;
67367
- u.bf.pC->rowidIsValid = u.bf.res==0 ?1:0;
67368
- u.bf.pC->nullRow = 0;
67369
- u.bf.pC->cacheStatus = CACHE_STALE;
67370
- u.bf.pC->deferredMoveto = 0;
67371
- if( u.bf.res!=0 ){
67412
+ u.bg.pC = p->apCsr[pOp->p1];
67413
+ assert( u.bg.pC!=0 );
67414
+ assert( u.bg.pC->isTable );
67415
+ assert( u.bg.pC->pseudoTableReg==0 );
67416
+ u.bg.pCrsr = u.bg.pC->pCursor;
67417
+ if( ALWAYS(u.bg.pCrsr!=0) ){
67418
+ u.bg.res = 0;
67419
+ u.bg.iKey = pIn3->u.i;
67420
+ rc = sqlite3BtreeMovetoUnpacked(u.bg.pCrsr, 0, u.bg.iKey, 0, &u.bg.res);
67421
+ u.bg.pC->lastRowid = pIn3->u.i;
67422
+ u.bg.pC->rowidIsValid = u.bg.res==0 ?1:0;
67423
+ u.bg.pC->nullRow = 0;
67424
+ u.bg.pC->cacheStatus = CACHE_STALE;
67425
+ u.bg.pC->deferredMoveto = 0;
67426
+ if( u.bg.res!=0 ){
6737267427
pc = pOp->p2 - 1;
67373
- assert( u.bf.pC->rowidIsValid==0 );
67428
+ assert( u.bg.pC->rowidIsValid==0 );
6737467429
}
67375
- u.bf.pC->seekResult = u.bf.res;
67430
+ u.bg.pC->seekResult = u.bg.res;
6737667431
}else{
6737767432
/* This happens when an attempt to open a read cursor on the
6737867433
** sqlite_master table returns SQLITE_EMPTY.
6737967434
*/
6738067435
pc = pOp->p2 - 1;
67381
- assert( u.bf.pC->rowidIsValid==0 );
67382
- u.bf.pC->seekResult = 0;
67436
+ assert( u.bg.pC->rowidIsValid==0 );
67437
+ u.bg.pC->seekResult = 0;
6738367438
}
6738467439
break;
6738567440
}
6738667441
6738767442
/* Opcode: Sequence P1 P2 * * *
@@ -67412,25 +67467,25 @@
6741267467
** an SQLITE_FULL error is generated. The P3 register is updated with the '
6741367468
** generated record number. This P3 mechanism is used to help implement the
6741467469
** AUTOINCREMENT feature.
6741567470
*/
6741667471
case OP_NewRowid: { /* out2-prerelease */
67417
-#if 0 /* local variables moved into u.bg */
67472
+#if 0 /* local variables moved into u.bh */
6741867473
i64 v; /* The new rowid */
6741967474
VdbeCursor *pC; /* Cursor of table to get the new rowid */
6742067475
int res; /* Result of an sqlite3BtreeLast() */
6742167476
int cnt; /* Counter to limit the number of searches */
6742267477
Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
6742367478
VdbeFrame *pFrame; /* Root frame of VDBE */
67424
-#endif /* local variables moved into u.bg */
67479
+#endif /* local variables moved into u.bh */
6742567480
67426
- u.bg.v = 0;
67427
- u.bg.res = 0;
67481
+ u.bh.v = 0;
67482
+ u.bh.res = 0;
6742867483
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67429
- u.bg.pC = p->apCsr[pOp->p1];
67430
- assert( u.bg.pC!=0 );
67431
- if( NEVER(u.bg.pC->pCursor==0) ){
67484
+ u.bh.pC = p->apCsr[pOp->p1];
67485
+ assert( u.bh.pC!=0 );
67486
+ if( NEVER(u.bh.pC->pCursor==0) ){
6743267487
/* The zero initialization above is all that is needed */
6743367488
}else{
6743467489
/* The next rowid or record number (different terms for the same
6743567490
** thing) is obtained in a two-step algorithm.
6743667491
**
@@ -67442,11 +67497,11 @@
6744267497
** The second algorithm is to select a rowid at random and see if
6744367498
** it already exists in the table. If it does not exist, we have
6744467499
** succeeded. If the random rowid does exist, we select a new one
6744567500
** and try again, up to 100 times.
6744667501
*/
67447
- assert( u.bg.pC->isTable );
67502
+ assert( u.bh.pC->isTable );
6744867503
6744967504
#ifdef SQLITE_32BIT_ROWID
6745067505
# define MAX_ROWID 0x7fffffff
6745167506
#else
6745267507
/* Some compilers complain about constants of the form 0x7fffffffffffffff.
@@ -67454,101 +67509,101 @@
6745467509
** to provide the constant while making all compilers happy.
6745567510
*/
6745667511
# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
6745767512
#endif
6745867513
67459
- if( !u.bg.pC->useRandomRowid ){
67460
- u.bg.v = sqlite3BtreeGetCachedRowid(u.bg.pC->pCursor);
67461
- if( u.bg.v==0 ){
67462
- rc = sqlite3BtreeLast(u.bg.pC->pCursor, &u.bg.res);
67514
+ if( !u.bh.pC->useRandomRowid ){
67515
+ u.bh.v = sqlite3BtreeGetCachedRowid(u.bh.pC->pCursor);
67516
+ if( u.bh.v==0 ){
67517
+ rc = sqlite3BtreeLast(u.bh.pC->pCursor, &u.bh.res);
6746367518
if( rc!=SQLITE_OK ){
6746467519
goto abort_due_to_error;
6746567520
}
67466
- if( u.bg.res ){
67467
- u.bg.v = 1; /* IMP: R-61914-48074 */
67521
+ if( u.bh.res ){
67522
+ u.bh.v = 1; /* IMP: R-61914-48074 */
6746867523
}else{
67469
- assert( sqlite3BtreeCursorIsValid(u.bg.pC->pCursor) );
67470
- rc = sqlite3BtreeKeySize(u.bg.pC->pCursor, &u.bg.v);
67524
+ assert( sqlite3BtreeCursorIsValid(u.bh.pC->pCursor) );
67525
+ rc = sqlite3BtreeKeySize(u.bh.pC->pCursor, &u.bh.v);
6747167526
assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
67472
- if( u.bg.v>=MAX_ROWID ){
67473
- u.bg.pC->useRandomRowid = 1;
67527
+ if( u.bh.v>=MAX_ROWID ){
67528
+ u.bh.pC->useRandomRowid = 1;
6747467529
}else{
67475
- u.bg.v++; /* IMP: R-29538-34987 */
67530
+ u.bh.v++; /* IMP: R-29538-34987 */
6747667531
}
6747767532
}
6747867533
}
6747967534
6748067535
#ifndef SQLITE_OMIT_AUTOINCREMENT
6748167536
if( pOp->p3 ){
6748267537
/* Assert that P3 is a valid memory cell. */
6748367538
assert( pOp->p3>0 );
6748467539
if( p->pFrame ){
67485
- for(u.bg.pFrame=p->pFrame; u.bg.pFrame->pParent; u.bg.pFrame=u.bg.pFrame->pParent);
67540
+ for(u.bh.pFrame=p->pFrame; u.bh.pFrame->pParent; u.bh.pFrame=u.bh.pFrame->pParent);
6748667541
/* Assert that P3 is a valid memory cell. */
67487
- assert( pOp->p3<=u.bg.pFrame->nMem );
67488
- u.bg.pMem = &u.bg.pFrame->aMem[pOp->p3];
67542
+ assert( pOp->p3<=u.bh.pFrame->nMem );
67543
+ u.bh.pMem = &u.bh.pFrame->aMem[pOp->p3];
6748967544
}else{
6749067545
/* Assert that P3 is a valid memory cell. */
6749167546
assert( pOp->p3<=p->nMem );
67492
- u.bg.pMem = &aMem[pOp->p3];
67493
- memAboutToChange(p, u.bg.pMem);
67494
- }
67495
- assert( memIsValid(u.bg.pMem) );
67496
-
67497
- REGISTER_TRACE(pOp->p3, u.bg.pMem);
67498
- sqlite3VdbeMemIntegerify(u.bg.pMem);
67499
- assert( (u.bg.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
67500
- if( u.bg.pMem->u.i==MAX_ROWID || u.bg.pC->useRandomRowid ){
67547
+ u.bh.pMem = &aMem[pOp->p3];
67548
+ memAboutToChange(p, u.bh.pMem);
67549
+ }
67550
+ assert( memIsValid(u.bh.pMem) );
67551
+
67552
+ REGISTER_TRACE(pOp->p3, u.bh.pMem);
67553
+ sqlite3VdbeMemIntegerify(u.bh.pMem);
67554
+ assert( (u.bh.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
67555
+ if( u.bh.pMem->u.i==MAX_ROWID || u.bh.pC->useRandomRowid ){
6750167556
rc = SQLITE_FULL; /* IMP: R-12275-61338 */
6750267557
goto abort_due_to_error;
6750367558
}
67504
- if( u.bg.v<u.bg.pMem->u.i+1 ){
67505
- u.bg.v = u.bg.pMem->u.i + 1;
67559
+ if( u.bh.v<u.bh.pMem->u.i+1 ){
67560
+ u.bh.v = u.bh.pMem->u.i + 1;
6750667561
}
67507
- u.bg.pMem->u.i = u.bg.v;
67562
+ u.bh.pMem->u.i = u.bh.v;
6750867563
}
6750967564
#endif
6751067565
67511
- sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, u.bg.v<MAX_ROWID ? u.bg.v+1 : 0);
67566
+ sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, u.bh.v<MAX_ROWID ? u.bh.v+1 : 0);
6751267567
}
67513
- if( u.bg.pC->useRandomRowid ){
67568
+ if( u.bh.pC->useRandomRowid ){
6751467569
/* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
6751567570
** largest possible integer (9223372036854775807) then the database
6751667571
** engine starts picking positive candidate ROWIDs at random until
6751767572
** it finds one that is not previously used. */
6751867573
assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
6751967574
** an AUTOINCREMENT table. */
6752067575
/* on the first attempt, simply do one more than previous */
67521
- u.bg.v = lastRowid;
67522
- u.bg.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67523
- u.bg.v++; /* ensure non-zero */
67524
- u.bg.cnt = 0;
67525
- while( ((rc = sqlite3BtreeMovetoUnpacked(u.bg.pC->pCursor, 0, (u64)u.bg.v,
67526
- 0, &u.bg.res))==SQLITE_OK)
67527
- && (u.bg.res==0)
67528
- && (++u.bg.cnt<100)){
67576
+ u.bh.v = lastRowid;
67577
+ u.bh.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67578
+ u.bh.v++; /* ensure non-zero */
67579
+ u.bh.cnt = 0;
67580
+ while( ((rc = sqlite3BtreeMovetoUnpacked(u.bh.pC->pCursor, 0, (u64)u.bh.v,
67581
+ 0, &u.bh.res))==SQLITE_OK)
67582
+ && (u.bh.res==0)
67583
+ && (++u.bh.cnt<100)){
6752967584
/* collision - try another random rowid */
67530
- sqlite3_randomness(sizeof(u.bg.v), &u.bg.v);
67531
- if( u.bg.cnt<5 ){
67585
+ sqlite3_randomness(sizeof(u.bh.v), &u.bh.v);
67586
+ if( u.bh.cnt<5 ){
6753267587
/* try "small" random rowids for the initial attempts */
67533
- u.bg.v &= 0xffffff;
67588
+ u.bh.v &= 0xffffff;
6753467589
}else{
67535
- u.bg.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67590
+ u.bh.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
6753667591
}
67537
- u.bg.v++; /* ensure non-zero */
67592
+ u.bh.v++; /* ensure non-zero */
6753867593
}
67539
- if( rc==SQLITE_OK && u.bg.res==0 ){
67594
+ if( rc==SQLITE_OK && u.bh.res==0 ){
6754067595
rc = SQLITE_FULL; /* IMP: R-38219-53002 */
6754167596
goto abort_due_to_error;
6754267597
}
67543
- assert( u.bg.v>0 ); /* EV: R-40812-03570 */
67598
+ assert( u.bh.v>0 ); /* EV: R-40812-03570 */
6754467599
}
67545
- u.bg.pC->rowidIsValid = 0;
67546
- u.bg.pC->deferredMoveto = 0;
67547
- u.bg.pC->cacheStatus = CACHE_STALE;
67600
+ u.bh.pC->rowidIsValid = 0;
67601
+ u.bh.pC->deferredMoveto = 0;
67602
+ u.bh.pC->cacheStatus = CACHE_STALE;
6754867603
}
67549
- pOut->u.i = u.bg.v;
67604
+ pOut->u.i = u.bh.v;
6755067605
break;
6755167606
}
6755267607
6755367608
/* Opcode: Insert P1 P2 P3 P4 P5
6755467609
**
@@ -67594,74 +67649,74 @@
6759467649
** This works exactly like OP_Insert except that the key is the
6759567650
** integer value P3, not the value of the integer stored in register P3.
6759667651
*/
6759767652
case OP_Insert:
6759867653
case OP_InsertInt: {
67599
-#if 0 /* local variables moved into u.bh */
67654
+#if 0 /* local variables moved into u.bi */
6760067655
Mem *pData; /* MEM cell holding data for the record to be inserted */
6760167656
Mem *pKey; /* MEM cell holding key for the record */
6760267657
i64 iKey; /* The integer ROWID or key for the record to be inserted */
6760367658
VdbeCursor *pC; /* Cursor to table into which insert is written */
6760467659
int nZero; /* Number of zero-bytes to append */
6760567660
int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
6760667661
const char *zDb; /* database name - used by the update hook */
6760767662
const char *zTbl; /* Table name - used by the opdate hook */
6760867663
int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
67609
-#endif /* local variables moved into u.bh */
67664
+#endif /* local variables moved into u.bi */
6761067665
67611
- u.bh.pData = &aMem[pOp->p2];
67666
+ u.bi.pData = &aMem[pOp->p2];
6761267667
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67613
- assert( memIsValid(u.bh.pData) );
67614
- u.bh.pC = p->apCsr[pOp->p1];
67615
- assert( u.bh.pC!=0 );
67616
- assert( u.bh.pC->pCursor!=0 );
67617
- assert( u.bh.pC->pseudoTableReg==0 );
67618
- assert( u.bh.pC->isTable );
67619
- REGISTER_TRACE(pOp->p2, u.bh.pData);
67668
+ assert( memIsValid(u.bi.pData) );
67669
+ u.bi.pC = p->apCsr[pOp->p1];
67670
+ assert( u.bi.pC!=0 );
67671
+ assert( u.bi.pC->pCursor!=0 );
67672
+ assert( u.bi.pC->pseudoTableReg==0 );
67673
+ assert( u.bi.pC->isTable );
67674
+ REGISTER_TRACE(pOp->p2, u.bi.pData);
6762067675
6762167676
if( pOp->opcode==OP_Insert ){
67622
- u.bh.pKey = &aMem[pOp->p3];
67623
- assert( u.bh.pKey->flags & MEM_Int );
67624
- assert( memIsValid(u.bh.pKey) );
67625
- REGISTER_TRACE(pOp->p3, u.bh.pKey);
67626
- u.bh.iKey = u.bh.pKey->u.i;
67677
+ u.bi.pKey = &aMem[pOp->p3];
67678
+ assert( u.bi.pKey->flags & MEM_Int );
67679
+ assert( memIsValid(u.bi.pKey) );
67680
+ REGISTER_TRACE(pOp->p3, u.bi.pKey);
67681
+ u.bi.iKey = u.bi.pKey->u.i;
6762767682
}else{
6762867683
assert( pOp->opcode==OP_InsertInt );
67629
- u.bh.iKey = pOp->p3;
67684
+ u.bi.iKey = pOp->p3;
6763067685
}
6763167686
6763267687
if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
67633
- if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bh.iKey;
67634
- if( u.bh.pData->flags & MEM_Null ){
67635
- u.bh.pData->z = 0;
67636
- u.bh.pData->n = 0;
67637
- }else{
67638
- assert( u.bh.pData->flags & (MEM_Blob|MEM_Str) );
67639
- }
67640
- u.bh.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bh.pC->seekResult : 0);
67641
- if( u.bh.pData->flags & MEM_Zero ){
67642
- u.bh.nZero = u.bh.pData->u.nZero;
67643
- }else{
67644
- u.bh.nZero = 0;
67645
- }
67646
- sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0);
67647
- rc = sqlite3BtreeInsert(u.bh.pC->pCursor, 0, u.bh.iKey,
67648
- u.bh.pData->z, u.bh.pData->n, u.bh.nZero,
67649
- pOp->p5 & OPFLAG_APPEND, u.bh.seekResult
67688
+ if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bi.iKey;
67689
+ if( u.bi.pData->flags & MEM_Null ){
67690
+ u.bi.pData->z = 0;
67691
+ u.bi.pData->n = 0;
67692
+ }else{
67693
+ assert( u.bi.pData->flags & (MEM_Blob|MEM_Str) );
67694
+ }
67695
+ u.bi.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bi.pC->seekResult : 0);
67696
+ if( u.bi.pData->flags & MEM_Zero ){
67697
+ u.bi.nZero = u.bi.pData->u.nZero;
67698
+ }else{
67699
+ u.bi.nZero = 0;
67700
+ }
67701
+ sqlite3BtreeSetCachedRowid(u.bi.pC->pCursor, 0);
67702
+ rc = sqlite3BtreeInsert(u.bi.pC->pCursor, 0, u.bi.iKey,
67703
+ u.bi.pData->z, u.bi.pData->n, u.bi.nZero,
67704
+ pOp->p5 & OPFLAG_APPEND, u.bi.seekResult
6765067705
);
67651
- u.bh.pC->rowidIsValid = 0;
67652
- u.bh.pC->deferredMoveto = 0;
67653
- u.bh.pC->cacheStatus = CACHE_STALE;
67706
+ u.bi.pC->rowidIsValid = 0;
67707
+ u.bi.pC->deferredMoveto = 0;
67708
+ u.bi.pC->cacheStatus = CACHE_STALE;
6765467709
6765567710
/* Invoke the update-hook if required. */
6765667711
if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67657
- u.bh.zDb = db->aDb[u.bh.pC->iDb].zName;
67658
- u.bh.zTbl = pOp->p4.z;
67659
- u.bh.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
67660
- assert( u.bh.pC->isTable );
67661
- db->xUpdateCallback(db->pUpdateArg, u.bh.op, u.bh.zDb, u.bh.zTbl, u.bh.iKey);
67662
- assert( u.bh.pC->iDb>=0 );
67712
+ u.bi.zDb = db->aDb[u.bi.pC->iDb].zName;
67713
+ u.bi.zTbl = pOp->p4.z;
67714
+ u.bi.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
67715
+ assert( u.bi.pC->isTable );
67716
+ db->xUpdateCallback(db->pUpdateArg, u.bi.op, u.bi.zDb, u.bi.zTbl, u.bi.iKey);
67717
+ assert( u.bi.pC->iDb>=0 );
6766367718
}
6766467719
break;
6766567720
}
6766667721
6766767722
/* Opcode: Delete P1 P2 * P4 *
@@ -67683,51 +67738,51 @@
6768367738
** pointing to. The update hook will be invoked, if it exists.
6768467739
** If P4 is not NULL then the P1 cursor must have been positioned
6768567740
** using OP_NotFound prior to invoking this opcode.
6768667741
*/
6768767742
case OP_Delete: {
67688
-#if 0 /* local variables moved into u.bi */
67743
+#if 0 /* local variables moved into u.bj */
6768967744
i64 iKey;
6769067745
VdbeCursor *pC;
67691
-#endif /* local variables moved into u.bi */
67746
+#endif /* local variables moved into u.bj */
6769267747
67693
- u.bi.iKey = 0;
67748
+ u.bj.iKey = 0;
6769467749
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67695
- u.bi.pC = p->apCsr[pOp->p1];
67696
- assert( u.bi.pC!=0 );
67697
- assert( u.bi.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
67750
+ u.bj.pC = p->apCsr[pOp->p1];
67751
+ assert( u.bj.pC!=0 );
67752
+ assert( u.bj.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
6769867753
67699
- /* If the update-hook will be invoked, set u.bi.iKey to the rowid of the
67754
+ /* If the update-hook will be invoked, set u.bj.iKey to the rowid of the
6770067755
** row being deleted.
6770167756
*/
6770267757
if( db->xUpdateCallback && pOp->p4.z ){
67703
- assert( u.bi.pC->isTable );
67704
- assert( u.bi.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
67705
- u.bi.iKey = u.bi.pC->lastRowid;
67758
+ assert( u.bj.pC->isTable );
67759
+ assert( u.bj.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
67760
+ u.bj.iKey = u.bj.pC->lastRowid;
6770667761
}
6770767762
6770867763
/* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
6770967764
** OP_Column on the same table without any intervening operations that
67710
- ** might move or invalidate the cursor. Hence cursor u.bi.pC is always pointing
67765
+ ** might move or invalidate the cursor. Hence cursor u.bj.pC is always pointing
6771167766
** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
6771267767
** below is always a no-op and cannot fail. We will run it anyhow, though,
6771367768
** to guard against future changes to the code generator.
6771467769
**/
67715
- assert( u.bi.pC->deferredMoveto==0 );
67716
- rc = sqlite3VdbeCursorMoveto(u.bi.pC);
67770
+ assert( u.bj.pC->deferredMoveto==0 );
67771
+ rc = sqlite3VdbeCursorMoveto(u.bj.pC);
6771767772
if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
6771867773
67719
- sqlite3BtreeSetCachedRowid(u.bi.pC->pCursor, 0);
67720
- rc = sqlite3BtreeDelete(u.bi.pC->pCursor);
67721
- u.bi.pC->cacheStatus = CACHE_STALE;
67774
+ sqlite3BtreeSetCachedRowid(u.bj.pC->pCursor, 0);
67775
+ rc = sqlite3BtreeDelete(u.bj.pC->pCursor);
67776
+ u.bj.pC->cacheStatus = CACHE_STALE;
6772267777
6772367778
/* Invoke the update-hook if required. */
6772467779
if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67725
- const char *zDb = db->aDb[u.bi.pC->iDb].zName;
67780
+ const char *zDb = db->aDb[u.bj.pC->iDb].zName;
6772667781
const char *zTbl = pOp->p4.z;
67727
- db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bi.iKey);
67728
- assert( u.bi.pC->iDb>=0 );
67782
+ db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bj.iKey);
67783
+ assert( u.bj.pC->iDb>=0 );
6772967784
}
6773067785
if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
6773167786
break;
6773267787
}
6773367788
/* Opcode: ResetCount * * * * *
@@ -67749,20 +67804,20 @@
6774967804
** register P3 with the entry that the sorter cursor currently points to.
6775067805
** If, excluding the rowid fields at the end, the two records are a match,
6775167806
** fall through to the next instruction. Otherwise, jump to instruction P2.
6775267807
*/
6775367808
case OP_SorterCompare: {
67754
-#if 0 /* local variables moved into u.bj */
67809
+#if 0 /* local variables moved into u.bk */
6775567810
VdbeCursor *pC;
6775667811
int res;
67757
-#endif /* local variables moved into u.bj */
67812
+#endif /* local variables moved into u.bk */
6775867813
67759
- u.bj.pC = p->apCsr[pOp->p1];
67760
- assert( isSorter(u.bj.pC) );
67814
+ u.bk.pC = p->apCsr[pOp->p1];
67815
+ assert( isSorter(u.bk.pC) );
6776167816
pIn3 = &aMem[pOp->p3];
67762
- rc = sqlite3VdbeSorterCompare(u.bj.pC, pIn3, &u.bj.res);
67763
- if( u.bj.res ){
67817
+ rc = sqlite3VdbeSorterCompare(u.bk.pC, pIn3, &u.bk.res);
67818
+ if( u.bk.res ){
6776467819
pc = pOp->p2-1;
6776567820
}
6776667821
break;
6776767822
};
6776867823
@@ -67769,18 +67824,19 @@
6776967824
/* Opcode: SorterData P1 P2 * * *
6777067825
**
6777167826
** Write into register P2 the current sorter data for sorter cursor P1.
6777267827
*/
6777367828
case OP_SorterData: {
67774
-#if 0 /* local variables moved into u.bk */
67829
+#if 0 /* local variables moved into u.bl */
6777567830
VdbeCursor *pC;
67776
-#endif /* local variables moved into u.bk */
67831
+#endif /* local variables moved into u.bl */
67832
+
6777767833
#ifndef SQLITE_OMIT_MERGE_SORT
6777867834
pOut = &aMem[pOp->p2];
67779
- u.bk.pC = p->apCsr[pOp->p1];
67780
- assert( u.bk.pC->isSorter );
67781
- rc = sqlite3VdbeSorterRowkey(u.bk.pC, pOut);
67835
+ u.bl.pC = p->apCsr[pOp->p1];
67836
+ assert( u.bl.pC->isSorter );
67837
+ rc = sqlite3VdbeSorterRowkey(u.bl.pC, pOut);
6778267838
#else
6778367839
pOp->opcode = OP_RowKey;
6778467840
pc--;
6778567841
#endif
6778667842
break;
@@ -67806,66 +67862,66 @@
6780667862
** If the P1 cursor must be pointing to a valid row (not a NULL row)
6780767863
** of a real table, not a pseudo-table.
6780867864
*/
6780967865
case OP_RowKey:
6781067866
case OP_RowData: {
67811
-#if 0 /* local variables moved into u.bl */
67867
+#if 0 /* local variables moved into u.bm */
6781267868
VdbeCursor *pC;
6781367869
BtCursor *pCrsr;
6781467870
u32 n;
6781567871
i64 n64;
67816
-#endif /* local variables moved into u.bl */
67872
+#endif /* local variables moved into u.bm */
6781767873
6781867874
pOut = &aMem[pOp->p2];
6781967875
memAboutToChange(p, pOut);
6782067876
6782167877
/* Note that RowKey and RowData are really exactly the same instruction */
6782267878
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67823
- u.bl.pC = p->apCsr[pOp->p1];
67824
- assert( u.bl.pC->isSorter==0 );
67825
- assert( u.bl.pC->isTable || pOp->opcode!=OP_RowData );
67826
- assert( u.bl.pC->isIndex || pOp->opcode==OP_RowData );
67827
- assert( u.bl.pC!=0 );
67828
- assert( u.bl.pC->nullRow==0 );
67829
- assert( u.bl.pC->pseudoTableReg==0 );
67830
- assert( u.bl.pC->pCursor!=0 );
67831
- u.bl.pCrsr = u.bl.pC->pCursor;
67832
- assert( sqlite3BtreeCursorIsValid(u.bl.pCrsr) );
67879
+ u.bm.pC = p->apCsr[pOp->p1];
67880
+ assert( u.bm.pC->isSorter==0 );
67881
+ assert( u.bm.pC->isTable || pOp->opcode!=OP_RowData );
67882
+ assert( u.bm.pC->isIndex || pOp->opcode==OP_RowData );
67883
+ assert( u.bm.pC!=0 );
67884
+ assert( u.bm.pC->nullRow==0 );
67885
+ assert( u.bm.pC->pseudoTableReg==0 );
67886
+ assert( u.bm.pC->pCursor!=0 );
67887
+ u.bm.pCrsr = u.bm.pC->pCursor;
67888
+ assert( sqlite3BtreeCursorIsValid(u.bm.pCrsr) );
6783367889
6783467890
/* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
6783567891
** OP_Rewind/Op_Next with no intervening instructions that might invalidate
6783667892
** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
6783767893
** a no-op and can never fail. But we leave it in place as a safety.
6783867894
*/
67839
- assert( u.bl.pC->deferredMoveto==0 );
67840
- rc = sqlite3VdbeCursorMoveto(u.bl.pC);
67841
- if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67842
-
67843
- if( u.bl.pC->isIndex ){
67844
- assert( !u.bl.pC->isTable );
67845
- VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bl.pCrsr, &u.bl.n64);
67846
- assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
67847
- if( u.bl.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67848
- goto too_big;
67849
- }
67850
- u.bl.n = (u32)u.bl.n64;
67851
- }else{
67852
- VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bl.pCrsr, &u.bl.n);
67853
- assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
67854
- if( u.bl.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
67855
- goto too_big;
67856
- }
67857
- }
67858
- if( sqlite3VdbeMemGrow(pOut, u.bl.n, 0) ){
67859
- goto no_mem;
67860
- }
67861
- pOut->n = u.bl.n;
67862
- MemSetTypeFlag(pOut, MEM_Blob);
67863
- if( u.bl.pC->isIndex ){
67864
- rc = sqlite3BtreeKey(u.bl.pCrsr, 0, u.bl.n, pOut->z);
67865
- }else{
67866
- rc = sqlite3BtreeData(u.bl.pCrsr, 0, u.bl.n, pOut->z);
67895
+ assert( u.bm.pC->deferredMoveto==0 );
67896
+ rc = sqlite3VdbeCursorMoveto(u.bm.pC);
67897
+ if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67898
+
67899
+ if( u.bm.pC->isIndex ){
67900
+ assert( !u.bm.pC->isTable );
67901
+ VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bm.pCrsr, &u.bm.n64);
67902
+ assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
67903
+ if( u.bm.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67904
+ goto too_big;
67905
+ }
67906
+ u.bm.n = (u32)u.bm.n64;
67907
+ }else{
67908
+ VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bm.pCrsr, &u.bm.n);
67909
+ assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
67910
+ if( u.bm.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
67911
+ goto too_big;
67912
+ }
67913
+ }
67914
+ if( sqlite3VdbeMemGrow(pOut, u.bm.n, 0) ){
67915
+ goto no_mem;
67916
+ }
67917
+ pOut->n = u.bm.n;
67918
+ MemSetTypeFlag(pOut, MEM_Blob);
67919
+ if( u.bm.pC->isIndex ){
67920
+ rc = sqlite3BtreeKey(u.bm.pCrsr, 0, u.bm.n, pOut->z);
67921
+ }else{
67922
+ rc = sqlite3BtreeData(u.bm.pCrsr, 0, u.bm.n, pOut->z);
6786767923
}
6786867924
pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
6786967925
UPDATE_MAX_BLOBSIZE(pOut);
6787067926
break;
6787167927
}
@@ -67878,46 +67934,46 @@
6787867934
** P1 can be either an ordinary table or a virtual table. There used to
6787967935
** be a separate OP_VRowid opcode for use with virtual tables, but this
6788067936
** one opcode now works for both table types.
6788167937
*/
6788267938
case OP_Rowid: { /* out2-prerelease */
67883
-#if 0 /* local variables moved into u.bm */
67939
+#if 0 /* local variables moved into u.bn */
6788467940
VdbeCursor *pC;
6788567941
i64 v;
6788667942
sqlite3_vtab *pVtab;
6788767943
const sqlite3_module *pModule;
67888
-#endif /* local variables moved into u.bm */
67944
+#endif /* local variables moved into u.bn */
6788967945
6789067946
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67891
- u.bm.pC = p->apCsr[pOp->p1];
67892
- assert( u.bm.pC!=0 );
67893
- assert( u.bm.pC->pseudoTableReg==0 );
67894
- if( u.bm.pC->nullRow ){
67947
+ u.bn.pC = p->apCsr[pOp->p1];
67948
+ assert( u.bn.pC!=0 );
67949
+ assert( u.bn.pC->pseudoTableReg==0 );
67950
+ if( u.bn.pC->nullRow ){
6789567951
pOut->flags = MEM_Null;
6789667952
break;
67897
- }else if( u.bm.pC->deferredMoveto ){
67898
- u.bm.v = u.bm.pC->movetoTarget;
67953
+ }else if( u.bn.pC->deferredMoveto ){
67954
+ u.bn.v = u.bn.pC->movetoTarget;
6789967955
#ifndef SQLITE_OMIT_VIRTUALTABLE
67900
- }else if( u.bm.pC->pVtabCursor ){
67901
- u.bm.pVtab = u.bm.pC->pVtabCursor->pVtab;
67902
- u.bm.pModule = u.bm.pVtab->pModule;
67903
- assert( u.bm.pModule->xRowid );
67904
- rc = u.bm.pModule->xRowid(u.bm.pC->pVtabCursor, &u.bm.v);
67905
- importVtabErrMsg(p, u.bm.pVtab);
67956
+ }else if( u.bn.pC->pVtabCursor ){
67957
+ u.bn.pVtab = u.bn.pC->pVtabCursor->pVtab;
67958
+ u.bn.pModule = u.bn.pVtab->pModule;
67959
+ assert( u.bn.pModule->xRowid );
67960
+ rc = u.bn.pModule->xRowid(u.bn.pC->pVtabCursor, &u.bn.v);
67961
+ importVtabErrMsg(p, u.bn.pVtab);
6790667962
#endif /* SQLITE_OMIT_VIRTUALTABLE */
6790767963
}else{
67908
- assert( u.bm.pC->pCursor!=0 );
67909
- rc = sqlite3VdbeCursorMoveto(u.bm.pC);
67964
+ assert( u.bn.pC->pCursor!=0 );
67965
+ rc = sqlite3VdbeCursorMoveto(u.bn.pC);
6791067966
if( rc ) goto abort_due_to_error;
67911
- if( u.bm.pC->rowidIsValid ){
67912
- u.bm.v = u.bm.pC->lastRowid;
67967
+ if( u.bn.pC->rowidIsValid ){
67968
+ u.bn.v = u.bn.pC->lastRowid;
6791367969
}else{
67914
- rc = sqlite3BtreeKeySize(u.bm.pC->pCursor, &u.bm.v);
67970
+ rc = sqlite3BtreeKeySize(u.bn.pC->pCursor, &u.bn.v);
6791567971
assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
6791667972
}
6791767973
}
67918
- pOut->u.i = u.bm.v;
67974
+ pOut->u.i = u.bn.v;
6791967975
break;
6792067976
}
6792167977
6792267978
/* Opcode: NullRow P1 * * * *
6792367979
**
@@ -67924,22 +67980,22 @@
6792467980
** Move the cursor P1 to a null row. Any OP_Column operations
6792567981
** that occur while the cursor is on the null row will always
6792667982
** write a NULL.
6792767983
*/
6792867984
case OP_NullRow: {
67929
-#if 0 /* local variables moved into u.bn */
67985
+#if 0 /* local variables moved into u.bo */
6793067986
VdbeCursor *pC;
67931
-#endif /* local variables moved into u.bn */
67987
+#endif /* local variables moved into u.bo */
6793267988
6793367989
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67934
- u.bn.pC = p->apCsr[pOp->p1];
67935
- assert( u.bn.pC!=0 );
67936
- u.bn.pC->nullRow = 1;
67937
- u.bn.pC->rowidIsValid = 0;
67938
- assert( u.bn.pC->pCursor || u.bn.pC->pVtabCursor );
67939
- if( u.bn.pC->pCursor ){
67940
- sqlite3BtreeClearCursor(u.bn.pC->pCursor);
67990
+ u.bo.pC = p->apCsr[pOp->p1];
67991
+ assert( u.bo.pC!=0 );
67992
+ u.bo.pC->nullRow = 1;
67993
+ u.bo.pC->rowidIsValid = 0;
67994
+ assert( u.bo.pC->pCursor || u.bo.pC->pVtabCursor );
67995
+ if( u.bo.pC->pCursor ){
67996
+ sqlite3BtreeClearCursor(u.bo.pC->pCursor);
6794167997
}
6794267998
break;
6794367999
}
6794468000
6794568001
/* Opcode: Last P1 P2 * * *
@@ -67949,29 +68005,29 @@
6794968005
** If the table or index is empty and P2>0, then jump immediately to P2.
6795068006
** If P2 is 0 or if the table or index is not empty, fall through
6795168007
** to the following instruction.
6795268008
*/
6795368009
case OP_Last: { /* jump */
67954
-#if 0 /* local variables moved into u.bo */
68010
+#if 0 /* local variables moved into u.bp */
6795568011
VdbeCursor *pC;
6795668012
BtCursor *pCrsr;
6795768013
int res;
67958
-#endif /* local variables moved into u.bo */
68014
+#endif /* local variables moved into u.bp */
6795968015
6796068016
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67961
- u.bo.pC = p->apCsr[pOp->p1];
67962
- assert( u.bo.pC!=0 );
67963
- u.bo.pCrsr = u.bo.pC->pCursor;
67964
- u.bo.res = 0;
67965
- if( ALWAYS(u.bo.pCrsr!=0) ){
67966
- rc = sqlite3BtreeLast(u.bo.pCrsr, &u.bo.res);
67967
- }
67968
- u.bo.pC->nullRow = (u8)u.bo.res;
67969
- u.bo.pC->deferredMoveto = 0;
67970
- u.bo.pC->rowidIsValid = 0;
67971
- u.bo.pC->cacheStatus = CACHE_STALE;
67972
- if( pOp->p2>0 && u.bo.res ){
68017
+ u.bp.pC = p->apCsr[pOp->p1];
68018
+ assert( u.bp.pC!=0 );
68019
+ u.bp.pCrsr = u.bp.pC->pCursor;
68020
+ u.bp.res = 0;
68021
+ if( ALWAYS(u.bp.pCrsr!=0) ){
68022
+ rc = sqlite3BtreeLast(u.bp.pCrsr, &u.bp.res);
68023
+ }
68024
+ u.bp.pC->nullRow = (u8)u.bp.res;
68025
+ u.bp.pC->deferredMoveto = 0;
68026
+ u.bp.pC->rowidIsValid = 0;
68027
+ u.bp.pC->cacheStatus = CACHE_STALE;
68028
+ if( pOp->p2>0 && u.bp.res ){
6797368029
pc = pOp->p2 - 1;
6797468030
}
6797568031
break;
6797668032
}
6797768033
@@ -68007,35 +68063,35 @@
6800768063
** If the table or index is empty and P2>0, then jump immediately to P2.
6800868064
** If P2 is 0 or if the table or index is not empty, fall through
6800968065
** to the following instruction.
6801068066
*/
6801168067
case OP_Rewind: { /* jump */
68012
-#if 0 /* local variables moved into u.bp */
68068
+#if 0 /* local variables moved into u.bq */
6801368069
VdbeCursor *pC;
6801468070
BtCursor *pCrsr;
6801568071
int res;
68016
-#endif /* local variables moved into u.bp */
68072
+#endif /* local variables moved into u.bq */
6801768073
6801868074
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68019
- u.bp.pC = p->apCsr[pOp->p1];
68020
- assert( u.bp.pC!=0 );
68021
- assert( u.bp.pC->isSorter==(pOp->opcode==OP_SorterSort) );
68022
- u.bp.res = 1;
68023
- if( isSorter(u.bp.pC) ){
68024
- rc = sqlite3VdbeSorterRewind(db, u.bp.pC, &u.bp.res);
68075
+ u.bq.pC = p->apCsr[pOp->p1];
68076
+ assert( u.bq.pC!=0 );
68077
+ assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterSort) );
68078
+ u.bq.res = 1;
68079
+ if( isSorter(u.bq.pC) ){
68080
+ rc = sqlite3VdbeSorterRewind(db, u.bq.pC, &u.bq.res);
6802568081
}else{
68026
- u.bp.pCrsr = u.bp.pC->pCursor;
68027
- assert( u.bp.pCrsr );
68028
- rc = sqlite3BtreeFirst(u.bp.pCrsr, &u.bp.res);
68029
- u.bp.pC->atFirst = u.bp.res==0 ?1:0;
68030
- u.bp.pC->deferredMoveto = 0;
68031
- u.bp.pC->cacheStatus = CACHE_STALE;
68032
- u.bp.pC->rowidIsValid = 0;
68033
- }
68034
- u.bp.pC->nullRow = (u8)u.bp.res;
68082
+ u.bq.pCrsr = u.bq.pC->pCursor;
68083
+ assert( u.bq.pCrsr );
68084
+ rc = sqlite3BtreeFirst(u.bq.pCrsr, &u.bq.res);
68085
+ u.bq.pC->atFirst = u.bq.res==0 ?1:0;
68086
+ u.bq.pC->deferredMoveto = 0;
68087
+ u.bq.pC->cacheStatus = CACHE_STALE;
68088
+ u.bq.pC->rowidIsValid = 0;
68089
+ }
68090
+ u.bq.pC->nullRow = (u8)u.bq.res;
6803568091
assert( pOp->p2>0 && pOp->p2<p->nOp );
68036
- if( u.bp.res ){
68092
+ if( u.bq.res ){
6803768093
pc = pOp->p2 - 1;
6803868094
}
6803968095
break;
6804068096
}
6804168097
@@ -68075,44 +68131,44 @@
6807568131
#ifdef SQLITE_OMIT_MERGE_SORT
6807668132
pOp->opcode = OP_Next;
6807768133
#endif
6807868134
case OP_Prev: /* jump */
6807968135
case OP_Next: { /* jump */
68080
-#if 0 /* local variables moved into u.bq */
68136
+#if 0 /* local variables moved into u.br */
6808168137
VdbeCursor *pC;
6808268138
int res;
68083
-#endif /* local variables moved into u.bq */
68139
+#endif /* local variables moved into u.br */
6808468140
6808568141
CHECK_FOR_INTERRUPT;
6808668142
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6808768143
assert( pOp->p5<=ArraySize(p->aCounter) );
68088
- u.bq.pC = p->apCsr[pOp->p1];
68089
- if( u.bq.pC==0 ){
68144
+ u.br.pC = p->apCsr[pOp->p1];
68145
+ if( u.br.pC==0 ){
6809068146
break; /* See ticket #2273 */
6809168147
}
68092
- assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterNext) );
68093
- if( isSorter(u.bq.pC) ){
68148
+ assert( u.br.pC->isSorter==(pOp->opcode==OP_SorterNext) );
68149
+ if( isSorter(u.br.pC) ){
6809468150
assert( pOp->opcode==OP_SorterNext );
68095
- rc = sqlite3VdbeSorterNext(db, u.bq.pC, &u.bq.res);
68151
+ rc = sqlite3VdbeSorterNext(db, u.br.pC, &u.br.res);
6809668152
}else{
68097
- u.bq.res = 1;
68098
- assert( u.bq.pC->deferredMoveto==0 );
68099
- assert( u.bq.pC->pCursor );
68153
+ u.br.res = 1;
68154
+ assert( u.br.pC->deferredMoveto==0 );
68155
+ assert( u.br.pC->pCursor );
6810068156
assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
6810168157
assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
68102
- rc = pOp->p4.xAdvance(u.bq.pC->pCursor, &u.bq.res);
68158
+ rc = pOp->p4.xAdvance(u.br.pC->pCursor, &u.br.res);
6810368159
}
68104
- u.bq.pC->nullRow = (u8)u.bq.res;
68105
- u.bq.pC->cacheStatus = CACHE_STALE;
68106
- if( u.bq.res==0 ){
68160
+ u.br.pC->nullRow = (u8)u.br.res;
68161
+ u.br.pC->cacheStatus = CACHE_STALE;
68162
+ if( u.br.res==0 ){
6810768163
pc = pOp->p2 - 1;
6810868164
if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
6810968165
#ifdef SQLITE_TEST
6811068166
sqlite3_search_count++;
6811168167
#endif
6811268168
}
68113
- u.bq.pC->rowidIsValid = 0;
68169
+ u.br.pC->rowidIsValid = 0;
6811468170
break;
6811568171
}
6811668172
6811768173
/* Opcode: IdxInsert P1 P2 P3 * P5
6811868174
**
@@ -68129,38 +68185,38 @@
6812968185
case OP_SorterInsert: /* in2 */
6813068186
#ifdef SQLITE_OMIT_MERGE_SORT
6813168187
pOp->opcode = OP_IdxInsert;
6813268188
#endif
6813368189
case OP_IdxInsert: { /* in2 */
68134
-#if 0 /* local variables moved into u.br */
68190
+#if 0 /* local variables moved into u.bs */
6813568191
VdbeCursor *pC;
6813668192
BtCursor *pCrsr;
6813768193
int nKey;
6813868194
const char *zKey;
68139
-#endif /* local variables moved into u.br */
68195
+#endif /* local variables moved into u.bs */
6814068196
6814168197
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68142
- u.br.pC = p->apCsr[pOp->p1];
68143
- assert( u.br.pC!=0 );
68144
- assert( u.br.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
68198
+ u.bs.pC = p->apCsr[pOp->p1];
68199
+ assert( u.bs.pC!=0 );
68200
+ assert( u.bs.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
6814568201
pIn2 = &aMem[pOp->p2];
6814668202
assert( pIn2->flags & MEM_Blob );
68147
- u.br.pCrsr = u.br.pC->pCursor;
68148
- if( ALWAYS(u.br.pCrsr!=0) ){
68149
- assert( u.br.pC->isTable==0 );
68203
+ u.bs.pCrsr = u.bs.pC->pCursor;
68204
+ if( ALWAYS(u.bs.pCrsr!=0) ){
68205
+ assert( u.bs.pC->isTable==0 );
6815068206
rc = ExpandBlob(pIn2);
6815168207
if( rc==SQLITE_OK ){
68152
- if( isSorter(u.br.pC) ){
68153
- rc = sqlite3VdbeSorterWrite(db, u.br.pC, pIn2);
68208
+ if( isSorter(u.bs.pC) ){
68209
+ rc = sqlite3VdbeSorterWrite(db, u.bs.pC, pIn2);
6815468210
}else{
68155
- u.br.nKey = pIn2->n;
68156
- u.br.zKey = pIn2->z;
68157
- rc = sqlite3BtreeInsert(u.br.pCrsr, u.br.zKey, u.br.nKey, "", 0, 0, pOp->p3,
68158
- ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.br.pC->seekResult : 0)
68211
+ u.bs.nKey = pIn2->n;
68212
+ u.bs.zKey = pIn2->z;
68213
+ rc = sqlite3BtreeInsert(u.bs.pCrsr, u.bs.zKey, u.bs.nKey, "", 0, 0, pOp->p3,
68214
+ ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bs.pC->seekResult : 0)
6815968215
);
68160
- assert( u.br.pC->deferredMoveto==0 );
68161
- u.br.pC->cacheStatus = CACHE_STALE;
68216
+ assert( u.bs.pC->deferredMoveto==0 );
68217
+ u.bs.pC->cacheStatus = CACHE_STALE;
6816268218
}
6816368219
}
6816468220
}
6816568221
break;
6816668222
}
@@ -68170,37 +68226,37 @@
6817068226
** The content of P3 registers starting at register P2 form
6817168227
** an unpacked index key. This opcode removes that entry from the
6817268228
** index opened by cursor P1.
6817368229
*/
6817468230
case OP_IdxDelete: {
68175
-#if 0 /* local variables moved into u.bs */
68231
+#if 0 /* local variables moved into u.bt */
6817668232
VdbeCursor *pC;
6817768233
BtCursor *pCrsr;
6817868234
int res;
6817968235
UnpackedRecord r;
68180
-#endif /* local variables moved into u.bs */
68236
+#endif /* local variables moved into u.bt */
6818168237
6818268238
assert( pOp->p3>0 );
6818368239
assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
6818468240
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68185
- u.bs.pC = p->apCsr[pOp->p1];
68186
- assert( u.bs.pC!=0 );
68187
- u.bs.pCrsr = u.bs.pC->pCursor;
68188
- if( ALWAYS(u.bs.pCrsr!=0) ){
68189
- u.bs.r.pKeyInfo = u.bs.pC->pKeyInfo;
68190
- u.bs.r.nField = (u16)pOp->p3;
68191
- u.bs.r.flags = 0;
68192
- u.bs.r.aMem = &aMem[pOp->p2];
68241
+ u.bt.pC = p->apCsr[pOp->p1];
68242
+ assert( u.bt.pC!=0 );
68243
+ u.bt.pCrsr = u.bt.pC->pCursor;
68244
+ if( ALWAYS(u.bt.pCrsr!=0) ){
68245
+ u.bt.r.pKeyInfo = u.bt.pC->pKeyInfo;
68246
+ u.bt.r.nField = (u16)pOp->p3;
68247
+ u.bt.r.flags = 0;
68248
+ u.bt.r.aMem = &aMem[pOp->p2];
6819368249
#ifdef SQLITE_DEBUG
68194
- { int i; for(i=0; i<u.bs.r.nField; i++) assert( memIsValid(&u.bs.r.aMem[i]) ); }
68250
+ { int i; for(i=0; i<u.bt.r.nField; i++) assert( memIsValid(&u.bt.r.aMem[i]) ); }
6819568251
#endif
68196
- rc = sqlite3BtreeMovetoUnpacked(u.bs.pCrsr, &u.bs.r, 0, 0, &u.bs.res);
68197
- if( rc==SQLITE_OK && u.bs.res==0 ){
68198
- rc = sqlite3BtreeDelete(u.bs.pCrsr);
68252
+ rc = sqlite3BtreeMovetoUnpacked(u.bt.pCrsr, &u.bt.r, 0, 0, &u.bt.res);
68253
+ if( rc==SQLITE_OK && u.bt.res==0 ){
68254
+ rc = sqlite3BtreeDelete(u.bt.pCrsr);
6819968255
}
68200
- assert( u.bs.pC->deferredMoveto==0 );
68201
- u.bs.pC->cacheStatus = CACHE_STALE;
68256
+ assert( u.bt.pC->deferredMoveto==0 );
68257
+ u.bt.pC->cacheStatus = CACHE_STALE;
6820268258
}
6820368259
break;
6820468260
}
6820568261
6820668262
/* Opcode: IdxRowid P1 P2 * * *
@@ -68210,32 +68266,32 @@
6821068266
** the rowid of the table entry to which this index entry points.
6821168267
**
6821268268
** See also: Rowid, MakeRecord.
6821368269
*/
6821468270
case OP_IdxRowid: { /* out2-prerelease */
68215
-#if 0 /* local variables moved into u.bt */
68271
+#if 0 /* local variables moved into u.bu */
6821668272
BtCursor *pCrsr;
6821768273
VdbeCursor *pC;
6821868274
i64 rowid;
68219
-#endif /* local variables moved into u.bt */
68275
+#endif /* local variables moved into u.bu */
6822068276
6822168277
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68222
- u.bt.pC = p->apCsr[pOp->p1];
68223
- assert( u.bt.pC!=0 );
68224
- u.bt.pCrsr = u.bt.pC->pCursor;
68278
+ u.bu.pC = p->apCsr[pOp->p1];
68279
+ assert( u.bu.pC!=0 );
68280
+ u.bu.pCrsr = u.bu.pC->pCursor;
6822568281
pOut->flags = MEM_Null;
68226
- if( ALWAYS(u.bt.pCrsr!=0) ){
68227
- rc = sqlite3VdbeCursorMoveto(u.bt.pC);
68282
+ if( ALWAYS(u.bu.pCrsr!=0) ){
68283
+ rc = sqlite3VdbeCursorMoveto(u.bu.pC);
6822868284
if( NEVER(rc) ) goto abort_due_to_error;
68229
- assert( u.bt.pC->deferredMoveto==0 );
68230
- assert( u.bt.pC->isTable==0 );
68231
- if( !u.bt.pC->nullRow ){
68232
- rc = sqlite3VdbeIdxRowid(db, u.bt.pCrsr, &u.bt.rowid);
68285
+ assert( u.bu.pC->deferredMoveto==0 );
68286
+ assert( u.bu.pC->isTable==0 );
68287
+ if( !u.bu.pC->nullRow ){
68288
+ rc = sqlite3VdbeIdxRowid(db, u.bu.pCrsr, &u.bu.rowid);
6823368289
if( rc!=SQLITE_OK ){
6823468290
goto abort_due_to_error;
6823568291
}
68236
- pOut->u.i = u.bt.rowid;
68292
+ pOut->u.i = u.bu.rowid;
6823768293
pOut->flags = MEM_Int;
6823868294
}
6823968295
}
6824068296
break;
6824168297
}
@@ -68266,43 +68322,43 @@
6826668322
** If P5 is non-zero then the key value is increased by an epsilon prior
6826768323
** to the comparison. This makes the opcode work like IdxLE.
6826868324
*/
6826968325
case OP_IdxLT: /* jump */
6827068326
case OP_IdxGE: { /* jump */
68271
-#if 0 /* local variables moved into u.bu */
68327
+#if 0 /* local variables moved into u.bv */
6827268328
VdbeCursor *pC;
6827368329
int res;
6827468330
UnpackedRecord r;
68275
-#endif /* local variables moved into u.bu */
68331
+#endif /* local variables moved into u.bv */
6827668332
6827768333
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68278
- u.bu.pC = p->apCsr[pOp->p1];
68279
- assert( u.bu.pC!=0 );
68280
- assert( u.bu.pC->isOrdered );
68281
- if( ALWAYS(u.bu.pC->pCursor!=0) ){
68282
- assert( u.bu.pC->deferredMoveto==0 );
68334
+ u.bv.pC = p->apCsr[pOp->p1];
68335
+ assert( u.bv.pC!=0 );
68336
+ assert( u.bv.pC->isOrdered );
68337
+ if( ALWAYS(u.bv.pC->pCursor!=0) ){
68338
+ assert( u.bv.pC->deferredMoveto==0 );
6828368339
assert( pOp->p5==0 || pOp->p5==1 );
6828468340
assert( pOp->p4type==P4_INT32 );
68285
- u.bu.r.pKeyInfo = u.bu.pC->pKeyInfo;
68286
- u.bu.r.nField = (u16)pOp->p4.i;
68341
+ u.bv.r.pKeyInfo = u.bv.pC->pKeyInfo;
68342
+ u.bv.r.nField = (u16)pOp->p4.i;
6828768343
if( pOp->p5 ){
68288
- u.bu.r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
68344
+ u.bv.r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
6828968345
}else{
68290
- u.bu.r.flags = UNPACKED_PREFIX_MATCH;
68346
+ u.bv.r.flags = UNPACKED_PREFIX_MATCH;
6829168347
}
68292
- u.bu.r.aMem = &aMem[pOp->p3];
68348
+ u.bv.r.aMem = &aMem[pOp->p3];
6829368349
#ifdef SQLITE_DEBUG
68294
- { int i; for(i=0; i<u.bu.r.nField; i++) assert( memIsValid(&u.bu.r.aMem[i]) ); }
68350
+ { int i; for(i=0; i<u.bv.r.nField; i++) assert( memIsValid(&u.bv.r.aMem[i]) ); }
6829568351
#endif
68296
- rc = sqlite3VdbeIdxKeyCompare(u.bu.pC, &u.bu.r, &u.bu.res);
68352
+ rc = sqlite3VdbeIdxKeyCompare(u.bv.pC, &u.bv.r, &u.bv.res);
6829768353
if( pOp->opcode==OP_IdxLT ){
68298
- u.bu.res = -u.bu.res;
68354
+ u.bv.res = -u.bv.res;
6829968355
}else{
6830068356
assert( pOp->opcode==OP_IdxGE );
68301
- u.bu.res++;
68357
+ u.bv.res++;
6830268358
}
68303
- if( u.bu.res>0 ){
68359
+ if( u.bv.res>0 ){
6830468360
pc = pOp->p2 - 1 ;
6830568361
}
6830668362
}
6830768363
break;
6830868364
}
@@ -68326,43 +68382,44 @@
6832668382
** If AUTOVACUUM is disabled then a zero is stored in register P2.
6832768383
**
6832868384
** See also: Clear
6832968385
*/
6833068386
case OP_Destroy: { /* out2-prerelease */
68331
-#if 0 /* local variables moved into u.bv */
68387
+#if 0 /* local variables moved into u.bw */
6833268388
int iMoved;
6833368389
int iCnt;
6833468390
Vdbe *pVdbe;
6833568391
int iDb;
68336
-#endif /* local variables moved into u.bv */
68392
+#endif /* local variables moved into u.bw */
68393
+
6833768394
#ifndef SQLITE_OMIT_VIRTUALTABLE
68338
- u.bv.iCnt = 0;
68339
- for(u.bv.pVdbe=db->pVdbe; u.bv.pVdbe; u.bv.pVdbe = u.bv.pVdbe->pNext){
68340
- if( u.bv.pVdbe->magic==VDBE_MAGIC_RUN && u.bv.pVdbe->inVtabMethod<2 && u.bv.pVdbe->pc>=0 ){
68341
- u.bv.iCnt++;
68395
+ u.bw.iCnt = 0;
68396
+ for(u.bw.pVdbe=db->pVdbe; u.bw.pVdbe; u.bw.pVdbe = u.bw.pVdbe->pNext){
68397
+ if( u.bw.pVdbe->magic==VDBE_MAGIC_RUN && u.bw.pVdbe->inVtabMethod<2 && u.bw.pVdbe->pc>=0 ){
68398
+ u.bw.iCnt++;
6834268399
}
6834368400
}
6834468401
#else
68345
- u.bv.iCnt = db->activeVdbeCnt;
68402
+ u.bw.iCnt = db->activeVdbeCnt;
6834668403
#endif
6834768404
pOut->flags = MEM_Null;
68348
- if( u.bv.iCnt>1 ){
68405
+ if( u.bw.iCnt>1 ){
6834968406
rc = SQLITE_LOCKED;
6835068407
p->errorAction = OE_Abort;
6835168408
}else{
68352
- u.bv.iDb = pOp->p3;
68353
- assert( u.bv.iCnt==1 );
68354
- assert( (p->btreeMask & (((yDbMask)1)<<u.bv.iDb))!=0 );
68355
- rc = sqlite3BtreeDropTable(db->aDb[u.bv.iDb].pBt, pOp->p1, &u.bv.iMoved);
68409
+ u.bw.iDb = pOp->p3;
68410
+ assert( u.bw.iCnt==1 );
68411
+ assert( (p->btreeMask & (((yDbMask)1)<<u.bw.iDb))!=0 );
68412
+ rc = sqlite3BtreeDropTable(db->aDb[u.bw.iDb].pBt, pOp->p1, &u.bw.iMoved);
6835668413
pOut->flags = MEM_Int;
68357
- pOut->u.i = u.bv.iMoved;
68414
+ pOut->u.i = u.bw.iMoved;
6835868415
#ifndef SQLITE_OMIT_AUTOVACUUM
68359
- if( rc==SQLITE_OK && u.bv.iMoved!=0 ){
68360
- sqlite3RootPageMoved(db, u.bv.iDb, u.bv.iMoved, pOp->p1);
68416
+ if( rc==SQLITE_OK && u.bw.iMoved!=0 ){
68417
+ sqlite3RootPageMoved(db, u.bw.iDb, u.bw.iMoved, pOp->p1);
6836168418
/* All OP_Destroy operations occur on the same btree */
68362
- assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bv.iDb+1 );
68363
- resetSchemaOnFault = u.bv.iDb+1;
68419
+ assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bw.iDb+1 );
68420
+ resetSchemaOnFault = u.bw.iDb+1;
6836468421
}
6836568422
#endif
6836668423
}
6836768424
break;
6836868425
}
@@ -68384,25 +68441,25 @@
6838468441
** also incremented by the number of rows in the table being cleared.
6838568442
**
6838668443
** See also: Destroy
6838768444
*/
6838868445
case OP_Clear: {
68389
-#if 0 /* local variables moved into u.bw */
68446
+#if 0 /* local variables moved into u.bx */
6839068447
int nChange;
68391
-#endif /* local variables moved into u.bw */
68448
+#endif /* local variables moved into u.bx */
6839268449
68393
- u.bw.nChange = 0;
68450
+ u.bx.nChange = 0;
6839468451
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
6839568452
rc = sqlite3BtreeClearTable(
68396
- db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bw.nChange : 0)
68453
+ db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bx.nChange : 0)
6839768454
);
6839868455
if( pOp->p3 ){
68399
- p->nChange += u.bw.nChange;
68456
+ p->nChange += u.bx.nChange;
6840068457
if( pOp->p3>0 ){
6840168458
assert( memIsValid(&aMem[pOp->p3]) );
6840268459
memAboutToChange(p, &aMem[pOp->p3]);
68403
- aMem[pOp->p3].u.i += u.bw.nChange;
68460
+ aMem[pOp->p3].u.i += u.bx.nChange;
6840468461
}
6840568462
}
6840668463
break;
6840768464
}
6840868465
@@ -68428,29 +68485,29 @@
6842868485
**
6842968486
** See documentation on OP_CreateTable for additional information.
6843068487
*/
6843168488
case OP_CreateIndex: /* out2-prerelease */
6843268489
case OP_CreateTable: { /* out2-prerelease */
68433
-#if 0 /* local variables moved into u.bx */
68490
+#if 0 /* local variables moved into u.by */
6843468491
int pgno;
6843568492
int flags;
6843668493
Db *pDb;
68437
-#endif /* local variables moved into u.bx */
68494
+#endif /* local variables moved into u.by */
6843868495
68439
- u.bx.pgno = 0;
68496
+ u.by.pgno = 0;
6844068497
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6844168498
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
68442
- u.bx.pDb = &db->aDb[pOp->p1];
68443
- assert( u.bx.pDb->pBt!=0 );
68499
+ u.by.pDb = &db->aDb[pOp->p1];
68500
+ assert( u.by.pDb->pBt!=0 );
6844468501
if( pOp->opcode==OP_CreateTable ){
68445
- /* u.bx.flags = BTREE_INTKEY; */
68446
- u.bx.flags = BTREE_INTKEY;
68502
+ /* u.by.flags = BTREE_INTKEY; */
68503
+ u.by.flags = BTREE_INTKEY;
6844768504
}else{
68448
- u.bx.flags = BTREE_BLOBKEY;
68505
+ u.by.flags = BTREE_BLOBKEY;
6844968506
}
68450
- rc = sqlite3BtreeCreateTable(u.bx.pDb->pBt, &u.bx.pgno, u.bx.flags);
68451
- pOut->u.i = u.bx.pgno;
68507
+ rc = sqlite3BtreeCreateTable(u.by.pDb->pBt, &u.by.pgno, u.by.flags);
68508
+ pOut->u.i = u.by.pgno;
6845268509
break;
6845368510
}
6845468511
6845568512
/* Opcode: ParseSchema P1 * * P4 *
6845668513
**
@@ -68459,48 +68516,48 @@
6845968516
**
6846068517
** This opcode invokes the parser to create a new virtual machine,
6846168518
** then runs the new virtual machine. It is thus a re-entrant opcode.
6846268519
*/
6846368520
case OP_ParseSchema: {
68464
-#if 0 /* local variables moved into u.by */
68521
+#if 0 /* local variables moved into u.bz */
6846568522
int iDb;
6846668523
const char *zMaster;
6846768524
char *zSql;
6846868525
InitData initData;
68469
-#endif /* local variables moved into u.by */
68526
+#endif /* local variables moved into u.bz */
6847068527
6847168528
/* Any prepared statement that invokes this opcode will hold mutexes
6847268529
** on every btree. This is a prerequisite for invoking
6847368530
** sqlite3InitCallback().
6847468531
*/
6847568532
#ifdef SQLITE_DEBUG
68476
- for(u.by.iDb=0; u.by.iDb<db->nDb; u.by.iDb++){
68477
- assert( u.by.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.by.iDb].pBt) );
68533
+ for(u.bz.iDb=0; u.bz.iDb<db->nDb; u.bz.iDb++){
68534
+ assert( u.bz.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bz.iDb].pBt) );
6847868535
}
6847968536
#endif
6848068537
68481
- u.by.iDb = pOp->p1;
68482
- assert( u.by.iDb>=0 && u.by.iDb<db->nDb );
68483
- assert( DbHasProperty(db, u.by.iDb, DB_SchemaLoaded) );
68538
+ u.bz.iDb = pOp->p1;
68539
+ assert( u.bz.iDb>=0 && u.bz.iDb<db->nDb );
68540
+ assert( DbHasProperty(db, u.bz.iDb, DB_SchemaLoaded) );
6848468541
/* Used to be a conditional */ {
68485
- u.by.zMaster = SCHEMA_TABLE(u.by.iDb);
68486
- u.by.initData.db = db;
68487
- u.by.initData.iDb = pOp->p1;
68488
- u.by.initData.pzErrMsg = &p->zErrMsg;
68489
- u.by.zSql = sqlite3MPrintf(db,
68542
+ u.bz.zMaster = SCHEMA_TABLE(u.bz.iDb);
68543
+ u.bz.initData.db = db;
68544
+ u.bz.initData.iDb = pOp->p1;
68545
+ u.bz.initData.pzErrMsg = &p->zErrMsg;
68546
+ u.bz.zSql = sqlite3MPrintf(db,
6849068547
"SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
68491
- db->aDb[u.by.iDb].zName, u.by.zMaster, pOp->p4.z);
68492
- if( u.by.zSql==0 ){
68548
+ db->aDb[u.bz.iDb].zName, u.bz.zMaster, pOp->p4.z);
68549
+ if( u.bz.zSql==0 ){
6849368550
rc = SQLITE_NOMEM;
6849468551
}else{
6849568552
assert( db->init.busy==0 );
6849668553
db->init.busy = 1;
68497
- u.by.initData.rc = SQLITE_OK;
68554
+ u.bz.initData.rc = SQLITE_OK;
6849868555
assert( !db->mallocFailed );
68499
- rc = sqlite3_exec(db, u.by.zSql, sqlite3InitCallback, &u.by.initData, 0);
68500
- if( rc==SQLITE_OK ) rc = u.by.initData.rc;
68501
- sqlite3DbFree(db, u.by.zSql);
68556
+ rc = sqlite3_exec(db, u.bz.zSql, sqlite3InitCallback, &u.bz.initData, 0);
68557
+ if( rc==SQLITE_OK ) rc = u.bz.initData.rc;
68558
+ sqlite3DbFree(db, u.bz.zSql);
6850268559
db->init.busy = 0;
6850368560
}
6850468561
}
6850568562
if( rc ) sqlite3ResetAllSchemasOfConnection(db);
6850668563
if( rc==SQLITE_NOMEM ){
@@ -68580,45 +68637,45 @@
6858068637
** file, not the main database file.
6858168638
**
6858268639
** This opcode is used to implement the integrity_check pragma.
6858368640
*/
6858468641
case OP_IntegrityCk: {
68585
-#if 0 /* local variables moved into u.bz */
68642
+#if 0 /* local variables moved into u.ca */
6858668643
int nRoot; /* Number of tables to check. (Number of root pages.) */
6858768644
int *aRoot; /* Array of rootpage numbers for tables to be checked */
6858868645
int j; /* Loop counter */
6858968646
int nErr; /* Number of errors reported */
6859068647
char *z; /* Text of the error report */
6859168648
Mem *pnErr; /* Register keeping track of errors remaining */
68592
-#endif /* local variables moved into u.bz */
68649
+#endif /* local variables moved into u.ca */
6859368650
68594
- u.bz.nRoot = pOp->p2;
68595
- assert( u.bz.nRoot>0 );
68596
- u.bz.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bz.nRoot+1) );
68597
- if( u.bz.aRoot==0 ) goto no_mem;
68651
+ u.ca.nRoot = pOp->p2;
68652
+ assert( u.ca.nRoot>0 );
68653
+ u.ca.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.ca.nRoot+1) );
68654
+ if( u.ca.aRoot==0 ) goto no_mem;
6859868655
assert( pOp->p3>0 && pOp->p3<=p->nMem );
68599
- u.bz.pnErr = &aMem[pOp->p3];
68600
- assert( (u.bz.pnErr->flags & MEM_Int)!=0 );
68601
- assert( (u.bz.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
68656
+ u.ca.pnErr = &aMem[pOp->p3];
68657
+ assert( (u.ca.pnErr->flags & MEM_Int)!=0 );
68658
+ assert( (u.ca.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
6860268659
pIn1 = &aMem[pOp->p1];
68603
- for(u.bz.j=0; u.bz.j<u.bz.nRoot; u.bz.j++){
68604
- u.bz.aRoot[u.bz.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bz.j]);
68660
+ for(u.ca.j=0; u.ca.j<u.ca.nRoot; u.ca.j++){
68661
+ u.ca.aRoot[u.ca.j] = (int)sqlite3VdbeIntValue(&pIn1[u.ca.j]);
6860568662
}
68606
- u.bz.aRoot[u.bz.j] = 0;
68663
+ u.ca.aRoot[u.ca.j] = 0;
6860768664
assert( pOp->p5<db->nDb );
6860868665
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
68609
- u.bz.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bz.aRoot, u.bz.nRoot,
68610
- (int)u.bz.pnErr->u.i, &u.bz.nErr);
68611
- sqlite3DbFree(db, u.bz.aRoot);
68612
- u.bz.pnErr->u.i -= u.bz.nErr;
68666
+ u.ca.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.ca.aRoot, u.ca.nRoot,
68667
+ (int)u.ca.pnErr->u.i, &u.ca.nErr);
68668
+ sqlite3DbFree(db, u.ca.aRoot);
68669
+ u.ca.pnErr->u.i -= u.ca.nErr;
6861368670
sqlite3VdbeMemSetNull(pIn1);
68614
- if( u.bz.nErr==0 ){
68615
- assert( u.bz.z==0 );
68616
- }else if( u.bz.z==0 ){
68671
+ if( u.ca.nErr==0 ){
68672
+ assert( u.ca.z==0 );
68673
+ }else if( u.ca.z==0 ){
6861768674
goto no_mem;
6861868675
}else{
68619
- sqlite3VdbeMemSetStr(pIn1, u.bz.z, -1, SQLITE_UTF8, sqlite3_free);
68676
+ sqlite3VdbeMemSetStr(pIn1, u.ca.z, -1, SQLITE_UTF8, sqlite3_free);
6862068677
}
6862168678
UPDATE_MAX_BLOBSIZE(pIn1);
6862268679
sqlite3VdbeChangeEncoding(pIn1, encoding);
6862368680
break;
6862468681
}
@@ -68648,24 +68705,24 @@
6864868705
** Extract the smallest value from boolean index P1 and put that value into
6864968706
** register P3. Or, if boolean index P1 is initially empty, leave P3
6865068707
** unchanged and jump to instruction P2.
6865168708
*/
6865268709
case OP_RowSetRead: { /* jump, in1, out3 */
68653
-#if 0 /* local variables moved into u.ca */
68710
+#if 0 /* local variables moved into u.cb */
6865468711
i64 val;
68655
-#endif /* local variables moved into u.ca */
68712
+#endif /* local variables moved into u.cb */
6865668713
CHECK_FOR_INTERRUPT;
6865768714
pIn1 = &aMem[pOp->p1];
6865868715
if( (pIn1->flags & MEM_RowSet)==0
68659
- || sqlite3RowSetNext(pIn1->u.pRowSet, &u.ca.val)==0
68716
+ || sqlite3RowSetNext(pIn1->u.pRowSet, &u.cb.val)==0
6866068717
){
6866168718
/* The boolean index is empty */
6866268719
sqlite3VdbeMemSetNull(pIn1);
6866368720
pc = pOp->p2 - 1;
6866468721
}else{
6866568722
/* A value was pulled from the index */
68666
- sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.ca.val);
68723
+ sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.cb.val);
6866768724
}
6866868725
break;
6866968726
}
6867068727
6867168728
/* Opcode: RowSetTest P1 P2 P3 P4
@@ -68690,18 +68747,18 @@
6869068747
** inserted, there is no need to search to see if the same value was
6869168748
** previously inserted as part of set X (only if it was previously
6869268749
** inserted as part of some other set).
6869368750
*/
6869468751
case OP_RowSetTest: { /* jump, in1, in3 */
68695
-#if 0 /* local variables moved into u.cb */
68752
+#if 0 /* local variables moved into u.cc */
6869668753
int iSet;
6869768754
int exists;
68698
-#endif /* local variables moved into u.cb */
68755
+#endif /* local variables moved into u.cc */
6869968756
6870068757
pIn1 = &aMem[pOp->p1];
6870168758
pIn3 = &aMem[pOp->p3];
68702
- u.cb.iSet = pOp->p4.i;
68759
+ u.cc.iSet = pOp->p4.i;
6870368760
assert( pIn3->flags&MEM_Int );
6870468761
6870568762
/* If there is anything other than a rowset object in memory cell P1,
6870668763
** delete it now and initialize P1 with an empty rowset
6870768764
*/
@@ -68709,21 +68766,21 @@
6870968766
sqlite3VdbeMemSetRowSet(pIn1);
6871068767
if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
6871168768
}
6871268769
6871368770
assert( pOp->p4type==P4_INT32 );
68714
- assert( u.cb.iSet==-1 || u.cb.iSet>=0 );
68715
- if( u.cb.iSet ){
68716
- u.cb.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
68717
- (u8)(u.cb.iSet>=0 ? u.cb.iSet & 0xf : 0xff),
68771
+ assert( u.cc.iSet==-1 || u.cc.iSet>=0 );
68772
+ if( u.cc.iSet ){
68773
+ u.cc.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
68774
+ (u8)(u.cc.iSet>=0 ? u.cc.iSet & 0xf : 0xff),
6871868775
pIn3->u.i);
68719
- if( u.cb.exists ){
68776
+ if( u.cc.exists ){
6872068777
pc = pOp->p2 - 1;
6872168778
break;
6872268779
}
6872368780
}
68724
- if( u.cb.iSet>=0 ){
68781
+ if( u.cc.iSet>=0 ){
6872568782
sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
6872668783
}
6872768784
break;
6872868785
}
6872968786
@@ -68742,24 +68799,24 @@
6874268799
** memory required by the sub-vdbe at runtime.
6874368800
**
6874468801
** P4 is a pointer to the VM containing the trigger program.
6874568802
*/
6874668803
case OP_Program: { /* jump */
68747
-#if 0 /* local variables moved into u.cc */
68804
+#if 0 /* local variables moved into u.cd */
6874868805
int nMem; /* Number of memory registers for sub-program */
6874968806
int nByte; /* Bytes of runtime space required for sub-program */
6875068807
Mem *pRt; /* Register to allocate runtime space */
6875168808
Mem *pMem; /* Used to iterate through memory cells */
6875268809
Mem *pEnd; /* Last memory cell in new array */
6875368810
VdbeFrame *pFrame; /* New vdbe frame to execute in */
6875468811
SubProgram *pProgram; /* Sub-program to execute */
6875568812
void *t; /* Token identifying trigger */
68756
-#endif /* local variables moved into u.cc */
68813
+#endif /* local variables moved into u.cd */
6875768814
68758
- u.cc.pProgram = pOp->p4.pProgram;
68759
- u.cc.pRt = &aMem[pOp->p3];
68760
- assert( u.cc.pProgram->nOp>0 );
68815
+ u.cd.pProgram = pOp->p4.pProgram;
68816
+ u.cd.pRt = &aMem[pOp->p3];
68817
+ assert( u.cd.pProgram->nOp>0 );
6876168818
6876268819
/* If the p5 flag is clear, then recursive invocation of triggers is
6876368820
** disabled for backwards compatibility (p5 is set if this sub-program
6876468821
** is really a trigger, not a foreign key action, and the flag set
6876568822
** and cleared by the "PRAGMA recursive_triggers" command is clear).
@@ -68769,84 +68826,84 @@
6876968826
** SubProgram (if the trigger may be executed with more than one different
6877068827
** ON CONFLICT algorithm). SubProgram structures associated with a
6877168828
** single trigger all have the same value for the SubProgram.token
6877268829
** variable. */
6877368830
if( pOp->p5 ){
68774
- u.cc.t = u.cc.pProgram->token;
68775
- for(u.cc.pFrame=p->pFrame; u.cc.pFrame && u.cc.pFrame->token!=u.cc.t; u.cc.pFrame=u.cc.pFrame->pParent);
68776
- if( u.cc.pFrame ) break;
68831
+ u.cd.t = u.cd.pProgram->token;
68832
+ for(u.cd.pFrame=p->pFrame; u.cd.pFrame && u.cd.pFrame->token!=u.cd.t; u.cd.pFrame=u.cd.pFrame->pParent);
68833
+ if( u.cd.pFrame ) break;
6877768834
}
6877868835
6877968836
if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
6878068837
rc = SQLITE_ERROR;
6878168838
sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
6878268839
break;
6878368840
}
6878468841
68785
- /* Register u.cc.pRt is used to store the memory required to save the state
68842
+ /* Register u.cd.pRt is used to store the memory required to save the state
6878668843
** of the current program, and the memory required at runtime to execute
68787
- ** the trigger program. If this trigger has been fired before, then u.cc.pRt
68844
+ ** the trigger program. If this trigger has been fired before, then u.cd.pRt
6878868845
** is already allocated. Otherwise, it must be initialized. */
68789
- if( (u.cc.pRt->flags&MEM_Frame)==0 ){
68846
+ if( (u.cd.pRt->flags&MEM_Frame)==0 ){
6879068847
/* SubProgram.nMem is set to the number of memory cells used by the
6879168848
** program stored in SubProgram.aOp. As well as these, one memory
6879268849
** cell is required for each cursor used by the program. Set local
68793
- ** variable u.cc.nMem (and later, VdbeFrame.nChildMem) to this value.
68850
+ ** variable u.cd.nMem (and later, VdbeFrame.nChildMem) to this value.
6879468851
*/
68795
- u.cc.nMem = u.cc.pProgram->nMem + u.cc.pProgram->nCsr;
68796
- u.cc.nByte = ROUND8(sizeof(VdbeFrame))
68797
- + u.cc.nMem * sizeof(Mem)
68798
- + u.cc.pProgram->nCsr * sizeof(VdbeCursor *)
68799
- + u.cc.pProgram->nOnce * sizeof(u8);
68800
- u.cc.pFrame = sqlite3DbMallocZero(db, u.cc.nByte);
68801
- if( !u.cc.pFrame ){
68852
+ u.cd.nMem = u.cd.pProgram->nMem + u.cd.pProgram->nCsr;
68853
+ u.cd.nByte = ROUND8(sizeof(VdbeFrame))
68854
+ + u.cd.nMem * sizeof(Mem)
68855
+ + u.cd.pProgram->nCsr * sizeof(VdbeCursor *)
68856
+ + u.cd.pProgram->nOnce * sizeof(u8);
68857
+ u.cd.pFrame = sqlite3DbMallocZero(db, u.cd.nByte);
68858
+ if( !u.cd.pFrame ){
6880268859
goto no_mem;
6880368860
}
68804
- sqlite3VdbeMemRelease(u.cc.pRt);
68805
- u.cc.pRt->flags = MEM_Frame;
68806
- u.cc.pRt->u.pFrame = u.cc.pFrame;
68807
-
68808
- u.cc.pFrame->v = p;
68809
- u.cc.pFrame->nChildMem = u.cc.nMem;
68810
- u.cc.pFrame->nChildCsr = u.cc.pProgram->nCsr;
68811
- u.cc.pFrame->pc = pc;
68812
- u.cc.pFrame->aMem = p->aMem;
68813
- u.cc.pFrame->nMem = p->nMem;
68814
- u.cc.pFrame->apCsr = p->apCsr;
68815
- u.cc.pFrame->nCursor = p->nCursor;
68816
- u.cc.pFrame->aOp = p->aOp;
68817
- u.cc.pFrame->nOp = p->nOp;
68818
- u.cc.pFrame->token = u.cc.pProgram->token;
68819
- u.cc.pFrame->aOnceFlag = p->aOnceFlag;
68820
- u.cc.pFrame->nOnceFlag = p->nOnceFlag;
68821
-
68822
- u.cc.pEnd = &VdbeFrameMem(u.cc.pFrame)[u.cc.pFrame->nChildMem];
68823
- for(u.cc.pMem=VdbeFrameMem(u.cc.pFrame); u.cc.pMem!=u.cc.pEnd; u.cc.pMem++){
68824
- u.cc.pMem->flags = MEM_Invalid;
68825
- u.cc.pMem->db = db;
68861
+ sqlite3VdbeMemRelease(u.cd.pRt);
68862
+ u.cd.pRt->flags = MEM_Frame;
68863
+ u.cd.pRt->u.pFrame = u.cd.pFrame;
68864
+
68865
+ u.cd.pFrame->v = p;
68866
+ u.cd.pFrame->nChildMem = u.cd.nMem;
68867
+ u.cd.pFrame->nChildCsr = u.cd.pProgram->nCsr;
68868
+ u.cd.pFrame->pc = pc;
68869
+ u.cd.pFrame->aMem = p->aMem;
68870
+ u.cd.pFrame->nMem = p->nMem;
68871
+ u.cd.pFrame->apCsr = p->apCsr;
68872
+ u.cd.pFrame->nCursor = p->nCursor;
68873
+ u.cd.pFrame->aOp = p->aOp;
68874
+ u.cd.pFrame->nOp = p->nOp;
68875
+ u.cd.pFrame->token = u.cd.pProgram->token;
68876
+ u.cd.pFrame->aOnceFlag = p->aOnceFlag;
68877
+ u.cd.pFrame->nOnceFlag = p->nOnceFlag;
68878
+
68879
+ u.cd.pEnd = &VdbeFrameMem(u.cd.pFrame)[u.cd.pFrame->nChildMem];
68880
+ for(u.cd.pMem=VdbeFrameMem(u.cd.pFrame); u.cd.pMem!=u.cd.pEnd; u.cd.pMem++){
68881
+ u.cd.pMem->flags = MEM_Invalid;
68882
+ u.cd.pMem->db = db;
6882668883
}
6882768884
}else{
68828
- u.cc.pFrame = u.cc.pRt->u.pFrame;
68829
- assert( u.cc.pProgram->nMem+u.cc.pProgram->nCsr==u.cc.pFrame->nChildMem );
68830
- assert( u.cc.pProgram->nCsr==u.cc.pFrame->nChildCsr );
68831
- assert( pc==u.cc.pFrame->pc );
68885
+ u.cd.pFrame = u.cd.pRt->u.pFrame;
68886
+ assert( u.cd.pProgram->nMem+u.cd.pProgram->nCsr==u.cd.pFrame->nChildMem );
68887
+ assert( u.cd.pProgram->nCsr==u.cd.pFrame->nChildCsr );
68888
+ assert( pc==u.cd.pFrame->pc );
6883268889
}
6883368890
6883468891
p->nFrame++;
68835
- u.cc.pFrame->pParent = p->pFrame;
68836
- u.cc.pFrame->lastRowid = lastRowid;
68837
- u.cc.pFrame->nChange = p->nChange;
68892
+ u.cd.pFrame->pParent = p->pFrame;
68893
+ u.cd.pFrame->lastRowid = lastRowid;
68894
+ u.cd.pFrame->nChange = p->nChange;
6883868895
p->nChange = 0;
68839
- p->pFrame = u.cc.pFrame;
68840
- p->aMem = aMem = &VdbeFrameMem(u.cc.pFrame)[-1];
68841
- p->nMem = u.cc.pFrame->nChildMem;
68842
- p->nCursor = (u16)u.cc.pFrame->nChildCsr;
68896
+ p->pFrame = u.cd.pFrame;
68897
+ p->aMem = aMem = &VdbeFrameMem(u.cd.pFrame)[-1];
68898
+ p->nMem = u.cd.pFrame->nChildMem;
68899
+ p->nCursor = (u16)u.cd.pFrame->nChildCsr;
6884368900
p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
68844
- p->aOp = aOp = u.cc.pProgram->aOp;
68845
- p->nOp = u.cc.pProgram->nOp;
68901
+ p->aOp = aOp = u.cd.pProgram->aOp;
68902
+ p->nOp = u.cd.pProgram->nOp;
6884668903
p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
68847
- p->nOnceFlag = u.cc.pProgram->nOnce;
68904
+ p->nOnceFlag = u.cd.pProgram->nOnce;
6884868905
pc = -1;
6884968906
memset(p->aOnceFlag, 0, p->nOnceFlag);
6885068907
6885168908
break;
6885268909
}
@@ -68862,17 +68919,17 @@
6886268919
** The address of the cell in the parent frame is determined by adding
6886368920
** the value of the P1 argument to the value of the P1 argument to the
6886468921
** calling OP_Program instruction.
6886568922
*/
6886668923
case OP_Param: { /* out2-prerelease */
68867
-#if 0 /* local variables moved into u.cd */
68924
+#if 0 /* local variables moved into u.ce */
6886868925
VdbeFrame *pFrame;
6886968926
Mem *pIn;
68870
-#endif /* local variables moved into u.cd */
68871
- u.cd.pFrame = p->pFrame;
68872
- u.cd.pIn = &u.cd.pFrame->aMem[pOp->p1 + u.cd.pFrame->aOp[u.cd.pFrame->pc].p1];
68873
- sqlite3VdbeMemShallowCopy(pOut, u.cd.pIn, MEM_Ephem);
68927
+#endif /* local variables moved into u.ce */
68928
+ u.ce.pFrame = p->pFrame;
68929
+ u.ce.pIn = &u.ce.pFrame->aMem[pOp->p1 + u.ce.pFrame->aOp[u.ce.pFrame->pc].p1];
68930
+ sqlite3VdbeMemShallowCopy(pOut, u.ce.pIn, MEM_Ephem);
6887468931
break;
6887568932
}
6887668933
6887768934
#endif /* #ifndef SQLITE_OMIT_TRIGGER */
6887868935
@@ -68924,26 +68981,26 @@
6892468981
**
6892568982
** This instruction throws an error if the memory cell is not initially
6892668983
** an integer.
6892768984
*/
6892868985
case OP_MemMax: { /* in2 */
68929
-#if 0 /* local variables moved into u.ce */
68986
+#if 0 /* local variables moved into u.cf */
6893068987
Mem *pIn1;
6893168988
VdbeFrame *pFrame;
68932
-#endif /* local variables moved into u.ce */
68989
+#endif /* local variables moved into u.cf */
6893368990
if( p->pFrame ){
68934
- for(u.ce.pFrame=p->pFrame; u.ce.pFrame->pParent; u.ce.pFrame=u.ce.pFrame->pParent);
68935
- u.ce.pIn1 = &u.ce.pFrame->aMem[pOp->p1];
68991
+ for(u.cf.pFrame=p->pFrame; u.cf.pFrame->pParent; u.cf.pFrame=u.cf.pFrame->pParent);
68992
+ u.cf.pIn1 = &u.cf.pFrame->aMem[pOp->p1];
6893668993
}else{
68937
- u.ce.pIn1 = &aMem[pOp->p1];
68994
+ u.cf.pIn1 = &aMem[pOp->p1];
6893868995
}
68939
- assert( memIsValid(u.ce.pIn1) );
68940
- sqlite3VdbeMemIntegerify(u.ce.pIn1);
68996
+ assert( memIsValid(u.cf.pIn1) );
68997
+ sqlite3VdbeMemIntegerify(u.cf.pIn1);
6894168998
pIn2 = &aMem[pOp->p2];
6894268999
sqlite3VdbeMemIntegerify(pIn2);
68943
- if( u.ce.pIn1->u.i<pIn2->u.i){
68944
- u.ce.pIn1->u.i = pIn2->u.i;
69000
+ if( u.cf.pIn1->u.i<pIn2->u.i){
69001
+ u.cf.pIn1->u.i = pIn2->u.i;
6894569002
}
6894669003
break;
6894769004
}
6894869005
#endif /* SQLITE_OMIT_AUTOINCREMENT */
6894969006
@@ -69006,60 +69063,60 @@
6900669063
**
6900769064
** The P5 arguments are taken from register P2 and its
6900869065
** successors.
6900969066
*/
6901069067
case OP_AggStep: {
69011
-#if 0 /* local variables moved into u.cf */
69068
+#if 0 /* local variables moved into u.cg */
6901269069
int n;
6901369070
int i;
6901469071
Mem *pMem;
6901569072
Mem *pRec;
6901669073
sqlite3_context ctx;
6901769074
sqlite3_value **apVal;
69018
-#endif /* local variables moved into u.cf */
69019
-
69020
- u.cf.n = pOp->p5;
69021
- assert( u.cf.n>=0 );
69022
- u.cf.pRec = &aMem[pOp->p2];
69023
- u.cf.apVal = p->apArg;
69024
- assert( u.cf.apVal || u.cf.n==0 );
69025
- for(u.cf.i=0; u.cf.i<u.cf.n; u.cf.i++, u.cf.pRec++){
69026
- assert( memIsValid(u.cf.pRec) );
69027
- u.cf.apVal[u.cf.i] = u.cf.pRec;
69028
- memAboutToChange(p, u.cf.pRec);
69029
- sqlite3VdbeMemStoreType(u.cf.pRec);
69030
- }
69031
- u.cf.ctx.pFunc = pOp->p4.pFunc;
69075
+#endif /* local variables moved into u.cg */
69076
+
69077
+ u.cg.n = pOp->p5;
69078
+ assert( u.cg.n>=0 );
69079
+ u.cg.pRec = &aMem[pOp->p2];
69080
+ u.cg.apVal = p->apArg;
69081
+ assert( u.cg.apVal || u.cg.n==0 );
69082
+ for(u.cg.i=0; u.cg.i<u.cg.n; u.cg.i++, u.cg.pRec++){
69083
+ assert( memIsValid(u.cg.pRec) );
69084
+ u.cg.apVal[u.cg.i] = u.cg.pRec;
69085
+ memAboutToChange(p, u.cg.pRec);
69086
+ sqlite3VdbeMemStoreType(u.cg.pRec);
69087
+ }
69088
+ u.cg.ctx.pFunc = pOp->p4.pFunc;
6903269089
assert( pOp->p3>0 && pOp->p3<=p->nMem );
69033
- u.cf.ctx.pMem = u.cf.pMem = &aMem[pOp->p3];
69034
- u.cf.pMem->n++;
69035
- u.cf.ctx.s.flags = MEM_Null;
69036
- u.cf.ctx.s.z = 0;
69037
- u.cf.ctx.s.zMalloc = 0;
69038
- u.cf.ctx.s.xDel = 0;
69039
- u.cf.ctx.s.db = db;
69040
- u.cf.ctx.isError = 0;
69041
- u.cf.ctx.pColl = 0;
69042
- u.cf.ctx.skipFlag = 0;
69043
- if( u.cf.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
69090
+ u.cg.ctx.pMem = u.cg.pMem = &aMem[pOp->p3];
69091
+ u.cg.pMem->n++;
69092
+ u.cg.ctx.s.flags = MEM_Null;
69093
+ u.cg.ctx.s.z = 0;
69094
+ u.cg.ctx.s.zMalloc = 0;
69095
+ u.cg.ctx.s.xDel = 0;
69096
+ u.cg.ctx.s.db = db;
69097
+ u.cg.ctx.isError = 0;
69098
+ u.cg.ctx.pColl = 0;
69099
+ u.cg.ctx.skipFlag = 0;
69100
+ if( u.cg.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
6904469101
assert( pOp>p->aOp );
6904569102
assert( pOp[-1].p4type==P4_COLLSEQ );
6904669103
assert( pOp[-1].opcode==OP_CollSeq );
69047
- u.cf.ctx.pColl = pOp[-1].p4.pColl;
69048
- }
69049
- (u.cf.ctx.pFunc->xStep)(&u.cf.ctx, u.cf.n, u.cf.apVal); /* IMP: R-24505-23230 */
69050
- if( u.cf.ctx.isError ){
69051
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cf.ctx.s));
69052
- rc = u.cf.ctx.isError;
69053
- }
69054
- if( u.cf.ctx.skipFlag ){
69104
+ u.cg.ctx.pColl = pOp[-1].p4.pColl;
69105
+ }
69106
+ (u.cg.ctx.pFunc->xStep)(&u.cg.ctx, u.cg.n, u.cg.apVal); /* IMP: R-24505-23230 */
69107
+ if( u.cg.ctx.isError ){
69108
+ sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cg.ctx.s));
69109
+ rc = u.cg.ctx.isError;
69110
+ }
69111
+ if( u.cg.ctx.skipFlag ){
6905569112
assert( pOp[-1].opcode==OP_CollSeq );
69056
- u.cf.i = pOp[-1].p1;
69057
- if( u.cf.i ) sqlite3VdbeMemSetInt64(&aMem[u.cf.i], 1);
69113
+ u.cg.i = pOp[-1].p1;
69114
+ if( u.cg.i ) sqlite3VdbeMemSetInt64(&aMem[u.cg.i], 1);
6905869115
}
6905969116
69060
- sqlite3VdbeMemRelease(&u.cf.ctx.s);
69117
+ sqlite3VdbeMemRelease(&u.cg.ctx.s);
6906169118
6906269119
break;
6906369120
}
6906469121
6906569122
/* Opcode: AggFinal P1 P2 * P4 *
@@ -69073,23 +69130,23 @@
6907369130
** functions that can take varying numbers of arguments. The
6907469131
** P4 argument is only needed for the degenerate case where
6907569132
** the step function was not previously called.
6907669133
*/
6907769134
case OP_AggFinal: {
69078
-#if 0 /* local variables moved into u.cg */
69135
+#if 0 /* local variables moved into u.ch */
6907969136
Mem *pMem;
69080
-#endif /* local variables moved into u.cg */
69137
+#endif /* local variables moved into u.ch */
6908169138
assert( pOp->p1>0 && pOp->p1<=p->nMem );
69082
- u.cg.pMem = &aMem[pOp->p1];
69083
- assert( (u.cg.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
69084
- rc = sqlite3VdbeMemFinalize(u.cg.pMem, pOp->p4.pFunc);
69139
+ u.ch.pMem = &aMem[pOp->p1];
69140
+ assert( (u.ch.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
69141
+ rc = sqlite3VdbeMemFinalize(u.ch.pMem, pOp->p4.pFunc);
6908569142
if( rc ){
69086
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cg.pMem));
69143
+ sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.ch.pMem));
6908769144
}
69088
- sqlite3VdbeChangeEncoding(u.cg.pMem, encoding);
69089
- UPDATE_MAX_BLOBSIZE(u.cg.pMem);
69090
- if( sqlite3VdbeMemTooBig(u.cg.pMem) ){
69145
+ sqlite3VdbeChangeEncoding(u.ch.pMem, encoding);
69146
+ UPDATE_MAX_BLOBSIZE(u.ch.pMem);
69147
+ if( sqlite3VdbeMemTooBig(u.ch.pMem) ){
6909169148
goto too_big;
6909269149
}
6909369150
break;
6909469151
}
6909569152
@@ -69104,29 +69161,29 @@
6910469161
** in the WAL that have been checkpointed after the checkpoint
6910569162
** completes into mem[P3+2]. However on an error, mem[P3+1] and
6910669163
** mem[P3+2] are initialized to -1.
6910769164
*/
6910869165
case OP_Checkpoint: {
69109
-#if 0 /* local variables moved into u.ch */
69166
+#if 0 /* local variables moved into u.ci */
6911069167
int i; /* Loop counter */
6911169168
int aRes[3]; /* Results */
6911269169
Mem *pMem; /* Write results here */
69113
-#endif /* local variables moved into u.ch */
69170
+#endif /* local variables moved into u.ci */
6911469171
69115
- u.ch.aRes[0] = 0;
69116
- u.ch.aRes[1] = u.ch.aRes[2] = -1;
69172
+ u.ci.aRes[0] = 0;
69173
+ u.ci.aRes[1] = u.ci.aRes[2] = -1;
6911769174
assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
6911869175
|| pOp->p2==SQLITE_CHECKPOINT_FULL
6911969176
|| pOp->p2==SQLITE_CHECKPOINT_RESTART
6912069177
);
69121
- rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.ch.aRes[1], &u.ch.aRes[2]);
69178
+ rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.ci.aRes[1], &u.ci.aRes[2]);
6912269179
if( rc==SQLITE_BUSY ){
6912369180
rc = SQLITE_OK;
69124
- u.ch.aRes[0] = 1;
69181
+ u.ci.aRes[0] = 1;
6912569182
}
69126
- for(u.ch.i=0, u.ch.pMem = &aMem[pOp->p3]; u.ch.i<3; u.ch.i++, u.ch.pMem++){
69127
- sqlite3VdbeMemSetInt64(u.ch.pMem, (i64)u.ch.aRes[u.ch.i]);
69183
+ for(u.ci.i=0, u.ci.pMem = &aMem[pOp->p3]; u.ci.i<3; u.ci.i++, u.ci.pMem++){
69184
+ sqlite3VdbeMemSetInt64(u.ci.pMem, (i64)u.ci.aRes[u.ci.i]);
6912869185
}
6912969186
break;
6913069187
};
6913169188
#endif
6913269189
@@ -69141,97 +69198,97 @@
6914169198
** If changing into or out of WAL mode the procedure is more complicated.
6914269199
**
6914369200
** Write a string containing the final journal-mode to register P2.
6914469201
*/
6914569202
case OP_JournalMode: { /* out2-prerelease */
69146
-#if 0 /* local variables moved into u.ci */
69203
+#if 0 /* local variables moved into u.cj */
6914769204
Btree *pBt; /* Btree to change journal mode of */
6914869205
Pager *pPager; /* Pager associated with pBt */
6914969206
int eNew; /* New journal mode */
6915069207
int eOld; /* The old journal mode */
69151
-#endif /* local variables moved into u.ci */
6915269208
#ifndef SQLITE_OMIT_WAL
69153
- const char *zFilename; /* Name of database file for u.ci.pPager */
69209
+ const char *zFilename; /* Name of database file for pPager */
6915469210
#endif
69155
-
69156
- u.ci.eNew = pOp->p3;
69157
- assert( u.ci.eNew==PAGER_JOURNALMODE_DELETE
69158
- || u.ci.eNew==PAGER_JOURNALMODE_TRUNCATE
69159
- || u.ci.eNew==PAGER_JOURNALMODE_PERSIST
69160
- || u.ci.eNew==PAGER_JOURNALMODE_OFF
69161
- || u.ci.eNew==PAGER_JOURNALMODE_MEMORY
69162
- || u.ci.eNew==PAGER_JOURNALMODE_WAL
69163
- || u.ci.eNew==PAGER_JOURNALMODE_QUERY
69211
+#endif /* local variables moved into u.cj */
69212
+
69213
+ u.cj.eNew = pOp->p3;
69214
+ assert( u.cj.eNew==PAGER_JOURNALMODE_DELETE
69215
+ || u.cj.eNew==PAGER_JOURNALMODE_TRUNCATE
69216
+ || u.cj.eNew==PAGER_JOURNALMODE_PERSIST
69217
+ || u.cj.eNew==PAGER_JOURNALMODE_OFF
69218
+ || u.cj.eNew==PAGER_JOURNALMODE_MEMORY
69219
+ || u.cj.eNew==PAGER_JOURNALMODE_WAL
69220
+ || u.cj.eNew==PAGER_JOURNALMODE_QUERY
6916469221
);
6916569222
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6916669223
69167
- u.ci.pBt = db->aDb[pOp->p1].pBt;
69168
- u.ci.pPager = sqlite3BtreePager(u.ci.pBt);
69169
- u.ci.eOld = sqlite3PagerGetJournalMode(u.ci.pPager);
69170
- if( u.ci.eNew==PAGER_JOURNALMODE_QUERY ) u.ci.eNew = u.ci.eOld;
69171
- if( !sqlite3PagerOkToChangeJournalMode(u.ci.pPager) ) u.ci.eNew = u.ci.eOld;
69224
+ u.cj.pBt = db->aDb[pOp->p1].pBt;
69225
+ u.cj.pPager = sqlite3BtreePager(u.cj.pBt);
69226
+ u.cj.eOld = sqlite3PagerGetJournalMode(u.cj.pPager);
69227
+ if( u.cj.eNew==PAGER_JOURNALMODE_QUERY ) u.cj.eNew = u.cj.eOld;
69228
+ if( !sqlite3PagerOkToChangeJournalMode(u.cj.pPager) ) u.cj.eNew = u.cj.eOld;
6917269229
6917369230
#ifndef SQLITE_OMIT_WAL
69174
- zFilename = sqlite3PagerFilename(u.ci.pPager, 1);
69231
+ u.cj.zFilename = sqlite3PagerFilename(u.cj.pPager, 1);
6917569232
6917669233
/* Do not allow a transition to journal_mode=WAL for a database
6917769234
** in temporary storage or if the VFS does not support shared memory
6917869235
*/
69179
- if( u.ci.eNew==PAGER_JOURNALMODE_WAL
69180
- && (sqlite3Strlen30(zFilename)==0 /* Temp file */
69181
- || !sqlite3PagerWalSupported(u.ci.pPager)) /* No shared-memory support */
69236
+ if( u.cj.eNew==PAGER_JOURNALMODE_WAL
69237
+ && (sqlite3Strlen30(u.cj.zFilename)==0 /* Temp file */
69238
+ || !sqlite3PagerWalSupported(u.cj.pPager)) /* No shared-memory support */
6918269239
){
69183
- u.ci.eNew = u.ci.eOld;
69240
+ u.cj.eNew = u.cj.eOld;
6918469241
}
6918569242
69186
- if( (u.ci.eNew!=u.ci.eOld)
69187
- && (u.ci.eOld==PAGER_JOURNALMODE_WAL || u.ci.eNew==PAGER_JOURNALMODE_WAL)
69243
+ if( (u.cj.eNew!=u.cj.eOld)
69244
+ && (u.cj.eOld==PAGER_JOURNALMODE_WAL || u.cj.eNew==PAGER_JOURNALMODE_WAL)
6918869245
){
6918969246
if( !db->autoCommit || db->activeVdbeCnt>1 ){
6919069247
rc = SQLITE_ERROR;
6919169248
sqlite3SetString(&p->zErrMsg, db,
6919269249
"cannot change %s wal mode from within a transaction",
69193
- (u.ci.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
69250
+ (u.cj.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6919469251
);
6919569252
break;
6919669253
}else{
6919769254
69198
- if( u.ci.eOld==PAGER_JOURNALMODE_WAL ){
69255
+ if( u.cj.eOld==PAGER_JOURNALMODE_WAL ){
6919969256
/* If leaving WAL mode, close the log file. If successful, the call
6920069257
** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6920169258
** file. An EXCLUSIVE lock may still be held on the database file
6920269259
** after a successful return.
6920369260
*/
69204
- rc = sqlite3PagerCloseWal(u.ci.pPager);
69261
+ rc = sqlite3PagerCloseWal(u.cj.pPager);
6920569262
if( rc==SQLITE_OK ){
69206
- sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
69263
+ sqlite3PagerSetJournalMode(u.cj.pPager, u.cj.eNew);
6920769264
}
69208
- }else if( u.ci.eOld==PAGER_JOURNALMODE_MEMORY ){
69265
+ }else if( u.cj.eOld==PAGER_JOURNALMODE_MEMORY ){
6920969266
/* Cannot transition directly from MEMORY to WAL. Use mode OFF
6921069267
** as an intermediate */
69211
- sqlite3PagerSetJournalMode(u.ci.pPager, PAGER_JOURNALMODE_OFF);
69268
+ sqlite3PagerSetJournalMode(u.cj.pPager, PAGER_JOURNALMODE_OFF);
6921269269
}
6921369270
6921469271
/* Open a transaction on the database file. Regardless of the journal
6921569272
** mode, this transaction always uses a rollback journal.
6921669273
*/
69217
- assert( sqlite3BtreeIsInTrans(u.ci.pBt)==0 );
69274
+ assert( sqlite3BtreeIsInTrans(u.cj.pBt)==0 );
6921869275
if( rc==SQLITE_OK ){
69219
- rc = sqlite3BtreeSetVersion(u.ci.pBt, (u.ci.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
69276
+ rc = sqlite3BtreeSetVersion(u.cj.pBt, (u.cj.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
6922069277
}
6922169278
}
6922269279
}
6922369280
#endif /* ifndef SQLITE_OMIT_WAL */
6922469281
6922569282
if( rc ){
69226
- u.ci.eNew = u.ci.eOld;
69283
+ u.cj.eNew = u.cj.eOld;
6922769284
}
69228
- u.ci.eNew = sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
69285
+ u.cj.eNew = sqlite3PagerSetJournalMode(u.cj.pPager, u.cj.eNew);
6922969286
6923069287
pOut = &aMem[pOp->p2];
6923169288
pOut->flags = MEM_Str|MEM_Static|MEM_Term;
69232
- pOut->z = (char *)sqlite3JournalModename(u.ci.eNew);
69289
+ pOut->z = (char *)sqlite3JournalModename(u.cj.eNew);
6923369290
pOut->n = sqlite3Strlen30(pOut->z);
6923469291
pOut->enc = SQLITE_UTF8;
6923569292
sqlite3VdbeChangeEncoding(pOut, encoding);
6923669293
break;
6923769294
};
@@ -69256,18 +69313,18 @@
6925669313
** Perform a single step of the incremental vacuum procedure on
6925769314
** the P1 database. If the vacuum has finished, jump to instruction
6925869315
** P2. Otherwise, fall through to the next instruction.
6925969316
*/
6926069317
case OP_IncrVacuum: { /* jump */
69261
-#if 0 /* local variables moved into u.cj */
69318
+#if 0 /* local variables moved into u.ck */
6926269319
Btree *pBt;
69263
-#endif /* local variables moved into u.cj */
69320
+#endif /* local variables moved into u.ck */
6926469321
6926569322
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6926669323
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
69267
- u.cj.pBt = db->aDb[pOp->p1].pBt;
69268
- rc = sqlite3BtreeIncrVacuum(u.cj.pBt);
69324
+ u.ck.pBt = db->aDb[pOp->p1].pBt;
69325
+ rc = sqlite3BtreeIncrVacuum(u.ck.pBt);
6926969326
if( rc==SQLITE_DONE ){
6927069327
pc = pOp->p2 - 1;
6927169328
rc = SQLITE_OK;
6927269329
}
6927369330
break;
@@ -69333,16 +69390,16 @@
6933369390
** Also, whether or not P4 is set, check that this is not being called from
6933469391
** within a callback to a virtual table xSync() method. If it is, the error
6933569392
** code will be set to SQLITE_LOCKED.
6933669393
*/
6933769394
case OP_VBegin: {
69338
-#if 0 /* local variables moved into u.ck */
69395
+#if 0 /* local variables moved into u.cl */
6933969396
VTable *pVTab;
69340
-#endif /* local variables moved into u.ck */
69341
- u.ck.pVTab = pOp->p4.pVtab;
69342
- rc = sqlite3VtabBegin(db, u.ck.pVTab);
69343
- if( u.ck.pVTab ) importVtabErrMsg(p, u.ck.pVTab->pVtab);
69397
+#endif /* local variables moved into u.cl */
69398
+ u.cl.pVTab = pOp->p4.pVtab;
69399
+ rc = sqlite3VtabBegin(db, u.cl.pVTab);
69400
+ if( u.cl.pVTab ) importVtabErrMsg(p, u.cl.pVTab->pVtab);
6934469401
break;
6934569402
}
6934669403
#endif /* SQLITE_OMIT_VIRTUALTABLE */
6934769404
6934869405
#ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -69377,36 +69434,36 @@
6937769434
** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
6937869435
** P1 is a cursor number. This opcode opens a cursor to the virtual
6937969436
** table and stores that cursor in P1.
6938069437
*/
6938169438
case OP_VOpen: {
69382
-#if 0 /* local variables moved into u.cl */
69439
+#if 0 /* local variables moved into u.cm */
6938369440
VdbeCursor *pCur;
6938469441
sqlite3_vtab_cursor *pVtabCursor;
6938569442
sqlite3_vtab *pVtab;
6938669443
sqlite3_module *pModule;
69387
-#endif /* local variables moved into u.cl */
69388
-
69389
- u.cl.pCur = 0;
69390
- u.cl.pVtabCursor = 0;
69391
- u.cl.pVtab = pOp->p4.pVtab->pVtab;
69392
- u.cl.pModule = (sqlite3_module *)u.cl.pVtab->pModule;
69393
- assert(u.cl.pVtab && u.cl.pModule);
69394
- rc = u.cl.pModule->xOpen(u.cl.pVtab, &u.cl.pVtabCursor);
69395
- importVtabErrMsg(p, u.cl.pVtab);
69444
+#endif /* local variables moved into u.cm */
69445
+
69446
+ u.cm.pCur = 0;
69447
+ u.cm.pVtabCursor = 0;
69448
+ u.cm.pVtab = pOp->p4.pVtab->pVtab;
69449
+ u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
69450
+ assert(u.cm.pVtab && u.cm.pModule);
69451
+ rc = u.cm.pModule->xOpen(u.cm.pVtab, &u.cm.pVtabCursor);
69452
+ importVtabErrMsg(p, u.cm.pVtab);
6939669453
if( SQLITE_OK==rc ){
6939769454
/* Initialize sqlite3_vtab_cursor base class */
69398
- u.cl.pVtabCursor->pVtab = u.cl.pVtab;
69455
+ u.cm.pVtabCursor->pVtab = u.cm.pVtab;
6939969456
6940069457
/* Initialise vdbe cursor object */
69401
- u.cl.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
69402
- if( u.cl.pCur ){
69403
- u.cl.pCur->pVtabCursor = u.cl.pVtabCursor;
69404
- u.cl.pCur->pModule = u.cl.pVtabCursor->pVtab->pModule;
69458
+ u.cm.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
69459
+ if( u.cm.pCur ){
69460
+ u.cm.pCur->pVtabCursor = u.cm.pVtabCursor;
69461
+ u.cm.pCur->pModule = u.cm.pVtabCursor->pVtab->pModule;
6940569462
}else{
6940669463
db->mallocFailed = 1;
69407
- u.cl.pModule->xClose(u.cl.pVtabCursor);
69464
+ u.cm.pModule->xClose(u.cm.pVtabCursor);
6940869465
}
6940969466
}
6941069467
break;
6941169468
}
6941269469
#endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -69429,11 +69486,11 @@
6942969486
** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
6943069487
**
6943169488
** A jump is made to P2 if the result set after filtering would be empty.
6943269489
*/
6943369490
case OP_VFilter: { /* jump */
69434
-#if 0 /* local variables moved into u.cm */
69491
+#if 0 /* local variables moved into u.cn */
6943569492
int nArg;
6943669493
int iQuery;
6943769494
const sqlite3_module *pModule;
6943869495
Mem *pQuery;
6943969496
Mem *pArgc;
@@ -69441,49 +69498,49 @@
6944169498
sqlite3_vtab *pVtab;
6944269499
VdbeCursor *pCur;
6944369500
int res;
6944469501
int i;
6944569502
Mem **apArg;
69446
-#endif /* local variables moved into u.cm */
69447
-
69448
- u.cm.pQuery = &aMem[pOp->p3];
69449
- u.cm.pArgc = &u.cm.pQuery[1];
69450
- u.cm.pCur = p->apCsr[pOp->p1];
69451
- assert( memIsValid(u.cm.pQuery) );
69452
- REGISTER_TRACE(pOp->p3, u.cm.pQuery);
69453
- assert( u.cm.pCur->pVtabCursor );
69454
- u.cm.pVtabCursor = u.cm.pCur->pVtabCursor;
69455
- u.cm.pVtab = u.cm.pVtabCursor->pVtab;
69456
- u.cm.pModule = u.cm.pVtab->pModule;
69503
+#endif /* local variables moved into u.cn */
69504
+
69505
+ u.cn.pQuery = &aMem[pOp->p3];
69506
+ u.cn.pArgc = &u.cn.pQuery[1];
69507
+ u.cn.pCur = p->apCsr[pOp->p1];
69508
+ assert( memIsValid(u.cn.pQuery) );
69509
+ REGISTER_TRACE(pOp->p3, u.cn.pQuery);
69510
+ assert( u.cn.pCur->pVtabCursor );
69511
+ u.cn.pVtabCursor = u.cn.pCur->pVtabCursor;
69512
+ u.cn.pVtab = u.cn.pVtabCursor->pVtab;
69513
+ u.cn.pModule = u.cn.pVtab->pModule;
6945769514
6945869515
/* Grab the index number and argc parameters */
69459
- assert( (u.cm.pQuery->flags&MEM_Int)!=0 && u.cm.pArgc->flags==MEM_Int );
69460
- u.cm.nArg = (int)u.cm.pArgc->u.i;
69461
- u.cm.iQuery = (int)u.cm.pQuery->u.i;
69516
+ assert( (u.cn.pQuery->flags&MEM_Int)!=0 && u.cn.pArgc->flags==MEM_Int );
69517
+ u.cn.nArg = (int)u.cn.pArgc->u.i;
69518
+ u.cn.iQuery = (int)u.cn.pQuery->u.i;
6946269519
6946369520
/* Invoke the xFilter method */
6946469521
{
69465
- u.cm.res = 0;
69466
- u.cm.apArg = p->apArg;
69467
- for(u.cm.i = 0; u.cm.i<u.cm.nArg; u.cm.i++){
69468
- u.cm.apArg[u.cm.i] = &u.cm.pArgc[u.cm.i+1];
69469
- sqlite3VdbeMemStoreType(u.cm.apArg[u.cm.i]);
69522
+ u.cn.res = 0;
69523
+ u.cn.apArg = p->apArg;
69524
+ for(u.cn.i = 0; u.cn.i<u.cn.nArg; u.cn.i++){
69525
+ u.cn.apArg[u.cn.i] = &u.cn.pArgc[u.cn.i+1];
69526
+ sqlite3VdbeMemStoreType(u.cn.apArg[u.cn.i]);
6947069527
}
6947169528
6947269529
p->inVtabMethod = 1;
69473
- rc = u.cm.pModule->xFilter(u.cm.pVtabCursor, u.cm.iQuery, pOp->p4.z, u.cm.nArg, u.cm.apArg);
69530
+ rc = u.cn.pModule->xFilter(u.cn.pVtabCursor, u.cn.iQuery, pOp->p4.z, u.cn.nArg, u.cn.apArg);
6947469531
p->inVtabMethod = 0;
69475
- importVtabErrMsg(p, u.cm.pVtab);
69532
+ importVtabErrMsg(p, u.cn.pVtab);
6947669533
if( rc==SQLITE_OK ){
69477
- u.cm.res = u.cm.pModule->xEof(u.cm.pVtabCursor);
69534
+ u.cn.res = u.cn.pModule->xEof(u.cn.pVtabCursor);
6947869535
}
6947969536
69480
- if( u.cm.res ){
69537
+ if( u.cn.res ){
6948169538
pc = pOp->p2 - 1;
6948269539
}
6948369540
}
69484
- u.cm.pCur->nullRow = 0;
69541
+ u.cn.pCur->nullRow = 0;
6948569542
6948669543
break;
6948769544
}
6948869545
#endif /* SQLITE_OMIT_VIRTUALTABLE */
6948969546
@@ -69493,55 +69550,55 @@
6949369550
** Store the value of the P2-th column of
6949469551
** the row of the virtual-table that the
6949569552
** P1 cursor is pointing to into register P3.
6949669553
*/
6949769554
case OP_VColumn: {
69498
-#if 0 /* local variables moved into u.cn */
69555
+#if 0 /* local variables moved into u.co */
6949969556
sqlite3_vtab *pVtab;
6950069557
const sqlite3_module *pModule;
6950169558
Mem *pDest;
6950269559
sqlite3_context sContext;
69503
-#endif /* local variables moved into u.cn */
69560
+#endif /* local variables moved into u.co */
6950469561
6950569562
VdbeCursor *pCur = p->apCsr[pOp->p1];
6950669563
assert( pCur->pVtabCursor );
6950769564
assert( pOp->p3>0 && pOp->p3<=p->nMem );
69508
- u.cn.pDest = &aMem[pOp->p3];
69509
- memAboutToChange(p, u.cn.pDest);
69565
+ u.co.pDest = &aMem[pOp->p3];
69566
+ memAboutToChange(p, u.co.pDest);
6951069567
if( pCur->nullRow ){
69511
- sqlite3VdbeMemSetNull(u.cn.pDest);
69568
+ sqlite3VdbeMemSetNull(u.co.pDest);
6951269569
break;
6951369570
}
69514
- u.cn.pVtab = pCur->pVtabCursor->pVtab;
69515
- u.cn.pModule = u.cn.pVtab->pModule;
69516
- assert( u.cn.pModule->xColumn );
69517
- memset(&u.cn.sContext, 0, sizeof(u.cn.sContext));
69571
+ u.co.pVtab = pCur->pVtabCursor->pVtab;
69572
+ u.co.pModule = u.co.pVtab->pModule;
69573
+ assert( u.co.pModule->xColumn );
69574
+ memset(&u.co.sContext, 0, sizeof(u.co.sContext));
6951869575
6951969576
/* The output cell may already have a buffer allocated. Move
69520
- ** the current contents to u.cn.sContext.s so in case the user-function
69577
+ ** the current contents to u.co.sContext.s so in case the user-function
6952169578
** can use the already allocated buffer instead of allocating a
6952269579
** new one.
6952369580
*/
69524
- sqlite3VdbeMemMove(&u.cn.sContext.s, u.cn.pDest);
69525
- MemSetTypeFlag(&u.cn.sContext.s, MEM_Null);
69581
+ sqlite3VdbeMemMove(&u.co.sContext.s, u.co.pDest);
69582
+ MemSetTypeFlag(&u.co.sContext.s, MEM_Null);
6952669583
69527
- rc = u.cn.pModule->xColumn(pCur->pVtabCursor, &u.cn.sContext, pOp->p2);
69528
- importVtabErrMsg(p, u.cn.pVtab);
69529
- if( u.cn.sContext.isError ){
69530
- rc = u.cn.sContext.isError;
69584
+ rc = u.co.pModule->xColumn(pCur->pVtabCursor, &u.co.sContext, pOp->p2);
69585
+ importVtabErrMsg(p, u.co.pVtab);
69586
+ if( u.co.sContext.isError ){
69587
+ rc = u.co.sContext.isError;
6953169588
}
6953269589
6953369590
/* Copy the result of the function to the P3 register. We
6953469591
** do this regardless of whether or not an error occurred to ensure any
69535
- ** dynamic allocation in u.cn.sContext.s (a Mem struct) is released.
69592
+ ** dynamic allocation in u.co.sContext.s (a Mem struct) is released.
6953669593
*/
69537
- sqlite3VdbeChangeEncoding(&u.cn.sContext.s, encoding);
69538
- sqlite3VdbeMemMove(u.cn.pDest, &u.cn.sContext.s);
69539
- REGISTER_TRACE(pOp->p3, u.cn.pDest);
69540
- UPDATE_MAX_BLOBSIZE(u.cn.pDest);
69594
+ sqlite3VdbeChangeEncoding(&u.co.sContext.s, encoding);
69595
+ sqlite3VdbeMemMove(u.co.pDest, &u.co.sContext.s);
69596
+ REGISTER_TRACE(pOp->p3, u.co.pDest);
69597
+ UPDATE_MAX_BLOBSIZE(u.co.pDest);
6954169598
69542
- if( sqlite3VdbeMemTooBig(u.cn.pDest) ){
69599
+ if( sqlite3VdbeMemTooBig(u.co.pDest) ){
6954369600
goto too_big;
6954469601
}
6954569602
break;
6954669603
}
6954769604
#endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -69552,42 +69609,42 @@
6955269609
** Advance virtual table P1 to the next row in its result set and
6955369610
** jump to instruction P2. Or, if the virtual table has reached
6955469611
** the end of its result set, then fall through to the next instruction.
6955569612
*/
6955669613
case OP_VNext: { /* jump */
69557
-#if 0 /* local variables moved into u.co */
69614
+#if 0 /* local variables moved into u.cp */
6955869615
sqlite3_vtab *pVtab;
6955969616
const sqlite3_module *pModule;
6956069617
int res;
6956169618
VdbeCursor *pCur;
69562
-#endif /* local variables moved into u.co */
69619
+#endif /* local variables moved into u.cp */
6956369620
69564
- u.co.res = 0;
69565
- u.co.pCur = p->apCsr[pOp->p1];
69566
- assert( u.co.pCur->pVtabCursor );
69567
- if( u.co.pCur->nullRow ){
69621
+ u.cp.res = 0;
69622
+ u.cp.pCur = p->apCsr[pOp->p1];
69623
+ assert( u.cp.pCur->pVtabCursor );
69624
+ if( u.cp.pCur->nullRow ){
6956869625
break;
6956969626
}
69570
- u.co.pVtab = u.co.pCur->pVtabCursor->pVtab;
69571
- u.co.pModule = u.co.pVtab->pModule;
69572
- assert( u.co.pModule->xNext );
69627
+ u.cp.pVtab = u.cp.pCur->pVtabCursor->pVtab;
69628
+ u.cp.pModule = u.cp.pVtab->pModule;
69629
+ assert( u.cp.pModule->xNext );
6957369630
6957469631
/* Invoke the xNext() method of the module. There is no way for the
6957569632
** underlying implementation to return an error if one occurs during
6957669633
** xNext(). Instead, if an error occurs, true is returned (indicating that
6957769634
** data is available) and the error code returned when xColumn or
6957869635
** some other method is next invoked on the save virtual table cursor.
6957969636
*/
6958069637
p->inVtabMethod = 1;
69581
- rc = u.co.pModule->xNext(u.co.pCur->pVtabCursor);
69638
+ rc = u.cp.pModule->xNext(u.cp.pCur->pVtabCursor);
6958269639
p->inVtabMethod = 0;
69583
- importVtabErrMsg(p, u.co.pVtab);
69640
+ importVtabErrMsg(p, u.cp.pVtab);
6958469641
if( rc==SQLITE_OK ){
69585
- u.co.res = u.co.pModule->xEof(u.co.pCur->pVtabCursor);
69642
+ u.cp.res = u.cp.pModule->xEof(u.cp.pCur->pVtabCursor);
6958669643
}
6958769644
69588
- if( !u.co.res ){
69645
+ if( !u.cp.res ){
6958969646
/* If there is data, jump to P2 */
6959069647
pc = pOp->p2 - 1;
6959169648
}
6959269649
break;
6959369650
}
@@ -69599,28 +69656,28 @@
6959969656
** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
6960069657
** This opcode invokes the corresponding xRename method. The value
6960169658
** in register P1 is passed as the zName argument to the xRename method.
6960269659
*/
6960369660
case OP_VRename: {
69604
-#if 0 /* local variables moved into u.cp */
69661
+#if 0 /* local variables moved into u.cq */
6960569662
sqlite3_vtab *pVtab;
6960669663
Mem *pName;
69607
-#endif /* local variables moved into u.cp */
69608
-
69609
- u.cp.pVtab = pOp->p4.pVtab->pVtab;
69610
- u.cp.pName = &aMem[pOp->p1];
69611
- assert( u.cp.pVtab->pModule->xRename );
69612
- assert( memIsValid(u.cp.pName) );
69613
- REGISTER_TRACE(pOp->p1, u.cp.pName);
69614
- assert( u.cp.pName->flags & MEM_Str );
69615
- testcase( u.cp.pName->enc==SQLITE_UTF8 );
69616
- testcase( u.cp.pName->enc==SQLITE_UTF16BE );
69617
- testcase( u.cp.pName->enc==SQLITE_UTF16LE );
69618
- rc = sqlite3VdbeChangeEncoding(u.cp.pName, SQLITE_UTF8);
69619
- if( rc==SQLITE_OK ){
69620
- rc = u.cp.pVtab->pModule->xRename(u.cp.pVtab, u.cp.pName->z);
69621
- importVtabErrMsg(p, u.cp.pVtab);
69664
+#endif /* local variables moved into u.cq */
69665
+
69666
+ u.cq.pVtab = pOp->p4.pVtab->pVtab;
69667
+ u.cq.pName = &aMem[pOp->p1];
69668
+ assert( u.cq.pVtab->pModule->xRename );
69669
+ assert( memIsValid(u.cq.pName) );
69670
+ REGISTER_TRACE(pOp->p1, u.cq.pName);
69671
+ assert( u.cq.pName->flags & MEM_Str );
69672
+ testcase( u.cq.pName->enc==SQLITE_UTF8 );
69673
+ testcase( u.cq.pName->enc==SQLITE_UTF16BE );
69674
+ testcase( u.cq.pName->enc==SQLITE_UTF16LE );
69675
+ rc = sqlite3VdbeChangeEncoding(u.cq.pName, SQLITE_UTF8);
69676
+ if( rc==SQLITE_OK ){
69677
+ rc = u.cq.pVtab->pModule->xRename(u.cq.pVtab, u.cq.pName->z);
69678
+ importVtabErrMsg(p, u.cq.pVtab);
6962269679
p->expired = 0;
6962369680
}
6962469681
break;
6962569682
}
6962669683
#endif
@@ -69648,45 +69705,45 @@
6964869705
** P1 is a boolean flag. If it is set to true and the xUpdate call
6964969706
** is successful, then the value returned by sqlite3_last_insert_rowid()
6965069707
** is set to the value of the rowid for the row just inserted.
6965169708
*/
6965269709
case OP_VUpdate: {
69653
-#if 0 /* local variables moved into u.cq */
69710
+#if 0 /* local variables moved into u.cr */
6965469711
sqlite3_vtab *pVtab;
6965569712
sqlite3_module *pModule;
6965669713
int nArg;
6965769714
int i;
6965869715
sqlite_int64 rowid;
6965969716
Mem **apArg;
6966069717
Mem *pX;
69661
-#endif /* local variables moved into u.cq */
69718
+#endif /* local variables moved into u.cr */
6966269719
6966369720
assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6966469721
|| pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6966569722
);
69666
- u.cq.pVtab = pOp->p4.pVtab->pVtab;
69667
- u.cq.pModule = (sqlite3_module *)u.cq.pVtab->pModule;
69668
- u.cq.nArg = pOp->p2;
69723
+ u.cr.pVtab = pOp->p4.pVtab->pVtab;
69724
+ u.cr.pModule = (sqlite3_module *)u.cr.pVtab->pModule;
69725
+ u.cr.nArg = pOp->p2;
6966969726
assert( pOp->p4type==P4_VTAB );
69670
- if( ALWAYS(u.cq.pModule->xUpdate) ){
69727
+ if( ALWAYS(u.cr.pModule->xUpdate) ){
6967169728
u8 vtabOnConflict = db->vtabOnConflict;
69672
- u.cq.apArg = p->apArg;
69673
- u.cq.pX = &aMem[pOp->p3];
69674
- for(u.cq.i=0; u.cq.i<u.cq.nArg; u.cq.i++){
69675
- assert( memIsValid(u.cq.pX) );
69676
- memAboutToChange(p, u.cq.pX);
69677
- sqlite3VdbeMemStoreType(u.cq.pX);
69678
- u.cq.apArg[u.cq.i] = u.cq.pX;
69679
- u.cq.pX++;
69729
+ u.cr.apArg = p->apArg;
69730
+ u.cr.pX = &aMem[pOp->p3];
69731
+ for(u.cr.i=0; u.cr.i<u.cr.nArg; u.cr.i++){
69732
+ assert( memIsValid(u.cr.pX) );
69733
+ memAboutToChange(p, u.cr.pX);
69734
+ sqlite3VdbeMemStoreType(u.cr.pX);
69735
+ u.cr.apArg[u.cr.i] = u.cr.pX;
69736
+ u.cr.pX++;
6968069737
}
6968169738
db->vtabOnConflict = pOp->p5;
69682
- rc = u.cq.pModule->xUpdate(u.cq.pVtab, u.cq.nArg, u.cq.apArg, &u.cq.rowid);
69739
+ rc = u.cr.pModule->xUpdate(u.cr.pVtab, u.cr.nArg, u.cr.apArg, &u.cr.rowid);
6968369740
db->vtabOnConflict = vtabOnConflict;
69684
- importVtabErrMsg(p, u.cq.pVtab);
69741
+ importVtabErrMsg(p, u.cr.pVtab);
6968569742
if( rc==SQLITE_OK && pOp->p1 ){
69686
- assert( u.cq.nArg>1 && u.cq.apArg[0] && (u.cq.apArg[0]->flags&MEM_Null) );
69687
- db->lastRowid = lastRowid = u.cq.rowid;
69743
+ assert( u.cr.nArg>1 && u.cr.apArg[0] && (u.cr.apArg[0]->flags&MEM_Null) );
69744
+ db->lastRowid = lastRowid = u.cr.rowid;
6968869745
}
6968969746
if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
6969069747
if( pOp->p5==OE_Ignore ){
6969169748
rc = SQLITE_OK;
6969269749
}else{
@@ -69742,28 +69799,28 @@
6974269799
**
6974369800
** If tracing is enabled (by the sqlite3_trace()) interface, then
6974469801
** the UTF-8 string contained in P4 is emitted on the trace callback.
6974569802
*/
6974669803
case OP_Trace: {
69747
-#if 0 /* local variables moved into u.cr */
69804
+#if 0 /* local variables moved into u.cs */
6974869805
char *zTrace;
6974969806
char *z;
69750
-#endif /* local variables moved into u.cr */
69807
+#endif /* local variables moved into u.cs */
6975169808
6975269809
if( db->xTrace
6975369810
&& !p->doingRerun
69754
- && (u.cr.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
69811
+ && (u.cs.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6975569812
){
69756
- u.cr.z = sqlite3VdbeExpandSql(p, u.cr.zTrace);
69757
- db->xTrace(db->pTraceArg, u.cr.z);
69758
- sqlite3DbFree(db, u.cr.z);
69813
+ u.cs.z = sqlite3VdbeExpandSql(p, u.cs.zTrace);
69814
+ db->xTrace(db->pTraceArg, u.cs.z);
69815
+ sqlite3DbFree(db, u.cs.z);
6975969816
}
6976069817
#ifdef SQLITE_DEBUG
6976169818
if( (db->flags & SQLITE_SqlTrace)!=0
69762
- && (u.cr.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
69819
+ && (u.cs.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6976369820
){
69764
- sqlite3DebugPrintf("SQL-trace: %s\n", u.cr.zTrace);
69821
+ sqlite3DebugPrintf("SQL-trace: %s\n", u.cs.zTrace);
6976569822
}
6976669823
#endif /* SQLITE_DEBUG */
6976769824
break;
6976869825
}
6976969826
#endif
@@ -74733,27 +74790,36 @@
7473374790
return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
7473474791
}
7473574792
7473674793
/*
7473774794
** This function is used by the implementation of the IN (...) operator.
74738
-** It's job is to find or create a b-tree structure that may be used
74739
-** either to test for membership of the (...) set or to iterate through
74740
-** its members, skipping duplicates.
74795
+** The pX parameter is the expression on the RHS of the IN operator, which
74796
+** might be either a list of expressions or a subquery.
7474174797
**
74742
-** The index of the cursor opened on the b-tree (database table, database index
74743
-** or ephermal table) is stored in pX->iTable before this function returns.
74798
+** The job of this routine is to find or create a b-tree object that can
74799
+** be used either to test for membership in the RHS set or to iterate through
74800
+** all members of the RHS set, skipping duplicates.
74801
+**
74802
+** A cursor is opened on the b-tree object that the RHS of the IN operator
74803
+** and pX->iTable is set to the index of that cursor.
74804
+**
7474474805
** The returned value of this function indicates the b-tree type, as follows:
7474574806
**
7474674807
** IN_INDEX_ROWID - The cursor was opened on a database table.
7474774808
** IN_INDEX_INDEX - The cursor was opened on a database index.
7474874809
** IN_INDEX_EPH - The cursor was opened on a specially created and
7474974810
** populated epheremal table.
7475074811
**
74751
-** An existing b-tree may only be used if the SELECT is of the simple
74752
-** form:
74812
+** An existing b-tree might be used if the RHS expression pX is a simple
74813
+** subquery such as:
7475374814
**
7475474815
** SELECT <column> FROM <table>
74816
+**
74817
+** If the RHS of the IN operator is a list or a more complex subquery, then
74818
+** an ephemeral table might need to be generated from the RHS and then
74819
+** pX->iTable made to point to the ephermeral table instead of an
74820
+** existing table.
7475574821
**
7475674822
** If the prNotFound parameter is 0, then the b-tree will be used to iterate
7475774823
** through the set members, skipping any duplicates. In this case an
7475874824
** epheremal table must be used unless the selected <column> is guaranteed
7475974825
** to be unique - either because it is an INTEGER PRIMARY KEY or it
@@ -74846,12 +74912,11 @@
7484674912
7484774913
/* Check that the affinity that will be used to perform the
7484874914
** comparison is the same as the affinity of the column. If
7484974915
** it is not, it is not possible to use any index.
7485074916
*/
74851
- char aff = comparisonAffinity(pX);
74852
- int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
74917
+ int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
7485374918
7485474919
for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
7485574920
if( (pIdx->aiColumn[0]==iCol)
7485674921
&& sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
7485774922
&& (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
@@ -75371,11 +75436,11 @@
7537175436
7537275437
/* The SQLITE_ColumnCache flag disables the column cache. This is used
7537375438
** for testing only - to verify that SQLite always gets the same answer
7537475439
** with and without the column cache.
7537575440
*/
75376
- if( pParse->db->flags & SQLITE_ColumnCache ) return;
75441
+ if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
7537775442
7537875443
/* First replace any existing entry.
7537975444
**
7538075445
** Actually, the way the column cache is currently used, we are guaranteed
7538175446
** that the object will never already be in cache. Verify this guarantee.
@@ -75568,32 +75633,20 @@
7556875633
** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
7556975634
*/
7557075635
SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
7557175636
int i;
7557275637
struct yColCache *p;
75573
- if( NEVER(iFrom==iTo) ) return;
75574
- sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
75638
+ assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
75639
+ sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg-1);
7557575640
for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
7557675641
int x = p->iReg;
7557775642
if( x>=iFrom && x<iFrom+nReg ){
7557875643
p->iReg += iTo-iFrom;
7557975644
}
7558075645
}
7558175646
}
7558275647
75583
-/*
75584
-** Generate code to copy content from registers iFrom...iFrom+nReg-1
75585
-** over to iTo..iTo+nReg-1.
75586
-*/
75587
-SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
75588
- int i;
75589
- if( NEVER(iFrom==iTo) ) return;
75590
- for(i=0; i<nReg; i++){
75591
- sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
75592
- }
75593
-}
75594
-
7559575648
#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
7559675649
/*
7559775650
** Return true if any register in the range iFrom..iTo (inclusive)
7559875651
** is used as part of the column cache.
7559975652
**
@@ -76699,11 +76752,11 @@
7669976752
** precomputed into registers or if they are inserted in-line.
7670076753
*/
7670176754
SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
7670276755
Walker w;
7670376756
if( pParse->cookieGoto ) return;
76704
- if( (pParse->db->flags & SQLITE_FactorOutConst)!=0 ) return;
76757
+ if( OptimizationDisabled(pParse->db, SQLITE_FactorOutConst) ) return;
7670576758
w.xExprCallback = evalConstExpr;
7670676759
w.xSelectCallback = 0;
7670776760
w.pParse = pParse;
7670876761
sqlite3WalkExpr(&w, pExpr);
7670976762
}
@@ -85180,11 +85233,13 @@
8518085233
sqlite3ColumnDefault(v, pTab, idx, -1);
8518185234
}
8518285235
}
8518385236
if( doMakeRec ){
8518485237
const char *zAff;
85185
- if( pTab->pSelect || (pParse->db->flags & SQLITE_IdxRealAsInt)!=0 ){
85238
+ if( pTab->pSelect
85239
+ || OptimizationDisabled(pParse->db, SQLITE_IdxRealAsInt)
85240
+ ){
8518685241
zAff = 0;
8518785242
}else{
8518885243
zAff = sqlite3IndexAffinityStr(v, pIdx);
8518985244
}
8519085245
sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut);
@@ -92422,11 +92477,11 @@
9242292477
sqlite3VdbeChangeP5(v, (u8)i);
9242392478
addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
9242492479
sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
9242592480
sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
9242692481
P4_DYNAMIC);
92427
- sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
92482
+ sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
9242892483
sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
9242992484
sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
9243092485
sqlite3VdbeJumpHere(v, addr);
9243192486
9243292487
/* Make sure all the indices are constructed correctly.
@@ -92725,10 +92780,26 @@
9272592780
*/
9272692781
if( sqlite3StrICmp(zLeft, "shrink_memory")==0 ){
9272792782
sqlite3_db_release_memory(db);
9272892783
}else
9272992784
92785
+ /*
92786
+ ** PRAGMA busy_timeout
92787
+ ** PRAGMA busy_timeout = N
92788
+ **
92789
+ ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
92790
+ ** if one is set. If no busy handler or a different busy handler is set
92791
+ ** then 0 is returned. Setting the busy_timeout to 0 or negative
92792
+ ** disables the timeout.
92793
+ */
92794
+ if( sqlite3StrICmp(zLeft, "busy_timeout")==0 ){
92795
+ if( zRight ){
92796
+ sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
92797
+ }
92798
+ returnSingleInt(pParse, "timeout", db->busyTimeout);
92799
+ }else
92800
+
9273092801
#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
9273192802
/*
9273292803
** Report the current state of file logs for all databases
9273392804
*/
9273492805
if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
@@ -92955,11 +93026,13 @@
9295593026
** indicate success or failure.
9295693027
*/
9295793028
static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
9295893029
int rc;
9295993030
int i;
93031
+#ifndef SQLITE_OMIT_DEPRECATED
9296093032
int size;
93033
+#endif
9296193034
Table *pTab;
9296293035
Db *pDb;
9296393036
char const *azArg[4];
9296493037
int meta[5];
9296593038
InitData initData;
@@ -94210,10 +94283,23 @@
9421094283
return 0;
9421194284
}
9421294285
}
9421394286
#endif
9421494287
94288
+/*
94289
+** An instance of the following object is used to record information about
94290
+** how to process the DISTINCT keyword, to simplify passing that information
94291
+** into the selectInnerLoop() routine.
94292
+*/
94293
+typedef struct DistinctCtx DistinctCtx;
94294
+struct DistinctCtx {
94295
+ u8 isTnct; /* True if the DISTINCT keyword is present */
94296
+ u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
94297
+ int tabTnct; /* Ephemeral table used for DISTINCT processing */
94298
+ int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
94299
+};
94300
+
9421594301
/*
9421694302
** This routine generates the code for the inside of the inner loop
9421794303
** of a SELECT.
9421894304
**
9421994305
** If srcTab and nColumn are both zero, then the pEList expressions
@@ -94226,11 +94312,11 @@
9422694312
Select *p, /* The complete select statement being coded */
9422794313
ExprList *pEList, /* List of values being extracted */
9422894314
int srcTab, /* Pull data from this table */
9422994315
int nColumn, /* Number of columns in the source table */
9423094316
ExprList *pOrderBy, /* If not NULL, sort results using this key */
94231
- int distinct, /* If >=0, make sure results are distinct */
94317
+ DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
9423294318
SelectDest *pDest, /* How to dispose of the results */
9423394319
int iContinue, /* Jump here to continue with next row */
9423494320
int iBreak /* Jump here to break out of the inner loop */
9423594321
){
9423694322
Vdbe *v = pParse->pVdbe;
@@ -94242,11 +94328,11 @@
9424294328
int nResultCol; /* Number of result columns */
9424394329
9424494330
assert( v );
9424594331
if( NEVER(v==0) ) return;
9424694332
assert( pEList!=0 );
94247
- hasDistinct = distinct>=0;
94333
+ hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
9424894334
if( pOrderBy==0 && !hasDistinct ){
9424994335
codeOffset(v, p, iContinue);
9425094336
}
9425194337
9425294338
/* Pull the requested columns.
@@ -94282,11 +94368,59 @@
9428294368
** part of the result.
9428394369
*/
9428494370
if( hasDistinct ){
9428594371
assert( pEList!=0 );
9428694372
assert( pEList->nExpr==nColumn );
94287
- codeDistinct(pParse, distinct, iContinue, nColumn, regResult);
94373
+ switch( pDistinct->eTnctType ){
94374
+ case WHERE_DISTINCT_ORDERED: {
94375
+ VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
94376
+ int iJump; /* Jump destination */
94377
+ int regPrev; /* Previous row content */
94378
+
94379
+ /* Allocate space for the previous row */
94380
+ regPrev = pParse->nMem+1;
94381
+ pParse->nMem += nColumn;
94382
+
94383
+ /* Change the OP_OpenEphemeral coded earlier to an OP_Null
94384
+ ** sets the MEM_Cleared bit on the first register of the
94385
+ ** previous value. This will cause the OP_Ne below to always
94386
+ ** fail on the first iteration of the loop even if the first
94387
+ ** row is all NULLs.
94388
+ */
94389
+ sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
94390
+ pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
94391
+ pOp->opcode = OP_Null;
94392
+ pOp->p1 = 1;
94393
+ pOp->p2 = regPrev;
94394
+
94395
+ iJump = sqlite3VdbeCurrentAddr(v) + nColumn;
94396
+ for(i=0; i<nColumn; i++){
94397
+ CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
94398
+ if( i<nColumn-1 ){
94399
+ sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
94400
+ }else{
94401
+ sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
94402
+ }
94403
+ sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
94404
+ sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
94405
+ }
94406
+ assert( sqlite3VdbeCurrentAddr(v)==iJump );
94407
+ sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nColumn-1);
94408
+ break;
94409
+ }
94410
+
94411
+ case WHERE_DISTINCT_UNIQUE: {
94412
+ sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
94413
+ break;
94414
+ }
94415
+
94416
+ default: {
94417
+ assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
94418
+ codeDistinct(pParse, pDistinct->tabTnct, iContinue, nColumn, regResult);
94419
+ break;
94420
+ }
94421
+ }
9428894422
if( pOrderBy==0 ){
9428994423
codeOffset(v, p, iContinue);
9429094424
}
9429194425
}
9429294426
@@ -94340,20 +94474,21 @@
9434094474
** then there should be a single item on the stack. Write this
9434194475
** item into the set table with bogus data.
9434294476
*/
9434394477
case SRT_Set: {
9434494478
assert( nColumn==1 );
94345
- p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
94479
+ pDest->affSdst =
94480
+ sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
9434694481
if( pOrderBy ){
9434794482
/* At first glance you would think we could optimize out the
9434894483
** ORDER BY in this case since the order of entries in the set
9434994484
** does not matter. But there might be a LIMIT clause, in which
9435094485
** case the order does matter */
9435194486
pushOntoSorter(pParse, pOrderBy, p, regResult);
9435294487
}else{
9435394488
int r1 = sqlite3GetTempReg(pParse);
94354
- sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, 1, r1, &p->affinity, 1);
94489
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
9435594490
sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
9435694491
sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
9435794492
sqlite3ReleaseTempReg(pParse, r1);
9435894493
}
9435994494
break;
@@ -94616,11 +94751,12 @@
9461694751
break;
9461794752
}
9461894753
#ifndef SQLITE_OMIT_SUBQUERY
9461994754
case SRT_Set: {
9462094755
assert( nColumn==1 );
94621
- sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, &p->affinity, 1);
94756
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid,
94757
+ &pDest->affSdst, 1);
9462294758
sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
9462394759
sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
9462494760
break;
9462594761
}
9462694762
case SRT_Mem: {
@@ -95453,11 +95589,11 @@
9545395589
iCont = sqlite3VdbeMakeLabel(v);
9545495590
computeLimitRegisters(pParse, p, iBreak);
9545595591
sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
9545695592
iStart = sqlite3VdbeCurrentAddr(v);
9545795593
selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
95458
- 0, -1, &dest, iCont, iBreak);
95594
+ 0, 0, &dest, iCont, iBreak);
9545995595
sqlite3VdbeResolveLabel(v, iCont);
9546095596
sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
9546195597
sqlite3VdbeResolveLabel(v, iBreak);
9546295598
sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
9546395599
}
@@ -95531,11 +95667,11 @@
9553195667
r1 = sqlite3GetTempReg(pParse);
9553295668
iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
9553395669
sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
9553495670
sqlite3ReleaseTempReg(pParse, r1);
9553595671
selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
95536
- 0, -1, &dest, iCont, iBreak);
95672
+ 0, 0, &dest, iCont, iBreak);
9553795673
sqlite3VdbeResolveLabel(v, iCont);
9553895674
sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
9553995675
sqlite3VdbeResolveLabel(v, iBreak);
9554095676
sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
9554195677
sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
@@ -95651,11 +95787,11 @@
9565195787
j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
9565295788
j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
9565395789
(char*)pKeyInfo, p4type);
9565495790
sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
9565595791
sqlite3VdbeJumpHere(v, j1);
95656
- sqlite3ExprCodeCopy(pParse, pIn->iSdst, regPrev+1, pIn->nSdst);
95792
+ sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
9565795793
sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
9565895794
}
9565995795
if( pParse->db->mallocFailed ) return 0;
9566095796
9566195797
/* Suppress the first OFFSET entries if there is an OFFSET clause
@@ -95686,14 +95822,14 @@
9568695822
** item into the set table with bogus data.
9568795823
*/
9568895824
case SRT_Set: {
9568995825
int r1;
9569095826
assert( pIn->nSdst==1 );
95691
- p->affinity =
95827
+ pDest->affSdst =
9569295828
sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
9569395829
r1 = sqlite3GetTempReg(pParse);
95694
- sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &p->affinity, 1);
95830
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
9569595831
sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
9569695832
sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
9569795833
sqlite3ReleaseTempReg(pParse, r1);
9569895834
break;
9569995835
}
@@ -96431,11 +96567,11 @@
9643196567
9643296568
/* Check to see if flattening is permitted. Return 0 if not.
9643396569
*/
9643496570
assert( p!=0 );
9643596571
assert( p->pPrior==0 ); /* Unable to flatten compound queries */
96436
- if( db->flags & SQLITE_QueryFlattener ) return 0;
96572
+ if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
9643796573
pSrc = p->pSrc;
9643896574
assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
9643996575
pSubitem = &pSrc->a[iFrom];
9644096576
iParent = pSubitem->iCursor;
9644196577
pSub = pSubitem->pSelect;
@@ -97471,15 +97607,13 @@
9747197607
SrcList *pTabList; /* List of tables to select from */
9747297608
Expr *pWhere; /* The WHERE clause. May be NULL */
9747397609
ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
9747497610
ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
9747597611
Expr *pHaving; /* The HAVING clause. May be NULL */
97476
- int isDistinct; /* True if the DISTINCT keyword is present */
97477
- int distinct; /* Table to use for the distinct set */
9747897612
int rc = 1; /* Value to return from this function */
9747997613
int addrSortIndex; /* Address of an OP_OpenEphemeral instruction */
97480
- int addrDistinctIndex; /* Address of an OP_OpenEphemeral instruction */
97614
+ DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
9748197615
AggInfo sAggInfo; /* Information used by aggregate queries */
9748297616
int iEnd; /* Address of the end of the query */
9748397617
sqlite3 *db; /* The database connection */
9748497618
9748597619
#ifndef SQLITE_OMIT_EXPLAIN
@@ -97601,11 +97735,11 @@
9760197735
pEList = p->pEList;
9760297736
#endif
9760397737
pWhere = p->pWhere;
9760497738
pGroupBy = p->pGroupBy;
9760597739
pHaving = p->pHaving;
97606
- isDistinct = (p->selFlags & SF_Distinct)!=0;
97740
+ sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
9760797741
9760897742
#ifndef SQLITE_OMIT_COMPOUND_SELECT
9760997743
/* If there is are a sequence of queries, do the earlier ones first.
9761097744
*/
9761197745
if( p->pPrior ){
@@ -97636,11 +97770,11 @@
9763697770
** an optimization - the correct answer should result regardless.
9763797771
** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
9763897772
** to disable this optimization for testing purposes.
9763997773
*/
9764097774
if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
97641
- && (db->flags & SQLITE_GroupByOrder)==0 ){
97775
+ && OptimizationEnabled(db, SQLITE_GroupByOrder) ){
9764297776
pOrderBy = 0;
9764397777
}
9764497778
9764597779
/* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
9764697780
** if the select-list is the same as the ORDER BY list, then this query
@@ -97662,10 +97796,14 @@
9766297796
){
9766397797
p->selFlags &= ~SF_Distinct;
9766497798
p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
9766597799
pGroupBy = p->pGroupBy;
9766697800
pOrderBy = 0;
97801
+ /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
97802
+ ** the sDistinct.isTnct is still set. Hence, isTnct represents the
97803
+ ** original setting of the SF_Distinct flag, not the current setting */
97804
+ assert( sDistinct.isTnct );
9766797805
}
9766897806
9766997807
/* If there is an ORDER BY clause, then this sorting
9767097808
** index might end up being unused if the data can be
9767197809
** extracted in pre-sorted order. If that is the case, then the
@@ -97702,28 +97840,31 @@
9770297840
}
9770397841
9770497842
/* Open a virtual index to use for the distinct set.
9770597843
*/
9770697844
if( p->selFlags & SF_Distinct ){
97707
- KeyInfo *pKeyInfo;
97708
- distinct = pParse->nTab++;
97709
- pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
97710
- addrDistinctIndex = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0,
97711
- (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
97845
+ sDistinct.tabTnct = pParse->nTab++;
97846
+ sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
97847
+ sDistinct.tabTnct, 0, 0,
97848
+ (char*)keyInfoFromExprList(pParse, p->pEList),
97849
+ P4_KEYINFO_HANDOFF);
9771297850
sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
97851
+ sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
9771397852
}else{
97714
- distinct = addrDistinctIndex = -1;
97853
+ sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
9771597854
}
9771697855
97717
- /* Aggregate and non-aggregate queries are handled differently */
9771897856
if( !isAgg && pGroupBy==0 ){
97719
- ExprList *pDist = (isDistinct ? p->pEList : 0);
97857
+ /* No aggregate functions and no GROUP BY clause */
97858
+ ExprList *pDist = (sDistinct.isTnct ? p->pEList : 0);
9772097859
9772197860
/* Begin the database scan. */
97722
- pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, pDist, 0,0);
97861
+ pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, pDist, 0,0);
9772397862
if( pWInfo==0 ) goto select_end;
9772497863
if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
97864
+ if( pWInfo->eDistinct ) sDistinct.eTnctType = pWInfo->eDistinct;
97865
+ if( pOrderBy && pWInfo->nOBSat==pOrderBy->nExpr ) pOrderBy = 0;
9772597866
9772697867
/* If sorting index that was created by a prior OP_OpenEphemeral
9772797868
** instruction ended up not being needed, then change the OP_OpenEphemeral
9772897869
** into an OP_Noop.
9772997870
*/
@@ -97730,63 +97871,20 @@
9773097871
if( addrSortIndex>=0 && pOrderBy==0 ){
9773197872
sqlite3VdbeChangeToNoop(v, addrSortIndex);
9773297873
p->addrOpenEphm[2] = -1;
9773397874
}
9773497875
97735
- if( pWInfo->eDistinct ){
97736
- VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
97737
-
97738
- assert( addrDistinctIndex>=0 );
97739
- pOp = sqlite3VdbeGetOp(v, addrDistinctIndex);
97740
-
97741
- assert( isDistinct );
97742
- assert( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
97743
- || pWInfo->eDistinct==WHERE_DISTINCT_UNIQUE
97744
- );
97745
- distinct = -1;
97746
- if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED ){
97747
- int iJump;
97748
- int iExpr;
97749
- int iFlag = ++pParse->nMem;
97750
- int iBase = pParse->nMem+1;
97751
- int iBase2 = iBase + pEList->nExpr;
97752
- pParse->nMem += (pEList->nExpr*2);
97753
-
97754
- /* Change the OP_OpenEphemeral coded earlier to an OP_Integer. The
97755
- ** OP_Integer initializes the "first row" flag. */
97756
- pOp->opcode = OP_Integer;
97757
- pOp->p1 = 1;
97758
- pOp->p2 = iFlag;
97759
-
97760
- sqlite3ExprCodeExprList(pParse, pEList, iBase, 1);
97761
- iJump = sqlite3VdbeCurrentAddr(v) + 1 + pEList->nExpr + 1 + 1;
97762
- sqlite3VdbeAddOp2(v, OP_If, iFlag, iJump-1);
97763
- for(iExpr=0; iExpr<pEList->nExpr; iExpr++){
97764
- CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[iExpr].pExpr);
97765
- sqlite3VdbeAddOp3(v, OP_Ne, iBase+iExpr, iJump, iBase2+iExpr);
97766
- sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
97767
- sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
97768
- }
97769
- sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iContinue);
97770
-
97771
- sqlite3VdbeAddOp2(v, OP_Integer, 0, iFlag);
97772
- assert( sqlite3VdbeCurrentAddr(v)==iJump );
97773
- sqlite3VdbeAddOp3(v, OP_Move, iBase, iBase2, pEList->nExpr);
97774
- }else{
97775
- pOp->opcode = OP_Noop;
97776
- }
97777
- }
97778
-
9777997876
/* Use the standard inner loop. */
97780
- selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, pDest,
97877
+ selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, &sDistinct, pDest,
9778197878
pWInfo->iContinue, pWInfo->iBreak);
9778297879
9778397880
/* End the database scan loop.
9778497881
*/
9778597882
sqlite3WhereEnd(pWInfo);
9778697883
}else{
97787
- /* This is the processing for aggregate queries */
97884
+ /* This case when there exist aggregate functions or a GROUP BY clause
97885
+ ** or both */
9778897886
NameContext sNC; /* Name context for processing aggregate information */
9778997887
int iAMem; /* First Mem address for storing current GROUP BY */
9779097888
int iBMem; /* First Mem address for previous GROUP BY */
9779197889
int iUseFlag; /* Mem address holding flag indicating that at least
9779297890
** one row of the input to the aggregator has been
@@ -97890,18 +97988,17 @@
9789097988
** This might involve two separate loops with an OP_Sort in between, or
9789197989
** it might be a single loop that uses an index to extract information
9789297990
** in the right order to begin with.
9789397991
*/
9789497992
sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
97895
- pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0, 0, 0);
97993
+ pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 0, 0);
9789697994
if( pWInfo==0 ) goto select_end;
97897
- if( pGroupBy==0 ){
97995
+ if( pWInfo->nOBSat==pGroupBy->nExpr ){
9789897996
/* The optimizer is able to deliver rows in group by order so
9789997997
** we do not have to sort. The OP_OpenEphemeral table will be
9790097998
** cancelled later because we still need to use the pKeyInfo
9790197999
*/
97902
- pGroupBy = p->pGroupBy;
9790398000
groupBySort = 0;
9790498001
}else{
9790598002
/* Rows are coming out in undetermined order. We have to push
9790698003
** each row into a sorting index, terminate the first loop,
9790798004
** then loop over the sorting index in order to get the output
@@ -97911,11 +98008,12 @@
9791198008
int regRecord;
9791298009
int nCol;
9791398010
int nGroupBy;
9791498011
9791598012
explainTempTable(pParse,
97916
- isDistinct && !(p->selFlags&SF_Distinct)?"DISTINCT":"GROUP BY");
98013
+ (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
98014
+ "DISTINCT" : "GROUP BY");
9791798015
9791898016
groupBySort = 1;
9791998017
nGroupBy = pGroupBy->nExpr;
9792098018
nCol = nGroupBy + 1;
9792198019
j = nGroupBy+1;
@@ -98043,11 +98141,11 @@
9804398141
VdbeComment((v, "Groupby result generator entry point"));
9804498142
sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
9804598143
finalizeAggFunctions(pParse, &sAggInfo);
9804698144
sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
9804798145
selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
98048
- distinct, pDest,
98146
+ &sDistinct, pDest,
9804998147
addrOutputRow+1, addrSetAbort);
9805098148
sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
9805198149
VdbeComment((v, "end groupby result generator"));
9805298150
9805398151
/* Generate a subroutine that will reset the group-by accumulator
@@ -98146,10 +98244,11 @@
9814698244
*/
9814798245
ExprList *pMinMax = 0;
9814898246
u8 flag = minMaxQuery(p);
9814998247
if( flag ){
9815098248
assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
98249
+ assert( p->pEList->a[0].pExpr->x.pList->nExpr==1 );
9815198250
pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
9815298251
pDel = pMinMax;
9815398252
if( pMinMax && !db->mallocFailed ){
9815498253
pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
9815598254
pMinMax->a[0].pExpr->op = TK_COLUMN;
@@ -98159,17 +98258,18 @@
9815998258
/* This case runs if the aggregate has no GROUP BY clause. The
9816098259
** processing is much simpler since there is only a single row
9816198260
** of output.
9816298261
*/
9816398262
resetAccumulator(pParse, &sAggInfo);
98164
- pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax,0,flag,0);
98263
+ pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
9816598264
if( pWInfo==0 ){
9816698265
sqlite3ExprListDelete(db, pDel);
9816798266
goto select_end;
9816898267
}
9816998268
updateAccumulator(pParse, &sAggInfo);
98170
- if( !pMinMax && flag ){
98269
+ assert( pMinMax==0 || pMinMax->nExpr==1 );
98270
+ if( pWInfo->nOBSat>0 ){
9817198271
sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
9817298272
VdbeComment((v, "%s() by index",
9817398273
(flag==WHERE_ORDERBY_MIN?"min":"max")));
9817498274
}
9817598275
sqlite3WhereEnd(pWInfo);
@@ -98176,19 +98276,19 @@
9817698276
finalizeAggFunctions(pParse, &sAggInfo);
9817798277
}
9817898278
9817998279
pOrderBy = 0;
9818098280
sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
98181
- selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1,
98281
+ selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, 0,
9818298282
pDest, addrEnd, addrEnd);
9818398283
sqlite3ExprListDelete(db, pDel);
9818498284
}
9818598285
sqlite3VdbeResolveLabel(v, addrEnd);
9818698286
9818798287
} /* endif aggregate query */
9818898288
98189
- if( distinct>=0 ){
98289
+ if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
9819098290
explainTempTable(pParse, "DISTINCT");
9819198291
}
9819298292
9819398293
/* If there is an ORDER BY clause, then we need to sort the results
9819498294
** and send them to the callback one by one.
@@ -101789,13 +101889,14 @@
101789101889
101790101890
/*
101791101891
** Trace output macros
101792101892
*/
101793101893
#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
101794
-SQLITE_PRIVATE int sqlite3WhereTrace = 0;
101894
+/***/ int sqlite3WhereTrace = 0;
101795101895
#endif
101796
-#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
101896
+#if defined(SQLITE_DEBUG) \
101897
+ && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
101797101898
# define WHERETRACE(X) if(sqlite3WhereTrace) sqlite3DebugPrintf X
101798101899
#else
101799101900
# define WHERETRACE(X)
101800101901
#endif
101801101902
@@ -102031,10 +102132,32 @@
102031102132
#define WHERE_VIRTUALTABLE 0x08000000 /* Use virtual-table processing */
102032102133
#define WHERE_MULTI_OR 0x10000000 /* OR using multiple indices */
102033102134
#define WHERE_TEMP_INDEX 0x20000000 /* Uses an ephemeral index */
102034102135
#define WHERE_DISTINCT 0x40000000 /* Correct order for DISTINCT */
102035102136
#define WHERE_COVER_SCAN 0x80000000 /* Full scan of a covering index */
102137
+
102138
+/*
102139
+** This module contains many separate subroutines that work together to
102140
+** find the best indices to use for accessing a particular table in a query.
102141
+** An instance of the following structure holds context information about the
102142
+** index search so that it can be more easily passed between the various
102143
+** routines.
102144
+*/
102145
+typedef struct WhereBestIdx WhereBestIdx;
102146
+struct WhereBestIdx {
102147
+ Parse *pParse; /* Parser context */
102148
+ WhereClause *pWC; /* The WHERE clause */
102149
+ struct SrcList_item *pSrc; /* The FROM clause term to search */
102150
+ Bitmask notReady; /* Mask of cursors not available */
102151
+ Bitmask notValid; /* Cursors not available for any purpose */
102152
+ ExprList *pOrderBy; /* The ORDER BY clause */
102153
+ ExprList *pDistinct; /* The select-list if query is DISTINCT */
102154
+ sqlite3_index_info **ppIdxInfo; /* Index information passed to xBestIndex */
102155
+ int i, n; /* Which loop is being coded; # of loops */
102156
+ WhereLevel *aLevel; /* Info about outer loops */
102157
+ WhereCost cost; /* Lowest cost query plan */
102158
+};
102036102159
102037102160
/*
102038102161
** Initialize a preallocated WhereClause structure.
102039102162
*/
102040102163
static void whereClauseInit(
@@ -103174,26 +103297,22 @@
103174103297
*/
103175103298
pTerm->prereqRight |= extraRight;
103176103299
}
103177103300
103178103301
/*
103179
-** Return TRUE if any of the expressions in pList->a[iFirst...] contain
103180
-** a reference to any table other than the iBase table.
103302
+** Return TRUE if the given index is UNIQUE and all columns past the
103303
+** first nSkip columns are NOT NULL.
103181103304
*/
103182
-static int referencesOtherTables(
103183
- ExprList *pList, /* Search expressions in ths list */
103184
- WhereMaskSet *pMaskSet, /* Mapping from tables to bitmaps */
103185
- int iFirst, /* Be searching with the iFirst-th expression */
103186
- int iBase /* Ignore references to this table */
103187
-){
103188
- Bitmask allowed = ~getMask(pMaskSet, iBase);
103189
- while( iFirst<pList->nExpr ){
103190
- if( (exprTableUsage(pMaskSet, pList->a[iFirst++].pExpr)&allowed)!=0 ){
103191
- return 1;
103192
- }
103193
- }
103194
- return 0;
103305
+static int indexIsUniqueNotNull(Index *pIdx, int nSkip){
103306
+ Table *pTab = pIdx->pTable;
103307
+ int i;
103308
+ if( pIdx->onError==OE_None ) return 0;
103309
+ for(i=nSkip; i<pIdx->nColumn; i++){
103310
+ int j = pIdx->aiColumn[i];
103311
+ if( j>=0 && pTab->aCol[j].notNull==0 ) return 0;
103312
+ }
103313
+ return 1;
103195103314
}
103196103315
103197103316
/*
103198103317
** This function searches the expression list passed as the second argument
103199103318
** for an expression of type TK_COLUMN that refers to the same column and
@@ -103355,47 +103474,63 @@
103355103474
return 0;
103356103475
}
103357103476
103358103477
/*
103359103478
** This routine decides if pIdx can be used to satisfy the ORDER BY
103360
-** clause. If it can, it returns 1. If pIdx cannot satisfy the
103361
-** ORDER BY clause, this routine returns 0.
103479
+** clause, either in whole or in part. The return value is the
103480
+** cumulative number of terms in the ORDER BY clause that are satisfied
103481
+** by the index pIdx and other indices in outer loops.
103362103482
**
103363
-** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
103364
-** left-most table in the FROM clause of that same SELECT statement and
103365
-** the table has a cursor number of "base". pIdx is an index on pTab.
103483
+** The table being queried has a cursor number of "base". pIdx is the
103484
+** index that is postulated for use to access the table.
103366103485
**
103367103486
** nEqCol is the number of columns of pIdx that are used as equality
103368
-** constraints. Any of these columns may be missing from the ORDER BY
103369
-** clause and the match can still be a success.
103487
+** constraints and where the other side of the == is an ordered column
103488
+** or constant. An "order column" in the previous sentence means a column
103489
+** in table from an outer loop whose values will always appear in the
103490
+** correct order due to othre index, or because the outer loop generates
103491
+** a unique result. Any of the first nEqCol columns of pIdx may be missing
103492
+** from the ORDER BY clause and the match can still be a success.
103370103493
**
103371
-** All terms of the ORDER BY that match against the index must be either
103372
-** ASC or DESC. (Terms of the ORDER BY clause past the end of a UNIQUE
103373
-** index do not need to satisfy this constraint.) The *pbRev value is
103374
-** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
103375
-** the ORDER BY clause is all ASC.
103494
+** The *pbRev value is set to 0 order 1 depending on whether or not
103495
+** pIdx should be run in the forward order or in reverse order.
103376103496
*/
103377103497
static int isSortingIndex(
103378
- Parse *pParse, /* Parsing context */
103379
- WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmaps */
103380
- Index *pIdx, /* The index we are testing */
103381
- int base, /* Cursor number for the table to be sorted */
103382
- ExprList *pOrderBy, /* The ORDER BY clause */
103383
- int nEqCol, /* Number of index columns with == constraints */
103384
- int wsFlags, /* Index usages flags */
103385
- int *pbRev /* Set to 1 if ORDER BY is DESC */
103498
+ WhereBestIdx *p, /* Best index search context */
103499
+ Index *pIdx, /* The index we are testing */
103500
+ int base, /* Cursor number for the table to be sorted */
103501
+ int nEqCol, /* Number of index columns with ordered == constraints */
103502
+ int wsFlags, /* Index usages flags */
103503
+ int bOuterRev, /* True if outer loops scan in reverse order */
103504
+ int *pbRev /* Set to 1 for reverse-order scan of pIdx */
103386103505
){
103387
- int i, j; /* Loop counters */
103388
- int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
103389
- int nTerm; /* Number of ORDER BY terms */
103390
- struct ExprList_item *pTerm; /* A term of the ORDER BY clause */
103391
- sqlite3 *db = pParse->db;
103392
-
103393
- if( !pOrderBy ) return 0;
103394
- if( wsFlags & WHERE_COLUMN_IN ) return 0;
103395
- if( pIdx->bUnordered ) return 0;
103396
-
103506
+ int i; /* Number of pIdx terms used */
103507
+ int j; /* Number of ORDER BY terms satisfied */
103508
+ int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
103509
+ int nTerm; /* Number of ORDER BY terms */
103510
+ struct ExprList_item *pTerm; /* A term of the ORDER BY clause */
103511
+ ExprList *pOrderBy; /* The ORDER BY clause */
103512
+ Parse *pParse = p->pParse; /* Parser context */
103513
+ sqlite3 *db = pParse->db; /* Database connection */
103514
+ int nPriorSat; /* ORDER BY terms satisfied by outer loops */
103515
+ int seenRowid = 0; /* True if an ORDER BY rowid term is seen */
103516
+ int nEqOneRow; /* Idx columns that ref unique values */
103517
+
103518
+ if( p->i==0 ){
103519
+ nPriorSat = 0;
103520
+ nEqOneRow = nEqCol;
103521
+ }else{
103522
+ if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
103523
+ nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
103524
+ sortOrder = bOuterRev;
103525
+ nEqOneRow = 0;
103526
+ }
103527
+ if( p->i>0 && nEqCol==0 /*&& !allOuterLoopsUnique(p)*/ ) return nPriorSat;
103528
+ pOrderBy = p->pOrderBy;
103529
+ if( !pOrderBy ) return nPriorSat;
103530
+ if( wsFlags & WHERE_COLUMN_IN ) return nPriorSat;
103531
+ if( pIdx->bUnordered ) return nPriorSat;
103397103532
nTerm = pOrderBy->nExpr;
103398103533
assert( nTerm>0 );
103399103534
103400103535
/* Argument pIdx must either point to a 'real' named index structure,
103401103536
** or an index structure allocated on the stack by bestBtreeIndex() to
@@ -103408,11 +103543,11 @@
103408103543
** Note that indices have pIdx->nColumn regular columns plus
103409103544
** one additional column containing the rowid. The rowid column
103410103545
** of the index is also allowed to match against the ORDER BY
103411103546
** clause.
103412103547
*/
103413
- for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<=pIdx->nColumn; i++){
103548
+ for(i=0,j=nPriorSat,pTerm=&pOrderBy->a[j]; j<nTerm && i<=pIdx->nColumn; i++){
103414103549
Expr *pExpr; /* The expression of the ORDER BY pTerm */
103415103550
CollSeq *pColl; /* The collating sequence of pExpr */
103416103551
int termSortOrder; /* Sort order for this term */
103417103552
int iColumn; /* The i-th column of the index. -1 for rowid */
103418103553
int iSortOrder; /* 1 for DESC, 0 for ASC on the i-th index term */
@@ -103452,68 +103587,53 @@
103452103587
break;
103453103588
}else{
103454103589
/* If an index column fails to match and is not constrained by ==
103455103590
** then the index cannot satisfy the ORDER BY constraint.
103456103591
*/
103457
- return 0;
103592
+ return nPriorSat;
103458103593
}
103459103594
}
103460103595
assert( pIdx->aSortOrder!=0 || iColumn==-1 );
103461103596
assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
103462103597
assert( iSortOrder==0 || iSortOrder==1 );
103463103598
termSortOrder = iSortOrder ^ pTerm->sortOrder;
103464
- if( i>nEqCol ){
103599
+ if( i>nEqOneRow ){
103465103600
if( termSortOrder!=sortOrder ){
103466103601
/* Indices can only be used if all ORDER BY terms past the
103467103602
** equality constraints are all either DESC or ASC. */
103468
- return 0;
103603
+ break;
103469103604
}
103470103605
}else{
103471103606
sortOrder = termSortOrder;
103472103607
}
103473103608
j++;
103474103609
pTerm++;
103475
- if( iColumn<0 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
103476
- /* If the indexed column is the primary key and everything matches
103477
- ** so far and none of the ORDER BY terms to the right reference other
103478
- ** tables in the join, then we are assured that the index can be used
103479
- ** to sort because the primary key is unique and so none of the other
103480
- ** columns will make any difference
103481
- */
103482
- j = nTerm;
103483
- }
103484
- }
103485
-
103486
- *pbRev = sortOrder!=0;
103487
- if( j>=nTerm ){
103488
- /* All terms of the ORDER BY clause are covered by this index so
103489
- ** this index can be used for sorting. */
103490
- return 1;
103491
- }
103492
- if( pIdx->onError!=OE_None && i==pIdx->nColumn
103493
- && (wsFlags & WHERE_COLUMN_NULL)==0
103494
- && !referencesOtherTables(pOrderBy, pMaskSet, j, base)
103610
+ if( iColumn<0 ){
103611
+ seenRowid = 1;
103612
+ break;
103613
+ }
103614
+ }
103615
+ *pbRev = sortOrder;
103616
+
103617
+ /* If there was an "ORDER BY rowid" term that matched, or it is only
103618
+ ** possible for a single row from this table to match, then skip over
103619
+ ** any additional ORDER BY terms dealing with this table.
103620
+ */
103621
+ if( seenRowid ||
103622
+ ( (wsFlags & WHERE_COLUMN_NULL)==0
103623
+ && i>=pIdx->nColumn
103624
+ && indexIsUniqueNotNull(pIdx, nEqCol)
103625
+ )
103495103626
){
103496
- Column *aCol = pIdx->pTable->aCol;
103497
-
103498
- /* All terms of this index match some prefix of the ORDER BY clause,
103499
- ** the index is UNIQUE, and no terms on the tail of the ORDER BY
103500
- ** refer to other tables in a join. So, assuming that the index entries
103501
- ** visited contain no NULL values, then this index delivers rows in
103502
- ** the required order.
103503
- **
103504
- ** It is not possible for any of the first nEqCol index fields to be
103505
- ** NULL (since the corresponding "=" operator in the WHERE clause would
103506
- ** not be true). So if all remaining index columns have NOT NULL
103507
- ** constaints attached to them, we can be confident that the visited
103508
- ** index entries are free of NULLs. */
103509
- for(i=nEqCol; i<pIdx->nColumn; i++){
103510
- if( aCol[pIdx->aiColumn[i]].notNull==0 ) break;
103511
- }
103512
- return (i==pIdx->nColumn);
103513
- }
103514
- return 0;
103627
+ /* Advance j over additional ORDER BY terms associated with base */
103628
+ WhereMaskSet *pMS = p->pWC->pMaskSet;
103629
+ Bitmask m = ~getMask(pMS, base);
103630
+ while( j<nTerm && (exprTableUsage(pMS, pOrderBy->a[j].pExpr)&m)==0 ){
103631
+ j++;
103632
+ }
103633
+ }
103634
+ return j;
103515103635
}
103516103636
103517103637
/*
103518103638
** Prepare a crude estimate of the logarithm of the input value.
103519103639
** The results need not be exact. This is only used for estimating
@@ -103576,35 +103696,27 @@
103576103696
#endif
103577103697
103578103698
/*
103579103699
** Required because bestIndex() is called by bestOrClauseIndex()
103580103700
*/
103581
-static void bestIndex(
103582
- Parse*, WhereClause*, struct SrcList_item*,
103583
- Bitmask, Bitmask, ExprList*, WhereCost*);
103701
+static void bestIndex(WhereBestIdx*);
103584103702
103585103703
/*
103586103704
** This routine attempts to find an scanning strategy that can be used
103587103705
** to optimize an 'OR' expression that is part of a WHERE clause.
103588103706
**
103589103707
** The table associated with FROM clause term pSrc may be either a
103590103708
** regular B-Tree table or a virtual table.
103591103709
*/
103592
-static void bestOrClauseIndex(
103593
- Parse *pParse, /* The parsing context */
103594
- WhereClause *pWC, /* The WHERE clause */
103595
- struct SrcList_item *pSrc, /* The FROM clause term to search */
103596
- Bitmask notReady, /* Mask of cursors not available for indexing */
103597
- Bitmask notValid, /* Cursors not available for any purpose */
103598
- ExprList *pOrderBy, /* The ORDER BY clause */
103599
- WhereCost *pCost /* Lowest cost query plan */
103600
-){
103710
+static void bestOrClauseIndex(WhereBestIdx *p){
103601103711
#ifndef SQLITE_OMIT_OR_OPTIMIZATION
103602
- const int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
103712
+ WhereClause *pWC = p->pWC; /* The WHERE clause */
103713
+ struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
103714
+ const int iCur = pSrc->iCursor; /* The cursor of the table */
103603103715
const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur); /* Bitmask for pSrc */
103604103716
WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm]; /* End of pWC->a[] */
103605
- WhereTerm *pTerm; /* A single term of the WHERE clause */
103717
+ WhereTerm *pTerm; /* A single term of the WHERE clause */
103606103718
103607103719
/* The OR-clause optimization is disallowed if the INDEXED BY or
103608103720
** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
103609103721
if( pSrc->notIndexed || pSrc->pIndex!=0 ){
103610103722
return;
@@ -103614,66 +103726,71 @@
103614103726
}
103615103727
103616103728
/* Search the WHERE clause terms for a usable WO_OR term. */
103617103729
for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
103618103730
if( pTerm->eOperator==WO_OR
103619
- && ((pTerm->prereqAll & ~maskSrc) & notReady)==0
103731
+ && ((pTerm->prereqAll & ~maskSrc) & p->notReady)==0
103620103732
&& (pTerm->u.pOrInfo->indexable & maskSrc)!=0
103621103733
){
103622103734
WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
103623103735
WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
103624103736
WhereTerm *pOrTerm;
103625103737
int flags = WHERE_MULTI_OR;
103626103738
double rTotal = 0;
103627103739
double nRow = 0;
103628103740
Bitmask used = 0;
103741
+ WhereBestIdx sBOI;
103629103742
103743
+ sBOI = *p;
103744
+ sBOI.pOrderBy = 0;
103745
+ sBOI.pDistinct = 0;
103746
+ sBOI.ppIdxInfo = 0;
103630103747
for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
103631
- WhereCost sTermCost;
103632103748
WHERETRACE(("... Multi-index OR testing for term %d of %d....\n",
103633103749
(pOrTerm - pOrWC->a), (pTerm - pWC->a)
103634103750
));
103635103751
if( pOrTerm->eOperator==WO_AND ){
103636
- WhereClause *pAndWC = &pOrTerm->u.pAndInfo->wc;
103637
- bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
103752
+ sBOI.pWC = &pOrTerm->u.pAndInfo->wc;
103753
+ bestIndex(&sBOI);
103638103754
}else if( pOrTerm->leftCursor==iCur ){
103639103755
WhereClause tempWC;
103640103756
tempWC.pParse = pWC->pParse;
103641103757
tempWC.pMaskSet = pWC->pMaskSet;
103642103758
tempWC.pOuter = pWC;
103643103759
tempWC.op = TK_AND;
103644103760
tempWC.a = pOrTerm;
103645103761
tempWC.wctrlFlags = 0;
103646103762
tempWC.nTerm = 1;
103647
- bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
103763
+ sBOI.pWC = &tempWC;
103764
+ bestIndex(&sBOI);
103648103765
}else{
103649103766
continue;
103650103767
}
103651
- rTotal += sTermCost.rCost;
103652
- nRow += sTermCost.plan.nRow;
103653
- used |= sTermCost.used;
103654
- if( rTotal>=pCost->rCost ) break;
103768
+ rTotal += sBOI.cost.rCost;
103769
+ nRow += sBOI.cost.plan.nRow;
103770
+ used |= sBOI.cost.used;
103771
+ if( rTotal>=p->cost.rCost ) break;
103655103772
}
103656103773
103657103774
/* If there is an ORDER BY clause, increase the scan cost to account
103658103775
** for the cost of the sort. */
103659
- if( pOrderBy!=0 ){
103776
+ if( p->pOrderBy!=0 ){
103660103777
WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
103661103778
rTotal, rTotal+nRow*estLog(nRow)));
103662103779
rTotal += nRow*estLog(nRow);
103663103780
}
103664103781
103665103782
/* If the cost of scanning using this OR term for optimization is
103666103783
** less than the current cost stored in pCost, replace the contents
103667103784
** of pCost. */
103668103785
WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
103669
- if( rTotal<pCost->rCost ){
103670
- pCost->rCost = rTotal;
103671
- pCost->used = used;
103672
- pCost->plan.nRow = nRow;
103673
- pCost->plan.wsFlags = flags;
103674
- pCost->plan.u.pTerm = pTerm;
103786
+ if( rTotal<p->cost.rCost ){
103787
+ p->cost.rCost = rTotal;
103788
+ p->cost.used = used;
103789
+ p->cost.plan.nRow = nRow;
103790
+ p->cost.plan.wsFlags = flags;
103791
+ p->cost.plan.u.pTerm = pTerm;
103675103792
}
103676103793
}
103677103794
}
103678103795
#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
103679103796
}
@@ -103706,19 +103823,16 @@
103706103823
** possible to construct a transient index that would perform better
103707103824
** than a full table scan even when the cost of constructing the index
103708103825
** is taken into account, then alter the query plan to use the
103709103826
** transient index.
103710103827
*/
103711
-static void bestAutomaticIndex(
103712
- Parse *pParse, /* The parsing context */
103713
- WhereClause *pWC, /* The WHERE clause */
103714
- struct SrcList_item *pSrc, /* The FROM clause term to search */
103715
- Bitmask notReady, /* Mask of cursors that are not available */
103716
- WhereCost *pCost /* Lowest cost query plan */
103717
-){
103718
- double nTableRow; /* Rows in the input table */
103719
- double logN; /* log(nTableRow) */
103828
+static void bestAutomaticIndex(WhereBestIdx *p){
103829
+ Parse *pParse = p->pParse; /* The parsing context */
103830
+ WhereClause *pWC = p->pWC; /* The WHERE clause */
103831
+ struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
103832
+ double nTableRow; /* Rows in the input table */
103833
+ double logN; /* log(nTableRow) */
103720103834
double costTempIdx; /* per-query cost of the transient index */
103721103835
WhereTerm *pTerm; /* A single term of the WHERE clause */
103722103836
WhereTerm *pWCEnd; /* End of pWC->a[] */
103723103837
Table *pTable; /* Table tht might be indexed */
103724103838
@@ -103728,11 +103842,11 @@
103728103842
}
103729103843
if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){
103730103844
/* Automatic indices are disabled at run-time */
103731103845
return;
103732103846
}
103733
- if( (pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){
103847
+ if( (p->cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){
103734103848
/* We already have some kind of index in use for this query. */
103735103849
return;
103736103850
}
103737103851
if( pSrc->notIndexed ){
103738103852
/* The NOT INDEXED clause appears in the SQL. */
@@ -103746,32 +103860,32 @@
103746103860
assert( pParse->nQueryLoop >= (double)1 );
103747103861
pTable = pSrc->pTab;
103748103862
nTableRow = pTable->nRowEst;
103749103863
logN = estLog(nTableRow);
103750103864
costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
103751
- if( costTempIdx>=pCost->rCost ){
103865
+ if( costTempIdx>=p->cost.rCost ){
103752103866
/* The cost of creating the transient table would be greater than
103753103867
** doing the full table scan */
103754103868
return;
103755103869
}
103756103870
103757103871
/* Search for any equality comparison term */
103758103872
pWCEnd = &pWC->a[pWC->nTerm];
103759103873
for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
103760
- if( termCanDriveIndex(pTerm, pSrc, notReady) ){
103874
+ if( termCanDriveIndex(pTerm, pSrc, p->notReady) ){
103761103875
WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
103762
- pCost->rCost, costTempIdx));
103763
- pCost->rCost = costTempIdx;
103764
- pCost->plan.nRow = logN + 1;
103765
- pCost->plan.wsFlags = WHERE_TEMP_INDEX;
103766
- pCost->used = pTerm->prereqRight;
103876
+ p->cost.rCost, costTempIdx));
103877
+ p->cost.rCost = costTempIdx;
103878
+ p->cost.plan.nRow = logN + 1;
103879
+ p->cost.plan.wsFlags = WHERE_TEMP_INDEX;
103880
+ p->cost.used = pTerm->prereqRight;
103767103881
break;
103768103882
}
103769103883
}
103770103884
}
103771103885
#else
103772
-# define bestAutomaticIndex(A,B,C,D,E) /* no-op */
103886
+# define bestAutomaticIndex(A) /* no-op */
103773103887
#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
103774103888
103775103889
103776103890
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
103777103891
/*
@@ -103928,16 +104042,15 @@
103928104042
/*
103929104043
** Allocate and populate an sqlite3_index_info structure. It is the
103930104044
** responsibility of the caller to eventually release the structure
103931104045
** by passing the pointer returned by this function to sqlite3_free().
103932104046
*/
103933
-static sqlite3_index_info *allocateIndexInfo(
103934
- Parse *pParse,
103935
- WhereClause *pWC,
103936
- struct SrcList_item *pSrc,
103937
- ExprList *pOrderBy
103938
-){
104047
+static sqlite3_index_info *allocateIndexInfo(WhereBestIdx *p){
104048
+ Parse *pParse = p->pParse;
104049
+ WhereClause *pWC = p->pWC;
104050
+ struct SrcList_item *pSrc = p->pSrc;
104051
+ ExprList *pOrderBy = p->pOrderBy;
103939104052
int i, j;
103940104053
int nTerm;
103941104054
struct sqlite3_index_constraint *pIdxCons;
103942104055
struct sqlite3_index_orderby *pIdxOrderBy;
103943104056
struct sqlite3_index_constraint_usage *pUsage;
@@ -103963,16 +104076,17 @@
103963104076
** virtual table then allocate space for the aOrderBy part of
103964104077
** the sqlite3_index_info structure.
103965104078
*/
103966104079
nOrderBy = 0;
103967104080
if( pOrderBy ){
103968
- for(i=0; i<pOrderBy->nExpr; i++){
104081
+ int n = pOrderBy->nExpr;
104082
+ for(i=0; i<n; i++){
103969104083
Expr *pExpr = pOrderBy->a[i].pExpr;
103970104084
if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
103971104085
}
103972
- if( i==pOrderBy->nExpr ){
103973
- nOrderBy = pOrderBy->nExpr;
104086
+ if( i==n){
104087
+ nOrderBy = n;
103974104088
}
103975104089
}
103976104090
103977104091
/* Allocate the sqlite3_index_info structure
103978104092
*/
@@ -104092,20 +104206,14 @@
104092104206
** invocations. The sqlite3_index_info structure is also used when
104093104207
** code is generated to access the virtual table. The whereInfoDelete()
104094104208
** routine takes care of freeing the sqlite3_index_info structure after
104095104209
** everybody has finished with it.
104096104210
*/
104097
-static void bestVirtualIndex(
104098
- Parse *pParse, /* The parsing context */
104099
- WhereClause *pWC, /* The WHERE clause */
104100
- struct SrcList_item *pSrc, /* The FROM clause term to search */
104101
- Bitmask notReady, /* Mask of cursors not available for index */
104102
- Bitmask notValid, /* Cursors not valid for any purpose */
104103
- ExprList *pOrderBy, /* The order by clause */
104104
- WhereCost *pCost, /* Lowest cost query plan */
104105
- sqlite3_index_info **ppIdxInfo /* Index information passed to xBestIndex */
104106
-){
104211
+static void bestVirtualIndex(WhereBestIdx *p){
104212
+ Parse *pParse = p->pParse; /* The parsing context */
104213
+ WhereClause *pWC = p->pWC; /* The WHERE clause */
104214
+ struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
104107104215
Table *pTab = pSrc->pTab;
104108104216
sqlite3_index_info *pIdxInfo;
104109104217
struct sqlite3_index_constraint *pIdxCons;
104110104218
struct sqlite3_index_constraint_usage *pUsage;
104111104219
WhereTerm *pTerm;
@@ -104115,19 +104223,19 @@
104115104223
104116104224
/* Make sure wsFlags is initialized to some sane value. Otherwise, if the
104117104225
** malloc in allocateIndexInfo() fails and this function returns leaving
104118104226
** wsFlags in an uninitialized state, the caller may behave unpredictably.
104119104227
*/
104120
- memset(pCost, 0, sizeof(*pCost));
104121
- pCost->plan.wsFlags = WHERE_VIRTUALTABLE;
104228
+ memset(&p->cost, 0, sizeof(p->cost));
104229
+ p->cost.plan.wsFlags = WHERE_VIRTUALTABLE;
104122104230
104123104231
/* If the sqlite3_index_info structure has not been previously
104124104232
** allocated and initialized, then allocate and initialize it now.
104125104233
*/
104126
- pIdxInfo = *ppIdxInfo;
104234
+ pIdxInfo = *p->ppIdxInfo;
104127104235
if( pIdxInfo==0 ){
104128
- *ppIdxInfo = pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pOrderBy);
104236
+ *p->ppIdxInfo = pIdxInfo = allocateIndexInfo(p);
104129104237
}
104130104238
if( pIdxInfo==0 ){
104131104239
return;
104132104240
}
104133104241
@@ -104168,11 +104276,11 @@
104168104276
pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
104169104277
pUsage = pIdxInfo->aConstraintUsage;
104170104278
for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
104171104279
j = pIdxCons->iTermOffset;
104172104280
pTerm = &pWC->a[j];
104173
- pIdxCons->usable = (pTerm->prereqRight&notReady) ? 0 : 1;
104281
+ pIdxCons->usable = (pTerm->prereqRight&p->notReady) ? 0 : 1;
104174104282
}
104175104283
memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
104176104284
if( pIdxInfo->needToFreeIdxStr ){
104177104285
sqlite3_free(pIdxInfo->idxStr);
104178104286
}
@@ -104181,11 +104289,11 @@
104181104289
pIdxInfo->needToFreeIdxStr = 0;
104182104290
pIdxInfo->orderByConsumed = 0;
104183104291
/* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
104184104292
pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
104185104293
nOrderBy = pIdxInfo->nOrderBy;
104186
- if( !pOrderBy ){
104294
+ if( !p->pOrderBy ){
104187104295
pIdxInfo->nOrderBy = 0;
104188104296
}
104189104297
104190104298
if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
104191104299
return;
@@ -104192,20 +104300,20 @@
104192104300
}
104193104301
104194104302
pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
104195104303
for(i=0; i<pIdxInfo->nConstraint; i++){
104196104304
if( pUsage[i].argvIndex>0 ){
104197
- pCost->used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
104305
+ p->cost.used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
104198104306
}
104199104307
}
104200104308
104201104309
/* If there is an ORDER BY clause, and the selected virtual table index
104202104310
** does not satisfy it, increase the cost of the scan accordingly. This
104203104311
** matches the processing for non-virtual tables in bestBtreeIndex().
104204104312
*/
104205104313
rCost = pIdxInfo->estimatedCost;
104206
- if( pOrderBy && pIdxInfo->orderByConsumed==0 ){
104314
+ if( p->pOrderBy && pIdxInfo->orderByConsumed==0 ){
104207104315
rCost += estLog(rCost)*rCost;
104208104316
}
104209104317
104210104318
/* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
104211104319
** inital value of lowestCost in this loop. If it is, then the
@@ -104213,25 +104321,25 @@
104213104321
**
104214104322
** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT
104215104323
** is defined.
104216104324
*/
104217104325
if( (SQLITE_BIG_DBL/((double)2))<rCost ){
104218
- pCost->rCost = (SQLITE_BIG_DBL/((double)2));
104326
+ p->cost.rCost = (SQLITE_BIG_DBL/((double)2));
104219104327
}else{
104220
- pCost->rCost = rCost;
104328
+ p->cost.rCost = rCost;
104221104329
}
104222
- pCost->plan.u.pVtabIdx = pIdxInfo;
104330
+ p->cost.plan.u.pVtabIdx = pIdxInfo;
104223104331
if( pIdxInfo->orderByConsumed ){
104224
- pCost->plan.wsFlags |= WHERE_ORDERBY;
104332
+ p->cost.plan.wsFlags |= WHERE_ORDERBY;
104225104333
}
104226
- pCost->plan.nEq = 0;
104334
+ p->cost.plan.nEq = 0;
104227104335
pIdxInfo->nOrderBy = nOrderBy;
104228104336
104229104337
/* Try to find a more efficient access pattern by using multiple indexes
104230104338
** to optimize an OR expression within the WHERE clause.
104231104339
*/
104232
- bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
104340
+ bestOrClauseIndex(p);
104233104341
}
104234104342
#endif /* SQLITE_OMIT_VIRTUALTABLE */
104235104343
104236104344
#ifdef SQLITE_ENABLE_STAT3
104237104345
/*
@@ -104626,15 +104734,88 @@
104626104734
}
104627104735
return rc;
104628104736
}
104629104737
#endif /* defined(SQLITE_ENABLE_STAT3) */
104630104738
104739
+/*
104740
+** Check to see if column iCol of the table with cursor iTab will appear
104741
+** in sorted order according to the current query plan. Return true if
104742
+** it will and false if not.
104743
+**
104744
+** If *pbRev is initially 2 (meaning "unknown") then set *pbRev to the
104745
+** sort order of iTab.iCol. If *pbRev is 0 or 1 but does not match
104746
+** the sort order of iTab.iCol, then consider the column to be unordered.
104747
+*/
104748
+static int isOrderedColumn(WhereBestIdx *p, int iTab, int iCol, int *pbRev){
104749
+ int i, j;
104750
+ WhereLevel *pLevel = &p->aLevel[p->i-1];
104751
+ Index *pIdx;
104752
+ u8 sortOrder;
104753
+ for(i=p->i-1; i>=0; i--, pLevel--){
104754
+ if( pLevel->iTabCur!=iTab ) continue;
104755
+ if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
104756
+ pIdx = pLevel->plan.u.pIdx;
104757
+ if( iCol<0 ){
104758
+ sortOrder = 0;
104759
+ testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
104760
+ }else{
104761
+ for(j=0; j<pIdx->nColumn; j++){
104762
+ if( iCol==pIdx->aiColumn[j] ) break;
104763
+ }
104764
+ if( j>=pIdx->nColumn ) return 0;
104765
+ sortOrder = pIdx->aSortOrder[j];
104766
+ testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
104767
+ }
104768
+ }else{
104769
+ if( iCol!=(-1) ) return 0;
104770
+ sortOrder = 0;
104771
+ testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
104772
+ }
104773
+ if( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ){
104774
+ assert( sortOrder==0 || sortOrder==1 );
104775
+ testcase( sortOrder==1 );
104776
+ sortOrder = 1 - sortOrder;
104777
+ }
104778
+ if( *pbRev==2 ){
104779
+ *pbRev = sortOrder;
104780
+ return 1;
104781
+ }
104782
+ return (*pbRev==sortOrder);
104783
+ }
104784
+ return 0;
104785
+}
104786
+
104787
+/*
104788
+** pTerm is an == constraint. Check to see if the other side of
104789
+** the == is a constant or a value that is guaranteed to be ordered
104790
+** by outer loops. Return 1 if pTerm is ordered, and 0 if not.
104791
+*/
104792
+static int isOrderedTerm(WhereBestIdx *p, WhereTerm *pTerm, int *pbRev){
104793
+ Expr *pExpr = pTerm->pExpr;
104794
+ assert( pExpr->op==TK_EQ );
104795
+ assert( pExpr->pLeft!=0 && pExpr->pLeft->op==TK_COLUMN );
104796
+ assert( pExpr->pRight!=0 );
104797
+ if( p->i==0 ){
104798
+ return 1; /* All == are ordered in the outer loop */
104799
+ }
104800
+ if( pTerm->prereqRight==0 ){
104801
+ return 1; /* RHS of the == is a constant */
104802
+ }
104803
+ if( pExpr->pRight->op==TK_COLUMN
104804
+ && isOrderedColumn(p, pExpr->pRight->iTable, pExpr->pRight->iColumn, pbRev)
104805
+ ){
104806
+ return 1;
104807
+ }
104808
+
104809
+ /* If we cannot prove that the constraint is ordered, assume it is not */
104810
+ return 0;
104811
+}
104812
+
104631104813
104632104814
/*
104633104815
** Find the best query plan for accessing a particular table. Write the
104634
-** best query plan and its cost into the WhereCost object supplied as the
104635
-** last parameter.
104816
+** best query plan and its cost into the p->cost.
104636104817
**
104637104818
** The lowest cost plan wins. The cost is an estimate of the amount of
104638104819
** CPU and disk I/O needed to process the requested result.
104639104820
** Factors that influence cost include:
104640104821
**
@@ -104655,33 +104836,27 @@
104655104836
** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table
104656104837
** in the SELECT statement, then no indexes are considered. However, the
104657104838
** selected plan may still take advantage of the built-in rowid primary key
104658104839
** index.
104659104840
*/
104660
-static void bestBtreeIndex(
104661
- Parse *pParse, /* The parsing context */
104662
- WhereClause *pWC, /* The WHERE clause */
104663
- struct SrcList_item *pSrc, /* The FROM clause term to search */
104664
- Bitmask notReady, /* Mask of cursors not available for indexing */
104665
- Bitmask notValid, /* Cursors not available for any purpose */
104666
- ExprList *pOrderBy, /* The ORDER BY clause */
104667
- ExprList *pDistinct, /* The select-list if query is DISTINCT */
104668
- WhereCost *pCost /* Lowest cost query plan */
104669
-){
104841
+static void bestBtreeIndex(WhereBestIdx *p){
104842
+ Parse *pParse = p->pParse; /* The parsing context */
104843
+ WhereClause *pWC = p->pWC; /* The WHERE clause */
104844
+ struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
104670104845
int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
104671104846
Index *pProbe; /* An index we are evaluating */
104672104847
Index *pIdx; /* Copy of pProbe, or zero for IPK index */
104673104848
int eqTermMask; /* Current mask of valid equality operators */
104674104849
int idxEqTermMask; /* Index mask of valid equality operators */
104675104850
Index sPk; /* A fake index object for the primary key */
104676104851
tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
104677104852
int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
104678
- int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */
104853
+ int wsFlagMask; /* Allowed flags in p->cost.plan.wsFlag */
104679104854
104680104855
/* Initialize the cost to a worst-case value */
104681
- memset(pCost, 0, sizeof(*pCost));
104682
- pCost->rCost = SQLITE_BIG_DBL;
104856
+ memset(&p->cost, 0, sizeof(p->cost));
104857
+ p->cost.rCost = SQLITE_BIG_DBL;
104683104858
104684104859
/* If the pSrc table is the right table of a LEFT JOIN then we may not
104685104860
** use an index to satisfy IS NULL constraints on that table. This is
104686104861
** because columns might end up being NULL if the table does not match -
104687104862
** a circumstance which the index cannot help us discover. Ticket #2177.
@@ -104730,11 +104905,11 @@
104730104905
for(; pProbe; pIdx=pProbe=pProbe->pNext){
104731104906
const tRowcnt * const aiRowEst = pProbe->aiRowEst;
104732104907
double cost; /* Cost of using pProbe */
104733104908
double nRow; /* Estimated number of rows in result set */
104734104909
double log10N = (double)1; /* base-10 logarithm of nRow (inexact) */
104735
- int rev; /* True to scan in reverse order */
104910
+ int bRev = 2; /* 0=forward scan. 1=reverse. 2=undecided */
104736104911
int wsFlags = 0;
104737104912
Bitmask used = 0;
104738104913
104739104914
/* The following variables are populated based on the properties of
104740104915
** index being evaluated. They are then used to determine the expected
@@ -104763,10 +104938,14 @@
104763104938
**
104764104939
** If there exists a WHERE term of the form "x IN (SELECT ...)", then
104765104940
** the sub-select is assumed to return 25 rows for the purposes of
104766104941
** determining nInMul.
104767104942
**
104943
+ ** nOrdered:
104944
+ ** The number of equality terms that are constrainted by outer loop
104945
+ ** variables that are well-ordered.
104946
+ **
104768104947
** bInEst:
104769104948
** Set to true if there was at least one "x IN (SELECT ...)" term used
104770104949
** in determining the value of nInMul. Note that the RHS of the
104771104950
** IN operator must be a SELECT, not a value list, for this variable
104772104951
** to be true.
@@ -104781,10 +104960,14 @@
104781104960
** bSort:
104782104961
** Boolean. True if there is an ORDER BY clause that will require an
104783104962
** external sort (i.e. scanning the index being evaluated will not
104784104963
** correctly order records).
104785104964
**
104965
+ ** bDistinct:
104966
+ ** Boolean. True if there is a DISTINCT clause that will require an
104967
+ ** external btree.
104968
+ **
104786104969
** bLookup:
104787104970
** Boolean. True if a table lookup is required for each index entry
104788104971
** visited. In other words, true if this is not a covering index.
104789104972
** This is always false for the rowid primary key index of a table.
104790104973
** For other indexes, it is true unless all the columns of the table
@@ -104797,26 +104980,33 @@
104797104980
**
104798104981
** SELECT a, b FROM tbl WHERE a = 1;
104799104982
** SELECT a, b, c FROM tbl WHERE a = 1;
104800104983
*/
104801104984
int nEq; /* Number of == or IN terms matching index */
104985
+ int nOrdered; /* Number of ordered terms matching index */
104802104986
int bInEst = 0; /* True if "x IN (SELECT...)" seen */
104803104987
int nInMul = 1; /* Number of distinct equalities to lookup */
104804104988
double rangeDiv = (double)1; /* Estimated reduction in search space */
104805104989
int nBound = 0; /* Number of range constraints seen */
104806
- int bSort = !!pOrderBy; /* True if external sort required */
104807
- int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */
104990
+ int bSort; /* True if external sort required */
104991
+ int bDist; /* True if index cannot help with DISTINCT */
104808104992
int bLookup = 0; /* True if not a covering index */
104993
+ int nOBSat = 0; /* Number of ORDER BY terms satisfied */
104994
+ int nOrderBy; /* Number of ORDER BY terms */
104809104995
WhereTerm *pTerm; /* A single term of the WHERE clause */
104810104996
#ifdef SQLITE_ENABLE_STAT3
104811104997
WhereTerm *pFirstTerm = 0; /* First term matching the index */
104812104998
#endif
104999
+
105000
+ nOrderBy = p->pOrderBy ? p->pOrderBy->nExpr : 0;
105001
+ bSort = nOrderBy>0 && (p->i==0 || p->aLevel[p->i-1].plan.nOBSat<nOrderBy);
105002
+ bDist = p->i==0 && p->pDistinct!=0;
104813105003
104814105004
/* Determine the values of nEq and nInMul */
104815
- for(nEq=0; nEq<pProbe->nColumn; nEq++){
105005
+ for(nEq=nOrdered=0; nEq<pProbe->nColumn; nEq++){
104816105006
int j = pProbe->aiColumn[nEq];
104817
- pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
105007
+ pTerm = findTerm(pWC, iCur, j, p->notReady, eqTermMask, pIdx);
104818105008
if( pTerm==0 ) break;
104819105009
wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
104820105010
testcase( pTerm->pWC!=pWC );
104821105011
if( pTerm->eOperator & WO_IN ){
104822105012
Expr *pExpr = pTerm->pExpr;
@@ -104829,10 +105019,13 @@
104829105019
/* "x IN (value, value, ...)" */
104830105020
nInMul *= pExpr->x.pList->nExpr;
104831105021
}
104832105022
}else if( pTerm->eOperator & WO_ISNULL ){
104833105023
wsFlags |= WHERE_COLUMN_NULL;
105024
+ if( nEq==nOrdered ) nOrdered++;
105025
+ }else if( bSort && nEq==nOrdered && isOrderedTerm(p, pTerm, &bRev) ){
105026
+ nOrdered++;
104834105027
}
104835105028
#ifdef SQLITE_ENABLE_STAT3
104836105029
if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
104837105030
#endif
104838105031
used |= pTerm->prereqRight;
@@ -104853,13 +105046,14 @@
104853105046
if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
104854105047
wsFlags |= WHERE_UNIQUE;
104855105048
}
104856105049
}else if( pProbe->bUnordered==0 ){
104857105050
int j = (nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[nEq]);
104858
- if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
104859
- WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
104860
- WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
105051
+ if( findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
105052
+ WhereTerm *pTop, *pBtm;
105053
+ pTop = findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE, pIdx);
105054
+ pBtm = findTerm(pWC, iCur, j, p->notReady, WO_GT|WO_GE, pIdx);
104861105055
whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
104862105056
if( pTop ){
104863105057
nBound = 1;
104864105058
wsFlags |= WHERE_TOP_LIMIT;
104865105059
used |= pTop->prereqRight;
@@ -104877,22 +105071,29 @@
104877105071
104878105072
/* If there is an ORDER BY clause and the index being considered will
104879105073
** naturally scan rows in the required order, set the appropriate flags
104880105074
** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
104881105075
** will scan rows in a different order, set the bSort variable. */
104882
- if( isSortingIndex(
104883
- pParse, pWC->pMaskSet, pProbe, iCur, pOrderBy, nEq, wsFlags, &rev)
104884
- ){
104885
- bSort = 0;
104886
- wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
104887
- wsFlags |= (rev ? WHERE_REVERSE : 0);
105076
+ assert( bRev>=0 && bRev<=2 );
105077
+ if( bSort ){
105078
+ testcase( bRev==0 );
105079
+ testcase( bRev==1 );
105080
+ testcase( bRev==2 );
105081
+ nOBSat = isSortingIndex(p, pProbe, iCur, nOrdered,
105082
+ wsFlags, bRev&1, &bRev);
105083
+ if( nOrderBy==nOBSat ){
105084
+ bSort = 0;
105085
+ wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
105086
+ }
105087
+ if( bRev & 1 ) wsFlags |= WHERE_REVERSE;
104888105088
}
104889105089
104890105090
/* If there is a DISTINCT qualifier and this index will scan rows in
104891105091
** order of the DISTINCT expressions, clear bDist and set the appropriate
104892105092
** flags in wsFlags. */
104893
- if( isDistinctIndex(pParse, pWC, pProbe, iCur, pDistinct, nEq)
105093
+ if( bDist
105094
+ && isDistinctIndex(pParse, pWC, pProbe, iCur, p->pDistinct, nEq)
104894105095
&& (wsFlags & WHERE_COLUMN_IN)==0
104895105096
){
104896105097
bDist = 0;
104897105098
wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
104898105099
}
@@ -104965,16 +105166,14 @@
104965105166
** The ANALYZE command and the sqlite_stat1 and sqlite_stat3 tables do
104966105167
** not give us data on the relative sizes of table and index records.
104967105168
** So this computation assumes table records are about twice as big
104968105169
** as index records
104969105170
*/
104970
- if( wsFlags==WHERE_IDX_ONLY
105171
+ if( (wsFlags&~WHERE_REVERSE)==WHERE_IDX_ONLY
104971105172
&& (pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
104972105173
&& sqlite3GlobalConfig.bUseCis
104973
-#ifndef SQLITE_OMIT_BUILTIN_TEST
104974
- && (pParse->db->flags & SQLITE_CoverIdxScan)==0
104975
-#endif
105174
+ && OptimizationEnabled(pParse->db, SQLITE_CoverIdxScan)
104976105175
){
104977105176
/* This index is not useful for indexing, but it is a covering index.
104978105177
** A full-scan of the index might be a little faster than a full-scan
104979105178
** of the table, so give this case a cost slightly less than a table
104980105179
** scan. */
@@ -105024,11 +105223,11 @@
105024105223
** adds C*N*log10(N) to the cost, where N is the number of rows to be
105025105224
** sorted and C is a factor between 1.95 and 4.3. We will split the
105026105225
** difference and select C of 3.0.
105027105226
*/
105028105227
if( bSort ){
105029
- cost += nRow*estLog(nRow)*3;
105228
+ cost += nRow*estLog(nRow*(nOrderBy - nOBSat)/nOrderBy)*3;
105030105229
}
105031105230
if( bDist ){
105032105231
cost += nRow*estLog(nRow)*3;
105033105232
}
105034105233
@@ -105048,20 +105247,20 @@
105048105247
** tables that are not in outer loops. If notReady is used here instead
105049105248
** of notValid, then a optimal index that depends on inner joins loops
105050105249
** might be selected even when there exists an optimal index that has
105051105250
** no such dependency.
105052105251
*/
105053
- if( nRow>2 && cost<=pCost->rCost ){
105252
+ if( nRow>2 && cost<=p->cost.rCost ){
105054105253
int k; /* Loop counter */
105055105254
int nSkipEq = nEq; /* Number of == constraints to skip */
105056105255
int nSkipRange = nBound; /* Number of < constraints to skip */
105057105256
Bitmask thisTab; /* Bitmap for pSrc */
105058105257
105059105258
thisTab = getMask(pWC->pMaskSet, iCur);
105060105259
for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){
105061105260
if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
105062
- if( (pTerm->prereqAll & notValid)!=thisTab ) continue;
105261
+ if( (pTerm->prereqAll & p->notValid)!=thisTab ) continue;
105063105262
if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
105064105263
if( nSkipEq ){
105065105264
/* Ignore the first nEq equality matches since the index
105066105265
** has already accounted for these */
105067105266
nSkipEq--;
@@ -105092,29 +105291,32 @@
105092105291
if( nRow<2 ) nRow = 2;
105093105292
}
105094105293
105095105294
105096105295
WHERETRACE((
105097
- "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
105098
- " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
105296
+ "%s(%s):\n"
105297
+ " nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%08x\n"
105298
+ " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f\n"
105299
+ " used=0x%llx nOrdered=%d nOBSat=%d\n",
105099105300
pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
105100105301
nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
105101
- notReady, log10N, nRow, cost, used
105302
+ p->notReady, log10N, nRow, cost, used, nOrdered, nOBSat
105102105303
));
105103105304
105104105305
/* If this index is the best we have seen so far, then record this
105105105306
** index and its cost in the pCost structure.
105106105307
*/
105107105308
if( (!pIdx || wsFlags)
105108
- && (cost<pCost->rCost || (cost<=pCost->rCost && nRow<pCost->plan.nRow))
105309
+ && (cost<p->cost.rCost || (cost<=p->cost.rCost && nRow<p->cost.plan.nRow))
105109105310
){
105110
- pCost->rCost = cost;
105111
- pCost->used = used;
105112
- pCost->plan.nRow = nRow;
105113
- pCost->plan.wsFlags = (wsFlags&wsFlagMask);
105114
- pCost->plan.nEq = nEq;
105115
- pCost->plan.u.pIdx = pIdx;
105311
+ p->cost.rCost = cost;
105312
+ p->cost.used = used;
105313
+ p->cost.plan.nRow = nRow;
105314
+ p->cost.plan.wsFlags = (wsFlags&wsFlagMask);
105315
+ p->cost.plan.nEq = nEq;
105316
+ p->cost.plan.nOBSat = nOBSat;
105317
+ p->cost.plan.u.pIdx = pIdx;
105116105318
}
105117105319
105118105320
/* If there was an INDEXED BY clause, then only that one index is
105119105321
** considered. */
105120105322
if( pSrc->pIndex ) break;
@@ -105127,58 +105329,57 @@
105127105329
/* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
105128105330
** is set, then reverse the order that the index will be scanned
105129105331
** in. This is used for application testing, to help find cases
105130105332
** where application behaviour depends on the (undefined) order that
105131105333
** SQLite outputs rows in in the absence of an ORDER BY clause. */
105132
- if( !pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
105133
- pCost->plan.wsFlags |= WHERE_REVERSE;
105334
+ if( !p->pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
105335
+ p->cost.plan.wsFlags |= WHERE_REVERSE;
105134105336
}
105135105337
105136
- assert( pOrderBy || (pCost->plan.wsFlags&WHERE_ORDERBY)==0 );
105137
- assert( pCost->plan.u.pIdx==0 || (pCost->plan.wsFlags&WHERE_ROWID_EQ)==0 );
105338
+ assert( p->pOrderBy || (p->cost.plan.wsFlags&WHERE_ORDERBY)==0 );
105339
+ assert( p->cost.plan.u.pIdx==0 || (p->cost.plan.wsFlags&WHERE_ROWID_EQ)==0 );
105138105340
assert( pSrc->pIndex==0
105139
- || pCost->plan.u.pIdx==0
105140
- || pCost->plan.u.pIdx==pSrc->pIndex
105341
+ || p->cost.plan.u.pIdx==0
105342
+ || p->cost.plan.u.pIdx==pSrc->pIndex
105141105343
);
105142105344
105143105345
WHERETRACE(("best index is: %s\n",
105144
- ((pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" :
105145
- pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk")
105346
+ ((p->cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" :
105347
+ p->cost.plan.u.pIdx ? p->cost.plan.u.pIdx->zName : "ipk")
105146105348
));
105147105349
105148
- bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
105149
- bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost);
105150
- pCost->plan.wsFlags |= eqTermMask;
105350
+ bestOrClauseIndex(p);
105351
+ bestAutomaticIndex(p);
105352
+ p->cost.plan.wsFlags |= eqTermMask;
105151105353
}
105152105354
105153105355
/*
105154105356
** Find the query plan for accessing table pSrc->pTab. Write the
105155105357
** best query plan and its cost into the WhereCost object supplied
105156105358
** as the last parameter. This function may calculate the cost of
105157105359
** both real and virtual table scans.
105360
+**
105361
+** This function does not take ORDER BY or DISTINCT into account. Nor
105362
+** does it remember the virtual table query plan. All it does is compute
105363
+** the cost while determining if an OR optimization is applicable. The
105364
+** details will be reconsidered later if the optimization is found to be
105365
+** applicable.
105158105366
*/
105159
-static void bestIndex(
105160
- Parse *pParse, /* The parsing context */
105161
- WhereClause *pWC, /* The WHERE clause */
105162
- struct SrcList_item *pSrc, /* The FROM clause term to search */
105163
- Bitmask notReady, /* Mask of cursors not available for indexing */
105164
- Bitmask notValid, /* Cursors not available for any purpose */
105165
- ExprList *pOrderBy, /* The ORDER BY clause */
105166
- WhereCost *pCost /* Lowest cost query plan */
105167
-){
105367
+static void bestIndex(WhereBestIdx *p){
105168105368
#ifndef SQLITE_OMIT_VIRTUALTABLE
105169
- if( IsVirtual(pSrc->pTab) ){
105170
- sqlite3_index_info *p = 0;
105171
- bestVirtualIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost,&p);
105172
- if( p->needToFreeIdxStr ){
105173
- sqlite3_free(p->idxStr);
105369
+ if( IsVirtual(p->pSrc->pTab) ){
105370
+ sqlite3_index_info *pIdxInfo = 0;
105371
+ p->ppIdxInfo = &pIdxInfo;
105372
+ bestVirtualIndex(p);
105373
+ if( pIdxInfo->needToFreeIdxStr ){
105374
+ sqlite3_free(pIdxInfo->idxStr);
105174105375
}
105175
- sqlite3DbFree(pParse->db, p);
105376
+ sqlite3DbFree(p->pParse->db, pIdxInfo);
105176105377
}else
105177105378
#endif
105178105379
{
105179
- bestBtreeIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, 0, pCost);
105380
+ bestBtreeIndex(p);
105180105381
}
105181105382
}
105182105383
105183105384
/*
105184105385
** Disable a term in the WHERE clause. Except, do not disable the term
@@ -106432,46 +106633,50 @@
106432106633
** fi
106433106634
** end
106434106635
**
106435106636
** ORDER BY CLAUSE PROCESSING
106436106637
**
106437
-** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
106638
+** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
106438106639
** if there is one. If there is no ORDER BY clause or if this routine
106439
-** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
106640
+** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
106440106641
**
106441106642
** If an index can be used so that the natural output order of the table
106442106643
** scan is correct for the ORDER BY clause, then that index is used and
106443
-** *ppOrderBy is set to NULL. This is an optimization that prevents an
106444
-** unnecessary sort of the result set if an index appropriate for the
106445
-** ORDER BY clause already exists.
106644
+** the returned WhereInfo.nOBSat field is set to pOrderBy->nExpr. This
106645
+** is an optimization that prevents an unnecessary sort of the result set
106646
+** if an index appropriate for the ORDER BY clause already exists.
106446106647
**
106447106648
** If the where clause loops cannot be arranged to provide the correct
106448
-** output order, then the *ppOrderBy is unchanged.
106649
+** output order, then WhereInfo.nOBSat is 0.
106449106650
*/
106450106651
SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
106451106652
Parse *pParse, /* The parser context */
106452106653
SrcList *pTabList, /* A list of all tables to be scanned */
106453106654
Expr *pWhere, /* The WHERE clause */
106454
- ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */
106655
+ ExprList *pOrderBy, /* An ORDER BY clause, or NULL */
106455106656
ExprList *pDistinct, /* The select-list for DISTINCT queries - or NULL */
106456106657
u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */
106457106658
int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */
106458106659
){
106459
- int i; /* Loop counter */
106460106660
int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
106461106661
int nTabList; /* Number of elements in pTabList */
106462106662
WhereInfo *pWInfo; /* Will become the return value of this function */
106463106663
Vdbe *v = pParse->pVdbe; /* The virtual database engine */
106464106664
Bitmask notReady; /* Cursors that are not yet positioned */
106665
+ WhereBestIdx sWBI; /* Best index search context */
106465106666
WhereMaskSet *pMaskSet; /* The expression mask set */
106466
- WhereClause *pWC; /* Decomposition of the WHERE clause */
106467
- struct SrcList_item *pTabItem; /* A single entry from pTabList */
106468
- WhereLevel *pLevel; /* A single level in the pWInfo list */
106469
- int iFrom; /* First unused FROM clause element */
106667
+ WhereLevel *pLevel; /* A single level in pWInfo->a[] */
106668
+ int iFrom; /* First unused FROM clause element */
106470106669
int andFlags; /* AND-ed combination of all pWC->a[].wtFlags */
106670
+ int ii; /* Loop counter */
106471106671
sqlite3 *db; /* Database connection */
106472106672
106673
+
106674
+ /* Variable initialization */
106675
+ memset(&sWBI, 0, sizeof(sWBI));
106676
+ sWBI.pParse = pParse;
106677
+
106473106678
/* The number of tables in the FROM clause is limited by the number of
106474106679
** bits in a Bitmask
106475106680
*/
106476106681
testcase( pTabList->nSrc==BMS );
106477106682
if( pTabList->nSrc>BMS ){
@@ -106507,26 +106712,27 @@
106507106712
}
106508106713
pWInfo->nLevel = nTabList;
106509106714
pWInfo->pParse = pParse;
106510106715
pWInfo->pTabList = pTabList;
106511106716
pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
106512
- pWInfo->pWC = pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
106717
+ pWInfo->pWC = sWBI.pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
106513106718
pWInfo->wctrlFlags = wctrlFlags;
106514106719
pWInfo->savedNQueryLoop = pParse->nQueryLoop;
106515
- pMaskSet = (WhereMaskSet*)&pWC[1];
106720
+ pMaskSet = (WhereMaskSet*)&sWBI.pWC[1];
106721
+ sWBI.aLevel = pWInfo->a;
106516106722
106517106723
/* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
106518106724
** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
106519
- if( db->flags & SQLITE_DistinctOpt ) pDistinct = 0;
106725
+ if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0;
106520106726
106521106727
/* Split the WHERE clause into separate subexpressions where each
106522106728
** subexpression is separated by an AND operator.
106523106729
*/
106524106730
initMaskSet(pMaskSet);
106525
- whereClauseInit(pWC, pParse, pMaskSet, wctrlFlags);
106731
+ whereClauseInit(sWBI.pWC, pParse, pMaskSet, wctrlFlags);
106526106732
sqlite3ExprCodeConstants(pParse, pWhere);
106527
- whereSplit(pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */
106733
+ whereSplit(sWBI.pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */
106528106734
106529106735
/* Special case: a WHERE clause that is constant. Evaluate the
106530106736
** expression and either jump over all of the code or fall thru.
106531106737
*/
106532106738
if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
@@ -106553,24 +106759,24 @@
106553106759
** Note that bitmasks are created for all pTabList->nSrc tables in
106554106760
** pTabList, not just the first nTabList tables. nTabList is normally
106555106761
** equal to pTabList->nSrc but might be shortened to 1 if the
106556106762
** WHERE_ONETABLE_ONLY flag is set.
106557106763
*/
106558
- assert( pWC->vmask==0 && pMaskSet->n==0 );
106559
- for(i=0; i<pTabList->nSrc; i++){
106560
- createMask(pMaskSet, pTabList->a[i].iCursor);
106764
+ assert( sWBI.pWC->vmask==0 && pMaskSet->n==0 );
106765
+ for(ii=0; ii<pTabList->nSrc; ii++){
106766
+ createMask(pMaskSet, pTabList->a[ii].iCursor);
106561106767
#ifndef SQLITE_OMIT_VIRTUALTABLE
106562
- if( ALWAYS(pTabList->a[i].pTab) && IsVirtual(pTabList->a[i].pTab) ){
106563
- pWC->vmask |= ((Bitmask)1 << i);
106768
+ if( ALWAYS(pTabList->a[ii].pTab) && IsVirtual(pTabList->a[ii].pTab) ){
106769
+ sWBI.pWC->vmask |= ((Bitmask)1 << ii);
106564106770
}
106565106771
#endif
106566106772
}
106567106773
#ifndef NDEBUG
106568106774
{
106569106775
Bitmask toTheLeft = 0;
106570
- for(i=0; i<pTabList->nSrc; i++){
106571
- Bitmask m = getMask(pMaskSet, pTabList->a[i].iCursor);
106776
+ for(ii=0; ii<pTabList->nSrc; ii++){
106777
+ Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
106572106778
assert( (m-1)==toTheLeft );
106573106779
toTheLeft |= m;
106574106780
}
106575106781
}
106576106782
#endif
@@ -106578,20 +106784,20 @@
106578106784
/* Analyze all of the subexpressions. Note that exprAnalyze() might
106579106785
** add new virtual terms onto the end of the WHERE clause. We do not
106580106786
** want to analyze these virtual terms, so start analyzing at the end
106581106787
** and work forward so that the added virtual terms are never processed.
106582106788
*/
106583
- exprAnalyzeAll(pTabList, pWC);
106789
+ exprAnalyzeAll(pTabList, sWBI.pWC);
106584106790
if( db->mallocFailed ){
106585106791
goto whereBeginError;
106586106792
}
106587106793
106588106794
/* Check if the DISTINCT qualifier, if there is one, is redundant.
106589106795
** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to
106590106796
** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT.
106591106797
*/
106592
- if( pDistinct && isDistinctRedundant(pParse, pTabList, pWC, pDistinct) ){
106798
+ if( pDistinct && isDistinctRedundant(pParse, pTabList, sWBI.pWC, pDistinct) ){
106593106799
pDistinct = 0;
106594106800
pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
106595106801
}
106596106802
106597106803
/* Chose the best index to use for each table in the FROM clause.
@@ -106607,14 +106813,17 @@
106607106813
** pWInfo->a[].pTerm When wsFlags==WO_OR, the OR-clause term
106608106814
**
106609106815
** This loop also figures out the nesting order of tables in the FROM
106610106816
** clause.
106611106817
*/
106612
- notReady = ~(Bitmask)0;
106818
+ sWBI.notValid = ~(Bitmask)0;
106819
+ sWBI.pOrderBy = pOrderBy;
106820
+ sWBI.n = nTabList;
106821
+ sWBI.pDistinct = pDistinct;
106613106822
andFlags = ~0;
106614106823
WHERETRACE(("*** Optimizer Start ***\n"));
106615
- for(i=iFrom=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
106824
+ for(sWBI.i=iFrom=0, pLevel=pWInfo->a; sWBI.i<nTabList; sWBI.i++, pLevel++){
106616106825
WhereCost bestPlan; /* Most efficient plan seen so far */
106617106826
Index *pIdx; /* Index for FROM table at pTabItem */
106618106827
int j; /* For looping over FROM tables */
106619106828
int bestJ = -1; /* The value of j */
106620106829
Bitmask m; /* Bitmask value for j or bestJ */
@@ -106622,11 +106831,11 @@
106622106831
int nUnconstrained; /* Number tables without INDEXED BY */
106623106832
Bitmask notIndexed; /* Mask of tables that cannot use an index */
106624106833
106625106834
memset(&bestPlan, 0, sizeof(bestPlan));
106626106835
bestPlan.rCost = SQLITE_BIG_DBL;
106627
- WHERETRACE(("*** Begin search for loop %d ***\n", i));
106836
+ WHERETRACE(("*** Begin search for loop %d ***\n", sWBI.i));
106628106837
106629106838
/* Loop through the remaining entries in the FROM clause to find the
106630106839
** next nested loop. The loop tests all FROM clause entries
106631106840
** either once or twice.
106632106841
**
@@ -106638,12 +106847,12 @@
106638106847
** were used as the innermost nested loop. In other words, a table
106639106848
** is chosen such that the cost of running that table cannot be reduced
106640106849
** by waiting for other tables to run first. This "optimal" test works
106641106850
** by first assuming that the FROM clause is on the inner loop and finding
106642106851
** its query plan, then checking to see if that query plan uses any
106643
- ** other FROM clause terms that are notReady. If no notReady terms are
106644
- ** used then the "optimal" query plan works.
106852
+ ** other FROM clause terms that are sWBI.notValid. If no notValid terms
106853
+ ** are used then the "optimal" query plan works.
106645106854
**
106646106855
** Note that the WhereCost.nRow parameter for an optimal scan might
106647106856
** not be as small as it would be if the table really were the innermost
106648106857
** join. The nRow value can be reduced by WHERE clause constraints
106649106858
** that do not use indices. But this nRow reduction only happens if the
@@ -106670,59 +106879,52 @@
106670106879
** costlier approach.
106671106880
*/
106672106881
nUnconstrained = 0;
106673106882
notIndexed = 0;
106674106883
for(isOptimal=(iFrom<nTabList-1); isOptimal>=0 && bestJ<0; isOptimal--){
106675
- Bitmask mask; /* Mask of tables not yet ready */
106676
- for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){
106884
+ for(j=iFrom, sWBI.pSrc=&pTabList->a[j]; j<nTabList; j++, sWBI.pSrc++){
106677106885
int doNotReorder; /* True if this table should not be reordered */
106678
- WhereCost sCost; /* Cost information from best[Virtual]Index() */
106679
- ExprList *pOrderBy; /* ORDER BY clause for index to optimize */
106680
- ExprList *pDist; /* DISTINCT clause for index to optimize */
106681106886
106682
- doNotReorder = (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0;
106887
+ doNotReorder = (sWBI.pSrc->jointype & (JT_LEFT|JT_CROSS))!=0;
106683106888
if( j!=iFrom && doNotReorder ) break;
106684
- m = getMask(pMaskSet, pTabItem->iCursor);
106685
- if( (m & notReady)==0 ){
106889
+ m = getMask(pMaskSet, sWBI.pSrc->iCursor);
106890
+ if( (m & sWBI.notValid)==0 ){
106686106891
if( j==iFrom ) iFrom++;
106687106892
continue;
106688106893
}
106689
- mask = (isOptimal ? m : notReady);
106690
- pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0);
106691
- pDist = (i==0 ? pDistinct : 0);
106692
- if( pTabItem->pIndex==0 ) nUnconstrained++;
106894
+ sWBI.notReady = (isOptimal ? m : sWBI.notValid);
106895
+ if( sWBI.pSrc->pIndex==0 ) nUnconstrained++;
106693106896
106694106897
WHERETRACE(("=== trying table %d with isOptimal=%d ===\n",
106695106898
j, isOptimal));
106696
- assert( pTabItem->pTab );
106899
+ assert( sWBI.pSrc->pTab );
106697106900
#ifndef SQLITE_OMIT_VIRTUALTABLE
106698
- if( IsVirtual(pTabItem->pTab) ){
106699
- sqlite3_index_info **pp = &pWInfo->a[j].pIdxInfo;
106700
- bestVirtualIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
106701
- &sCost, pp);
106901
+ if( IsVirtual(sWBI.pSrc->pTab) ){
106902
+ sWBI.ppIdxInfo = &pWInfo->a[j].pIdxInfo;
106903
+ bestVirtualIndex(&sWBI);
106702106904
}else
106703106905
#endif
106704106906
{
106705
- bestBtreeIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
106706
- pDist, &sCost);
106907
+ bestBtreeIndex(&sWBI);
106707106908
}
106708
- assert( isOptimal || (sCost.used&notReady)==0 );
106909
+ assert( isOptimal || (sWBI.cost.used&sWBI.notValid)==0 );
106709106910
106710106911
/* If an INDEXED BY clause is present, then the plan must use that
106711106912
** index if it uses any index at all */
106712
- assert( pTabItem->pIndex==0
106713
- || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
106714
- || sCost.plan.u.pIdx==pTabItem->pIndex );
106913
+ assert( sWBI.pSrc->pIndex==0
106914
+ || (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
106915
+ || sWBI.cost.plan.u.pIdx==sWBI.pSrc->pIndex );
106715106916
106716
- if( isOptimal && (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
106917
+ if( isOptimal && (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
106717106918
notIndexed |= m;
106718106919
}
106719106920
106720106921
/* Conditions under which this table becomes the best so far:
106721106922
**
106722106923
** (1) The table must not depend on other tables that have not
106723
- ** yet run.
106924
+ ** yet run. (In other words, it must not depend on tables
106925
+ ** in inner loops.)
106724106926
**
106725106927
** (2) A full-table-scan plan cannot supercede indexed plan unless
106726106928
** the full-table-scan is an "optimal" plan as defined above.
106727106929
**
106728106930
** (3) All tables have an INDEXED BY clause or this table lacks an
@@ -106735,37 +106937,38 @@
106735106937
** An indexable full-table-scan from reaching rule (3).
106736106938
**
106737106939
** (4) The plan cost must be lower than prior plans or else the
106738106940
** cost must be the same and the number of rows must be lower.
106739106941
*/
106740
- if( (sCost.used&notReady)==0 /* (1) */
106741
- && (bestJ<0 || (notIndexed&m)!=0 /* (2) */
106942
+ if( (sWBI.cost.used&sWBI.notValid)==0 /* (1) */
106943
+ && (bestJ<0 || (notIndexed&m)!=0 /* (2) */
106742106944
|| (bestPlan.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
106743
- || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
106744
- && (nUnconstrained==0 || pTabItem->pIndex==0 /* (3) */
106745
- || NEVER((sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
106746
- && (bestJ<0 || sCost.rCost<bestPlan.rCost /* (4) */
106747
- || (sCost.rCost<=bestPlan.rCost
106748
- && sCost.plan.nRow<bestPlan.plan.nRow))
106945
+ || (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
106946
+ && (nUnconstrained==0 || sWBI.pSrc->pIndex==0 /* (3) */
106947
+ || NEVER((sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
106948
+ && (bestJ<0 || sWBI.cost.rCost<bestPlan.rCost /* (4) */
106949
+ || (sWBI.cost.rCost<=bestPlan.rCost
106950
+ && sWBI.cost.plan.nRow<bestPlan.plan.nRow))
106749106951
){
106750106952
WHERETRACE(("=== table %d is best so far"
106751
- " with cost=%g and nRow=%g\n",
106752
- j, sCost.rCost, sCost.plan.nRow));
106753
- bestPlan = sCost;
106953
+ " with cost=%.1f, nRow=%.1f, nOBSat=%d\n",
106954
+ j, sWBI.cost.rCost, sWBI.cost.plan.nRow,
106955
+ sWBI.cost.plan.nOBSat));
106956
+ bestPlan = sWBI.cost;
106754106957
bestJ = j;
106755106958
}
106756106959
if( doNotReorder ) break;
106757106960
}
106758106961
}
106759106962
assert( bestJ>=0 );
106760
- assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
106761
- WHERETRACE(("*** Optimizer selects table %d for loop %d"
106762
- " with cost=%g and nRow=%g\n",
106763
- bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow));
106764
- /* The ALWAYS() that follows was added to hush up clang scan-build */
106765
- if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 && ALWAYS(ppOrderBy) ){
106766
- *ppOrderBy = 0;
106963
+ assert( sWBI.notValid & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
106964
+ WHERETRACE(("*** Optimizer selects table %d for loop %d with:\n"
106965
+ " cost=%.1f, nRow=%.1f, nOBSat=%d wsFlags=0x%08x\n",
106966
+ bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow,
106967
+ bestPlan.plan.nOBSat, bestPlan.plan.wsFlags));
106968
+ if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){
106969
+ pWInfo->nOBSat = pOrderBy->nExpr;
106767106970
}
106768106971
if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
106769106972
assert( pWInfo->eDistinct==0 );
106770106973
pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
106771106974
}
@@ -106782,11 +106985,11 @@
106782106985
pLevel->iIdxCur = pParse->nTab++;
106783106986
}
106784106987
}else{
106785106988
pLevel->iIdxCur = -1;
106786106989
}
106787
- notReady &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
106990
+ sWBI.notValid &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
106788106991
pLevel->iFrom = (u8)bestJ;
106789106992
if( bestPlan.plan.nRow>=(double)1 ){
106790106993
pParse->nQueryLoop *= bestPlan.plan.nRow;
106791106994
}
106792106995
@@ -106814,12 +107017,12 @@
106814107017
}
106815107018
106816107019
/* If the total query only selects a single row, then the ORDER BY
106817107020
** clause is irrelevant.
106818107021
*/
106819
- if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
106820
- *ppOrderBy = 0;
107022
+ if( (andFlags & WHERE_UNIQUE)!=0 && pOrderBy ){
107023
+ pWInfo->nOBSat = pOrderBy->nExpr;
106821107024
}
106822107025
106823107026
/* If the caller is an UPDATE or DELETE statement that is requesting
106824107027
** to use a one-pass algorithm, determine if this is appropriate.
106825107028
** The one-pass algorithm only works if the WHERE clause constraints
@@ -106835,13 +107038,14 @@
106835107038
** searching those tables.
106836107039
*/
106837107040
sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
106838107041
notReady = ~(Bitmask)0;
106839107042
pWInfo->nRowOut = (double)1;
106840
- for(i=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
107043
+ for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
106841107044
Table *pTab; /* Table to open */
106842107045
int iDb; /* Index of database containing table/index */
107046
+ struct SrcList_item *pTabItem;
106843107047
106844107048
pTabItem = &pTabList->a[pLevel->iFrom];
106845107049
pTab = pTabItem->pTab;
106846107050
pLevel->iTabCur = pTabItem->iCursor;
106847107051
pWInfo->nRowOut *= pLevel->plan.nRow;
@@ -106873,11 +107077,11 @@
106873107077
}else{
106874107078
sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
106875107079
}
106876107080
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
106877107081
if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){
106878
- constructAutomaticIndex(pParse, pWC, pTabItem, notReady, pLevel);
107082
+ constructAutomaticIndex(pParse, sWBI.pWC, pTabItem, notReady, pLevel);
106879107083
}else
106880107084
#endif
106881107085
if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
106882107086
Index *pIx = pLevel->plan.u.pIdx;
106883107087
KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
@@ -106887,24 +107091,24 @@
106887107091
sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb,
106888107092
(char*)pKey, P4_KEYINFO_HANDOFF);
106889107093
VdbeComment((v, "%s", pIx->zName));
106890107094
}
106891107095
sqlite3CodeVerifySchema(pParse, iDb);
106892
- notReady &= ~getMask(pWC->pMaskSet, pTabItem->iCursor);
107096
+ notReady &= ~getMask(sWBI.pWC->pMaskSet, pTabItem->iCursor);
106893107097
}
106894107098
pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
106895107099
if( db->mallocFailed ) goto whereBeginError;
106896107100
106897107101
/* Generate the code to do the search. Each iteration of the for
106898107102
** loop below generates code for a single nested loop of the VM
106899107103
** program.
106900107104
*/
106901107105
notReady = ~(Bitmask)0;
106902
- for(i=0; i<nTabList; i++){
106903
- pLevel = &pWInfo->a[i];
106904
- explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags);
106905
- notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady);
107106
+ for(ii=0; ii<nTabList; ii++){
107107
+ pLevel = &pWInfo->a[ii];
107108
+ explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
107109
+ notReady = codeOneLoopStart(pWInfo, ii, wctrlFlags, notReady);
106906107110
pWInfo->iContinue = pLevel->addrCont;
106907107111
}
106908107112
106909107113
#ifdef SQLITE_TEST /* For testing and debugging use only */
106910107114
/* Record in the query plan information about the current table
@@ -106911,15 +107115,17 @@
106911107115
** and the index used to access it (if any). If the table itself
106912107116
** is not used, its name is just '{}'. If no index is used
106913107117
** the index is listed as "{}". If the primary key is used the
106914107118
** index name is '*'.
106915107119
*/
106916
- for(i=0; i<nTabList; i++){
107120
+ for(ii=0; ii<nTabList; ii++){
106917107121
char *z;
106918107122
int n;
106919107123
int w;
106920
- pLevel = &pWInfo->a[i];
107124
+ struct SrcList_item *pTabItem;
107125
+
107126
+ pLevel = &pWInfo->a[ii];
106921107127
w = pLevel->plan.wsFlags;
106922107128
pTabItem = &pTabList->a[pLevel->iFrom];
106923107129
z = pTabItem->zAlias;
106924107130
if( z==0 ) z = pTabItem->pTab->zName;
106925107131
n = sqlite3Strlen30(z);
@@ -112874,10 +113080,11 @@
112874113080
){
112875113081
sqlite3_mutex_enter(db->mutex);
112876113082
db->busyHandler.xFunc = xBusy;
112877113083
db->busyHandler.pArg = pArg;
112878113084
db->busyHandler.nBusy = 0;
113085
+ db->busyTimeout = 0;
112879113086
sqlite3_mutex_leave(db->mutex);
112880113087
return SQLITE_OK;
112881113088
}
112882113089
112883113090
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
@@ -112911,12 +113118,12 @@
112911113118
** This routine installs a default busy handler that waits for the
112912113119
** specified number of milliseconds before returning 0.
112913113120
*/
112914113121
SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
112915113122
if( ms>0 ){
112916
- db->busyTimeout = ms;
112917113123
sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
113124
+ db->busyTimeout = ms;
112918113125
}else{
112919113126
sqlite3_busy_handler(db, 0, 0);
112920113127
}
112921113128
return SQLITE_OK;
112922113129
}
@@ -114771,12 +114978,11 @@
114771114978
** with various optimizations disabled to verify that the same answer
114772114979
** is obtained in every case.
114773114980
*/
114774114981
case SQLITE_TESTCTRL_OPTIMIZATIONS: {
114775114982
sqlite3 *db = va_arg(ap, sqlite3*);
114776
- int x = va_arg(ap,int);
114777
- db->flags = (x & SQLITE_OptMask) | (db->flags & ~SQLITE_OptMask);
114983
+ db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
114778114984
break;
114779114985
}
114780114986
114781114987
#ifdef SQLITE_N_KEYWORD
114782114988
/* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
114783114989
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -673,11 +673,11 @@
673 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
674 ** [sqlite_version()] and [sqlite_source_id()].
675 */
676 #define SQLITE_VERSION "3.7.15"
677 #define SQLITE_VERSION_NUMBER 3007015
678 #define SQLITE_SOURCE_ID "2012-09-17 21:24:01 698b2a28004a9a2f0eabaadf36d833da4400b2bf"
679
680 /*
681 ** CAPI3REF: Run-Time Library Version Numbers
682 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
683 **
@@ -5315,10 +5315,13 @@
5315 ** successfully. An [error code] is returned otherwise.)^
5316 **
5317 ** ^Shared cache is disabled by default. But this might change in
5318 ** future releases of SQLite. Applications that care about shared
5319 ** cache setting should set it explicitly.
 
 
 
5320 **
5321 ** See Also: [SQLite Shared-Cache Mode]
5322 */
5323 SQLITE_API int sqlite3_enable_shared_cache(int);
5324
@@ -8281,10 +8284,11 @@
8281 typedef struct NameContext NameContext;
8282 typedef struct Parse Parse;
8283 typedef struct RowSet RowSet;
8284 typedef struct Savepoint Savepoint;
8285 typedef struct Select Select;
 
8286 typedef struct SrcList SrcList;
8287 typedef struct StrAccum StrAccum;
8288 typedef struct Table Table;
8289 typedef struct TableLock TableLock;
8290 typedef struct Token Token;
@@ -8887,11 +8891,11 @@
8887 #define OPFLG_IN3 0x0010 /* in3: P3 is an input */
8888 #define OPFLG_OUT2 0x0020 /* out2: P2 is an output */
8889 #define OPFLG_OUT3 0x0040 /* out3: P3 is an output */
8890 #define OPFLG_INITIALIZER {\
8891 /* 0 */ 0x00, 0x01, 0x01, 0x04, 0x04, 0x10, 0x00, 0x02,\
8892 /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
8893 /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
8894 /* 24 */ 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00,\
8895 /* 32 */ 0x02, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,\
8896 /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11,\
8897 /* 48 */ 0x11, 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02,\
@@ -9843,10 +9847,11 @@
9843 int flags; /* Miscellaneous flags. See below */
9844 i64 lastRowid; /* ROWID of most recent insert (see above) */
9845 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
9846 int errCode; /* Most recent error code (SQLITE_*) */
9847 int errMask; /* & result codes with this before returning */
 
9848 u8 autoCommit; /* The auto-commit flag. */
9849 u8 temp_store; /* 1: file 2: memory 0: default */
9850 u8 mallocFailed; /* True if we have seen a malloc failure */
9851 u8 dfltLockMode; /* Default locking-mode for attached dbs */
9852 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
@@ -9947,50 +9952,62 @@
9947 #define ENC(db) ((db)->aDb[0].pSchema->enc)
9948
9949 /*
9950 ** Possible values for the sqlite3.flags.
9951 */
9952 #define SQLITE_VdbeTrace 0x00000100 /* True to trace VDBE execution */
9953 #define SQLITE_InternChanges 0x00000200 /* Uncommitted Hash table changes */
9954 #define SQLITE_FullColNames 0x00000400 /* Show full column names on SELECT */
9955 #define SQLITE_ShortColNames 0x00000800 /* Show short columns names */
9956 #define SQLITE_CountRows 0x00001000 /* Count rows changed by INSERT, */
9957 /* DELETE, or UPDATE and return */
9958 /* the count using a callback. */
9959 #define SQLITE_NullCallback 0x00002000 /* Invoke the callback once if the */
9960 /* result set is empty */
9961 #define SQLITE_SqlTrace 0x00004000 /* Debug print SQL as it executes */
9962 #define SQLITE_VdbeListing 0x00008000 /* Debug listings of VDBE programs */
9963 #define SQLITE_WriteSchema 0x00010000 /* OK to update SQLITE_MASTER */
9964 /* 0x00020000 Unused */
9965 #define SQLITE_IgnoreChecks 0x00040000 /* Do not enforce check constraints */
9966 #define SQLITE_ReadUncommitted 0x0080000 /* For shared-cache mode */
9967 #define SQLITE_LegacyFileFmt 0x00100000 /* Create new databases in format 1 */
9968 #define SQLITE_FullFSync 0x00200000 /* Use full fsync on the backend */
9969 #define SQLITE_CkptFullFSync 0x00400000 /* Use full fsync for checkpoint */
9970 #define SQLITE_RecoveryMode 0x00800000 /* Ignore schema errors */
9971 #define SQLITE_ReverseOrder 0x01000000 /* Reverse unordered SELECTs */
9972 #define SQLITE_RecTriggers 0x02000000 /* Enable recursive triggers */
9973 #define SQLITE_ForeignKeys 0x04000000 /* Enforce foreign key constraints */
9974 #define SQLITE_AutoIndex 0x08000000 /* Enable automatic indexes */
9975 #define SQLITE_PreferBuiltin 0x10000000 /* Preference to built-in funcs */
9976 #define SQLITE_LoadExtension 0x20000000 /* Enable load_extension */
9977 #define SQLITE_EnableTrigger 0x40000000 /* True to enable triggers */
9978
9979 /*
9980 ** Bits of the sqlite3.flags field that are used by the
9981 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
9982 ** These must be the low-order bits of the flags field.
9983 */
9984 #define SQLITE_QueryFlattener 0x01 /* Disable query flattening */
9985 #define SQLITE_ColumnCache 0x02 /* Disable the column cache */
9986 #define SQLITE_GroupByOrder 0x04 /* Disable GROUPBY cover of ORDERBY */
9987 #define SQLITE_FactorOutConst 0x08 /* Disable factoring out constants */
9988 #define SQLITE_IdxRealAsInt 0x10 /* Store REAL as INT in indices */
9989 #define SQLITE_DistinctOpt 0x20 /* DISTINCT using indexes */
9990 #define SQLITE_CoverIdxScan 0x40 /* Disable covering index scans */
9991 #define SQLITE_OptMask 0xff /* Mask of all disablable opts */
 
 
 
 
 
 
 
 
 
 
 
 
9992
9993 /*
9994 ** Possible values for the sqlite.magic field.
9995 ** The numbers are obtained at random and have no special meaning, other
9996 ** than being distinct from one another.
@@ -10922,11 +10939,12 @@
10922 ** is only used when wsFlags&WHERE_VIRTUALTABLE is true. It is never the
10923 ** case that more than one of these conditions is true.
10924 */
10925 struct WherePlan {
10926 u32 wsFlags; /* WHERE_* flags that describe the strategy */
10927 u32 nEq; /* Number of == constraints */
 
10928 double nRow; /* Estimated number of rows (for EQP) */
10929 union {
10930 Index *pIdx; /* Index when WHERE_INDEXED is true */
10931 struct WhereTerm *pTerm; /* WHERE clause term for OR-search */
10932 sqlite3_index_info *pVtabIdx; /* Virtual table index to use */
@@ -10998,28 +11016,32 @@
10998 ** half does the tail of the WHERE loop. An instance of
10999 ** this structure is returned by the first half and passed
11000 ** into the second half to give some continuity.
11001 */
11002 struct WhereInfo {
11003 Parse *pParse; /* Parsing and code generating context */
11004 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
11005 u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE or DELETE */
11006 u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
11007 u8 eDistinct;
11008 SrcList *pTabList; /* List of tables in the join */
11009 int iTop; /* The very beginning of the WHERE loop */
11010 int iContinue; /* Jump here to continue with next record */
11011 int iBreak; /* Jump here to break out of the loop */
11012 int nLevel; /* Number of nested loop */
11013 struct WhereClause *pWC; /* Decomposition of the WHERE clause */
11014 double savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
11015 double nRowOut; /* Estimated number of output rows */
11016 WhereLevel a[1]; /* Information about each nest loop in WHERE */
 
11017 };
11018
11019 #define WHERE_DISTINCT_UNIQUE 1
11020 #define WHERE_DISTINCT_ORDERED 2
 
 
 
11021
11022 /*
11023 ** A NameContext defines a context in which to resolve table and column
11024 ** names. The context consists of a list of tables (the pSrcList) field and
11025 ** a list of named expression (pEList). The named expression list may
@@ -11074,17 +11096,16 @@
11074 ** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
11075 ** the number of columns in P2 can be computed at the same time
11076 ** as the OP_OpenEphm instruction is coded because not
11077 ** enough information about the compound query is known at that point.
11078 ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
11079 ** for the result set. The KeyInfo for addrOpenTran[2] contains collating
11080 ** sequences for the ORDER BY clause.
11081 */
11082 struct Select {
11083 ExprList *pEList; /* The fields of the result */
11084 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
11085 char affinity; /* MakeRecord with this affinity for SRT_Set */
11086 u16 selFlags; /* Various SF_* values */
11087 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
11088 int addrOpenEphm[3]; /* OP_OpenEphem opcodes related to this select */
11089 double nSelectRow; /* Estimated number of result rows */
11090 SrcList *pSrc; /* The FROM clause */
@@ -11131,17 +11152,16 @@
11131 #define SRT_Table 8 /* Store result as data with an automatic rowid */
11132 #define SRT_EphemTab 9 /* Create transient tab and store like SRT_Table */
11133 #define SRT_Coroutine 10 /* Generate a single row of result */
11134
11135 /*
11136 ** A structure used to customize the behavior of sqlite3Select(). See
11137 ** comments above sqlite3Select() for details.
11138 */
11139 typedef struct SelectDest SelectDest;
11140 struct SelectDest {
11141 u8 eDest; /* How to dispose of the results */
11142 u8 affSdst; /* Affinity used when eDest==SRT_Set */
11143 int iSDParm; /* A parameter used by the eDest disposal method */
11144 int iSdst; /* Base register where results are written */
11145 int nSdst; /* Number of registers allocated */
11146 };
11147
@@ -11826,17 +11846,15 @@
11826 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
11827 SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
11828 #endif
11829 SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
11830 SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
11831 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
11832 Parse*,SrcList*,Expr*,ExprList**,ExprList*,u16,int);
11833 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
11834 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
11835 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
11836 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
11837 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, int, int, int);
11838 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
11839 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
11840 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
11841 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
11842 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
@@ -13178,11 +13196,13 @@
13178 #define MEM_Real 0x0008 /* Value is a real number */
13179 #define MEM_Blob 0x0010 /* Value is a BLOB */
13180 #define MEM_RowSet 0x0020 /* Value is a RowSet object */
13181 #define MEM_Frame 0x0040 /* Value is a VdbeFrame object */
13182 #define MEM_Invalid 0x0080 /* Value is undefined */
13183 #define MEM_TypeMask 0x00ff /* Mask of type bits */
 
 
13184
13185 /* Whenever Mem contains a valid string or blob representation, one of
13186 ** the following flags must be set to determine the memory management
13187 ** policy for Mem.z. The MEM_Term flag tells us whether or not the
13188 ** string is \000 or \u0000 terminated
@@ -63693,10 +63713,11 @@
63693 struct OP_Yield_stack_vars {
63694 int pcDest;
63695 } aa;
63696 struct OP_Null_stack_vars {
63697 int cnt;
 
63698 } ab;
63699 struct OP_Variable_stack_vars {
63700 Mem *pVar; /* Value being transferred */
63701 } ac;
63702 struct OP_Move_stack_vars {
@@ -63703,60 +63724,63 @@
63703 char *zMalloc; /* Holding variable for allocated memory */
63704 int n; /* Number of registers left to copy */
63705 int p1; /* Register to copy from */
63706 int p2; /* Register to copy to */
63707 } ad;
 
 
 
63708 struct OP_ResultRow_stack_vars {
63709 Mem *pMem;
63710 int i;
63711 } ae;
63712 struct OP_Concat_stack_vars {
63713 i64 nByte;
63714 } af;
63715 struct OP_Remainder_stack_vars {
63716 int flags; /* Combined MEM_* flags from both inputs */
63717 i64 iA; /* Integer value of left operand */
63718 i64 iB; /* Integer value of right operand */
63719 double rA; /* Real value of left operand */
63720 double rB; /* Real value of right operand */
63721 } ag;
63722 struct OP_Function_stack_vars {
63723 int i;
63724 Mem *pArg;
63725 sqlite3_context ctx;
63726 sqlite3_value **apVal;
63727 int n;
63728 } ah;
63729 struct OP_ShiftRight_stack_vars {
63730 i64 iA;
63731 u64 uA;
63732 i64 iB;
63733 u8 op;
63734 } ai;
63735 struct OP_Ge_stack_vars {
63736 int res; /* Result of the comparison of pIn1 against pIn3 */
63737 char affinity; /* Affinity to use for comparison */
63738 u16 flags1; /* Copy of initial value of pIn1->flags */
63739 u16 flags3; /* Copy of initial value of pIn3->flags */
63740 } aj;
63741 struct OP_Compare_stack_vars {
63742 int n;
63743 int i;
63744 int p1;
63745 int p2;
63746 const KeyInfo *pKeyInfo;
63747 int idx;
63748 CollSeq *pColl; /* Collating sequence to use on this term */
63749 int bRev; /* True for DESCENDING sort order */
63750 } ak;
63751 struct OP_Or_stack_vars {
63752 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
63753 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
63754 } al;
63755 struct OP_IfNot_stack_vars {
63756 int c;
63757 } am;
63758 struct OP_Column_stack_vars {
63759 u32 payloadSize; /* Number of bytes in the record */
63760 i64 payloadSize64; /* Number of bytes in the record */
63761 int p1; /* P1 value of the opcode */
63762 int p2; /* column number to retrieve */
@@ -63777,15 +63801,15 @@
63777 u32 szField; /* Number of bytes in the content of a field */
63778 int szHdr; /* Size of the header size field at start of record */
63779 int avail; /* Number of bytes of available data */
63780 u32 t; /* A type code from the record header */
63781 Mem *pReg; /* PseudoTable input register */
63782 } an;
63783 struct OP_Affinity_stack_vars {
63784 const char *zAffinity; /* The affinity to be applied */
63785 char cAff; /* A single character of affinity */
63786 } ao;
63787 struct OP_MakeRecord_stack_vars {
63788 u8 *zNewRecord; /* A buffer to hold the data for the new record */
63789 Mem *pRec; /* The new record */
63790 u64 nData; /* Number of bytes of data space */
63791 int nHdr; /* Number of bytes of header space */
@@ -63798,108 +63822,108 @@
63798 int nField; /* Number of fields in the record */
63799 char *zAffinity; /* The affinity string for the record */
63800 int file_format; /* File format to use for encoding */
63801 int i; /* Space used in zNewRecord[] */
63802 int len; /* Length of a field */
63803 } ap;
63804 struct OP_Count_stack_vars {
63805 i64 nEntry;
63806 BtCursor *pCrsr;
63807 } aq;
63808 struct OP_Savepoint_stack_vars {
63809 int p1; /* Value of P1 operand */
63810 char *zName; /* Name of savepoint */
63811 int nName;
63812 Savepoint *pNew;
63813 Savepoint *pSavepoint;
63814 Savepoint *pTmp;
63815 int iSavepoint;
63816 int ii;
63817 } ar;
63818 struct OP_AutoCommit_stack_vars {
63819 int desiredAutoCommit;
63820 int iRollback;
63821 int turnOnAC;
63822 } as;
63823 struct OP_Transaction_stack_vars {
63824 Btree *pBt;
63825 } at;
63826 struct OP_ReadCookie_stack_vars {
63827 int iMeta;
63828 int iDb;
63829 int iCookie;
63830 } au;
63831 struct OP_SetCookie_stack_vars {
63832 Db *pDb;
63833 } av;
63834 struct OP_VerifyCookie_stack_vars {
63835 int iMeta;
63836 int iGen;
63837 Btree *pBt;
63838 } aw;
63839 struct OP_OpenWrite_stack_vars {
63840 int nField;
63841 KeyInfo *pKeyInfo;
63842 int p2;
63843 int iDb;
63844 int wrFlag;
63845 Btree *pX;
63846 VdbeCursor *pCur;
63847 Db *pDb;
63848 } ax;
63849 struct OP_OpenEphemeral_stack_vars {
63850 VdbeCursor *pCx;
63851 } ay;
63852 struct OP_SorterOpen_stack_vars {
63853 VdbeCursor *pCx;
63854 } az;
63855 struct OP_OpenPseudo_stack_vars {
63856 VdbeCursor *pCx;
63857 } ba;
63858 struct OP_SeekGt_stack_vars {
63859 int res;
63860 int oc;
63861 VdbeCursor *pC;
63862 UnpackedRecord r;
63863 int nField;
63864 i64 iKey; /* The rowid we are to seek to */
63865 } bb;
63866 struct OP_Seek_stack_vars {
63867 VdbeCursor *pC;
63868 } bc;
63869 struct OP_Found_stack_vars {
63870 int alreadyExists;
63871 VdbeCursor *pC;
63872 int res;
63873 char *pFree;
63874 UnpackedRecord *pIdxKey;
63875 UnpackedRecord r;
63876 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
63877 } bd;
63878 struct OP_IsUnique_stack_vars {
63879 u16 ii;
63880 VdbeCursor *pCx;
63881 BtCursor *pCrsr;
63882 u16 nField;
63883 Mem *aMx;
63884 UnpackedRecord r; /* B-Tree index search key */
63885 i64 R; /* Rowid stored in register P3 */
63886 } be;
63887 struct OP_NotExists_stack_vars {
63888 VdbeCursor *pC;
63889 BtCursor *pCrsr;
63890 int res;
63891 u64 iKey;
63892 } bf;
63893 struct OP_NewRowid_stack_vars {
63894 i64 v; /* The new rowid */
63895 VdbeCursor *pC; /* Cursor of table to get the new rowid */
63896 int res; /* Result of an sqlite3BtreeLast() */
63897 int cnt; /* Counter to limit the number of searches */
63898 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
63899 VdbeFrame *pFrame; /* Root frame of VDBE */
63900 } bg;
63901 struct OP_InsertInt_stack_vars {
63902 Mem *pData; /* MEM cell holding data for the record to be inserted */
63903 Mem *pKey; /* MEM cell holding key for the record */
63904 i64 iKey; /* The integer ROWID or key for the record to be inserted */
63905 VdbeCursor *pC; /* Cursor to table into which insert is written */
@@ -63906,160 +63930,163 @@
63906 int nZero; /* Number of zero-bytes to append */
63907 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
63908 const char *zDb; /* database name - used by the update hook */
63909 const char *zTbl; /* Table name - used by the opdate hook */
63910 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
63911 } bh;
63912 struct OP_Delete_stack_vars {
63913 i64 iKey;
63914 VdbeCursor *pC;
63915 } bi;
63916 struct OP_SorterCompare_stack_vars {
63917 VdbeCursor *pC;
63918 int res;
63919 } bj;
63920 struct OP_SorterData_stack_vars {
63921 VdbeCursor *pC;
63922 } bk;
63923 struct OP_RowData_stack_vars {
63924 VdbeCursor *pC;
63925 BtCursor *pCrsr;
63926 u32 n;
63927 i64 n64;
63928 } bl;
63929 struct OP_Rowid_stack_vars {
63930 VdbeCursor *pC;
63931 i64 v;
63932 sqlite3_vtab *pVtab;
63933 const sqlite3_module *pModule;
63934 } bm;
63935 struct OP_NullRow_stack_vars {
63936 VdbeCursor *pC;
63937 } bn;
63938 struct OP_Last_stack_vars {
63939 VdbeCursor *pC;
63940 BtCursor *pCrsr;
63941 int res;
63942 } bo;
63943 struct OP_Rewind_stack_vars {
63944 VdbeCursor *pC;
63945 BtCursor *pCrsr;
63946 int res;
63947 } bp;
63948 struct OP_Next_stack_vars {
63949 VdbeCursor *pC;
63950 int res;
63951 } bq;
63952 struct OP_IdxInsert_stack_vars {
63953 VdbeCursor *pC;
63954 BtCursor *pCrsr;
63955 int nKey;
63956 const char *zKey;
63957 } br;
63958 struct OP_IdxDelete_stack_vars {
63959 VdbeCursor *pC;
63960 BtCursor *pCrsr;
63961 int res;
63962 UnpackedRecord r;
63963 } bs;
63964 struct OP_IdxRowid_stack_vars {
63965 BtCursor *pCrsr;
63966 VdbeCursor *pC;
63967 i64 rowid;
63968 } bt;
63969 struct OP_IdxGE_stack_vars {
63970 VdbeCursor *pC;
63971 int res;
63972 UnpackedRecord r;
63973 } bu;
63974 struct OP_Destroy_stack_vars {
63975 int iMoved;
63976 int iCnt;
63977 Vdbe *pVdbe;
63978 int iDb;
63979 } bv;
63980 struct OP_Clear_stack_vars {
63981 int nChange;
63982 } bw;
63983 struct OP_CreateTable_stack_vars {
63984 int pgno;
63985 int flags;
63986 Db *pDb;
63987 } bx;
63988 struct OP_ParseSchema_stack_vars {
63989 int iDb;
63990 const char *zMaster;
63991 char *zSql;
63992 InitData initData;
63993 } by;
63994 struct OP_IntegrityCk_stack_vars {
63995 int nRoot; /* Number of tables to check. (Number of root pages.) */
63996 int *aRoot; /* Array of rootpage numbers for tables to be checked */
63997 int j; /* Loop counter */
63998 int nErr; /* Number of errors reported */
63999 char *z; /* Text of the error report */
64000 Mem *pnErr; /* Register keeping track of errors remaining */
64001 } bz;
64002 struct OP_RowSetRead_stack_vars {
64003 i64 val;
64004 } ca;
64005 struct OP_RowSetTest_stack_vars {
64006 int iSet;
64007 int exists;
64008 } cb;
64009 struct OP_Program_stack_vars {
64010 int nMem; /* Number of memory registers for sub-program */
64011 int nByte; /* Bytes of runtime space required for sub-program */
64012 Mem *pRt; /* Register to allocate runtime space */
64013 Mem *pMem; /* Used to iterate through memory cells */
64014 Mem *pEnd; /* Last memory cell in new array */
64015 VdbeFrame *pFrame; /* New vdbe frame to execute in */
64016 SubProgram *pProgram; /* Sub-program to execute */
64017 void *t; /* Token identifying trigger */
64018 } cc;
64019 struct OP_Param_stack_vars {
64020 VdbeFrame *pFrame;
64021 Mem *pIn;
64022 } cd;
64023 struct OP_MemMax_stack_vars {
64024 Mem *pIn1;
64025 VdbeFrame *pFrame;
64026 } ce;
64027 struct OP_AggStep_stack_vars {
64028 int n;
64029 int i;
64030 Mem *pMem;
64031 Mem *pRec;
64032 sqlite3_context ctx;
64033 sqlite3_value **apVal;
64034 } cf;
64035 struct OP_AggFinal_stack_vars {
64036 Mem *pMem;
64037 } cg;
64038 struct OP_Checkpoint_stack_vars {
64039 int i; /* Loop counter */
64040 int aRes[3]; /* Results */
64041 Mem *pMem; /* Write results here */
64042 } ch;
64043 struct OP_JournalMode_stack_vars {
64044 Btree *pBt; /* Btree to change journal mode of */
64045 Pager *pPager; /* Pager associated with pBt */
64046 int eNew; /* New journal mode */
64047 int eOld; /* The old journal mode */
64048 } ci;
 
 
 
64049 struct OP_IncrVacuum_stack_vars {
64050 Btree *pBt;
64051 } cj;
64052 struct OP_VBegin_stack_vars {
64053 VTable *pVTab;
64054 } ck;
64055 struct OP_VOpen_stack_vars {
64056 VdbeCursor *pCur;
64057 sqlite3_vtab_cursor *pVtabCursor;
64058 sqlite3_vtab *pVtab;
64059 sqlite3_module *pModule;
64060 } cl;
64061 struct OP_VFilter_stack_vars {
64062 int nArg;
64063 int iQuery;
64064 const sqlite3_module *pModule;
64065 Mem *pQuery;
@@ -64068,40 +64095,40 @@
64068 sqlite3_vtab *pVtab;
64069 VdbeCursor *pCur;
64070 int res;
64071 int i;
64072 Mem **apArg;
64073 } cm;
64074 struct OP_VColumn_stack_vars {
64075 sqlite3_vtab *pVtab;
64076 const sqlite3_module *pModule;
64077 Mem *pDest;
64078 sqlite3_context sContext;
64079 } cn;
64080 struct OP_VNext_stack_vars {
64081 sqlite3_vtab *pVtab;
64082 const sqlite3_module *pModule;
64083 int res;
64084 VdbeCursor *pCur;
64085 } co;
64086 struct OP_VRename_stack_vars {
64087 sqlite3_vtab *pVtab;
64088 Mem *pName;
64089 } cp;
64090 struct OP_VUpdate_stack_vars {
64091 sqlite3_vtab *pVtab;
64092 sqlite3_module *pModule;
64093 int nArg;
64094 int i;
64095 sqlite_int64 rowid;
64096 Mem **apArg;
64097 Mem *pX;
64098 } cq;
64099 struct OP_Trace_stack_vars {
64100 char *zTrace;
64101 char *z;
64102 } cr;
64103 } u;
64104 /* End automatically generated code
64105 ********************************************************************/
64106
64107 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -64488,29 +64515,34 @@
64488 pOut->enc = encoding;
64489 UPDATE_MAX_BLOBSIZE(pOut);
64490 break;
64491 }
64492
64493 /* Opcode: Null * P2 P3 * *
64494 **
64495 ** Write a NULL into registers P2. If P3 greater than P2, then also write
64496 ** NULL into register P3 and ever register in between P2 and P3. If P3
64497 ** is less than P2 (typically P3 is zero) then only register P2 is
64498 ** set to NULL
 
 
 
 
64499 */
64500 case OP_Null: { /* out2-prerelease */
64501 #if 0 /* local variables moved into u.ab */
64502 int cnt;
 
64503 #endif /* local variables moved into u.ab */
64504 u.ab.cnt = pOp->p3-pOp->p2;
64505 assert( pOp->p3<=p->nMem );
64506 pOut->flags = MEM_Null;
64507 while( u.ab.cnt>0 ){
64508 pOut++;
64509 memAboutToChange(p, pOut);
64510 VdbeMemRelease(pOut);
64511 pOut->flags = MEM_Null;
64512 u.ab.cnt--;
64513 }
64514 break;
64515 }
64516
@@ -64551,24 +64583,24 @@
64551 break;
64552 }
64553
64554 /* Opcode: Move P1 P2 P3 * *
64555 **
64556 ** Move the values in register P1..P1+P3-1 over into
64557 ** registers P2..P2+P3-1. Registers P1..P1+P1-1 are
64558 ** left holding a NULL. It is an error for register ranges
64559 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
64560 */
64561 case OP_Move: {
64562 #if 0 /* local variables moved into u.ad */
64563 char *zMalloc; /* Holding variable for allocated memory */
64564 int n; /* Number of registers left to copy */
64565 int p1; /* Register to copy from */
64566 int p2; /* Register to copy to */
64567 #endif /* local variables moved into u.ad */
64568
64569 u.ad.n = pOp->p3;
64570 u.ad.p1 = pOp->p1;
64571 u.ad.p2 = pOp->p2;
64572 assert( u.ad.n>0 && u.ad.p1>0 && u.ad.p2>0 );
64573 assert( u.ad.p1+u.ad.n<=u.ad.p2 || u.ad.p2+u.ad.n<=u.ad.p1 );
64574
@@ -64593,24 +64625,34 @@
64593 pOut++;
64594 }
64595 break;
64596 }
64597
64598 /* Opcode: Copy P1 P2 * * *
64599 **
64600 ** Make a copy of register P1 into register P2.
64601 **
64602 ** This instruction makes a deep copy of the value. A duplicate
64603 ** is made of any string or blob constant. See also OP_SCopy.
64604 */
64605 case OP_Copy: { /* in1, out2 */
 
 
 
 
 
64606 pIn1 = &aMem[pOp->p1];
64607 pOut = &aMem[pOp->p2];
64608 assert( pOut!=pIn1 );
64609 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
64610 Deephemeralize(pOut);
64611 REGISTER_TRACE(pOp->p2, pOut);
 
 
 
 
 
64612 break;
64613 }
64614
64615 /* Opcode: SCopy P1 P2 * * *
64616 **
@@ -64643,14 +64685,14 @@
64643 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
64644 ** structure to provide access to the top P1 values as the result
64645 ** row.
64646 */
64647 case OP_ResultRow: {
64648 #if 0 /* local variables moved into u.ae */
64649 Mem *pMem;
64650 int i;
64651 #endif /* local variables moved into u.ae */
64652 assert( p->nResColumn==pOp->p2 );
64653 assert( pOp->p1>0 );
64654 assert( pOp->p1+pOp->p2<=p->nMem+1 );
64655
64656 /* If this statement has violated immediate foreign key constraints, do
@@ -64688,19 +64730,19 @@
64688
64689 /* Make sure the results of the current row are \000 terminated
64690 ** and have an assigned type. The results are de-ephemeralized as
64691 ** a side effect.
64692 */
64693 u.ae.pMem = p->pResultSet = &aMem[pOp->p1];
64694 for(u.ae.i=0; u.ae.i<pOp->p2; u.ae.i++){
64695 assert( memIsValid(&u.ae.pMem[u.ae.i]) );
64696 Deephemeralize(&u.ae.pMem[u.ae.i]);
64697 assert( (u.ae.pMem[u.ae.i].flags & MEM_Ephem)==0
64698 || (u.ae.pMem[u.ae.i].flags & (MEM_Str|MEM_Blob))==0 );
64699 sqlite3VdbeMemNulTerminate(&u.ae.pMem[u.ae.i]);
64700 sqlite3VdbeMemStoreType(&u.ae.pMem[u.ae.i]);
64701 REGISTER_TRACE(pOp->p1+u.ae.i, &u.ae.pMem[u.ae.i]);
64702 }
64703 if( db->mallocFailed ) goto no_mem;
64704
64705 /* Return SQLITE_ROW
64706 */
@@ -64720,13 +64762,13 @@
64720 ** It is illegal for P1 and P3 to be the same register. Sometimes,
64721 ** if P3 is the same register as P2, the implementation is able
64722 ** to avoid a memcpy().
64723 */
64724 case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
64725 #if 0 /* local variables moved into u.af */
64726 i64 nByte;
64727 #endif /* local variables moved into u.af */
64728
64729 pIn1 = &aMem[pOp->p1];
64730 pIn2 = &aMem[pOp->p2];
64731 pOut = &aMem[pOp->p3];
64732 assert( pIn1!=pOut );
@@ -64735,26 +64777,26 @@
64735 break;
64736 }
64737 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
64738 Stringify(pIn1, encoding);
64739 Stringify(pIn2, encoding);
64740 u.af.nByte = pIn1->n + pIn2->n;
64741 if( u.af.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
64742 goto too_big;
64743 }
64744 MemSetTypeFlag(pOut, MEM_Str);
64745 if( sqlite3VdbeMemGrow(pOut, (int)u.af.nByte+2, pOut==pIn2) ){
64746 goto no_mem;
64747 }
64748 if( pOut!=pIn2 ){
64749 memcpy(pOut->z, pIn2->z, pIn2->n);
64750 }
64751 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
64752 pOut->z[u.af.nByte] = 0;
64753 pOut->z[u.af.nByte+1] = 0;
64754 pOut->flags |= MEM_Term;
64755 pOut->n = (int)u.af.nByte;
64756 pOut->enc = encoding;
64757 UPDATE_MAX_BLOBSIZE(pOut);
64758 break;
64759 }
64760
@@ -64794,80 +64836,80 @@
64794 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
64795 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
64796 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
64797 case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
64798 case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
64799 #if 0 /* local variables moved into u.ag */
64800 int flags; /* Combined MEM_* flags from both inputs */
64801 i64 iA; /* Integer value of left operand */
64802 i64 iB; /* Integer value of right operand */
64803 double rA; /* Real value of left operand */
64804 double rB; /* Real value of right operand */
64805 #endif /* local variables moved into u.ag */
64806
64807 pIn1 = &aMem[pOp->p1];
64808 applyNumericAffinity(pIn1);
64809 pIn2 = &aMem[pOp->p2];
64810 applyNumericAffinity(pIn2);
64811 pOut = &aMem[pOp->p3];
64812 u.ag.flags = pIn1->flags | pIn2->flags;
64813 if( (u.ag.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
64814 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
64815 u.ag.iA = pIn1->u.i;
64816 u.ag.iB = pIn2->u.i;
64817 switch( pOp->opcode ){
64818 case OP_Add: if( sqlite3AddInt64(&u.ag.iB,u.ag.iA) ) goto fp_math; break;
64819 case OP_Subtract: if( sqlite3SubInt64(&u.ag.iB,u.ag.iA) ) goto fp_math; break;
64820 case OP_Multiply: if( sqlite3MulInt64(&u.ag.iB,u.ag.iA) ) goto fp_math; break;
64821 case OP_Divide: {
64822 if( u.ag.iA==0 ) goto arithmetic_result_is_null;
64823 if( u.ag.iA==-1 && u.ag.iB==SMALLEST_INT64 ) goto fp_math;
64824 u.ag.iB /= u.ag.iA;
64825 break;
64826 }
64827 default: {
64828 if( u.ag.iA==0 ) goto arithmetic_result_is_null;
64829 if( u.ag.iA==-1 ) u.ag.iA = 1;
64830 u.ag.iB %= u.ag.iA;
64831 break;
64832 }
64833 }
64834 pOut->u.i = u.ag.iB;
64835 MemSetTypeFlag(pOut, MEM_Int);
64836 }else{
64837 fp_math:
64838 u.ag.rA = sqlite3VdbeRealValue(pIn1);
64839 u.ag.rB = sqlite3VdbeRealValue(pIn2);
64840 switch( pOp->opcode ){
64841 case OP_Add: u.ag.rB += u.ag.rA; break;
64842 case OP_Subtract: u.ag.rB -= u.ag.rA; break;
64843 case OP_Multiply: u.ag.rB *= u.ag.rA; break;
64844 case OP_Divide: {
64845 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
64846 if( u.ag.rA==(double)0 ) goto arithmetic_result_is_null;
64847 u.ag.rB /= u.ag.rA;
64848 break;
64849 }
64850 default: {
64851 u.ag.iA = (i64)u.ag.rA;
64852 u.ag.iB = (i64)u.ag.rB;
64853 if( u.ag.iA==0 ) goto arithmetic_result_is_null;
64854 if( u.ag.iA==-1 ) u.ag.iA = 1;
64855 u.ag.rB = (double)(u.ag.iB % u.ag.iA);
64856 break;
64857 }
64858 }
64859 #ifdef SQLITE_OMIT_FLOATING_POINT
64860 pOut->u.i = u.ag.rB;
64861 MemSetTypeFlag(pOut, MEM_Int);
64862 #else
64863 if( sqlite3IsNaN(u.ag.rB) ){
64864 goto arithmetic_result_is_null;
64865 }
64866 pOut->r = u.ag.rB;
64867 MemSetTypeFlag(pOut, MEM_Real);
64868 if( (u.ag.flags & MEM_Real)==0 ){
64869 sqlite3VdbeIntegerAffinity(pOut);
64870 }
64871 #endif
64872 }
64873 break;
@@ -64915,96 +64957,96 @@
64915 ** invocation of this opcode.
64916 **
64917 ** See also: AggStep and AggFinal
64918 */
64919 case OP_Function: {
64920 #if 0 /* local variables moved into u.ah */
64921 int i;
64922 Mem *pArg;
64923 sqlite3_context ctx;
64924 sqlite3_value **apVal;
64925 int n;
64926 #endif /* local variables moved into u.ah */
64927
64928 u.ah.n = pOp->p5;
64929 u.ah.apVal = p->apArg;
64930 assert( u.ah.apVal || u.ah.n==0 );
64931 assert( pOp->p3>0 && pOp->p3<=p->nMem );
64932 pOut = &aMem[pOp->p3];
64933 memAboutToChange(p, pOut);
64934
64935 assert( u.ah.n==0 || (pOp->p2>0 && pOp->p2+u.ah.n<=p->nMem+1) );
64936 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ah.n );
64937 u.ah.pArg = &aMem[pOp->p2];
64938 for(u.ah.i=0; u.ah.i<u.ah.n; u.ah.i++, u.ah.pArg++){
64939 assert( memIsValid(u.ah.pArg) );
64940 u.ah.apVal[u.ah.i] = u.ah.pArg;
64941 Deephemeralize(u.ah.pArg);
64942 sqlite3VdbeMemStoreType(u.ah.pArg);
64943 REGISTER_TRACE(pOp->p2+u.ah.i, u.ah.pArg);
64944 }
64945
64946 assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
64947 if( pOp->p4type==P4_FUNCDEF ){
64948 u.ah.ctx.pFunc = pOp->p4.pFunc;
64949 u.ah.ctx.pVdbeFunc = 0;
64950 }else{
64951 u.ah.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
64952 u.ah.ctx.pFunc = u.ah.ctx.pVdbeFunc->pFunc;
64953 }
64954
64955 u.ah.ctx.s.flags = MEM_Null;
64956 u.ah.ctx.s.db = db;
64957 u.ah.ctx.s.xDel = 0;
64958 u.ah.ctx.s.zMalloc = 0;
64959
64960 /* The output cell may already have a buffer allocated. Move
64961 ** the pointer to u.ah.ctx.s so in case the user-function can use
64962 ** the already allocated buffer instead of allocating a new one.
64963 */
64964 sqlite3VdbeMemMove(&u.ah.ctx.s, pOut);
64965 MemSetTypeFlag(&u.ah.ctx.s, MEM_Null);
64966
64967 u.ah.ctx.isError = 0;
64968 if( u.ah.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
64969 assert( pOp>aOp );
64970 assert( pOp[-1].p4type==P4_COLLSEQ );
64971 assert( pOp[-1].opcode==OP_CollSeq );
64972 u.ah.ctx.pColl = pOp[-1].p4.pColl;
64973 }
64974 db->lastRowid = lastRowid;
64975 (*u.ah.ctx.pFunc->xFunc)(&u.ah.ctx, u.ah.n, u.ah.apVal); /* IMP: R-24505-23230 */
64976 lastRowid = db->lastRowid;
64977
64978 /* If any auxiliary data functions have been called by this user function,
64979 ** immediately call the destructor for any non-static values.
64980 */
64981 if( u.ah.ctx.pVdbeFunc ){
64982 sqlite3VdbeDeleteAuxData(u.ah.ctx.pVdbeFunc, pOp->p1);
64983 pOp->p4.pVdbeFunc = u.ah.ctx.pVdbeFunc;
64984 pOp->p4type = P4_VDBEFUNC;
64985 }
64986
64987 if( db->mallocFailed ){
64988 /* Even though a malloc() has failed, the implementation of the
64989 ** user function may have called an sqlite3_result_XXX() function
64990 ** to return a value. The following call releases any resources
64991 ** associated with such a value.
64992 */
64993 sqlite3VdbeMemRelease(&u.ah.ctx.s);
64994 goto no_mem;
64995 }
64996
64997 /* If the function returned an error, throw an exception */
64998 if( u.ah.ctx.isError ){
64999 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ah.ctx.s));
65000 rc = u.ah.ctx.isError;
65001 }
65002
65003 /* Copy the result of the function into register P3 */
65004 sqlite3VdbeChangeEncoding(&u.ah.ctx.s, encoding);
65005 sqlite3VdbeMemMove(pOut, &u.ah.ctx.s);
65006 if( sqlite3VdbeMemTooBig(pOut) ){
65007 goto too_big;
65008 }
65009
65010 #if 0
@@ -65048,56 +65090,56 @@
65048 */
65049 case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
65050 case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
65051 case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
65052 case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
65053 #if 0 /* local variables moved into u.ai */
65054 i64 iA;
65055 u64 uA;
65056 i64 iB;
65057 u8 op;
65058 #endif /* local variables moved into u.ai */
65059
65060 pIn1 = &aMem[pOp->p1];
65061 pIn2 = &aMem[pOp->p2];
65062 pOut = &aMem[pOp->p3];
65063 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
65064 sqlite3VdbeMemSetNull(pOut);
65065 break;
65066 }
65067 u.ai.iA = sqlite3VdbeIntValue(pIn2);
65068 u.ai.iB = sqlite3VdbeIntValue(pIn1);
65069 u.ai.op = pOp->opcode;
65070 if( u.ai.op==OP_BitAnd ){
65071 u.ai.iA &= u.ai.iB;
65072 }else if( u.ai.op==OP_BitOr ){
65073 u.ai.iA |= u.ai.iB;
65074 }else if( u.ai.iB!=0 ){
65075 assert( u.ai.op==OP_ShiftRight || u.ai.op==OP_ShiftLeft );
65076
65077 /* If shifting by a negative amount, shift in the other direction */
65078 if( u.ai.iB<0 ){
65079 assert( OP_ShiftRight==OP_ShiftLeft+1 );
65080 u.ai.op = 2*OP_ShiftLeft + 1 - u.ai.op;
65081 u.ai.iB = u.ai.iB>(-64) ? -u.ai.iB : 64;
65082 }
65083
65084 if( u.ai.iB>=64 ){
65085 u.ai.iA = (u.ai.iA>=0 || u.ai.op==OP_ShiftLeft) ? 0 : -1;
65086 }else{
65087 memcpy(&u.ai.uA, &u.ai.iA, sizeof(u.ai.uA));
65088 if( u.ai.op==OP_ShiftLeft ){
65089 u.ai.uA <<= u.ai.iB;
65090 }else{
65091 u.ai.uA >>= u.ai.iB;
65092 /* Sign-extend on a right shift of a negative number */
65093 if( u.ai.iA<0 ) u.ai.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.ai.iB);
65094 }
65095 memcpy(&u.ai.iA, &u.ai.uA, sizeof(u.ai.iA));
65096 }
65097 }
65098 pOut->u.i = u.ai.iA;
65099 MemSetTypeFlag(pOut, MEM_Int);
65100 break;
65101 }
65102
65103 /* Opcode: AddImm P1 P2 * * *
@@ -65285,10 +65327,14 @@
65285 ** are of different types, then numbers are considered less than
65286 ** strings and strings are considered less than blobs.
65287 **
65288 ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
65289 ** store a boolean result (either 0, or 1, or NULL) in register P2.
 
 
 
 
65290 */
65291 /* Opcode: Ne P1 P2 P3 P4 P5
65292 **
65293 ** This works just like the Lt opcode except that the jump is taken if
65294 ** the operands in registers P1 and P3 are not equal. See the Lt opcode for
@@ -65334,30 +65380,38 @@
65334 case OP_Ne: /* same as TK_NE, jump, in1, in3 */
65335 case OP_Lt: /* same as TK_LT, jump, in1, in3 */
65336 case OP_Le: /* same as TK_LE, jump, in1, in3 */
65337 case OP_Gt: /* same as TK_GT, jump, in1, in3 */
65338 case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
65339 #if 0 /* local variables moved into u.aj */
65340 int res; /* Result of the comparison of pIn1 against pIn3 */
65341 char affinity; /* Affinity to use for comparison */
65342 u16 flags1; /* Copy of initial value of pIn1->flags */
65343 u16 flags3; /* Copy of initial value of pIn3->flags */
65344 #endif /* local variables moved into u.aj */
65345
65346 pIn1 = &aMem[pOp->p1];
65347 pIn3 = &aMem[pOp->p3];
65348 u.aj.flags1 = pIn1->flags;
65349 u.aj.flags3 = pIn3->flags;
65350 if( (u.aj.flags1 | u.aj.flags3)&MEM_Null ){
65351 /* One or both operands are NULL */
65352 if( pOp->p5 & SQLITE_NULLEQ ){
65353 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
65354 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
65355 ** or not both operands are null.
65356 */
65357 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
65358 u.aj.res = (u.aj.flags1 & u.aj.flags3 & MEM_Null)==0;
 
 
 
 
 
 
 
 
65359 }else{
65360 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
65361 ** then the result is always NULL.
65362 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
65363 */
@@ -65370,44 +65424,44 @@
65370 }
65371 break;
65372 }
65373 }else{
65374 /* Neither operand is NULL. Do a comparison. */
65375 u.aj.affinity = pOp->p5 & SQLITE_AFF_MASK;
65376 if( u.aj.affinity ){
65377 applyAffinity(pIn1, u.aj.affinity, encoding);
65378 applyAffinity(pIn3, u.aj.affinity, encoding);
65379 if( db->mallocFailed ) goto no_mem;
65380 }
65381
65382 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
65383 ExpandBlob(pIn1);
65384 ExpandBlob(pIn3);
65385 u.aj.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
65386 }
65387 switch( pOp->opcode ){
65388 case OP_Eq: u.aj.res = u.aj.res==0; break;
65389 case OP_Ne: u.aj.res = u.aj.res!=0; break;
65390 case OP_Lt: u.aj.res = u.aj.res<0; break;
65391 case OP_Le: u.aj.res = u.aj.res<=0; break;
65392 case OP_Gt: u.aj.res = u.aj.res>0; break;
65393 default: u.aj.res = u.aj.res>=0; break;
65394 }
65395
65396 if( pOp->p5 & SQLITE_STOREP2 ){
65397 pOut = &aMem[pOp->p2];
65398 memAboutToChange(p, pOut);
65399 MemSetTypeFlag(pOut, MEM_Int);
65400 pOut->u.i = u.aj.res;
65401 REGISTER_TRACE(pOp->p2, pOut);
65402 }else if( u.aj.res ){
65403 pc = pOp->p2-1;
65404 }
65405
65406 /* Undo any changes made by applyAffinity() to the input registers. */
65407 pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.aj.flags1&MEM_TypeMask);
65408 pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.aj.flags3&MEM_TypeMask);
65409 break;
65410 }
65411
65412 /* Opcode: Permutation * * * P4 *
65413 **
@@ -65438,50 +65492,50 @@
65438 ** The comparison is a sort comparison, so NULLs compare equal,
65439 ** NULLs are less than numbers, numbers are less than strings,
65440 ** and strings are less than blobs.
65441 */
65442 case OP_Compare: {
65443 #if 0 /* local variables moved into u.ak */
65444 int n;
65445 int i;
65446 int p1;
65447 int p2;
65448 const KeyInfo *pKeyInfo;
65449 int idx;
65450 CollSeq *pColl; /* Collating sequence to use on this term */
65451 int bRev; /* True for DESCENDING sort order */
65452 #endif /* local variables moved into u.ak */
65453
65454 u.ak.n = pOp->p3;
65455 u.ak.pKeyInfo = pOp->p4.pKeyInfo;
65456 assert( u.ak.n>0 );
65457 assert( u.ak.pKeyInfo!=0 );
65458 u.ak.p1 = pOp->p1;
65459 u.ak.p2 = pOp->p2;
65460 #if SQLITE_DEBUG
65461 if( aPermute ){
65462 int k, mx = 0;
65463 for(k=0; k<u.ak.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
65464 assert( u.ak.p1>0 && u.ak.p1+mx<=p->nMem+1 );
65465 assert( u.ak.p2>0 && u.ak.p2+mx<=p->nMem+1 );
65466 }else{
65467 assert( u.ak.p1>0 && u.ak.p1+u.ak.n<=p->nMem+1 );
65468 assert( u.ak.p2>0 && u.ak.p2+u.ak.n<=p->nMem+1 );
65469 }
65470 #endif /* SQLITE_DEBUG */
65471 for(u.ak.i=0; u.ak.i<u.ak.n; u.ak.i++){
65472 u.ak.idx = aPermute ? aPermute[u.ak.i] : u.ak.i;
65473 assert( memIsValid(&aMem[u.ak.p1+u.ak.idx]) );
65474 assert( memIsValid(&aMem[u.ak.p2+u.ak.idx]) );
65475 REGISTER_TRACE(u.ak.p1+u.ak.idx, &aMem[u.ak.p1+u.ak.idx]);
65476 REGISTER_TRACE(u.ak.p2+u.ak.idx, &aMem[u.ak.p2+u.ak.idx]);
65477 assert( u.ak.i<u.ak.pKeyInfo->nField );
65478 u.ak.pColl = u.ak.pKeyInfo->aColl[u.ak.i];
65479 u.ak.bRev = u.ak.pKeyInfo->aSortOrder[u.ak.i];
65480 iCompare = sqlite3MemCompare(&aMem[u.ak.p1+u.ak.idx], &aMem[u.ak.p2+u.ak.idx], u.ak.pColl);
65481 if( iCompare ){
65482 if( u.ak.bRev ) iCompare = -iCompare;
65483 break;
65484 }
65485 }
65486 aPermute = 0;
65487 break;
@@ -65522,39 +65576,39 @@
65522 ** even if the other input is NULL. A NULL and false or two NULLs
65523 ** give a NULL output.
65524 */
65525 case OP_And: /* same as TK_AND, in1, in2, out3 */
65526 case OP_Or: { /* same as TK_OR, in1, in2, out3 */
65527 #if 0 /* local variables moved into u.al */
65528 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
65529 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
65530 #endif /* local variables moved into u.al */
65531
65532 pIn1 = &aMem[pOp->p1];
65533 if( pIn1->flags & MEM_Null ){
65534 u.al.v1 = 2;
65535 }else{
65536 u.al.v1 = sqlite3VdbeIntValue(pIn1)!=0;
65537 }
65538 pIn2 = &aMem[pOp->p2];
65539 if( pIn2->flags & MEM_Null ){
65540 u.al.v2 = 2;
65541 }else{
65542 u.al.v2 = sqlite3VdbeIntValue(pIn2)!=0;
65543 }
65544 if( pOp->opcode==OP_And ){
65545 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
65546 u.al.v1 = and_logic[u.al.v1*3+u.al.v2];
65547 }else{
65548 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
65549 u.al.v1 = or_logic[u.al.v1*3+u.al.v2];
65550 }
65551 pOut = &aMem[pOp->p3];
65552 if( u.al.v1==2 ){
65553 MemSetTypeFlag(pOut, MEM_Null);
65554 }else{
65555 pOut->u.i = u.al.v1;
65556 MemSetTypeFlag(pOut, MEM_Int);
65557 }
65558 break;
65559 }
65560
@@ -65621,25 +65675,25 @@
65621 ** is considered false if it has a numeric value of zero. If the value
65622 ** in P1 is NULL then take the jump if P3 is zero.
65623 */
65624 case OP_If: /* jump, in1 */
65625 case OP_IfNot: { /* jump, in1 */
65626 #if 0 /* local variables moved into u.am */
65627 int c;
65628 #endif /* local variables moved into u.am */
65629 pIn1 = &aMem[pOp->p1];
65630 if( pIn1->flags & MEM_Null ){
65631 u.am.c = pOp->p3;
65632 }else{
65633 #ifdef SQLITE_OMIT_FLOATING_POINT
65634 u.am.c = sqlite3VdbeIntValue(pIn1)!=0;
65635 #else
65636 u.am.c = sqlite3VdbeRealValue(pIn1)!=0.0;
65637 #endif
65638 if( pOp->opcode==OP_IfNot ) u.am.c = !u.am.c;
65639 }
65640 if( u.am.c ){
65641 pc = pOp->p2-1;
65642 }
65643 break;
65644 }
65645
@@ -65690,11 +65744,11 @@
65690 ** the result is guaranteed to only be used as the argument of a length()
65691 ** or typeof() function, respectively. The loading of large blobs can be
65692 ** skipped for length() and all content loading can be skipped for typeof().
65693 */
65694 case OP_Column: {
65695 #if 0 /* local variables moved into u.an */
65696 u32 payloadSize; /* Number of bytes in the record */
65697 i64 payloadSize64; /* Number of bytes in the record */
65698 int p1; /* P1 value of the opcode */
65699 int p2; /* column number to retrieve */
65700 VdbeCursor *pC; /* The VDBE cursor */
@@ -65714,130 +65768,130 @@
65714 u32 szField; /* Number of bytes in the content of a field */
65715 int szHdr; /* Size of the header size field at start of record */
65716 int avail; /* Number of bytes of available data */
65717 u32 t; /* A type code from the record header */
65718 Mem *pReg; /* PseudoTable input register */
65719 #endif /* local variables moved into u.an */
65720
65721
65722 u.an.p1 = pOp->p1;
65723 u.an.p2 = pOp->p2;
65724 u.an.pC = 0;
65725 memset(&u.an.sMem, 0, sizeof(u.an.sMem));
65726 assert( u.an.p1<p->nCursor );
65727 assert( pOp->p3>0 && pOp->p3<=p->nMem );
65728 u.an.pDest = &aMem[pOp->p3];
65729 memAboutToChange(p, u.an.pDest);
65730 u.an.zRec = 0;
65731
65732 /* This block sets the variable u.an.payloadSize to be the total number of
65733 ** bytes in the record.
65734 **
65735 ** u.an.zRec is set to be the complete text of the record if it is available.
65736 ** The complete record text is always available for pseudo-tables
65737 ** If the record is stored in a cursor, the complete record text
65738 ** might be available in the u.an.pC->aRow cache. Or it might not be.
65739 ** If the data is unavailable, u.an.zRec is set to NULL.
65740 **
65741 ** We also compute the number of columns in the record. For cursors,
65742 ** the number of columns is stored in the VdbeCursor.nField element.
65743 */
65744 u.an.pC = p->apCsr[u.an.p1];
65745 assert( u.an.pC!=0 );
65746 #ifndef SQLITE_OMIT_VIRTUALTABLE
65747 assert( u.an.pC->pVtabCursor==0 );
65748 #endif
65749 u.an.pCrsr = u.an.pC->pCursor;
65750 if( u.an.pCrsr!=0 ){
65751 /* The record is stored in a B-Tree */
65752 rc = sqlite3VdbeCursorMoveto(u.an.pC);
65753 if( rc ) goto abort_due_to_error;
65754 if( u.an.pC->nullRow ){
65755 u.an.payloadSize = 0;
65756 }else if( u.an.pC->cacheStatus==p->cacheCtr ){
65757 u.an.payloadSize = u.an.pC->payloadSize;
65758 u.an.zRec = (char*)u.an.pC->aRow;
65759 }else if( u.an.pC->isIndex ){
65760 assert( sqlite3BtreeCursorIsValid(u.an.pCrsr) );
65761 VVA_ONLY(rc =) sqlite3BtreeKeySize(u.an.pCrsr, &u.an.payloadSize64);
65762 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
65763 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
65764 ** payload size, so it is impossible for u.an.payloadSize64 to be
65765 ** larger than 32 bits. */
65766 assert( (u.an.payloadSize64 & SQLITE_MAX_U32)==(u64)u.an.payloadSize64 );
65767 u.an.payloadSize = (u32)u.an.payloadSize64;
65768 }else{
65769 assert( sqlite3BtreeCursorIsValid(u.an.pCrsr) );
65770 VVA_ONLY(rc =) sqlite3BtreeDataSize(u.an.pCrsr, &u.an.payloadSize);
65771 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
65772 }
65773 }else if( ALWAYS(u.an.pC->pseudoTableReg>0) ){
65774 u.an.pReg = &aMem[u.an.pC->pseudoTableReg];
65775 assert( u.an.pReg->flags & MEM_Blob );
65776 assert( memIsValid(u.an.pReg) );
65777 u.an.payloadSize = u.an.pReg->n;
65778 u.an.zRec = u.an.pReg->z;
65779 u.an.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
65780 assert( u.an.payloadSize==0 || u.an.zRec!=0 );
65781 }else{
65782 /* Consider the row to be NULL */
65783 u.an.payloadSize = 0;
65784 }
65785
65786 /* If u.an.payloadSize is 0, then just store a NULL. This can happen because of
65787 ** nullRow or because of a corrupt database. */
65788 if( u.an.payloadSize==0 ){
65789 MemSetTypeFlag(u.an.pDest, MEM_Null);
65790 goto op_column_out;
65791 }
65792 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
65793 if( u.an.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
65794 goto too_big;
65795 }
65796
65797 u.an.nField = u.an.pC->nField;
65798 assert( u.an.p2<u.an.nField );
65799
65800 /* Read and parse the table header. Store the results of the parse
65801 ** into the record header cache fields of the cursor.
65802 */
65803 u.an.aType = u.an.pC->aType;
65804 if( u.an.pC->cacheStatus==p->cacheCtr ){
65805 u.an.aOffset = u.an.pC->aOffset;
65806 }else{
65807 assert(u.an.aType);
65808 u.an.avail = 0;
65809 u.an.pC->aOffset = u.an.aOffset = &u.an.aType[u.an.nField];
65810 u.an.pC->payloadSize = u.an.payloadSize;
65811 u.an.pC->cacheStatus = p->cacheCtr;
65812
65813 /* Figure out how many bytes are in the header */
65814 if( u.an.zRec ){
65815 u.an.zData = u.an.zRec;
65816 }else{
65817 if( u.an.pC->isIndex ){
65818 u.an.zData = (char*)sqlite3BtreeKeyFetch(u.an.pCrsr, &u.an.avail);
65819 }else{
65820 u.an.zData = (char*)sqlite3BtreeDataFetch(u.an.pCrsr, &u.an.avail);
65821 }
65822 /* If KeyFetch()/DataFetch() managed to get the entire payload,
65823 ** save the payload in the u.an.pC->aRow cache. That will save us from
65824 ** having to make additional calls to fetch the content portion of
65825 ** the record.
65826 */
65827 assert( u.an.avail>=0 );
65828 if( u.an.payloadSize <= (u32)u.an.avail ){
65829 u.an.zRec = u.an.zData;
65830 u.an.pC->aRow = (u8*)u.an.zData;
65831 }else{
65832 u.an.pC->aRow = 0;
65833 }
65834 }
65835 /* The following assert is true in all cases except when
65836 ** the database file has been corrupted externally.
65837 ** assert( u.an.zRec!=0 || u.an.avail>=u.an.payloadSize || u.an.avail>=9 ); */
65838 u.an.szHdr = getVarint32((u8*)u.an.zData, u.an.offset);
65839
65840 /* Make sure a corrupt database has not given us an oversize header.
65841 ** Do this now to avoid an oversize memory allocation.
65842 **
65843 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
@@ -65844,161 +65898,161 @@
65844 ** types use so much data space that there can only be 4096 and 32 of
65845 ** them, respectively. So the maximum header length results from a
65846 ** 3-byte type for each of the maximum of 32768 columns plus three
65847 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
65848 */
65849 if( u.an.offset > 98307 ){
65850 rc = SQLITE_CORRUPT_BKPT;
65851 goto op_column_out;
65852 }
65853
65854 /* Compute in u.an.len the number of bytes of data we need to read in order
65855 ** to get u.an.nField type values. u.an.offset is an upper bound on this. But
65856 ** u.an.nField might be significantly less than the true number of columns
65857 ** in the table, and in that case, 5*u.an.nField+3 might be smaller than u.an.offset.
65858 ** We want to minimize u.an.len in order to limit the size of the memory
65859 ** allocation, especially if a corrupt database file has caused u.an.offset
65860 ** to be oversized. Offset is limited to 98307 above. But 98307 might
65861 ** still exceed Robson memory allocation limits on some configurations.
65862 ** On systems that cannot tolerate large memory allocations, u.an.nField*5+3
65863 ** will likely be much smaller since u.an.nField will likely be less than
65864 ** 20 or so. This insures that Robson memory allocation limits are
65865 ** not exceeded even for corrupt database files.
65866 */
65867 u.an.len = u.an.nField*5 + 3;
65868 if( u.an.len > (int)u.an.offset ) u.an.len = (int)u.an.offset;
65869
65870 /* The KeyFetch() or DataFetch() above are fast and will get the entire
65871 ** record header in most cases. But they will fail to get the complete
65872 ** record header if the record header does not fit on a single page
65873 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
65874 ** acquire the complete header text.
65875 */
65876 if( !u.an.zRec && u.an.avail<u.an.len ){
65877 u.an.sMem.flags = 0;
65878 u.an.sMem.db = 0;
65879 rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, 0, u.an.len, u.an.pC->isIndex, &u.an.sMem);
65880 if( rc!=SQLITE_OK ){
65881 goto op_column_out;
65882 }
65883 u.an.zData = u.an.sMem.z;
65884 }
65885 u.an.zEndHdr = (u8 *)&u.an.zData[u.an.len];
65886 u.an.zIdx = (u8 *)&u.an.zData[u.an.szHdr];
65887
65888 /* Scan the header and use it to fill in the u.an.aType[] and u.an.aOffset[]
65889 ** arrays. u.an.aType[u.an.i] will contain the type integer for the u.an.i-th
65890 ** column and u.an.aOffset[u.an.i] will contain the u.an.offset from the beginning
65891 ** of the record to the start of the data for the u.an.i-th column
65892 */
65893 for(u.an.i=0; u.an.i<u.an.nField; u.an.i++){
65894 if( u.an.zIdx<u.an.zEndHdr ){
65895 u.an.aOffset[u.an.i] = u.an.offset;
65896 if( u.an.zIdx[0]<0x80 ){
65897 u.an.t = u.an.zIdx[0];
65898 u.an.zIdx++;
65899 }else{
65900 u.an.zIdx += sqlite3GetVarint32(u.an.zIdx, &u.an.t);
65901 }
65902 u.an.aType[u.an.i] = u.an.t;
65903 u.an.szField = sqlite3VdbeSerialTypeLen(u.an.t);
65904 u.an.offset += u.an.szField;
65905 if( u.an.offset<u.an.szField ){ /* True if u.an.offset overflows */
65906 u.an.zIdx = &u.an.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
65907 break;
65908 }
65909 }else{
65910 /* If u.an.i is less that u.an.nField, then there are fewer fields in this
65911 ** record than SetNumColumns indicated there are columns in the
65912 ** table. Set the u.an.offset for any extra columns not present in
65913 ** the record to 0. This tells code below to store the default value
65914 ** for the column instead of deserializing a value from the record.
65915 */
65916 u.an.aOffset[u.an.i] = 0;
65917 }
65918 }
65919 sqlite3VdbeMemRelease(&u.an.sMem);
65920 u.an.sMem.flags = MEM_Null;
65921
65922 /* If we have read more header data than was contained in the header,
65923 ** or if the end of the last field appears to be past the end of the
65924 ** record, or if the end of the last field appears to be before the end
65925 ** of the record (when all fields present), then we must be dealing
65926 ** with a corrupt database.
65927 */
65928 if( (u.an.zIdx > u.an.zEndHdr) || (u.an.offset > u.an.payloadSize)
65929 || (u.an.zIdx==u.an.zEndHdr && u.an.offset!=u.an.payloadSize) ){
65930 rc = SQLITE_CORRUPT_BKPT;
65931 goto op_column_out;
65932 }
65933 }
65934
65935 /* Get the column information. If u.an.aOffset[u.an.p2] is non-zero, then
65936 ** deserialize the value from the record. If u.an.aOffset[u.an.p2] is zero,
65937 ** then there are not enough fields in the record to satisfy the
65938 ** request. In this case, set the value NULL or to P4 if P4 is
65939 ** a pointer to a Mem object.
65940 */
65941 if( u.an.aOffset[u.an.p2] ){
65942 assert( rc==SQLITE_OK );
65943 if( u.an.zRec ){
65944 /* This is the common case where the whole row fits on a single page */
65945 VdbeMemRelease(u.an.pDest);
65946 sqlite3VdbeSerialGet((u8 *)&u.an.zRec[u.an.aOffset[u.an.p2]], u.an.aType[u.an.p2], u.an.pDest);
65947 }else{
65948 /* This branch happens only when the row overflows onto multiple pages */
65949 u.an.t = u.an.aType[u.an.p2];
65950 if( (pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
65951 && ((u.an.t>=12 && (u.an.t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)
65952 ){
65953 /* Content is irrelevant for the typeof() function and for
65954 ** the length(X) function if X is a blob. So we might as well use
65955 ** bogus content rather than reading content from disk. NULL works
65956 ** for text and blob and whatever is in the u.an.payloadSize64 variable
65957 ** will work for everything else. */
65958 u.an.zData = u.an.t<12 ? (char*)&u.an.payloadSize64 : 0;
65959 }else{
65960 u.an.len = sqlite3VdbeSerialTypeLen(u.an.t);
65961 sqlite3VdbeMemMove(&u.an.sMem, u.an.pDest);
65962 rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, u.an.aOffset[u.an.p2], u.an.len, u.an.pC->isIndex,
65963 &u.an.sMem);
65964 if( rc!=SQLITE_OK ){
65965 goto op_column_out;
65966 }
65967 u.an.zData = u.an.sMem.z;
65968 }
65969 sqlite3VdbeSerialGet((u8*)u.an.zData, u.an.t, u.an.pDest);
65970 }
65971 u.an.pDest->enc = encoding;
65972 }else{
65973 if( pOp->p4type==P4_MEM ){
65974 sqlite3VdbeMemShallowCopy(u.an.pDest, pOp->p4.pMem, MEM_Static);
65975 }else{
65976 MemSetTypeFlag(u.an.pDest, MEM_Null);
65977 }
65978 }
65979
65980 /* If we dynamically allocated space to hold the data (in the
65981 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
65982 ** dynamically allocated space over to the u.an.pDest structure.
65983 ** This prevents a memory copy.
65984 */
65985 if( u.an.sMem.zMalloc ){
65986 assert( u.an.sMem.z==u.an.sMem.zMalloc );
65987 assert( !(u.an.pDest->flags & MEM_Dyn) );
65988 assert( !(u.an.pDest->flags & (MEM_Blob|MEM_Str)) || u.an.pDest->z==u.an.sMem.z );
65989 u.an.pDest->flags &= ~(MEM_Ephem|MEM_Static);
65990 u.an.pDest->flags |= MEM_Term;
65991 u.an.pDest->z = u.an.sMem.z;
65992 u.an.pDest->zMalloc = u.an.sMem.zMalloc;
65993 }
65994
65995 rc = sqlite3VdbeMemMakeWriteable(u.an.pDest);
65996
65997 op_column_out:
65998 UPDATE_MAX_BLOBSIZE(u.an.pDest);
65999 REGISTER_TRACE(pOp->p3, u.an.pDest);
66000 break;
66001 }
66002
66003 /* Opcode: Affinity P1 P2 * P4 *
66004 **
@@ -66007,24 +66061,24 @@
66007 ** P4 is a string that is P2 characters long. The nth character of the
66008 ** string indicates the column affinity that should be used for the nth
66009 ** memory cell in the range.
66010 */
66011 case OP_Affinity: {
66012 #if 0 /* local variables moved into u.ao */
66013 const char *zAffinity; /* The affinity to be applied */
66014 char cAff; /* A single character of affinity */
66015 #endif /* local variables moved into u.ao */
66016
66017 u.ao.zAffinity = pOp->p4.z;
66018 assert( u.ao.zAffinity!=0 );
66019 assert( u.ao.zAffinity[pOp->p2]==0 );
66020 pIn1 = &aMem[pOp->p1];
66021 while( (u.ao.cAff = *(u.ao.zAffinity++))!=0 ){
66022 assert( pIn1 <= &p->aMem[p->nMem] );
66023 assert( memIsValid(pIn1) );
66024 ExpandBlob(pIn1);
66025 applyAffinity(pIn1, u.ao.cAff, encoding);
66026 pIn1++;
66027 }
66028 break;
66029 }
66030
@@ -66042,11 +66096,11 @@
66042 ** macros defined in sqliteInt.h.
66043 **
66044 ** If P4 is NULL then all index fields have the affinity NONE.
66045 */
66046 case OP_MakeRecord: {
66047 #if 0 /* local variables moved into u.ap */
66048 u8 *zNewRecord; /* A buffer to hold the data for the new record */
66049 Mem *pRec; /* The new record */
66050 u64 nData; /* Number of bytes of data space */
66051 int nHdr; /* Number of bytes of header space */
66052 i64 nByte; /* Data space required for this record */
@@ -66058,11 +66112,11 @@
66058 int nField; /* Number of fields in the record */
66059 char *zAffinity; /* The affinity string for the record */
66060 int file_format; /* File format to use for encoding */
66061 int i; /* Space used in zNewRecord[] */
66062 int len; /* Length of a field */
66063 #endif /* local variables moved into u.ap */
66064
66065 /* Assuming the record contains N fields, the record format looks
66066 ** like this:
66067 **
66068 ** ------------------------------------------------------------------------
@@ -66075,87 +66129,87 @@
66075 ** Each type field is a varint representing the serial type of the
66076 ** corresponding data element (see sqlite3VdbeSerialType()). The
66077 ** hdr-size field is also a varint which is the offset from the beginning
66078 ** of the record to data0.
66079 */
66080 u.ap.nData = 0; /* Number of bytes of data space */
66081 u.ap.nHdr = 0; /* Number of bytes of header space */
66082 u.ap.nZero = 0; /* Number of zero bytes at the end of the record */
66083 u.ap.nField = pOp->p1;
66084 u.ap.zAffinity = pOp->p4.z;
66085 assert( u.ap.nField>0 && pOp->p2>0 && pOp->p2+u.ap.nField<=p->nMem+1 );
66086 u.ap.pData0 = &aMem[u.ap.nField];
66087 u.ap.nField = pOp->p2;
66088 u.ap.pLast = &u.ap.pData0[u.ap.nField-1];
66089 u.ap.file_format = p->minWriteFileFormat;
66090
66091 /* Identify the output register */
66092 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
66093 pOut = &aMem[pOp->p3];
66094 memAboutToChange(p, pOut);
66095
66096 /* Loop through the elements that will make up the record to figure
66097 ** out how much space is required for the new record.
66098 */
66099 for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
66100 assert( memIsValid(u.ap.pRec) );
66101 if( u.ap.zAffinity ){
66102 applyAffinity(u.ap.pRec, u.ap.zAffinity[u.ap.pRec-u.ap.pData0], encoding);
66103 }
66104 if( u.ap.pRec->flags&MEM_Zero && u.ap.pRec->n>0 ){
66105 sqlite3VdbeMemExpandBlob(u.ap.pRec);
66106 }
66107 u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
66108 u.ap.len = sqlite3VdbeSerialTypeLen(u.ap.serial_type);
66109 u.ap.nData += u.ap.len;
66110 u.ap.nHdr += sqlite3VarintLen(u.ap.serial_type);
66111 if( u.ap.pRec->flags & MEM_Zero ){
66112 /* Only pure zero-filled BLOBs can be input to this Opcode.
66113 ** We do not allow blobs with a prefix and a zero-filled tail. */
66114 u.ap.nZero += u.ap.pRec->u.nZero;
66115 }else if( u.ap.len ){
66116 u.ap.nZero = 0;
66117 }
66118 }
66119
66120 /* Add the initial header varint and total the size */
66121 u.ap.nHdr += u.ap.nVarint = sqlite3VarintLen(u.ap.nHdr);
66122 if( u.ap.nVarint<sqlite3VarintLen(u.ap.nHdr) ){
66123 u.ap.nHdr++;
66124 }
66125 u.ap.nByte = u.ap.nHdr+u.ap.nData-u.ap.nZero;
66126 if( u.ap.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
66127 goto too_big;
66128 }
66129
66130 /* Make sure the output register has a buffer large enough to store
66131 ** the new record. The output register (pOp->p3) is not allowed to
66132 ** be one of the input registers (because the following call to
66133 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
66134 */
66135 if( sqlite3VdbeMemGrow(pOut, (int)u.ap.nByte, 0) ){
66136 goto no_mem;
66137 }
66138 u.ap.zNewRecord = (u8 *)pOut->z;
66139
66140 /* Write the record */
66141 u.ap.i = putVarint32(u.ap.zNewRecord, u.ap.nHdr);
66142 for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
66143 u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
66144 u.ap.i += putVarint32(&u.ap.zNewRecord[u.ap.i], u.ap.serial_type); /* serial type */
66145 }
66146 for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){ /* serial data */
66147 u.ap.i += sqlite3VdbeSerialPut(&u.ap.zNewRecord[u.ap.i], (int)(u.ap.nByte-u.ap.i), u.ap.pRec,u.ap.file_format);
66148 }
66149 assert( u.ap.i==u.ap.nByte );
66150
66151 assert( pOp->p3>0 && pOp->p3<=p->nMem );
66152 pOut->n = (int)u.ap.nByte;
66153 pOut->flags = MEM_Blob | MEM_Dyn;
66154 pOut->xDel = 0;
66155 if( u.ap.nZero ){
66156 pOut->u.nZero = u.ap.nZero;
66157 pOut->flags |= MEM_Zero;
66158 }
66159 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
66160 REGISTER_TRACE(pOp->p3, pOut);
66161 UPDATE_MAX_BLOBSIZE(pOut);
@@ -66167,22 +66221,22 @@
66167 ** Store the number of entries (an integer value) in the table or index
66168 ** opened by cursor P1 in register P2
66169 */
66170 #ifndef SQLITE_OMIT_BTREECOUNT
66171 case OP_Count: { /* out2-prerelease */
66172 #if 0 /* local variables moved into u.aq */
66173 i64 nEntry;
66174 BtCursor *pCrsr;
66175 #endif /* local variables moved into u.aq */
66176
66177 u.aq.pCrsr = p->apCsr[pOp->p1]->pCursor;
66178 if( ALWAYS(u.aq.pCrsr) ){
66179 rc = sqlite3BtreeCount(u.aq.pCrsr, &u.aq.nEntry);
66180 }else{
66181 u.aq.nEntry = 0;
66182 }
66183 pOut->u.i = u.aq.nEntry;
66184 break;
66185 }
66186 #endif
66187
66188 /* Opcode: Savepoint P1 * * P4 *
@@ -66190,42 +66244,42 @@
66190 ** Open, release or rollback the savepoint named by parameter P4, depending
66191 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
66192 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
66193 */
66194 case OP_Savepoint: {
66195 #if 0 /* local variables moved into u.ar */
66196 int p1; /* Value of P1 operand */
66197 char *zName; /* Name of savepoint */
66198 int nName;
66199 Savepoint *pNew;
66200 Savepoint *pSavepoint;
66201 Savepoint *pTmp;
66202 int iSavepoint;
66203 int ii;
66204 #endif /* local variables moved into u.ar */
66205
66206 u.ar.p1 = pOp->p1;
66207 u.ar.zName = pOp->p4.z;
66208
66209 /* Assert that the u.ar.p1 parameter is valid. Also that if there is no open
66210 ** transaction, then there cannot be any savepoints.
66211 */
66212 assert( db->pSavepoint==0 || db->autoCommit==0 );
66213 assert( u.ar.p1==SAVEPOINT_BEGIN||u.ar.p1==SAVEPOINT_RELEASE||u.ar.p1==SAVEPOINT_ROLLBACK );
66214 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
66215 assert( checkSavepointCount(db) );
66216
66217 if( u.ar.p1==SAVEPOINT_BEGIN ){
66218 if( db->writeVdbeCnt>0 ){
66219 /* A new savepoint cannot be created if there are active write
66220 ** statements (i.e. open read/write incremental blob handles).
66221 */
66222 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
66223 "SQL statements in progress");
66224 rc = SQLITE_BUSY;
66225 }else{
66226 u.ar.nName = sqlite3Strlen30(u.ar.zName);
66227
66228 #ifndef SQLITE_OMIT_VIRTUALTABLE
66229 /* This call is Ok even if this savepoint is actually a transaction
66230 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
66231 ** If this is a transaction savepoint being opened, it is guaranteed
@@ -66235,14 +66289,14 @@
66235 db->nStatement+db->nSavepoint);
66236 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66237 #endif
66238
66239 /* Create a new savepoint structure. */
66240 u.ar.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.ar.nName+1);
66241 if( u.ar.pNew ){
66242 u.ar.pNew->zName = (char *)&u.ar.pNew[1];
66243 memcpy(u.ar.pNew->zName, u.ar.zName, u.ar.nName+1);
66244
66245 /* If there is no open transaction, then mark this as a special
66246 ** "transaction savepoint". */
66247 if( db->autoCommit ){
66248 db->autoCommit = 0;
@@ -66250,31 +66304,31 @@
66250 }else{
66251 db->nSavepoint++;
66252 }
66253
66254 /* Link the new savepoint into the database handle's list. */
66255 u.ar.pNew->pNext = db->pSavepoint;
66256 db->pSavepoint = u.ar.pNew;
66257 u.ar.pNew->nDeferredCons = db->nDeferredCons;
66258 }
66259 }
66260 }else{
66261 u.ar.iSavepoint = 0;
66262
66263 /* Find the named savepoint. If there is no such savepoint, then an
66264 ** an error is returned to the user. */
66265 for(
66266 u.ar.pSavepoint = db->pSavepoint;
66267 u.ar.pSavepoint && sqlite3StrICmp(u.ar.pSavepoint->zName, u.ar.zName);
66268 u.ar.pSavepoint = u.ar.pSavepoint->pNext
66269 ){
66270 u.ar.iSavepoint++;
66271 }
66272 if( !u.ar.pSavepoint ){
66273 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.ar.zName);
66274 rc = SQLITE_ERROR;
66275 }else if( db->writeVdbeCnt>0 && u.ar.p1==SAVEPOINT_RELEASE ){
66276 /* It is not possible to release (commit) a savepoint if there are
66277 ** active write statements.
66278 */
66279 sqlite3SetString(&p->zErrMsg, db,
66280 "cannot release savepoint - SQL statements in progress"
@@ -66284,12 +66338,12 @@
66284
66285 /* Determine whether or not this is a transaction savepoint. If so,
66286 ** and this is a RELEASE command, then the current transaction
66287 ** is committed.
66288 */
66289 int isTransaction = u.ar.pSavepoint->pNext==0 && db->isTransactionSavepoint;
66290 if( isTransaction && u.ar.p1==SAVEPOINT_RELEASE ){
66291 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
66292 goto vdbe_return;
66293 }
66294 db->autoCommit = 1;
66295 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
@@ -66299,55 +66353,55 @@
66299 goto vdbe_return;
66300 }
66301 db->isTransactionSavepoint = 0;
66302 rc = p->rc;
66303 }else{
66304 u.ar.iSavepoint = db->nSavepoint - u.ar.iSavepoint - 1;
66305 if( u.ar.p1==SAVEPOINT_ROLLBACK ){
66306 for(u.ar.ii=0; u.ar.ii<db->nDb; u.ar.ii++){
66307 sqlite3BtreeTripAllCursors(db->aDb[u.ar.ii].pBt, SQLITE_ABORT);
66308 }
66309 }
66310 for(u.ar.ii=0; u.ar.ii<db->nDb; u.ar.ii++){
66311 rc = sqlite3BtreeSavepoint(db->aDb[u.ar.ii].pBt, u.ar.p1, u.ar.iSavepoint);
66312 if( rc!=SQLITE_OK ){
66313 goto abort_due_to_error;
66314 }
66315 }
66316 if( u.ar.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
66317 sqlite3ExpirePreparedStatements(db);
66318 sqlite3ResetAllSchemasOfConnection(db);
66319 db->flags = (db->flags | SQLITE_InternChanges);
66320 }
66321 }
66322
66323 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
66324 ** savepoints nested inside of the savepoint being operated on. */
66325 while( db->pSavepoint!=u.ar.pSavepoint ){
66326 u.ar.pTmp = db->pSavepoint;
66327 db->pSavepoint = u.ar.pTmp->pNext;
66328 sqlite3DbFree(db, u.ar.pTmp);
66329 db->nSavepoint--;
66330 }
66331
66332 /* If it is a RELEASE, then destroy the savepoint being operated on
66333 ** too. If it is a ROLLBACK TO, then set the number of deferred
66334 ** constraint violations present in the database to the value stored
66335 ** when the savepoint was created. */
66336 if( u.ar.p1==SAVEPOINT_RELEASE ){
66337 assert( u.ar.pSavepoint==db->pSavepoint );
66338 db->pSavepoint = u.ar.pSavepoint->pNext;
66339 sqlite3DbFree(db, u.ar.pSavepoint);
66340 if( !isTransaction ){
66341 db->nSavepoint--;
66342 }
66343 }else{
66344 db->nDeferredCons = u.ar.pSavepoint->nDeferredCons;
66345 }
66346
66347 if( !isTransaction ){
66348 rc = sqlite3VtabSavepoint(db, u.ar.p1, u.ar.iSavepoint);
66349 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66350 }
66351 }
66352 }
66353
@@ -66362,53 +66416,53 @@
66362 ** there are active writing VMs or active VMs that use shared cache.
66363 **
66364 ** This instruction causes the VM to halt.
66365 */
66366 case OP_AutoCommit: {
66367 #if 0 /* local variables moved into u.as */
66368 int desiredAutoCommit;
66369 int iRollback;
66370 int turnOnAC;
66371 #endif /* local variables moved into u.as */
66372
66373 u.as.desiredAutoCommit = pOp->p1;
66374 u.as.iRollback = pOp->p2;
66375 u.as.turnOnAC = u.as.desiredAutoCommit && !db->autoCommit;
66376 assert( u.as.desiredAutoCommit==1 || u.as.desiredAutoCommit==0 );
66377 assert( u.as.desiredAutoCommit==1 || u.as.iRollback==0 );
66378 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
66379
66380 #if 0
66381 if( u.as.turnOnAC && u.as.iRollback && db->activeVdbeCnt>1 ){
66382 /* If this instruction implements a ROLLBACK and other VMs are
66383 ** still running, and a transaction is active, return an error indicating
66384 ** that the other VMs must complete first.
66385 */
66386 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
66387 "SQL statements in progress");
66388 rc = SQLITE_BUSY;
66389 }else
66390 #endif
66391 if( u.as.turnOnAC && !u.as.iRollback && db->writeVdbeCnt>0 ){
66392 /* If this instruction implements a COMMIT and other VMs are writing
66393 ** return an error indicating that the other VMs must complete first.
66394 */
66395 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
66396 "SQL statements in progress");
66397 rc = SQLITE_BUSY;
66398 }else if( u.as.desiredAutoCommit!=db->autoCommit ){
66399 if( u.as.iRollback ){
66400 assert( u.as.desiredAutoCommit==1 );
66401 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
66402 db->autoCommit = 1;
66403 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
66404 goto vdbe_return;
66405 }else{
66406 db->autoCommit = (u8)u.as.desiredAutoCommit;
66407 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
66408 p->pc = pc;
66409 db->autoCommit = (u8)(1-u.as.desiredAutoCommit);
66410 p->rc = rc = SQLITE_BUSY;
66411 goto vdbe_return;
66412 }
66413 }
66414 assert( db->nStatement==0 );
@@ -66419,12 +66473,12 @@
66419 rc = SQLITE_ERROR;
66420 }
66421 goto vdbe_return;
66422 }else{
66423 sqlite3SetString(&p->zErrMsg, db,
66424 (!u.as.desiredAutoCommit)?"cannot start a transaction within a transaction":(
66425 (u.as.iRollback)?"cannot rollback - no transaction is active":
66426 "cannot commit - no transaction is active"));
66427
66428 rc = SQLITE_ERROR;
66429 }
66430 break;
@@ -66460,20 +66514,20 @@
66460 ** will automatically commit when the VDBE halts.
66461 **
66462 ** If P2 is zero, then a read-lock is obtained on the database file.
66463 */
66464 case OP_Transaction: {
66465 #if 0 /* local variables moved into u.at */
66466 Btree *pBt;
66467 #endif /* local variables moved into u.at */
66468
66469 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66470 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66471 u.at.pBt = db->aDb[pOp->p1].pBt;
66472
66473 if( u.at.pBt ){
66474 rc = sqlite3BtreeBeginTrans(u.at.pBt, pOp->p2);
66475 if( rc==SQLITE_BUSY ){
66476 p->pc = pc;
66477 p->rc = rc = SQLITE_BUSY;
66478 goto vdbe_return;
66479 }
@@ -66482,20 +66536,20 @@
66482 }
66483
66484 if( pOp->p2 && p->usesStmtJournal
66485 && (db->autoCommit==0 || db->activeVdbeCnt>1)
66486 ){
66487 assert( sqlite3BtreeIsInTrans(u.at.pBt) );
66488 if( p->iStatement==0 ){
66489 assert( db->nStatement>=0 && db->nSavepoint>=0 );
66490 db->nStatement++;
66491 p->iStatement = db->nSavepoint + db->nStatement;
66492 }
66493
66494 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
66495 if( rc==SQLITE_OK ){
66496 rc = sqlite3BtreeBeginStmt(u.at.pBt, p->iStatement);
66497 }
66498
66499 /* Store the current value of the database handles deferred constraint
66500 ** counter. If the statement transaction needs to be rolled back,
66501 ** the value of this counter needs to be restored too. */
@@ -66516,25 +66570,25 @@
66516 ** There must be a read-lock on the database (either a transaction
66517 ** must be started or there must be an open cursor) before
66518 ** executing this instruction.
66519 */
66520 case OP_ReadCookie: { /* out2-prerelease */
66521 #if 0 /* local variables moved into u.au */
66522 int iMeta;
66523 int iDb;
66524 int iCookie;
66525 #endif /* local variables moved into u.au */
66526
66527 u.au.iDb = pOp->p1;
66528 u.au.iCookie = pOp->p3;
66529 assert( pOp->p3<SQLITE_N_BTREE_META );
66530 assert( u.au.iDb>=0 && u.au.iDb<db->nDb );
66531 assert( db->aDb[u.au.iDb].pBt!=0 );
66532 assert( (p->btreeMask & (((yDbMask)1)<<u.au.iDb))!=0 );
66533
66534 sqlite3BtreeGetMeta(db->aDb[u.au.iDb].pBt, u.au.iCookie, (u32 *)&u.au.iMeta);
66535 pOut->u.i = u.au.iMeta;
66536 break;
66537 }
66538
66539 /* Opcode: SetCookie P1 P2 P3 * *
66540 **
@@ -66545,30 +66599,30 @@
66545 ** database file used to store temporary tables.
66546 **
66547 ** A transaction must be started before executing this opcode.
66548 */
66549 case OP_SetCookie: { /* in3 */
66550 #if 0 /* local variables moved into u.av */
66551 Db *pDb;
66552 #endif /* local variables moved into u.av */
66553 assert( pOp->p2<SQLITE_N_BTREE_META );
66554 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66555 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66556 u.av.pDb = &db->aDb[pOp->p1];
66557 assert( u.av.pDb->pBt!=0 );
66558 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
66559 pIn3 = &aMem[pOp->p3];
66560 sqlite3VdbeMemIntegerify(pIn3);
66561 /* See note about index shifting on OP_ReadCookie */
66562 rc = sqlite3BtreeUpdateMeta(u.av.pDb->pBt, pOp->p2, (int)pIn3->u.i);
66563 if( pOp->p2==BTREE_SCHEMA_VERSION ){
66564 /* When the schema cookie changes, record the new cookie internally */
66565 u.av.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
66566 db->flags |= SQLITE_InternChanges;
66567 }else if( pOp->p2==BTREE_FILE_FORMAT ){
66568 /* Record changes in the file format */
66569 u.av.pDb->pSchema->file_format = (u8)pIn3->u.i;
66570 }
66571 if( pOp->p1==1 ){
66572 /* Invalidate all prepared statements whenever the TEMP database
66573 ** schema is changed. Ticket #1644 */
66574 sqlite3ExpirePreparedStatements(db);
@@ -66594,27 +66648,27 @@
66594 ** Either a transaction needs to have been started or an OP_Open needs
66595 ** to be executed (to establish a read lock) before this opcode is
66596 ** invoked.
66597 */
66598 case OP_VerifyCookie: {
66599 #if 0 /* local variables moved into u.aw */
66600 int iMeta;
66601 int iGen;
66602 Btree *pBt;
66603 #endif /* local variables moved into u.aw */
66604
66605 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66606 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66607 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
66608 u.aw.pBt = db->aDb[pOp->p1].pBt;
66609 if( u.aw.pBt ){
66610 sqlite3BtreeGetMeta(u.aw.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.aw.iMeta);
66611 u.aw.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
66612 }else{
66613 u.aw.iGen = u.aw.iMeta = 0;
66614 }
66615 if( u.aw.iMeta!=pOp->p2 || u.aw.iGen!=pOp->p3 ){
66616 sqlite3DbFree(db, p->zErrMsg);
66617 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
66618 /* If the schema-cookie from the database file matches the cookie
66619 ** stored with the in-memory representation of the schema, do
66620 ** not reload the schema from the database file.
@@ -66626,11 +66680,11 @@
66626 ** discard the database schema, as the user code implementing the
66627 ** v-table would have to be ready for the sqlite3_vtab structure itself
66628 ** to be invalidated whenever sqlite3_step() is called from within
66629 ** a v-table method.
66630 */
66631 if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.aw.iMeta ){
66632 sqlite3ResetOneSchema(db, pOp->p1);
66633 }
66634
66635 p->expired = 1;
66636 rc = SQLITE_SCHEMA;
@@ -66687,91 +66741,91 @@
66687 **
66688 ** See also OpenRead.
66689 */
66690 case OP_OpenRead:
66691 case OP_OpenWrite: {
66692 #if 0 /* local variables moved into u.ax */
66693 int nField;
66694 KeyInfo *pKeyInfo;
66695 int p2;
66696 int iDb;
66697 int wrFlag;
66698 Btree *pX;
66699 VdbeCursor *pCur;
66700 Db *pDb;
66701 #endif /* local variables moved into u.ax */
66702
66703 assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
66704 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
66705
66706 if( p->expired ){
66707 rc = SQLITE_ABORT;
66708 break;
66709 }
66710
66711 u.ax.nField = 0;
66712 u.ax.pKeyInfo = 0;
66713 u.ax.p2 = pOp->p2;
66714 u.ax.iDb = pOp->p3;
66715 assert( u.ax.iDb>=0 && u.ax.iDb<db->nDb );
66716 assert( (p->btreeMask & (((yDbMask)1)<<u.ax.iDb))!=0 );
66717 u.ax.pDb = &db->aDb[u.ax.iDb];
66718 u.ax.pX = u.ax.pDb->pBt;
66719 assert( u.ax.pX!=0 );
66720 if( pOp->opcode==OP_OpenWrite ){
66721 u.ax.wrFlag = 1;
66722 assert( sqlite3SchemaMutexHeld(db, u.ax.iDb, 0) );
66723 if( u.ax.pDb->pSchema->file_format < p->minWriteFileFormat ){
66724 p->minWriteFileFormat = u.ax.pDb->pSchema->file_format;
66725 }
66726 }else{
66727 u.ax.wrFlag = 0;
66728 }
66729 if( pOp->p5 & OPFLAG_P2ISREG ){
66730 assert( u.ax.p2>0 );
66731 assert( u.ax.p2<=p->nMem );
66732 pIn2 = &aMem[u.ax.p2];
66733 assert( memIsValid(pIn2) );
66734 assert( (pIn2->flags & MEM_Int)!=0 );
66735 sqlite3VdbeMemIntegerify(pIn2);
66736 u.ax.p2 = (int)pIn2->u.i;
66737 /* The u.ax.p2 value always comes from a prior OP_CreateTable opcode and
66738 ** that opcode will always set the u.ax.p2 value to 2 or more or else fail.
66739 ** If there were a failure, the prepared statement would have halted
66740 ** before reaching this instruction. */
66741 if( NEVER(u.ax.p2<2) ) {
66742 rc = SQLITE_CORRUPT_BKPT;
66743 goto abort_due_to_error;
66744 }
66745 }
66746 if( pOp->p4type==P4_KEYINFO ){
66747 u.ax.pKeyInfo = pOp->p4.pKeyInfo;
66748 u.ax.pKeyInfo->enc = ENC(p->db);
66749 u.ax.nField = u.ax.pKeyInfo->nField+1;
66750 }else if( pOp->p4type==P4_INT32 ){
66751 u.ax.nField = pOp->p4.i;
66752 }
66753 assert( pOp->p1>=0 );
66754 u.ax.pCur = allocateCursor(p, pOp->p1, u.ax.nField, u.ax.iDb, 1);
66755 if( u.ax.pCur==0 ) goto no_mem;
66756 u.ax.pCur->nullRow = 1;
66757 u.ax.pCur->isOrdered = 1;
66758 rc = sqlite3BtreeCursor(u.ax.pX, u.ax.p2, u.ax.wrFlag, u.ax.pKeyInfo, u.ax.pCur->pCursor);
66759 u.ax.pCur->pKeyInfo = u.ax.pKeyInfo;
66760 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
66761 sqlite3BtreeCursorHints(u.ax.pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
66762
66763 /* Since it performs no memory allocation or IO, the only value that
66764 ** sqlite3BtreeCursor() may return is SQLITE_OK. */
66765 assert( rc==SQLITE_OK );
66766
66767 /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
66768 ** SQLite used to check if the root-page flags were sane at this point
66769 ** and report database corruption if they were not, but this check has
66770 ** since moved into the btree layer. */
66771 u.ax.pCur->isTable = pOp->p4type!=P4_KEYINFO;
66772 u.ax.pCur->isIndex = !u.ax.pCur->isTable;
66773 break;
66774 }
66775
66776 /* Opcode: OpenEphemeral P1 P2 * P4 P5
66777 **
@@ -66803,28 +66857,28 @@
66803 ** by this opcode will be used for automatically created transient
66804 ** indices in joins.
66805 */
66806 case OP_OpenAutoindex:
66807 case OP_OpenEphemeral: {
66808 #if 0 /* local variables moved into u.ay */
66809 VdbeCursor *pCx;
66810 #endif /* local variables moved into u.ay */
66811 static const int vfsFlags =
66812 SQLITE_OPEN_READWRITE |
66813 SQLITE_OPEN_CREATE |
66814 SQLITE_OPEN_EXCLUSIVE |
66815 SQLITE_OPEN_DELETEONCLOSE |
66816 SQLITE_OPEN_TRANSIENT_DB;
66817
66818 assert( pOp->p1>=0 );
66819 u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66820 if( u.ay.pCx==0 ) goto no_mem;
66821 u.ay.pCx->nullRow = 1;
66822 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ay.pCx->pBt,
66823 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
66824 if( rc==SQLITE_OK ){
66825 rc = sqlite3BtreeBeginTrans(u.ay.pCx->pBt, 1);
66826 }
66827 if( rc==SQLITE_OK ){
66828 /* If a transient index is required, create it by calling
66829 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
66830 ** opening it. If a transient table is required, just use the
@@ -66831,26 +66885,26 @@
66831 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
66832 */
66833 if( pOp->p4.pKeyInfo ){
66834 int pgno;
66835 assert( pOp->p4type==P4_KEYINFO );
66836 rc = sqlite3BtreeCreateTable(u.ay.pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
66837 if( rc==SQLITE_OK ){
66838 assert( pgno==MASTER_ROOT+1 );
66839 rc = sqlite3BtreeCursor(u.ay.pCx->pBt, pgno, 1,
66840 (KeyInfo*)pOp->p4.z, u.ay.pCx->pCursor);
66841 u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66842 u.ay.pCx->pKeyInfo->enc = ENC(p->db);
66843 }
66844 u.ay.pCx->isTable = 0;
66845 }else{
66846 rc = sqlite3BtreeCursor(u.ay.pCx->pBt, MASTER_ROOT, 1, 0, u.ay.pCx->pCursor);
66847 u.ay.pCx->isTable = 1;
66848 }
66849 }
66850 u.ay.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
66851 u.ay.pCx->isIndex = !u.ay.pCx->isTable;
66852 break;
66853 }
66854
66855 /* Opcode: OpenSorter P1 P2 * P4 *
66856 **
@@ -66857,20 +66911,21 @@
66857 ** This opcode works like OP_OpenEphemeral except that it opens
66858 ** a transient index that is specifically designed to sort large
66859 ** tables using an external merge-sort algorithm.
66860 */
66861 case OP_SorterOpen: {
66862 #if 0 /* local variables moved into u.az */
66863 VdbeCursor *pCx;
66864 #endif /* local variables moved into u.az */
 
66865 #ifndef SQLITE_OMIT_MERGE_SORT
66866 u.az.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66867 if( u.az.pCx==0 ) goto no_mem;
66868 u.az.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66869 u.az.pCx->pKeyInfo->enc = ENC(p->db);
66870 u.az.pCx->isSorter = 1;
66871 rc = sqlite3VdbeSorterInit(db, u.az.pCx);
66872 #else
66873 pOp->opcode = OP_OpenEphemeral;
66874 pc--;
66875 #endif
66876 break;
@@ -66890,21 +66945,21 @@
66890 **
66891 ** P3 is the number of fields in the records that will be stored by
66892 ** the pseudo-table.
66893 */
66894 case OP_OpenPseudo: {
66895 #if 0 /* local variables moved into u.ba */
66896 VdbeCursor *pCx;
66897 #endif /* local variables moved into u.ba */
66898
66899 assert( pOp->p1>=0 );
66900 u.ba.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
66901 if( u.ba.pCx==0 ) goto no_mem;
66902 u.ba.pCx->nullRow = 1;
66903 u.ba.pCx->pseudoTableReg = pOp->p2;
66904 u.ba.pCx->isTable = 1;
66905 u.ba.pCx->isIndex = 0;
66906 break;
66907 }
66908
66909 /* Opcode: Close P1 * * * *
66910 **
@@ -66972,39 +67027,39 @@
66972 */
66973 case OP_SeekLt: /* jump, in3 */
66974 case OP_SeekLe: /* jump, in3 */
66975 case OP_SeekGe: /* jump, in3 */
66976 case OP_SeekGt: { /* jump, in3 */
66977 #if 0 /* local variables moved into u.bb */
66978 int res;
66979 int oc;
66980 VdbeCursor *pC;
66981 UnpackedRecord r;
66982 int nField;
66983 i64 iKey; /* The rowid we are to seek to */
66984 #endif /* local variables moved into u.bb */
66985
66986 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66987 assert( pOp->p2!=0 );
66988 u.bb.pC = p->apCsr[pOp->p1];
66989 assert( u.bb.pC!=0 );
66990 assert( u.bb.pC->pseudoTableReg==0 );
66991 assert( OP_SeekLe == OP_SeekLt+1 );
66992 assert( OP_SeekGe == OP_SeekLt+2 );
66993 assert( OP_SeekGt == OP_SeekLt+3 );
66994 assert( u.bb.pC->isOrdered );
66995 if( ALWAYS(u.bb.pC->pCursor!=0) ){
66996 u.bb.oc = pOp->opcode;
66997 u.bb.pC->nullRow = 0;
66998 if( u.bb.pC->isTable ){
66999 /* The input value in P3 might be of any type: integer, real, string,
67000 ** blob, or NULL. But it needs to be an integer before we can do
67001 ** the seek, so covert it. */
67002 pIn3 = &aMem[pOp->p3];
67003 applyNumericAffinity(pIn3);
67004 u.bb.iKey = sqlite3VdbeIntValue(pIn3);
67005 u.bb.pC->rowidIsValid = 0;
67006
67007 /* If the P3 value could not be converted into an integer without
67008 ** loss of information, then special processing is required... */
67009 if( (pIn3->flags & MEM_Int)==0 ){
67010 if( (pIn3->flags & MEM_Real)==0 ){
@@ -67015,105 +67070,105 @@
67015 }
67016 /* If we reach this point, then the P3 value must be a floating
67017 ** point number. */
67018 assert( (pIn3->flags & MEM_Real)!=0 );
67019
67020 if( u.bb.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.bb.iKey || pIn3->r>0) ){
67021 /* The P3 value is too large in magnitude to be expressed as an
67022 ** integer. */
67023 u.bb.res = 1;
67024 if( pIn3->r<0 ){
67025 if( u.bb.oc>=OP_SeekGe ){ assert( u.bb.oc==OP_SeekGe || u.bb.oc==OP_SeekGt );
67026 rc = sqlite3BtreeFirst(u.bb.pC->pCursor, &u.bb.res);
67027 if( rc!=SQLITE_OK ) goto abort_due_to_error;
67028 }
67029 }else{
67030 if( u.bb.oc<=OP_SeekLe ){ assert( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekLe );
67031 rc = sqlite3BtreeLast(u.bb.pC->pCursor, &u.bb.res);
67032 if( rc!=SQLITE_OK ) goto abort_due_to_error;
67033 }
67034 }
67035 if( u.bb.res ){
67036 pc = pOp->p2 - 1;
67037 }
67038 break;
67039 }else if( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekGe ){
67040 /* Use the ceiling() function to convert real->int */
67041 if( pIn3->r > (double)u.bb.iKey ) u.bb.iKey++;
67042 }else{
67043 /* Use the floor() function to convert real->int */
67044 assert( u.bb.oc==OP_SeekLe || u.bb.oc==OP_SeekGt );
67045 if( pIn3->r < (double)u.bb.iKey ) u.bb.iKey--;
67046 }
67047 }
67048 rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, 0, (u64)u.bb.iKey, 0, &u.bb.res);
67049 if( rc!=SQLITE_OK ){
67050 goto abort_due_to_error;
67051 }
67052 if( u.bb.res==0 ){
67053 u.bb.pC->rowidIsValid = 1;
67054 u.bb.pC->lastRowid = u.bb.iKey;
67055 }
67056 }else{
67057 u.bb.nField = pOp->p4.i;
67058 assert( pOp->p4type==P4_INT32 );
67059 assert( u.bb.nField>0 );
67060 u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo;
67061 u.bb.r.nField = (u16)u.bb.nField;
67062
67063 /* The next line of code computes as follows, only faster:
67064 ** if( u.bb.oc==OP_SeekGt || u.bb.oc==OP_SeekLe ){
67065 ** u.bb.r.flags = UNPACKED_INCRKEY;
67066 ** }else{
67067 ** u.bb.r.flags = 0;
67068 ** }
67069 */
67070 u.bb.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.bb.oc - OP_SeekLt)));
67071 assert( u.bb.oc!=OP_SeekGt || u.bb.r.flags==UNPACKED_INCRKEY );
67072 assert( u.bb.oc!=OP_SeekLe || u.bb.r.flags==UNPACKED_INCRKEY );
67073 assert( u.bb.oc!=OP_SeekGe || u.bb.r.flags==0 );
67074 assert( u.bb.oc!=OP_SeekLt || u.bb.r.flags==0 );
67075
67076 u.bb.r.aMem = &aMem[pOp->p3];
67077 #ifdef SQLITE_DEBUG
67078 { int i; for(i=0; i<u.bb.r.nField; i++) assert( memIsValid(&u.bb.r.aMem[i]) ); }
67079 #endif
67080 ExpandBlob(u.bb.r.aMem);
67081 rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, &u.bb.r, 0, 0, &u.bb.res);
67082 if( rc!=SQLITE_OK ){
67083 goto abort_due_to_error;
67084 }
67085 u.bb.pC->rowidIsValid = 0;
67086 }
67087 u.bb.pC->deferredMoveto = 0;
67088 u.bb.pC->cacheStatus = CACHE_STALE;
67089 #ifdef SQLITE_TEST
67090 sqlite3_search_count++;
67091 #endif
67092 if( u.bb.oc>=OP_SeekGe ){ assert( u.bb.oc==OP_SeekGe || u.bb.oc==OP_SeekGt );
67093 if( u.bb.res<0 || (u.bb.res==0 && u.bb.oc==OP_SeekGt) ){
67094 rc = sqlite3BtreeNext(u.bb.pC->pCursor, &u.bb.res);
67095 if( rc!=SQLITE_OK ) goto abort_due_to_error;
67096 u.bb.pC->rowidIsValid = 0;
67097 }else{
67098 u.bb.res = 0;
67099 }
67100 }else{
67101 assert( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekLe );
67102 if( u.bb.res>0 || (u.bb.res==0 && u.bb.oc==OP_SeekLt) ){
67103 rc = sqlite3BtreePrevious(u.bb.pC->pCursor, &u.bb.res);
67104 if( rc!=SQLITE_OK ) goto abort_due_to_error;
67105 u.bb.pC->rowidIsValid = 0;
67106 }else{
67107 /* u.bb.res might be negative because the table is empty. Check to
67108 ** see if this is the case.
67109 */
67110 u.bb.res = sqlite3BtreeEof(u.bb.pC->pCursor);
67111 }
67112 }
67113 assert( pOp->p2>0 );
67114 if( u.bb.res ){
67115 pc = pOp->p2 - 1;
67116 }
67117 }else{
67118 /* This happens when attempting to open the sqlite3_master table
67119 ** for read access returns SQLITE_EMPTY. In this case always
@@ -67132,24 +67187,24 @@
67132 ** This is actually a deferred seek. Nothing actually happens until
67133 ** the cursor is used to read a record. That way, if no reads
67134 ** occur, no unnecessary I/O happens.
67135 */
67136 case OP_Seek: { /* in2 */
67137 #if 0 /* local variables moved into u.bc */
67138 VdbeCursor *pC;
67139 #endif /* local variables moved into u.bc */
67140
67141 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67142 u.bc.pC = p->apCsr[pOp->p1];
67143 assert( u.bc.pC!=0 );
67144 if( ALWAYS(u.bc.pC->pCursor!=0) ){
67145 assert( u.bc.pC->isTable );
67146 u.bc.pC->nullRow = 0;
67147 pIn2 = &aMem[pOp->p2];
67148 u.bc.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
67149 u.bc.pC->rowidIsValid = 0;
67150 u.bc.pC->deferredMoveto = 1;
67151 }
67152 break;
67153 }
67154
67155
@@ -67177,67 +67232,67 @@
67177 **
67178 ** See also: Found, NotExists, IsUnique
67179 */
67180 case OP_NotFound: /* jump, in3 */
67181 case OP_Found: { /* jump, in3 */
67182 #if 0 /* local variables moved into u.bd */
67183 int alreadyExists;
67184 VdbeCursor *pC;
67185 int res;
67186 char *pFree;
67187 UnpackedRecord *pIdxKey;
67188 UnpackedRecord r;
67189 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
67190 #endif /* local variables moved into u.bd */
67191
67192 #ifdef SQLITE_TEST
67193 sqlite3_found_count++;
67194 #endif
67195
67196 u.bd.alreadyExists = 0;
67197 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67198 assert( pOp->p4type==P4_INT32 );
67199 u.bd.pC = p->apCsr[pOp->p1];
67200 assert( u.bd.pC!=0 );
67201 pIn3 = &aMem[pOp->p3];
67202 if( ALWAYS(u.bd.pC->pCursor!=0) ){
67203
67204 assert( u.bd.pC->isTable==0 );
67205 if( pOp->p4.i>0 ){
67206 u.bd.r.pKeyInfo = u.bd.pC->pKeyInfo;
67207 u.bd.r.nField = (u16)pOp->p4.i;
67208 u.bd.r.aMem = pIn3;
67209 #ifdef SQLITE_DEBUG
67210 { int i; for(i=0; i<u.bd.r.nField; i++) assert( memIsValid(&u.bd.r.aMem[i]) ); }
67211 #endif
67212 u.bd.r.flags = UNPACKED_PREFIX_MATCH;
67213 u.bd.pIdxKey = &u.bd.r;
67214 }else{
67215 u.bd.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
67216 u.bd.pC->pKeyInfo, u.bd.aTempRec, sizeof(u.bd.aTempRec), &u.bd.pFree
67217 );
67218 if( u.bd.pIdxKey==0 ) goto no_mem;
67219 assert( pIn3->flags & MEM_Blob );
67220 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
67221 sqlite3VdbeRecordUnpack(u.bd.pC->pKeyInfo, pIn3->n, pIn3->z, u.bd.pIdxKey);
67222 u.bd.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
67223 }
67224 rc = sqlite3BtreeMovetoUnpacked(u.bd.pC->pCursor, u.bd.pIdxKey, 0, 0, &u.bd.res);
67225 if( pOp->p4.i==0 ){
67226 sqlite3DbFree(db, u.bd.pFree);
67227 }
67228 if( rc!=SQLITE_OK ){
67229 break;
67230 }
67231 u.bd.alreadyExists = (u.bd.res==0);
67232 u.bd.pC->deferredMoveto = 0;
67233 u.bd.pC->cacheStatus = CACHE_STALE;
67234 }
67235 if( pOp->opcode==OP_Found ){
67236 if( u.bd.alreadyExists ) pc = pOp->p2 - 1;
67237 }else{
67238 if( !u.bd.alreadyExists ) pc = pOp->p2 - 1;
67239 }
67240 break;
67241 }
67242
67243 /* Opcode: IsUnique P1 P2 P3 P4 *
@@ -67265,67 +67320,67 @@
67265 ** instruction.
67266 **
67267 ** See also: NotFound, NotExists, Found
67268 */
67269 case OP_IsUnique: { /* jump, in3 */
67270 #if 0 /* local variables moved into u.be */
67271 u16 ii;
67272 VdbeCursor *pCx;
67273 BtCursor *pCrsr;
67274 u16 nField;
67275 Mem *aMx;
67276 UnpackedRecord r; /* B-Tree index search key */
67277 i64 R; /* Rowid stored in register P3 */
67278 #endif /* local variables moved into u.be */
67279
67280 pIn3 = &aMem[pOp->p3];
67281 u.be.aMx = &aMem[pOp->p4.i];
67282 /* Assert that the values of parameters P1 and P4 are in range. */
67283 assert( pOp->p4type==P4_INT32 );
67284 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
67285 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67286
67287 /* Find the index cursor. */
67288 u.be.pCx = p->apCsr[pOp->p1];
67289 assert( u.be.pCx->deferredMoveto==0 );
67290 u.be.pCx->seekResult = 0;
67291 u.be.pCx->cacheStatus = CACHE_STALE;
67292 u.be.pCrsr = u.be.pCx->pCursor;
67293
67294 /* If any of the values are NULL, take the jump. */
67295 u.be.nField = u.be.pCx->pKeyInfo->nField;
67296 for(u.be.ii=0; u.be.ii<u.be.nField; u.be.ii++){
67297 if( u.be.aMx[u.be.ii].flags & MEM_Null ){
67298 pc = pOp->p2 - 1;
67299 u.be.pCrsr = 0;
67300 break;
67301 }
67302 }
67303 assert( (u.be.aMx[u.be.nField].flags & MEM_Null)==0 );
67304
67305 if( u.be.pCrsr!=0 ){
67306 /* Populate the index search key. */
67307 u.be.r.pKeyInfo = u.be.pCx->pKeyInfo;
67308 u.be.r.nField = u.be.nField + 1;
67309 u.be.r.flags = UNPACKED_PREFIX_SEARCH;
67310 u.be.r.aMem = u.be.aMx;
67311 #ifdef SQLITE_DEBUG
67312 { int i; for(i=0; i<u.be.r.nField; i++) assert( memIsValid(&u.be.r.aMem[i]) ); }
67313 #endif
67314
67315 /* Extract the value of u.be.R from register P3. */
67316 sqlite3VdbeMemIntegerify(pIn3);
67317 u.be.R = pIn3->u.i;
67318
67319 /* Search the B-Tree index. If no conflicting record is found, jump
67320 ** to P2. Otherwise, copy the rowid of the conflicting record to
67321 ** register P3 and fall through to the next instruction. */
67322 rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, &u.be.r, 0, 0, &u.be.pCx->seekResult);
67323 if( (u.be.r.flags & UNPACKED_PREFIX_SEARCH) || u.be.r.rowid==u.be.R ){
67324 pc = pOp->p2 - 1;
67325 }else{
67326 pIn3->u.i = u.be.r.rowid;
67327 }
67328 }
67329 break;
67330 }
67331
@@ -67342,46 +67397,46 @@
67342 ** P1 is an index.
67343 **
67344 ** See also: Found, NotFound, IsUnique
67345 */
67346 case OP_NotExists: { /* jump, in3 */
67347 #if 0 /* local variables moved into u.bf */
67348 VdbeCursor *pC;
67349 BtCursor *pCrsr;
67350 int res;
67351 u64 iKey;
67352 #endif /* local variables moved into u.bf */
67353
67354 pIn3 = &aMem[pOp->p3];
67355 assert( pIn3->flags & MEM_Int );
67356 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67357 u.bf.pC = p->apCsr[pOp->p1];
67358 assert( u.bf.pC!=0 );
67359 assert( u.bf.pC->isTable );
67360 assert( u.bf.pC->pseudoTableReg==0 );
67361 u.bf.pCrsr = u.bf.pC->pCursor;
67362 if( ALWAYS(u.bf.pCrsr!=0) ){
67363 u.bf.res = 0;
67364 u.bf.iKey = pIn3->u.i;
67365 rc = sqlite3BtreeMovetoUnpacked(u.bf.pCrsr, 0, u.bf.iKey, 0, &u.bf.res);
67366 u.bf.pC->lastRowid = pIn3->u.i;
67367 u.bf.pC->rowidIsValid = u.bf.res==0 ?1:0;
67368 u.bf.pC->nullRow = 0;
67369 u.bf.pC->cacheStatus = CACHE_STALE;
67370 u.bf.pC->deferredMoveto = 0;
67371 if( u.bf.res!=0 ){
67372 pc = pOp->p2 - 1;
67373 assert( u.bf.pC->rowidIsValid==0 );
67374 }
67375 u.bf.pC->seekResult = u.bf.res;
67376 }else{
67377 /* This happens when an attempt to open a read cursor on the
67378 ** sqlite_master table returns SQLITE_EMPTY.
67379 */
67380 pc = pOp->p2 - 1;
67381 assert( u.bf.pC->rowidIsValid==0 );
67382 u.bf.pC->seekResult = 0;
67383 }
67384 break;
67385 }
67386
67387 /* Opcode: Sequence P1 P2 * * *
@@ -67412,25 +67467,25 @@
67412 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
67413 ** generated record number. This P3 mechanism is used to help implement the
67414 ** AUTOINCREMENT feature.
67415 */
67416 case OP_NewRowid: { /* out2-prerelease */
67417 #if 0 /* local variables moved into u.bg */
67418 i64 v; /* The new rowid */
67419 VdbeCursor *pC; /* Cursor of table to get the new rowid */
67420 int res; /* Result of an sqlite3BtreeLast() */
67421 int cnt; /* Counter to limit the number of searches */
67422 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
67423 VdbeFrame *pFrame; /* Root frame of VDBE */
67424 #endif /* local variables moved into u.bg */
67425
67426 u.bg.v = 0;
67427 u.bg.res = 0;
67428 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67429 u.bg.pC = p->apCsr[pOp->p1];
67430 assert( u.bg.pC!=0 );
67431 if( NEVER(u.bg.pC->pCursor==0) ){
67432 /* The zero initialization above is all that is needed */
67433 }else{
67434 /* The next rowid or record number (different terms for the same
67435 ** thing) is obtained in a two-step algorithm.
67436 **
@@ -67442,11 +67497,11 @@
67442 ** The second algorithm is to select a rowid at random and see if
67443 ** it already exists in the table. If it does not exist, we have
67444 ** succeeded. If the random rowid does exist, we select a new one
67445 ** and try again, up to 100 times.
67446 */
67447 assert( u.bg.pC->isTable );
67448
67449 #ifdef SQLITE_32BIT_ROWID
67450 # define MAX_ROWID 0x7fffffff
67451 #else
67452 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
@@ -67454,101 +67509,101 @@
67454 ** to provide the constant while making all compilers happy.
67455 */
67456 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
67457 #endif
67458
67459 if( !u.bg.pC->useRandomRowid ){
67460 u.bg.v = sqlite3BtreeGetCachedRowid(u.bg.pC->pCursor);
67461 if( u.bg.v==0 ){
67462 rc = sqlite3BtreeLast(u.bg.pC->pCursor, &u.bg.res);
67463 if( rc!=SQLITE_OK ){
67464 goto abort_due_to_error;
67465 }
67466 if( u.bg.res ){
67467 u.bg.v = 1; /* IMP: R-61914-48074 */
67468 }else{
67469 assert( sqlite3BtreeCursorIsValid(u.bg.pC->pCursor) );
67470 rc = sqlite3BtreeKeySize(u.bg.pC->pCursor, &u.bg.v);
67471 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
67472 if( u.bg.v>=MAX_ROWID ){
67473 u.bg.pC->useRandomRowid = 1;
67474 }else{
67475 u.bg.v++; /* IMP: R-29538-34987 */
67476 }
67477 }
67478 }
67479
67480 #ifndef SQLITE_OMIT_AUTOINCREMENT
67481 if( pOp->p3 ){
67482 /* Assert that P3 is a valid memory cell. */
67483 assert( pOp->p3>0 );
67484 if( p->pFrame ){
67485 for(u.bg.pFrame=p->pFrame; u.bg.pFrame->pParent; u.bg.pFrame=u.bg.pFrame->pParent);
67486 /* Assert that P3 is a valid memory cell. */
67487 assert( pOp->p3<=u.bg.pFrame->nMem );
67488 u.bg.pMem = &u.bg.pFrame->aMem[pOp->p3];
67489 }else{
67490 /* Assert that P3 is a valid memory cell. */
67491 assert( pOp->p3<=p->nMem );
67492 u.bg.pMem = &aMem[pOp->p3];
67493 memAboutToChange(p, u.bg.pMem);
67494 }
67495 assert( memIsValid(u.bg.pMem) );
67496
67497 REGISTER_TRACE(pOp->p3, u.bg.pMem);
67498 sqlite3VdbeMemIntegerify(u.bg.pMem);
67499 assert( (u.bg.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
67500 if( u.bg.pMem->u.i==MAX_ROWID || u.bg.pC->useRandomRowid ){
67501 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
67502 goto abort_due_to_error;
67503 }
67504 if( u.bg.v<u.bg.pMem->u.i+1 ){
67505 u.bg.v = u.bg.pMem->u.i + 1;
67506 }
67507 u.bg.pMem->u.i = u.bg.v;
67508 }
67509 #endif
67510
67511 sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, u.bg.v<MAX_ROWID ? u.bg.v+1 : 0);
67512 }
67513 if( u.bg.pC->useRandomRowid ){
67514 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
67515 ** largest possible integer (9223372036854775807) then the database
67516 ** engine starts picking positive candidate ROWIDs at random until
67517 ** it finds one that is not previously used. */
67518 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
67519 ** an AUTOINCREMENT table. */
67520 /* on the first attempt, simply do one more than previous */
67521 u.bg.v = lastRowid;
67522 u.bg.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67523 u.bg.v++; /* ensure non-zero */
67524 u.bg.cnt = 0;
67525 while( ((rc = sqlite3BtreeMovetoUnpacked(u.bg.pC->pCursor, 0, (u64)u.bg.v,
67526 0, &u.bg.res))==SQLITE_OK)
67527 && (u.bg.res==0)
67528 && (++u.bg.cnt<100)){
67529 /* collision - try another random rowid */
67530 sqlite3_randomness(sizeof(u.bg.v), &u.bg.v);
67531 if( u.bg.cnt<5 ){
67532 /* try "small" random rowids for the initial attempts */
67533 u.bg.v &= 0xffffff;
67534 }else{
67535 u.bg.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67536 }
67537 u.bg.v++; /* ensure non-zero */
67538 }
67539 if( rc==SQLITE_OK && u.bg.res==0 ){
67540 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
67541 goto abort_due_to_error;
67542 }
67543 assert( u.bg.v>0 ); /* EV: R-40812-03570 */
67544 }
67545 u.bg.pC->rowidIsValid = 0;
67546 u.bg.pC->deferredMoveto = 0;
67547 u.bg.pC->cacheStatus = CACHE_STALE;
67548 }
67549 pOut->u.i = u.bg.v;
67550 break;
67551 }
67552
67553 /* Opcode: Insert P1 P2 P3 P4 P5
67554 **
@@ -67594,74 +67649,74 @@
67594 ** This works exactly like OP_Insert except that the key is the
67595 ** integer value P3, not the value of the integer stored in register P3.
67596 */
67597 case OP_Insert:
67598 case OP_InsertInt: {
67599 #if 0 /* local variables moved into u.bh */
67600 Mem *pData; /* MEM cell holding data for the record to be inserted */
67601 Mem *pKey; /* MEM cell holding key for the record */
67602 i64 iKey; /* The integer ROWID or key for the record to be inserted */
67603 VdbeCursor *pC; /* Cursor to table into which insert is written */
67604 int nZero; /* Number of zero-bytes to append */
67605 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
67606 const char *zDb; /* database name - used by the update hook */
67607 const char *zTbl; /* Table name - used by the opdate hook */
67608 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
67609 #endif /* local variables moved into u.bh */
67610
67611 u.bh.pData = &aMem[pOp->p2];
67612 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67613 assert( memIsValid(u.bh.pData) );
67614 u.bh.pC = p->apCsr[pOp->p1];
67615 assert( u.bh.pC!=0 );
67616 assert( u.bh.pC->pCursor!=0 );
67617 assert( u.bh.pC->pseudoTableReg==0 );
67618 assert( u.bh.pC->isTable );
67619 REGISTER_TRACE(pOp->p2, u.bh.pData);
67620
67621 if( pOp->opcode==OP_Insert ){
67622 u.bh.pKey = &aMem[pOp->p3];
67623 assert( u.bh.pKey->flags & MEM_Int );
67624 assert( memIsValid(u.bh.pKey) );
67625 REGISTER_TRACE(pOp->p3, u.bh.pKey);
67626 u.bh.iKey = u.bh.pKey->u.i;
67627 }else{
67628 assert( pOp->opcode==OP_InsertInt );
67629 u.bh.iKey = pOp->p3;
67630 }
67631
67632 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
67633 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bh.iKey;
67634 if( u.bh.pData->flags & MEM_Null ){
67635 u.bh.pData->z = 0;
67636 u.bh.pData->n = 0;
67637 }else{
67638 assert( u.bh.pData->flags & (MEM_Blob|MEM_Str) );
67639 }
67640 u.bh.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bh.pC->seekResult : 0);
67641 if( u.bh.pData->flags & MEM_Zero ){
67642 u.bh.nZero = u.bh.pData->u.nZero;
67643 }else{
67644 u.bh.nZero = 0;
67645 }
67646 sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0);
67647 rc = sqlite3BtreeInsert(u.bh.pC->pCursor, 0, u.bh.iKey,
67648 u.bh.pData->z, u.bh.pData->n, u.bh.nZero,
67649 pOp->p5 & OPFLAG_APPEND, u.bh.seekResult
67650 );
67651 u.bh.pC->rowidIsValid = 0;
67652 u.bh.pC->deferredMoveto = 0;
67653 u.bh.pC->cacheStatus = CACHE_STALE;
67654
67655 /* Invoke the update-hook if required. */
67656 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67657 u.bh.zDb = db->aDb[u.bh.pC->iDb].zName;
67658 u.bh.zTbl = pOp->p4.z;
67659 u.bh.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
67660 assert( u.bh.pC->isTable );
67661 db->xUpdateCallback(db->pUpdateArg, u.bh.op, u.bh.zDb, u.bh.zTbl, u.bh.iKey);
67662 assert( u.bh.pC->iDb>=0 );
67663 }
67664 break;
67665 }
67666
67667 /* Opcode: Delete P1 P2 * P4 *
@@ -67683,51 +67738,51 @@
67683 ** pointing to. The update hook will be invoked, if it exists.
67684 ** If P4 is not NULL then the P1 cursor must have been positioned
67685 ** using OP_NotFound prior to invoking this opcode.
67686 */
67687 case OP_Delete: {
67688 #if 0 /* local variables moved into u.bi */
67689 i64 iKey;
67690 VdbeCursor *pC;
67691 #endif /* local variables moved into u.bi */
67692
67693 u.bi.iKey = 0;
67694 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67695 u.bi.pC = p->apCsr[pOp->p1];
67696 assert( u.bi.pC!=0 );
67697 assert( u.bi.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
67698
67699 /* If the update-hook will be invoked, set u.bi.iKey to the rowid of the
67700 ** row being deleted.
67701 */
67702 if( db->xUpdateCallback && pOp->p4.z ){
67703 assert( u.bi.pC->isTable );
67704 assert( u.bi.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
67705 u.bi.iKey = u.bi.pC->lastRowid;
67706 }
67707
67708 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
67709 ** OP_Column on the same table without any intervening operations that
67710 ** might move or invalidate the cursor. Hence cursor u.bi.pC is always pointing
67711 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
67712 ** below is always a no-op and cannot fail. We will run it anyhow, though,
67713 ** to guard against future changes to the code generator.
67714 **/
67715 assert( u.bi.pC->deferredMoveto==0 );
67716 rc = sqlite3VdbeCursorMoveto(u.bi.pC);
67717 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67718
67719 sqlite3BtreeSetCachedRowid(u.bi.pC->pCursor, 0);
67720 rc = sqlite3BtreeDelete(u.bi.pC->pCursor);
67721 u.bi.pC->cacheStatus = CACHE_STALE;
67722
67723 /* Invoke the update-hook if required. */
67724 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67725 const char *zDb = db->aDb[u.bi.pC->iDb].zName;
67726 const char *zTbl = pOp->p4.z;
67727 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bi.iKey);
67728 assert( u.bi.pC->iDb>=0 );
67729 }
67730 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
67731 break;
67732 }
67733 /* Opcode: ResetCount * * * * *
@@ -67749,20 +67804,20 @@
67749 ** register P3 with the entry that the sorter cursor currently points to.
67750 ** If, excluding the rowid fields at the end, the two records are a match,
67751 ** fall through to the next instruction. Otherwise, jump to instruction P2.
67752 */
67753 case OP_SorterCompare: {
67754 #if 0 /* local variables moved into u.bj */
67755 VdbeCursor *pC;
67756 int res;
67757 #endif /* local variables moved into u.bj */
67758
67759 u.bj.pC = p->apCsr[pOp->p1];
67760 assert( isSorter(u.bj.pC) );
67761 pIn3 = &aMem[pOp->p3];
67762 rc = sqlite3VdbeSorterCompare(u.bj.pC, pIn3, &u.bj.res);
67763 if( u.bj.res ){
67764 pc = pOp->p2-1;
67765 }
67766 break;
67767 };
67768
@@ -67769,18 +67824,19 @@
67769 /* Opcode: SorterData P1 P2 * * *
67770 **
67771 ** Write into register P2 the current sorter data for sorter cursor P1.
67772 */
67773 case OP_SorterData: {
67774 #if 0 /* local variables moved into u.bk */
67775 VdbeCursor *pC;
67776 #endif /* local variables moved into u.bk */
 
67777 #ifndef SQLITE_OMIT_MERGE_SORT
67778 pOut = &aMem[pOp->p2];
67779 u.bk.pC = p->apCsr[pOp->p1];
67780 assert( u.bk.pC->isSorter );
67781 rc = sqlite3VdbeSorterRowkey(u.bk.pC, pOut);
67782 #else
67783 pOp->opcode = OP_RowKey;
67784 pc--;
67785 #endif
67786 break;
@@ -67806,66 +67862,66 @@
67806 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
67807 ** of a real table, not a pseudo-table.
67808 */
67809 case OP_RowKey:
67810 case OP_RowData: {
67811 #if 0 /* local variables moved into u.bl */
67812 VdbeCursor *pC;
67813 BtCursor *pCrsr;
67814 u32 n;
67815 i64 n64;
67816 #endif /* local variables moved into u.bl */
67817
67818 pOut = &aMem[pOp->p2];
67819 memAboutToChange(p, pOut);
67820
67821 /* Note that RowKey and RowData are really exactly the same instruction */
67822 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67823 u.bl.pC = p->apCsr[pOp->p1];
67824 assert( u.bl.pC->isSorter==0 );
67825 assert( u.bl.pC->isTable || pOp->opcode!=OP_RowData );
67826 assert( u.bl.pC->isIndex || pOp->opcode==OP_RowData );
67827 assert( u.bl.pC!=0 );
67828 assert( u.bl.pC->nullRow==0 );
67829 assert( u.bl.pC->pseudoTableReg==0 );
67830 assert( u.bl.pC->pCursor!=0 );
67831 u.bl.pCrsr = u.bl.pC->pCursor;
67832 assert( sqlite3BtreeCursorIsValid(u.bl.pCrsr) );
67833
67834 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
67835 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
67836 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
67837 ** a no-op and can never fail. But we leave it in place as a safety.
67838 */
67839 assert( u.bl.pC->deferredMoveto==0 );
67840 rc = sqlite3VdbeCursorMoveto(u.bl.pC);
67841 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67842
67843 if( u.bl.pC->isIndex ){
67844 assert( !u.bl.pC->isTable );
67845 VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bl.pCrsr, &u.bl.n64);
67846 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
67847 if( u.bl.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67848 goto too_big;
67849 }
67850 u.bl.n = (u32)u.bl.n64;
67851 }else{
67852 VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bl.pCrsr, &u.bl.n);
67853 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
67854 if( u.bl.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
67855 goto too_big;
67856 }
67857 }
67858 if( sqlite3VdbeMemGrow(pOut, u.bl.n, 0) ){
67859 goto no_mem;
67860 }
67861 pOut->n = u.bl.n;
67862 MemSetTypeFlag(pOut, MEM_Blob);
67863 if( u.bl.pC->isIndex ){
67864 rc = sqlite3BtreeKey(u.bl.pCrsr, 0, u.bl.n, pOut->z);
67865 }else{
67866 rc = sqlite3BtreeData(u.bl.pCrsr, 0, u.bl.n, pOut->z);
67867 }
67868 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
67869 UPDATE_MAX_BLOBSIZE(pOut);
67870 break;
67871 }
@@ -67878,46 +67934,46 @@
67878 ** P1 can be either an ordinary table or a virtual table. There used to
67879 ** be a separate OP_VRowid opcode for use with virtual tables, but this
67880 ** one opcode now works for both table types.
67881 */
67882 case OP_Rowid: { /* out2-prerelease */
67883 #if 0 /* local variables moved into u.bm */
67884 VdbeCursor *pC;
67885 i64 v;
67886 sqlite3_vtab *pVtab;
67887 const sqlite3_module *pModule;
67888 #endif /* local variables moved into u.bm */
67889
67890 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67891 u.bm.pC = p->apCsr[pOp->p1];
67892 assert( u.bm.pC!=0 );
67893 assert( u.bm.pC->pseudoTableReg==0 );
67894 if( u.bm.pC->nullRow ){
67895 pOut->flags = MEM_Null;
67896 break;
67897 }else if( u.bm.pC->deferredMoveto ){
67898 u.bm.v = u.bm.pC->movetoTarget;
67899 #ifndef SQLITE_OMIT_VIRTUALTABLE
67900 }else if( u.bm.pC->pVtabCursor ){
67901 u.bm.pVtab = u.bm.pC->pVtabCursor->pVtab;
67902 u.bm.pModule = u.bm.pVtab->pModule;
67903 assert( u.bm.pModule->xRowid );
67904 rc = u.bm.pModule->xRowid(u.bm.pC->pVtabCursor, &u.bm.v);
67905 importVtabErrMsg(p, u.bm.pVtab);
67906 #endif /* SQLITE_OMIT_VIRTUALTABLE */
67907 }else{
67908 assert( u.bm.pC->pCursor!=0 );
67909 rc = sqlite3VdbeCursorMoveto(u.bm.pC);
67910 if( rc ) goto abort_due_to_error;
67911 if( u.bm.pC->rowidIsValid ){
67912 u.bm.v = u.bm.pC->lastRowid;
67913 }else{
67914 rc = sqlite3BtreeKeySize(u.bm.pC->pCursor, &u.bm.v);
67915 assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
67916 }
67917 }
67918 pOut->u.i = u.bm.v;
67919 break;
67920 }
67921
67922 /* Opcode: NullRow P1 * * * *
67923 **
@@ -67924,22 +67980,22 @@
67924 ** Move the cursor P1 to a null row. Any OP_Column operations
67925 ** that occur while the cursor is on the null row will always
67926 ** write a NULL.
67927 */
67928 case OP_NullRow: {
67929 #if 0 /* local variables moved into u.bn */
67930 VdbeCursor *pC;
67931 #endif /* local variables moved into u.bn */
67932
67933 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67934 u.bn.pC = p->apCsr[pOp->p1];
67935 assert( u.bn.pC!=0 );
67936 u.bn.pC->nullRow = 1;
67937 u.bn.pC->rowidIsValid = 0;
67938 assert( u.bn.pC->pCursor || u.bn.pC->pVtabCursor );
67939 if( u.bn.pC->pCursor ){
67940 sqlite3BtreeClearCursor(u.bn.pC->pCursor);
67941 }
67942 break;
67943 }
67944
67945 /* Opcode: Last P1 P2 * * *
@@ -67949,29 +68005,29 @@
67949 ** If the table or index is empty and P2>0, then jump immediately to P2.
67950 ** If P2 is 0 or if the table or index is not empty, fall through
67951 ** to the following instruction.
67952 */
67953 case OP_Last: { /* jump */
67954 #if 0 /* local variables moved into u.bo */
67955 VdbeCursor *pC;
67956 BtCursor *pCrsr;
67957 int res;
67958 #endif /* local variables moved into u.bo */
67959
67960 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67961 u.bo.pC = p->apCsr[pOp->p1];
67962 assert( u.bo.pC!=0 );
67963 u.bo.pCrsr = u.bo.pC->pCursor;
67964 u.bo.res = 0;
67965 if( ALWAYS(u.bo.pCrsr!=0) ){
67966 rc = sqlite3BtreeLast(u.bo.pCrsr, &u.bo.res);
67967 }
67968 u.bo.pC->nullRow = (u8)u.bo.res;
67969 u.bo.pC->deferredMoveto = 0;
67970 u.bo.pC->rowidIsValid = 0;
67971 u.bo.pC->cacheStatus = CACHE_STALE;
67972 if( pOp->p2>0 && u.bo.res ){
67973 pc = pOp->p2 - 1;
67974 }
67975 break;
67976 }
67977
@@ -68007,35 +68063,35 @@
68007 ** If the table or index is empty and P2>0, then jump immediately to P2.
68008 ** If P2 is 0 or if the table or index is not empty, fall through
68009 ** to the following instruction.
68010 */
68011 case OP_Rewind: { /* jump */
68012 #if 0 /* local variables moved into u.bp */
68013 VdbeCursor *pC;
68014 BtCursor *pCrsr;
68015 int res;
68016 #endif /* local variables moved into u.bp */
68017
68018 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68019 u.bp.pC = p->apCsr[pOp->p1];
68020 assert( u.bp.pC!=0 );
68021 assert( u.bp.pC->isSorter==(pOp->opcode==OP_SorterSort) );
68022 u.bp.res = 1;
68023 if( isSorter(u.bp.pC) ){
68024 rc = sqlite3VdbeSorterRewind(db, u.bp.pC, &u.bp.res);
68025 }else{
68026 u.bp.pCrsr = u.bp.pC->pCursor;
68027 assert( u.bp.pCrsr );
68028 rc = sqlite3BtreeFirst(u.bp.pCrsr, &u.bp.res);
68029 u.bp.pC->atFirst = u.bp.res==0 ?1:0;
68030 u.bp.pC->deferredMoveto = 0;
68031 u.bp.pC->cacheStatus = CACHE_STALE;
68032 u.bp.pC->rowidIsValid = 0;
68033 }
68034 u.bp.pC->nullRow = (u8)u.bp.res;
68035 assert( pOp->p2>0 && pOp->p2<p->nOp );
68036 if( u.bp.res ){
68037 pc = pOp->p2 - 1;
68038 }
68039 break;
68040 }
68041
@@ -68075,44 +68131,44 @@
68075 #ifdef SQLITE_OMIT_MERGE_SORT
68076 pOp->opcode = OP_Next;
68077 #endif
68078 case OP_Prev: /* jump */
68079 case OP_Next: { /* jump */
68080 #if 0 /* local variables moved into u.bq */
68081 VdbeCursor *pC;
68082 int res;
68083 #endif /* local variables moved into u.bq */
68084
68085 CHECK_FOR_INTERRUPT;
68086 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68087 assert( pOp->p5<=ArraySize(p->aCounter) );
68088 u.bq.pC = p->apCsr[pOp->p1];
68089 if( u.bq.pC==0 ){
68090 break; /* See ticket #2273 */
68091 }
68092 assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterNext) );
68093 if( isSorter(u.bq.pC) ){
68094 assert( pOp->opcode==OP_SorterNext );
68095 rc = sqlite3VdbeSorterNext(db, u.bq.pC, &u.bq.res);
68096 }else{
68097 u.bq.res = 1;
68098 assert( u.bq.pC->deferredMoveto==0 );
68099 assert( u.bq.pC->pCursor );
68100 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
68101 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
68102 rc = pOp->p4.xAdvance(u.bq.pC->pCursor, &u.bq.res);
68103 }
68104 u.bq.pC->nullRow = (u8)u.bq.res;
68105 u.bq.pC->cacheStatus = CACHE_STALE;
68106 if( u.bq.res==0 ){
68107 pc = pOp->p2 - 1;
68108 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
68109 #ifdef SQLITE_TEST
68110 sqlite3_search_count++;
68111 #endif
68112 }
68113 u.bq.pC->rowidIsValid = 0;
68114 break;
68115 }
68116
68117 /* Opcode: IdxInsert P1 P2 P3 * P5
68118 **
@@ -68129,38 +68185,38 @@
68129 case OP_SorterInsert: /* in2 */
68130 #ifdef SQLITE_OMIT_MERGE_SORT
68131 pOp->opcode = OP_IdxInsert;
68132 #endif
68133 case OP_IdxInsert: { /* in2 */
68134 #if 0 /* local variables moved into u.br */
68135 VdbeCursor *pC;
68136 BtCursor *pCrsr;
68137 int nKey;
68138 const char *zKey;
68139 #endif /* local variables moved into u.br */
68140
68141 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68142 u.br.pC = p->apCsr[pOp->p1];
68143 assert( u.br.pC!=0 );
68144 assert( u.br.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
68145 pIn2 = &aMem[pOp->p2];
68146 assert( pIn2->flags & MEM_Blob );
68147 u.br.pCrsr = u.br.pC->pCursor;
68148 if( ALWAYS(u.br.pCrsr!=0) ){
68149 assert( u.br.pC->isTable==0 );
68150 rc = ExpandBlob(pIn2);
68151 if( rc==SQLITE_OK ){
68152 if( isSorter(u.br.pC) ){
68153 rc = sqlite3VdbeSorterWrite(db, u.br.pC, pIn2);
68154 }else{
68155 u.br.nKey = pIn2->n;
68156 u.br.zKey = pIn2->z;
68157 rc = sqlite3BtreeInsert(u.br.pCrsr, u.br.zKey, u.br.nKey, "", 0, 0, pOp->p3,
68158 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.br.pC->seekResult : 0)
68159 );
68160 assert( u.br.pC->deferredMoveto==0 );
68161 u.br.pC->cacheStatus = CACHE_STALE;
68162 }
68163 }
68164 }
68165 break;
68166 }
@@ -68170,37 +68226,37 @@
68170 ** The content of P3 registers starting at register P2 form
68171 ** an unpacked index key. This opcode removes that entry from the
68172 ** index opened by cursor P1.
68173 */
68174 case OP_IdxDelete: {
68175 #if 0 /* local variables moved into u.bs */
68176 VdbeCursor *pC;
68177 BtCursor *pCrsr;
68178 int res;
68179 UnpackedRecord r;
68180 #endif /* local variables moved into u.bs */
68181
68182 assert( pOp->p3>0 );
68183 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
68184 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68185 u.bs.pC = p->apCsr[pOp->p1];
68186 assert( u.bs.pC!=0 );
68187 u.bs.pCrsr = u.bs.pC->pCursor;
68188 if( ALWAYS(u.bs.pCrsr!=0) ){
68189 u.bs.r.pKeyInfo = u.bs.pC->pKeyInfo;
68190 u.bs.r.nField = (u16)pOp->p3;
68191 u.bs.r.flags = 0;
68192 u.bs.r.aMem = &aMem[pOp->p2];
68193 #ifdef SQLITE_DEBUG
68194 { int i; for(i=0; i<u.bs.r.nField; i++) assert( memIsValid(&u.bs.r.aMem[i]) ); }
68195 #endif
68196 rc = sqlite3BtreeMovetoUnpacked(u.bs.pCrsr, &u.bs.r, 0, 0, &u.bs.res);
68197 if( rc==SQLITE_OK && u.bs.res==0 ){
68198 rc = sqlite3BtreeDelete(u.bs.pCrsr);
68199 }
68200 assert( u.bs.pC->deferredMoveto==0 );
68201 u.bs.pC->cacheStatus = CACHE_STALE;
68202 }
68203 break;
68204 }
68205
68206 /* Opcode: IdxRowid P1 P2 * * *
@@ -68210,32 +68266,32 @@
68210 ** the rowid of the table entry to which this index entry points.
68211 **
68212 ** See also: Rowid, MakeRecord.
68213 */
68214 case OP_IdxRowid: { /* out2-prerelease */
68215 #if 0 /* local variables moved into u.bt */
68216 BtCursor *pCrsr;
68217 VdbeCursor *pC;
68218 i64 rowid;
68219 #endif /* local variables moved into u.bt */
68220
68221 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68222 u.bt.pC = p->apCsr[pOp->p1];
68223 assert( u.bt.pC!=0 );
68224 u.bt.pCrsr = u.bt.pC->pCursor;
68225 pOut->flags = MEM_Null;
68226 if( ALWAYS(u.bt.pCrsr!=0) ){
68227 rc = sqlite3VdbeCursorMoveto(u.bt.pC);
68228 if( NEVER(rc) ) goto abort_due_to_error;
68229 assert( u.bt.pC->deferredMoveto==0 );
68230 assert( u.bt.pC->isTable==0 );
68231 if( !u.bt.pC->nullRow ){
68232 rc = sqlite3VdbeIdxRowid(db, u.bt.pCrsr, &u.bt.rowid);
68233 if( rc!=SQLITE_OK ){
68234 goto abort_due_to_error;
68235 }
68236 pOut->u.i = u.bt.rowid;
68237 pOut->flags = MEM_Int;
68238 }
68239 }
68240 break;
68241 }
@@ -68266,43 +68322,43 @@
68266 ** If P5 is non-zero then the key value is increased by an epsilon prior
68267 ** to the comparison. This makes the opcode work like IdxLE.
68268 */
68269 case OP_IdxLT: /* jump */
68270 case OP_IdxGE: { /* jump */
68271 #if 0 /* local variables moved into u.bu */
68272 VdbeCursor *pC;
68273 int res;
68274 UnpackedRecord r;
68275 #endif /* local variables moved into u.bu */
68276
68277 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68278 u.bu.pC = p->apCsr[pOp->p1];
68279 assert( u.bu.pC!=0 );
68280 assert( u.bu.pC->isOrdered );
68281 if( ALWAYS(u.bu.pC->pCursor!=0) ){
68282 assert( u.bu.pC->deferredMoveto==0 );
68283 assert( pOp->p5==0 || pOp->p5==1 );
68284 assert( pOp->p4type==P4_INT32 );
68285 u.bu.r.pKeyInfo = u.bu.pC->pKeyInfo;
68286 u.bu.r.nField = (u16)pOp->p4.i;
68287 if( pOp->p5 ){
68288 u.bu.r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
68289 }else{
68290 u.bu.r.flags = UNPACKED_PREFIX_MATCH;
68291 }
68292 u.bu.r.aMem = &aMem[pOp->p3];
68293 #ifdef SQLITE_DEBUG
68294 { int i; for(i=0; i<u.bu.r.nField; i++) assert( memIsValid(&u.bu.r.aMem[i]) ); }
68295 #endif
68296 rc = sqlite3VdbeIdxKeyCompare(u.bu.pC, &u.bu.r, &u.bu.res);
68297 if( pOp->opcode==OP_IdxLT ){
68298 u.bu.res = -u.bu.res;
68299 }else{
68300 assert( pOp->opcode==OP_IdxGE );
68301 u.bu.res++;
68302 }
68303 if( u.bu.res>0 ){
68304 pc = pOp->p2 - 1 ;
68305 }
68306 }
68307 break;
68308 }
@@ -68326,43 +68382,44 @@
68326 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
68327 **
68328 ** See also: Clear
68329 */
68330 case OP_Destroy: { /* out2-prerelease */
68331 #if 0 /* local variables moved into u.bv */
68332 int iMoved;
68333 int iCnt;
68334 Vdbe *pVdbe;
68335 int iDb;
68336 #endif /* local variables moved into u.bv */
 
68337 #ifndef SQLITE_OMIT_VIRTUALTABLE
68338 u.bv.iCnt = 0;
68339 for(u.bv.pVdbe=db->pVdbe; u.bv.pVdbe; u.bv.pVdbe = u.bv.pVdbe->pNext){
68340 if( u.bv.pVdbe->magic==VDBE_MAGIC_RUN && u.bv.pVdbe->inVtabMethod<2 && u.bv.pVdbe->pc>=0 ){
68341 u.bv.iCnt++;
68342 }
68343 }
68344 #else
68345 u.bv.iCnt = db->activeVdbeCnt;
68346 #endif
68347 pOut->flags = MEM_Null;
68348 if( u.bv.iCnt>1 ){
68349 rc = SQLITE_LOCKED;
68350 p->errorAction = OE_Abort;
68351 }else{
68352 u.bv.iDb = pOp->p3;
68353 assert( u.bv.iCnt==1 );
68354 assert( (p->btreeMask & (((yDbMask)1)<<u.bv.iDb))!=0 );
68355 rc = sqlite3BtreeDropTable(db->aDb[u.bv.iDb].pBt, pOp->p1, &u.bv.iMoved);
68356 pOut->flags = MEM_Int;
68357 pOut->u.i = u.bv.iMoved;
68358 #ifndef SQLITE_OMIT_AUTOVACUUM
68359 if( rc==SQLITE_OK && u.bv.iMoved!=0 ){
68360 sqlite3RootPageMoved(db, u.bv.iDb, u.bv.iMoved, pOp->p1);
68361 /* All OP_Destroy operations occur on the same btree */
68362 assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bv.iDb+1 );
68363 resetSchemaOnFault = u.bv.iDb+1;
68364 }
68365 #endif
68366 }
68367 break;
68368 }
@@ -68384,25 +68441,25 @@
68384 ** also incremented by the number of rows in the table being cleared.
68385 **
68386 ** See also: Destroy
68387 */
68388 case OP_Clear: {
68389 #if 0 /* local variables moved into u.bw */
68390 int nChange;
68391 #endif /* local variables moved into u.bw */
68392
68393 u.bw.nChange = 0;
68394 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
68395 rc = sqlite3BtreeClearTable(
68396 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bw.nChange : 0)
68397 );
68398 if( pOp->p3 ){
68399 p->nChange += u.bw.nChange;
68400 if( pOp->p3>0 ){
68401 assert( memIsValid(&aMem[pOp->p3]) );
68402 memAboutToChange(p, &aMem[pOp->p3]);
68403 aMem[pOp->p3].u.i += u.bw.nChange;
68404 }
68405 }
68406 break;
68407 }
68408
@@ -68428,29 +68485,29 @@
68428 **
68429 ** See documentation on OP_CreateTable for additional information.
68430 */
68431 case OP_CreateIndex: /* out2-prerelease */
68432 case OP_CreateTable: { /* out2-prerelease */
68433 #if 0 /* local variables moved into u.bx */
68434 int pgno;
68435 int flags;
68436 Db *pDb;
68437 #endif /* local variables moved into u.bx */
68438
68439 u.bx.pgno = 0;
68440 assert( pOp->p1>=0 && pOp->p1<db->nDb );
68441 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
68442 u.bx.pDb = &db->aDb[pOp->p1];
68443 assert( u.bx.pDb->pBt!=0 );
68444 if( pOp->opcode==OP_CreateTable ){
68445 /* u.bx.flags = BTREE_INTKEY; */
68446 u.bx.flags = BTREE_INTKEY;
68447 }else{
68448 u.bx.flags = BTREE_BLOBKEY;
68449 }
68450 rc = sqlite3BtreeCreateTable(u.bx.pDb->pBt, &u.bx.pgno, u.bx.flags);
68451 pOut->u.i = u.bx.pgno;
68452 break;
68453 }
68454
68455 /* Opcode: ParseSchema P1 * * P4 *
68456 **
@@ -68459,48 +68516,48 @@
68459 **
68460 ** This opcode invokes the parser to create a new virtual machine,
68461 ** then runs the new virtual machine. It is thus a re-entrant opcode.
68462 */
68463 case OP_ParseSchema: {
68464 #if 0 /* local variables moved into u.by */
68465 int iDb;
68466 const char *zMaster;
68467 char *zSql;
68468 InitData initData;
68469 #endif /* local variables moved into u.by */
68470
68471 /* Any prepared statement that invokes this opcode will hold mutexes
68472 ** on every btree. This is a prerequisite for invoking
68473 ** sqlite3InitCallback().
68474 */
68475 #ifdef SQLITE_DEBUG
68476 for(u.by.iDb=0; u.by.iDb<db->nDb; u.by.iDb++){
68477 assert( u.by.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.by.iDb].pBt) );
68478 }
68479 #endif
68480
68481 u.by.iDb = pOp->p1;
68482 assert( u.by.iDb>=0 && u.by.iDb<db->nDb );
68483 assert( DbHasProperty(db, u.by.iDb, DB_SchemaLoaded) );
68484 /* Used to be a conditional */ {
68485 u.by.zMaster = SCHEMA_TABLE(u.by.iDb);
68486 u.by.initData.db = db;
68487 u.by.initData.iDb = pOp->p1;
68488 u.by.initData.pzErrMsg = &p->zErrMsg;
68489 u.by.zSql = sqlite3MPrintf(db,
68490 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
68491 db->aDb[u.by.iDb].zName, u.by.zMaster, pOp->p4.z);
68492 if( u.by.zSql==0 ){
68493 rc = SQLITE_NOMEM;
68494 }else{
68495 assert( db->init.busy==0 );
68496 db->init.busy = 1;
68497 u.by.initData.rc = SQLITE_OK;
68498 assert( !db->mallocFailed );
68499 rc = sqlite3_exec(db, u.by.zSql, sqlite3InitCallback, &u.by.initData, 0);
68500 if( rc==SQLITE_OK ) rc = u.by.initData.rc;
68501 sqlite3DbFree(db, u.by.zSql);
68502 db->init.busy = 0;
68503 }
68504 }
68505 if( rc ) sqlite3ResetAllSchemasOfConnection(db);
68506 if( rc==SQLITE_NOMEM ){
@@ -68580,45 +68637,45 @@
68580 ** file, not the main database file.
68581 **
68582 ** This opcode is used to implement the integrity_check pragma.
68583 */
68584 case OP_IntegrityCk: {
68585 #if 0 /* local variables moved into u.bz */
68586 int nRoot; /* Number of tables to check. (Number of root pages.) */
68587 int *aRoot; /* Array of rootpage numbers for tables to be checked */
68588 int j; /* Loop counter */
68589 int nErr; /* Number of errors reported */
68590 char *z; /* Text of the error report */
68591 Mem *pnErr; /* Register keeping track of errors remaining */
68592 #endif /* local variables moved into u.bz */
68593
68594 u.bz.nRoot = pOp->p2;
68595 assert( u.bz.nRoot>0 );
68596 u.bz.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bz.nRoot+1) );
68597 if( u.bz.aRoot==0 ) goto no_mem;
68598 assert( pOp->p3>0 && pOp->p3<=p->nMem );
68599 u.bz.pnErr = &aMem[pOp->p3];
68600 assert( (u.bz.pnErr->flags & MEM_Int)!=0 );
68601 assert( (u.bz.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
68602 pIn1 = &aMem[pOp->p1];
68603 for(u.bz.j=0; u.bz.j<u.bz.nRoot; u.bz.j++){
68604 u.bz.aRoot[u.bz.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bz.j]);
68605 }
68606 u.bz.aRoot[u.bz.j] = 0;
68607 assert( pOp->p5<db->nDb );
68608 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
68609 u.bz.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bz.aRoot, u.bz.nRoot,
68610 (int)u.bz.pnErr->u.i, &u.bz.nErr);
68611 sqlite3DbFree(db, u.bz.aRoot);
68612 u.bz.pnErr->u.i -= u.bz.nErr;
68613 sqlite3VdbeMemSetNull(pIn1);
68614 if( u.bz.nErr==0 ){
68615 assert( u.bz.z==0 );
68616 }else if( u.bz.z==0 ){
68617 goto no_mem;
68618 }else{
68619 sqlite3VdbeMemSetStr(pIn1, u.bz.z, -1, SQLITE_UTF8, sqlite3_free);
68620 }
68621 UPDATE_MAX_BLOBSIZE(pIn1);
68622 sqlite3VdbeChangeEncoding(pIn1, encoding);
68623 break;
68624 }
@@ -68648,24 +68705,24 @@
68648 ** Extract the smallest value from boolean index P1 and put that value into
68649 ** register P3. Or, if boolean index P1 is initially empty, leave P3
68650 ** unchanged and jump to instruction P2.
68651 */
68652 case OP_RowSetRead: { /* jump, in1, out3 */
68653 #if 0 /* local variables moved into u.ca */
68654 i64 val;
68655 #endif /* local variables moved into u.ca */
68656 CHECK_FOR_INTERRUPT;
68657 pIn1 = &aMem[pOp->p1];
68658 if( (pIn1->flags & MEM_RowSet)==0
68659 || sqlite3RowSetNext(pIn1->u.pRowSet, &u.ca.val)==0
68660 ){
68661 /* The boolean index is empty */
68662 sqlite3VdbeMemSetNull(pIn1);
68663 pc = pOp->p2 - 1;
68664 }else{
68665 /* A value was pulled from the index */
68666 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.ca.val);
68667 }
68668 break;
68669 }
68670
68671 /* Opcode: RowSetTest P1 P2 P3 P4
@@ -68690,18 +68747,18 @@
68690 ** inserted, there is no need to search to see if the same value was
68691 ** previously inserted as part of set X (only if it was previously
68692 ** inserted as part of some other set).
68693 */
68694 case OP_RowSetTest: { /* jump, in1, in3 */
68695 #if 0 /* local variables moved into u.cb */
68696 int iSet;
68697 int exists;
68698 #endif /* local variables moved into u.cb */
68699
68700 pIn1 = &aMem[pOp->p1];
68701 pIn3 = &aMem[pOp->p3];
68702 u.cb.iSet = pOp->p4.i;
68703 assert( pIn3->flags&MEM_Int );
68704
68705 /* If there is anything other than a rowset object in memory cell P1,
68706 ** delete it now and initialize P1 with an empty rowset
68707 */
@@ -68709,21 +68766,21 @@
68709 sqlite3VdbeMemSetRowSet(pIn1);
68710 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
68711 }
68712
68713 assert( pOp->p4type==P4_INT32 );
68714 assert( u.cb.iSet==-1 || u.cb.iSet>=0 );
68715 if( u.cb.iSet ){
68716 u.cb.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
68717 (u8)(u.cb.iSet>=0 ? u.cb.iSet & 0xf : 0xff),
68718 pIn3->u.i);
68719 if( u.cb.exists ){
68720 pc = pOp->p2 - 1;
68721 break;
68722 }
68723 }
68724 if( u.cb.iSet>=0 ){
68725 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
68726 }
68727 break;
68728 }
68729
@@ -68742,24 +68799,24 @@
68742 ** memory required by the sub-vdbe at runtime.
68743 **
68744 ** P4 is a pointer to the VM containing the trigger program.
68745 */
68746 case OP_Program: { /* jump */
68747 #if 0 /* local variables moved into u.cc */
68748 int nMem; /* Number of memory registers for sub-program */
68749 int nByte; /* Bytes of runtime space required for sub-program */
68750 Mem *pRt; /* Register to allocate runtime space */
68751 Mem *pMem; /* Used to iterate through memory cells */
68752 Mem *pEnd; /* Last memory cell in new array */
68753 VdbeFrame *pFrame; /* New vdbe frame to execute in */
68754 SubProgram *pProgram; /* Sub-program to execute */
68755 void *t; /* Token identifying trigger */
68756 #endif /* local variables moved into u.cc */
68757
68758 u.cc.pProgram = pOp->p4.pProgram;
68759 u.cc.pRt = &aMem[pOp->p3];
68760 assert( u.cc.pProgram->nOp>0 );
68761
68762 /* If the p5 flag is clear, then recursive invocation of triggers is
68763 ** disabled for backwards compatibility (p5 is set if this sub-program
68764 ** is really a trigger, not a foreign key action, and the flag set
68765 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
@@ -68769,84 +68826,84 @@
68769 ** SubProgram (if the trigger may be executed with more than one different
68770 ** ON CONFLICT algorithm). SubProgram structures associated with a
68771 ** single trigger all have the same value for the SubProgram.token
68772 ** variable. */
68773 if( pOp->p5 ){
68774 u.cc.t = u.cc.pProgram->token;
68775 for(u.cc.pFrame=p->pFrame; u.cc.pFrame && u.cc.pFrame->token!=u.cc.t; u.cc.pFrame=u.cc.pFrame->pParent);
68776 if( u.cc.pFrame ) break;
68777 }
68778
68779 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
68780 rc = SQLITE_ERROR;
68781 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
68782 break;
68783 }
68784
68785 /* Register u.cc.pRt is used to store the memory required to save the state
68786 ** of the current program, and the memory required at runtime to execute
68787 ** the trigger program. If this trigger has been fired before, then u.cc.pRt
68788 ** is already allocated. Otherwise, it must be initialized. */
68789 if( (u.cc.pRt->flags&MEM_Frame)==0 ){
68790 /* SubProgram.nMem is set to the number of memory cells used by the
68791 ** program stored in SubProgram.aOp. As well as these, one memory
68792 ** cell is required for each cursor used by the program. Set local
68793 ** variable u.cc.nMem (and later, VdbeFrame.nChildMem) to this value.
68794 */
68795 u.cc.nMem = u.cc.pProgram->nMem + u.cc.pProgram->nCsr;
68796 u.cc.nByte = ROUND8(sizeof(VdbeFrame))
68797 + u.cc.nMem * sizeof(Mem)
68798 + u.cc.pProgram->nCsr * sizeof(VdbeCursor *)
68799 + u.cc.pProgram->nOnce * sizeof(u8);
68800 u.cc.pFrame = sqlite3DbMallocZero(db, u.cc.nByte);
68801 if( !u.cc.pFrame ){
68802 goto no_mem;
68803 }
68804 sqlite3VdbeMemRelease(u.cc.pRt);
68805 u.cc.pRt->flags = MEM_Frame;
68806 u.cc.pRt->u.pFrame = u.cc.pFrame;
68807
68808 u.cc.pFrame->v = p;
68809 u.cc.pFrame->nChildMem = u.cc.nMem;
68810 u.cc.pFrame->nChildCsr = u.cc.pProgram->nCsr;
68811 u.cc.pFrame->pc = pc;
68812 u.cc.pFrame->aMem = p->aMem;
68813 u.cc.pFrame->nMem = p->nMem;
68814 u.cc.pFrame->apCsr = p->apCsr;
68815 u.cc.pFrame->nCursor = p->nCursor;
68816 u.cc.pFrame->aOp = p->aOp;
68817 u.cc.pFrame->nOp = p->nOp;
68818 u.cc.pFrame->token = u.cc.pProgram->token;
68819 u.cc.pFrame->aOnceFlag = p->aOnceFlag;
68820 u.cc.pFrame->nOnceFlag = p->nOnceFlag;
68821
68822 u.cc.pEnd = &VdbeFrameMem(u.cc.pFrame)[u.cc.pFrame->nChildMem];
68823 for(u.cc.pMem=VdbeFrameMem(u.cc.pFrame); u.cc.pMem!=u.cc.pEnd; u.cc.pMem++){
68824 u.cc.pMem->flags = MEM_Invalid;
68825 u.cc.pMem->db = db;
68826 }
68827 }else{
68828 u.cc.pFrame = u.cc.pRt->u.pFrame;
68829 assert( u.cc.pProgram->nMem+u.cc.pProgram->nCsr==u.cc.pFrame->nChildMem );
68830 assert( u.cc.pProgram->nCsr==u.cc.pFrame->nChildCsr );
68831 assert( pc==u.cc.pFrame->pc );
68832 }
68833
68834 p->nFrame++;
68835 u.cc.pFrame->pParent = p->pFrame;
68836 u.cc.pFrame->lastRowid = lastRowid;
68837 u.cc.pFrame->nChange = p->nChange;
68838 p->nChange = 0;
68839 p->pFrame = u.cc.pFrame;
68840 p->aMem = aMem = &VdbeFrameMem(u.cc.pFrame)[-1];
68841 p->nMem = u.cc.pFrame->nChildMem;
68842 p->nCursor = (u16)u.cc.pFrame->nChildCsr;
68843 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
68844 p->aOp = aOp = u.cc.pProgram->aOp;
68845 p->nOp = u.cc.pProgram->nOp;
68846 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
68847 p->nOnceFlag = u.cc.pProgram->nOnce;
68848 pc = -1;
68849 memset(p->aOnceFlag, 0, p->nOnceFlag);
68850
68851 break;
68852 }
@@ -68862,17 +68919,17 @@
68862 ** The address of the cell in the parent frame is determined by adding
68863 ** the value of the P1 argument to the value of the P1 argument to the
68864 ** calling OP_Program instruction.
68865 */
68866 case OP_Param: { /* out2-prerelease */
68867 #if 0 /* local variables moved into u.cd */
68868 VdbeFrame *pFrame;
68869 Mem *pIn;
68870 #endif /* local variables moved into u.cd */
68871 u.cd.pFrame = p->pFrame;
68872 u.cd.pIn = &u.cd.pFrame->aMem[pOp->p1 + u.cd.pFrame->aOp[u.cd.pFrame->pc].p1];
68873 sqlite3VdbeMemShallowCopy(pOut, u.cd.pIn, MEM_Ephem);
68874 break;
68875 }
68876
68877 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
68878
@@ -68924,26 +68981,26 @@
68924 **
68925 ** This instruction throws an error if the memory cell is not initially
68926 ** an integer.
68927 */
68928 case OP_MemMax: { /* in2 */
68929 #if 0 /* local variables moved into u.ce */
68930 Mem *pIn1;
68931 VdbeFrame *pFrame;
68932 #endif /* local variables moved into u.ce */
68933 if( p->pFrame ){
68934 for(u.ce.pFrame=p->pFrame; u.ce.pFrame->pParent; u.ce.pFrame=u.ce.pFrame->pParent);
68935 u.ce.pIn1 = &u.ce.pFrame->aMem[pOp->p1];
68936 }else{
68937 u.ce.pIn1 = &aMem[pOp->p1];
68938 }
68939 assert( memIsValid(u.ce.pIn1) );
68940 sqlite3VdbeMemIntegerify(u.ce.pIn1);
68941 pIn2 = &aMem[pOp->p2];
68942 sqlite3VdbeMemIntegerify(pIn2);
68943 if( u.ce.pIn1->u.i<pIn2->u.i){
68944 u.ce.pIn1->u.i = pIn2->u.i;
68945 }
68946 break;
68947 }
68948 #endif /* SQLITE_OMIT_AUTOINCREMENT */
68949
@@ -69006,60 +69063,60 @@
69006 **
69007 ** The P5 arguments are taken from register P2 and its
69008 ** successors.
69009 */
69010 case OP_AggStep: {
69011 #if 0 /* local variables moved into u.cf */
69012 int n;
69013 int i;
69014 Mem *pMem;
69015 Mem *pRec;
69016 sqlite3_context ctx;
69017 sqlite3_value **apVal;
69018 #endif /* local variables moved into u.cf */
69019
69020 u.cf.n = pOp->p5;
69021 assert( u.cf.n>=0 );
69022 u.cf.pRec = &aMem[pOp->p2];
69023 u.cf.apVal = p->apArg;
69024 assert( u.cf.apVal || u.cf.n==0 );
69025 for(u.cf.i=0; u.cf.i<u.cf.n; u.cf.i++, u.cf.pRec++){
69026 assert( memIsValid(u.cf.pRec) );
69027 u.cf.apVal[u.cf.i] = u.cf.pRec;
69028 memAboutToChange(p, u.cf.pRec);
69029 sqlite3VdbeMemStoreType(u.cf.pRec);
69030 }
69031 u.cf.ctx.pFunc = pOp->p4.pFunc;
69032 assert( pOp->p3>0 && pOp->p3<=p->nMem );
69033 u.cf.ctx.pMem = u.cf.pMem = &aMem[pOp->p3];
69034 u.cf.pMem->n++;
69035 u.cf.ctx.s.flags = MEM_Null;
69036 u.cf.ctx.s.z = 0;
69037 u.cf.ctx.s.zMalloc = 0;
69038 u.cf.ctx.s.xDel = 0;
69039 u.cf.ctx.s.db = db;
69040 u.cf.ctx.isError = 0;
69041 u.cf.ctx.pColl = 0;
69042 u.cf.ctx.skipFlag = 0;
69043 if( u.cf.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
69044 assert( pOp>p->aOp );
69045 assert( pOp[-1].p4type==P4_COLLSEQ );
69046 assert( pOp[-1].opcode==OP_CollSeq );
69047 u.cf.ctx.pColl = pOp[-1].p4.pColl;
69048 }
69049 (u.cf.ctx.pFunc->xStep)(&u.cf.ctx, u.cf.n, u.cf.apVal); /* IMP: R-24505-23230 */
69050 if( u.cf.ctx.isError ){
69051 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cf.ctx.s));
69052 rc = u.cf.ctx.isError;
69053 }
69054 if( u.cf.ctx.skipFlag ){
69055 assert( pOp[-1].opcode==OP_CollSeq );
69056 u.cf.i = pOp[-1].p1;
69057 if( u.cf.i ) sqlite3VdbeMemSetInt64(&aMem[u.cf.i], 1);
69058 }
69059
69060 sqlite3VdbeMemRelease(&u.cf.ctx.s);
69061
69062 break;
69063 }
69064
69065 /* Opcode: AggFinal P1 P2 * P4 *
@@ -69073,23 +69130,23 @@
69073 ** functions that can take varying numbers of arguments. The
69074 ** P4 argument is only needed for the degenerate case where
69075 ** the step function was not previously called.
69076 */
69077 case OP_AggFinal: {
69078 #if 0 /* local variables moved into u.cg */
69079 Mem *pMem;
69080 #endif /* local variables moved into u.cg */
69081 assert( pOp->p1>0 && pOp->p1<=p->nMem );
69082 u.cg.pMem = &aMem[pOp->p1];
69083 assert( (u.cg.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
69084 rc = sqlite3VdbeMemFinalize(u.cg.pMem, pOp->p4.pFunc);
69085 if( rc ){
69086 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cg.pMem));
69087 }
69088 sqlite3VdbeChangeEncoding(u.cg.pMem, encoding);
69089 UPDATE_MAX_BLOBSIZE(u.cg.pMem);
69090 if( sqlite3VdbeMemTooBig(u.cg.pMem) ){
69091 goto too_big;
69092 }
69093 break;
69094 }
69095
@@ -69104,29 +69161,29 @@
69104 ** in the WAL that have been checkpointed after the checkpoint
69105 ** completes into mem[P3+2]. However on an error, mem[P3+1] and
69106 ** mem[P3+2] are initialized to -1.
69107 */
69108 case OP_Checkpoint: {
69109 #if 0 /* local variables moved into u.ch */
69110 int i; /* Loop counter */
69111 int aRes[3]; /* Results */
69112 Mem *pMem; /* Write results here */
69113 #endif /* local variables moved into u.ch */
69114
69115 u.ch.aRes[0] = 0;
69116 u.ch.aRes[1] = u.ch.aRes[2] = -1;
69117 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
69118 || pOp->p2==SQLITE_CHECKPOINT_FULL
69119 || pOp->p2==SQLITE_CHECKPOINT_RESTART
69120 );
69121 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.ch.aRes[1], &u.ch.aRes[2]);
69122 if( rc==SQLITE_BUSY ){
69123 rc = SQLITE_OK;
69124 u.ch.aRes[0] = 1;
69125 }
69126 for(u.ch.i=0, u.ch.pMem = &aMem[pOp->p3]; u.ch.i<3; u.ch.i++, u.ch.pMem++){
69127 sqlite3VdbeMemSetInt64(u.ch.pMem, (i64)u.ch.aRes[u.ch.i]);
69128 }
69129 break;
69130 };
69131 #endif
69132
@@ -69141,97 +69198,97 @@
69141 ** If changing into or out of WAL mode the procedure is more complicated.
69142 **
69143 ** Write a string containing the final journal-mode to register P2.
69144 */
69145 case OP_JournalMode: { /* out2-prerelease */
69146 #if 0 /* local variables moved into u.ci */
69147 Btree *pBt; /* Btree to change journal mode of */
69148 Pager *pPager; /* Pager associated with pBt */
69149 int eNew; /* New journal mode */
69150 int eOld; /* The old journal mode */
69151 #endif /* local variables moved into u.ci */
69152 #ifndef SQLITE_OMIT_WAL
69153 const char *zFilename; /* Name of database file for u.ci.pPager */
69154 #endif
69155
69156 u.ci.eNew = pOp->p3;
69157 assert( u.ci.eNew==PAGER_JOURNALMODE_DELETE
69158 || u.ci.eNew==PAGER_JOURNALMODE_TRUNCATE
69159 || u.ci.eNew==PAGER_JOURNALMODE_PERSIST
69160 || u.ci.eNew==PAGER_JOURNALMODE_OFF
69161 || u.ci.eNew==PAGER_JOURNALMODE_MEMORY
69162 || u.ci.eNew==PAGER_JOURNALMODE_WAL
69163 || u.ci.eNew==PAGER_JOURNALMODE_QUERY
 
69164 );
69165 assert( pOp->p1>=0 && pOp->p1<db->nDb );
69166
69167 u.ci.pBt = db->aDb[pOp->p1].pBt;
69168 u.ci.pPager = sqlite3BtreePager(u.ci.pBt);
69169 u.ci.eOld = sqlite3PagerGetJournalMode(u.ci.pPager);
69170 if( u.ci.eNew==PAGER_JOURNALMODE_QUERY ) u.ci.eNew = u.ci.eOld;
69171 if( !sqlite3PagerOkToChangeJournalMode(u.ci.pPager) ) u.ci.eNew = u.ci.eOld;
69172
69173 #ifndef SQLITE_OMIT_WAL
69174 zFilename = sqlite3PagerFilename(u.ci.pPager, 1);
69175
69176 /* Do not allow a transition to journal_mode=WAL for a database
69177 ** in temporary storage or if the VFS does not support shared memory
69178 */
69179 if( u.ci.eNew==PAGER_JOURNALMODE_WAL
69180 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
69181 || !sqlite3PagerWalSupported(u.ci.pPager)) /* No shared-memory support */
69182 ){
69183 u.ci.eNew = u.ci.eOld;
69184 }
69185
69186 if( (u.ci.eNew!=u.ci.eOld)
69187 && (u.ci.eOld==PAGER_JOURNALMODE_WAL || u.ci.eNew==PAGER_JOURNALMODE_WAL)
69188 ){
69189 if( !db->autoCommit || db->activeVdbeCnt>1 ){
69190 rc = SQLITE_ERROR;
69191 sqlite3SetString(&p->zErrMsg, db,
69192 "cannot change %s wal mode from within a transaction",
69193 (u.ci.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
69194 );
69195 break;
69196 }else{
69197
69198 if( u.ci.eOld==PAGER_JOURNALMODE_WAL ){
69199 /* If leaving WAL mode, close the log file. If successful, the call
69200 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
69201 ** file. An EXCLUSIVE lock may still be held on the database file
69202 ** after a successful return.
69203 */
69204 rc = sqlite3PagerCloseWal(u.ci.pPager);
69205 if( rc==SQLITE_OK ){
69206 sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
69207 }
69208 }else if( u.ci.eOld==PAGER_JOURNALMODE_MEMORY ){
69209 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
69210 ** as an intermediate */
69211 sqlite3PagerSetJournalMode(u.ci.pPager, PAGER_JOURNALMODE_OFF);
69212 }
69213
69214 /* Open a transaction on the database file. Regardless of the journal
69215 ** mode, this transaction always uses a rollback journal.
69216 */
69217 assert( sqlite3BtreeIsInTrans(u.ci.pBt)==0 );
69218 if( rc==SQLITE_OK ){
69219 rc = sqlite3BtreeSetVersion(u.ci.pBt, (u.ci.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
69220 }
69221 }
69222 }
69223 #endif /* ifndef SQLITE_OMIT_WAL */
69224
69225 if( rc ){
69226 u.ci.eNew = u.ci.eOld;
69227 }
69228 u.ci.eNew = sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
69229
69230 pOut = &aMem[pOp->p2];
69231 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
69232 pOut->z = (char *)sqlite3JournalModename(u.ci.eNew);
69233 pOut->n = sqlite3Strlen30(pOut->z);
69234 pOut->enc = SQLITE_UTF8;
69235 sqlite3VdbeChangeEncoding(pOut, encoding);
69236 break;
69237 };
@@ -69256,18 +69313,18 @@
69256 ** Perform a single step of the incremental vacuum procedure on
69257 ** the P1 database. If the vacuum has finished, jump to instruction
69258 ** P2. Otherwise, fall through to the next instruction.
69259 */
69260 case OP_IncrVacuum: { /* jump */
69261 #if 0 /* local variables moved into u.cj */
69262 Btree *pBt;
69263 #endif /* local variables moved into u.cj */
69264
69265 assert( pOp->p1>=0 && pOp->p1<db->nDb );
69266 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
69267 u.cj.pBt = db->aDb[pOp->p1].pBt;
69268 rc = sqlite3BtreeIncrVacuum(u.cj.pBt);
69269 if( rc==SQLITE_DONE ){
69270 pc = pOp->p2 - 1;
69271 rc = SQLITE_OK;
69272 }
69273 break;
@@ -69333,16 +69390,16 @@
69333 ** Also, whether or not P4 is set, check that this is not being called from
69334 ** within a callback to a virtual table xSync() method. If it is, the error
69335 ** code will be set to SQLITE_LOCKED.
69336 */
69337 case OP_VBegin: {
69338 #if 0 /* local variables moved into u.ck */
69339 VTable *pVTab;
69340 #endif /* local variables moved into u.ck */
69341 u.ck.pVTab = pOp->p4.pVtab;
69342 rc = sqlite3VtabBegin(db, u.ck.pVTab);
69343 if( u.ck.pVTab ) importVtabErrMsg(p, u.ck.pVTab->pVtab);
69344 break;
69345 }
69346 #endif /* SQLITE_OMIT_VIRTUALTABLE */
69347
69348 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -69377,36 +69434,36 @@
69377 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
69378 ** P1 is a cursor number. This opcode opens a cursor to the virtual
69379 ** table and stores that cursor in P1.
69380 */
69381 case OP_VOpen: {
69382 #if 0 /* local variables moved into u.cl */
69383 VdbeCursor *pCur;
69384 sqlite3_vtab_cursor *pVtabCursor;
69385 sqlite3_vtab *pVtab;
69386 sqlite3_module *pModule;
69387 #endif /* local variables moved into u.cl */
69388
69389 u.cl.pCur = 0;
69390 u.cl.pVtabCursor = 0;
69391 u.cl.pVtab = pOp->p4.pVtab->pVtab;
69392 u.cl.pModule = (sqlite3_module *)u.cl.pVtab->pModule;
69393 assert(u.cl.pVtab && u.cl.pModule);
69394 rc = u.cl.pModule->xOpen(u.cl.pVtab, &u.cl.pVtabCursor);
69395 importVtabErrMsg(p, u.cl.pVtab);
69396 if( SQLITE_OK==rc ){
69397 /* Initialize sqlite3_vtab_cursor base class */
69398 u.cl.pVtabCursor->pVtab = u.cl.pVtab;
69399
69400 /* Initialise vdbe cursor object */
69401 u.cl.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
69402 if( u.cl.pCur ){
69403 u.cl.pCur->pVtabCursor = u.cl.pVtabCursor;
69404 u.cl.pCur->pModule = u.cl.pVtabCursor->pVtab->pModule;
69405 }else{
69406 db->mallocFailed = 1;
69407 u.cl.pModule->xClose(u.cl.pVtabCursor);
69408 }
69409 }
69410 break;
69411 }
69412 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -69429,11 +69486,11 @@
69429 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
69430 **
69431 ** A jump is made to P2 if the result set after filtering would be empty.
69432 */
69433 case OP_VFilter: { /* jump */
69434 #if 0 /* local variables moved into u.cm */
69435 int nArg;
69436 int iQuery;
69437 const sqlite3_module *pModule;
69438 Mem *pQuery;
69439 Mem *pArgc;
@@ -69441,49 +69498,49 @@
69441 sqlite3_vtab *pVtab;
69442 VdbeCursor *pCur;
69443 int res;
69444 int i;
69445 Mem **apArg;
69446 #endif /* local variables moved into u.cm */
69447
69448 u.cm.pQuery = &aMem[pOp->p3];
69449 u.cm.pArgc = &u.cm.pQuery[1];
69450 u.cm.pCur = p->apCsr[pOp->p1];
69451 assert( memIsValid(u.cm.pQuery) );
69452 REGISTER_TRACE(pOp->p3, u.cm.pQuery);
69453 assert( u.cm.pCur->pVtabCursor );
69454 u.cm.pVtabCursor = u.cm.pCur->pVtabCursor;
69455 u.cm.pVtab = u.cm.pVtabCursor->pVtab;
69456 u.cm.pModule = u.cm.pVtab->pModule;
69457
69458 /* Grab the index number and argc parameters */
69459 assert( (u.cm.pQuery->flags&MEM_Int)!=0 && u.cm.pArgc->flags==MEM_Int );
69460 u.cm.nArg = (int)u.cm.pArgc->u.i;
69461 u.cm.iQuery = (int)u.cm.pQuery->u.i;
69462
69463 /* Invoke the xFilter method */
69464 {
69465 u.cm.res = 0;
69466 u.cm.apArg = p->apArg;
69467 for(u.cm.i = 0; u.cm.i<u.cm.nArg; u.cm.i++){
69468 u.cm.apArg[u.cm.i] = &u.cm.pArgc[u.cm.i+1];
69469 sqlite3VdbeMemStoreType(u.cm.apArg[u.cm.i]);
69470 }
69471
69472 p->inVtabMethod = 1;
69473 rc = u.cm.pModule->xFilter(u.cm.pVtabCursor, u.cm.iQuery, pOp->p4.z, u.cm.nArg, u.cm.apArg);
69474 p->inVtabMethod = 0;
69475 importVtabErrMsg(p, u.cm.pVtab);
69476 if( rc==SQLITE_OK ){
69477 u.cm.res = u.cm.pModule->xEof(u.cm.pVtabCursor);
69478 }
69479
69480 if( u.cm.res ){
69481 pc = pOp->p2 - 1;
69482 }
69483 }
69484 u.cm.pCur->nullRow = 0;
69485
69486 break;
69487 }
69488 #endif /* SQLITE_OMIT_VIRTUALTABLE */
69489
@@ -69493,55 +69550,55 @@
69493 ** Store the value of the P2-th column of
69494 ** the row of the virtual-table that the
69495 ** P1 cursor is pointing to into register P3.
69496 */
69497 case OP_VColumn: {
69498 #if 0 /* local variables moved into u.cn */
69499 sqlite3_vtab *pVtab;
69500 const sqlite3_module *pModule;
69501 Mem *pDest;
69502 sqlite3_context sContext;
69503 #endif /* local variables moved into u.cn */
69504
69505 VdbeCursor *pCur = p->apCsr[pOp->p1];
69506 assert( pCur->pVtabCursor );
69507 assert( pOp->p3>0 && pOp->p3<=p->nMem );
69508 u.cn.pDest = &aMem[pOp->p3];
69509 memAboutToChange(p, u.cn.pDest);
69510 if( pCur->nullRow ){
69511 sqlite3VdbeMemSetNull(u.cn.pDest);
69512 break;
69513 }
69514 u.cn.pVtab = pCur->pVtabCursor->pVtab;
69515 u.cn.pModule = u.cn.pVtab->pModule;
69516 assert( u.cn.pModule->xColumn );
69517 memset(&u.cn.sContext, 0, sizeof(u.cn.sContext));
69518
69519 /* The output cell may already have a buffer allocated. Move
69520 ** the current contents to u.cn.sContext.s so in case the user-function
69521 ** can use the already allocated buffer instead of allocating a
69522 ** new one.
69523 */
69524 sqlite3VdbeMemMove(&u.cn.sContext.s, u.cn.pDest);
69525 MemSetTypeFlag(&u.cn.sContext.s, MEM_Null);
69526
69527 rc = u.cn.pModule->xColumn(pCur->pVtabCursor, &u.cn.sContext, pOp->p2);
69528 importVtabErrMsg(p, u.cn.pVtab);
69529 if( u.cn.sContext.isError ){
69530 rc = u.cn.sContext.isError;
69531 }
69532
69533 /* Copy the result of the function to the P3 register. We
69534 ** do this regardless of whether or not an error occurred to ensure any
69535 ** dynamic allocation in u.cn.sContext.s (a Mem struct) is released.
69536 */
69537 sqlite3VdbeChangeEncoding(&u.cn.sContext.s, encoding);
69538 sqlite3VdbeMemMove(u.cn.pDest, &u.cn.sContext.s);
69539 REGISTER_TRACE(pOp->p3, u.cn.pDest);
69540 UPDATE_MAX_BLOBSIZE(u.cn.pDest);
69541
69542 if( sqlite3VdbeMemTooBig(u.cn.pDest) ){
69543 goto too_big;
69544 }
69545 break;
69546 }
69547 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -69552,42 +69609,42 @@
69552 ** Advance virtual table P1 to the next row in its result set and
69553 ** jump to instruction P2. Or, if the virtual table has reached
69554 ** the end of its result set, then fall through to the next instruction.
69555 */
69556 case OP_VNext: { /* jump */
69557 #if 0 /* local variables moved into u.co */
69558 sqlite3_vtab *pVtab;
69559 const sqlite3_module *pModule;
69560 int res;
69561 VdbeCursor *pCur;
69562 #endif /* local variables moved into u.co */
69563
69564 u.co.res = 0;
69565 u.co.pCur = p->apCsr[pOp->p1];
69566 assert( u.co.pCur->pVtabCursor );
69567 if( u.co.pCur->nullRow ){
69568 break;
69569 }
69570 u.co.pVtab = u.co.pCur->pVtabCursor->pVtab;
69571 u.co.pModule = u.co.pVtab->pModule;
69572 assert( u.co.pModule->xNext );
69573
69574 /* Invoke the xNext() method of the module. There is no way for the
69575 ** underlying implementation to return an error if one occurs during
69576 ** xNext(). Instead, if an error occurs, true is returned (indicating that
69577 ** data is available) and the error code returned when xColumn or
69578 ** some other method is next invoked on the save virtual table cursor.
69579 */
69580 p->inVtabMethod = 1;
69581 rc = u.co.pModule->xNext(u.co.pCur->pVtabCursor);
69582 p->inVtabMethod = 0;
69583 importVtabErrMsg(p, u.co.pVtab);
69584 if( rc==SQLITE_OK ){
69585 u.co.res = u.co.pModule->xEof(u.co.pCur->pVtabCursor);
69586 }
69587
69588 if( !u.co.res ){
69589 /* If there is data, jump to P2 */
69590 pc = pOp->p2 - 1;
69591 }
69592 break;
69593 }
@@ -69599,28 +69656,28 @@
69599 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
69600 ** This opcode invokes the corresponding xRename method. The value
69601 ** in register P1 is passed as the zName argument to the xRename method.
69602 */
69603 case OP_VRename: {
69604 #if 0 /* local variables moved into u.cp */
69605 sqlite3_vtab *pVtab;
69606 Mem *pName;
69607 #endif /* local variables moved into u.cp */
69608
69609 u.cp.pVtab = pOp->p4.pVtab->pVtab;
69610 u.cp.pName = &aMem[pOp->p1];
69611 assert( u.cp.pVtab->pModule->xRename );
69612 assert( memIsValid(u.cp.pName) );
69613 REGISTER_TRACE(pOp->p1, u.cp.pName);
69614 assert( u.cp.pName->flags & MEM_Str );
69615 testcase( u.cp.pName->enc==SQLITE_UTF8 );
69616 testcase( u.cp.pName->enc==SQLITE_UTF16BE );
69617 testcase( u.cp.pName->enc==SQLITE_UTF16LE );
69618 rc = sqlite3VdbeChangeEncoding(u.cp.pName, SQLITE_UTF8);
69619 if( rc==SQLITE_OK ){
69620 rc = u.cp.pVtab->pModule->xRename(u.cp.pVtab, u.cp.pName->z);
69621 importVtabErrMsg(p, u.cp.pVtab);
69622 p->expired = 0;
69623 }
69624 break;
69625 }
69626 #endif
@@ -69648,45 +69705,45 @@
69648 ** P1 is a boolean flag. If it is set to true and the xUpdate call
69649 ** is successful, then the value returned by sqlite3_last_insert_rowid()
69650 ** is set to the value of the rowid for the row just inserted.
69651 */
69652 case OP_VUpdate: {
69653 #if 0 /* local variables moved into u.cq */
69654 sqlite3_vtab *pVtab;
69655 sqlite3_module *pModule;
69656 int nArg;
69657 int i;
69658 sqlite_int64 rowid;
69659 Mem **apArg;
69660 Mem *pX;
69661 #endif /* local variables moved into u.cq */
69662
69663 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
69664 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
69665 );
69666 u.cq.pVtab = pOp->p4.pVtab->pVtab;
69667 u.cq.pModule = (sqlite3_module *)u.cq.pVtab->pModule;
69668 u.cq.nArg = pOp->p2;
69669 assert( pOp->p4type==P4_VTAB );
69670 if( ALWAYS(u.cq.pModule->xUpdate) ){
69671 u8 vtabOnConflict = db->vtabOnConflict;
69672 u.cq.apArg = p->apArg;
69673 u.cq.pX = &aMem[pOp->p3];
69674 for(u.cq.i=0; u.cq.i<u.cq.nArg; u.cq.i++){
69675 assert( memIsValid(u.cq.pX) );
69676 memAboutToChange(p, u.cq.pX);
69677 sqlite3VdbeMemStoreType(u.cq.pX);
69678 u.cq.apArg[u.cq.i] = u.cq.pX;
69679 u.cq.pX++;
69680 }
69681 db->vtabOnConflict = pOp->p5;
69682 rc = u.cq.pModule->xUpdate(u.cq.pVtab, u.cq.nArg, u.cq.apArg, &u.cq.rowid);
69683 db->vtabOnConflict = vtabOnConflict;
69684 importVtabErrMsg(p, u.cq.pVtab);
69685 if( rc==SQLITE_OK && pOp->p1 ){
69686 assert( u.cq.nArg>1 && u.cq.apArg[0] && (u.cq.apArg[0]->flags&MEM_Null) );
69687 db->lastRowid = lastRowid = u.cq.rowid;
69688 }
69689 if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
69690 if( pOp->p5==OE_Ignore ){
69691 rc = SQLITE_OK;
69692 }else{
@@ -69742,28 +69799,28 @@
69742 **
69743 ** If tracing is enabled (by the sqlite3_trace()) interface, then
69744 ** the UTF-8 string contained in P4 is emitted on the trace callback.
69745 */
69746 case OP_Trace: {
69747 #if 0 /* local variables moved into u.cr */
69748 char *zTrace;
69749 char *z;
69750 #endif /* local variables moved into u.cr */
69751
69752 if( db->xTrace
69753 && !p->doingRerun
69754 && (u.cr.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
69755 ){
69756 u.cr.z = sqlite3VdbeExpandSql(p, u.cr.zTrace);
69757 db->xTrace(db->pTraceArg, u.cr.z);
69758 sqlite3DbFree(db, u.cr.z);
69759 }
69760 #ifdef SQLITE_DEBUG
69761 if( (db->flags & SQLITE_SqlTrace)!=0
69762 && (u.cr.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
69763 ){
69764 sqlite3DebugPrintf("SQL-trace: %s\n", u.cr.zTrace);
69765 }
69766 #endif /* SQLITE_DEBUG */
69767 break;
69768 }
69769 #endif
@@ -74733,27 +74790,36 @@
74733 return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
74734 }
74735
74736 /*
74737 ** This function is used by the implementation of the IN (...) operator.
74738 ** It's job is to find or create a b-tree structure that may be used
74739 ** either to test for membership of the (...) set or to iterate through
74740 ** its members, skipping duplicates.
74741 **
74742 ** The index of the cursor opened on the b-tree (database table, database index
74743 ** or ephermal table) is stored in pX->iTable before this function returns.
 
 
 
 
 
74744 ** The returned value of this function indicates the b-tree type, as follows:
74745 **
74746 ** IN_INDEX_ROWID - The cursor was opened on a database table.
74747 ** IN_INDEX_INDEX - The cursor was opened on a database index.
74748 ** IN_INDEX_EPH - The cursor was opened on a specially created and
74749 ** populated epheremal table.
74750 **
74751 ** An existing b-tree may only be used if the SELECT is of the simple
74752 ** form:
74753 **
74754 ** SELECT <column> FROM <table>
 
 
 
 
 
74755 **
74756 ** If the prNotFound parameter is 0, then the b-tree will be used to iterate
74757 ** through the set members, skipping any duplicates. In this case an
74758 ** epheremal table must be used unless the selected <column> is guaranteed
74759 ** to be unique - either because it is an INTEGER PRIMARY KEY or it
@@ -74846,12 +74912,11 @@
74846
74847 /* Check that the affinity that will be used to perform the
74848 ** comparison is the same as the affinity of the column. If
74849 ** it is not, it is not possible to use any index.
74850 */
74851 char aff = comparisonAffinity(pX);
74852 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
74853
74854 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
74855 if( (pIdx->aiColumn[0]==iCol)
74856 && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
74857 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
@@ -75371,11 +75436,11 @@
75371
75372 /* The SQLITE_ColumnCache flag disables the column cache. This is used
75373 ** for testing only - to verify that SQLite always gets the same answer
75374 ** with and without the column cache.
75375 */
75376 if( pParse->db->flags & SQLITE_ColumnCache ) return;
75377
75378 /* First replace any existing entry.
75379 **
75380 ** Actually, the way the column cache is currently used, we are guaranteed
75381 ** that the object will never already be in cache. Verify this guarantee.
@@ -75568,32 +75633,20 @@
75568 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
75569 */
75570 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
75571 int i;
75572 struct yColCache *p;
75573 if( NEVER(iFrom==iTo) ) return;
75574 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
75575 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
75576 int x = p->iReg;
75577 if( x>=iFrom && x<iFrom+nReg ){
75578 p->iReg += iTo-iFrom;
75579 }
75580 }
75581 }
75582
75583 /*
75584 ** Generate code to copy content from registers iFrom...iFrom+nReg-1
75585 ** over to iTo..iTo+nReg-1.
75586 */
75587 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
75588 int i;
75589 if( NEVER(iFrom==iTo) ) return;
75590 for(i=0; i<nReg; i++){
75591 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
75592 }
75593 }
75594
75595 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
75596 /*
75597 ** Return true if any register in the range iFrom..iTo (inclusive)
75598 ** is used as part of the column cache.
75599 **
@@ -76699,11 +76752,11 @@
76699 ** precomputed into registers or if they are inserted in-line.
76700 */
76701 SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
76702 Walker w;
76703 if( pParse->cookieGoto ) return;
76704 if( (pParse->db->flags & SQLITE_FactorOutConst)!=0 ) return;
76705 w.xExprCallback = evalConstExpr;
76706 w.xSelectCallback = 0;
76707 w.pParse = pParse;
76708 sqlite3WalkExpr(&w, pExpr);
76709 }
@@ -85180,11 +85233,13 @@
85180 sqlite3ColumnDefault(v, pTab, idx, -1);
85181 }
85182 }
85183 if( doMakeRec ){
85184 const char *zAff;
85185 if( pTab->pSelect || (pParse->db->flags & SQLITE_IdxRealAsInt)!=0 ){
 
 
85186 zAff = 0;
85187 }else{
85188 zAff = sqlite3IndexAffinityStr(v, pIdx);
85189 }
85190 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut);
@@ -92422,11 +92477,11 @@
92422 sqlite3VdbeChangeP5(v, (u8)i);
92423 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
92424 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
92425 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
92426 P4_DYNAMIC);
92427 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
92428 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
92429 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
92430 sqlite3VdbeJumpHere(v, addr);
92431
92432 /* Make sure all the indices are constructed correctly.
@@ -92725,10 +92780,26 @@
92725 */
92726 if( sqlite3StrICmp(zLeft, "shrink_memory")==0 ){
92727 sqlite3_db_release_memory(db);
92728 }else
92729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92730 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
92731 /*
92732 ** Report the current state of file logs for all databases
92733 */
92734 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
@@ -92955,11 +93026,13 @@
92955 ** indicate success or failure.
92956 */
92957 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
92958 int rc;
92959 int i;
 
92960 int size;
 
92961 Table *pTab;
92962 Db *pDb;
92963 char const *azArg[4];
92964 int meta[5];
92965 InitData initData;
@@ -94210,10 +94283,23 @@
94210 return 0;
94211 }
94212 }
94213 #endif
94214
 
 
 
 
 
 
 
 
 
 
 
 
 
94215 /*
94216 ** This routine generates the code for the inside of the inner loop
94217 ** of a SELECT.
94218 **
94219 ** If srcTab and nColumn are both zero, then the pEList expressions
@@ -94226,11 +94312,11 @@
94226 Select *p, /* The complete select statement being coded */
94227 ExprList *pEList, /* List of values being extracted */
94228 int srcTab, /* Pull data from this table */
94229 int nColumn, /* Number of columns in the source table */
94230 ExprList *pOrderBy, /* If not NULL, sort results using this key */
94231 int distinct, /* If >=0, make sure results are distinct */
94232 SelectDest *pDest, /* How to dispose of the results */
94233 int iContinue, /* Jump here to continue with next row */
94234 int iBreak /* Jump here to break out of the inner loop */
94235 ){
94236 Vdbe *v = pParse->pVdbe;
@@ -94242,11 +94328,11 @@
94242 int nResultCol; /* Number of result columns */
94243
94244 assert( v );
94245 if( NEVER(v==0) ) return;
94246 assert( pEList!=0 );
94247 hasDistinct = distinct>=0;
94248 if( pOrderBy==0 && !hasDistinct ){
94249 codeOffset(v, p, iContinue);
94250 }
94251
94252 /* Pull the requested columns.
@@ -94282,11 +94368,59 @@
94282 ** part of the result.
94283 */
94284 if( hasDistinct ){
94285 assert( pEList!=0 );
94286 assert( pEList->nExpr==nColumn );
94287 codeDistinct(pParse, distinct, iContinue, nColumn, regResult);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94288 if( pOrderBy==0 ){
94289 codeOffset(v, p, iContinue);
94290 }
94291 }
94292
@@ -94340,20 +94474,21 @@
94340 ** then there should be a single item on the stack. Write this
94341 ** item into the set table with bogus data.
94342 */
94343 case SRT_Set: {
94344 assert( nColumn==1 );
94345 p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
 
94346 if( pOrderBy ){
94347 /* At first glance you would think we could optimize out the
94348 ** ORDER BY in this case since the order of entries in the set
94349 ** does not matter. But there might be a LIMIT clause, in which
94350 ** case the order does matter */
94351 pushOntoSorter(pParse, pOrderBy, p, regResult);
94352 }else{
94353 int r1 = sqlite3GetTempReg(pParse);
94354 sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, 1, r1, &p->affinity, 1);
94355 sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
94356 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
94357 sqlite3ReleaseTempReg(pParse, r1);
94358 }
94359 break;
@@ -94616,11 +94751,12 @@
94616 break;
94617 }
94618 #ifndef SQLITE_OMIT_SUBQUERY
94619 case SRT_Set: {
94620 assert( nColumn==1 );
94621 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, &p->affinity, 1);
 
94622 sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
94623 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
94624 break;
94625 }
94626 case SRT_Mem: {
@@ -95453,11 +95589,11 @@
95453 iCont = sqlite3VdbeMakeLabel(v);
95454 computeLimitRegisters(pParse, p, iBreak);
95455 sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
95456 iStart = sqlite3VdbeCurrentAddr(v);
95457 selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
95458 0, -1, &dest, iCont, iBreak);
95459 sqlite3VdbeResolveLabel(v, iCont);
95460 sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
95461 sqlite3VdbeResolveLabel(v, iBreak);
95462 sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
95463 }
@@ -95531,11 +95667,11 @@
95531 r1 = sqlite3GetTempReg(pParse);
95532 iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
95533 sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
95534 sqlite3ReleaseTempReg(pParse, r1);
95535 selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
95536 0, -1, &dest, iCont, iBreak);
95537 sqlite3VdbeResolveLabel(v, iCont);
95538 sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
95539 sqlite3VdbeResolveLabel(v, iBreak);
95540 sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
95541 sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
@@ -95651,11 +95787,11 @@
95651 j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
95652 j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
95653 (char*)pKeyInfo, p4type);
95654 sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
95655 sqlite3VdbeJumpHere(v, j1);
95656 sqlite3ExprCodeCopy(pParse, pIn->iSdst, regPrev+1, pIn->nSdst);
95657 sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
95658 }
95659 if( pParse->db->mallocFailed ) return 0;
95660
95661 /* Suppress the first OFFSET entries if there is an OFFSET clause
@@ -95686,14 +95822,14 @@
95686 ** item into the set table with bogus data.
95687 */
95688 case SRT_Set: {
95689 int r1;
95690 assert( pIn->nSdst==1 );
95691 p->affinity =
95692 sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
95693 r1 = sqlite3GetTempReg(pParse);
95694 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &p->affinity, 1);
95695 sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
95696 sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
95697 sqlite3ReleaseTempReg(pParse, r1);
95698 break;
95699 }
@@ -96431,11 +96567,11 @@
96431
96432 /* Check to see if flattening is permitted. Return 0 if not.
96433 */
96434 assert( p!=0 );
96435 assert( p->pPrior==0 ); /* Unable to flatten compound queries */
96436 if( db->flags & SQLITE_QueryFlattener ) return 0;
96437 pSrc = p->pSrc;
96438 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
96439 pSubitem = &pSrc->a[iFrom];
96440 iParent = pSubitem->iCursor;
96441 pSub = pSubitem->pSelect;
@@ -97471,15 +97607,13 @@
97471 SrcList *pTabList; /* List of tables to select from */
97472 Expr *pWhere; /* The WHERE clause. May be NULL */
97473 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
97474 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
97475 Expr *pHaving; /* The HAVING clause. May be NULL */
97476 int isDistinct; /* True if the DISTINCT keyword is present */
97477 int distinct; /* Table to use for the distinct set */
97478 int rc = 1; /* Value to return from this function */
97479 int addrSortIndex; /* Address of an OP_OpenEphemeral instruction */
97480 int addrDistinctIndex; /* Address of an OP_OpenEphemeral instruction */
97481 AggInfo sAggInfo; /* Information used by aggregate queries */
97482 int iEnd; /* Address of the end of the query */
97483 sqlite3 *db; /* The database connection */
97484
97485 #ifndef SQLITE_OMIT_EXPLAIN
@@ -97601,11 +97735,11 @@
97601 pEList = p->pEList;
97602 #endif
97603 pWhere = p->pWhere;
97604 pGroupBy = p->pGroupBy;
97605 pHaving = p->pHaving;
97606 isDistinct = (p->selFlags & SF_Distinct)!=0;
97607
97608 #ifndef SQLITE_OMIT_COMPOUND_SELECT
97609 /* If there is are a sequence of queries, do the earlier ones first.
97610 */
97611 if( p->pPrior ){
@@ -97636,11 +97770,11 @@
97636 ** an optimization - the correct answer should result regardless.
97637 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
97638 ** to disable this optimization for testing purposes.
97639 */
97640 if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
97641 && (db->flags & SQLITE_GroupByOrder)==0 ){
97642 pOrderBy = 0;
97643 }
97644
97645 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
97646 ** if the select-list is the same as the ORDER BY list, then this query
@@ -97662,10 +97796,14 @@
97662 ){
97663 p->selFlags &= ~SF_Distinct;
97664 p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
97665 pGroupBy = p->pGroupBy;
97666 pOrderBy = 0;
 
 
 
 
97667 }
97668
97669 /* If there is an ORDER BY clause, then this sorting
97670 ** index might end up being unused if the data can be
97671 ** extracted in pre-sorted order. If that is the case, then the
@@ -97702,28 +97840,31 @@
97702 }
97703
97704 /* Open a virtual index to use for the distinct set.
97705 */
97706 if( p->selFlags & SF_Distinct ){
97707 KeyInfo *pKeyInfo;
97708 distinct = pParse->nTab++;
97709 pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
97710 addrDistinctIndex = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0,
97711 (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
97712 sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
 
97713 }else{
97714 distinct = addrDistinctIndex = -1;
97715 }
97716
97717 /* Aggregate and non-aggregate queries are handled differently */
97718 if( !isAgg && pGroupBy==0 ){
97719 ExprList *pDist = (isDistinct ? p->pEList : 0);
 
97720
97721 /* Begin the database scan. */
97722 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, pDist, 0,0);
97723 if( pWInfo==0 ) goto select_end;
97724 if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
 
 
97725
97726 /* If sorting index that was created by a prior OP_OpenEphemeral
97727 ** instruction ended up not being needed, then change the OP_OpenEphemeral
97728 ** into an OP_Noop.
97729 */
@@ -97730,63 +97871,20 @@
97730 if( addrSortIndex>=0 && pOrderBy==0 ){
97731 sqlite3VdbeChangeToNoop(v, addrSortIndex);
97732 p->addrOpenEphm[2] = -1;
97733 }
97734
97735 if( pWInfo->eDistinct ){
97736 VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
97737
97738 assert( addrDistinctIndex>=0 );
97739 pOp = sqlite3VdbeGetOp(v, addrDistinctIndex);
97740
97741 assert( isDistinct );
97742 assert( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
97743 || pWInfo->eDistinct==WHERE_DISTINCT_UNIQUE
97744 );
97745 distinct = -1;
97746 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED ){
97747 int iJump;
97748 int iExpr;
97749 int iFlag = ++pParse->nMem;
97750 int iBase = pParse->nMem+1;
97751 int iBase2 = iBase + pEList->nExpr;
97752 pParse->nMem += (pEList->nExpr*2);
97753
97754 /* Change the OP_OpenEphemeral coded earlier to an OP_Integer. The
97755 ** OP_Integer initializes the "first row" flag. */
97756 pOp->opcode = OP_Integer;
97757 pOp->p1 = 1;
97758 pOp->p2 = iFlag;
97759
97760 sqlite3ExprCodeExprList(pParse, pEList, iBase, 1);
97761 iJump = sqlite3VdbeCurrentAddr(v) + 1 + pEList->nExpr + 1 + 1;
97762 sqlite3VdbeAddOp2(v, OP_If, iFlag, iJump-1);
97763 for(iExpr=0; iExpr<pEList->nExpr; iExpr++){
97764 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[iExpr].pExpr);
97765 sqlite3VdbeAddOp3(v, OP_Ne, iBase+iExpr, iJump, iBase2+iExpr);
97766 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
97767 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
97768 }
97769 sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iContinue);
97770
97771 sqlite3VdbeAddOp2(v, OP_Integer, 0, iFlag);
97772 assert( sqlite3VdbeCurrentAddr(v)==iJump );
97773 sqlite3VdbeAddOp3(v, OP_Move, iBase, iBase2, pEList->nExpr);
97774 }else{
97775 pOp->opcode = OP_Noop;
97776 }
97777 }
97778
97779 /* Use the standard inner loop. */
97780 selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, pDest,
97781 pWInfo->iContinue, pWInfo->iBreak);
97782
97783 /* End the database scan loop.
97784 */
97785 sqlite3WhereEnd(pWInfo);
97786 }else{
97787 /* This is the processing for aggregate queries */
 
97788 NameContext sNC; /* Name context for processing aggregate information */
97789 int iAMem; /* First Mem address for storing current GROUP BY */
97790 int iBMem; /* First Mem address for previous GROUP BY */
97791 int iUseFlag; /* Mem address holding flag indicating that at least
97792 ** one row of the input to the aggregator has been
@@ -97890,18 +97988,17 @@
97890 ** This might involve two separate loops with an OP_Sort in between, or
97891 ** it might be a single loop that uses an index to extract information
97892 ** in the right order to begin with.
97893 */
97894 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
97895 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0, 0, 0);
97896 if( pWInfo==0 ) goto select_end;
97897 if( pGroupBy==0 ){
97898 /* The optimizer is able to deliver rows in group by order so
97899 ** we do not have to sort. The OP_OpenEphemeral table will be
97900 ** cancelled later because we still need to use the pKeyInfo
97901 */
97902 pGroupBy = p->pGroupBy;
97903 groupBySort = 0;
97904 }else{
97905 /* Rows are coming out in undetermined order. We have to push
97906 ** each row into a sorting index, terminate the first loop,
97907 ** then loop over the sorting index in order to get the output
@@ -97911,11 +98008,12 @@
97911 int regRecord;
97912 int nCol;
97913 int nGroupBy;
97914
97915 explainTempTable(pParse,
97916 isDistinct && !(p->selFlags&SF_Distinct)?"DISTINCT":"GROUP BY");
 
97917
97918 groupBySort = 1;
97919 nGroupBy = pGroupBy->nExpr;
97920 nCol = nGroupBy + 1;
97921 j = nGroupBy+1;
@@ -98043,11 +98141,11 @@
98043 VdbeComment((v, "Groupby result generator entry point"));
98044 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
98045 finalizeAggFunctions(pParse, &sAggInfo);
98046 sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
98047 selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
98048 distinct, pDest,
98049 addrOutputRow+1, addrSetAbort);
98050 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
98051 VdbeComment((v, "end groupby result generator"));
98052
98053 /* Generate a subroutine that will reset the group-by accumulator
@@ -98146,10 +98244,11 @@
98146 */
98147 ExprList *pMinMax = 0;
98148 u8 flag = minMaxQuery(p);
98149 if( flag ){
98150 assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
 
98151 pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
98152 pDel = pMinMax;
98153 if( pMinMax && !db->mallocFailed ){
98154 pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
98155 pMinMax->a[0].pExpr->op = TK_COLUMN;
@@ -98159,17 +98258,18 @@
98159 /* This case runs if the aggregate has no GROUP BY clause. The
98160 ** processing is much simpler since there is only a single row
98161 ** of output.
98162 */
98163 resetAccumulator(pParse, &sAggInfo);
98164 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax,0,flag,0);
98165 if( pWInfo==0 ){
98166 sqlite3ExprListDelete(db, pDel);
98167 goto select_end;
98168 }
98169 updateAccumulator(pParse, &sAggInfo);
98170 if( !pMinMax && flag ){
 
98171 sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
98172 VdbeComment((v, "%s() by index",
98173 (flag==WHERE_ORDERBY_MIN?"min":"max")));
98174 }
98175 sqlite3WhereEnd(pWInfo);
@@ -98176,19 +98276,19 @@
98176 finalizeAggFunctions(pParse, &sAggInfo);
98177 }
98178
98179 pOrderBy = 0;
98180 sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
98181 selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1,
98182 pDest, addrEnd, addrEnd);
98183 sqlite3ExprListDelete(db, pDel);
98184 }
98185 sqlite3VdbeResolveLabel(v, addrEnd);
98186
98187 } /* endif aggregate query */
98188
98189 if( distinct>=0 ){
98190 explainTempTable(pParse, "DISTINCT");
98191 }
98192
98193 /* If there is an ORDER BY clause, then we need to sort the results
98194 ** and send them to the callback one by one.
@@ -101789,13 +101889,14 @@
101789
101790 /*
101791 ** Trace output macros
101792 */
101793 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
101794 SQLITE_PRIVATE int sqlite3WhereTrace = 0;
101795 #endif
101796 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 
101797 # define WHERETRACE(X) if(sqlite3WhereTrace) sqlite3DebugPrintf X
101798 #else
101799 # define WHERETRACE(X)
101800 #endif
101801
@@ -102031,10 +102132,32 @@
102031 #define WHERE_VIRTUALTABLE 0x08000000 /* Use virtual-table processing */
102032 #define WHERE_MULTI_OR 0x10000000 /* OR using multiple indices */
102033 #define WHERE_TEMP_INDEX 0x20000000 /* Uses an ephemeral index */
102034 #define WHERE_DISTINCT 0x40000000 /* Correct order for DISTINCT */
102035 #define WHERE_COVER_SCAN 0x80000000 /* Full scan of a covering index */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102036
102037 /*
102038 ** Initialize a preallocated WhereClause structure.
102039 */
102040 static void whereClauseInit(
@@ -103174,26 +103297,22 @@
103174 */
103175 pTerm->prereqRight |= extraRight;
103176 }
103177
103178 /*
103179 ** Return TRUE if any of the expressions in pList->a[iFirst...] contain
103180 ** a reference to any table other than the iBase table.
103181 */
103182 static int referencesOtherTables(
103183 ExprList *pList, /* Search expressions in ths list */
103184 WhereMaskSet *pMaskSet, /* Mapping from tables to bitmaps */
103185 int iFirst, /* Be searching with the iFirst-th expression */
103186 int iBase /* Ignore references to this table */
103187 ){
103188 Bitmask allowed = ~getMask(pMaskSet, iBase);
103189 while( iFirst<pList->nExpr ){
103190 if( (exprTableUsage(pMaskSet, pList->a[iFirst++].pExpr)&allowed)!=0 ){
103191 return 1;
103192 }
103193 }
103194 return 0;
103195 }
103196
103197 /*
103198 ** This function searches the expression list passed as the second argument
103199 ** for an expression of type TK_COLUMN that refers to the same column and
@@ -103355,47 +103474,63 @@
103355 return 0;
103356 }
103357
103358 /*
103359 ** This routine decides if pIdx can be used to satisfy the ORDER BY
103360 ** clause. If it can, it returns 1. If pIdx cannot satisfy the
103361 ** ORDER BY clause, this routine returns 0.
 
103362 **
103363 ** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
103364 ** left-most table in the FROM clause of that same SELECT statement and
103365 ** the table has a cursor number of "base". pIdx is an index on pTab.
103366 **
103367 ** nEqCol is the number of columns of pIdx that are used as equality
103368 ** constraints. Any of these columns may be missing from the ORDER BY
103369 ** clause and the match can still be a success.
 
 
 
 
103370 **
103371 ** All terms of the ORDER BY that match against the index must be either
103372 ** ASC or DESC. (Terms of the ORDER BY clause past the end of a UNIQUE
103373 ** index do not need to satisfy this constraint.) The *pbRev value is
103374 ** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
103375 ** the ORDER BY clause is all ASC.
103376 */
103377 static int isSortingIndex(
103378 Parse *pParse, /* Parsing context */
103379 WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmaps */
103380 Index *pIdx, /* The index we are testing */
103381 int base, /* Cursor number for the table to be sorted */
103382 ExprList *pOrderBy, /* The ORDER BY clause */
103383 int nEqCol, /* Number of index columns with == constraints */
103384 int wsFlags, /* Index usages flags */
103385 int *pbRev /* Set to 1 if ORDER BY is DESC */
103386 ){
103387 int i, j; /* Loop counters */
103388 int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
103389 int nTerm; /* Number of ORDER BY terms */
103390 struct ExprList_item *pTerm; /* A term of the ORDER BY clause */
103391 sqlite3 *db = pParse->db;
103392
103393 if( !pOrderBy ) return 0;
103394 if( wsFlags & WHERE_COLUMN_IN ) return 0;
103395 if( pIdx->bUnordered ) return 0;
103396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103397 nTerm = pOrderBy->nExpr;
103398 assert( nTerm>0 );
103399
103400 /* Argument pIdx must either point to a 'real' named index structure,
103401 ** or an index structure allocated on the stack by bestBtreeIndex() to
@@ -103408,11 +103543,11 @@
103408 ** Note that indices have pIdx->nColumn regular columns plus
103409 ** one additional column containing the rowid. The rowid column
103410 ** of the index is also allowed to match against the ORDER BY
103411 ** clause.
103412 */
103413 for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<=pIdx->nColumn; i++){
103414 Expr *pExpr; /* The expression of the ORDER BY pTerm */
103415 CollSeq *pColl; /* The collating sequence of pExpr */
103416 int termSortOrder; /* Sort order for this term */
103417 int iColumn; /* The i-th column of the index. -1 for rowid */
103418 int iSortOrder; /* 1 for DESC, 0 for ASC on the i-th index term */
@@ -103452,68 +103587,53 @@
103452 break;
103453 }else{
103454 /* If an index column fails to match and is not constrained by ==
103455 ** then the index cannot satisfy the ORDER BY constraint.
103456 */
103457 return 0;
103458 }
103459 }
103460 assert( pIdx->aSortOrder!=0 || iColumn==-1 );
103461 assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
103462 assert( iSortOrder==0 || iSortOrder==1 );
103463 termSortOrder = iSortOrder ^ pTerm->sortOrder;
103464 if( i>nEqCol ){
103465 if( termSortOrder!=sortOrder ){
103466 /* Indices can only be used if all ORDER BY terms past the
103467 ** equality constraints are all either DESC or ASC. */
103468 return 0;
103469 }
103470 }else{
103471 sortOrder = termSortOrder;
103472 }
103473 j++;
103474 pTerm++;
103475 if( iColumn<0 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
103476 /* If the indexed column is the primary key and everything matches
103477 ** so far and none of the ORDER BY terms to the right reference other
103478 ** tables in the join, then we are assured that the index can be used
103479 ** to sort because the primary key is unique and so none of the other
103480 ** columns will make any difference
103481 */
103482 j = nTerm;
103483 }
103484 }
103485
103486 *pbRev = sortOrder!=0;
103487 if( j>=nTerm ){
103488 /* All terms of the ORDER BY clause are covered by this index so
103489 ** this index can be used for sorting. */
103490 return 1;
103491 }
103492 if( pIdx->onError!=OE_None && i==pIdx->nColumn
103493 && (wsFlags & WHERE_COLUMN_NULL)==0
103494 && !referencesOtherTables(pOrderBy, pMaskSet, j, base)
103495 ){
103496 Column *aCol = pIdx->pTable->aCol;
103497
103498 /* All terms of this index match some prefix of the ORDER BY clause,
103499 ** the index is UNIQUE, and no terms on the tail of the ORDER BY
103500 ** refer to other tables in a join. So, assuming that the index entries
103501 ** visited contain no NULL values, then this index delivers rows in
103502 ** the required order.
103503 **
103504 ** It is not possible for any of the first nEqCol index fields to be
103505 ** NULL (since the corresponding "=" operator in the WHERE clause would
103506 ** not be true). So if all remaining index columns have NOT NULL
103507 ** constaints attached to them, we can be confident that the visited
103508 ** index entries are free of NULLs. */
103509 for(i=nEqCol; i<pIdx->nColumn; i++){
103510 if( aCol[pIdx->aiColumn[i]].notNull==0 ) break;
103511 }
103512 return (i==pIdx->nColumn);
103513 }
103514 return 0;
103515 }
103516
103517 /*
103518 ** Prepare a crude estimate of the logarithm of the input value.
103519 ** The results need not be exact. This is only used for estimating
@@ -103576,35 +103696,27 @@
103576 #endif
103577
103578 /*
103579 ** Required because bestIndex() is called by bestOrClauseIndex()
103580 */
103581 static void bestIndex(
103582 Parse*, WhereClause*, struct SrcList_item*,
103583 Bitmask, Bitmask, ExprList*, WhereCost*);
103584
103585 /*
103586 ** This routine attempts to find an scanning strategy that can be used
103587 ** to optimize an 'OR' expression that is part of a WHERE clause.
103588 **
103589 ** The table associated with FROM clause term pSrc may be either a
103590 ** regular B-Tree table or a virtual table.
103591 */
103592 static void bestOrClauseIndex(
103593 Parse *pParse, /* The parsing context */
103594 WhereClause *pWC, /* The WHERE clause */
103595 struct SrcList_item *pSrc, /* The FROM clause term to search */
103596 Bitmask notReady, /* Mask of cursors not available for indexing */
103597 Bitmask notValid, /* Cursors not available for any purpose */
103598 ExprList *pOrderBy, /* The ORDER BY clause */
103599 WhereCost *pCost /* Lowest cost query plan */
103600 ){
103601 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
103602 const int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
 
 
103603 const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur); /* Bitmask for pSrc */
103604 WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm]; /* End of pWC->a[] */
103605 WhereTerm *pTerm; /* A single term of the WHERE clause */
103606
103607 /* The OR-clause optimization is disallowed if the INDEXED BY or
103608 ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
103609 if( pSrc->notIndexed || pSrc->pIndex!=0 ){
103610 return;
@@ -103614,66 +103726,71 @@
103614 }
103615
103616 /* Search the WHERE clause terms for a usable WO_OR term. */
103617 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
103618 if( pTerm->eOperator==WO_OR
103619 && ((pTerm->prereqAll & ~maskSrc) & notReady)==0
103620 && (pTerm->u.pOrInfo->indexable & maskSrc)!=0
103621 ){
103622 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
103623 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
103624 WhereTerm *pOrTerm;
103625 int flags = WHERE_MULTI_OR;
103626 double rTotal = 0;
103627 double nRow = 0;
103628 Bitmask used = 0;
 
103629
 
 
 
 
103630 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
103631 WhereCost sTermCost;
103632 WHERETRACE(("... Multi-index OR testing for term %d of %d....\n",
103633 (pOrTerm - pOrWC->a), (pTerm - pWC->a)
103634 ));
103635 if( pOrTerm->eOperator==WO_AND ){
103636 WhereClause *pAndWC = &pOrTerm->u.pAndInfo->wc;
103637 bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
103638 }else if( pOrTerm->leftCursor==iCur ){
103639 WhereClause tempWC;
103640 tempWC.pParse = pWC->pParse;
103641 tempWC.pMaskSet = pWC->pMaskSet;
103642 tempWC.pOuter = pWC;
103643 tempWC.op = TK_AND;
103644 tempWC.a = pOrTerm;
103645 tempWC.wctrlFlags = 0;
103646 tempWC.nTerm = 1;
103647 bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
 
103648 }else{
103649 continue;
103650 }
103651 rTotal += sTermCost.rCost;
103652 nRow += sTermCost.plan.nRow;
103653 used |= sTermCost.used;
103654 if( rTotal>=pCost->rCost ) break;
103655 }
103656
103657 /* If there is an ORDER BY clause, increase the scan cost to account
103658 ** for the cost of the sort. */
103659 if( pOrderBy!=0 ){
103660 WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
103661 rTotal, rTotal+nRow*estLog(nRow)));
103662 rTotal += nRow*estLog(nRow);
103663 }
103664
103665 /* If the cost of scanning using this OR term for optimization is
103666 ** less than the current cost stored in pCost, replace the contents
103667 ** of pCost. */
103668 WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
103669 if( rTotal<pCost->rCost ){
103670 pCost->rCost = rTotal;
103671 pCost->used = used;
103672 pCost->plan.nRow = nRow;
103673 pCost->plan.wsFlags = flags;
103674 pCost->plan.u.pTerm = pTerm;
103675 }
103676 }
103677 }
103678 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
103679 }
@@ -103706,19 +103823,16 @@
103706 ** possible to construct a transient index that would perform better
103707 ** than a full table scan even when the cost of constructing the index
103708 ** is taken into account, then alter the query plan to use the
103709 ** transient index.
103710 */
103711 static void bestAutomaticIndex(
103712 Parse *pParse, /* The parsing context */
103713 WhereClause *pWC, /* The WHERE clause */
103714 struct SrcList_item *pSrc, /* The FROM clause term to search */
103715 Bitmask notReady, /* Mask of cursors that are not available */
103716 WhereCost *pCost /* Lowest cost query plan */
103717 ){
103718 double nTableRow; /* Rows in the input table */
103719 double logN; /* log(nTableRow) */
103720 double costTempIdx; /* per-query cost of the transient index */
103721 WhereTerm *pTerm; /* A single term of the WHERE clause */
103722 WhereTerm *pWCEnd; /* End of pWC->a[] */
103723 Table *pTable; /* Table tht might be indexed */
103724
@@ -103728,11 +103842,11 @@
103728 }
103729 if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){
103730 /* Automatic indices are disabled at run-time */
103731 return;
103732 }
103733 if( (pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){
103734 /* We already have some kind of index in use for this query. */
103735 return;
103736 }
103737 if( pSrc->notIndexed ){
103738 /* The NOT INDEXED clause appears in the SQL. */
@@ -103746,32 +103860,32 @@
103746 assert( pParse->nQueryLoop >= (double)1 );
103747 pTable = pSrc->pTab;
103748 nTableRow = pTable->nRowEst;
103749 logN = estLog(nTableRow);
103750 costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
103751 if( costTempIdx>=pCost->rCost ){
103752 /* The cost of creating the transient table would be greater than
103753 ** doing the full table scan */
103754 return;
103755 }
103756
103757 /* Search for any equality comparison term */
103758 pWCEnd = &pWC->a[pWC->nTerm];
103759 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
103760 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
103761 WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
103762 pCost->rCost, costTempIdx));
103763 pCost->rCost = costTempIdx;
103764 pCost->plan.nRow = logN + 1;
103765 pCost->plan.wsFlags = WHERE_TEMP_INDEX;
103766 pCost->used = pTerm->prereqRight;
103767 break;
103768 }
103769 }
103770 }
103771 #else
103772 # define bestAutomaticIndex(A,B,C,D,E) /* no-op */
103773 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
103774
103775
103776 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
103777 /*
@@ -103928,16 +104042,15 @@
103928 /*
103929 ** Allocate and populate an sqlite3_index_info structure. It is the
103930 ** responsibility of the caller to eventually release the structure
103931 ** by passing the pointer returned by this function to sqlite3_free().
103932 */
103933 static sqlite3_index_info *allocateIndexInfo(
103934 Parse *pParse,
103935 WhereClause *pWC,
103936 struct SrcList_item *pSrc,
103937 ExprList *pOrderBy
103938 ){
103939 int i, j;
103940 int nTerm;
103941 struct sqlite3_index_constraint *pIdxCons;
103942 struct sqlite3_index_orderby *pIdxOrderBy;
103943 struct sqlite3_index_constraint_usage *pUsage;
@@ -103963,16 +104076,17 @@
103963 ** virtual table then allocate space for the aOrderBy part of
103964 ** the sqlite3_index_info structure.
103965 */
103966 nOrderBy = 0;
103967 if( pOrderBy ){
103968 for(i=0; i<pOrderBy->nExpr; i++){
 
103969 Expr *pExpr = pOrderBy->a[i].pExpr;
103970 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
103971 }
103972 if( i==pOrderBy->nExpr ){
103973 nOrderBy = pOrderBy->nExpr;
103974 }
103975 }
103976
103977 /* Allocate the sqlite3_index_info structure
103978 */
@@ -104092,20 +104206,14 @@
104092 ** invocations. The sqlite3_index_info structure is also used when
104093 ** code is generated to access the virtual table. The whereInfoDelete()
104094 ** routine takes care of freeing the sqlite3_index_info structure after
104095 ** everybody has finished with it.
104096 */
104097 static void bestVirtualIndex(
104098 Parse *pParse, /* The parsing context */
104099 WhereClause *pWC, /* The WHERE clause */
104100 struct SrcList_item *pSrc, /* The FROM clause term to search */
104101 Bitmask notReady, /* Mask of cursors not available for index */
104102 Bitmask notValid, /* Cursors not valid for any purpose */
104103 ExprList *pOrderBy, /* The order by clause */
104104 WhereCost *pCost, /* Lowest cost query plan */
104105 sqlite3_index_info **ppIdxInfo /* Index information passed to xBestIndex */
104106 ){
104107 Table *pTab = pSrc->pTab;
104108 sqlite3_index_info *pIdxInfo;
104109 struct sqlite3_index_constraint *pIdxCons;
104110 struct sqlite3_index_constraint_usage *pUsage;
104111 WhereTerm *pTerm;
@@ -104115,19 +104223,19 @@
104115
104116 /* Make sure wsFlags is initialized to some sane value. Otherwise, if the
104117 ** malloc in allocateIndexInfo() fails and this function returns leaving
104118 ** wsFlags in an uninitialized state, the caller may behave unpredictably.
104119 */
104120 memset(pCost, 0, sizeof(*pCost));
104121 pCost->plan.wsFlags = WHERE_VIRTUALTABLE;
104122
104123 /* If the sqlite3_index_info structure has not been previously
104124 ** allocated and initialized, then allocate and initialize it now.
104125 */
104126 pIdxInfo = *ppIdxInfo;
104127 if( pIdxInfo==0 ){
104128 *ppIdxInfo = pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pOrderBy);
104129 }
104130 if( pIdxInfo==0 ){
104131 return;
104132 }
104133
@@ -104168,11 +104276,11 @@
104168 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
104169 pUsage = pIdxInfo->aConstraintUsage;
104170 for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
104171 j = pIdxCons->iTermOffset;
104172 pTerm = &pWC->a[j];
104173 pIdxCons->usable = (pTerm->prereqRight&notReady) ? 0 : 1;
104174 }
104175 memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
104176 if( pIdxInfo->needToFreeIdxStr ){
104177 sqlite3_free(pIdxInfo->idxStr);
104178 }
@@ -104181,11 +104289,11 @@
104181 pIdxInfo->needToFreeIdxStr = 0;
104182 pIdxInfo->orderByConsumed = 0;
104183 /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
104184 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
104185 nOrderBy = pIdxInfo->nOrderBy;
104186 if( !pOrderBy ){
104187 pIdxInfo->nOrderBy = 0;
104188 }
104189
104190 if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
104191 return;
@@ -104192,20 +104300,20 @@
104192 }
104193
104194 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
104195 for(i=0; i<pIdxInfo->nConstraint; i++){
104196 if( pUsage[i].argvIndex>0 ){
104197 pCost->used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
104198 }
104199 }
104200
104201 /* If there is an ORDER BY clause, and the selected virtual table index
104202 ** does not satisfy it, increase the cost of the scan accordingly. This
104203 ** matches the processing for non-virtual tables in bestBtreeIndex().
104204 */
104205 rCost = pIdxInfo->estimatedCost;
104206 if( pOrderBy && pIdxInfo->orderByConsumed==0 ){
104207 rCost += estLog(rCost)*rCost;
104208 }
104209
104210 /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
104211 ** inital value of lowestCost in this loop. If it is, then the
@@ -104213,25 +104321,25 @@
104213 **
104214 ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT
104215 ** is defined.
104216 */
104217 if( (SQLITE_BIG_DBL/((double)2))<rCost ){
104218 pCost->rCost = (SQLITE_BIG_DBL/((double)2));
104219 }else{
104220 pCost->rCost = rCost;
104221 }
104222 pCost->plan.u.pVtabIdx = pIdxInfo;
104223 if( pIdxInfo->orderByConsumed ){
104224 pCost->plan.wsFlags |= WHERE_ORDERBY;
104225 }
104226 pCost->plan.nEq = 0;
104227 pIdxInfo->nOrderBy = nOrderBy;
104228
104229 /* Try to find a more efficient access pattern by using multiple indexes
104230 ** to optimize an OR expression within the WHERE clause.
104231 */
104232 bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
104233 }
104234 #endif /* SQLITE_OMIT_VIRTUALTABLE */
104235
104236 #ifdef SQLITE_ENABLE_STAT3
104237 /*
@@ -104626,15 +104734,88 @@
104626 }
104627 return rc;
104628 }
104629 #endif /* defined(SQLITE_ENABLE_STAT3) */
104630
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104631
104632 /*
104633 ** Find the best query plan for accessing a particular table. Write the
104634 ** best query plan and its cost into the WhereCost object supplied as the
104635 ** last parameter.
104636 **
104637 ** The lowest cost plan wins. The cost is an estimate of the amount of
104638 ** CPU and disk I/O needed to process the requested result.
104639 ** Factors that influence cost include:
104640 **
@@ -104655,33 +104836,27 @@
104655 ** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table
104656 ** in the SELECT statement, then no indexes are considered. However, the
104657 ** selected plan may still take advantage of the built-in rowid primary key
104658 ** index.
104659 */
104660 static void bestBtreeIndex(
104661 Parse *pParse, /* The parsing context */
104662 WhereClause *pWC, /* The WHERE clause */
104663 struct SrcList_item *pSrc, /* The FROM clause term to search */
104664 Bitmask notReady, /* Mask of cursors not available for indexing */
104665 Bitmask notValid, /* Cursors not available for any purpose */
104666 ExprList *pOrderBy, /* The ORDER BY clause */
104667 ExprList *pDistinct, /* The select-list if query is DISTINCT */
104668 WhereCost *pCost /* Lowest cost query plan */
104669 ){
104670 int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
104671 Index *pProbe; /* An index we are evaluating */
104672 Index *pIdx; /* Copy of pProbe, or zero for IPK index */
104673 int eqTermMask; /* Current mask of valid equality operators */
104674 int idxEqTermMask; /* Index mask of valid equality operators */
104675 Index sPk; /* A fake index object for the primary key */
104676 tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
104677 int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
104678 int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */
104679
104680 /* Initialize the cost to a worst-case value */
104681 memset(pCost, 0, sizeof(*pCost));
104682 pCost->rCost = SQLITE_BIG_DBL;
104683
104684 /* If the pSrc table is the right table of a LEFT JOIN then we may not
104685 ** use an index to satisfy IS NULL constraints on that table. This is
104686 ** because columns might end up being NULL if the table does not match -
104687 ** a circumstance which the index cannot help us discover. Ticket #2177.
@@ -104730,11 +104905,11 @@
104730 for(; pProbe; pIdx=pProbe=pProbe->pNext){
104731 const tRowcnt * const aiRowEst = pProbe->aiRowEst;
104732 double cost; /* Cost of using pProbe */
104733 double nRow; /* Estimated number of rows in result set */
104734 double log10N = (double)1; /* base-10 logarithm of nRow (inexact) */
104735 int rev; /* True to scan in reverse order */
104736 int wsFlags = 0;
104737 Bitmask used = 0;
104738
104739 /* The following variables are populated based on the properties of
104740 ** index being evaluated. They are then used to determine the expected
@@ -104763,10 +104938,14 @@
104763 **
104764 ** If there exists a WHERE term of the form "x IN (SELECT ...)", then
104765 ** the sub-select is assumed to return 25 rows for the purposes of
104766 ** determining nInMul.
104767 **
 
 
 
 
104768 ** bInEst:
104769 ** Set to true if there was at least one "x IN (SELECT ...)" term used
104770 ** in determining the value of nInMul. Note that the RHS of the
104771 ** IN operator must be a SELECT, not a value list, for this variable
104772 ** to be true.
@@ -104781,10 +104960,14 @@
104781 ** bSort:
104782 ** Boolean. True if there is an ORDER BY clause that will require an
104783 ** external sort (i.e. scanning the index being evaluated will not
104784 ** correctly order records).
104785 **
 
 
 
 
104786 ** bLookup:
104787 ** Boolean. True if a table lookup is required for each index entry
104788 ** visited. In other words, true if this is not a covering index.
104789 ** This is always false for the rowid primary key index of a table.
104790 ** For other indexes, it is true unless all the columns of the table
@@ -104797,26 +104980,33 @@
104797 **
104798 ** SELECT a, b FROM tbl WHERE a = 1;
104799 ** SELECT a, b, c FROM tbl WHERE a = 1;
104800 */
104801 int nEq; /* Number of == or IN terms matching index */
 
104802 int bInEst = 0; /* True if "x IN (SELECT...)" seen */
104803 int nInMul = 1; /* Number of distinct equalities to lookup */
104804 double rangeDiv = (double)1; /* Estimated reduction in search space */
104805 int nBound = 0; /* Number of range constraints seen */
104806 int bSort = !!pOrderBy; /* True if external sort required */
104807 int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */
104808 int bLookup = 0; /* True if not a covering index */
 
 
104809 WhereTerm *pTerm; /* A single term of the WHERE clause */
104810 #ifdef SQLITE_ENABLE_STAT3
104811 WhereTerm *pFirstTerm = 0; /* First term matching the index */
104812 #endif
 
 
 
 
104813
104814 /* Determine the values of nEq and nInMul */
104815 for(nEq=0; nEq<pProbe->nColumn; nEq++){
104816 int j = pProbe->aiColumn[nEq];
104817 pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
104818 if( pTerm==0 ) break;
104819 wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
104820 testcase( pTerm->pWC!=pWC );
104821 if( pTerm->eOperator & WO_IN ){
104822 Expr *pExpr = pTerm->pExpr;
@@ -104829,10 +105019,13 @@
104829 /* "x IN (value, value, ...)" */
104830 nInMul *= pExpr->x.pList->nExpr;
104831 }
104832 }else if( pTerm->eOperator & WO_ISNULL ){
104833 wsFlags |= WHERE_COLUMN_NULL;
 
 
 
104834 }
104835 #ifdef SQLITE_ENABLE_STAT3
104836 if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
104837 #endif
104838 used |= pTerm->prereqRight;
@@ -104853,13 +105046,14 @@
104853 if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
104854 wsFlags |= WHERE_UNIQUE;
104855 }
104856 }else if( pProbe->bUnordered==0 ){
104857 int j = (nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[nEq]);
104858 if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
104859 WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
104860 WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
 
104861 whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
104862 if( pTop ){
104863 nBound = 1;
104864 wsFlags |= WHERE_TOP_LIMIT;
104865 used |= pTop->prereqRight;
@@ -104877,22 +105071,29 @@
104877
104878 /* If there is an ORDER BY clause and the index being considered will
104879 ** naturally scan rows in the required order, set the appropriate flags
104880 ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
104881 ** will scan rows in a different order, set the bSort variable. */
104882 if( isSortingIndex(
104883 pParse, pWC->pMaskSet, pProbe, iCur, pOrderBy, nEq, wsFlags, &rev)
104884 ){
104885 bSort = 0;
104886 wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
104887 wsFlags |= (rev ? WHERE_REVERSE : 0);
 
 
 
 
 
 
104888 }
104889
104890 /* If there is a DISTINCT qualifier and this index will scan rows in
104891 ** order of the DISTINCT expressions, clear bDist and set the appropriate
104892 ** flags in wsFlags. */
104893 if( isDistinctIndex(pParse, pWC, pProbe, iCur, pDistinct, nEq)
 
104894 && (wsFlags & WHERE_COLUMN_IN)==0
104895 ){
104896 bDist = 0;
104897 wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
104898 }
@@ -104965,16 +105166,14 @@
104965 ** The ANALYZE command and the sqlite_stat1 and sqlite_stat3 tables do
104966 ** not give us data on the relative sizes of table and index records.
104967 ** So this computation assumes table records are about twice as big
104968 ** as index records
104969 */
104970 if( wsFlags==WHERE_IDX_ONLY
104971 && (pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
104972 && sqlite3GlobalConfig.bUseCis
104973 #ifndef SQLITE_OMIT_BUILTIN_TEST
104974 && (pParse->db->flags & SQLITE_CoverIdxScan)==0
104975 #endif
104976 ){
104977 /* This index is not useful for indexing, but it is a covering index.
104978 ** A full-scan of the index might be a little faster than a full-scan
104979 ** of the table, so give this case a cost slightly less than a table
104980 ** scan. */
@@ -105024,11 +105223,11 @@
105024 ** adds C*N*log10(N) to the cost, where N is the number of rows to be
105025 ** sorted and C is a factor between 1.95 and 4.3. We will split the
105026 ** difference and select C of 3.0.
105027 */
105028 if( bSort ){
105029 cost += nRow*estLog(nRow)*3;
105030 }
105031 if( bDist ){
105032 cost += nRow*estLog(nRow)*3;
105033 }
105034
@@ -105048,20 +105247,20 @@
105048 ** tables that are not in outer loops. If notReady is used here instead
105049 ** of notValid, then a optimal index that depends on inner joins loops
105050 ** might be selected even when there exists an optimal index that has
105051 ** no such dependency.
105052 */
105053 if( nRow>2 && cost<=pCost->rCost ){
105054 int k; /* Loop counter */
105055 int nSkipEq = nEq; /* Number of == constraints to skip */
105056 int nSkipRange = nBound; /* Number of < constraints to skip */
105057 Bitmask thisTab; /* Bitmap for pSrc */
105058
105059 thisTab = getMask(pWC->pMaskSet, iCur);
105060 for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){
105061 if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
105062 if( (pTerm->prereqAll & notValid)!=thisTab ) continue;
105063 if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
105064 if( nSkipEq ){
105065 /* Ignore the first nEq equality matches since the index
105066 ** has already accounted for these */
105067 nSkipEq--;
@@ -105092,29 +105291,32 @@
105092 if( nRow<2 ) nRow = 2;
105093 }
105094
105095
105096 WHERETRACE((
105097 "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
105098 " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
 
 
105099 pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
105100 nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
105101 notReady, log10N, nRow, cost, used
105102 ));
105103
105104 /* If this index is the best we have seen so far, then record this
105105 ** index and its cost in the pCost structure.
105106 */
105107 if( (!pIdx || wsFlags)
105108 && (cost<pCost->rCost || (cost<=pCost->rCost && nRow<pCost->plan.nRow))
105109 ){
105110 pCost->rCost = cost;
105111 pCost->used = used;
105112 pCost->plan.nRow = nRow;
105113 pCost->plan.wsFlags = (wsFlags&wsFlagMask);
105114 pCost->plan.nEq = nEq;
105115 pCost->plan.u.pIdx = pIdx;
 
105116 }
105117
105118 /* If there was an INDEXED BY clause, then only that one index is
105119 ** considered. */
105120 if( pSrc->pIndex ) break;
@@ -105127,58 +105329,57 @@
105127 /* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
105128 ** is set, then reverse the order that the index will be scanned
105129 ** in. This is used for application testing, to help find cases
105130 ** where application behaviour depends on the (undefined) order that
105131 ** SQLite outputs rows in in the absence of an ORDER BY clause. */
105132 if( !pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
105133 pCost->plan.wsFlags |= WHERE_REVERSE;
105134 }
105135
105136 assert( pOrderBy || (pCost->plan.wsFlags&WHERE_ORDERBY)==0 );
105137 assert( pCost->plan.u.pIdx==0 || (pCost->plan.wsFlags&WHERE_ROWID_EQ)==0 );
105138 assert( pSrc->pIndex==0
105139 || pCost->plan.u.pIdx==0
105140 || pCost->plan.u.pIdx==pSrc->pIndex
105141 );
105142
105143 WHERETRACE(("best index is: %s\n",
105144 ((pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" :
105145 pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk")
105146 ));
105147
105148 bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
105149 bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost);
105150 pCost->plan.wsFlags |= eqTermMask;
105151 }
105152
105153 /*
105154 ** Find the query plan for accessing table pSrc->pTab. Write the
105155 ** best query plan and its cost into the WhereCost object supplied
105156 ** as the last parameter. This function may calculate the cost of
105157 ** both real and virtual table scans.
 
 
 
 
 
 
105158 */
105159 static void bestIndex(
105160 Parse *pParse, /* The parsing context */
105161 WhereClause *pWC, /* The WHERE clause */
105162 struct SrcList_item *pSrc, /* The FROM clause term to search */
105163 Bitmask notReady, /* Mask of cursors not available for indexing */
105164 Bitmask notValid, /* Cursors not available for any purpose */
105165 ExprList *pOrderBy, /* The ORDER BY clause */
105166 WhereCost *pCost /* Lowest cost query plan */
105167 ){
105168 #ifndef SQLITE_OMIT_VIRTUALTABLE
105169 if( IsVirtual(pSrc->pTab) ){
105170 sqlite3_index_info *p = 0;
105171 bestVirtualIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost,&p);
105172 if( p->needToFreeIdxStr ){
105173 sqlite3_free(p->idxStr);
 
105174 }
105175 sqlite3DbFree(pParse->db, p);
105176 }else
105177 #endif
105178 {
105179 bestBtreeIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, 0, pCost);
105180 }
105181 }
105182
105183 /*
105184 ** Disable a term in the WHERE clause. Except, do not disable the term
@@ -106432,46 +106633,50 @@
106432 ** fi
106433 ** end
106434 **
106435 ** ORDER BY CLAUSE PROCESSING
106436 **
106437 ** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
106438 ** if there is one. If there is no ORDER BY clause or if this routine
106439 ** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
106440 **
106441 ** If an index can be used so that the natural output order of the table
106442 ** scan is correct for the ORDER BY clause, then that index is used and
106443 ** *ppOrderBy is set to NULL. This is an optimization that prevents an
106444 ** unnecessary sort of the result set if an index appropriate for the
106445 ** ORDER BY clause already exists.
106446 **
106447 ** If the where clause loops cannot be arranged to provide the correct
106448 ** output order, then the *ppOrderBy is unchanged.
106449 */
106450 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
106451 Parse *pParse, /* The parser context */
106452 SrcList *pTabList, /* A list of all tables to be scanned */
106453 Expr *pWhere, /* The WHERE clause */
106454 ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */
106455 ExprList *pDistinct, /* The select-list for DISTINCT queries - or NULL */
106456 u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */
106457 int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */
106458 ){
106459 int i; /* Loop counter */
106460 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
106461 int nTabList; /* Number of elements in pTabList */
106462 WhereInfo *pWInfo; /* Will become the return value of this function */
106463 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
106464 Bitmask notReady; /* Cursors that are not yet positioned */
 
106465 WhereMaskSet *pMaskSet; /* The expression mask set */
106466 WhereClause *pWC; /* Decomposition of the WHERE clause */
106467 struct SrcList_item *pTabItem; /* A single entry from pTabList */
106468 WhereLevel *pLevel; /* A single level in the pWInfo list */
106469 int iFrom; /* First unused FROM clause element */
106470 int andFlags; /* AND-ed combination of all pWC->a[].wtFlags */
 
106471 sqlite3 *db; /* Database connection */
106472
 
 
 
 
 
106473 /* The number of tables in the FROM clause is limited by the number of
106474 ** bits in a Bitmask
106475 */
106476 testcase( pTabList->nSrc==BMS );
106477 if( pTabList->nSrc>BMS ){
@@ -106507,26 +106712,27 @@
106507 }
106508 pWInfo->nLevel = nTabList;
106509 pWInfo->pParse = pParse;
106510 pWInfo->pTabList = pTabList;
106511 pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
106512 pWInfo->pWC = pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
106513 pWInfo->wctrlFlags = wctrlFlags;
106514 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
106515 pMaskSet = (WhereMaskSet*)&pWC[1];
 
106516
106517 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
106518 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
106519 if( db->flags & SQLITE_DistinctOpt ) pDistinct = 0;
106520
106521 /* Split the WHERE clause into separate subexpressions where each
106522 ** subexpression is separated by an AND operator.
106523 */
106524 initMaskSet(pMaskSet);
106525 whereClauseInit(pWC, pParse, pMaskSet, wctrlFlags);
106526 sqlite3ExprCodeConstants(pParse, pWhere);
106527 whereSplit(pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */
106528
106529 /* Special case: a WHERE clause that is constant. Evaluate the
106530 ** expression and either jump over all of the code or fall thru.
106531 */
106532 if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
@@ -106553,24 +106759,24 @@
106553 ** Note that bitmasks are created for all pTabList->nSrc tables in
106554 ** pTabList, not just the first nTabList tables. nTabList is normally
106555 ** equal to pTabList->nSrc but might be shortened to 1 if the
106556 ** WHERE_ONETABLE_ONLY flag is set.
106557 */
106558 assert( pWC->vmask==0 && pMaskSet->n==0 );
106559 for(i=0; i<pTabList->nSrc; i++){
106560 createMask(pMaskSet, pTabList->a[i].iCursor);
106561 #ifndef SQLITE_OMIT_VIRTUALTABLE
106562 if( ALWAYS(pTabList->a[i].pTab) && IsVirtual(pTabList->a[i].pTab) ){
106563 pWC->vmask |= ((Bitmask)1 << i);
106564 }
106565 #endif
106566 }
106567 #ifndef NDEBUG
106568 {
106569 Bitmask toTheLeft = 0;
106570 for(i=0; i<pTabList->nSrc; i++){
106571 Bitmask m = getMask(pMaskSet, pTabList->a[i].iCursor);
106572 assert( (m-1)==toTheLeft );
106573 toTheLeft |= m;
106574 }
106575 }
106576 #endif
@@ -106578,20 +106784,20 @@
106578 /* Analyze all of the subexpressions. Note that exprAnalyze() might
106579 ** add new virtual terms onto the end of the WHERE clause. We do not
106580 ** want to analyze these virtual terms, so start analyzing at the end
106581 ** and work forward so that the added virtual terms are never processed.
106582 */
106583 exprAnalyzeAll(pTabList, pWC);
106584 if( db->mallocFailed ){
106585 goto whereBeginError;
106586 }
106587
106588 /* Check if the DISTINCT qualifier, if there is one, is redundant.
106589 ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to
106590 ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT.
106591 */
106592 if( pDistinct && isDistinctRedundant(pParse, pTabList, pWC, pDistinct) ){
106593 pDistinct = 0;
106594 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
106595 }
106596
106597 /* Chose the best index to use for each table in the FROM clause.
@@ -106607,14 +106813,17 @@
106607 ** pWInfo->a[].pTerm When wsFlags==WO_OR, the OR-clause term
106608 **
106609 ** This loop also figures out the nesting order of tables in the FROM
106610 ** clause.
106611 */
106612 notReady = ~(Bitmask)0;
 
 
 
106613 andFlags = ~0;
106614 WHERETRACE(("*** Optimizer Start ***\n"));
106615 for(i=iFrom=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
106616 WhereCost bestPlan; /* Most efficient plan seen so far */
106617 Index *pIdx; /* Index for FROM table at pTabItem */
106618 int j; /* For looping over FROM tables */
106619 int bestJ = -1; /* The value of j */
106620 Bitmask m; /* Bitmask value for j or bestJ */
@@ -106622,11 +106831,11 @@
106622 int nUnconstrained; /* Number tables without INDEXED BY */
106623 Bitmask notIndexed; /* Mask of tables that cannot use an index */
106624
106625 memset(&bestPlan, 0, sizeof(bestPlan));
106626 bestPlan.rCost = SQLITE_BIG_DBL;
106627 WHERETRACE(("*** Begin search for loop %d ***\n", i));
106628
106629 /* Loop through the remaining entries in the FROM clause to find the
106630 ** next nested loop. The loop tests all FROM clause entries
106631 ** either once or twice.
106632 **
@@ -106638,12 +106847,12 @@
106638 ** were used as the innermost nested loop. In other words, a table
106639 ** is chosen such that the cost of running that table cannot be reduced
106640 ** by waiting for other tables to run first. This "optimal" test works
106641 ** by first assuming that the FROM clause is on the inner loop and finding
106642 ** its query plan, then checking to see if that query plan uses any
106643 ** other FROM clause terms that are notReady. If no notReady terms are
106644 ** used then the "optimal" query plan works.
106645 **
106646 ** Note that the WhereCost.nRow parameter for an optimal scan might
106647 ** not be as small as it would be if the table really were the innermost
106648 ** join. The nRow value can be reduced by WHERE clause constraints
106649 ** that do not use indices. But this nRow reduction only happens if the
@@ -106670,59 +106879,52 @@
106670 ** costlier approach.
106671 */
106672 nUnconstrained = 0;
106673 notIndexed = 0;
106674 for(isOptimal=(iFrom<nTabList-1); isOptimal>=0 && bestJ<0; isOptimal--){
106675 Bitmask mask; /* Mask of tables not yet ready */
106676 for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){
106677 int doNotReorder; /* True if this table should not be reordered */
106678 WhereCost sCost; /* Cost information from best[Virtual]Index() */
106679 ExprList *pOrderBy; /* ORDER BY clause for index to optimize */
106680 ExprList *pDist; /* DISTINCT clause for index to optimize */
106681
106682 doNotReorder = (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0;
106683 if( j!=iFrom && doNotReorder ) break;
106684 m = getMask(pMaskSet, pTabItem->iCursor);
106685 if( (m & notReady)==0 ){
106686 if( j==iFrom ) iFrom++;
106687 continue;
106688 }
106689 mask = (isOptimal ? m : notReady);
106690 pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0);
106691 pDist = (i==0 ? pDistinct : 0);
106692 if( pTabItem->pIndex==0 ) nUnconstrained++;
106693
106694 WHERETRACE(("=== trying table %d with isOptimal=%d ===\n",
106695 j, isOptimal));
106696 assert( pTabItem->pTab );
106697 #ifndef SQLITE_OMIT_VIRTUALTABLE
106698 if( IsVirtual(pTabItem->pTab) ){
106699 sqlite3_index_info **pp = &pWInfo->a[j].pIdxInfo;
106700 bestVirtualIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
106701 &sCost, pp);
106702 }else
106703 #endif
106704 {
106705 bestBtreeIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
106706 pDist, &sCost);
106707 }
106708 assert( isOptimal || (sCost.used&notReady)==0 );
106709
106710 /* If an INDEXED BY clause is present, then the plan must use that
106711 ** index if it uses any index at all */
106712 assert( pTabItem->pIndex==0
106713 || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
106714 || sCost.plan.u.pIdx==pTabItem->pIndex );
106715
106716 if( isOptimal && (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
106717 notIndexed |= m;
106718 }
106719
106720 /* Conditions under which this table becomes the best so far:
106721 **
106722 ** (1) The table must not depend on other tables that have not
106723 ** yet run.
 
106724 **
106725 ** (2) A full-table-scan plan cannot supercede indexed plan unless
106726 ** the full-table-scan is an "optimal" plan as defined above.
106727 **
106728 ** (3) All tables have an INDEXED BY clause or this table lacks an
@@ -106735,37 +106937,38 @@
106735 ** An indexable full-table-scan from reaching rule (3).
106736 **
106737 ** (4) The plan cost must be lower than prior plans or else the
106738 ** cost must be the same and the number of rows must be lower.
106739 */
106740 if( (sCost.used&notReady)==0 /* (1) */
106741 && (bestJ<0 || (notIndexed&m)!=0 /* (2) */
106742 || (bestPlan.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
106743 || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
106744 && (nUnconstrained==0 || pTabItem->pIndex==0 /* (3) */
106745 || NEVER((sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
106746 && (bestJ<0 || sCost.rCost<bestPlan.rCost /* (4) */
106747 || (sCost.rCost<=bestPlan.rCost
106748 && sCost.plan.nRow<bestPlan.plan.nRow))
106749 ){
106750 WHERETRACE(("=== table %d is best so far"
106751 " with cost=%g and nRow=%g\n",
106752 j, sCost.rCost, sCost.plan.nRow));
106753 bestPlan = sCost;
 
106754 bestJ = j;
106755 }
106756 if( doNotReorder ) break;
106757 }
106758 }
106759 assert( bestJ>=0 );
106760 assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
106761 WHERETRACE(("*** Optimizer selects table %d for loop %d"
106762 " with cost=%g and nRow=%g\n",
106763 bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow));
106764 /* The ALWAYS() that follows was added to hush up clang scan-build */
106765 if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 && ALWAYS(ppOrderBy) ){
106766 *ppOrderBy = 0;
106767 }
106768 if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
106769 assert( pWInfo->eDistinct==0 );
106770 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
106771 }
@@ -106782,11 +106985,11 @@
106782 pLevel->iIdxCur = pParse->nTab++;
106783 }
106784 }else{
106785 pLevel->iIdxCur = -1;
106786 }
106787 notReady &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
106788 pLevel->iFrom = (u8)bestJ;
106789 if( bestPlan.plan.nRow>=(double)1 ){
106790 pParse->nQueryLoop *= bestPlan.plan.nRow;
106791 }
106792
@@ -106814,12 +107017,12 @@
106814 }
106815
106816 /* If the total query only selects a single row, then the ORDER BY
106817 ** clause is irrelevant.
106818 */
106819 if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
106820 *ppOrderBy = 0;
106821 }
106822
106823 /* If the caller is an UPDATE or DELETE statement that is requesting
106824 ** to use a one-pass algorithm, determine if this is appropriate.
106825 ** The one-pass algorithm only works if the WHERE clause constraints
@@ -106835,13 +107038,14 @@
106835 ** searching those tables.
106836 */
106837 sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
106838 notReady = ~(Bitmask)0;
106839 pWInfo->nRowOut = (double)1;
106840 for(i=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
106841 Table *pTab; /* Table to open */
106842 int iDb; /* Index of database containing table/index */
 
106843
106844 pTabItem = &pTabList->a[pLevel->iFrom];
106845 pTab = pTabItem->pTab;
106846 pLevel->iTabCur = pTabItem->iCursor;
106847 pWInfo->nRowOut *= pLevel->plan.nRow;
@@ -106873,11 +107077,11 @@
106873 }else{
106874 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
106875 }
106876 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
106877 if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){
106878 constructAutomaticIndex(pParse, pWC, pTabItem, notReady, pLevel);
106879 }else
106880 #endif
106881 if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
106882 Index *pIx = pLevel->plan.u.pIdx;
106883 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
@@ -106887,24 +107091,24 @@
106887 sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb,
106888 (char*)pKey, P4_KEYINFO_HANDOFF);
106889 VdbeComment((v, "%s", pIx->zName));
106890 }
106891 sqlite3CodeVerifySchema(pParse, iDb);
106892 notReady &= ~getMask(pWC->pMaskSet, pTabItem->iCursor);
106893 }
106894 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
106895 if( db->mallocFailed ) goto whereBeginError;
106896
106897 /* Generate the code to do the search. Each iteration of the for
106898 ** loop below generates code for a single nested loop of the VM
106899 ** program.
106900 */
106901 notReady = ~(Bitmask)0;
106902 for(i=0; i<nTabList; i++){
106903 pLevel = &pWInfo->a[i];
106904 explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags);
106905 notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady);
106906 pWInfo->iContinue = pLevel->addrCont;
106907 }
106908
106909 #ifdef SQLITE_TEST /* For testing and debugging use only */
106910 /* Record in the query plan information about the current table
@@ -106911,15 +107115,17 @@
106911 ** and the index used to access it (if any). If the table itself
106912 ** is not used, its name is just '{}'. If no index is used
106913 ** the index is listed as "{}". If the primary key is used the
106914 ** index name is '*'.
106915 */
106916 for(i=0; i<nTabList; i++){
106917 char *z;
106918 int n;
106919 int w;
106920 pLevel = &pWInfo->a[i];
 
 
106921 w = pLevel->plan.wsFlags;
106922 pTabItem = &pTabList->a[pLevel->iFrom];
106923 z = pTabItem->zAlias;
106924 if( z==0 ) z = pTabItem->pTab->zName;
106925 n = sqlite3Strlen30(z);
@@ -112874,10 +113080,11 @@
112874 ){
112875 sqlite3_mutex_enter(db->mutex);
112876 db->busyHandler.xFunc = xBusy;
112877 db->busyHandler.pArg = pArg;
112878 db->busyHandler.nBusy = 0;
 
112879 sqlite3_mutex_leave(db->mutex);
112880 return SQLITE_OK;
112881 }
112882
112883 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
@@ -112911,12 +113118,12 @@
112911 ** This routine installs a default busy handler that waits for the
112912 ** specified number of milliseconds before returning 0.
112913 */
112914 SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
112915 if( ms>0 ){
112916 db->busyTimeout = ms;
112917 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
 
112918 }else{
112919 sqlite3_busy_handler(db, 0, 0);
112920 }
112921 return SQLITE_OK;
112922 }
@@ -114771,12 +114978,11 @@
114771 ** with various optimizations disabled to verify that the same answer
114772 ** is obtained in every case.
114773 */
114774 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
114775 sqlite3 *db = va_arg(ap, sqlite3*);
114776 int x = va_arg(ap,int);
114777 db->flags = (x & SQLITE_OptMask) | (db->flags & ~SQLITE_OptMask);
114778 break;
114779 }
114780
114781 #ifdef SQLITE_N_KEYWORD
114782 /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
114783
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -673,11 +673,11 @@
673 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
674 ** [sqlite_version()] and [sqlite_source_id()].
675 */
676 #define SQLITE_VERSION "3.7.15"
677 #define SQLITE_VERSION_NUMBER 3007015
678 #define SQLITE_SOURCE_ID "2012-09-28 00:44:28 1e874629d7cf568368b912b295bd3001147d0b52"
679
680 /*
681 ** CAPI3REF: Run-Time Library Version Numbers
682 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
683 **
@@ -5315,10 +5315,13 @@
5315 ** successfully. An [error code] is returned otherwise.)^
5316 **
5317 ** ^Shared cache is disabled by default. But this might change in
5318 ** future releases of SQLite. Applications that care about shared
5319 ** cache setting should set it explicitly.
5320 **
5321 ** This interface is threadsafe on processors where writing a
5322 ** 32-bit integer is atomic.
5323 **
5324 ** See Also: [SQLite Shared-Cache Mode]
5325 */
5326 SQLITE_API int sqlite3_enable_shared_cache(int);
5327
@@ -8281,10 +8284,11 @@
8284 typedef struct NameContext NameContext;
8285 typedef struct Parse Parse;
8286 typedef struct RowSet RowSet;
8287 typedef struct Savepoint Savepoint;
8288 typedef struct Select Select;
8289 typedef struct SelectDest SelectDest;
8290 typedef struct SrcList SrcList;
8291 typedef struct StrAccum StrAccum;
8292 typedef struct Table Table;
8293 typedef struct TableLock TableLock;
8294 typedef struct Token Token;
@@ -8887,11 +8891,11 @@
8891 #define OPFLG_IN3 0x0010 /* in3: P3 is an input */
8892 #define OPFLG_OUT2 0x0020 /* out2: P2 is an output */
8893 #define OPFLG_OUT3 0x0040 /* out3: P3 is an output */
8894 #define OPFLG_INITIALIZER {\
8895 /* 0 */ 0x00, 0x01, 0x01, 0x04, 0x04, 0x10, 0x00, 0x02,\
8896 /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x24,\
8897 /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
8898 /* 24 */ 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00,\
8899 /* 32 */ 0x02, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,\
8900 /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11,\
8901 /* 48 */ 0x11, 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02,\
@@ -9843,10 +9847,11 @@
9847 int flags; /* Miscellaneous flags. See below */
9848 i64 lastRowid; /* ROWID of most recent insert (see above) */
9849 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
9850 int errCode; /* Most recent error code (SQLITE_*) */
9851 int errMask; /* & result codes with this before returning */
9852 u8 dbOptFlags; /* Flags to enable/disable optimizations */
9853 u8 autoCommit; /* The auto-commit flag. */
9854 u8 temp_store; /* 1: file 2: memory 0: default */
9855 u8 mallocFailed; /* True if we have seen a malloc failure */
9856 u8 dfltLockMode; /* Default locking-mode for attached dbs */
9857 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
@@ -9947,50 +9952,62 @@
9952 #define ENC(db) ((db)->aDb[0].pSchema->enc)
9953
9954 /*
9955 ** Possible values for the sqlite3.flags.
9956 */
9957 #define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */
9958 #define SQLITE_InternChanges 0x00000002 /* Uncommitted Hash table changes */
9959 #define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */
9960 #define SQLITE_ShortColNames 0x00000008 /* Show short columns names */
9961 #define SQLITE_CountRows 0x00000010 /* Count rows changed by INSERT, */
9962 /* DELETE, or UPDATE and return */
9963 /* the count using a callback. */
9964 #define SQLITE_NullCallback 0x00000020 /* Invoke the callback once if the */
9965 /* result set is empty */
9966 #define SQLITE_SqlTrace 0x00000040 /* Debug print SQL as it executes */
9967 #define SQLITE_VdbeListing 0x00000080 /* Debug listings of VDBE programs */
9968 #define SQLITE_WriteSchema 0x00000100 /* OK to update SQLITE_MASTER */
9969 /* 0x00000200 Unused */
9970 #define SQLITE_IgnoreChecks 0x00000400 /* Do not enforce check constraints */
9971 #define SQLITE_ReadUncommitted 0x0000800 /* For shared-cache mode */
9972 #define SQLITE_LegacyFileFmt 0x00001000 /* Create new databases in format 1 */
9973 #define SQLITE_FullFSync 0x00002000 /* Use full fsync on the backend */
9974 #define SQLITE_CkptFullFSync 0x00004000 /* Use full fsync for checkpoint */
9975 #define SQLITE_RecoveryMode 0x00008000 /* Ignore schema errors */
9976 #define SQLITE_ReverseOrder 0x00010000 /* Reverse unordered SELECTs */
9977 #define SQLITE_RecTriggers 0x00020000 /* Enable recursive triggers */
9978 #define SQLITE_ForeignKeys 0x00040000 /* Enforce foreign key constraints */
9979 #define SQLITE_AutoIndex 0x00080000 /* Enable automatic indexes */
9980 #define SQLITE_PreferBuiltin 0x00100000 /* Preference to built-in funcs */
9981 #define SQLITE_LoadExtension 0x00200000 /* Enable load_extension */
9982 #define SQLITE_EnableTrigger 0x00400000 /* True to enable triggers */
9983
9984 /*
9985 ** Bits of the sqlite3.dbOptFlags field that are used by the
9986 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
9987 ** selectively disable various optimizations.
9988 */
9989 #define SQLITE_QueryFlattener 0x0001 /* Query flattening */
9990 #define SQLITE_ColumnCache 0x0002 /* Column cache */
9991 #define SQLITE_GroupByOrder 0x0004 /* GROUPBY cover of ORDERBY */
9992 #define SQLITE_FactorOutConst 0x0008 /* Constant factoring */
9993 #define SQLITE_IdxRealAsInt 0x0010 /* Store REAL as INT in indices */
9994 #define SQLITE_DistinctOpt 0x0020 /* DISTINCT using indexes */
9995 #define SQLITE_CoverIdxScan 0x0040 /* Covering index scans */
9996 #define SQLITE_OrderByIdxJoin 0x0080 /* ORDER BY of joins via index */
9997 #define SQLITE_AllOpts 0x00ff /* All optimizations */
9998
9999 /*
10000 ** Macros for testing whether or not optimizations are enabled or disabled.
10001 */
10002 #ifndef SQLITE_OMIT_BUILTIN_TEST
10003 #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
10004 #define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
10005 #else
10006 #define OptimizationDisabled(db, mask) 0
10007 #define OptimizationEnabled(db, mask) 1
10008 #endif
10009
10010 /*
10011 ** Possible values for the sqlite.magic field.
10012 ** The numbers are obtained at random and have no special meaning, other
10013 ** than being distinct from one another.
@@ -10922,11 +10939,12 @@
10939 ** is only used when wsFlags&WHERE_VIRTUALTABLE is true. It is never the
10940 ** case that more than one of these conditions is true.
10941 */
10942 struct WherePlan {
10943 u32 wsFlags; /* WHERE_* flags that describe the strategy */
10944 u16 nEq; /* Number of == constraints */
10945 u16 nOBSat; /* Number of ORDER BY terms satisfied */
10946 double nRow; /* Estimated number of rows (for EQP) */
10947 union {
10948 Index *pIdx; /* Index when WHERE_INDEXED is true */
10949 struct WhereTerm *pTerm; /* WHERE clause term for OR-search */
10950 sqlite3_index_info *pVtabIdx; /* Virtual table index to use */
@@ -10998,28 +11016,32 @@
11016 ** half does the tail of the WHERE loop. An instance of
11017 ** this structure is returned by the first half and passed
11018 ** into the second half to give some continuity.
11019 */
11020 struct WhereInfo {
11021 Parse *pParse; /* Parsing and code generating context */
11022 SrcList *pTabList; /* List of tables in the join */
11023 u16 nOBSat; /* Number of ORDER BY terms satisfied by indices */
11024 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
11025 u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */
11026 u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
11027 u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */
11028 int iTop; /* The very beginning of the WHERE loop */
11029 int iContinue; /* Jump here to continue with next record */
11030 int iBreak; /* Jump here to break out of the loop */
11031 int nLevel; /* Number of nested loop */
11032 struct WhereClause *pWC; /* Decomposition of the WHERE clause */
11033 double savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
11034 double nRowOut; /* Estimated number of output rows */
11035 WhereLevel a[1]; /* Information about each nest loop in WHERE */
11036 };
11037
11038 /* Allowed values for WhereInfo.eDistinct and DistinctCtx.eTnctType */
11039 #define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
11040 #define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
11041 #define WHERE_DISTINCT_ORDERED 2 /* All duplicates are adjacent */
11042 #define WHERE_DISTINCT_UNORDERED 3 /* Duplicates are scattered */
11043
11044 /*
11045 ** A NameContext defines a context in which to resolve table and column
11046 ** names. The context consists of a list of tables (the pSrcList) field and
11047 ** a list of named expression (pEList). The named expression list may
@@ -11074,17 +11096,16 @@
11096 ** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
11097 ** the number of columns in P2 can be computed at the same time
11098 ** as the OP_OpenEphm instruction is coded because not
11099 ** enough information about the compound query is known at that point.
11100 ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
11101 ** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
11102 ** sequences for the ORDER BY clause.
11103 */
11104 struct Select {
11105 ExprList *pEList; /* The fields of the result */
11106 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
 
11107 u16 selFlags; /* Various SF_* values */
11108 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
11109 int addrOpenEphm[3]; /* OP_OpenEphem opcodes related to this select */
11110 double nSelectRow; /* Estimated number of result rows */
11111 SrcList *pSrc; /* The FROM clause */
@@ -11131,17 +11152,16 @@
11152 #define SRT_Table 8 /* Store result as data with an automatic rowid */
11153 #define SRT_EphemTab 9 /* Create transient tab and store like SRT_Table */
11154 #define SRT_Coroutine 10 /* Generate a single row of result */
11155
11156 /*
11157 ** An instance of this object describes where to put of the results of
11158 ** a SELECT statement.
11159 */
 
11160 struct SelectDest {
11161 u8 eDest; /* How to dispose of the results. On of SRT_* above. */
11162 char affSdst; /* Affinity used when eDest==SRT_Set */
11163 int iSDParm; /* A parameter used by the eDest disposal method */
11164 int iSdst; /* Base register where results are written */
11165 int nSdst; /* Number of registers allocated */
11166 };
11167
@@ -11826,17 +11846,15 @@
11846 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
11847 SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
11848 #endif
11849 SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
11850 SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
11851 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
 
11852 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
11853 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
11854 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
11855 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
 
11856 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
11857 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
11858 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
11859 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
11860 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
@@ -13178,11 +13196,13 @@
13196 #define MEM_Real 0x0008 /* Value is a real number */
13197 #define MEM_Blob 0x0010 /* Value is a BLOB */
13198 #define MEM_RowSet 0x0020 /* Value is a RowSet object */
13199 #define MEM_Frame 0x0040 /* Value is a VdbeFrame object */
13200 #define MEM_Invalid 0x0080 /* Value is undefined */
13201 #define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */
13202 #define MEM_TypeMask 0x01ff /* Mask of type bits */
13203
13204
13205 /* Whenever Mem contains a valid string or blob representation, one of
13206 ** the following flags must be set to determine the memory management
13207 ** policy for Mem.z. The MEM_Term flag tells us whether or not the
13208 ** string is \000 or \u0000 terminated
@@ -63693,10 +63713,11 @@
63713 struct OP_Yield_stack_vars {
63714 int pcDest;
63715 } aa;
63716 struct OP_Null_stack_vars {
63717 int cnt;
63718 u16 nullFlag;
63719 } ab;
63720 struct OP_Variable_stack_vars {
63721 Mem *pVar; /* Value being transferred */
63722 } ac;
63723 struct OP_Move_stack_vars {
@@ -63703,60 +63724,63 @@
63724 char *zMalloc; /* Holding variable for allocated memory */
63725 int n; /* Number of registers left to copy */
63726 int p1; /* Register to copy from */
63727 int p2; /* Register to copy to */
63728 } ad;
63729 struct OP_Copy_stack_vars {
63730 int n;
63731 } ae;
63732 struct OP_ResultRow_stack_vars {
63733 Mem *pMem;
63734 int i;
63735 } af;
63736 struct OP_Concat_stack_vars {
63737 i64 nByte;
63738 } ag;
63739 struct OP_Remainder_stack_vars {
63740 int flags; /* Combined MEM_* flags from both inputs */
63741 i64 iA; /* Integer value of left operand */
63742 i64 iB; /* Integer value of right operand */
63743 double rA; /* Real value of left operand */
63744 double rB; /* Real value of right operand */
63745 } ah;
63746 struct OP_Function_stack_vars {
63747 int i;
63748 Mem *pArg;
63749 sqlite3_context ctx;
63750 sqlite3_value **apVal;
63751 int n;
63752 } ai;
63753 struct OP_ShiftRight_stack_vars {
63754 i64 iA;
63755 u64 uA;
63756 i64 iB;
63757 u8 op;
63758 } aj;
63759 struct OP_Ge_stack_vars {
63760 int res; /* Result of the comparison of pIn1 against pIn3 */
63761 char affinity; /* Affinity to use for comparison */
63762 u16 flags1; /* Copy of initial value of pIn1->flags */
63763 u16 flags3; /* Copy of initial value of pIn3->flags */
63764 } ak;
63765 struct OP_Compare_stack_vars {
63766 int n;
63767 int i;
63768 int p1;
63769 int p2;
63770 const KeyInfo *pKeyInfo;
63771 int idx;
63772 CollSeq *pColl; /* Collating sequence to use on this term */
63773 int bRev; /* True for DESCENDING sort order */
63774 } al;
63775 struct OP_Or_stack_vars {
63776 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
63777 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
63778 } am;
63779 struct OP_IfNot_stack_vars {
63780 int c;
63781 } an;
63782 struct OP_Column_stack_vars {
63783 u32 payloadSize; /* Number of bytes in the record */
63784 i64 payloadSize64; /* Number of bytes in the record */
63785 int p1; /* P1 value of the opcode */
63786 int p2; /* column number to retrieve */
@@ -63777,15 +63801,15 @@
63801 u32 szField; /* Number of bytes in the content of a field */
63802 int szHdr; /* Size of the header size field at start of record */
63803 int avail; /* Number of bytes of available data */
63804 u32 t; /* A type code from the record header */
63805 Mem *pReg; /* PseudoTable input register */
63806 } ao;
63807 struct OP_Affinity_stack_vars {
63808 const char *zAffinity; /* The affinity to be applied */
63809 char cAff; /* A single character of affinity */
63810 } ap;
63811 struct OP_MakeRecord_stack_vars {
63812 u8 *zNewRecord; /* A buffer to hold the data for the new record */
63813 Mem *pRec; /* The new record */
63814 u64 nData; /* Number of bytes of data space */
63815 int nHdr; /* Number of bytes of header space */
@@ -63798,108 +63822,108 @@
63822 int nField; /* Number of fields in the record */
63823 char *zAffinity; /* The affinity string for the record */
63824 int file_format; /* File format to use for encoding */
63825 int i; /* Space used in zNewRecord[] */
63826 int len; /* Length of a field */
63827 } aq;
63828 struct OP_Count_stack_vars {
63829 i64 nEntry;
63830 BtCursor *pCrsr;
63831 } ar;
63832 struct OP_Savepoint_stack_vars {
63833 int p1; /* Value of P1 operand */
63834 char *zName; /* Name of savepoint */
63835 int nName;
63836 Savepoint *pNew;
63837 Savepoint *pSavepoint;
63838 Savepoint *pTmp;
63839 int iSavepoint;
63840 int ii;
63841 } as;
63842 struct OP_AutoCommit_stack_vars {
63843 int desiredAutoCommit;
63844 int iRollback;
63845 int turnOnAC;
63846 } at;
63847 struct OP_Transaction_stack_vars {
63848 Btree *pBt;
63849 } au;
63850 struct OP_ReadCookie_stack_vars {
63851 int iMeta;
63852 int iDb;
63853 int iCookie;
63854 } av;
63855 struct OP_SetCookie_stack_vars {
63856 Db *pDb;
63857 } aw;
63858 struct OP_VerifyCookie_stack_vars {
63859 int iMeta;
63860 int iGen;
63861 Btree *pBt;
63862 } ax;
63863 struct OP_OpenWrite_stack_vars {
63864 int nField;
63865 KeyInfo *pKeyInfo;
63866 int p2;
63867 int iDb;
63868 int wrFlag;
63869 Btree *pX;
63870 VdbeCursor *pCur;
63871 Db *pDb;
63872 } ay;
63873 struct OP_OpenEphemeral_stack_vars {
63874 VdbeCursor *pCx;
63875 } az;
63876 struct OP_SorterOpen_stack_vars {
63877 VdbeCursor *pCx;
63878 } ba;
63879 struct OP_OpenPseudo_stack_vars {
63880 VdbeCursor *pCx;
63881 } bb;
63882 struct OP_SeekGt_stack_vars {
63883 int res;
63884 int oc;
63885 VdbeCursor *pC;
63886 UnpackedRecord r;
63887 int nField;
63888 i64 iKey; /* The rowid we are to seek to */
63889 } bc;
63890 struct OP_Seek_stack_vars {
63891 VdbeCursor *pC;
63892 } bd;
63893 struct OP_Found_stack_vars {
63894 int alreadyExists;
63895 VdbeCursor *pC;
63896 int res;
63897 char *pFree;
63898 UnpackedRecord *pIdxKey;
63899 UnpackedRecord r;
63900 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
63901 } be;
63902 struct OP_IsUnique_stack_vars {
63903 u16 ii;
63904 VdbeCursor *pCx;
63905 BtCursor *pCrsr;
63906 u16 nField;
63907 Mem *aMx;
63908 UnpackedRecord r; /* B-Tree index search key */
63909 i64 R; /* Rowid stored in register P3 */
63910 } bf;
63911 struct OP_NotExists_stack_vars {
63912 VdbeCursor *pC;
63913 BtCursor *pCrsr;
63914 int res;
63915 u64 iKey;
63916 } bg;
63917 struct OP_NewRowid_stack_vars {
63918 i64 v; /* The new rowid */
63919 VdbeCursor *pC; /* Cursor of table to get the new rowid */
63920 int res; /* Result of an sqlite3BtreeLast() */
63921 int cnt; /* Counter to limit the number of searches */
63922 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
63923 VdbeFrame *pFrame; /* Root frame of VDBE */
63924 } bh;
63925 struct OP_InsertInt_stack_vars {
63926 Mem *pData; /* MEM cell holding data for the record to be inserted */
63927 Mem *pKey; /* MEM cell holding key for the record */
63928 i64 iKey; /* The integer ROWID or key for the record to be inserted */
63929 VdbeCursor *pC; /* Cursor to table into which insert is written */
@@ -63906,160 +63930,163 @@
63930 int nZero; /* Number of zero-bytes to append */
63931 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
63932 const char *zDb; /* database name - used by the update hook */
63933 const char *zTbl; /* Table name - used by the opdate hook */
63934 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
63935 } bi;
63936 struct OP_Delete_stack_vars {
63937 i64 iKey;
63938 VdbeCursor *pC;
63939 } bj;
63940 struct OP_SorterCompare_stack_vars {
63941 VdbeCursor *pC;
63942 int res;
63943 } bk;
63944 struct OP_SorterData_stack_vars {
63945 VdbeCursor *pC;
63946 } bl;
63947 struct OP_RowData_stack_vars {
63948 VdbeCursor *pC;
63949 BtCursor *pCrsr;
63950 u32 n;
63951 i64 n64;
63952 } bm;
63953 struct OP_Rowid_stack_vars {
63954 VdbeCursor *pC;
63955 i64 v;
63956 sqlite3_vtab *pVtab;
63957 const sqlite3_module *pModule;
63958 } bn;
63959 struct OP_NullRow_stack_vars {
63960 VdbeCursor *pC;
63961 } bo;
63962 struct OP_Last_stack_vars {
63963 VdbeCursor *pC;
63964 BtCursor *pCrsr;
63965 int res;
63966 } bp;
63967 struct OP_Rewind_stack_vars {
63968 VdbeCursor *pC;
63969 BtCursor *pCrsr;
63970 int res;
63971 } bq;
63972 struct OP_Next_stack_vars {
63973 VdbeCursor *pC;
63974 int res;
63975 } br;
63976 struct OP_IdxInsert_stack_vars {
63977 VdbeCursor *pC;
63978 BtCursor *pCrsr;
63979 int nKey;
63980 const char *zKey;
63981 } bs;
63982 struct OP_IdxDelete_stack_vars {
63983 VdbeCursor *pC;
63984 BtCursor *pCrsr;
63985 int res;
63986 UnpackedRecord r;
63987 } bt;
63988 struct OP_IdxRowid_stack_vars {
63989 BtCursor *pCrsr;
63990 VdbeCursor *pC;
63991 i64 rowid;
63992 } bu;
63993 struct OP_IdxGE_stack_vars {
63994 VdbeCursor *pC;
63995 int res;
63996 UnpackedRecord r;
63997 } bv;
63998 struct OP_Destroy_stack_vars {
63999 int iMoved;
64000 int iCnt;
64001 Vdbe *pVdbe;
64002 int iDb;
64003 } bw;
64004 struct OP_Clear_stack_vars {
64005 int nChange;
64006 } bx;
64007 struct OP_CreateTable_stack_vars {
64008 int pgno;
64009 int flags;
64010 Db *pDb;
64011 } by;
64012 struct OP_ParseSchema_stack_vars {
64013 int iDb;
64014 const char *zMaster;
64015 char *zSql;
64016 InitData initData;
64017 } bz;
64018 struct OP_IntegrityCk_stack_vars {
64019 int nRoot; /* Number of tables to check. (Number of root pages.) */
64020 int *aRoot; /* Array of rootpage numbers for tables to be checked */
64021 int j; /* Loop counter */
64022 int nErr; /* Number of errors reported */
64023 char *z; /* Text of the error report */
64024 Mem *pnErr; /* Register keeping track of errors remaining */
64025 } ca;
64026 struct OP_RowSetRead_stack_vars {
64027 i64 val;
64028 } cb;
64029 struct OP_RowSetTest_stack_vars {
64030 int iSet;
64031 int exists;
64032 } cc;
64033 struct OP_Program_stack_vars {
64034 int nMem; /* Number of memory registers for sub-program */
64035 int nByte; /* Bytes of runtime space required for sub-program */
64036 Mem *pRt; /* Register to allocate runtime space */
64037 Mem *pMem; /* Used to iterate through memory cells */
64038 Mem *pEnd; /* Last memory cell in new array */
64039 VdbeFrame *pFrame; /* New vdbe frame to execute in */
64040 SubProgram *pProgram; /* Sub-program to execute */
64041 void *t; /* Token identifying trigger */
64042 } cd;
64043 struct OP_Param_stack_vars {
64044 VdbeFrame *pFrame;
64045 Mem *pIn;
64046 } ce;
64047 struct OP_MemMax_stack_vars {
64048 Mem *pIn1;
64049 VdbeFrame *pFrame;
64050 } cf;
64051 struct OP_AggStep_stack_vars {
64052 int n;
64053 int i;
64054 Mem *pMem;
64055 Mem *pRec;
64056 sqlite3_context ctx;
64057 sqlite3_value **apVal;
64058 } cg;
64059 struct OP_AggFinal_stack_vars {
64060 Mem *pMem;
64061 } ch;
64062 struct OP_Checkpoint_stack_vars {
64063 int i; /* Loop counter */
64064 int aRes[3]; /* Results */
64065 Mem *pMem; /* Write results here */
64066 } ci;
64067 struct OP_JournalMode_stack_vars {
64068 Btree *pBt; /* Btree to change journal mode of */
64069 Pager *pPager; /* Pager associated with pBt */
64070 int eNew; /* New journal mode */
64071 int eOld; /* The old journal mode */
64072 #ifndef SQLITE_OMIT_WAL
64073 const char *zFilename; /* Name of database file for pPager */
64074 #endif
64075 } cj;
64076 struct OP_IncrVacuum_stack_vars {
64077 Btree *pBt;
64078 } ck;
64079 struct OP_VBegin_stack_vars {
64080 VTable *pVTab;
64081 } cl;
64082 struct OP_VOpen_stack_vars {
64083 VdbeCursor *pCur;
64084 sqlite3_vtab_cursor *pVtabCursor;
64085 sqlite3_vtab *pVtab;
64086 sqlite3_module *pModule;
64087 } cm;
64088 struct OP_VFilter_stack_vars {
64089 int nArg;
64090 int iQuery;
64091 const sqlite3_module *pModule;
64092 Mem *pQuery;
@@ -64068,40 +64095,40 @@
64095 sqlite3_vtab *pVtab;
64096 VdbeCursor *pCur;
64097 int res;
64098 int i;
64099 Mem **apArg;
64100 } cn;
64101 struct OP_VColumn_stack_vars {
64102 sqlite3_vtab *pVtab;
64103 const sqlite3_module *pModule;
64104 Mem *pDest;
64105 sqlite3_context sContext;
64106 } co;
64107 struct OP_VNext_stack_vars {
64108 sqlite3_vtab *pVtab;
64109 const sqlite3_module *pModule;
64110 int res;
64111 VdbeCursor *pCur;
64112 } cp;
64113 struct OP_VRename_stack_vars {
64114 sqlite3_vtab *pVtab;
64115 Mem *pName;
64116 } cq;
64117 struct OP_VUpdate_stack_vars {
64118 sqlite3_vtab *pVtab;
64119 sqlite3_module *pModule;
64120 int nArg;
64121 int i;
64122 sqlite_int64 rowid;
64123 Mem **apArg;
64124 Mem *pX;
64125 } cr;
64126 struct OP_Trace_stack_vars {
64127 char *zTrace;
64128 char *z;
64129 } cs;
64130 } u;
64131 /* End automatically generated code
64132 ********************************************************************/
64133
64134 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -64488,29 +64515,34 @@
64515 pOut->enc = encoding;
64516 UPDATE_MAX_BLOBSIZE(pOut);
64517 break;
64518 }
64519
64520 /* Opcode: Null P1 P2 P3 * *
64521 **
64522 ** Write a NULL into registers P2. If P3 greater than P2, then also write
64523 ** NULL into register P3 and every register in between P2 and P3. If P3
64524 ** is less than P2 (typically P3 is zero) then only register P2 is
64525 ** set to NULL.
64526 **
64527 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
64528 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on
64529 ** OP_Ne or OP_Eq.
64530 */
64531 case OP_Null: { /* out2-prerelease */
64532 #if 0 /* local variables moved into u.ab */
64533 int cnt;
64534 u16 nullFlag;
64535 #endif /* local variables moved into u.ab */
64536 u.ab.cnt = pOp->p3-pOp->p2;
64537 assert( pOp->p3<=p->nMem );
64538 pOut->flags = u.ab.nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
64539 while( u.ab.cnt>0 ){
64540 pOut++;
64541 memAboutToChange(p, pOut);
64542 VdbeMemRelease(pOut);
64543 pOut->flags = u.ab.nullFlag;
64544 u.ab.cnt--;
64545 }
64546 break;
64547 }
64548
@@ -64551,24 +64583,24 @@
64583 break;
64584 }
64585
64586 /* Opcode: Move P1 P2 P3 * *
64587 **
64588 ** Move the values in register P1..P1+P3 over into
64589 ** registers P2..P2+P3. Registers P1..P1+P3 are
64590 ** left holding a NULL. It is an error for register ranges
64591 ** P1..P1+P3 and P2..P2+P3 to overlap.
64592 */
64593 case OP_Move: {
64594 #if 0 /* local variables moved into u.ad */
64595 char *zMalloc; /* Holding variable for allocated memory */
64596 int n; /* Number of registers left to copy */
64597 int p1; /* Register to copy from */
64598 int p2; /* Register to copy to */
64599 #endif /* local variables moved into u.ad */
64600
64601 u.ad.n = pOp->p3 + 1;
64602 u.ad.p1 = pOp->p1;
64603 u.ad.p2 = pOp->p2;
64604 assert( u.ad.n>0 && u.ad.p1>0 && u.ad.p2>0 );
64605 assert( u.ad.p1+u.ad.n<=u.ad.p2 || u.ad.p2+u.ad.n<=u.ad.p1 );
64606
@@ -64593,24 +64625,34 @@
64625 pOut++;
64626 }
64627 break;
64628 }
64629
64630 /* Opcode: Copy P1 P2 P3 * *
64631 **
64632 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
64633 **
64634 ** This instruction makes a deep copy of the value. A duplicate
64635 ** is made of any string or blob constant. See also OP_SCopy.
64636 */
64637 case OP_Copy: {
64638 #if 0 /* local variables moved into u.ae */
64639 int n;
64640 #endif /* local variables moved into u.ae */
64641
64642 u.ae.n = pOp->p3;
64643 pIn1 = &aMem[pOp->p1];
64644 pOut = &aMem[pOp->p2];
64645 assert( pOut!=pIn1 );
64646 while( 1 ){
64647 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
64648 Deephemeralize(pOut);
64649 REGISTER_TRACE(pOp->p2+pOp->p3-u.ae.n, pOut);
64650 if( (u.ae.n--)==0 ) break;
64651 pOut++;
64652 pIn1++;
64653 }
64654 break;
64655 }
64656
64657 /* Opcode: SCopy P1 P2 * * *
64658 **
@@ -64643,14 +64685,14 @@
64685 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
64686 ** structure to provide access to the top P1 values as the result
64687 ** row.
64688 */
64689 case OP_ResultRow: {
64690 #if 0 /* local variables moved into u.af */
64691 Mem *pMem;
64692 int i;
64693 #endif /* local variables moved into u.af */
64694 assert( p->nResColumn==pOp->p2 );
64695 assert( pOp->p1>0 );
64696 assert( pOp->p1+pOp->p2<=p->nMem+1 );
64697
64698 /* If this statement has violated immediate foreign key constraints, do
@@ -64688,19 +64730,19 @@
64730
64731 /* Make sure the results of the current row are \000 terminated
64732 ** and have an assigned type. The results are de-ephemeralized as
64733 ** a side effect.
64734 */
64735 u.af.pMem = p->pResultSet = &aMem[pOp->p1];
64736 for(u.af.i=0; u.af.i<pOp->p2; u.af.i++){
64737 assert( memIsValid(&u.af.pMem[u.af.i]) );
64738 Deephemeralize(&u.af.pMem[u.af.i]);
64739 assert( (u.af.pMem[u.af.i].flags & MEM_Ephem)==0
64740 || (u.af.pMem[u.af.i].flags & (MEM_Str|MEM_Blob))==0 );
64741 sqlite3VdbeMemNulTerminate(&u.af.pMem[u.af.i]);
64742 sqlite3VdbeMemStoreType(&u.af.pMem[u.af.i]);
64743 REGISTER_TRACE(pOp->p1+u.af.i, &u.af.pMem[u.af.i]);
64744 }
64745 if( db->mallocFailed ) goto no_mem;
64746
64747 /* Return SQLITE_ROW
64748 */
@@ -64720,13 +64762,13 @@
64762 ** It is illegal for P1 and P3 to be the same register. Sometimes,
64763 ** if P3 is the same register as P2, the implementation is able
64764 ** to avoid a memcpy().
64765 */
64766 case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
64767 #if 0 /* local variables moved into u.ag */
64768 i64 nByte;
64769 #endif /* local variables moved into u.ag */
64770
64771 pIn1 = &aMem[pOp->p1];
64772 pIn2 = &aMem[pOp->p2];
64773 pOut = &aMem[pOp->p3];
64774 assert( pIn1!=pOut );
@@ -64735,26 +64777,26 @@
64777 break;
64778 }
64779 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
64780 Stringify(pIn1, encoding);
64781 Stringify(pIn2, encoding);
64782 u.ag.nByte = pIn1->n + pIn2->n;
64783 if( u.ag.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
64784 goto too_big;
64785 }
64786 MemSetTypeFlag(pOut, MEM_Str);
64787 if( sqlite3VdbeMemGrow(pOut, (int)u.ag.nByte+2, pOut==pIn2) ){
64788 goto no_mem;
64789 }
64790 if( pOut!=pIn2 ){
64791 memcpy(pOut->z, pIn2->z, pIn2->n);
64792 }
64793 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
64794 pOut->z[u.ag.nByte] = 0;
64795 pOut->z[u.ag.nByte+1] = 0;
64796 pOut->flags |= MEM_Term;
64797 pOut->n = (int)u.ag.nByte;
64798 pOut->enc = encoding;
64799 UPDATE_MAX_BLOBSIZE(pOut);
64800 break;
64801 }
64802
@@ -64794,80 +64836,80 @@
64836 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
64837 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
64838 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
64839 case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
64840 case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
64841 #if 0 /* local variables moved into u.ah */
64842 int flags; /* Combined MEM_* flags from both inputs */
64843 i64 iA; /* Integer value of left operand */
64844 i64 iB; /* Integer value of right operand */
64845 double rA; /* Real value of left operand */
64846 double rB; /* Real value of right operand */
64847 #endif /* local variables moved into u.ah */
64848
64849 pIn1 = &aMem[pOp->p1];
64850 applyNumericAffinity(pIn1);
64851 pIn2 = &aMem[pOp->p2];
64852 applyNumericAffinity(pIn2);
64853 pOut = &aMem[pOp->p3];
64854 u.ah.flags = pIn1->flags | pIn2->flags;
64855 if( (u.ah.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
64856 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
64857 u.ah.iA = pIn1->u.i;
64858 u.ah.iB = pIn2->u.i;
64859 switch( pOp->opcode ){
64860 case OP_Add: if( sqlite3AddInt64(&u.ah.iB,u.ah.iA) ) goto fp_math; break;
64861 case OP_Subtract: if( sqlite3SubInt64(&u.ah.iB,u.ah.iA) ) goto fp_math; break;
64862 case OP_Multiply: if( sqlite3MulInt64(&u.ah.iB,u.ah.iA) ) goto fp_math; break;
64863 case OP_Divide: {
64864 if( u.ah.iA==0 ) goto arithmetic_result_is_null;
64865 if( u.ah.iA==-1 && u.ah.iB==SMALLEST_INT64 ) goto fp_math;
64866 u.ah.iB /= u.ah.iA;
64867 break;
64868 }
64869 default: {
64870 if( u.ah.iA==0 ) goto arithmetic_result_is_null;
64871 if( u.ah.iA==-1 ) u.ah.iA = 1;
64872 u.ah.iB %= u.ah.iA;
64873 break;
64874 }
64875 }
64876 pOut->u.i = u.ah.iB;
64877 MemSetTypeFlag(pOut, MEM_Int);
64878 }else{
64879 fp_math:
64880 u.ah.rA = sqlite3VdbeRealValue(pIn1);
64881 u.ah.rB = sqlite3VdbeRealValue(pIn2);
64882 switch( pOp->opcode ){
64883 case OP_Add: u.ah.rB += u.ah.rA; break;
64884 case OP_Subtract: u.ah.rB -= u.ah.rA; break;
64885 case OP_Multiply: u.ah.rB *= u.ah.rA; break;
64886 case OP_Divide: {
64887 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
64888 if( u.ah.rA==(double)0 ) goto arithmetic_result_is_null;
64889 u.ah.rB /= u.ah.rA;
64890 break;
64891 }
64892 default: {
64893 u.ah.iA = (i64)u.ah.rA;
64894 u.ah.iB = (i64)u.ah.rB;
64895 if( u.ah.iA==0 ) goto arithmetic_result_is_null;
64896 if( u.ah.iA==-1 ) u.ah.iA = 1;
64897 u.ah.rB = (double)(u.ah.iB % u.ah.iA);
64898 break;
64899 }
64900 }
64901 #ifdef SQLITE_OMIT_FLOATING_POINT
64902 pOut->u.i = u.ah.rB;
64903 MemSetTypeFlag(pOut, MEM_Int);
64904 #else
64905 if( sqlite3IsNaN(u.ah.rB) ){
64906 goto arithmetic_result_is_null;
64907 }
64908 pOut->r = u.ah.rB;
64909 MemSetTypeFlag(pOut, MEM_Real);
64910 if( (u.ah.flags & MEM_Real)==0 ){
64911 sqlite3VdbeIntegerAffinity(pOut);
64912 }
64913 #endif
64914 }
64915 break;
@@ -64915,96 +64957,96 @@
64957 ** invocation of this opcode.
64958 **
64959 ** See also: AggStep and AggFinal
64960 */
64961 case OP_Function: {
64962 #if 0 /* local variables moved into u.ai */
64963 int i;
64964 Mem *pArg;
64965 sqlite3_context ctx;
64966 sqlite3_value **apVal;
64967 int n;
64968 #endif /* local variables moved into u.ai */
64969
64970 u.ai.n = pOp->p5;
64971 u.ai.apVal = p->apArg;
64972 assert( u.ai.apVal || u.ai.n==0 );
64973 assert( pOp->p3>0 && pOp->p3<=p->nMem );
64974 pOut = &aMem[pOp->p3];
64975 memAboutToChange(p, pOut);
64976
64977 assert( u.ai.n==0 || (pOp->p2>0 && pOp->p2+u.ai.n<=p->nMem+1) );
64978 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ai.n );
64979 u.ai.pArg = &aMem[pOp->p2];
64980 for(u.ai.i=0; u.ai.i<u.ai.n; u.ai.i++, u.ai.pArg++){
64981 assert( memIsValid(u.ai.pArg) );
64982 u.ai.apVal[u.ai.i] = u.ai.pArg;
64983 Deephemeralize(u.ai.pArg);
64984 sqlite3VdbeMemStoreType(u.ai.pArg);
64985 REGISTER_TRACE(pOp->p2+u.ai.i, u.ai.pArg);
64986 }
64987
64988 assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
64989 if( pOp->p4type==P4_FUNCDEF ){
64990 u.ai.ctx.pFunc = pOp->p4.pFunc;
64991 u.ai.ctx.pVdbeFunc = 0;
64992 }else{
64993 u.ai.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
64994 u.ai.ctx.pFunc = u.ai.ctx.pVdbeFunc->pFunc;
64995 }
64996
64997 u.ai.ctx.s.flags = MEM_Null;
64998 u.ai.ctx.s.db = db;
64999 u.ai.ctx.s.xDel = 0;
65000 u.ai.ctx.s.zMalloc = 0;
65001
65002 /* The output cell may already have a buffer allocated. Move
65003 ** the pointer to u.ai.ctx.s so in case the user-function can use
65004 ** the already allocated buffer instead of allocating a new one.
65005 */
65006 sqlite3VdbeMemMove(&u.ai.ctx.s, pOut);
65007 MemSetTypeFlag(&u.ai.ctx.s, MEM_Null);
65008
65009 u.ai.ctx.isError = 0;
65010 if( u.ai.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
65011 assert( pOp>aOp );
65012 assert( pOp[-1].p4type==P4_COLLSEQ );
65013 assert( pOp[-1].opcode==OP_CollSeq );
65014 u.ai.ctx.pColl = pOp[-1].p4.pColl;
65015 }
65016 db->lastRowid = lastRowid;
65017 (*u.ai.ctx.pFunc->xFunc)(&u.ai.ctx, u.ai.n, u.ai.apVal); /* IMP: R-24505-23230 */
65018 lastRowid = db->lastRowid;
65019
65020 /* If any auxiliary data functions have been called by this user function,
65021 ** immediately call the destructor for any non-static values.
65022 */
65023 if( u.ai.ctx.pVdbeFunc ){
65024 sqlite3VdbeDeleteAuxData(u.ai.ctx.pVdbeFunc, pOp->p1);
65025 pOp->p4.pVdbeFunc = u.ai.ctx.pVdbeFunc;
65026 pOp->p4type = P4_VDBEFUNC;
65027 }
65028
65029 if( db->mallocFailed ){
65030 /* Even though a malloc() has failed, the implementation of the
65031 ** user function may have called an sqlite3_result_XXX() function
65032 ** to return a value. The following call releases any resources
65033 ** associated with such a value.
65034 */
65035 sqlite3VdbeMemRelease(&u.ai.ctx.s);
65036 goto no_mem;
65037 }
65038
65039 /* If the function returned an error, throw an exception */
65040 if( u.ai.ctx.isError ){
65041 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ai.ctx.s));
65042 rc = u.ai.ctx.isError;
65043 }
65044
65045 /* Copy the result of the function into register P3 */
65046 sqlite3VdbeChangeEncoding(&u.ai.ctx.s, encoding);
65047 sqlite3VdbeMemMove(pOut, &u.ai.ctx.s);
65048 if( sqlite3VdbeMemTooBig(pOut) ){
65049 goto too_big;
65050 }
65051
65052 #if 0
@@ -65048,56 +65090,56 @@
65090 */
65091 case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
65092 case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
65093 case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
65094 case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
65095 #if 0 /* local variables moved into u.aj */
65096 i64 iA;
65097 u64 uA;
65098 i64 iB;
65099 u8 op;
65100 #endif /* local variables moved into u.aj */
65101
65102 pIn1 = &aMem[pOp->p1];
65103 pIn2 = &aMem[pOp->p2];
65104 pOut = &aMem[pOp->p3];
65105 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
65106 sqlite3VdbeMemSetNull(pOut);
65107 break;
65108 }
65109 u.aj.iA = sqlite3VdbeIntValue(pIn2);
65110 u.aj.iB = sqlite3VdbeIntValue(pIn1);
65111 u.aj.op = pOp->opcode;
65112 if( u.aj.op==OP_BitAnd ){
65113 u.aj.iA &= u.aj.iB;
65114 }else if( u.aj.op==OP_BitOr ){
65115 u.aj.iA |= u.aj.iB;
65116 }else if( u.aj.iB!=0 ){
65117 assert( u.aj.op==OP_ShiftRight || u.aj.op==OP_ShiftLeft );
65118
65119 /* If shifting by a negative amount, shift in the other direction */
65120 if( u.aj.iB<0 ){
65121 assert( OP_ShiftRight==OP_ShiftLeft+1 );
65122 u.aj.op = 2*OP_ShiftLeft + 1 - u.aj.op;
65123 u.aj.iB = u.aj.iB>(-64) ? -u.aj.iB : 64;
65124 }
65125
65126 if( u.aj.iB>=64 ){
65127 u.aj.iA = (u.aj.iA>=0 || u.aj.op==OP_ShiftLeft) ? 0 : -1;
65128 }else{
65129 memcpy(&u.aj.uA, &u.aj.iA, sizeof(u.aj.uA));
65130 if( u.aj.op==OP_ShiftLeft ){
65131 u.aj.uA <<= u.aj.iB;
65132 }else{
65133 u.aj.uA >>= u.aj.iB;
65134 /* Sign-extend on a right shift of a negative number */
65135 if( u.aj.iA<0 ) u.aj.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.aj.iB);
65136 }
65137 memcpy(&u.aj.iA, &u.aj.uA, sizeof(u.aj.iA));
65138 }
65139 }
65140 pOut->u.i = u.aj.iA;
65141 MemSetTypeFlag(pOut, MEM_Int);
65142 break;
65143 }
65144
65145 /* Opcode: AddImm P1 P2 * * *
@@ -65285,10 +65327,14 @@
65327 ** are of different types, then numbers are considered less than
65328 ** strings and strings are considered less than blobs.
65329 **
65330 ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
65331 ** store a boolean result (either 0, or 1, or NULL) in register P2.
65332 **
65333 ** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
65334 ** equal to one another, provided that they do not have their MEM_Cleared
65335 ** bit set.
65336 */
65337 /* Opcode: Ne P1 P2 P3 P4 P5
65338 **
65339 ** This works just like the Lt opcode except that the jump is taken if
65340 ** the operands in registers P1 and P3 are not equal. See the Lt opcode for
@@ -65334,30 +65380,38 @@
65380 case OP_Ne: /* same as TK_NE, jump, in1, in3 */
65381 case OP_Lt: /* same as TK_LT, jump, in1, in3 */
65382 case OP_Le: /* same as TK_LE, jump, in1, in3 */
65383 case OP_Gt: /* same as TK_GT, jump, in1, in3 */
65384 case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
65385 #if 0 /* local variables moved into u.ak */
65386 int res; /* Result of the comparison of pIn1 against pIn3 */
65387 char affinity; /* Affinity to use for comparison */
65388 u16 flags1; /* Copy of initial value of pIn1->flags */
65389 u16 flags3; /* Copy of initial value of pIn3->flags */
65390 #endif /* local variables moved into u.ak */
65391
65392 pIn1 = &aMem[pOp->p1];
65393 pIn3 = &aMem[pOp->p3];
65394 u.ak.flags1 = pIn1->flags;
65395 u.ak.flags3 = pIn3->flags;
65396 if( (u.ak.flags1 | u.ak.flags3)&MEM_Null ){
65397 /* One or both operands are NULL */
65398 if( pOp->p5 & SQLITE_NULLEQ ){
65399 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
65400 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
65401 ** or not both operands are null.
65402 */
65403 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
65404 assert( (u.ak.flags1 & MEM_Cleared)==0 );
65405 if( (u.ak.flags1&MEM_Null)!=0
65406 && (u.ak.flags3&MEM_Null)!=0
65407 && (u.ak.flags3&MEM_Cleared)==0
65408 ){
65409 u.ak.res = 0; /* Results are equal */
65410 }else{
65411 u.ak.res = 1; /* Results are not equal */
65412 }
65413 }else{
65414 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
65415 ** then the result is always NULL.
65416 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
65417 */
@@ -65370,44 +65424,44 @@
65424 }
65425 break;
65426 }
65427 }else{
65428 /* Neither operand is NULL. Do a comparison. */
65429 u.ak.affinity = pOp->p5 & SQLITE_AFF_MASK;
65430 if( u.ak.affinity ){
65431 applyAffinity(pIn1, u.ak.affinity, encoding);
65432 applyAffinity(pIn3, u.ak.affinity, encoding);
65433 if( db->mallocFailed ) goto no_mem;
65434 }
65435
65436 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
65437 ExpandBlob(pIn1);
65438 ExpandBlob(pIn3);
65439 u.ak.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
65440 }
65441 switch( pOp->opcode ){
65442 case OP_Eq: u.ak.res = u.ak.res==0; break;
65443 case OP_Ne: u.ak.res = u.ak.res!=0; break;
65444 case OP_Lt: u.ak.res = u.ak.res<0; break;
65445 case OP_Le: u.ak.res = u.ak.res<=0; break;
65446 case OP_Gt: u.ak.res = u.ak.res>0; break;
65447 default: u.ak.res = u.ak.res>=0; break;
65448 }
65449
65450 if( pOp->p5 & SQLITE_STOREP2 ){
65451 pOut = &aMem[pOp->p2];
65452 memAboutToChange(p, pOut);
65453 MemSetTypeFlag(pOut, MEM_Int);
65454 pOut->u.i = u.ak.res;
65455 REGISTER_TRACE(pOp->p2, pOut);
65456 }else if( u.ak.res ){
65457 pc = pOp->p2-1;
65458 }
65459
65460 /* Undo any changes made by applyAffinity() to the input registers. */
65461 pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.ak.flags1&MEM_TypeMask);
65462 pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.ak.flags3&MEM_TypeMask);
65463 break;
65464 }
65465
65466 /* Opcode: Permutation * * * P4 *
65467 **
@@ -65438,50 +65492,50 @@
65492 ** The comparison is a sort comparison, so NULLs compare equal,
65493 ** NULLs are less than numbers, numbers are less than strings,
65494 ** and strings are less than blobs.
65495 */
65496 case OP_Compare: {
65497 #if 0 /* local variables moved into u.al */
65498 int n;
65499 int i;
65500 int p1;
65501 int p2;
65502 const KeyInfo *pKeyInfo;
65503 int idx;
65504 CollSeq *pColl; /* Collating sequence to use on this term */
65505 int bRev; /* True for DESCENDING sort order */
65506 #endif /* local variables moved into u.al */
65507
65508 u.al.n = pOp->p3;
65509 u.al.pKeyInfo = pOp->p4.pKeyInfo;
65510 assert( u.al.n>0 );
65511 assert( u.al.pKeyInfo!=0 );
65512 u.al.p1 = pOp->p1;
65513 u.al.p2 = pOp->p2;
65514 #if SQLITE_DEBUG
65515 if( aPermute ){
65516 int k, mx = 0;
65517 for(k=0; k<u.al.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
65518 assert( u.al.p1>0 && u.al.p1+mx<=p->nMem+1 );
65519 assert( u.al.p2>0 && u.al.p2+mx<=p->nMem+1 );
65520 }else{
65521 assert( u.al.p1>0 && u.al.p1+u.al.n<=p->nMem+1 );
65522 assert( u.al.p2>0 && u.al.p2+u.al.n<=p->nMem+1 );
65523 }
65524 #endif /* SQLITE_DEBUG */
65525 for(u.al.i=0; u.al.i<u.al.n; u.al.i++){
65526 u.al.idx = aPermute ? aPermute[u.al.i] : u.al.i;
65527 assert( memIsValid(&aMem[u.al.p1+u.al.idx]) );
65528 assert( memIsValid(&aMem[u.al.p2+u.al.idx]) );
65529 REGISTER_TRACE(u.al.p1+u.al.idx, &aMem[u.al.p1+u.al.idx]);
65530 REGISTER_TRACE(u.al.p2+u.al.idx, &aMem[u.al.p2+u.al.idx]);
65531 assert( u.al.i<u.al.pKeyInfo->nField );
65532 u.al.pColl = u.al.pKeyInfo->aColl[u.al.i];
65533 u.al.bRev = u.al.pKeyInfo->aSortOrder[u.al.i];
65534 iCompare = sqlite3MemCompare(&aMem[u.al.p1+u.al.idx], &aMem[u.al.p2+u.al.idx], u.al.pColl);
65535 if( iCompare ){
65536 if( u.al.bRev ) iCompare = -iCompare;
65537 break;
65538 }
65539 }
65540 aPermute = 0;
65541 break;
@@ -65522,39 +65576,39 @@
65576 ** even if the other input is NULL. A NULL and false or two NULLs
65577 ** give a NULL output.
65578 */
65579 case OP_And: /* same as TK_AND, in1, in2, out3 */
65580 case OP_Or: { /* same as TK_OR, in1, in2, out3 */
65581 #if 0 /* local variables moved into u.am */
65582 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
65583 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
65584 #endif /* local variables moved into u.am */
65585
65586 pIn1 = &aMem[pOp->p1];
65587 if( pIn1->flags & MEM_Null ){
65588 u.am.v1 = 2;
65589 }else{
65590 u.am.v1 = sqlite3VdbeIntValue(pIn1)!=0;
65591 }
65592 pIn2 = &aMem[pOp->p2];
65593 if( pIn2->flags & MEM_Null ){
65594 u.am.v2 = 2;
65595 }else{
65596 u.am.v2 = sqlite3VdbeIntValue(pIn2)!=0;
65597 }
65598 if( pOp->opcode==OP_And ){
65599 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
65600 u.am.v1 = and_logic[u.am.v1*3+u.am.v2];
65601 }else{
65602 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
65603 u.am.v1 = or_logic[u.am.v1*3+u.am.v2];
65604 }
65605 pOut = &aMem[pOp->p3];
65606 if( u.am.v1==2 ){
65607 MemSetTypeFlag(pOut, MEM_Null);
65608 }else{
65609 pOut->u.i = u.am.v1;
65610 MemSetTypeFlag(pOut, MEM_Int);
65611 }
65612 break;
65613 }
65614
@@ -65621,25 +65675,25 @@
65675 ** is considered false if it has a numeric value of zero. If the value
65676 ** in P1 is NULL then take the jump if P3 is zero.
65677 */
65678 case OP_If: /* jump, in1 */
65679 case OP_IfNot: { /* jump, in1 */
65680 #if 0 /* local variables moved into u.an */
65681 int c;
65682 #endif /* local variables moved into u.an */
65683 pIn1 = &aMem[pOp->p1];
65684 if( pIn1->flags & MEM_Null ){
65685 u.an.c = pOp->p3;
65686 }else{
65687 #ifdef SQLITE_OMIT_FLOATING_POINT
65688 u.an.c = sqlite3VdbeIntValue(pIn1)!=0;
65689 #else
65690 u.an.c = sqlite3VdbeRealValue(pIn1)!=0.0;
65691 #endif
65692 if( pOp->opcode==OP_IfNot ) u.an.c = !u.an.c;
65693 }
65694 if( u.an.c ){
65695 pc = pOp->p2-1;
65696 }
65697 break;
65698 }
65699
@@ -65690,11 +65744,11 @@
65744 ** the result is guaranteed to only be used as the argument of a length()
65745 ** or typeof() function, respectively. The loading of large blobs can be
65746 ** skipped for length() and all content loading can be skipped for typeof().
65747 */
65748 case OP_Column: {
65749 #if 0 /* local variables moved into u.ao */
65750 u32 payloadSize; /* Number of bytes in the record */
65751 i64 payloadSize64; /* Number of bytes in the record */
65752 int p1; /* P1 value of the opcode */
65753 int p2; /* column number to retrieve */
65754 VdbeCursor *pC; /* The VDBE cursor */
@@ -65714,130 +65768,130 @@
65768 u32 szField; /* Number of bytes in the content of a field */
65769 int szHdr; /* Size of the header size field at start of record */
65770 int avail; /* Number of bytes of available data */
65771 u32 t; /* A type code from the record header */
65772 Mem *pReg; /* PseudoTable input register */
65773 #endif /* local variables moved into u.ao */
65774
65775
65776 u.ao.p1 = pOp->p1;
65777 u.ao.p2 = pOp->p2;
65778 u.ao.pC = 0;
65779 memset(&u.ao.sMem, 0, sizeof(u.ao.sMem));
65780 assert( u.ao.p1<p->nCursor );
65781 assert( pOp->p3>0 && pOp->p3<=p->nMem );
65782 u.ao.pDest = &aMem[pOp->p3];
65783 memAboutToChange(p, u.ao.pDest);
65784 u.ao.zRec = 0;
65785
65786 /* This block sets the variable u.ao.payloadSize to be the total number of
65787 ** bytes in the record.
65788 **
65789 ** u.ao.zRec is set to be the complete text of the record if it is available.
65790 ** The complete record text is always available for pseudo-tables
65791 ** If the record is stored in a cursor, the complete record text
65792 ** might be available in the u.ao.pC->aRow cache. Or it might not be.
65793 ** If the data is unavailable, u.ao.zRec is set to NULL.
65794 **
65795 ** We also compute the number of columns in the record. For cursors,
65796 ** the number of columns is stored in the VdbeCursor.nField element.
65797 */
65798 u.ao.pC = p->apCsr[u.ao.p1];
65799 assert( u.ao.pC!=0 );
65800 #ifndef SQLITE_OMIT_VIRTUALTABLE
65801 assert( u.ao.pC->pVtabCursor==0 );
65802 #endif
65803 u.ao.pCrsr = u.ao.pC->pCursor;
65804 if( u.ao.pCrsr!=0 ){
65805 /* The record is stored in a B-Tree */
65806 rc = sqlite3VdbeCursorMoveto(u.ao.pC);
65807 if( rc ) goto abort_due_to_error;
65808 if( u.ao.pC->nullRow ){
65809 u.ao.payloadSize = 0;
65810 }else if( u.ao.pC->cacheStatus==p->cacheCtr ){
65811 u.ao.payloadSize = u.ao.pC->payloadSize;
65812 u.ao.zRec = (char*)u.ao.pC->aRow;
65813 }else if( u.ao.pC->isIndex ){
65814 assert( sqlite3BtreeCursorIsValid(u.ao.pCrsr) );
65815 VVA_ONLY(rc =) sqlite3BtreeKeySize(u.ao.pCrsr, &u.ao.payloadSize64);
65816 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
65817 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
65818 ** payload size, so it is impossible for u.ao.payloadSize64 to be
65819 ** larger than 32 bits. */
65820 assert( (u.ao.payloadSize64 & SQLITE_MAX_U32)==(u64)u.ao.payloadSize64 );
65821 u.ao.payloadSize = (u32)u.ao.payloadSize64;
65822 }else{
65823 assert( sqlite3BtreeCursorIsValid(u.ao.pCrsr) );
65824 VVA_ONLY(rc =) sqlite3BtreeDataSize(u.ao.pCrsr, &u.ao.payloadSize);
65825 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
65826 }
65827 }else if( ALWAYS(u.ao.pC->pseudoTableReg>0) ){
65828 u.ao.pReg = &aMem[u.ao.pC->pseudoTableReg];
65829 assert( u.ao.pReg->flags & MEM_Blob );
65830 assert( memIsValid(u.ao.pReg) );
65831 u.ao.payloadSize = u.ao.pReg->n;
65832 u.ao.zRec = u.ao.pReg->z;
65833 u.ao.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
65834 assert( u.ao.payloadSize==0 || u.ao.zRec!=0 );
65835 }else{
65836 /* Consider the row to be NULL */
65837 u.ao.payloadSize = 0;
65838 }
65839
65840 /* If u.ao.payloadSize is 0, then just store a NULL. This can happen because of
65841 ** nullRow or because of a corrupt database. */
65842 if( u.ao.payloadSize==0 ){
65843 MemSetTypeFlag(u.ao.pDest, MEM_Null);
65844 goto op_column_out;
65845 }
65846 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
65847 if( u.ao.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
65848 goto too_big;
65849 }
65850
65851 u.ao.nField = u.ao.pC->nField;
65852 assert( u.ao.p2<u.ao.nField );
65853
65854 /* Read and parse the table header. Store the results of the parse
65855 ** into the record header cache fields of the cursor.
65856 */
65857 u.ao.aType = u.ao.pC->aType;
65858 if( u.ao.pC->cacheStatus==p->cacheCtr ){
65859 u.ao.aOffset = u.ao.pC->aOffset;
65860 }else{
65861 assert(u.ao.aType);
65862 u.ao.avail = 0;
65863 u.ao.pC->aOffset = u.ao.aOffset = &u.ao.aType[u.ao.nField];
65864 u.ao.pC->payloadSize = u.ao.payloadSize;
65865 u.ao.pC->cacheStatus = p->cacheCtr;
65866
65867 /* Figure out how many bytes are in the header */
65868 if( u.ao.zRec ){
65869 u.ao.zData = u.ao.zRec;
65870 }else{
65871 if( u.ao.pC->isIndex ){
65872 u.ao.zData = (char*)sqlite3BtreeKeyFetch(u.ao.pCrsr, &u.ao.avail);
65873 }else{
65874 u.ao.zData = (char*)sqlite3BtreeDataFetch(u.ao.pCrsr, &u.ao.avail);
65875 }
65876 /* If KeyFetch()/DataFetch() managed to get the entire payload,
65877 ** save the payload in the u.ao.pC->aRow cache. That will save us from
65878 ** having to make additional calls to fetch the content portion of
65879 ** the record.
65880 */
65881 assert( u.ao.avail>=0 );
65882 if( u.ao.payloadSize <= (u32)u.ao.avail ){
65883 u.ao.zRec = u.ao.zData;
65884 u.ao.pC->aRow = (u8*)u.ao.zData;
65885 }else{
65886 u.ao.pC->aRow = 0;
65887 }
65888 }
65889 /* The following assert is true in all cases except when
65890 ** the database file has been corrupted externally.
65891 ** assert( u.ao.zRec!=0 || u.ao.avail>=u.ao.payloadSize || u.ao.avail>=9 ); */
65892 u.ao.szHdr = getVarint32((u8*)u.ao.zData, u.ao.offset);
65893
65894 /* Make sure a corrupt database has not given us an oversize header.
65895 ** Do this now to avoid an oversize memory allocation.
65896 **
65897 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
@@ -65844,161 +65898,161 @@
65898 ** types use so much data space that there can only be 4096 and 32 of
65899 ** them, respectively. So the maximum header length results from a
65900 ** 3-byte type for each of the maximum of 32768 columns plus three
65901 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
65902 */
65903 if( u.ao.offset > 98307 ){
65904 rc = SQLITE_CORRUPT_BKPT;
65905 goto op_column_out;
65906 }
65907
65908 /* Compute in u.ao.len the number of bytes of data we need to read in order
65909 ** to get u.ao.nField type values. u.ao.offset is an upper bound on this. But
65910 ** u.ao.nField might be significantly less than the true number of columns
65911 ** in the table, and in that case, 5*u.ao.nField+3 might be smaller than u.ao.offset.
65912 ** We want to minimize u.ao.len in order to limit the size of the memory
65913 ** allocation, especially if a corrupt database file has caused u.ao.offset
65914 ** to be oversized. Offset is limited to 98307 above. But 98307 might
65915 ** still exceed Robson memory allocation limits on some configurations.
65916 ** On systems that cannot tolerate large memory allocations, u.ao.nField*5+3
65917 ** will likely be much smaller since u.ao.nField will likely be less than
65918 ** 20 or so. This insures that Robson memory allocation limits are
65919 ** not exceeded even for corrupt database files.
65920 */
65921 u.ao.len = u.ao.nField*5 + 3;
65922 if( u.ao.len > (int)u.ao.offset ) u.ao.len = (int)u.ao.offset;
65923
65924 /* The KeyFetch() or DataFetch() above are fast and will get the entire
65925 ** record header in most cases. But they will fail to get the complete
65926 ** record header if the record header does not fit on a single page
65927 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
65928 ** acquire the complete header text.
65929 */
65930 if( !u.ao.zRec && u.ao.avail<u.ao.len ){
65931 u.ao.sMem.flags = 0;
65932 u.ao.sMem.db = 0;
65933 rc = sqlite3VdbeMemFromBtree(u.ao.pCrsr, 0, u.ao.len, u.ao.pC->isIndex, &u.ao.sMem);
65934 if( rc!=SQLITE_OK ){
65935 goto op_column_out;
65936 }
65937 u.ao.zData = u.ao.sMem.z;
65938 }
65939 u.ao.zEndHdr = (u8 *)&u.ao.zData[u.ao.len];
65940 u.ao.zIdx = (u8 *)&u.ao.zData[u.ao.szHdr];
65941
65942 /* Scan the header and use it to fill in the u.ao.aType[] and u.ao.aOffset[]
65943 ** arrays. u.ao.aType[u.ao.i] will contain the type integer for the u.ao.i-th
65944 ** column and u.ao.aOffset[u.ao.i] will contain the u.ao.offset from the beginning
65945 ** of the record to the start of the data for the u.ao.i-th column
65946 */
65947 for(u.ao.i=0; u.ao.i<u.ao.nField; u.ao.i++){
65948 if( u.ao.zIdx<u.ao.zEndHdr ){
65949 u.ao.aOffset[u.ao.i] = u.ao.offset;
65950 if( u.ao.zIdx[0]<0x80 ){
65951 u.ao.t = u.ao.zIdx[0];
65952 u.ao.zIdx++;
65953 }else{
65954 u.ao.zIdx += sqlite3GetVarint32(u.ao.zIdx, &u.ao.t);
65955 }
65956 u.ao.aType[u.ao.i] = u.ao.t;
65957 u.ao.szField = sqlite3VdbeSerialTypeLen(u.ao.t);
65958 u.ao.offset += u.ao.szField;
65959 if( u.ao.offset<u.ao.szField ){ /* True if u.ao.offset overflows */
65960 u.ao.zIdx = &u.ao.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
65961 break;
65962 }
65963 }else{
65964 /* If u.ao.i is less that u.ao.nField, then there are fewer fields in this
65965 ** record than SetNumColumns indicated there are columns in the
65966 ** table. Set the u.ao.offset for any extra columns not present in
65967 ** the record to 0. This tells code below to store the default value
65968 ** for the column instead of deserializing a value from the record.
65969 */
65970 u.ao.aOffset[u.ao.i] = 0;
65971 }
65972 }
65973 sqlite3VdbeMemRelease(&u.ao.sMem);
65974 u.ao.sMem.flags = MEM_Null;
65975
65976 /* If we have read more header data than was contained in the header,
65977 ** or if the end of the last field appears to be past the end of the
65978 ** record, or if the end of the last field appears to be before the end
65979 ** of the record (when all fields present), then we must be dealing
65980 ** with a corrupt database.
65981 */
65982 if( (u.ao.zIdx > u.ao.zEndHdr) || (u.ao.offset > u.ao.payloadSize)
65983 || (u.ao.zIdx==u.ao.zEndHdr && u.ao.offset!=u.ao.payloadSize) ){
65984 rc = SQLITE_CORRUPT_BKPT;
65985 goto op_column_out;
65986 }
65987 }
65988
65989 /* Get the column information. If u.ao.aOffset[u.ao.p2] is non-zero, then
65990 ** deserialize the value from the record. If u.ao.aOffset[u.ao.p2] is zero,
65991 ** then there are not enough fields in the record to satisfy the
65992 ** request. In this case, set the value NULL or to P4 if P4 is
65993 ** a pointer to a Mem object.
65994 */
65995 if( u.ao.aOffset[u.ao.p2] ){
65996 assert( rc==SQLITE_OK );
65997 if( u.ao.zRec ){
65998 /* This is the common case where the whole row fits on a single page */
65999 VdbeMemRelease(u.ao.pDest);
66000 sqlite3VdbeSerialGet((u8 *)&u.ao.zRec[u.ao.aOffset[u.ao.p2]], u.ao.aType[u.ao.p2], u.ao.pDest);
66001 }else{
66002 /* This branch happens only when the row overflows onto multiple pages */
66003 u.ao.t = u.ao.aType[u.ao.p2];
66004 if( (pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
66005 && ((u.ao.t>=12 && (u.ao.t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)
66006 ){
66007 /* Content is irrelevant for the typeof() function and for
66008 ** the length(X) function if X is a blob. So we might as well use
66009 ** bogus content rather than reading content from disk. NULL works
66010 ** for text and blob and whatever is in the u.ao.payloadSize64 variable
66011 ** will work for everything else. */
66012 u.ao.zData = u.ao.t<12 ? (char*)&u.ao.payloadSize64 : 0;
66013 }else{
66014 u.ao.len = sqlite3VdbeSerialTypeLen(u.ao.t);
66015 sqlite3VdbeMemMove(&u.ao.sMem, u.ao.pDest);
66016 rc = sqlite3VdbeMemFromBtree(u.ao.pCrsr, u.ao.aOffset[u.ao.p2], u.ao.len, u.ao.pC->isIndex,
66017 &u.ao.sMem);
66018 if( rc!=SQLITE_OK ){
66019 goto op_column_out;
66020 }
66021 u.ao.zData = u.ao.sMem.z;
66022 }
66023 sqlite3VdbeSerialGet((u8*)u.ao.zData, u.ao.t, u.ao.pDest);
66024 }
66025 u.ao.pDest->enc = encoding;
66026 }else{
66027 if( pOp->p4type==P4_MEM ){
66028 sqlite3VdbeMemShallowCopy(u.ao.pDest, pOp->p4.pMem, MEM_Static);
66029 }else{
66030 MemSetTypeFlag(u.ao.pDest, MEM_Null);
66031 }
66032 }
66033
66034 /* If we dynamically allocated space to hold the data (in the
66035 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
66036 ** dynamically allocated space over to the u.ao.pDest structure.
66037 ** This prevents a memory copy.
66038 */
66039 if( u.ao.sMem.zMalloc ){
66040 assert( u.ao.sMem.z==u.ao.sMem.zMalloc );
66041 assert( !(u.ao.pDest->flags & MEM_Dyn) );
66042 assert( !(u.ao.pDest->flags & (MEM_Blob|MEM_Str)) || u.ao.pDest->z==u.ao.sMem.z );
66043 u.ao.pDest->flags &= ~(MEM_Ephem|MEM_Static);
66044 u.ao.pDest->flags |= MEM_Term;
66045 u.ao.pDest->z = u.ao.sMem.z;
66046 u.ao.pDest->zMalloc = u.ao.sMem.zMalloc;
66047 }
66048
66049 rc = sqlite3VdbeMemMakeWriteable(u.ao.pDest);
66050
66051 op_column_out:
66052 UPDATE_MAX_BLOBSIZE(u.ao.pDest);
66053 REGISTER_TRACE(pOp->p3, u.ao.pDest);
66054 break;
66055 }
66056
66057 /* Opcode: Affinity P1 P2 * P4 *
66058 **
@@ -66007,24 +66061,24 @@
66061 ** P4 is a string that is P2 characters long. The nth character of the
66062 ** string indicates the column affinity that should be used for the nth
66063 ** memory cell in the range.
66064 */
66065 case OP_Affinity: {
66066 #if 0 /* local variables moved into u.ap */
66067 const char *zAffinity; /* The affinity to be applied */
66068 char cAff; /* A single character of affinity */
66069 #endif /* local variables moved into u.ap */
66070
66071 u.ap.zAffinity = pOp->p4.z;
66072 assert( u.ap.zAffinity!=0 );
66073 assert( u.ap.zAffinity[pOp->p2]==0 );
66074 pIn1 = &aMem[pOp->p1];
66075 while( (u.ap.cAff = *(u.ap.zAffinity++))!=0 ){
66076 assert( pIn1 <= &p->aMem[p->nMem] );
66077 assert( memIsValid(pIn1) );
66078 ExpandBlob(pIn1);
66079 applyAffinity(pIn1, u.ap.cAff, encoding);
66080 pIn1++;
66081 }
66082 break;
66083 }
66084
@@ -66042,11 +66096,11 @@
66096 ** macros defined in sqliteInt.h.
66097 **
66098 ** If P4 is NULL then all index fields have the affinity NONE.
66099 */
66100 case OP_MakeRecord: {
66101 #if 0 /* local variables moved into u.aq */
66102 u8 *zNewRecord; /* A buffer to hold the data for the new record */
66103 Mem *pRec; /* The new record */
66104 u64 nData; /* Number of bytes of data space */
66105 int nHdr; /* Number of bytes of header space */
66106 i64 nByte; /* Data space required for this record */
@@ -66058,11 +66112,11 @@
66112 int nField; /* Number of fields in the record */
66113 char *zAffinity; /* The affinity string for the record */
66114 int file_format; /* File format to use for encoding */
66115 int i; /* Space used in zNewRecord[] */
66116 int len; /* Length of a field */
66117 #endif /* local variables moved into u.aq */
66118
66119 /* Assuming the record contains N fields, the record format looks
66120 ** like this:
66121 **
66122 ** ------------------------------------------------------------------------
@@ -66075,87 +66129,87 @@
66129 ** Each type field is a varint representing the serial type of the
66130 ** corresponding data element (see sqlite3VdbeSerialType()). The
66131 ** hdr-size field is also a varint which is the offset from the beginning
66132 ** of the record to data0.
66133 */
66134 u.aq.nData = 0; /* Number of bytes of data space */
66135 u.aq.nHdr = 0; /* Number of bytes of header space */
66136 u.aq.nZero = 0; /* Number of zero bytes at the end of the record */
66137 u.aq.nField = pOp->p1;
66138 u.aq.zAffinity = pOp->p4.z;
66139 assert( u.aq.nField>0 && pOp->p2>0 && pOp->p2+u.aq.nField<=p->nMem+1 );
66140 u.aq.pData0 = &aMem[u.aq.nField];
66141 u.aq.nField = pOp->p2;
66142 u.aq.pLast = &u.aq.pData0[u.aq.nField-1];
66143 u.aq.file_format = p->minWriteFileFormat;
66144
66145 /* Identify the output register */
66146 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
66147 pOut = &aMem[pOp->p3];
66148 memAboutToChange(p, pOut);
66149
66150 /* Loop through the elements that will make up the record to figure
66151 ** out how much space is required for the new record.
66152 */
66153 for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){
66154 assert( memIsValid(u.aq.pRec) );
66155 if( u.aq.zAffinity ){
66156 applyAffinity(u.aq.pRec, u.aq.zAffinity[u.aq.pRec-u.aq.pData0], encoding);
66157 }
66158 if( u.aq.pRec->flags&MEM_Zero && u.aq.pRec->n>0 ){
66159 sqlite3VdbeMemExpandBlob(u.aq.pRec);
66160 }
66161 u.aq.serial_type = sqlite3VdbeSerialType(u.aq.pRec, u.aq.file_format);
66162 u.aq.len = sqlite3VdbeSerialTypeLen(u.aq.serial_type);
66163 u.aq.nData += u.aq.len;
66164 u.aq.nHdr += sqlite3VarintLen(u.aq.serial_type);
66165 if( u.aq.pRec->flags & MEM_Zero ){
66166 /* Only pure zero-filled BLOBs can be input to this Opcode.
66167 ** We do not allow blobs with a prefix and a zero-filled tail. */
66168 u.aq.nZero += u.aq.pRec->u.nZero;
66169 }else if( u.aq.len ){
66170 u.aq.nZero = 0;
66171 }
66172 }
66173
66174 /* Add the initial header varint and total the size */
66175 u.aq.nHdr += u.aq.nVarint = sqlite3VarintLen(u.aq.nHdr);
66176 if( u.aq.nVarint<sqlite3VarintLen(u.aq.nHdr) ){
66177 u.aq.nHdr++;
66178 }
66179 u.aq.nByte = u.aq.nHdr+u.aq.nData-u.aq.nZero;
66180 if( u.aq.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
66181 goto too_big;
66182 }
66183
66184 /* Make sure the output register has a buffer large enough to store
66185 ** the new record. The output register (pOp->p3) is not allowed to
66186 ** be one of the input registers (because the following call to
66187 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
66188 */
66189 if( sqlite3VdbeMemGrow(pOut, (int)u.aq.nByte, 0) ){
66190 goto no_mem;
66191 }
66192 u.aq.zNewRecord = (u8 *)pOut->z;
66193
66194 /* Write the record */
66195 u.aq.i = putVarint32(u.aq.zNewRecord, u.aq.nHdr);
66196 for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){
66197 u.aq.serial_type = sqlite3VdbeSerialType(u.aq.pRec, u.aq.file_format);
66198 u.aq.i += putVarint32(&u.aq.zNewRecord[u.aq.i], u.aq.serial_type); /* serial type */
66199 }
66200 for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){ /* serial data */
66201 u.aq.i += sqlite3VdbeSerialPut(&u.aq.zNewRecord[u.aq.i], (int)(u.aq.nByte-u.aq.i), u.aq.pRec,u.aq.file_format);
66202 }
66203 assert( u.aq.i==u.aq.nByte );
66204
66205 assert( pOp->p3>0 && pOp->p3<=p->nMem );
66206 pOut->n = (int)u.aq.nByte;
66207 pOut->flags = MEM_Blob | MEM_Dyn;
66208 pOut->xDel = 0;
66209 if( u.aq.nZero ){
66210 pOut->u.nZero = u.aq.nZero;
66211 pOut->flags |= MEM_Zero;
66212 }
66213 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
66214 REGISTER_TRACE(pOp->p3, pOut);
66215 UPDATE_MAX_BLOBSIZE(pOut);
@@ -66167,22 +66221,22 @@
66221 ** Store the number of entries (an integer value) in the table or index
66222 ** opened by cursor P1 in register P2
66223 */
66224 #ifndef SQLITE_OMIT_BTREECOUNT
66225 case OP_Count: { /* out2-prerelease */
66226 #if 0 /* local variables moved into u.ar */
66227 i64 nEntry;
66228 BtCursor *pCrsr;
66229 #endif /* local variables moved into u.ar */
66230
66231 u.ar.pCrsr = p->apCsr[pOp->p1]->pCursor;
66232 if( ALWAYS(u.ar.pCrsr) ){
66233 rc = sqlite3BtreeCount(u.ar.pCrsr, &u.ar.nEntry);
66234 }else{
66235 u.ar.nEntry = 0;
66236 }
66237 pOut->u.i = u.ar.nEntry;
66238 break;
66239 }
66240 #endif
66241
66242 /* Opcode: Savepoint P1 * * P4 *
@@ -66190,42 +66244,42 @@
66244 ** Open, release or rollback the savepoint named by parameter P4, depending
66245 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
66246 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
66247 */
66248 case OP_Savepoint: {
66249 #if 0 /* local variables moved into u.as */
66250 int p1; /* Value of P1 operand */
66251 char *zName; /* Name of savepoint */
66252 int nName;
66253 Savepoint *pNew;
66254 Savepoint *pSavepoint;
66255 Savepoint *pTmp;
66256 int iSavepoint;
66257 int ii;
66258 #endif /* local variables moved into u.as */
66259
66260 u.as.p1 = pOp->p1;
66261 u.as.zName = pOp->p4.z;
66262
66263 /* Assert that the u.as.p1 parameter is valid. Also that if there is no open
66264 ** transaction, then there cannot be any savepoints.
66265 */
66266 assert( db->pSavepoint==0 || db->autoCommit==0 );
66267 assert( u.as.p1==SAVEPOINT_BEGIN||u.as.p1==SAVEPOINT_RELEASE||u.as.p1==SAVEPOINT_ROLLBACK );
66268 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
66269 assert( checkSavepointCount(db) );
66270
66271 if( u.as.p1==SAVEPOINT_BEGIN ){
66272 if( db->writeVdbeCnt>0 ){
66273 /* A new savepoint cannot be created if there are active write
66274 ** statements (i.e. open read/write incremental blob handles).
66275 */
66276 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
66277 "SQL statements in progress");
66278 rc = SQLITE_BUSY;
66279 }else{
66280 u.as.nName = sqlite3Strlen30(u.as.zName);
66281
66282 #ifndef SQLITE_OMIT_VIRTUALTABLE
66283 /* This call is Ok even if this savepoint is actually a transaction
66284 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
66285 ** If this is a transaction savepoint being opened, it is guaranteed
@@ -66235,14 +66289,14 @@
66289 db->nStatement+db->nSavepoint);
66290 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66291 #endif
66292
66293 /* Create a new savepoint structure. */
66294 u.as.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.as.nName+1);
66295 if( u.as.pNew ){
66296 u.as.pNew->zName = (char *)&u.as.pNew[1];
66297 memcpy(u.as.pNew->zName, u.as.zName, u.as.nName+1);
66298
66299 /* If there is no open transaction, then mark this as a special
66300 ** "transaction savepoint". */
66301 if( db->autoCommit ){
66302 db->autoCommit = 0;
@@ -66250,31 +66304,31 @@
66304 }else{
66305 db->nSavepoint++;
66306 }
66307
66308 /* Link the new savepoint into the database handle's list. */
66309 u.as.pNew->pNext = db->pSavepoint;
66310 db->pSavepoint = u.as.pNew;
66311 u.as.pNew->nDeferredCons = db->nDeferredCons;
66312 }
66313 }
66314 }else{
66315 u.as.iSavepoint = 0;
66316
66317 /* Find the named savepoint. If there is no such savepoint, then an
66318 ** an error is returned to the user. */
66319 for(
66320 u.as.pSavepoint = db->pSavepoint;
66321 u.as.pSavepoint && sqlite3StrICmp(u.as.pSavepoint->zName, u.as.zName);
66322 u.as.pSavepoint = u.as.pSavepoint->pNext
66323 ){
66324 u.as.iSavepoint++;
66325 }
66326 if( !u.as.pSavepoint ){
66327 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.as.zName);
66328 rc = SQLITE_ERROR;
66329 }else if( db->writeVdbeCnt>0 && u.as.p1==SAVEPOINT_RELEASE ){
66330 /* It is not possible to release (commit) a savepoint if there are
66331 ** active write statements.
66332 */
66333 sqlite3SetString(&p->zErrMsg, db,
66334 "cannot release savepoint - SQL statements in progress"
@@ -66284,12 +66338,12 @@
66338
66339 /* Determine whether or not this is a transaction savepoint. If so,
66340 ** and this is a RELEASE command, then the current transaction
66341 ** is committed.
66342 */
66343 int isTransaction = u.as.pSavepoint->pNext==0 && db->isTransactionSavepoint;
66344 if( isTransaction && u.as.p1==SAVEPOINT_RELEASE ){
66345 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
66346 goto vdbe_return;
66347 }
66348 db->autoCommit = 1;
66349 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
@@ -66299,55 +66353,55 @@
66353 goto vdbe_return;
66354 }
66355 db->isTransactionSavepoint = 0;
66356 rc = p->rc;
66357 }else{
66358 u.as.iSavepoint = db->nSavepoint - u.as.iSavepoint - 1;
66359 if( u.as.p1==SAVEPOINT_ROLLBACK ){
66360 for(u.as.ii=0; u.as.ii<db->nDb; u.as.ii++){
66361 sqlite3BtreeTripAllCursors(db->aDb[u.as.ii].pBt, SQLITE_ABORT);
66362 }
66363 }
66364 for(u.as.ii=0; u.as.ii<db->nDb; u.as.ii++){
66365 rc = sqlite3BtreeSavepoint(db->aDb[u.as.ii].pBt, u.as.p1, u.as.iSavepoint);
66366 if( rc!=SQLITE_OK ){
66367 goto abort_due_to_error;
66368 }
66369 }
66370 if( u.as.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
66371 sqlite3ExpirePreparedStatements(db);
66372 sqlite3ResetAllSchemasOfConnection(db);
66373 db->flags = (db->flags | SQLITE_InternChanges);
66374 }
66375 }
66376
66377 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
66378 ** savepoints nested inside of the savepoint being operated on. */
66379 while( db->pSavepoint!=u.as.pSavepoint ){
66380 u.as.pTmp = db->pSavepoint;
66381 db->pSavepoint = u.as.pTmp->pNext;
66382 sqlite3DbFree(db, u.as.pTmp);
66383 db->nSavepoint--;
66384 }
66385
66386 /* If it is a RELEASE, then destroy the savepoint being operated on
66387 ** too. If it is a ROLLBACK TO, then set the number of deferred
66388 ** constraint violations present in the database to the value stored
66389 ** when the savepoint was created. */
66390 if( u.as.p1==SAVEPOINT_RELEASE ){
66391 assert( u.as.pSavepoint==db->pSavepoint );
66392 db->pSavepoint = u.as.pSavepoint->pNext;
66393 sqlite3DbFree(db, u.as.pSavepoint);
66394 if( !isTransaction ){
66395 db->nSavepoint--;
66396 }
66397 }else{
66398 db->nDeferredCons = u.as.pSavepoint->nDeferredCons;
66399 }
66400
66401 if( !isTransaction ){
66402 rc = sqlite3VtabSavepoint(db, u.as.p1, u.as.iSavepoint);
66403 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66404 }
66405 }
66406 }
66407
@@ -66362,53 +66416,53 @@
66416 ** there are active writing VMs or active VMs that use shared cache.
66417 **
66418 ** This instruction causes the VM to halt.
66419 */
66420 case OP_AutoCommit: {
66421 #if 0 /* local variables moved into u.at */
66422 int desiredAutoCommit;
66423 int iRollback;
66424 int turnOnAC;
66425 #endif /* local variables moved into u.at */
66426
66427 u.at.desiredAutoCommit = pOp->p1;
66428 u.at.iRollback = pOp->p2;
66429 u.at.turnOnAC = u.at.desiredAutoCommit && !db->autoCommit;
66430 assert( u.at.desiredAutoCommit==1 || u.at.desiredAutoCommit==0 );
66431 assert( u.at.desiredAutoCommit==1 || u.at.iRollback==0 );
66432 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
66433
66434 #if 0
66435 if( u.at.turnOnAC && u.at.iRollback && db->activeVdbeCnt>1 ){
66436 /* If this instruction implements a ROLLBACK and other VMs are
66437 ** still running, and a transaction is active, return an error indicating
66438 ** that the other VMs must complete first.
66439 */
66440 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
66441 "SQL statements in progress");
66442 rc = SQLITE_BUSY;
66443 }else
66444 #endif
66445 if( u.at.turnOnAC && !u.at.iRollback && db->writeVdbeCnt>0 ){
66446 /* If this instruction implements a COMMIT and other VMs are writing
66447 ** return an error indicating that the other VMs must complete first.
66448 */
66449 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
66450 "SQL statements in progress");
66451 rc = SQLITE_BUSY;
66452 }else if( u.at.desiredAutoCommit!=db->autoCommit ){
66453 if( u.at.iRollback ){
66454 assert( u.at.desiredAutoCommit==1 );
66455 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
66456 db->autoCommit = 1;
66457 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
66458 goto vdbe_return;
66459 }else{
66460 db->autoCommit = (u8)u.at.desiredAutoCommit;
66461 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
66462 p->pc = pc;
66463 db->autoCommit = (u8)(1-u.at.desiredAutoCommit);
66464 p->rc = rc = SQLITE_BUSY;
66465 goto vdbe_return;
66466 }
66467 }
66468 assert( db->nStatement==0 );
@@ -66419,12 +66473,12 @@
66473 rc = SQLITE_ERROR;
66474 }
66475 goto vdbe_return;
66476 }else{
66477 sqlite3SetString(&p->zErrMsg, db,
66478 (!u.at.desiredAutoCommit)?"cannot start a transaction within a transaction":(
66479 (u.at.iRollback)?"cannot rollback - no transaction is active":
66480 "cannot commit - no transaction is active"));
66481
66482 rc = SQLITE_ERROR;
66483 }
66484 break;
@@ -66460,20 +66514,20 @@
66514 ** will automatically commit when the VDBE halts.
66515 **
66516 ** If P2 is zero, then a read-lock is obtained on the database file.
66517 */
66518 case OP_Transaction: {
66519 #if 0 /* local variables moved into u.au */
66520 Btree *pBt;
66521 #endif /* local variables moved into u.au */
66522
66523 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66524 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66525 u.au.pBt = db->aDb[pOp->p1].pBt;
66526
66527 if( u.au.pBt ){
66528 rc = sqlite3BtreeBeginTrans(u.au.pBt, pOp->p2);
66529 if( rc==SQLITE_BUSY ){
66530 p->pc = pc;
66531 p->rc = rc = SQLITE_BUSY;
66532 goto vdbe_return;
66533 }
@@ -66482,20 +66536,20 @@
66536 }
66537
66538 if( pOp->p2 && p->usesStmtJournal
66539 && (db->autoCommit==0 || db->activeVdbeCnt>1)
66540 ){
66541 assert( sqlite3BtreeIsInTrans(u.au.pBt) );
66542 if( p->iStatement==0 ){
66543 assert( db->nStatement>=0 && db->nSavepoint>=0 );
66544 db->nStatement++;
66545 p->iStatement = db->nSavepoint + db->nStatement;
66546 }
66547
66548 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
66549 if( rc==SQLITE_OK ){
66550 rc = sqlite3BtreeBeginStmt(u.au.pBt, p->iStatement);
66551 }
66552
66553 /* Store the current value of the database handles deferred constraint
66554 ** counter. If the statement transaction needs to be rolled back,
66555 ** the value of this counter needs to be restored too. */
@@ -66516,25 +66570,25 @@
66570 ** There must be a read-lock on the database (either a transaction
66571 ** must be started or there must be an open cursor) before
66572 ** executing this instruction.
66573 */
66574 case OP_ReadCookie: { /* out2-prerelease */
66575 #if 0 /* local variables moved into u.av */
66576 int iMeta;
66577 int iDb;
66578 int iCookie;
66579 #endif /* local variables moved into u.av */
66580
66581 u.av.iDb = pOp->p1;
66582 u.av.iCookie = pOp->p3;
66583 assert( pOp->p3<SQLITE_N_BTREE_META );
66584 assert( u.av.iDb>=0 && u.av.iDb<db->nDb );
66585 assert( db->aDb[u.av.iDb].pBt!=0 );
66586 assert( (p->btreeMask & (((yDbMask)1)<<u.av.iDb))!=0 );
66587
66588 sqlite3BtreeGetMeta(db->aDb[u.av.iDb].pBt, u.av.iCookie, (u32 *)&u.av.iMeta);
66589 pOut->u.i = u.av.iMeta;
66590 break;
66591 }
66592
66593 /* Opcode: SetCookie P1 P2 P3 * *
66594 **
@@ -66545,30 +66599,30 @@
66599 ** database file used to store temporary tables.
66600 **
66601 ** A transaction must be started before executing this opcode.
66602 */
66603 case OP_SetCookie: { /* in3 */
66604 #if 0 /* local variables moved into u.aw */
66605 Db *pDb;
66606 #endif /* local variables moved into u.aw */
66607 assert( pOp->p2<SQLITE_N_BTREE_META );
66608 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66609 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66610 u.aw.pDb = &db->aDb[pOp->p1];
66611 assert( u.aw.pDb->pBt!=0 );
66612 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
66613 pIn3 = &aMem[pOp->p3];
66614 sqlite3VdbeMemIntegerify(pIn3);
66615 /* See note about index shifting on OP_ReadCookie */
66616 rc = sqlite3BtreeUpdateMeta(u.aw.pDb->pBt, pOp->p2, (int)pIn3->u.i);
66617 if( pOp->p2==BTREE_SCHEMA_VERSION ){
66618 /* When the schema cookie changes, record the new cookie internally */
66619 u.aw.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
66620 db->flags |= SQLITE_InternChanges;
66621 }else if( pOp->p2==BTREE_FILE_FORMAT ){
66622 /* Record changes in the file format */
66623 u.aw.pDb->pSchema->file_format = (u8)pIn3->u.i;
66624 }
66625 if( pOp->p1==1 ){
66626 /* Invalidate all prepared statements whenever the TEMP database
66627 ** schema is changed. Ticket #1644 */
66628 sqlite3ExpirePreparedStatements(db);
@@ -66594,27 +66648,27 @@
66648 ** Either a transaction needs to have been started or an OP_Open needs
66649 ** to be executed (to establish a read lock) before this opcode is
66650 ** invoked.
66651 */
66652 case OP_VerifyCookie: {
66653 #if 0 /* local variables moved into u.ax */
66654 int iMeta;
66655 int iGen;
66656 Btree *pBt;
66657 #endif /* local variables moved into u.ax */
66658
66659 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66660 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66661 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
66662 u.ax.pBt = db->aDb[pOp->p1].pBt;
66663 if( u.ax.pBt ){
66664 sqlite3BtreeGetMeta(u.ax.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.ax.iMeta);
66665 u.ax.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
66666 }else{
66667 u.ax.iGen = u.ax.iMeta = 0;
66668 }
66669 if( u.ax.iMeta!=pOp->p2 || u.ax.iGen!=pOp->p3 ){
66670 sqlite3DbFree(db, p->zErrMsg);
66671 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
66672 /* If the schema-cookie from the database file matches the cookie
66673 ** stored with the in-memory representation of the schema, do
66674 ** not reload the schema from the database file.
@@ -66626,11 +66680,11 @@
66680 ** discard the database schema, as the user code implementing the
66681 ** v-table would have to be ready for the sqlite3_vtab structure itself
66682 ** to be invalidated whenever sqlite3_step() is called from within
66683 ** a v-table method.
66684 */
66685 if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.ax.iMeta ){
66686 sqlite3ResetOneSchema(db, pOp->p1);
66687 }
66688
66689 p->expired = 1;
66690 rc = SQLITE_SCHEMA;
@@ -66687,91 +66741,91 @@
66741 **
66742 ** See also OpenRead.
66743 */
66744 case OP_OpenRead:
66745 case OP_OpenWrite: {
66746 #if 0 /* local variables moved into u.ay */
66747 int nField;
66748 KeyInfo *pKeyInfo;
66749 int p2;
66750 int iDb;
66751 int wrFlag;
66752 Btree *pX;
66753 VdbeCursor *pCur;
66754 Db *pDb;
66755 #endif /* local variables moved into u.ay */
66756
66757 assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
66758 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
66759
66760 if( p->expired ){
66761 rc = SQLITE_ABORT;
66762 break;
66763 }
66764
66765 u.ay.nField = 0;
66766 u.ay.pKeyInfo = 0;
66767 u.ay.p2 = pOp->p2;
66768 u.ay.iDb = pOp->p3;
66769 assert( u.ay.iDb>=0 && u.ay.iDb<db->nDb );
66770 assert( (p->btreeMask & (((yDbMask)1)<<u.ay.iDb))!=0 );
66771 u.ay.pDb = &db->aDb[u.ay.iDb];
66772 u.ay.pX = u.ay.pDb->pBt;
66773 assert( u.ay.pX!=0 );
66774 if( pOp->opcode==OP_OpenWrite ){
66775 u.ay.wrFlag = 1;
66776 assert( sqlite3SchemaMutexHeld(db, u.ay.iDb, 0) );
66777 if( u.ay.pDb->pSchema->file_format < p->minWriteFileFormat ){
66778 p->minWriteFileFormat = u.ay.pDb->pSchema->file_format;
66779 }
66780 }else{
66781 u.ay.wrFlag = 0;
66782 }
66783 if( pOp->p5 & OPFLAG_P2ISREG ){
66784 assert( u.ay.p2>0 );
66785 assert( u.ay.p2<=p->nMem );
66786 pIn2 = &aMem[u.ay.p2];
66787 assert( memIsValid(pIn2) );
66788 assert( (pIn2->flags & MEM_Int)!=0 );
66789 sqlite3VdbeMemIntegerify(pIn2);
66790 u.ay.p2 = (int)pIn2->u.i;
66791 /* The u.ay.p2 value always comes from a prior OP_CreateTable opcode and
66792 ** that opcode will always set the u.ay.p2 value to 2 or more or else fail.
66793 ** If there were a failure, the prepared statement would have halted
66794 ** before reaching this instruction. */
66795 if( NEVER(u.ay.p2<2) ) {
66796 rc = SQLITE_CORRUPT_BKPT;
66797 goto abort_due_to_error;
66798 }
66799 }
66800 if( pOp->p4type==P4_KEYINFO ){
66801 u.ay.pKeyInfo = pOp->p4.pKeyInfo;
66802 u.ay.pKeyInfo->enc = ENC(p->db);
66803 u.ay.nField = u.ay.pKeyInfo->nField+1;
66804 }else if( pOp->p4type==P4_INT32 ){
66805 u.ay.nField = pOp->p4.i;
66806 }
66807 assert( pOp->p1>=0 );
66808 u.ay.pCur = allocateCursor(p, pOp->p1, u.ay.nField, u.ay.iDb, 1);
66809 if( u.ay.pCur==0 ) goto no_mem;
66810 u.ay.pCur->nullRow = 1;
66811 u.ay.pCur->isOrdered = 1;
66812 rc = sqlite3BtreeCursor(u.ay.pX, u.ay.p2, u.ay.wrFlag, u.ay.pKeyInfo, u.ay.pCur->pCursor);
66813 u.ay.pCur->pKeyInfo = u.ay.pKeyInfo;
66814 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
66815 sqlite3BtreeCursorHints(u.ay.pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
66816
66817 /* Since it performs no memory allocation or IO, the only value that
66818 ** sqlite3BtreeCursor() may return is SQLITE_OK. */
66819 assert( rc==SQLITE_OK );
66820
66821 /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
66822 ** SQLite used to check if the root-page flags were sane at this point
66823 ** and report database corruption if they were not, but this check has
66824 ** since moved into the btree layer. */
66825 u.ay.pCur->isTable = pOp->p4type!=P4_KEYINFO;
66826 u.ay.pCur->isIndex = !u.ay.pCur->isTable;
66827 break;
66828 }
66829
66830 /* Opcode: OpenEphemeral P1 P2 * P4 P5
66831 **
@@ -66803,28 +66857,28 @@
66857 ** by this opcode will be used for automatically created transient
66858 ** indices in joins.
66859 */
66860 case OP_OpenAutoindex:
66861 case OP_OpenEphemeral: {
66862 #if 0 /* local variables moved into u.az */
66863 VdbeCursor *pCx;
66864 #endif /* local variables moved into u.az */
66865 static const int vfsFlags =
66866 SQLITE_OPEN_READWRITE |
66867 SQLITE_OPEN_CREATE |
66868 SQLITE_OPEN_EXCLUSIVE |
66869 SQLITE_OPEN_DELETEONCLOSE |
66870 SQLITE_OPEN_TRANSIENT_DB;
66871
66872 assert( pOp->p1>=0 );
66873 u.az.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66874 if( u.az.pCx==0 ) goto no_mem;
66875 u.az.pCx->nullRow = 1;
66876 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.az.pCx->pBt,
66877 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
66878 if( rc==SQLITE_OK ){
66879 rc = sqlite3BtreeBeginTrans(u.az.pCx->pBt, 1);
66880 }
66881 if( rc==SQLITE_OK ){
66882 /* If a transient index is required, create it by calling
66883 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
66884 ** opening it. If a transient table is required, just use the
@@ -66831,26 +66885,26 @@
66885 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
66886 */
66887 if( pOp->p4.pKeyInfo ){
66888 int pgno;
66889 assert( pOp->p4type==P4_KEYINFO );
66890 rc = sqlite3BtreeCreateTable(u.az.pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
66891 if( rc==SQLITE_OK ){
66892 assert( pgno==MASTER_ROOT+1 );
66893 rc = sqlite3BtreeCursor(u.az.pCx->pBt, pgno, 1,
66894 (KeyInfo*)pOp->p4.z, u.az.pCx->pCursor);
66895 u.az.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66896 u.az.pCx->pKeyInfo->enc = ENC(p->db);
66897 }
66898 u.az.pCx->isTable = 0;
66899 }else{
66900 rc = sqlite3BtreeCursor(u.az.pCx->pBt, MASTER_ROOT, 1, 0, u.az.pCx->pCursor);
66901 u.az.pCx->isTable = 1;
66902 }
66903 }
66904 u.az.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
66905 u.az.pCx->isIndex = !u.az.pCx->isTable;
66906 break;
66907 }
66908
66909 /* Opcode: OpenSorter P1 P2 * P4 *
66910 **
@@ -66857,20 +66911,21 @@
66911 ** This opcode works like OP_OpenEphemeral except that it opens
66912 ** a transient index that is specifically designed to sort large
66913 ** tables using an external merge-sort algorithm.
66914 */
66915 case OP_SorterOpen: {
66916 #if 0 /* local variables moved into u.ba */
66917 VdbeCursor *pCx;
66918 #endif /* local variables moved into u.ba */
66919
66920 #ifndef SQLITE_OMIT_MERGE_SORT
66921 u.ba.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66922 if( u.ba.pCx==0 ) goto no_mem;
66923 u.ba.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66924 u.ba.pCx->pKeyInfo->enc = ENC(p->db);
66925 u.ba.pCx->isSorter = 1;
66926 rc = sqlite3VdbeSorterInit(db, u.ba.pCx);
66927 #else
66928 pOp->opcode = OP_OpenEphemeral;
66929 pc--;
66930 #endif
66931 break;
@@ -66890,21 +66945,21 @@
66945 **
66946 ** P3 is the number of fields in the records that will be stored by
66947 ** the pseudo-table.
66948 */
66949 case OP_OpenPseudo: {
66950 #if 0 /* local variables moved into u.bb */
66951 VdbeCursor *pCx;
66952 #endif /* local variables moved into u.bb */
66953
66954 assert( pOp->p1>=0 );
66955 u.bb.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
66956 if( u.bb.pCx==0 ) goto no_mem;
66957 u.bb.pCx->nullRow = 1;
66958 u.bb.pCx->pseudoTableReg = pOp->p2;
66959 u.bb.pCx->isTable = 1;
66960 u.bb.pCx->isIndex = 0;
66961 break;
66962 }
66963
66964 /* Opcode: Close P1 * * * *
66965 **
@@ -66972,39 +67027,39 @@
67027 */
67028 case OP_SeekLt: /* jump, in3 */
67029 case OP_SeekLe: /* jump, in3 */
67030 case OP_SeekGe: /* jump, in3 */
67031 case OP_SeekGt: { /* jump, in3 */
67032 #if 0 /* local variables moved into u.bc */
67033 int res;
67034 int oc;
67035 VdbeCursor *pC;
67036 UnpackedRecord r;
67037 int nField;
67038 i64 iKey; /* The rowid we are to seek to */
67039 #endif /* local variables moved into u.bc */
67040
67041 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67042 assert( pOp->p2!=0 );
67043 u.bc.pC = p->apCsr[pOp->p1];
67044 assert( u.bc.pC!=0 );
67045 assert( u.bc.pC->pseudoTableReg==0 );
67046 assert( OP_SeekLe == OP_SeekLt+1 );
67047 assert( OP_SeekGe == OP_SeekLt+2 );
67048 assert( OP_SeekGt == OP_SeekLt+3 );
67049 assert( u.bc.pC->isOrdered );
67050 if( ALWAYS(u.bc.pC->pCursor!=0) ){
67051 u.bc.oc = pOp->opcode;
67052 u.bc.pC->nullRow = 0;
67053 if( u.bc.pC->isTable ){
67054 /* The input value in P3 might be of any type: integer, real, string,
67055 ** blob, or NULL. But it needs to be an integer before we can do
67056 ** the seek, so covert it. */
67057 pIn3 = &aMem[pOp->p3];
67058 applyNumericAffinity(pIn3);
67059 u.bc.iKey = sqlite3VdbeIntValue(pIn3);
67060 u.bc.pC->rowidIsValid = 0;
67061
67062 /* If the P3 value could not be converted into an integer without
67063 ** loss of information, then special processing is required... */
67064 if( (pIn3->flags & MEM_Int)==0 ){
67065 if( (pIn3->flags & MEM_Real)==0 ){
@@ -67015,105 +67070,105 @@
67070 }
67071 /* If we reach this point, then the P3 value must be a floating
67072 ** point number. */
67073 assert( (pIn3->flags & MEM_Real)!=0 );
67074
67075 if( u.bc.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.bc.iKey || pIn3->r>0) ){
67076 /* The P3 value is too large in magnitude to be expressed as an
67077 ** integer. */
67078 u.bc.res = 1;
67079 if( pIn3->r<0 ){
67080 if( u.bc.oc>=OP_SeekGe ){ assert( u.bc.oc==OP_SeekGe || u.bc.oc==OP_SeekGt );
67081 rc = sqlite3BtreeFirst(u.bc.pC->pCursor, &u.bc.res);
67082 if( rc!=SQLITE_OK ) goto abort_due_to_error;
67083 }
67084 }else{
67085 if( u.bc.oc<=OP_SeekLe ){ assert( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekLe );
67086 rc = sqlite3BtreeLast(u.bc.pC->pCursor, &u.bc.res);
67087 if( rc!=SQLITE_OK ) goto abort_due_to_error;
67088 }
67089 }
67090 if( u.bc.res ){
67091 pc = pOp->p2 - 1;
67092 }
67093 break;
67094 }else if( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekGe ){
67095 /* Use the ceiling() function to convert real->int */
67096 if( pIn3->r > (double)u.bc.iKey ) u.bc.iKey++;
67097 }else{
67098 /* Use the floor() function to convert real->int */
67099 assert( u.bc.oc==OP_SeekLe || u.bc.oc==OP_SeekGt );
67100 if( pIn3->r < (double)u.bc.iKey ) u.bc.iKey--;
67101 }
67102 }
67103 rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, 0, (u64)u.bc.iKey, 0, &u.bc.res);
67104 if( rc!=SQLITE_OK ){
67105 goto abort_due_to_error;
67106 }
67107 if( u.bc.res==0 ){
67108 u.bc.pC->rowidIsValid = 1;
67109 u.bc.pC->lastRowid = u.bc.iKey;
67110 }
67111 }else{
67112 u.bc.nField = pOp->p4.i;
67113 assert( pOp->p4type==P4_INT32 );
67114 assert( u.bc.nField>0 );
67115 u.bc.r.pKeyInfo = u.bc.pC->pKeyInfo;
67116 u.bc.r.nField = (u16)u.bc.nField;
67117
67118 /* The next line of code computes as follows, only faster:
67119 ** if( u.bc.oc==OP_SeekGt || u.bc.oc==OP_SeekLe ){
67120 ** u.bc.r.flags = UNPACKED_INCRKEY;
67121 ** }else{
67122 ** u.bc.r.flags = 0;
67123 ** }
67124 */
67125 u.bc.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.bc.oc - OP_SeekLt)));
67126 assert( u.bc.oc!=OP_SeekGt || u.bc.r.flags==UNPACKED_INCRKEY );
67127 assert( u.bc.oc!=OP_SeekLe || u.bc.r.flags==UNPACKED_INCRKEY );
67128 assert( u.bc.oc!=OP_SeekGe || u.bc.r.flags==0 );
67129 assert( u.bc.oc!=OP_SeekLt || u.bc.r.flags==0 );
67130
67131 u.bc.r.aMem = &aMem[pOp->p3];
67132 #ifdef SQLITE_DEBUG
67133 { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
67134 #endif
67135 ExpandBlob(u.bc.r.aMem);
67136 rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, &u.bc.r, 0, 0, &u.bc.res);
67137 if( rc!=SQLITE_OK ){
67138 goto abort_due_to_error;
67139 }
67140 u.bc.pC->rowidIsValid = 0;
67141 }
67142 u.bc.pC->deferredMoveto = 0;
67143 u.bc.pC->cacheStatus = CACHE_STALE;
67144 #ifdef SQLITE_TEST
67145 sqlite3_search_count++;
67146 #endif
67147 if( u.bc.oc>=OP_SeekGe ){ assert( u.bc.oc==OP_SeekGe || u.bc.oc==OP_SeekGt );
67148 if( u.bc.res<0 || (u.bc.res==0 && u.bc.oc==OP_SeekGt) ){
67149 rc = sqlite3BtreeNext(u.bc.pC->pCursor, &u.bc.res);
67150 if( rc!=SQLITE_OK ) goto abort_due_to_error;
67151 u.bc.pC->rowidIsValid = 0;
67152 }else{
67153 u.bc.res = 0;
67154 }
67155 }else{
67156 assert( u.bc.oc==OP_SeekLt || u.bc.oc==OP_SeekLe );
67157 if( u.bc.res>0 || (u.bc.res==0 && u.bc.oc==OP_SeekLt) ){
67158 rc = sqlite3BtreePrevious(u.bc.pC->pCursor, &u.bc.res);
67159 if( rc!=SQLITE_OK ) goto abort_due_to_error;
67160 u.bc.pC->rowidIsValid = 0;
67161 }else{
67162 /* u.bc.res might be negative because the table is empty. Check to
67163 ** see if this is the case.
67164 */
67165 u.bc.res = sqlite3BtreeEof(u.bc.pC->pCursor);
67166 }
67167 }
67168 assert( pOp->p2>0 );
67169 if( u.bc.res ){
67170 pc = pOp->p2 - 1;
67171 }
67172 }else{
67173 /* This happens when attempting to open the sqlite3_master table
67174 ** for read access returns SQLITE_EMPTY. In this case always
@@ -67132,24 +67187,24 @@
67187 ** This is actually a deferred seek. Nothing actually happens until
67188 ** the cursor is used to read a record. That way, if no reads
67189 ** occur, no unnecessary I/O happens.
67190 */
67191 case OP_Seek: { /* in2 */
67192 #if 0 /* local variables moved into u.bd */
67193 VdbeCursor *pC;
67194 #endif /* local variables moved into u.bd */
67195
67196 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67197 u.bd.pC = p->apCsr[pOp->p1];
67198 assert( u.bd.pC!=0 );
67199 if( ALWAYS(u.bd.pC->pCursor!=0) ){
67200 assert( u.bd.pC->isTable );
67201 u.bd.pC->nullRow = 0;
67202 pIn2 = &aMem[pOp->p2];
67203 u.bd.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
67204 u.bd.pC->rowidIsValid = 0;
67205 u.bd.pC->deferredMoveto = 1;
67206 }
67207 break;
67208 }
67209
67210
@@ -67177,67 +67232,67 @@
67232 **
67233 ** See also: Found, NotExists, IsUnique
67234 */
67235 case OP_NotFound: /* jump, in3 */
67236 case OP_Found: { /* jump, in3 */
67237 #if 0 /* local variables moved into u.be */
67238 int alreadyExists;
67239 VdbeCursor *pC;
67240 int res;
67241 char *pFree;
67242 UnpackedRecord *pIdxKey;
67243 UnpackedRecord r;
67244 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
67245 #endif /* local variables moved into u.be */
67246
67247 #ifdef SQLITE_TEST
67248 sqlite3_found_count++;
67249 #endif
67250
67251 u.be.alreadyExists = 0;
67252 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67253 assert( pOp->p4type==P4_INT32 );
67254 u.be.pC = p->apCsr[pOp->p1];
67255 assert( u.be.pC!=0 );
67256 pIn3 = &aMem[pOp->p3];
67257 if( ALWAYS(u.be.pC->pCursor!=0) ){
67258
67259 assert( u.be.pC->isTable==0 );
67260 if( pOp->p4.i>0 ){
67261 u.be.r.pKeyInfo = u.be.pC->pKeyInfo;
67262 u.be.r.nField = (u16)pOp->p4.i;
67263 u.be.r.aMem = pIn3;
67264 #ifdef SQLITE_DEBUG
67265 { int i; for(i=0; i<u.be.r.nField; i++) assert( memIsValid(&u.be.r.aMem[i]) ); }
67266 #endif
67267 u.be.r.flags = UNPACKED_PREFIX_MATCH;
67268 u.be.pIdxKey = &u.be.r;
67269 }else{
67270 u.be.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
67271 u.be.pC->pKeyInfo, u.be.aTempRec, sizeof(u.be.aTempRec), &u.be.pFree
67272 );
67273 if( u.be.pIdxKey==0 ) goto no_mem;
67274 assert( pIn3->flags & MEM_Blob );
67275 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
67276 sqlite3VdbeRecordUnpack(u.be.pC->pKeyInfo, pIn3->n, pIn3->z, u.be.pIdxKey);
67277 u.be.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
67278 }
67279 rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, u.be.pIdxKey, 0, 0, &u.be.res);
67280 if( pOp->p4.i==0 ){
67281 sqlite3DbFree(db, u.be.pFree);
67282 }
67283 if( rc!=SQLITE_OK ){
67284 break;
67285 }
67286 u.be.alreadyExists = (u.be.res==0);
67287 u.be.pC->deferredMoveto = 0;
67288 u.be.pC->cacheStatus = CACHE_STALE;
67289 }
67290 if( pOp->opcode==OP_Found ){
67291 if( u.be.alreadyExists ) pc = pOp->p2 - 1;
67292 }else{
67293 if( !u.be.alreadyExists ) pc = pOp->p2 - 1;
67294 }
67295 break;
67296 }
67297
67298 /* Opcode: IsUnique P1 P2 P3 P4 *
@@ -67265,67 +67320,67 @@
67320 ** instruction.
67321 **
67322 ** See also: NotFound, NotExists, Found
67323 */
67324 case OP_IsUnique: { /* jump, in3 */
67325 #if 0 /* local variables moved into u.bf */
67326 u16 ii;
67327 VdbeCursor *pCx;
67328 BtCursor *pCrsr;
67329 u16 nField;
67330 Mem *aMx;
67331 UnpackedRecord r; /* B-Tree index search key */
67332 i64 R; /* Rowid stored in register P3 */
67333 #endif /* local variables moved into u.bf */
67334
67335 pIn3 = &aMem[pOp->p3];
67336 u.bf.aMx = &aMem[pOp->p4.i];
67337 /* Assert that the values of parameters P1 and P4 are in range. */
67338 assert( pOp->p4type==P4_INT32 );
67339 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
67340 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67341
67342 /* Find the index cursor. */
67343 u.bf.pCx = p->apCsr[pOp->p1];
67344 assert( u.bf.pCx->deferredMoveto==0 );
67345 u.bf.pCx->seekResult = 0;
67346 u.bf.pCx->cacheStatus = CACHE_STALE;
67347 u.bf.pCrsr = u.bf.pCx->pCursor;
67348
67349 /* If any of the values are NULL, take the jump. */
67350 u.bf.nField = u.bf.pCx->pKeyInfo->nField;
67351 for(u.bf.ii=0; u.bf.ii<u.bf.nField; u.bf.ii++){
67352 if( u.bf.aMx[u.bf.ii].flags & MEM_Null ){
67353 pc = pOp->p2 - 1;
67354 u.bf.pCrsr = 0;
67355 break;
67356 }
67357 }
67358 assert( (u.bf.aMx[u.bf.nField].flags & MEM_Null)==0 );
67359
67360 if( u.bf.pCrsr!=0 ){
67361 /* Populate the index search key. */
67362 u.bf.r.pKeyInfo = u.bf.pCx->pKeyInfo;
67363 u.bf.r.nField = u.bf.nField + 1;
67364 u.bf.r.flags = UNPACKED_PREFIX_SEARCH;
67365 u.bf.r.aMem = u.bf.aMx;
67366 #ifdef SQLITE_DEBUG
67367 { int i; for(i=0; i<u.bf.r.nField; i++) assert( memIsValid(&u.bf.r.aMem[i]) ); }
67368 #endif
67369
67370 /* Extract the value of u.bf.R from register P3. */
67371 sqlite3VdbeMemIntegerify(pIn3);
67372 u.bf.R = pIn3->u.i;
67373
67374 /* Search the B-Tree index. If no conflicting record is found, jump
67375 ** to P2. Otherwise, copy the rowid of the conflicting record to
67376 ** register P3 and fall through to the next instruction. */
67377 rc = sqlite3BtreeMovetoUnpacked(u.bf.pCrsr, &u.bf.r, 0, 0, &u.bf.pCx->seekResult);
67378 if( (u.bf.r.flags & UNPACKED_PREFIX_SEARCH) || u.bf.r.rowid==u.bf.R ){
67379 pc = pOp->p2 - 1;
67380 }else{
67381 pIn3->u.i = u.bf.r.rowid;
67382 }
67383 }
67384 break;
67385 }
67386
@@ -67342,46 +67397,46 @@
67397 ** P1 is an index.
67398 **
67399 ** See also: Found, NotFound, IsUnique
67400 */
67401 case OP_NotExists: { /* jump, in3 */
67402 #if 0 /* local variables moved into u.bg */
67403 VdbeCursor *pC;
67404 BtCursor *pCrsr;
67405 int res;
67406 u64 iKey;
67407 #endif /* local variables moved into u.bg */
67408
67409 pIn3 = &aMem[pOp->p3];
67410 assert( pIn3->flags & MEM_Int );
67411 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67412 u.bg.pC = p->apCsr[pOp->p1];
67413 assert( u.bg.pC!=0 );
67414 assert( u.bg.pC->isTable );
67415 assert( u.bg.pC->pseudoTableReg==0 );
67416 u.bg.pCrsr = u.bg.pC->pCursor;
67417 if( ALWAYS(u.bg.pCrsr!=0) ){
67418 u.bg.res = 0;
67419 u.bg.iKey = pIn3->u.i;
67420 rc = sqlite3BtreeMovetoUnpacked(u.bg.pCrsr, 0, u.bg.iKey, 0, &u.bg.res);
67421 u.bg.pC->lastRowid = pIn3->u.i;
67422 u.bg.pC->rowidIsValid = u.bg.res==0 ?1:0;
67423 u.bg.pC->nullRow = 0;
67424 u.bg.pC->cacheStatus = CACHE_STALE;
67425 u.bg.pC->deferredMoveto = 0;
67426 if( u.bg.res!=0 ){
67427 pc = pOp->p2 - 1;
67428 assert( u.bg.pC->rowidIsValid==0 );
67429 }
67430 u.bg.pC->seekResult = u.bg.res;
67431 }else{
67432 /* This happens when an attempt to open a read cursor on the
67433 ** sqlite_master table returns SQLITE_EMPTY.
67434 */
67435 pc = pOp->p2 - 1;
67436 assert( u.bg.pC->rowidIsValid==0 );
67437 u.bg.pC->seekResult = 0;
67438 }
67439 break;
67440 }
67441
67442 /* Opcode: Sequence P1 P2 * * *
@@ -67412,25 +67467,25 @@
67467 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
67468 ** generated record number. This P3 mechanism is used to help implement the
67469 ** AUTOINCREMENT feature.
67470 */
67471 case OP_NewRowid: { /* out2-prerelease */
67472 #if 0 /* local variables moved into u.bh */
67473 i64 v; /* The new rowid */
67474 VdbeCursor *pC; /* Cursor of table to get the new rowid */
67475 int res; /* Result of an sqlite3BtreeLast() */
67476 int cnt; /* Counter to limit the number of searches */
67477 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
67478 VdbeFrame *pFrame; /* Root frame of VDBE */
67479 #endif /* local variables moved into u.bh */
67480
67481 u.bh.v = 0;
67482 u.bh.res = 0;
67483 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67484 u.bh.pC = p->apCsr[pOp->p1];
67485 assert( u.bh.pC!=0 );
67486 if( NEVER(u.bh.pC->pCursor==0) ){
67487 /* The zero initialization above is all that is needed */
67488 }else{
67489 /* The next rowid or record number (different terms for the same
67490 ** thing) is obtained in a two-step algorithm.
67491 **
@@ -67442,11 +67497,11 @@
67497 ** The second algorithm is to select a rowid at random and see if
67498 ** it already exists in the table. If it does not exist, we have
67499 ** succeeded. If the random rowid does exist, we select a new one
67500 ** and try again, up to 100 times.
67501 */
67502 assert( u.bh.pC->isTable );
67503
67504 #ifdef SQLITE_32BIT_ROWID
67505 # define MAX_ROWID 0x7fffffff
67506 #else
67507 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
@@ -67454,101 +67509,101 @@
67509 ** to provide the constant while making all compilers happy.
67510 */
67511 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
67512 #endif
67513
67514 if( !u.bh.pC->useRandomRowid ){
67515 u.bh.v = sqlite3BtreeGetCachedRowid(u.bh.pC->pCursor);
67516 if( u.bh.v==0 ){
67517 rc = sqlite3BtreeLast(u.bh.pC->pCursor, &u.bh.res);
67518 if( rc!=SQLITE_OK ){
67519 goto abort_due_to_error;
67520 }
67521 if( u.bh.res ){
67522 u.bh.v = 1; /* IMP: R-61914-48074 */
67523 }else{
67524 assert( sqlite3BtreeCursorIsValid(u.bh.pC->pCursor) );
67525 rc = sqlite3BtreeKeySize(u.bh.pC->pCursor, &u.bh.v);
67526 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
67527 if( u.bh.v>=MAX_ROWID ){
67528 u.bh.pC->useRandomRowid = 1;
67529 }else{
67530 u.bh.v++; /* IMP: R-29538-34987 */
67531 }
67532 }
67533 }
67534
67535 #ifndef SQLITE_OMIT_AUTOINCREMENT
67536 if( pOp->p3 ){
67537 /* Assert that P3 is a valid memory cell. */
67538 assert( pOp->p3>0 );
67539 if( p->pFrame ){
67540 for(u.bh.pFrame=p->pFrame; u.bh.pFrame->pParent; u.bh.pFrame=u.bh.pFrame->pParent);
67541 /* Assert that P3 is a valid memory cell. */
67542 assert( pOp->p3<=u.bh.pFrame->nMem );
67543 u.bh.pMem = &u.bh.pFrame->aMem[pOp->p3];
67544 }else{
67545 /* Assert that P3 is a valid memory cell. */
67546 assert( pOp->p3<=p->nMem );
67547 u.bh.pMem = &aMem[pOp->p3];
67548 memAboutToChange(p, u.bh.pMem);
67549 }
67550 assert( memIsValid(u.bh.pMem) );
67551
67552 REGISTER_TRACE(pOp->p3, u.bh.pMem);
67553 sqlite3VdbeMemIntegerify(u.bh.pMem);
67554 assert( (u.bh.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
67555 if( u.bh.pMem->u.i==MAX_ROWID || u.bh.pC->useRandomRowid ){
67556 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
67557 goto abort_due_to_error;
67558 }
67559 if( u.bh.v<u.bh.pMem->u.i+1 ){
67560 u.bh.v = u.bh.pMem->u.i + 1;
67561 }
67562 u.bh.pMem->u.i = u.bh.v;
67563 }
67564 #endif
67565
67566 sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, u.bh.v<MAX_ROWID ? u.bh.v+1 : 0);
67567 }
67568 if( u.bh.pC->useRandomRowid ){
67569 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
67570 ** largest possible integer (9223372036854775807) then the database
67571 ** engine starts picking positive candidate ROWIDs at random until
67572 ** it finds one that is not previously used. */
67573 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
67574 ** an AUTOINCREMENT table. */
67575 /* on the first attempt, simply do one more than previous */
67576 u.bh.v = lastRowid;
67577 u.bh.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67578 u.bh.v++; /* ensure non-zero */
67579 u.bh.cnt = 0;
67580 while( ((rc = sqlite3BtreeMovetoUnpacked(u.bh.pC->pCursor, 0, (u64)u.bh.v,
67581 0, &u.bh.res))==SQLITE_OK)
67582 && (u.bh.res==0)
67583 && (++u.bh.cnt<100)){
67584 /* collision - try another random rowid */
67585 sqlite3_randomness(sizeof(u.bh.v), &u.bh.v);
67586 if( u.bh.cnt<5 ){
67587 /* try "small" random rowids for the initial attempts */
67588 u.bh.v &= 0xffffff;
67589 }else{
67590 u.bh.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67591 }
67592 u.bh.v++; /* ensure non-zero */
67593 }
67594 if( rc==SQLITE_OK && u.bh.res==0 ){
67595 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
67596 goto abort_due_to_error;
67597 }
67598 assert( u.bh.v>0 ); /* EV: R-40812-03570 */
67599 }
67600 u.bh.pC->rowidIsValid = 0;
67601 u.bh.pC->deferredMoveto = 0;
67602 u.bh.pC->cacheStatus = CACHE_STALE;
67603 }
67604 pOut->u.i = u.bh.v;
67605 break;
67606 }
67607
67608 /* Opcode: Insert P1 P2 P3 P4 P5
67609 **
@@ -67594,74 +67649,74 @@
67649 ** This works exactly like OP_Insert except that the key is the
67650 ** integer value P3, not the value of the integer stored in register P3.
67651 */
67652 case OP_Insert:
67653 case OP_InsertInt: {
67654 #if 0 /* local variables moved into u.bi */
67655 Mem *pData; /* MEM cell holding data for the record to be inserted */
67656 Mem *pKey; /* MEM cell holding key for the record */
67657 i64 iKey; /* The integer ROWID or key for the record to be inserted */
67658 VdbeCursor *pC; /* Cursor to table into which insert is written */
67659 int nZero; /* Number of zero-bytes to append */
67660 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
67661 const char *zDb; /* database name - used by the update hook */
67662 const char *zTbl; /* Table name - used by the opdate hook */
67663 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
67664 #endif /* local variables moved into u.bi */
67665
67666 u.bi.pData = &aMem[pOp->p2];
67667 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67668 assert( memIsValid(u.bi.pData) );
67669 u.bi.pC = p->apCsr[pOp->p1];
67670 assert( u.bi.pC!=0 );
67671 assert( u.bi.pC->pCursor!=0 );
67672 assert( u.bi.pC->pseudoTableReg==0 );
67673 assert( u.bi.pC->isTable );
67674 REGISTER_TRACE(pOp->p2, u.bi.pData);
67675
67676 if( pOp->opcode==OP_Insert ){
67677 u.bi.pKey = &aMem[pOp->p3];
67678 assert( u.bi.pKey->flags & MEM_Int );
67679 assert( memIsValid(u.bi.pKey) );
67680 REGISTER_TRACE(pOp->p3, u.bi.pKey);
67681 u.bi.iKey = u.bi.pKey->u.i;
67682 }else{
67683 assert( pOp->opcode==OP_InsertInt );
67684 u.bi.iKey = pOp->p3;
67685 }
67686
67687 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
67688 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bi.iKey;
67689 if( u.bi.pData->flags & MEM_Null ){
67690 u.bi.pData->z = 0;
67691 u.bi.pData->n = 0;
67692 }else{
67693 assert( u.bi.pData->flags & (MEM_Blob|MEM_Str) );
67694 }
67695 u.bi.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bi.pC->seekResult : 0);
67696 if( u.bi.pData->flags & MEM_Zero ){
67697 u.bi.nZero = u.bi.pData->u.nZero;
67698 }else{
67699 u.bi.nZero = 0;
67700 }
67701 sqlite3BtreeSetCachedRowid(u.bi.pC->pCursor, 0);
67702 rc = sqlite3BtreeInsert(u.bi.pC->pCursor, 0, u.bi.iKey,
67703 u.bi.pData->z, u.bi.pData->n, u.bi.nZero,
67704 pOp->p5 & OPFLAG_APPEND, u.bi.seekResult
67705 );
67706 u.bi.pC->rowidIsValid = 0;
67707 u.bi.pC->deferredMoveto = 0;
67708 u.bi.pC->cacheStatus = CACHE_STALE;
67709
67710 /* Invoke the update-hook if required. */
67711 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67712 u.bi.zDb = db->aDb[u.bi.pC->iDb].zName;
67713 u.bi.zTbl = pOp->p4.z;
67714 u.bi.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
67715 assert( u.bi.pC->isTable );
67716 db->xUpdateCallback(db->pUpdateArg, u.bi.op, u.bi.zDb, u.bi.zTbl, u.bi.iKey);
67717 assert( u.bi.pC->iDb>=0 );
67718 }
67719 break;
67720 }
67721
67722 /* Opcode: Delete P1 P2 * P4 *
@@ -67683,51 +67738,51 @@
67738 ** pointing to. The update hook will be invoked, if it exists.
67739 ** If P4 is not NULL then the P1 cursor must have been positioned
67740 ** using OP_NotFound prior to invoking this opcode.
67741 */
67742 case OP_Delete: {
67743 #if 0 /* local variables moved into u.bj */
67744 i64 iKey;
67745 VdbeCursor *pC;
67746 #endif /* local variables moved into u.bj */
67747
67748 u.bj.iKey = 0;
67749 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67750 u.bj.pC = p->apCsr[pOp->p1];
67751 assert( u.bj.pC!=0 );
67752 assert( u.bj.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
67753
67754 /* If the update-hook will be invoked, set u.bj.iKey to the rowid of the
67755 ** row being deleted.
67756 */
67757 if( db->xUpdateCallback && pOp->p4.z ){
67758 assert( u.bj.pC->isTable );
67759 assert( u.bj.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
67760 u.bj.iKey = u.bj.pC->lastRowid;
67761 }
67762
67763 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
67764 ** OP_Column on the same table without any intervening operations that
67765 ** might move or invalidate the cursor. Hence cursor u.bj.pC is always pointing
67766 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
67767 ** below is always a no-op and cannot fail. We will run it anyhow, though,
67768 ** to guard against future changes to the code generator.
67769 **/
67770 assert( u.bj.pC->deferredMoveto==0 );
67771 rc = sqlite3VdbeCursorMoveto(u.bj.pC);
67772 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67773
67774 sqlite3BtreeSetCachedRowid(u.bj.pC->pCursor, 0);
67775 rc = sqlite3BtreeDelete(u.bj.pC->pCursor);
67776 u.bj.pC->cacheStatus = CACHE_STALE;
67777
67778 /* Invoke the update-hook if required. */
67779 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67780 const char *zDb = db->aDb[u.bj.pC->iDb].zName;
67781 const char *zTbl = pOp->p4.z;
67782 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bj.iKey);
67783 assert( u.bj.pC->iDb>=0 );
67784 }
67785 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
67786 break;
67787 }
67788 /* Opcode: ResetCount * * * * *
@@ -67749,20 +67804,20 @@
67804 ** register P3 with the entry that the sorter cursor currently points to.
67805 ** If, excluding the rowid fields at the end, the two records are a match,
67806 ** fall through to the next instruction. Otherwise, jump to instruction P2.
67807 */
67808 case OP_SorterCompare: {
67809 #if 0 /* local variables moved into u.bk */
67810 VdbeCursor *pC;
67811 int res;
67812 #endif /* local variables moved into u.bk */
67813
67814 u.bk.pC = p->apCsr[pOp->p1];
67815 assert( isSorter(u.bk.pC) );
67816 pIn3 = &aMem[pOp->p3];
67817 rc = sqlite3VdbeSorterCompare(u.bk.pC, pIn3, &u.bk.res);
67818 if( u.bk.res ){
67819 pc = pOp->p2-1;
67820 }
67821 break;
67822 };
67823
@@ -67769,18 +67824,19 @@
67824 /* Opcode: SorterData P1 P2 * * *
67825 **
67826 ** Write into register P2 the current sorter data for sorter cursor P1.
67827 */
67828 case OP_SorterData: {
67829 #if 0 /* local variables moved into u.bl */
67830 VdbeCursor *pC;
67831 #endif /* local variables moved into u.bl */
67832
67833 #ifndef SQLITE_OMIT_MERGE_SORT
67834 pOut = &aMem[pOp->p2];
67835 u.bl.pC = p->apCsr[pOp->p1];
67836 assert( u.bl.pC->isSorter );
67837 rc = sqlite3VdbeSorterRowkey(u.bl.pC, pOut);
67838 #else
67839 pOp->opcode = OP_RowKey;
67840 pc--;
67841 #endif
67842 break;
@@ -67806,66 +67862,66 @@
67862 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
67863 ** of a real table, not a pseudo-table.
67864 */
67865 case OP_RowKey:
67866 case OP_RowData: {
67867 #if 0 /* local variables moved into u.bm */
67868 VdbeCursor *pC;
67869 BtCursor *pCrsr;
67870 u32 n;
67871 i64 n64;
67872 #endif /* local variables moved into u.bm */
67873
67874 pOut = &aMem[pOp->p2];
67875 memAboutToChange(p, pOut);
67876
67877 /* Note that RowKey and RowData are really exactly the same instruction */
67878 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67879 u.bm.pC = p->apCsr[pOp->p1];
67880 assert( u.bm.pC->isSorter==0 );
67881 assert( u.bm.pC->isTable || pOp->opcode!=OP_RowData );
67882 assert( u.bm.pC->isIndex || pOp->opcode==OP_RowData );
67883 assert( u.bm.pC!=0 );
67884 assert( u.bm.pC->nullRow==0 );
67885 assert( u.bm.pC->pseudoTableReg==0 );
67886 assert( u.bm.pC->pCursor!=0 );
67887 u.bm.pCrsr = u.bm.pC->pCursor;
67888 assert( sqlite3BtreeCursorIsValid(u.bm.pCrsr) );
67889
67890 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
67891 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
67892 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
67893 ** a no-op and can never fail. But we leave it in place as a safety.
67894 */
67895 assert( u.bm.pC->deferredMoveto==0 );
67896 rc = sqlite3VdbeCursorMoveto(u.bm.pC);
67897 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67898
67899 if( u.bm.pC->isIndex ){
67900 assert( !u.bm.pC->isTable );
67901 VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bm.pCrsr, &u.bm.n64);
67902 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
67903 if( u.bm.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67904 goto too_big;
67905 }
67906 u.bm.n = (u32)u.bm.n64;
67907 }else{
67908 VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bm.pCrsr, &u.bm.n);
67909 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
67910 if( u.bm.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
67911 goto too_big;
67912 }
67913 }
67914 if( sqlite3VdbeMemGrow(pOut, u.bm.n, 0) ){
67915 goto no_mem;
67916 }
67917 pOut->n = u.bm.n;
67918 MemSetTypeFlag(pOut, MEM_Blob);
67919 if( u.bm.pC->isIndex ){
67920 rc = sqlite3BtreeKey(u.bm.pCrsr, 0, u.bm.n, pOut->z);
67921 }else{
67922 rc = sqlite3BtreeData(u.bm.pCrsr, 0, u.bm.n, pOut->z);
67923 }
67924 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
67925 UPDATE_MAX_BLOBSIZE(pOut);
67926 break;
67927 }
@@ -67878,46 +67934,46 @@
67934 ** P1 can be either an ordinary table or a virtual table. There used to
67935 ** be a separate OP_VRowid opcode for use with virtual tables, but this
67936 ** one opcode now works for both table types.
67937 */
67938 case OP_Rowid: { /* out2-prerelease */
67939 #if 0 /* local variables moved into u.bn */
67940 VdbeCursor *pC;
67941 i64 v;
67942 sqlite3_vtab *pVtab;
67943 const sqlite3_module *pModule;
67944 #endif /* local variables moved into u.bn */
67945
67946 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67947 u.bn.pC = p->apCsr[pOp->p1];
67948 assert( u.bn.pC!=0 );
67949 assert( u.bn.pC->pseudoTableReg==0 );
67950 if( u.bn.pC->nullRow ){
67951 pOut->flags = MEM_Null;
67952 break;
67953 }else if( u.bn.pC->deferredMoveto ){
67954 u.bn.v = u.bn.pC->movetoTarget;
67955 #ifndef SQLITE_OMIT_VIRTUALTABLE
67956 }else if( u.bn.pC->pVtabCursor ){
67957 u.bn.pVtab = u.bn.pC->pVtabCursor->pVtab;
67958 u.bn.pModule = u.bn.pVtab->pModule;
67959 assert( u.bn.pModule->xRowid );
67960 rc = u.bn.pModule->xRowid(u.bn.pC->pVtabCursor, &u.bn.v);
67961 importVtabErrMsg(p, u.bn.pVtab);
67962 #endif /* SQLITE_OMIT_VIRTUALTABLE */
67963 }else{
67964 assert( u.bn.pC->pCursor!=0 );
67965 rc = sqlite3VdbeCursorMoveto(u.bn.pC);
67966 if( rc ) goto abort_due_to_error;
67967 if( u.bn.pC->rowidIsValid ){
67968 u.bn.v = u.bn.pC->lastRowid;
67969 }else{
67970 rc = sqlite3BtreeKeySize(u.bn.pC->pCursor, &u.bn.v);
67971 assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
67972 }
67973 }
67974 pOut->u.i = u.bn.v;
67975 break;
67976 }
67977
67978 /* Opcode: NullRow P1 * * * *
67979 **
@@ -67924,22 +67980,22 @@
67980 ** Move the cursor P1 to a null row. Any OP_Column operations
67981 ** that occur while the cursor is on the null row will always
67982 ** write a NULL.
67983 */
67984 case OP_NullRow: {
67985 #if 0 /* local variables moved into u.bo */
67986 VdbeCursor *pC;
67987 #endif /* local variables moved into u.bo */
67988
67989 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67990 u.bo.pC = p->apCsr[pOp->p1];
67991 assert( u.bo.pC!=0 );
67992 u.bo.pC->nullRow = 1;
67993 u.bo.pC->rowidIsValid = 0;
67994 assert( u.bo.pC->pCursor || u.bo.pC->pVtabCursor );
67995 if( u.bo.pC->pCursor ){
67996 sqlite3BtreeClearCursor(u.bo.pC->pCursor);
67997 }
67998 break;
67999 }
68000
68001 /* Opcode: Last P1 P2 * * *
@@ -67949,29 +68005,29 @@
68005 ** If the table or index is empty and P2>0, then jump immediately to P2.
68006 ** If P2 is 0 or if the table or index is not empty, fall through
68007 ** to the following instruction.
68008 */
68009 case OP_Last: { /* jump */
68010 #if 0 /* local variables moved into u.bp */
68011 VdbeCursor *pC;
68012 BtCursor *pCrsr;
68013 int res;
68014 #endif /* local variables moved into u.bp */
68015
68016 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68017 u.bp.pC = p->apCsr[pOp->p1];
68018 assert( u.bp.pC!=0 );
68019 u.bp.pCrsr = u.bp.pC->pCursor;
68020 u.bp.res = 0;
68021 if( ALWAYS(u.bp.pCrsr!=0) ){
68022 rc = sqlite3BtreeLast(u.bp.pCrsr, &u.bp.res);
68023 }
68024 u.bp.pC->nullRow = (u8)u.bp.res;
68025 u.bp.pC->deferredMoveto = 0;
68026 u.bp.pC->rowidIsValid = 0;
68027 u.bp.pC->cacheStatus = CACHE_STALE;
68028 if( pOp->p2>0 && u.bp.res ){
68029 pc = pOp->p2 - 1;
68030 }
68031 break;
68032 }
68033
@@ -68007,35 +68063,35 @@
68063 ** If the table or index is empty and P2>0, then jump immediately to P2.
68064 ** If P2 is 0 or if the table or index is not empty, fall through
68065 ** to the following instruction.
68066 */
68067 case OP_Rewind: { /* jump */
68068 #if 0 /* local variables moved into u.bq */
68069 VdbeCursor *pC;
68070 BtCursor *pCrsr;
68071 int res;
68072 #endif /* local variables moved into u.bq */
68073
68074 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68075 u.bq.pC = p->apCsr[pOp->p1];
68076 assert( u.bq.pC!=0 );
68077 assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterSort) );
68078 u.bq.res = 1;
68079 if( isSorter(u.bq.pC) ){
68080 rc = sqlite3VdbeSorterRewind(db, u.bq.pC, &u.bq.res);
68081 }else{
68082 u.bq.pCrsr = u.bq.pC->pCursor;
68083 assert( u.bq.pCrsr );
68084 rc = sqlite3BtreeFirst(u.bq.pCrsr, &u.bq.res);
68085 u.bq.pC->atFirst = u.bq.res==0 ?1:0;
68086 u.bq.pC->deferredMoveto = 0;
68087 u.bq.pC->cacheStatus = CACHE_STALE;
68088 u.bq.pC->rowidIsValid = 0;
68089 }
68090 u.bq.pC->nullRow = (u8)u.bq.res;
68091 assert( pOp->p2>0 && pOp->p2<p->nOp );
68092 if( u.bq.res ){
68093 pc = pOp->p2 - 1;
68094 }
68095 break;
68096 }
68097
@@ -68075,44 +68131,44 @@
68131 #ifdef SQLITE_OMIT_MERGE_SORT
68132 pOp->opcode = OP_Next;
68133 #endif
68134 case OP_Prev: /* jump */
68135 case OP_Next: { /* jump */
68136 #if 0 /* local variables moved into u.br */
68137 VdbeCursor *pC;
68138 int res;
68139 #endif /* local variables moved into u.br */
68140
68141 CHECK_FOR_INTERRUPT;
68142 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68143 assert( pOp->p5<=ArraySize(p->aCounter) );
68144 u.br.pC = p->apCsr[pOp->p1];
68145 if( u.br.pC==0 ){
68146 break; /* See ticket #2273 */
68147 }
68148 assert( u.br.pC->isSorter==(pOp->opcode==OP_SorterNext) );
68149 if( isSorter(u.br.pC) ){
68150 assert( pOp->opcode==OP_SorterNext );
68151 rc = sqlite3VdbeSorterNext(db, u.br.pC, &u.br.res);
68152 }else{
68153 u.br.res = 1;
68154 assert( u.br.pC->deferredMoveto==0 );
68155 assert( u.br.pC->pCursor );
68156 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
68157 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
68158 rc = pOp->p4.xAdvance(u.br.pC->pCursor, &u.br.res);
68159 }
68160 u.br.pC->nullRow = (u8)u.br.res;
68161 u.br.pC->cacheStatus = CACHE_STALE;
68162 if( u.br.res==0 ){
68163 pc = pOp->p2 - 1;
68164 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
68165 #ifdef SQLITE_TEST
68166 sqlite3_search_count++;
68167 #endif
68168 }
68169 u.br.pC->rowidIsValid = 0;
68170 break;
68171 }
68172
68173 /* Opcode: IdxInsert P1 P2 P3 * P5
68174 **
@@ -68129,38 +68185,38 @@
68185 case OP_SorterInsert: /* in2 */
68186 #ifdef SQLITE_OMIT_MERGE_SORT
68187 pOp->opcode = OP_IdxInsert;
68188 #endif
68189 case OP_IdxInsert: { /* in2 */
68190 #if 0 /* local variables moved into u.bs */
68191 VdbeCursor *pC;
68192 BtCursor *pCrsr;
68193 int nKey;
68194 const char *zKey;
68195 #endif /* local variables moved into u.bs */
68196
68197 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68198 u.bs.pC = p->apCsr[pOp->p1];
68199 assert( u.bs.pC!=0 );
68200 assert( u.bs.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
68201 pIn2 = &aMem[pOp->p2];
68202 assert( pIn2->flags & MEM_Blob );
68203 u.bs.pCrsr = u.bs.pC->pCursor;
68204 if( ALWAYS(u.bs.pCrsr!=0) ){
68205 assert( u.bs.pC->isTable==0 );
68206 rc = ExpandBlob(pIn2);
68207 if( rc==SQLITE_OK ){
68208 if( isSorter(u.bs.pC) ){
68209 rc = sqlite3VdbeSorterWrite(db, u.bs.pC, pIn2);
68210 }else{
68211 u.bs.nKey = pIn2->n;
68212 u.bs.zKey = pIn2->z;
68213 rc = sqlite3BtreeInsert(u.bs.pCrsr, u.bs.zKey, u.bs.nKey, "", 0, 0, pOp->p3,
68214 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bs.pC->seekResult : 0)
68215 );
68216 assert( u.bs.pC->deferredMoveto==0 );
68217 u.bs.pC->cacheStatus = CACHE_STALE;
68218 }
68219 }
68220 }
68221 break;
68222 }
@@ -68170,37 +68226,37 @@
68226 ** The content of P3 registers starting at register P2 form
68227 ** an unpacked index key. This opcode removes that entry from the
68228 ** index opened by cursor P1.
68229 */
68230 case OP_IdxDelete: {
68231 #if 0 /* local variables moved into u.bt */
68232 VdbeCursor *pC;
68233 BtCursor *pCrsr;
68234 int res;
68235 UnpackedRecord r;
68236 #endif /* local variables moved into u.bt */
68237
68238 assert( pOp->p3>0 );
68239 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
68240 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68241 u.bt.pC = p->apCsr[pOp->p1];
68242 assert( u.bt.pC!=0 );
68243 u.bt.pCrsr = u.bt.pC->pCursor;
68244 if( ALWAYS(u.bt.pCrsr!=0) ){
68245 u.bt.r.pKeyInfo = u.bt.pC->pKeyInfo;
68246 u.bt.r.nField = (u16)pOp->p3;
68247 u.bt.r.flags = 0;
68248 u.bt.r.aMem = &aMem[pOp->p2];
68249 #ifdef SQLITE_DEBUG
68250 { int i; for(i=0; i<u.bt.r.nField; i++) assert( memIsValid(&u.bt.r.aMem[i]) ); }
68251 #endif
68252 rc = sqlite3BtreeMovetoUnpacked(u.bt.pCrsr, &u.bt.r, 0, 0, &u.bt.res);
68253 if( rc==SQLITE_OK && u.bt.res==0 ){
68254 rc = sqlite3BtreeDelete(u.bt.pCrsr);
68255 }
68256 assert( u.bt.pC->deferredMoveto==0 );
68257 u.bt.pC->cacheStatus = CACHE_STALE;
68258 }
68259 break;
68260 }
68261
68262 /* Opcode: IdxRowid P1 P2 * * *
@@ -68210,32 +68266,32 @@
68266 ** the rowid of the table entry to which this index entry points.
68267 **
68268 ** See also: Rowid, MakeRecord.
68269 */
68270 case OP_IdxRowid: { /* out2-prerelease */
68271 #if 0 /* local variables moved into u.bu */
68272 BtCursor *pCrsr;
68273 VdbeCursor *pC;
68274 i64 rowid;
68275 #endif /* local variables moved into u.bu */
68276
68277 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68278 u.bu.pC = p->apCsr[pOp->p1];
68279 assert( u.bu.pC!=0 );
68280 u.bu.pCrsr = u.bu.pC->pCursor;
68281 pOut->flags = MEM_Null;
68282 if( ALWAYS(u.bu.pCrsr!=0) ){
68283 rc = sqlite3VdbeCursorMoveto(u.bu.pC);
68284 if( NEVER(rc) ) goto abort_due_to_error;
68285 assert( u.bu.pC->deferredMoveto==0 );
68286 assert( u.bu.pC->isTable==0 );
68287 if( !u.bu.pC->nullRow ){
68288 rc = sqlite3VdbeIdxRowid(db, u.bu.pCrsr, &u.bu.rowid);
68289 if( rc!=SQLITE_OK ){
68290 goto abort_due_to_error;
68291 }
68292 pOut->u.i = u.bu.rowid;
68293 pOut->flags = MEM_Int;
68294 }
68295 }
68296 break;
68297 }
@@ -68266,43 +68322,43 @@
68322 ** If P5 is non-zero then the key value is increased by an epsilon prior
68323 ** to the comparison. This makes the opcode work like IdxLE.
68324 */
68325 case OP_IdxLT: /* jump */
68326 case OP_IdxGE: { /* jump */
68327 #if 0 /* local variables moved into u.bv */
68328 VdbeCursor *pC;
68329 int res;
68330 UnpackedRecord r;
68331 #endif /* local variables moved into u.bv */
68332
68333 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68334 u.bv.pC = p->apCsr[pOp->p1];
68335 assert( u.bv.pC!=0 );
68336 assert( u.bv.pC->isOrdered );
68337 if( ALWAYS(u.bv.pC->pCursor!=0) ){
68338 assert( u.bv.pC->deferredMoveto==0 );
68339 assert( pOp->p5==0 || pOp->p5==1 );
68340 assert( pOp->p4type==P4_INT32 );
68341 u.bv.r.pKeyInfo = u.bv.pC->pKeyInfo;
68342 u.bv.r.nField = (u16)pOp->p4.i;
68343 if( pOp->p5 ){
68344 u.bv.r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
68345 }else{
68346 u.bv.r.flags = UNPACKED_PREFIX_MATCH;
68347 }
68348 u.bv.r.aMem = &aMem[pOp->p3];
68349 #ifdef SQLITE_DEBUG
68350 { int i; for(i=0; i<u.bv.r.nField; i++) assert( memIsValid(&u.bv.r.aMem[i]) ); }
68351 #endif
68352 rc = sqlite3VdbeIdxKeyCompare(u.bv.pC, &u.bv.r, &u.bv.res);
68353 if( pOp->opcode==OP_IdxLT ){
68354 u.bv.res = -u.bv.res;
68355 }else{
68356 assert( pOp->opcode==OP_IdxGE );
68357 u.bv.res++;
68358 }
68359 if( u.bv.res>0 ){
68360 pc = pOp->p2 - 1 ;
68361 }
68362 }
68363 break;
68364 }
@@ -68326,43 +68382,44 @@
68382 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
68383 **
68384 ** See also: Clear
68385 */
68386 case OP_Destroy: { /* out2-prerelease */
68387 #if 0 /* local variables moved into u.bw */
68388 int iMoved;
68389 int iCnt;
68390 Vdbe *pVdbe;
68391 int iDb;
68392 #endif /* local variables moved into u.bw */
68393
68394 #ifndef SQLITE_OMIT_VIRTUALTABLE
68395 u.bw.iCnt = 0;
68396 for(u.bw.pVdbe=db->pVdbe; u.bw.pVdbe; u.bw.pVdbe = u.bw.pVdbe->pNext){
68397 if( u.bw.pVdbe->magic==VDBE_MAGIC_RUN && u.bw.pVdbe->inVtabMethod<2 && u.bw.pVdbe->pc>=0 ){
68398 u.bw.iCnt++;
68399 }
68400 }
68401 #else
68402 u.bw.iCnt = db->activeVdbeCnt;
68403 #endif
68404 pOut->flags = MEM_Null;
68405 if( u.bw.iCnt>1 ){
68406 rc = SQLITE_LOCKED;
68407 p->errorAction = OE_Abort;
68408 }else{
68409 u.bw.iDb = pOp->p3;
68410 assert( u.bw.iCnt==1 );
68411 assert( (p->btreeMask & (((yDbMask)1)<<u.bw.iDb))!=0 );
68412 rc = sqlite3BtreeDropTable(db->aDb[u.bw.iDb].pBt, pOp->p1, &u.bw.iMoved);
68413 pOut->flags = MEM_Int;
68414 pOut->u.i = u.bw.iMoved;
68415 #ifndef SQLITE_OMIT_AUTOVACUUM
68416 if( rc==SQLITE_OK && u.bw.iMoved!=0 ){
68417 sqlite3RootPageMoved(db, u.bw.iDb, u.bw.iMoved, pOp->p1);
68418 /* All OP_Destroy operations occur on the same btree */
68419 assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bw.iDb+1 );
68420 resetSchemaOnFault = u.bw.iDb+1;
68421 }
68422 #endif
68423 }
68424 break;
68425 }
@@ -68384,25 +68441,25 @@
68441 ** also incremented by the number of rows in the table being cleared.
68442 **
68443 ** See also: Destroy
68444 */
68445 case OP_Clear: {
68446 #if 0 /* local variables moved into u.bx */
68447 int nChange;
68448 #endif /* local variables moved into u.bx */
68449
68450 u.bx.nChange = 0;
68451 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
68452 rc = sqlite3BtreeClearTable(
68453 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bx.nChange : 0)
68454 );
68455 if( pOp->p3 ){
68456 p->nChange += u.bx.nChange;
68457 if( pOp->p3>0 ){
68458 assert( memIsValid(&aMem[pOp->p3]) );
68459 memAboutToChange(p, &aMem[pOp->p3]);
68460 aMem[pOp->p3].u.i += u.bx.nChange;
68461 }
68462 }
68463 break;
68464 }
68465
@@ -68428,29 +68485,29 @@
68485 **
68486 ** See documentation on OP_CreateTable for additional information.
68487 */
68488 case OP_CreateIndex: /* out2-prerelease */
68489 case OP_CreateTable: { /* out2-prerelease */
68490 #if 0 /* local variables moved into u.by */
68491 int pgno;
68492 int flags;
68493 Db *pDb;
68494 #endif /* local variables moved into u.by */
68495
68496 u.by.pgno = 0;
68497 assert( pOp->p1>=0 && pOp->p1<db->nDb );
68498 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
68499 u.by.pDb = &db->aDb[pOp->p1];
68500 assert( u.by.pDb->pBt!=0 );
68501 if( pOp->opcode==OP_CreateTable ){
68502 /* u.by.flags = BTREE_INTKEY; */
68503 u.by.flags = BTREE_INTKEY;
68504 }else{
68505 u.by.flags = BTREE_BLOBKEY;
68506 }
68507 rc = sqlite3BtreeCreateTable(u.by.pDb->pBt, &u.by.pgno, u.by.flags);
68508 pOut->u.i = u.by.pgno;
68509 break;
68510 }
68511
68512 /* Opcode: ParseSchema P1 * * P4 *
68513 **
@@ -68459,48 +68516,48 @@
68516 **
68517 ** This opcode invokes the parser to create a new virtual machine,
68518 ** then runs the new virtual machine. It is thus a re-entrant opcode.
68519 */
68520 case OP_ParseSchema: {
68521 #if 0 /* local variables moved into u.bz */
68522 int iDb;
68523 const char *zMaster;
68524 char *zSql;
68525 InitData initData;
68526 #endif /* local variables moved into u.bz */
68527
68528 /* Any prepared statement that invokes this opcode will hold mutexes
68529 ** on every btree. This is a prerequisite for invoking
68530 ** sqlite3InitCallback().
68531 */
68532 #ifdef SQLITE_DEBUG
68533 for(u.bz.iDb=0; u.bz.iDb<db->nDb; u.bz.iDb++){
68534 assert( u.bz.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bz.iDb].pBt) );
68535 }
68536 #endif
68537
68538 u.bz.iDb = pOp->p1;
68539 assert( u.bz.iDb>=0 && u.bz.iDb<db->nDb );
68540 assert( DbHasProperty(db, u.bz.iDb, DB_SchemaLoaded) );
68541 /* Used to be a conditional */ {
68542 u.bz.zMaster = SCHEMA_TABLE(u.bz.iDb);
68543 u.bz.initData.db = db;
68544 u.bz.initData.iDb = pOp->p1;
68545 u.bz.initData.pzErrMsg = &p->zErrMsg;
68546 u.bz.zSql = sqlite3MPrintf(db,
68547 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
68548 db->aDb[u.bz.iDb].zName, u.bz.zMaster, pOp->p4.z);
68549 if( u.bz.zSql==0 ){
68550 rc = SQLITE_NOMEM;
68551 }else{
68552 assert( db->init.busy==0 );
68553 db->init.busy = 1;
68554 u.bz.initData.rc = SQLITE_OK;
68555 assert( !db->mallocFailed );
68556 rc = sqlite3_exec(db, u.bz.zSql, sqlite3InitCallback, &u.bz.initData, 0);
68557 if( rc==SQLITE_OK ) rc = u.bz.initData.rc;
68558 sqlite3DbFree(db, u.bz.zSql);
68559 db->init.busy = 0;
68560 }
68561 }
68562 if( rc ) sqlite3ResetAllSchemasOfConnection(db);
68563 if( rc==SQLITE_NOMEM ){
@@ -68580,45 +68637,45 @@
68637 ** file, not the main database file.
68638 **
68639 ** This opcode is used to implement the integrity_check pragma.
68640 */
68641 case OP_IntegrityCk: {
68642 #if 0 /* local variables moved into u.ca */
68643 int nRoot; /* Number of tables to check. (Number of root pages.) */
68644 int *aRoot; /* Array of rootpage numbers for tables to be checked */
68645 int j; /* Loop counter */
68646 int nErr; /* Number of errors reported */
68647 char *z; /* Text of the error report */
68648 Mem *pnErr; /* Register keeping track of errors remaining */
68649 #endif /* local variables moved into u.ca */
68650
68651 u.ca.nRoot = pOp->p2;
68652 assert( u.ca.nRoot>0 );
68653 u.ca.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.ca.nRoot+1) );
68654 if( u.ca.aRoot==0 ) goto no_mem;
68655 assert( pOp->p3>0 && pOp->p3<=p->nMem );
68656 u.ca.pnErr = &aMem[pOp->p3];
68657 assert( (u.ca.pnErr->flags & MEM_Int)!=0 );
68658 assert( (u.ca.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
68659 pIn1 = &aMem[pOp->p1];
68660 for(u.ca.j=0; u.ca.j<u.ca.nRoot; u.ca.j++){
68661 u.ca.aRoot[u.ca.j] = (int)sqlite3VdbeIntValue(&pIn1[u.ca.j]);
68662 }
68663 u.ca.aRoot[u.ca.j] = 0;
68664 assert( pOp->p5<db->nDb );
68665 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
68666 u.ca.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.ca.aRoot, u.ca.nRoot,
68667 (int)u.ca.pnErr->u.i, &u.ca.nErr);
68668 sqlite3DbFree(db, u.ca.aRoot);
68669 u.ca.pnErr->u.i -= u.ca.nErr;
68670 sqlite3VdbeMemSetNull(pIn1);
68671 if( u.ca.nErr==0 ){
68672 assert( u.ca.z==0 );
68673 }else if( u.ca.z==0 ){
68674 goto no_mem;
68675 }else{
68676 sqlite3VdbeMemSetStr(pIn1, u.ca.z, -1, SQLITE_UTF8, sqlite3_free);
68677 }
68678 UPDATE_MAX_BLOBSIZE(pIn1);
68679 sqlite3VdbeChangeEncoding(pIn1, encoding);
68680 break;
68681 }
@@ -68648,24 +68705,24 @@
68705 ** Extract the smallest value from boolean index P1 and put that value into
68706 ** register P3. Or, if boolean index P1 is initially empty, leave P3
68707 ** unchanged and jump to instruction P2.
68708 */
68709 case OP_RowSetRead: { /* jump, in1, out3 */
68710 #if 0 /* local variables moved into u.cb */
68711 i64 val;
68712 #endif /* local variables moved into u.cb */
68713 CHECK_FOR_INTERRUPT;
68714 pIn1 = &aMem[pOp->p1];
68715 if( (pIn1->flags & MEM_RowSet)==0
68716 || sqlite3RowSetNext(pIn1->u.pRowSet, &u.cb.val)==0
68717 ){
68718 /* The boolean index is empty */
68719 sqlite3VdbeMemSetNull(pIn1);
68720 pc = pOp->p2 - 1;
68721 }else{
68722 /* A value was pulled from the index */
68723 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.cb.val);
68724 }
68725 break;
68726 }
68727
68728 /* Opcode: RowSetTest P1 P2 P3 P4
@@ -68690,18 +68747,18 @@
68747 ** inserted, there is no need to search to see if the same value was
68748 ** previously inserted as part of set X (only if it was previously
68749 ** inserted as part of some other set).
68750 */
68751 case OP_RowSetTest: { /* jump, in1, in3 */
68752 #if 0 /* local variables moved into u.cc */
68753 int iSet;
68754 int exists;
68755 #endif /* local variables moved into u.cc */
68756
68757 pIn1 = &aMem[pOp->p1];
68758 pIn3 = &aMem[pOp->p3];
68759 u.cc.iSet = pOp->p4.i;
68760 assert( pIn3->flags&MEM_Int );
68761
68762 /* If there is anything other than a rowset object in memory cell P1,
68763 ** delete it now and initialize P1 with an empty rowset
68764 */
@@ -68709,21 +68766,21 @@
68766 sqlite3VdbeMemSetRowSet(pIn1);
68767 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
68768 }
68769
68770 assert( pOp->p4type==P4_INT32 );
68771 assert( u.cc.iSet==-1 || u.cc.iSet>=0 );
68772 if( u.cc.iSet ){
68773 u.cc.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
68774 (u8)(u.cc.iSet>=0 ? u.cc.iSet & 0xf : 0xff),
68775 pIn3->u.i);
68776 if( u.cc.exists ){
68777 pc = pOp->p2 - 1;
68778 break;
68779 }
68780 }
68781 if( u.cc.iSet>=0 ){
68782 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
68783 }
68784 break;
68785 }
68786
@@ -68742,24 +68799,24 @@
68799 ** memory required by the sub-vdbe at runtime.
68800 **
68801 ** P4 is a pointer to the VM containing the trigger program.
68802 */
68803 case OP_Program: { /* jump */
68804 #if 0 /* local variables moved into u.cd */
68805 int nMem; /* Number of memory registers for sub-program */
68806 int nByte; /* Bytes of runtime space required for sub-program */
68807 Mem *pRt; /* Register to allocate runtime space */
68808 Mem *pMem; /* Used to iterate through memory cells */
68809 Mem *pEnd; /* Last memory cell in new array */
68810 VdbeFrame *pFrame; /* New vdbe frame to execute in */
68811 SubProgram *pProgram; /* Sub-program to execute */
68812 void *t; /* Token identifying trigger */
68813 #endif /* local variables moved into u.cd */
68814
68815 u.cd.pProgram = pOp->p4.pProgram;
68816 u.cd.pRt = &aMem[pOp->p3];
68817 assert( u.cd.pProgram->nOp>0 );
68818
68819 /* If the p5 flag is clear, then recursive invocation of triggers is
68820 ** disabled for backwards compatibility (p5 is set if this sub-program
68821 ** is really a trigger, not a foreign key action, and the flag set
68822 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
@@ -68769,84 +68826,84 @@
68826 ** SubProgram (if the trigger may be executed with more than one different
68827 ** ON CONFLICT algorithm). SubProgram structures associated with a
68828 ** single trigger all have the same value for the SubProgram.token
68829 ** variable. */
68830 if( pOp->p5 ){
68831 u.cd.t = u.cd.pProgram->token;
68832 for(u.cd.pFrame=p->pFrame; u.cd.pFrame && u.cd.pFrame->token!=u.cd.t; u.cd.pFrame=u.cd.pFrame->pParent);
68833 if( u.cd.pFrame ) break;
68834 }
68835
68836 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
68837 rc = SQLITE_ERROR;
68838 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
68839 break;
68840 }
68841
68842 /* Register u.cd.pRt is used to store the memory required to save the state
68843 ** of the current program, and the memory required at runtime to execute
68844 ** the trigger program. If this trigger has been fired before, then u.cd.pRt
68845 ** is already allocated. Otherwise, it must be initialized. */
68846 if( (u.cd.pRt->flags&MEM_Frame)==0 ){
68847 /* SubProgram.nMem is set to the number of memory cells used by the
68848 ** program stored in SubProgram.aOp. As well as these, one memory
68849 ** cell is required for each cursor used by the program. Set local
68850 ** variable u.cd.nMem (and later, VdbeFrame.nChildMem) to this value.
68851 */
68852 u.cd.nMem = u.cd.pProgram->nMem + u.cd.pProgram->nCsr;
68853 u.cd.nByte = ROUND8(sizeof(VdbeFrame))
68854 + u.cd.nMem * sizeof(Mem)
68855 + u.cd.pProgram->nCsr * sizeof(VdbeCursor *)
68856 + u.cd.pProgram->nOnce * sizeof(u8);
68857 u.cd.pFrame = sqlite3DbMallocZero(db, u.cd.nByte);
68858 if( !u.cd.pFrame ){
68859 goto no_mem;
68860 }
68861 sqlite3VdbeMemRelease(u.cd.pRt);
68862 u.cd.pRt->flags = MEM_Frame;
68863 u.cd.pRt->u.pFrame = u.cd.pFrame;
68864
68865 u.cd.pFrame->v = p;
68866 u.cd.pFrame->nChildMem = u.cd.nMem;
68867 u.cd.pFrame->nChildCsr = u.cd.pProgram->nCsr;
68868 u.cd.pFrame->pc = pc;
68869 u.cd.pFrame->aMem = p->aMem;
68870 u.cd.pFrame->nMem = p->nMem;
68871 u.cd.pFrame->apCsr = p->apCsr;
68872 u.cd.pFrame->nCursor = p->nCursor;
68873 u.cd.pFrame->aOp = p->aOp;
68874 u.cd.pFrame->nOp = p->nOp;
68875 u.cd.pFrame->token = u.cd.pProgram->token;
68876 u.cd.pFrame->aOnceFlag = p->aOnceFlag;
68877 u.cd.pFrame->nOnceFlag = p->nOnceFlag;
68878
68879 u.cd.pEnd = &VdbeFrameMem(u.cd.pFrame)[u.cd.pFrame->nChildMem];
68880 for(u.cd.pMem=VdbeFrameMem(u.cd.pFrame); u.cd.pMem!=u.cd.pEnd; u.cd.pMem++){
68881 u.cd.pMem->flags = MEM_Invalid;
68882 u.cd.pMem->db = db;
68883 }
68884 }else{
68885 u.cd.pFrame = u.cd.pRt->u.pFrame;
68886 assert( u.cd.pProgram->nMem+u.cd.pProgram->nCsr==u.cd.pFrame->nChildMem );
68887 assert( u.cd.pProgram->nCsr==u.cd.pFrame->nChildCsr );
68888 assert( pc==u.cd.pFrame->pc );
68889 }
68890
68891 p->nFrame++;
68892 u.cd.pFrame->pParent = p->pFrame;
68893 u.cd.pFrame->lastRowid = lastRowid;
68894 u.cd.pFrame->nChange = p->nChange;
68895 p->nChange = 0;
68896 p->pFrame = u.cd.pFrame;
68897 p->aMem = aMem = &VdbeFrameMem(u.cd.pFrame)[-1];
68898 p->nMem = u.cd.pFrame->nChildMem;
68899 p->nCursor = (u16)u.cd.pFrame->nChildCsr;
68900 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
68901 p->aOp = aOp = u.cd.pProgram->aOp;
68902 p->nOp = u.cd.pProgram->nOp;
68903 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
68904 p->nOnceFlag = u.cd.pProgram->nOnce;
68905 pc = -1;
68906 memset(p->aOnceFlag, 0, p->nOnceFlag);
68907
68908 break;
68909 }
@@ -68862,17 +68919,17 @@
68919 ** The address of the cell in the parent frame is determined by adding
68920 ** the value of the P1 argument to the value of the P1 argument to the
68921 ** calling OP_Program instruction.
68922 */
68923 case OP_Param: { /* out2-prerelease */
68924 #if 0 /* local variables moved into u.ce */
68925 VdbeFrame *pFrame;
68926 Mem *pIn;
68927 #endif /* local variables moved into u.ce */
68928 u.ce.pFrame = p->pFrame;
68929 u.ce.pIn = &u.ce.pFrame->aMem[pOp->p1 + u.ce.pFrame->aOp[u.ce.pFrame->pc].p1];
68930 sqlite3VdbeMemShallowCopy(pOut, u.ce.pIn, MEM_Ephem);
68931 break;
68932 }
68933
68934 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
68935
@@ -68924,26 +68981,26 @@
68981 **
68982 ** This instruction throws an error if the memory cell is not initially
68983 ** an integer.
68984 */
68985 case OP_MemMax: { /* in2 */
68986 #if 0 /* local variables moved into u.cf */
68987 Mem *pIn1;
68988 VdbeFrame *pFrame;
68989 #endif /* local variables moved into u.cf */
68990 if( p->pFrame ){
68991 for(u.cf.pFrame=p->pFrame; u.cf.pFrame->pParent; u.cf.pFrame=u.cf.pFrame->pParent);
68992 u.cf.pIn1 = &u.cf.pFrame->aMem[pOp->p1];
68993 }else{
68994 u.cf.pIn1 = &aMem[pOp->p1];
68995 }
68996 assert( memIsValid(u.cf.pIn1) );
68997 sqlite3VdbeMemIntegerify(u.cf.pIn1);
68998 pIn2 = &aMem[pOp->p2];
68999 sqlite3VdbeMemIntegerify(pIn2);
69000 if( u.cf.pIn1->u.i<pIn2->u.i){
69001 u.cf.pIn1->u.i = pIn2->u.i;
69002 }
69003 break;
69004 }
69005 #endif /* SQLITE_OMIT_AUTOINCREMENT */
69006
@@ -69006,60 +69063,60 @@
69063 **
69064 ** The P5 arguments are taken from register P2 and its
69065 ** successors.
69066 */
69067 case OP_AggStep: {
69068 #if 0 /* local variables moved into u.cg */
69069 int n;
69070 int i;
69071 Mem *pMem;
69072 Mem *pRec;
69073 sqlite3_context ctx;
69074 sqlite3_value **apVal;
69075 #endif /* local variables moved into u.cg */
69076
69077 u.cg.n = pOp->p5;
69078 assert( u.cg.n>=0 );
69079 u.cg.pRec = &aMem[pOp->p2];
69080 u.cg.apVal = p->apArg;
69081 assert( u.cg.apVal || u.cg.n==0 );
69082 for(u.cg.i=0; u.cg.i<u.cg.n; u.cg.i++, u.cg.pRec++){
69083 assert( memIsValid(u.cg.pRec) );
69084 u.cg.apVal[u.cg.i] = u.cg.pRec;
69085 memAboutToChange(p, u.cg.pRec);
69086 sqlite3VdbeMemStoreType(u.cg.pRec);
69087 }
69088 u.cg.ctx.pFunc = pOp->p4.pFunc;
69089 assert( pOp->p3>0 && pOp->p3<=p->nMem );
69090 u.cg.ctx.pMem = u.cg.pMem = &aMem[pOp->p3];
69091 u.cg.pMem->n++;
69092 u.cg.ctx.s.flags = MEM_Null;
69093 u.cg.ctx.s.z = 0;
69094 u.cg.ctx.s.zMalloc = 0;
69095 u.cg.ctx.s.xDel = 0;
69096 u.cg.ctx.s.db = db;
69097 u.cg.ctx.isError = 0;
69098 u.cg.ctx.pColl = 0;
69099 u.cg.ctx.skipFlag = 0;
69100 if( u.cg.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
69101 assert( pOp>p->aOp );
69102 assert( pOp[-1].p4type==P4_COLLSEQ );
69103 assert( pOp[-1].opcode==OP_CollSeq );
69104 u.cg.ctx.pColl = pOp[-1].p4.pColl;
69105 }
69106 (u.cg.ctx.pFunc->xStep)(&u.cg.ctx, u.cg.n, u.cg.apVal); /* IMP: R-24505-23230 */
69107 if( u.cg.ctx.isError ){
69108 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cg.ctx.s));
69109 rc = u.cg.ctx.isError;
69110 }
69111 if( u.cg.ctx.skipFlag ){
69112 assert( pOp[-1].opcode==OP_CollSeq );
69113 u.cg.i = pOp[-1].p1;
69114 if( u.cg.i ) sqlite3VdbeMemSetInt64(&aMem[u.cg.i], 1);
69115 }
69116
69117 sqlite3VdbeMemRelease(&u.cg.ctx.s);
69118
69119 break;
69120 }
69121
69122 /* Opcode: AggFinal P1 P2 * P4 *
@@ -69073,23 +69130,23 @@
69130 ** functions that can take varying numbers of arguments. The
69131 ** P4 argument is only needed for the degenerate case where
69132 ** the step function was not previously called.
69133 */
69134 case OP_AggFinal: {
69135 #if 0 /* local variables moved into u.ch */
69136 Mem *pMem;
69137 #endif /* local variables moved into u.ch */
69138 assert( pOp->p1>0 && pOp->p1<=p->nMem );
69139 u.ch.pMem = &aMem[pOp->p1];
69140 assert( (u.ch.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
69141 rc = sqlite3VdbeMemFinalize(u.ch.pMem, pOp->p4.pFunc);
69142 if( rc ){
69143 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.ch.pMem));
69144 }
69145 sqlite3VdbeChangeEncoding(u.ch.pMem, encoding);
69146 UPDATE_MAX_BLOBSIZE(u.ch.pMem);
69147 if( sqlite3VdbeMemTooBig(u.ch.pMem) ){
69148 goto too_big;
69149 }
69150 break;
69151 }
69152
@@ -69104,29 +69161,29 @@
69161 ** in the WAL that have been checkpointed after the checkpoint
69162 ** completes into mem[P3+2]. However on an error, mem[P3+1] and
69163 ** mem[P3+2] are initialized to -1.
69164 */
69165 case OP_Checkpoint: {
69166 #if 0 /* local variables moved into u.ci */
69167 int i; /* Loop counter */
69168 int aRes[3]; /* Results */
69169 Mem *pMem; /* Write results here */
69170 #endif /* local variables moved into u.ci */
69171
69172 u.ci.aRes[0] = 0;
69173 u.ci.aRes[1] = u.ci.aRes[2] = -1;
69174 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
69175 || pOp->p2==SQLITE_CHECKPOINT_FULL
69176 || pOp->p2==SQLITE_CHECKPOINT_RESTART
69177 );
69178 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.ci.aRes[1], &u.ci.aRes[2]);
69179 if( rc==SQLITE_BUSY ){
69180 rc = SQLITE_OK;
69181 u.ci.aRes[0] = 1;
69182 }
69183 for(u.ci.i=0, u.ci.pMem = &aMem[pOp->p3]; u.ci.i<3; u.ci.i++, u.ci.pMem++){
69184 sqlite3VdbeMemSetInt64(u.ci.pMem, (i64)u.ci.aRes[u.ci.i]);
69185 }
69186 break;
69187 };
69188 #endif
69189
@@ -69141,97 +69198,97 @@
69198 ** If changing into or out of WAL mode the procedure is more complicated.
69199 **
69200 ** Write a string containing the final journal-mode to register P2.
69201 */
69202 case OP_JournalMode: { /* out2-prerelease */
69203 #if 0 /* local variables moved into u.cj */
69204 Btree *pBt; /* Btree to change journal mode of */
69205 Pager *pPager; /* Pager associated with pBt */
69206 int eNew; /* New journal mode */
69207 int eOld; /* The old journal mode */
 
69208 #ifndef SQLITE_OMIT_WAL
69209 const char *zFilename; /* Name of database file for pPager */
69210 #endif
69211 #endif /* local variables moved into u.cj */
69212
69213 u.cj.eNew = pOp->p3;
69214 assert( u.cj.eNew==PAGER_JOURNALMODE_DELETE
69215 || u.cj.eNew==PAGER_JOURNALMODE_TRUNCATE
69216 || u.cj.eNew==PAGER_JOURNALMODE_PERSIST
69217 || u.cj.eNew==PAGER_JOURNALMODE_OFF
69218 || u.cj.eNew==PAGER_JOURNALMODE_MEMORY
69219 || u.cj.eNew==PAGER_JOURNALMODE_WAL
69220 || u.cj.eNew==PAGER_JOURNALMODE_QUERY
69221 );
69222 assert( pOp->p1>=0 && pOp->p1<db->nDb );
69223
69224 u.cj.pBt = db->aDb[pOp->p1].pBt;
69225 u.cj.pPager = sqlite3BtreePager(u.cj.pBt);
69226 u.cj.eOld = sqlite3PagerGetJournalMode(u.cj.pPager);
69227 if( u.cj.eNew==PAGER_JOURNALMODE_QUERY ) u.cj.eNew = u.cj.eOld;
69228 if( !sqlite3PagerOkToChangeJournalMode(u.cj.pPager) ) u.cj.eNew = u.cj.eOld;
69229
69230 #ifndef SQLITE_OMIT_WAL
69231 u.cj.zFilename = sqlite3PagerFilename(u.cj.pPager, 1);
69232
69233 /* Do not allow a transition to journal_mode=WAL for a database
69234 ** in temporary storage or if the VFS does not support shared memory
69235 */
69236 if( u.cj.eNew==PAGER_JOURNALMODE_WAL
69237 && (sqlite3Strlen30(u.cj.zFilename)==0 /* Temp file */
69238 || !sqlite3PagerWalSupported(u.cj.pPager)) /* No shared-memory support */
69239 ){
69240 u.cj.eNew = u.cj.eOld;
69241 }
69242
69243 if( (u.cj.eNew!=u.cj.eOld)
69244 && (u.cj.eOld==PAGER_JOURNALMODE_WAL || u.cj.eNew==PAGER_JOURNALMODE_WAL)
69245 ){
69246 if( !db->autoCommit || db->activeVdbeCnt>1 ){
69247 rc = SQLITE_ERROR;
69248 sqlite3SetString(&p->zErrMsg, db,
69249 "cannot change %s wal mode from within a transaction",
69250 (u.cj.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
69251 );
69252 break;
69253 }else{
69254
69255 if( u.cj.eOld==PAGER_JOURNALMODE_WAL ){
69256 /* If leaving WAL mode, close the log file. If successful, the call
69257 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
69258 ** file. An EXCLUSIVE lock may still be held on the database file
69259 ** after a successful return.
69260 */
69261 rc = sqlite3PagerCloseWal(u.cj.pPager);
69262 if( rc==SQLITE_OK ){
69263 sqlite3PagerSetJournalMode(u.cj.pPager, u.cj.eNew);
69264 }
69265 }else if( u.cj.eOld==PAGER_JOURNALMODE_MEMORY ){
69266 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
69267 ** as an intermediate */
69268 sqlite3PagerSetJournalMode(u.cj.pPager, PAGER_JOURNALMODE_OFF);
69269 }
69270
69271 /* Open a transaction on the database file. Regardless of the journal
69272 ** mode, this transaction always uses a rollback journal.
69273 */
69274 assert( sqlite3BtreeIsInTrans(u.cj.pBt)==0 );
69275 if( rc==SQLITE_OK ){
69276 rc = sqlite3BtreeSetVersion(u.cj.pBt, (u.cj.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
69277 }
69278 }
69279 }
69280 #endif /* ifndef SQLITE_OMIT_WAL */
69281
69282 if( rc ){
69283 u.cj.eNew = u.cj.eOld;
69284 }
69285 u.cj.eNew = sqlite3PagerSetJournalMode(u.cj.pPager, u.cj.eNew);
69286
69287 pOut = &aMem[pOp->p2];
69288 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
69289 pOut->z = (char *)sqlite3JournalModename(u.cj.eNew);
69290 pOut->n = sqlite3Strlen30(pOut->z);
69291 pOut->enc = SQLITE_UTF8;
69292 sqlite3VdbeChangeEncoding(pOut, encoding);
69293 break;
69294 };
@@ -69256,18 +69313,18 @@
69313 ** Perform a single step of the incremental vacuum procedure on
69314 ** the P1 database. If the vacuum has finished, jump to instruction
69315 ** P2. Otherwise, fall through to the next instruction.
69316 */
69317 case OP_IncrVacuum: { /* jump */
69318 #if 0 /* local variables moved into u.ck */
69319 Btree *pBt;
69320 #endif /* local variables moved into u.ck */
69321
69322 assert( pOp->p1>=0 && pOp->p1<db->nDb );
69323 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
69324 u.ck.pBt = db->aDb[pOp->p1].pBt;
69325 rc = sqlite3BtreeIncrVacuum(u.ck.pBt);
69326 if( rc==SQLITE_DONE ){
69327 pc = pOp->p2 - 1;
69328 rc = SQLITE_OK;
69329 }
69330 break;
@@ -69333,16 +69390,16 @@
69390 ** Also, whether or not P4 is set, check that this is not being called from
69391 ** within a callback to a virtual table xSync() method. If it is, the error
69392 ** code will be set to SQLITE_LOCKED.
69393 */
69394 case OP_VBegin: {
69395 #if 0 /* local variables moved into u.cl */
69396 VTable *pVTab;
69397 #endif /* local variables moved into u.cl */
69398 u.cl.pVTab = pOp->p4.pVtab;
69399 rc = sqlite3VtabBegin(db, u.cl.pVTab);
69400 if( u.cl.pVTab ) importVtabErrMsg(p, u.cl.pVTab->pVtab);
69401 break;
69402 }
69403 #endif /* SQLITE_OMIT_VIRTUALTABLE */
69404
69405 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -69377,36 +69434,36 @@
69434 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
69435 ** P1 is a cursor number. This opcode opens a cursor to the virtual
69436 ** table and stores that cursor in P1.
69437 */
69438 case OP_VOpen: {
69439 #if 0 /* local variables moved into u.cm */
69440 VdbeCursor *pCur;
69441 sqlite3_vtab_cursor *pVtabCursor;
69442 sqlite3_vtab *pVtab;
69443 sqlite3_module *pModule;
69444 #endif /* local variables moved into u.cm */
69445
69446 u.cm.pCur = 0;
69447 u.cm.pVtabCursor = 0;
69448 u.cm.pVtab = pOp->p4.pVtab->pVtab;
69449 u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
69450 assert(u.cm.pVtab && u.cm.pModule);
69451 rc = u.cm.pModule->xOpen(u.cm.pVtab, &u.cm.pVtabCursor);
69452 importVtabErrMsg(p, u.cm.pVtab);
69453 if( SQLITE_OK==rc ){
69454 /* Initialize sqlite3_vtab_cursor base class */
69455 u.cm.pVtabCursor->pVtab = u.cm.pVtab;
69456
69457 /* Initialise vdbe cursor object */
69458 u.cm.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
69459 if( u.cm.pCur ){
69460 u.cm.pCur->pVtabCursor = u.cm.pVtabCursor;
69461 u.cm.pCur->pModule = u.cm.pVtabCursor->pVtab->pModule;
69462 }else{
69463 db->mallocFailed = 1;
69464 u.cm.pModule->xClose(u.cm.pVtabCursor);
69465 }
69466 }
69467 break;
69468 }
69469 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -69429,11 +69486,11 @@
69486 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
69487 **
69488 ** A jump is made to P2 if the result set after filtering would be empty.
69489 */
69490 case OP_VFilter: { /* jump */
69491 #if 0 /* local variables moved into u.cn */
69492 int nArg;
69493 int iQuery;
69494 const sqlite3_module *pModule;
69495 Mem *pQuery;
69496 Mem *pArgc;
@@ -69441,49 +69498,49 @@
69498 sqlite3_vtab *pVtab;
69499 VdbeCursor *pCur;
69500 int res;
69501 int i;
69502 Mem **apArg;
69503 #endif /* local variables moved into u.cn */
69504
69505 u.cn.pQuery = &aMem[pOp->p3];
69506 u.cn.pArgc = &u.cn.pQuery[1];
69507 u.cn.pCur = p->apCsr[pOp->p1];
69508 assert( memIsValid(u.cn.pQuery) );
69509 REGISTER_TRACE(pOp->p3, u.cn.pQuery);
69510 assert( u.cn.pCur->pVtabCursor );
69511 u.cn.pVtabCursor = u.cn.pCur->pVtabCursor;
69512 u.cn.pVtab = u.cn.pVtabCursor->pVtab;
69513 u.cn.pModule = u.cn.pVtab->pModule;
69514
69515 /* Grab the index number and argc parameters */
69516 assert( (u.cn.pQuery->flags&MEM_Int)!=0 && u.cn.pArgc->flags==MEM_Int );
69517 u.cn.nArg = (int)u.cn.pArgc->u.i;
69518 u.cn.iQuery = (int)u.cn.pQuery->u.i;
69519
69520 /* Invoke the xFilter method */
69521 {
69522 u.cn.res = 0;
69523 u.cn.apArg = p->apArg;
69524 for(u.cn.i = 0; u.cn.i<u.cn.nArg; u.cn.i++){
69525 u.cn.apArg[u.cn.i] = &u.cn.pArgc[u.cn.i+1];
69526 sqlite3VdbeMemStoreType(u.cn.apArg[u.cn.i]);
69527 }
69528
69529 p->inVtabMethod = 1;
69530 rc = u.cn.pModule->xFilter(u.cn.pVtabCursor, u.cn.iQuery, pOp->p4.z, u.cn.nArg, u.cn.apArg);
69531 p->inVtabMethod = 0;
69532 importVtabErrMsg(p, u.cn.pVtab);
69533 if( rc==SQLITE_OK ){
69534 u.cn.res = u.cn.pModule->xEof(u.cn.pVtabCursor);
69535 }
69536
69537 if( u.cn.res ){
69538 pc = pOp->p2 - 1;
69539 }
69540 }
69541 u.cn.pCur->nullRow = 0;
69542
69543 break;
69544 }
69545 #endif /* SQLITE_OMIT_VIRTUALTABLE */
69546
@@ -69493,55 +69550,55 @@
69550 ** Store the value of the P2-th column of
69551 ** the row of the virtual-table that the
69552 ** P1 cursor is pointing to into register P3.
69553 */
69554 case OP_VColumn: {
69555 #if 0 /* local variables moved into u.co */
69556 sqlite3_vtab *pVtab;
69557 const sqlite3_module *pModule;
69558 Mem *pDest;
69559 sqlite3_context sContext;
69560 #endif /* local variables moved into u.co */
69561
69562 VdbeCursor *pCur = p->apCsr[pOp->p1];
69563 assert( pCur->pVtabCursor );
69564 assert( pOp->p3>0 && pOp->p3<=p->nMem );
69565 u.co.pDest = &aMem[pOp->p3];
69566 memAboutToChange(p, u.co.pDest);
69567 if( pCur->nullRow ){
69568 sqlite3VdbeMemSetNull(u.co.pDest);
69569 break;
69570 }
69571 u.co.pVtab = pCur->pVtabCursor->pVtab;
69572 u.co.pModule = u.co.pVtab->pModule;
69573 assert( u.co.pModule->xColumn );
69574 memset(&u.co.sContext, 0, sizeof(u.co.sContext));
69575
69576 /* The output cell may already have a buffer allocated. Move
69577 ** the current contents to u.co.sContext.s so in case the user-function
69578 ** can use the already allocated buffer instead of allocating a
69579 ** new one.
69580 */
69581 sqlite3VdbeMemMove(&u.co.sContext.s, u.co.pDest);
69582 MemSetTypeFlag(&u.co.sContext.s, MEM_Null);
69583
69584 rc = u.co.pModule->xColumn(pCur->pVtabCursor, &u.co.sContext, pOp->p2);
69585 importVtabErrMsg(p, u.co.pVtab);
69586 if( u.co.sContext.isError ){
69587 rc = u.co.sContext.isError;
69588 }
69589
69590 /* Copy the result of the function to the P3 register. We
69591 ** do this regardless of whether or not an error occurred to ensure any
69592 ** dynamic allocation in u.co.sContext.s (a Mem struct) is released.
69593 */
69594 sqlite3VdbeChangeEncoding(&u.co.sContext.s, encoding);
69595 sqlite3VdbeMemMove(u.co.pDest, &u.co.sContext.s);
69596 REGISTER_TRACE(pOp->p3, u.co.pDest);
69597 UPDATE_MAX_BLOBSIZE(u.co.pDest);
69598
69599 if( sqlite3VdbeMemTooBig(u.co.pDest) ){
69600 goto too_big;
69601 }
69602 break;
69603 }
69604 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -69552,42 +69609,42 @@
69609 ** Advance virtual table P1 to the next row in its result set and
69610 ** jump to instruction P2. Or, if the virtual table has reached
69611 ** the end of its result set, then fall through to the next instruction.
69612 */
69613 case OP_VNext: { /* jump */
69614 #if 0 /* local variables moved into u.cp */
69615 sqlite3_vtab *pVtab;
69616 const sqlite3_module *pModule;
69617 int res;
69618 VdbeCursor *pCur;
69619 #endif /* local variables moved into u.cp */
69620
69621 u.cp.res = 0;
69622 u.cp.pCur = p->apCsr[pOp->p1];
69623 assert( u.cp.pCur->pVtabCursor );
69624 if( u.cp.pCur->nullRow ){
69625 break;
69626 }
69627 u.cp.pVtab = u.cp.pCur->pVtabCursor->pVtab;
69628 u.cp.pModule = u.cp.pVtab->pModule;
69629 assert( u.cp.pModule->xNext );
69630
69631 /* Invoke the xNext() method of the module. There is no way for the
69632 ** underlying implementation to return an error if one occurs during
69633 ** xNext(). Instead, if an error occurs, true is returned (indicating that
69634 ** data is available) and the error code returned when xColumn or
69635 ** some other method is next invoked on the save virtual table cursor.
69636 */
69637 p->inVtabMethod = 1;
69638 rc = u.cp.pModule->xNext(u.cp.pCur->pVtabCursor);
69639 p->inVtabMethod = 0;
69640 importVtabErrMsg(p, u.cp.pVtab);
69641 if( rc==SQLITE_OK ){
69642 u.cp.res = u.cp.pModule->xEof(u.cp.pCur->pVtabCursor);
69643 }
69644
69645 if( !u.cp.res ){
69646 /* If there is data, jump to P2 */
69647 pc = pOp->p2 - 1;
69648 }
69649 break;
69650 }
@@ -69599,28 +69656,28 @@
69656 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
69657 ** This opcode invokes the corresponding xRename method. The value
69658 ** in register P1 is passed as the zName argument to the xRename method.
69659 */
69660 case OP_VRename: {
69661 #if 0 /* local variables moved into u.cq */
69662 sqlite3_vtab *pVtab;
69663 Mem *pName;
69664 #endif /* local variables moved into u.cq */
69665
69666 u.cq.pVtab = pOp->p4.pVtab->pVtab;
69667 u.cq.pName = &aMem[pOp->p1];
69668 assert( u.cq.pVtab->pModule->xRename );
69669 assert( memIsValid(u.cq.pName) );
69670 REGISTER_TRACE(pOp->p1, u.cq.pName);
69671 assert( u.cq.pName->flags & MEM_Str );
69672 testcase( u.cq.pName->enc==SQLITE_UTF8 );
69673 testcase( u.cq.pName->enc==SQLITE_UTF16BE );
69674 testcase( u.cq.pName->enc==SQLITE_UTF16LE );
69675 rc = sqlite3VdbeChangeEncoding(u.cq.pName, SQLITE_UTF8);
69676 if( rc==SQLITE_OK ){
69677 rc = u.cq.pVtab->pModule->xRename(u.cq.pVtab, u.cq.pName->z);
69678 importVtabErrMsg(p, u.cq.pVtab);
69679 p->expired = 0;
69680 }
69681 break;
69682 }
69683 #endif
@@ -69648,45 +69705,45 @@
69705 ** P1 is a boolean flag. If it is set to true and the xUpdate call
69706 ** is successful, then the value returned by sqlite3_last_insert_rowid()
69707 ** is set to the value of the rowid for the row just inserted.
69708 */
69709 case OP_VUpdate: {
69710 #if 0 /* local variables moved into u.cr */
69711 sqlite3_vtab *pVtab;
69712 sqlite3_module *pModule;
69713 int nArg;
69714 int i;
69715 sqlite_int64 rowid;
69716 Mem **apArg;
69717 Mem *pX;
69718 #endif /* local variables moved into u.cr */
69719
69720 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
69721 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
69722 );
69723 u.cr.pVtab = pOp->p4.pVtab->pVtab;
69724 u.cr.pModule = (sqlite3_module *)u.cr.pVtab->pModule;
69725 u.cr.nArg = pOp->p2;
69726 assert( pOp->p4type==P4_VTAB );
69727 if( ALWAYS(u.cr.pModule->xUpdate) ){
69728 u8 vtabOnConflict = db->vtabOnConflict;
69729 u.cr.apArg = p->apArg;
69730 u.cr.pX = &aMem[pOp->p3];
69731 for(u.cr.i=0; u.cr.i<u.cr.nArg; u.cr.i++){
69732 assert( memIsValid(u.cr.pX) );
69733 memAboutToChange(p, u.cr.pX);
69734 sqlite3VdbeMemStoreType(u.cr.pX);
69735 u.cr.apArg[u.cr.i] = u.cr.pX;
69736 u.cr.pX++;
69737 }
69738 db->vtabOnConflict = pOp->p5;
69739 rc = u.cr.pModule->xUpdate(u.cr.pVtab, u.cr.nArg, u.cr.apArg, &u.cr.rowid);
69740 db->vtabOnConflict = vtabOnConflict;
69741 importVtabErrMsg(p, u.cr.pVtab);
69742 if( rc==SQLITE_OK && pOp->p1 ){
69743 assert( u.cr.nArg>1 && u.cr.apArg[0] && (u.cr.apArg[0]->flags&MEM_Null) );
69744 db->lastRowid = lastRowid = u.cr.rowid;
69745 }
69746 if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
69747 if( pOp->p5==OE_Ignore ){
69748 rc = SQLITE_OK;
69749 }else{
@@ -69742,28 +69799,28 @@
69799 **
69800 ** If tracing is enabled (by the sqlite3_trace()) interface, then
69801 ** the UTF-8 string contained in P4 is emitted on the trace callback.
69802 */
69803 case OP_Trace: {
69804 #if 0 /* local variables moved into u.cs */
69805 char *zTrace;
69806 char *z;
69807 #endif /* local variables moved into u.cs */
69808
69809 if( db->xTrace
69810 && !p->doingRerun
69811 && (u.cs.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
69812 ){
69813 u.cs.z = sqlite3VdbeExpandSql(p, u.cs.zTrace);
69814 db->xTrace(db->pTraceArg, u.cs.z);
69815 sqlite3DbFree(db, u.cs.z);
69816 }
69817 #ifdef SQLITE_DEBUG
69818 if( (db->flags & SQLITE_SqlTrace)!=0
69819 && (u.cs.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
69820 ){
69821 sqlite3DebugPrintf("SQL-trace: %s\n", u.cs.zTrace);
69822 }
69823 #endif /* SQLITE_DEBUG */
69824 break;
69825 }
69826 #endif
@@ -74733,27 +74790,36 @@
74790 return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
74791 }
74792
74793 /*
74794 ** This function is used by the implementation of the IN (...) operator.
74795 ** The pX parameter is the expression on the RHS of the IN operator, which
74796 ** might be either a list of expressions or a subquery.
 
74797 **
74798 ** The job of this routine is to find or create a b-tree object that can
74799 ** be used either to test for membership in the RHS set or to iterate through
74800 ** all members of the RHS set, skipping duplicates.
74801 **
74802 ** A cursor is opened on the b-tree object that the RHS of the IN operator
74803 ** and pX->iTable is set to the index of that cursor.
74804 **
74805 ** The returned value of this function indicates the b-tree type, as follows:
74806 **
74807 ** IN_INDEX_ROWID - The cursor was opened on a database table.
74808 ** IN_INDEX_INDEX - The cursor was opened on a database index.
74809 ** IN_INDEX_EPH - The cursor was opened on a specially created and
74810 ** populated epheremal table.
74811 **
74812 ** An existing b-tree might be used if the RHS expression pX is a simple
74813 ** subquery such as:
74814 **
74815 ** SELECT <column> FROM <table>
74816 **
74817 ** If the RHS of the IN operator is a list or a more complex subquery, then
74818 ** an ephemeral table might need to be generated from the RHS and then
74819 ** pX->iTable made to point to the ephermeral table instead of an
74820 ** existing table.
74821 **
74822 ** If the prNotFound parameter is 0, then the b-tree will be used to iterate
74823 ** through the set members, skipping any duplicates. In this case an
74824 ** epheremal table must be used unless the selected <column> is guaranteed
74825 ** to be unique - either because it is an INTEGER PRIMARY KEY or it
@@ -74846,12 +74912,11 @@
74912
74913 /* Check that the affinity that will be used to perform the
74914 ** comparison is the same as the affinity of the column. If
74915 ** it is not, it is not possible to use any index.
74916 */
74917 int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
 
74918
74919 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
74920 if( (pIdx->aiColumn[0]==iCol)
74921 && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
74922 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
@@ -75371,11 +75436,11 @@
75436
75437 /* The SQLITE_ColumnCache flag disables the column cache. This is used
75438 ** for testing only - to verify that SQLite always gets the same answer
75439 ** with and without the column cache.
75440 */
75441 if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
75442
75443 /* First replace any existing entry.
75444 **
75445 ** Actually, the way the column cache is currently used, we are guaranteed
75446 ** that the object will never already be in cache. Verify this guarantee.
@@ -75568,32 +75633,20 @@
75633 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
75634 */
75635 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
75636 int i;
75637 struct yColCache *p;
75638 assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
75639 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg-1);
75640 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
75641 int x = p->iReg;
75642 if( x>=iFrom && x<iFrom+nReg ){
75643 p->iReg += iTo-iFrom;
75644 }
75645 }
75646 }
75647
 
 
 
 
 
 
 
 
 
 
 
 
75648 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
75649 /*
75650 ** Return true if any register in the range iFrom..iTo (inclusive)
75651 ** is used as part of the column cache.
75652 **
@@ -76699,11 +76752,11 @@
76752 ** precomputed into registers or if they are inserted in-line.
76753 */
76754 SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
76755 Walker w;
76756 if( pParse->cookieGoto ) return;
76757 if( OptimizationDisabled(pParse->db, SQLITE_FactorOutConst) ) return;
76758 w.xExprCallback = evalConstExpr;
76759 w.xSelectCallback = 0;
76760 w.pParse = pParse;
76761 sqlite3WalkExpr(&w, pExpr);
76762 }
@@ -85180,11 +85233,13 @@
85233 sqlite3ColumnDefault(v, pTab, idx, -1);
85234 }
85235 }
85236 if( doMakeRec ){
85237 const char *zAff;
85238 if( pTab->pSelect
85239 || OptimizationDisabled(pParse->db, SQLITE_IdxRealAsInt)
85240 ){
85241 zAff = 0;
85242 }else{
85243 zAff = sqlite3IndexAffinityStr(v, pIdx);
85244 }
85245 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut);
@@ -92422,11 +92477,11 @@
92477 sqlite3VdbeChangeP5(v, (u8)i);
92478 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
92479 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
92480 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
92481 P4_DYNAMIC);
92482 sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
92483 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
92484 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
92485 sqlite3VdbeJumpHere(v, addr);
92486
92487 /* Make sure all the indices are constructed correctly.
@@ -92725,10 +92780,26 @@
92780 */
92781 if( sqlite3StrICmp(zLeft, "shrink_memory")==0 ){
92782 sqlite3_db_release_memory(db);
92783 }else
92784
92785 /*
92786 ** PRAGMA busy_timeout
92787 ** PRAGMA busy_timeout = N
92788 **
92789 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
92790 ** if one is set. If no busy handler or a different busy handler is set
92791 ** then 0 is returned. Setting the busy_timeout to 0 or negative
92792 ** disables the timeout.
92793 */
92794 if( sqlite3StrICmp(zLeft, "busy_timeout")==0 ){
92795 if( zRight ){
92796 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
92797 }
92798 returnSingleInt(pParse, "timeout", db->busyTimeout);
92799 }else
92800
92801 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
92802 /*
92803 ** Report the current state of file logs for all databases
92804 */
92805 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
@@ -92955,11 +93026,13 @@
93026 ** indicate success or failure.
93027 */
93028 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
93029 int rc;
93030 int i;
93031 #ifndef SQLITE_OMIT_DEPRECATED
93032 int size;
93033 #endif
93034 Table *pTab;
93035 Db *pDb;
93036 char const *azArg[4];
93037 int meta[5];
93038 InitData initData;
@@ -94210,10 +94283,23 @@
94283 return 0;
94284 }
94285 }
94286 #endif
94287
94288 /*
94289 ** An instance of the following object is used to record information about
94290 ** how to process the DISTINCT keyword, to simplify passing that information
94291 ** into the selectInnerLoop() routine.
94292 */
94293 typedef struct DistinctCtx DistinctCtx;
94294 struct DistinctCtx {
94295 u8 isTnct; /* True if the DISTINCT keyword is present */
94296 u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
94297 int tabTnct; /* Ephemeral table used for DISTINCT processing */
94298 int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
94299 };
94300
94301 /*
94302 ** This routine generates the code for the inside of the inner loop
94303 ** of a SELECT.
94304 **
94305 ** If srcTab and nColumn are both zero, then the pEList expressions
@@ -94226,11 +94312,11 @@
94312 Select *p, /* The complete select statement being coded */
94313 ExprList *pEList, /* List of values being extracted */
94314 int srcTab, /* Pull data from this table */
94315 int nColumn, /* Number of columns in the source table */
94316 ExprList *pOrderBy, /* If not NULL, sort results using this key */
94317 DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
94318 SelectDest *pDest, /* How to dispose of the results */
94319 int iContinue, /* Jump here to continue with next row */
94320 int iBreak /* Jump here to break out of the inner loop */
94321 ){
94322 Vdbe *v = pParse->pVdbe;
@@ -94242,11 +94328,11 @@
94328 int nResultCol; /* Number of result columns */
94329
94330 assert( v );
94331 if( NEVER(v==0) ) return;
94332 assert( pEList!=0 );
94333 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
94334 if( pOrderBy==0 && !hasDistinct ){
94335 codeOffset(v, p, iContinue);
94336 }
94337
94338 /* Pull the requested columns.
@@ -94282,11 +94368,59 @@
94368 ** part of the result.
94369 */
94370 if( hasDistinct ){
94371 assert( pEList!=0 );
94372 assert( pEList->nExpr==nColumn );
94373 switch( pDistinct->eTnctType ){
94374 case WHERE_DISTINCT_ORDERED: {
94375 VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
94376 int iJump; /* Jump destination */
94377 int regPrev; /* Previous row content */
94378
94379 /* Allocate space for the previous row */
94380 regPrev = pParse->nMem+1;
94381 pParse->nMem += nColumn;
94382
94383 /* Change the OP_OpenEphemeral coded earlier to an OP_Null
94384 ** sets the MEM_Cleared bit on the first register of the
94385 ** previous value. This will cause the OP_Ne below to always
94386 ** fail on the first iteration of the loop even if the first
94387 ** row is all NULLs.
94388 */
94389 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
94390 pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
94391 pOp->opcode = OP_Null;
94392 pOp->p1 = 1;
94393 pOp->p2 = regPrev;
94394
94395 iJump = sqlite3VdbeCurrentAddr(v) + nColumn;
94396 for(i=0; i<nColumn; i++){
94397 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
94398 if( i<nColumn-1 ){
94399 sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
94400 }else{
94401 sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
94402 }
94403 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
94404 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
94405 }
94406 assert( sqlite3VdbeCurrentAddr(v)==iJump );
94407 sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nColumn-1);
94408 break;
94409 }
94410
94411 case WHERE_DISTINCT_UNIQUE: {
94412 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
94413 break;
94414 }
94415
94416 default: {
94417 assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
94418 codeDistinct(pParse, pDistinct->tabTnct, iContinue, nColumn, regResult);
94419 break;
94420 }
94421 }
94422 if( pOrderBy==0 ){
94423 codeOffset(v, p, iContinue);
94424 }
94425 }
94426
@@ -94340,20 +94474,21 @@
94474 ** then there should be a single item on the stack. Write this
94475 ** item into the set table with bogus data.
94476 */
94477 case SRT_Set: {
94478 assert( nColumn==1 );
94479 pDest->affSdst =
94480 sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
94481 if( pOrderBy ){
94482 /* At first glance you would think we could optimize out the
94483 ** ORDER BY in this case since the order of entries in the set
94484 ** does not matter. But there might be a LIMIT clause, in which
94485 ** case the order does matter */
94486 pushOntoSorter(pParse, pOrderBy, p, regResult);
94487 }else{
94488 int r1 = sqlite3GetTempReg(pParse);
94489 sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
94490 sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
94491 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
94492 sqlite3ReleaseTempReg(pParse, r1);
94493 }
94494 break;
@@ -94616,11 +94751,12 @@
94751 break;
94752 }
94753 #ifndef SQLITE_OMIT_SUBQUERY
94754 case SRT_Set: {
94755 assert( nColumn==1 );
94756 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid,
94757 &pDest->affSdst, 1);
94758 sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
94759 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
94760 break;
94761 }
94762 case SRT_Mem: {
@@ -95453,11 +95589,11 @@
95589 iCont = sqlite3VdbeMakeLabel(v);
95590 computeLimitRegisters(pParse, p, iBreak);
95591 sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
95592 iStart = sqlite3VdbeCurrentAddr(v);
95593 selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
95594 0, 0, &dest, iCont, iBreak);
95595 sqlite3VdbeResolveLabel(v, iCont);
95596 sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
95597 sqlite3VdbeResolveLabel(v, iBreak);
95598 sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
95599 }
@@ -95531,11 +95667,11 @@
95667 r1 = sqlite3GetTempReg(pParse);
95668 iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
95669 sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
95670 sqlite3ReleaseTempReg(pParse, r1);
95671 selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
95672 0, 0, &dest, iCont, iBreak);
95673 sqlite3VdbeResolveLabel(v, iCont);
95674 sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
95675 sqlite3VdbeResolveLabel(v, iBreak);
95676 sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
95677 sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
@@ -95651,11 +95787,11 @@
95787 j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
95788 j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
95789 (char*)pKeyInfo, p4type);
95790 sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
95791 sqlite3VdbeJumpHere(v, j1);
95792 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
95793 sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
95794 }
95795 if( pParse->db->mallocFailed ) return 0;
95796
95797 /* Suppress the first OFFSET entries if there is an OFFSET clause
@@ -95686,14 +95822,14 @@
95822 ** item into the set table with bogus data.
95823 */
95824 case SRT_Set: {
95825 int r1;
95826 assert( pIn->nSdst==1 );
95827 pDest->affSdst =
95828 sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
95829 r1 = sqlite3GetTempReg(pParse);
95830 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
95831 sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
95832 sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
95833 sqlite3ReleaseTempReg(pParse, r1);
95834 break;
95835 }
@@ -96431,11 +96567,11 @@
96567
96568 /* Check to see if flattening is permitted. Return 0 if not.
96569 */
96570 assert( p!=0 );
96571 assert( p->pPrior==0 ); /* Unable to flatten compound queries */
96572 if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
96573 pSrc = p->pSrc;
96574 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
96575 pSubitem = &pSrc->a[iFrom];
96576 iParent = pSubitem->iCursor;
96577 pSub = pSubitem->pSelect;
@@ -97471,15 +97607,13 @@
97607 SrcList *pTabList; /* List of tables to select from */
97608 Expr *pWhere; /* The WHERE clause. May be NULL */
97609 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
97610 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
97611 Expr *pHaving; /* The HAVING clause. May be NULL */
 
 
97612 int rc = 1; /* Value to return from this function */
97613 int addrSortIndex; /* Address of an OP_OpenEphemeral instruction */
97614 DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
97615 AggInfo sAggInfo; /* Information used by aggregate queries */
97616 int iEnd; /* Address of the end of the query */
97617 sqlite3 *db; /* The database connection */
97618
97619 #ifndef SQLITE_OMIT_EXPLAIN
@@ -97601,11 +97735,11 @@
97735 pEList = p->pEList;
97736 #endif
97737 pWhere = p->pWhere;
97738 pGroupBy = p->pGroupBy;
97739 pHaving = p->pHaving;
97740 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
97741
97742 #ifndef SQLITE_OMIT_COMPOUND_SELECT
97743 /* If there is are a sequence of queries, do the earlier ones first.
97744 */
97745 if( p->pPrior ){
@@ -97636,11 +97770,11 @@
97770 ** an optimization - the correct answer should result regardless.
97771 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
97772 ** to disable this optimization for testing purposes.
97773 */
97774 if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
97775 && OptimizationEnabled(db, SQLITE_GroupByOrder) ){
97776 pOrderBy = 0;
97777 }
97778
97779 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
97780 ** if the select-list is the same as the ORDER BY list, then this query
@@ -97662,10 +97796,14 @@
97796 ){
97797 p->selFlags &= ~SF_Distinct;
97798 p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
97799 pGroupBy = p->pGroupBy;
97800 pOrderBy = 0;
97801 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
97802 ** the sDistinct.isTnct is still set. Hence, isTnct represents the
97803 ** original setting of the SF_Distinct flag, not the current setting */
97804 assert( sDistinct.isTnct );
97805 }
97806
97807 /* If there is an ORDER BY clause, then this sorting
97808 ** index might end up being unused if the data can be
97809 ** extracted in pre-sorted order. If that is the case, then the
@@ -97702,28 +97840,31 @@
97840 }
97841
97842 /* Open a virtual index to use for the distinct set.
97843 */
97844 if( p->selFlags & SF_Distinct ){
97845 sDistinct.tabTnct = pParse->nTab++;
97846 sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
97847 sDistinct.tabTnct, 0, 0,
97848 (char*)keyInfoFromExprList(pParse, p->pEList),
97849 P4_KEYINFO_HANDOFF);
97850 sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
97851 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
97852 }else{
97853 sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
97854 }
97855
 
97856 if( !isAgg && pGroupBy==0 ){
97857 /* No aggregate functions and no GROUP BY clause */
97858 ExprList *pDist = (sDistinct.isTnct ? p->pEList : 0);
97859
97860 /* Begin the database scan. */
97861 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, pDist, 0,0);
97862 if( pWInfo==0 ) goto select_end;
97863 if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
97864 if( pWInfo->eDistinct ) sDistinct.eTnctType = pWInfo->eDistinct;
97865 if( pOrderBy && pWInfo->nOBSat==pOrderBy->nExpr ) pOrderBy = 0;
97866
97867 /* If sorting index that was created by a prior OP_OpenEphemeral
97868 ** instruction ended up not being needed, then change the OP_OpenEphemeral
97869 ** into an OP_Noop.
97870 */
@@ -97730,63 +97871,20 @@
97871 if( addrSortIndex>=0 && pOrderBy==0 ){
97872 sqlite3VdbeChangeToNoop(v, addrSortIndex);
97873 p->addrOpenEphm[2] = -1;
97874 }
97875
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97876 /* Use the standard inner loop. */
97877 selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, &sDistinct, pDest,
97878 pWInfo->iContinue, pWInfo->iBreak);
97879
97880 /* End the database scan loop.
97881 */
97882 sqlite3WhereEnd(pWInfo);
97883 }else{
97884 /* This case when there exist aggregate functions or a GROUP BY clause
97885 ** or both */
97886 NameContext sNC; /* Name context for processing aggregate information */
97887 int iAMem; /* First Mem address for storing current GROUP BY */
97888 int iBMem; /* First Mem address for previous GROUP BY */
97889 int iUseFlag; /* Mem address holding flag indicating that at least
97890 ** one row of the input to the aggregator has been
@@ -97890,18 +97988,17 @@
97988 ** This might involve two separate loops with an OP_Sort in between, or
97989 ** it might be a single loop that uses an index to extract information
97990 ** in the right order to begin with.
97991 */
97992 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
97993 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 0, 0);
97994 if( pWInfo==0 ) goto select_end;
97995 if( pWInfo->nOBSat==pGroupBy->nExpr ){
97996 /* The optimizer is able to deliver rows in group by order so
97997 ** we do not have to sort. The OP_OpenEphemeral table will be
97998 ** cancelled later because we still need to use the pKeyInfo
97999 */
 
98000 groupBySort = 0;
98001 }else{
98002 /* Rows are coming out in undetermined order. We have to push
98003 ** each row into a sorting index, terminate the first loop,
98004 ** then loop over the sorting index in order to get the output
@@ -97911,11 +98008,12 @@
98008 int regRecord;
98009 int nCol;
98010 int nGroupBy;
98011
98012 explainTempTable(pParse,
98013 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
98014 "DISTINCT" : "GROUP BY");
98015
98016 groupBySort = 1;
98017 nGroupBy = pGroupBy->nExpr;
98018 nCol = nGroupBy + 1;
98019 j = nGroupBy+1;
@@ -98043,11 +98141,11 @@
98141 VdbeComment((v, "Groupby result generator entry point"));
98142 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
98143 finalizeAggFunctions(pParse, &sAggInfo);
98144 sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
98145 selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
98146 &sDistinct, pDest,
98147 addrOutputRow+1, addrSetAbort);
98148 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
98149 VdbeComment((v, "end groupby result generator"));
98150
98151 /* Generate a subroutine that will reset the group-by accumulator
@@ -98146,10 +98244,11 @@
98244 */
98245 ExprList *pMinMax = 0;
98246 u8 flag = minMaxQuery(p);
98247 if( flag ){
98248 assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
98249 assert( p->pEList->a[0].pExpr->x.pList->nExpr==1 );
98250 pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
98251 pDel = pMinMax;
98252 if( pMinMax && !db->mallocFailed ){
98253 pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
98254 pMinMax->a[0].pExpr->op = TK_COLUMN;
@@ -98159,17 +98258,18 @@
98258 /* This case runs if the aggregate has no GROUP BY clause. The
98259 ** processing is much simpler since there is only a single row
98260 ** of output.
98261 */
98262 resetAccumulator(pParse, &sAggInfo);
98263 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
98264 if( pWInfo==0 ){
98265 sqlite3ExprListDelete(db, pDel);
98266 goto select_end;
98267 }
98268 updateAccumulator(pParse, &sAggInfo);
98269 assert( pMinMax==0 || pMinMax->nExpr==1 );
98270 if( pWInfo->nOBSat>0 ){
98271 sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
98272 VdbeComment((v, "%s() by index",
98273 (flag==WHERE_ORDERBY_MIN?"min":"max")));
98274 }
98275 sqlite3WhereEnd(pWInfo);
@@ -98176,19 +98276,19 @@
98276 finalizeAggFunctions(pParse, &sAggInfo);
98277 }
98278
98279 pOrderBy = 0;
98280 sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
98281 selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, 0,
98282 pDest, addrEnd, addrEnd);
98283 sqlite3ExprListDelete(db, pDel);
98284 }
98285 sqlite3VdbeResolveLabel(v, addrEnd);
98286
98287 } /* endif aggregate query */
98288
98289 if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
98290 explainTempTable(pParse, "DISTINCT");
98291 }
98292
98293 /* If there is an ORDER BY clause, then we need to sort the results
98294 ** and send them to the callback one by one.
@@ -101789,13 +101889,14 @@
101889
101890 /*
101891 ** Trace output macros
101892 */
101893 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
101894 /***/ int sqlite3WhereTrace = 0;
101895 #endif
101896 #if defined(SQLITE_DEBUG) \
101897 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
101898 # define WHERETRACE(X) if(sqlite3WhereTrace) sqlite3DebugPrintf X
101899 #else
101900 # define WHERETRACE(X)
101901 #endif
101902
@@ -102031,10 +102132,32 @@
102132 #define WHERE_VIRTUALTABLE 0x08000000 /* Use virtual-table processing */
102133 #define WHERE_MULTI_OR 0x10000000 /* OR using multiple indices */
102134 #define WHERE_TEMP_INDEX 0x20000000 /* Uses an ephemeral index */
102135 #define WHERE_DISTINCT 0x40000000 /* Correct order for DISTINCT */
102136 #define WHERE_COVER_SCAN 0x80000000 /* Full scan of a covering index */
102137
102138 /*
102139 ** This module contains many separate subroutines that work together to
102140 ** find the best indices to use for accessing a particular table in a query.
102141 ** An instance of the following structure holds context information about the
102142 ** index search so that it can be more easily passed between the various
102143 ** routines.
102144 */
102145 typedef struct WhereBestIdx WhereBestIdx;
102146 struct WhereBestIdx {
102147 Parse *pParse; /* Parser context */
102148 WhereClause *pWC; /* The WHERE clause */
102149 struct SrcList_item *pSrc; /* The FROM clause term to search */
102150 Bitmask notReady; /* Mask of cursors not available */
102151 Bitmask notValid; /* Cursors not available for any purpose */
102152 ExprList *pOrderBy; /* The ORDER BY clause */
102153 ExprList *pDistinct; /* The select-list if query is DISTINCT */
102154 sqlite3_index_info **ppIdxInfo; /* Index information passed to xBestIndex */
102155 int i, n; /* Which loop is being coded; # of loops */
102156 WhereLevel *aLevel; /* Info about outer loops */
102157 WhereCost cost; /* Lowest cost query plan */
102158 };
102159
102160 /*
102161 ** Initialize a preallocated WhereClause structure.
102162 */
102163 static void whereClauseInit(
@@ -103174,26 +103297,22 @@
103297 */
103298 pTerm->prereqRight |= extraRight;
103299 }
103300
103301 /*
103302 ** Return TRUE if the given index is UNIQUE and all columns past the
103303 ** first nSkip columns are NOT NULL.
103304 */
103305 static int indexIsUniqueNotNull(Index *pIdx, int nSkip){
103306 Table *pTab = pIdx->pTable;
103307 int i;
103308 if( pIdx->onError==OE_None ) return 0;
103309 for(i=nSkip; i<pIdx->nColumn; i++){
103310 int j = pIdx->aiColumn[i];
103311 if( j>=0 && pTab->aCol[j].notNull==0 ) return 0;
103312 }
103313 return 1;
 
 
 
 
103314 }
103315
103316 /*
103317 ** This function searches the expression list passed as the second argument
103318 ** for an expression of type TK_COLUMN that refers to the same column and
@@ -103355,47 +103474,63 @@
103474 return 0;
103475 }
103476
103477 /*
103478 ** This routine decides if pIdx can be used to satisfy the ORDER BY
103479 ** clause, either in whole or in part. The return value is the
103480 ** cumulative number of terms in the ORDER BY clause that are satisfied
103481 ** by the index pIdx and other indices in outer loops.
103482 **
103483 ** The table being queried has a cursor number of "base". pIdx is the
103484 ** index that is postulated for use to access the table.
 
103485 **
103486 ** nEqCol is the number of columns of pIdx that are used as equality
103487 ** constraints and where the other side of the == is an ordered column
103488 ** or constant. An "order column" in the previous sentence means a column
103489 ** in table from an outer loop whose values will always appear in the
103490 ** correct order due to othre index, or because the outer loop generates
103491 ** a unique result. Any of the first nEqCol columns of pIdx may be missing
103492 ** from the ORDER BY clause and the match can still be a success.
103493 **
103494 ** The *pbRev value is set to 0 order 1 depending on whether or not
103495 ** pIdx should be run in the forward order or in reverse order.
 
 
 
103496 */
103497 static int isSortingIndex(
103498 WhereBestIdx *p, /* Best index search context */
103499 Index *pIdx, /* The index we are testing */
103500 int base, /* Cursor number for the table to be sorted */
103501 int nEqCol, /* Number of index columns with ordered == constraints */
103502 int wsFlags, /* Index usages flags */
103503 int bOuterRev, /* True if outer loops scan in reverse order */
103504 int *pbRev /* Set to 1 for reverse-order scan of pIdx */
 
103505 ){
103506 int i; /* Number of pIdx terms used */
103507 int j; /* Number of ORDER BY terms satisfied */
103508 int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
103509 int nTerm; /* Number of ORDER BY terms */
103510 struct ExprList_item *pTerm; /* A term of the ORDER BY clause */
103511 ExprList *pOrderBy; /* The ORDER BY clause */
103512 Parse *pParse = p->pParse; /* Parser context */
103513 sqlite3 *db = pParse->db; /* Database connection */
103514 int nPriorSat; /* ORDER BY terms satisfied by outer loops */
103515 int seenRowid = 0; /* True if an ORDER BY rowid term is seen */
103516 int nEqOneRow; /* Idx columns that ref unique values */
103517
103518 if( p->i==0 ){
103519 nPriorSat = 0;
103520 nEqOneRow = nEqCol;
103521 }else{
103522 if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
103523 nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
103524 sortOrder = bOuterRev;
103525 nEqOneRow = 0;
103526 }
103527 if( p->i>0 && nEqCol==0 /*&& !allOuterLoopsUnique(p)*/ ) return nPriorSat;
103528 pOrderBy = p->pOrderBy;
103529 if( !pOrderBy ) return nPriorSat;
103530 if( wsFlags & WHERE_COLUMN_IN ) return nPriorSat;
103531 if( pIdx->bUnordered ) return nPriorSat;
103532 nTerm = pOrderBy->nExpr;
103533 assert( nTerm>0 );
103534
103535 /* Argument pIdx must either point to a 'real' named index structure,
103536 ** or an index structure allocated on the stack by bestBtreeIndex() to
@@ -103408,11 +103543,11 @@
103543 ** Note that indices have pIdx->nColumn regular columns plus
103544 ** one additional column containing the rowid. The rowid column
103545 ** of the index is also allowed to match against the ORDER BY
103546 ** clause.
103547 */
103548 for(i=0,j=nPriorSat,pTerm=&pOrderBy->a[j]; j<nTerm && i<=pIdx->nColumn; i++){
103549 Expr *pExpr; /* The expression of the ORDER BY pTerm */
103550 CollSeq *pColl; /* The collating sequence of pExpr */
103551 int termSortOrder; /* Sort order for this term */
103552 int iColumn; /* The i-th column of the index. -1 for rowid */
103553 int iSortOrder; /* 1 for DESC, 0 for ASC on the i-th index term */
@@ -103452,68 +103587,53 @@
103587 break;
103588 }else{
103589 /* If an index column fails to match and is not constrained by ==
103590 ** then the index cannot satisfy the ORDER BY constraint.
103591 */
103592 return nPriorSat;
103593 }
103594 }
103595 assert( pIdx->aSortOrder!=0 || iColumn==-1 );
103596 assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
103597 assert( iSortOrder==0 || iSortOrder==1 );
103598 termSortOrder = iSortOrder ^ pTerm->sortOrder;
103599 if( i>nEqOneRow ){
103600 if( termSortOrder!=sortOrder ){
103601 /* Indices can only be used if all ORDER BY terms past the
103602 ** equality constraints are all either DESC or ASC. */
103603 break;
103604 }
103605 }else{
103606 sortOrder = termSortOrder;
103607 }
103608 j++;
103609 pTerm++;
103610 if( iColumn<0 ){
103611 seenRowid = 1;
103612 break;
103613 }
103614 }
103615 *pbRev = sortOrder;
103616
103617 /* If there was an "ORDER BY rowid" term that matched, or it is only
103618 ** possible for a single row from this table to match, then skip over
103619 ** any additional ORDER BY terms dealing with this table.
103620 */
103621 if( seenRowid ||
103622 ( (wsFlags & WHERE_COLUMN_NULL)==0
103623 && i>=pIdx->nColumn
103624 && indexIsUniqueNotNull(pIdx, nEqCol)
103625 )
 
 
 
 
103626 ){
103627 /* Advance j over additional ORDER BY terms associated with base */
103628 WhereMaskSet *pMS = p->pWC->pMaskSet;
103629 Bitmask m = ~getMask(pMS, base);
103630 while( j<nTerm && (exprTableUsage(pMS, pOrderBy->a[j].pExpr)&m)==0 ){
103631 j++;
103632 }
103633 }
103634 return j;
 
 
 
 
 
 
 
 
 
 
 
103635 }
103636
103637 /*
103638 ** Prepare a crude estimate of the logarithm of the input value.
103639 ** The results need not be exact. This is only used for estimating
@@ -103576,35 +103696,27 @@
103696 #endif
103697
103698 /*
103699 ** Required because bestIndex() is called by bestOrClauseIndex()
103700 */
103701 static void bestIndex(WhereBestIdx*);
 
 
103702
103703 /*
103704 ** This routine attempts to find an scanning strategy that can be used
103705 ** to optimize an 'OR' expression that is part of a WHERE clause.
103706 **
103707 ** The table associated with FROM clause term pSrc may be either a
103708 ** regular B-Tree table or a virtual table.
103709 */
103710 static void bestOrClauseIndex(WhereBestIdx *p){
 
 
 
 
 
 
 
 
103711 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
103712 WhereClause *pWC = p->pWC; /* The WHERE clause */
103713 struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
103714 const int iCur = pSrc->iCursor; /* The cursor of the table */
103715 const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur); /* Bitmask for pSrc */
103716 WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm]; /* End of pWC->a[] */
103717 WhereTerm *pTerm; /* A single term of the WHERE clause */
103718
103719 /* The OR-clause optimization is disallowed if the INDEXED BY or
103720 ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
103721 if( pSrc->notIndexed || pSrc->pIndex!=0 ){
103722 return;
@@ -103614,66 +103726,71 @@
103726 }
103727
103728 /* Search the WHERE clause terms for a usable WO_OR term. */
103729 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
103730 if( pTerm->eOperator==WO_OR
103731 && ((pTerm->prereqAll & ~maskSrc) & p->notReady)==0
103732 && (pTerm->u.pOrInfo->indexable & maskSrc)!=0
103733 ){
103734 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
103735 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
103736 WhereTerm *pOrTerm;
103737 int flags = WHERE_MULTI_OR;
103738 double rTotal = 0;
103739 double nRow = 0;
103740 Bitmask used = 0;
103741 WhereBestIdx sBOI;
103742
103743 sBOI = *p;
103744 sBOI.pOrderBy = 0;
103745 sBOI.pDistinct = 0;
103746 sBOI.ppIdxInfo = 0;
103747 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
 
103748 WHERETRACE(("... Multi-index OR testing for term %d of %d....\n",
103749 (pOrTerm - pOrWC->a), (pTerm - pWC->a)
103750 ));
103751 if( pOrTerm->eOperator==WO_AND ){
103752 sBOI.pWC = &pOrTerm->u.pAndInfo->wc;
103753 bestIndex(&sBOI);
103754 }else if( pOrTerm->leftCursor==iCur ){
103755 WhereClause tempWC;
103756 tempWC.pParse = pWC->pParse;
103757 tempWC.pMaskSet = pWC->pMaskSet;
103758 tempWC.pOuter = pWC;
103759 tempWC.op = TK_AND;
103760 tempWC.a = pOrTerm;
103761 tempWC.wctrlFlags = 0;
103762 tempWC.nTerm = 1;
103763 sBOI.pWC = &tempWC;
103764 bestIndex(&sBOI);
103765 }else{
103766 continue;
103767 }
103768 rTotal += sBOI.cost.rCost;
103769 nRow += sBOI.cost.plan.nRow;
103770 used |= sBOI.cost.used;
103771 if( rTotal>=p->cost.rCost ) break;
103772 }
103773
103774 /* If there is an ORDER BY clause, increase the scan cost to account
103775 ** for the cost of the sort. */
103776 if( p->pOrderBy!=0 ){
103777 WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
103778 rTotal, rTotal+nRow*estLog(nRow)));
103779 rTotal += nRow*estLog(nRow);
103780 }
103781
103782 /* If the cost of scanning using this OR term for optimization is
103783 ** less than the current cost stored in pCost, replace the contents
103784 ** of pCost. */
103785 WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
103786 if( rTotal<p->cost.rCost ){
103787 p->cost.rCost = rTotal;
103788 p->cost.used = used;
103789 p->cost.plan.nRow = nRow;
103790 p->cost.plan.wsFlags = flags;
103791 p->cost.plan.u.pTerm = pTerm;
103792 }
103793 }
103794 }
103795 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
103796 }
@@ -103706,19 +103823,16 @@
103823 ** possible to construct a transient index that would perform better
103824 ** than a full table scan even when the cost of constructing the index
103825 ** is taken into account, then alter the query plan to use the
103826 ** transient index.
103827 */
103828 static void bestAutomaticIndex(WhereBestIdx *p){
103829 Parse *pParse = p->pParse; /* The parsing context */
103830 WhereClause *pWC = p->pWC; /* The WHERE clause */
103831 struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
103832 double nTableRow; /* Rows in the input table */
103833 double logN; /* log(nTableRow) */
 
 
 
103834 double costTempIdx; /* per-query cost of the transient index */
103835 WhereTerm *pTerm; /* A single term of the WHERE clause */
103836 WhereTerm *pWCEnd; /* End of pWC->a[] */
103837 Table *pTable; /* Table tht might be indexed */
103838
@@ -103728,11 +103842,11 @@
103842 }
103843 if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){
103844 /* Automatic indices are disabled at run-time */
103845 return;
103846 }
103847 if( (p->cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){
103848 /* We already have some kind of index in use for this query. */
103849 return;
103850 }
103851 if( pSrc->notIndexed ){
103852 /* The NOT INDEXED clause appears in the SQL. */
@@ -103746,32 +103860,32 @@
103860 assert( pParse->nQueryLoop >= (double)1 );
103861 pTable = pSrc->pTab;
103862 nTableRow = pTable->nRowEst;
103863 logN = estLog(nTableRow);
103864 costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
103865 if( costTempIdx>=p->cost.rCost ){
103866 /* The cost of creating the transient table would be greater than
103867 ** doing the full table scan */
103868 return;
103869 }
103870
103871 /* Search for any equality comparison term */
103872 pWCEnd = &pWC->a[pWC->nTerm];
103873 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
103874 if( termCanDriveIndex(pTerm, pSrc, p->notReady) ){
103875 WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
103876 p->cost.rCost, costTempIdx));
103877 p->cost.rCost = costTempIdx;
103878 p->cost.plan.nRow = logN + 1;
103879 p->cost.plan.wsFlags = WHERE_TEMP_INDEX;
103880 p->cost.used = pTerm->prereqRight;
103881 break;
103882 }
103883 }
103884 }
103885 #else
103886 # define bestAutomaticIndex(A) /* no-op */
103887 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
103888
103889
103890 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
103891 /*
@@ -103928,16 +104042,15 @@
104042 /*
104043 ** Allocate and populate an sqlite3_index_info structure. It is the
104044 ** responsibility of the caller to eventually release the structure
104045 ** by passing the pointer returned by this function to sqlite3_free().
104046 */
104047 static sqlite3_index_info *allocateIndexInfo(WhereBestIdx *p){
104048 Parse *pParse = p->pParse;
104049 WhereClause *pWC = p->pWC;
104050 struct SrcList_item *pSrc = p->pSrc;
104051 ExprList *pOrderBy = p->pOrderBy;
 
104052 int i, j;
104053 int nTerm;
104054 struct sqlite3_index_constraint *pIdxCons;
104055 struct sqlite3_index_orderby *pIdxOrderBy;
104056 struct sqlite3_index_constraint_usage *pUsage;
@@ -103963,16 +104076,17 @@
104076 ** virtual table then allocate space for the aOrderBy part of
104077 ** the sqlite3_index_info structure.
104078 */
104079 nOrderBy = 0;
104080 if( pOrderBy ){
104081 int n = pOrderBy->nExpr;
104082 for(i=0; i<n; i++){
104083 Expr *pExpr = pOrderBy->a[i].pExpr;
104084 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
104085 }
104086 if( i==n){
104087 nOrderBy = n;
104088 }
104089 }
104090
104091 /* Allocate the sqlite3_index_info structure
104092 */
@@ -104092,20 +104206,14 @@
104206 ** invocations. The sqlite3_index_info structure is also used when
104207 ** code is generated to access the virtual table. The whereInfoDelete()
104208 ** routine takes care of freeing the sqlite3_index_info structure after
104209 ** everybody has finished with it.
104210 */
104211 static void bestVirtualIndex(WhereBestIdx *p){
104212 Parse *pParse = p->pParse; /* The parsing context */
104213 WhereClause *pWC = p->pWC; /* The WHERE clause */
104214 struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
 
 
 
 
 
 
104215 Table *pTab = pSrc->pTab;
104216 sqlite3_index_info *pIdxInfo;
104217 struct sqlite3_index_constraint *pIdxCons;
104218 struct sqlite3_index_constraint_usage *pUsage;
104219 WhereTerm *pTerm;
@@ -104115,19 +104223,19 @@
104223
104224 /* Make sure wsFlags is initialized to some sane value. Otherwise, if the
104225 ** malloc in allocateIndexInfo() fails and this function returns leaving
104226 ** wsFlags in an uninitialized state, the caller may behave unpredictably.
104227 */
104228 memset(&p->cost, 0, sizeof(p->cost));
104229 p->cost.plan.wsFlags = WHERE_VIRTUALTABLE;
104230
104231 /* If the sqlite3_index_info structure has not been previously
104232 ** allocated and initialized, then allocate and initialize it now.
104233 */
104234 pIdxInfo = *p->ppIdxInfo;
104235 if( pIdxInfo==0 ){
104236 *p->ppIdxInfo = pIdxInfo = allocateIndexInfo(p);
104237 }
104238 if( pIdxInfo==0 ){
104239 return;
104240 }
104241
@@ -104168,11 +104276,11 @@
104276 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
104277 pUsage = pIdxInfo->aConstraintUsage;
104278 for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
104279 j = pIdxCons->iTermOffset;
104280 pTerm = &pWC->a[j];
104281 pIdxCons->usable = (pTerm->prereqRight&p->notReady) ? 0 : 1;
104282 }
104283 memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
104284 if( pIdxInfo->needToFreeIdxStr ){
104285 sqlite3_free(pIdxInfo->idxStr);
104286 }
@@ -104181,11 +104289,11 @@
104289 pIdxInfo->needToFreeIdxStr = 0;
104290 pIdxInfo->orderByConsumed = 0;
104291 /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
104292 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
104293 nOrderBy = pIdxInfo->nOrderBy;
104294 if( !p->pOrderBy ){
104295 pIdxInfo->nOrderBy = 0;
104296 }
104297
104298 if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
104299 return;
@@ -104192,20 +104300,20 @@
104300 }
104301
104302 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
104303 for(i=0; i<pIdxInfo->nConstraint; i++){
104304 if( pUsage[i].argvIndex>0 ){
104305 p->cost.used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
104306 }
104307 }
104308
104309 /* If there is an ORDER BY clause, and the selected virtual table index
104310 ** does not satisfy it, increase the cost of the scan accordingly. This
104311 ** matches the processing for non-virtual tables in bestBtreeIndex().
104312 */
104313 rCost = pIdxInfo->estimatedCost;
104314 if( p->pOrderBy && pIdxInfo->orderByConsumed==0 ){
104315 rCost += estLog(rCost)*rCost;
104316 }
104317
104318 /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
104319 ** inital value of lowestCost in this loop. If it is, then the
@@ -104213,25 +104321,25 @@
104321 **
104322 ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT
104323 ** is defined.
104324 */
104325 if( (SQLITE_BIG_DBL/((double)2))<rCost ){
104326 p->cost.rCost = (SQLITE_BIG_DBL/((double)2));
104327 }else{
104328 p->cost.rCost = rCost;
104329 }
104330 p->cost.plan.u.pVtabIdx = pIdxInfo;
104331 if( pIdxInfo->orderByConsumed ){
104332 p->cost.plan.wsFlags |= WHERE_ORDERBY;
104333 }
104334 p->cost.plan.nEq = 0;
104335 pIdxInfo->nOrderBy = nOrderBy;
104336
104337 /* Try to find a more efficient access pattern by using multiple indexes
104338 ** to optimize an OR expression within the WHERE clause.
104339 */
104340 bestOrClauseIndex(p);
104341 }
104342 #endif /* SQLITE_OMIT_VIRTUALTABLE */
104343
104344 #ifdef SQLITE_ENABLE_STAT3
104345 /*
@@ -104626,15 +104734,88 @@
104734 }
104735 return rc;
104736 }
104737 #endif /* defined(SQLITE_ENABLE_STAT3) */
104738
104739 /*
104740 ** Check to see if column iCol of the table with cursor iTab will appear
104741 ** in sorted order according to the current query plan. Return true if
104742 ** it will and false if not.
104743 **
104744 ** If *pbRev is initially 2 (meaning "unknown") then set *pbRev to the
104745 ** sort order of iTab.iCol. If *pbRev is 0 or 1 but does not match
104746 ** the sort order of iTab.iCol, then consider the column to be unordered.
104747 */
104748 static int isOrderedColumn(WhereBestIdx *p, int iTab, int iCol, int *pbRev){
104749 int i, j;
104750 WhereLevel *pLevel = &p->aLevel[p->i-1];
104751 Index *pIdx;
104752 u8 sortOrder;
104753 for(i=p->i-1; i>=0; i--, pLevel--){
104754 if( pLevel->iTabCur!=iTab ) continue;
104755 if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
104756 pIdx = pLevel->plan.u.pIdx;
104757 if( iCol<0 ){
104758 sortOrder = 0;
104759 testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
104760 }else{
104761 for(j=0; j<pIdx->nColumn; j++){
104762 if( iCol==pIdx->aiColumn[j] ) break;
104763 }
104764 if( j>=pIdx->nColumn ) return 0;
104765 sortOrder = pIdx->aSortOrder[j];
104766 testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
104767 }
104768 }else{
104769 if( iCol!=(-1) ) return 0;
104770 sortOrder = 0;
104771 testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
104772 }
104773 if( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ){
104774 assert( sortOrder==0 || sortOrder==1 );
104775 testcase( sortOrder==1 );
104776 sortOrder = 1 - sortOrder;
104777 }
104778 if( *pbRev==2 ){
104779 *pbRev = sortOrder;
104780 return 1;
104781 }
104782 return (*pbRev==sortOrder);
104783 }
104784 return 0;
104785 }
104786
104787 /*
104788 ** pTerm is an == constraint. Check to see if the other side of
104789 ** the == is a constant or a value that is guaranteed to be ordered
104790 ** by outer loops. Return 1 if pTerm is ordered, and 0 if not.
104791 */
104792 static int isOrderedTerm(WhereBestIdx *p, WhereTerm *pTerm, int *pbRev){
104793 Expr *pExpr = pTerm->pExpr;
104794 assert( pExpr->op==TK_EQ );
104795 assert( pExpr->pLeft!=0 && pExpr->pLeft->op==TK_COLUMN );
104796 assert( pExpr->pRight!=0 );
104797 if( p->i==0 ){
104798 return 1; /* All == are ordered in the outer loop */
104799 }
104800 if( pTerm->prereqRight==0 ){
104801 return 1; /* RHS of the == is a constant */
104802 }
104803 if( pExpr->pRight->op==TK_COLUMN
104804 && isOrderedColumn(p, pExpr->pRight->iTable, pExpr->pRight->iColumn, pbRev)
104805 ){
104806 return 1;
104807 }
104808
104809 /* If we cannot prove that the constraint is ordered, assume it is not */
104810 return 0;
104811 }
104812
104813
104814 /*
104815 ** Find the best query plan for accessing a particular table. Write the
104816 ** best query plan and its cost into the p->cost.
 
104817 **
104818 ** The lowest cost plan wins. The cost is an estimate of the amount of
104819 ** CPU and disk I/O needed to process the requested result.
104820 ** Factors that influence cost include:
104821 **
@@ -104655,33 +104836,27 @@
104836 ** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table
104837 ** in the SELECT statement, then no indexes are considered. However, the
104838 ** selected plan may still take advantage of the built-in rowid primary key
104839 ** index.
104840 */
104841 static void bestBtreeIndex(WhereBestIdx *p){
104842 Parse *pParse = p->pParse; /* The parsing context */
104843 WhereClause *pWC = p->pWC; /* The WHERE clause */
104844 struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
 
 
 
 
 
 
104845 int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
104846 Index *pProbe; /* An index we are evaluating */
104847 Index *pIdx; /* Copy of pProbe, or zero for IPK index */
104848 int eqTermMask; /* Current mask of valid equality operators */
104849 int idxEqTermMask; /* Index mask of valid equality operators */
104850 Index sPk; /* A fake index object for the primary key */
104851 tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
104852 int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
104853 int wsFlagMask; /* Allowed flags in p->cost.plan.wsFlag */
104854
104855 /* Initialize the cost to a worst-case value */
104856 memset(&p->cost, 0, sizeof(p->cost));
104857 p->cost.rCost = SQLITE_BIG_DBL;
104858
104859 /* If the pSrc table is the right table of a LEFT JOIN then we may not
104860 ** use an index to satisfy IS NULL constraints on that table. This is
104861 ** because columns might end up being NULL if the table does not match -
104862 ** a circumstance which the index cannot help us discover. Ticket #2177.
@@ -104730,11 +104905,11 @@
104905 for(; pProbe; pIdx=pProbe=pProbe->pNext){
104906 const tRowcnt * const aiRowEst = pProbe->aiRowEst;
104907 double cost; /* Cost of using pProbe */
104908 double nRow; /* Estimated number of rows in result set */
104909 double log10N = (double)1; /* base-10 logarithm of nRow (inexact) */
104910 int bRev = 2; /* 0=forward scan. 1=reverse. 2=undecided */
104911 int wsFlags = 0;
104912 Bitmask used = 0;
104913
104914 /* The following variables are populated based on the properties of
104915 ** index being evaluated. They are then used to determine the expected
@@ -104763,10 +104938,14 @@
104938 **
104939 ** If there exists a WHERE term of the form "x IN (SELECT ...)", then
104940 ** the sub-select is assumed to return 25 rows for the purposes of
104941 ** determining nInMul.
104942 **
104943 ** nOrdered:
104944 ** The number of equality terms that are constrainted by outer loop
104945 ** variables that are well-ordered.
104946 **
104947 ** bInEst:
104948 ** Set to true if there was at least one "x IN (SELECT ...)" term used
104949 ** in determining the value of nInMul. Note that the RHS of the
104950 ** IN operator must be a SELECT, not a value list, for this variable
104951 ** to be true.
@@ -104781,10 +104960,14 @@
104960 ** bSort:
104961 ** Boolean. True if there is an ORDER BY clause that will require an
104962 ** external sort (i.e. scanning the index being evaluated will not
104963 ** correctly order records).
104964 **
104965 ** bDistinct:
104966 ** Boolean. True if there is a DISTINCT clause that will require an
104967 ** external btree.
104968 **
104969 ** bLookup:
104970 ** Boolean. True if a table lookup is required for each index entry
104971 ** visited. In other words, true if this is not a covering index.
104972 ** This is always false for the rowid primary key index of a table.
104973 ** For other indexes, it is true unless all the columns of the table
@@ -104797,26 +104980,33 @@
104980 **
104981 ** SELECT a, b FROM tbl WHERE a = 1;
104982 ** SELECT a, b, c FROM tbl WHERE a = 1;
104983 */
104984 int nEq; /* Number of == or IN terms matching index */
104985 int nOrdered; /* Number of ordered terms matching index */
104986 int bInEst = 0; /* True if "x IN (SELECT...)" seen */
104987 int nInMul = 1; /* Number of distinct equalities to lookup */
104988 double rangeDiv = (double)1; /* Estimated reduction in search space */
104989 int nBound = 0; /* Number of range constraints seen */
104990 int bSort; /* True if external sort required */
104991 int bDist; /* True if index cannot help with DISTINCT */
104992 int bLookup = 0; /* True if not a covering index */
104993 int nOBSat = 0; /* Number of ORDER BY terms satisfied */
104994 int nOrderBy; /* Number of ORDER BY terms */
104995 WhereTerm *pTerm; /* A single term of the WHERE clause */
104996 #ifdef SQLITE_ENABLE_STAT3
104997 WhereTerm *pFirstTerm = 0; /* First term matching the index */
104998 #endif
104999
105000 nOrderBy = p->pOrderBy ? p->pOrderBy->nExpr : 0;
105001 bSort = nOrderBy>0 && (p->i==0 || p->aLevel[p->i-1].plan.nOBSat<nOrderBy);
105002 bDist = p->i==0 && p->pDistinct!=0;
105003
105004 /* Determine the values of nEq and nInMul */
105005 for(nEq=nOrdered=0; nEq<pProbe->nColumn; nEq++){
105006 int j = pProbe->aiColumn[nEq];
105007 pTerm = findTerm(pWC, iCur, j, p->notReady, eqTermMask, pIdx);
105008 if( pTerm==0 ) break;
105009 wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
105010 testcase( pTerm->pWC!=pWC );
105011 if( pTerm->eOperator & WO_IN ){
105012 Expr *pExpr = pTerm->pExpr;
@@ -104829,10 +105019,13 @@
105019 /* "x IN (value, value, ...)" */
105020 nInMul *= pExpr->x.pList->nExpr;
105021 }
105022 }else if( pTerm->eOperator & WO_ISNULL ){
105023 wsFlags |= WHERE_COLUMN_NULL;
105024 if( nEq==nOrdered ) nOrdered++;
105025 }else if( bSort && nEq==nOrdered && isOrderedTerm(p, pTerm, &bRev) ){
105026 nOrdered++;
105027 }
105028 #ifdef SQLITE_ENABLE_STAT3
105029 if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
105030 #endif
105031 used |= pTerm->prereqRight;
@@ -104853,13 +105046,14 @@
105046 if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
105047 wsFlags |= WHERE_UNIQUE;
105048 }
105049 }else if( pProbe->bUnordered==0 ){
105050 int j = (nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[nEq]);
105051 if( findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
105052 WhereTerm *pTop, *pBtm;
105053 pTop = findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE, pIdx);
105054 pBtm = findTerm(pWC, iCur, j, p->notReady, WO_GT|WO_GE, pIdx);
105055 whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
105056 if( pTop ){
105057 nBound = 1;
105058 wsFlags |= WHERE_TOP_LIMIT;
105059 used |= pTop->prereqRight;
@@ -104877,22 +105071,29 @@
105071
105072 /* If there is an ORDER BY clause and the index being considered will
105073 ** naturally scan rows in the required order, set the appropriate flags
105074 ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
105075 ** will scan rows in a different order, set the bSort variable. */
105076 assert( bRev>=0 && bRev<=2 );
105077 if( bSort ){
105078 testcase( bRev==0 );
105079 testcase( bRev==1 );
105080 testcase( bRev==2 );
105081 nOBSat = isSortingIndex(p, pProbe, iCur, nOrdered,
105082 wsFlags, bRev&1, &bRev);
105083 if( nOrderBy==nOBSat ){
105084 bSort = 0;
105085 wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
105086 }
105087 if( bRev & 1 ) wsFlags |= WHERE_REVERSE;
105088 }
105089
105090 /* If there is a DISTINCT qualifier and this index will scan rows in
105091 ** order of the DISTINCT expressions, clear bDist and set the appropriate
105092 ** flags in wsFlags. */
105093 if( bDist
105094 && isDistinctIndex(pParse, pWC, pProbe, iCur, p->pDistinct, nEq)
105095 && (wsFlags & WHERE_COLUMN_IN)==0
105096 ){
105097 bDist = 0;
105098 wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
105099 }
@@ -104965,16 +105166,14 @@
105166 ** The ANALYZE command and the sqlite_stat1 and sqlite_stat3 tables do
105167 ** not give us data on the relative sizes of table and index records.
105168 ** So this computation assumes table records are about twice as big
105169 ** as index records
105170 */
105171 if( (wsFlags&~WHERE_REVERSE)==WHERE_IDX_ONLY
105172 && (pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
105173 && sqlite3GlobalConfig.bUseCis
105174 && OptimizationEnabled(pParse->db, SQLITE_CoverIdxScan)
 
 
105175 ){
105176 /* This index is not useful for indexing, but it is a covering index.
105177 ** A full-scan of the index might be a little faster than a full-scan
105178 ** of the table, so give this case a cost slightly less than a table
105179 ** scan. */
@@ -105024,11 +105223,11 @@
105223 ** adds C*N*log10(N) to the cost, where N is the number of rows to be
105224 ** sorted and C is a factor between 1.95 and 4.3. We will split the
105225 ** difference and select C of 3.0.
105226 */
105227 if( bSort ){
105228 cost += nRow*estLog(nRow*(nOrderBy - nOBSat)/nOrderBy)*3;
105229 }
105230 if( bDist ){
105231 cost += nRow*estLog(nRow)*3;
105232 }
105233
@@ -105048,20 +105247,20 @@
105247 ** tables that are not in outer loops. If notReady is used here instead
105248 ** of notValid, then a optimal index that depends on inner joins loops
105249 ** might be selected even when there exists an optimal index that has
105250 ** no such dependency.
105251 */
105252 if( nRow>2 && cost<=p->cost.rCost ){
105253 int k; /* Loop counter */
105254 int nSkipEq = nEq; /* Number of == constraints to skip */
105255 int nSkipRange = nBound; /* Number of < constraints to skip */
105256 Bitmask thisTab; /* Bitmap for pSrc */
105257
105258 thisTab = getMask(pWC->pMaskSet, iCur);
105259 for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){
105260 if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
105261 if( (pTerm->prereqAll & p->notValid)!=thisTab ) continue;
105262 if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
105263 if( nSkipEq ){
105264 /* Ignore the first nEq equality matches since the index
105265 ** has already accounted for these */
105266 nSkipEq--;
@@ -105092,29 +105291,32 @@
105291 if( nRow<2 ) nRow = 2;
105292 }
105293
105294
105295 WHERETRACE((
105296 "%s(%s):\n"
105297 " nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%08x\n"
105298 " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f\n"
105299 " used=0x%llx nOrdered=%d nOBSat=%d\n",
105300 pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
105301 nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
105302 p->notReady, log10N, nRow, cost, used, nOrdered, nOBSat
105303 ));
105304
105305 /* If this index is the best we have seen so far, then record this
105306 ** index and its cost in the pCost structure.
105307 */
105308 if( (!pIdx || wsFlags)
105309 && (cost<p->cost.rCost || (cost<=p->cost.rCost && nRow<p->cost.plan.nRow))
105310 ){
105311 p->cost.rCost = cost;
105312 p->cost.used = used;
105313 p->cost.plan.nRow = nRow;
105314 p->cost.plan.wsFlags = (wsFlags&wsFlagMask);
105315 p->cost.plan.nEq = nEq;
105316 p->cost.plan.nOBSat = nOBSat;
105317 p->cost.plan.u.pIdx = pIdx;
105318 }
105319
105320 /* If there was an INDEXED BY clause, then only that one index is
105321 ** considered. */
105322 if( pSrc->pIndex ) break;
@@ -105127,58 +105329,57 @@
105329 /* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
105330 ** is set, then reverse the order that the index will be scanned
105331 ** in. This is used for application testing, to help find cases
105332 ** where application behaviour depends on the (undefined) order that
105333 ** SQLite outputs rows in in the absence of an ORDER BY clause. */
105334 if( !p->pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
105335 p->cost.plan.wsFlags |= WHERE_REVERSE;
105336 }
105337
105338 assert( p->pOrderBy || (p->cost.plan.wsFlags&WHERE_ORDERBY)==0 );
105339 assert( p->cost.plan.u.pIdx==0 || (p->cost.plan.wsFlags&WHERE_ROWID_EQ)==0 );
105340 assert( pSrc->pIndex==0
105341 || p->cost.plan.u.pIdx==0
105342 || p->cost.plan.u.pIdx==pSrc->pIndex
105343 );
105344
105345 WHERETRACE(("best index is: %s\n",
105346 ((p->cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" :
105347 p->cost.plan.u.pIdx ? p->cost.plan.u.pIdx->zName : "ipk")
105348 ));
105349
105350 bestOrClauseIndex(p);
105351 bestAutomaticIndex(p);
105352 p->cost.plan.wsFlags |= eqTermMask;
105353 }
105354
105355 /*
105356 ** Find the query plan for accessing table pSrc->pTab. Write the
105357 ** best query plan and its cost into the WhereCost object supplied
105358 ** as the last parameter. This function may calculate the cost of
105359 ** both real and virtual table scans.
105360 **
105361 ** This function does not take ORDER BY or DISTINCT into account. Nor
105362 ** does it remember the virtual table query plan. All it does is compute
105363 ** the cost while determining if an OR optimization is applicable. The
105364 ** details will be reconsidered later if the optimization is found to be
105365 ** applicable.
105366 */
105367 static void bestIndex(WhereBestIdx *p){
 
 
 
 
 
 
 
 
105368 #ifndef SQLITE_OMIT_VIRTUALTABLE
105369 if( IsVirtual(p->pSrc->pTab) ){
105370 sqlite3_index_info *pIdxInfo = 0;
105371 p->ppIdxInfo = &pIdxInfo;
105372 bestVirtualIndex(p);
105373 if( pIdxInfo->needToFreeIdxStr ){
105374 sqlite3_free(pIdxInfo->idxStr);
105375 }
105376 sqlite3DbFree(p->pParse->db, pIdxInfo);
105377 }else
105378 #endif
105379 {
105380 bestBtreeIndex(p);
105381 }
105382 }
105383
105384 /*
105385 ** Disable a term in the WHERE clause. Except, do not disable the term
@@ -106432,46 +106633,50 @@
106633 ** fi
106634 ** end
106635 **
106636 ** ORDER BY CLAUSE PROCESSING
106637 **
106638 ** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
106639 ** if there is one. If there is no ORDER BY clause or if this routine
106640 ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
106641 **
106642 ** If an index can be used so that the natural output order of the table
106643 ** scan is correct for the ORDER BY clause, then that index is used and
106644 ** the returned WhereInfo.nOBSat field is set to pOrderBy->nExpr. This
106645 ** is an optimization that prevents an unnecessary sort of the result set
106646 ** if an index appropriate for the ORDER BY clause already exists.
106647 **
106648 ** If the where clause loops cannot be arranged to provide the correct
106649 ** output order, then WhereInfo.nOBSat is 0.
106650 */
106651 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
106652 Parse *pParse, /* The parser context */
106653 SrcList *pTabList, /* A list of all tables to be scanned */
106654 Expr *pWhere, /* The WHERE clause */
106655 ExprList *pOrderBy, /* An ORDER BY clause, or NULL */
106656 ExprList *pDistinct, /* The select-list for DISTINCT queries - or NULL */
106657 u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */
106658 int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */
106659 ){
 
106660 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
106661 int nTabList; /* Number of elements in pTabList */
106662 WhereInfo *pWInfo; /* Will become the return value of this function */
106663 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
106664 Bitmask notReady; /* Cursors that are not yet positioned */
106665 WhereBestIdx sWBI; /* Best index search context */
106666 WhereMaskSet *pMaskSet; /* The expression mask set */
106667 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
106668 int iFrom; /* First unused FROM clause element */
 
 
106669 int andFlags; /* AND-ed combination of all pWC->a[].wtFlags */
106670 int ii; /* Loop counter */
106671 sqlite3 *db; /* Database connection */
106672
106673
106674 /* Variable initialization */
106675 memset(&sWBI, 0, sizeof(sWBI));
106676 sWBI.pParse = pParse;
106677
106678 /* The number of tables in the FROM clause is limited by the number of
106679 ** bits in a Bitmask
106680 */
106681 testcase( pTabList->nSrc==BMS );
106682 if( pTabList->nSrc>BMS ){
@@ -106507,26 +106712,27 @@
106712 }
106713 pWInfo->nLevel = nTabList;
106714 pWInfo->pParse = pParse;
106715 pWInfo->pTabList = pTabList;
106716 pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
106717 pWInfo->pWC = sWBI.pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
106718 pWInfo->wctrlFlags = wctrlFlags;
106719 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
106720 pMaskSet = (WhereMaskSet*)&sWBI.pWC[1];
106721 sWBI.aLevel = pWInfo->a;
106722
106723 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
106724 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
106725 if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0;
106726
106727 /* Split the WHERE clause into separate subexpressions where each
106728 ** subexpression is separated by an AND operator.
106729 */
106730 initMaskSet(pMaskSet);
106731 whereClauseInit(sWBI.pWC, pParse, pMaskSet, wctrlFlags);
106732 sqlite3ExprCodeConstants(pParse, pWhere);
106733 whereSplit(sWBI.pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */
106734
106735 /* Special case: a WHERE clause that is constant. Evaluate the
106736 ** expression and either jump over all of the code or fall thru.
106737 */
106738 if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
@@ -106553,24 +106759,24 @@
106759 ** Note that bitmasks are created for all pTabList->nSrc tables in
106760 ** pTabList, not just the first nTabList tables. nTabList is normally
106761 ** equal to pTabList->nSrc but might be shortened to 1 if the
106762 ** WHERE_ONETABLE_ONLY flag is set.
106763 */
106764 assert( sWBI.pWC->vmask==0 && pMaskSet->n==0 );
106765 for(ii=0; ii<pTabList->nSrc; ii++){
106766 createMask(pMaskSet, pTabList->a[ii].iCursor);
106767 #ifndef SQLITE_OMIT_VIRTUALTABLE
106768 if( ALWAYS(pTabList->a[ii].pTab) && IsVirtual(pTabList->a[ii].pTab) ){
106769 sWBI.pWC->vmask |= ((Bitmask)1 << ii);
106770 }
106771 #endif
106772 }
106773 #ifndef NDEBUG
106774 {
106775 Bitmask toTheLeft = 0;
106776 for(ii=0; ii<pTabList->nSrc; ii++){
106777 Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
106778 assert( (m-1)==toTheLeft );
106779 toTheLeft |= m;
106780 }
106781 }
106782 #endif
@@ -106578,20 +106784,20 @@
106784 /* Analyze all of the subexpressions. Note that exprAnalyze() might
106785 ** add new virtual terms onto the end of the WHERE clause. We do not
106786 ** want to analyze these virtual terms, so start analyzing at the end
106787 ** and work forward so that the added virtual terms are never processed.
106788 */
106789 exprAnalyzeAll(pTabList, sWBI.pWC);
106790 if( db->mallocFailed ){
106791 goto whereBeginError;
106792 }
106793
106794 /* Check if the DISTINCT qualifier, if there is one, is redundant.
106795 ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to
106796 ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT.
106797 */
106798 if( pDistinct && isDistinctRedundant(pParse, pTabList, sWBI.pWC, pDistinct) ){
106799 pDistinct = 0;
106800 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
106801 }
106802
106803 /* Chose the best index to use for each table in the FROM clause.
@@ -106607,14 +106813,17 @@
106813 ** pWInfo->a[].pTerm When wsFlags==WO_OR, the OR-clause term
106814 **
106815 ** This loop also figures out the nesting order of tables in the FROM
106816 ** clause.
106817 */
106818 sWBI.notValid = ~(Bitmask)0;
106819 sWBI.pOrderBy = pOrderBy;
106820 sWBI.n = nTabList;
106821 sWBI.pDistinct = pDistinct;
106822 andFlags = ~0;
106823 WHERETRACE(("*** Optimizer Start ***\n"));
106824 for(sWBI.i=iFrom=0, pLevel=pWInfo->a; sWBI.i<nTabList; sWBI.i++, pLevel++){
106825 WhereCost bestPlan; /* Most efficient plan seen so far */
106826 Index *pIdx; /* Index for FROM table at pTabItem */
106827 int j; /* For looping over FROM tables */
106828 int bestJ = -1; /* The value of j */
106829 Bitmask m; /* Bitmask value for j or bestJ */
@@ -106622,11 +106831,11 @@
106831 int nUnconstrained; /* Number tables without INDEXED BY */
106832 Bitmask notIndexed; /* Mask of tables that cannot use an index */
106833
106834 memset(&bestPlan, 0, sizeof(bestPlan));
106835 bestPlan.rCost = SQLITE_BIG_DBL;
106836 WHERETRACE(("*** Begin search for loop %d ***\n", sWBI.i));
106837
106838 /* Loop through the remaining entries in the FROM clause to find the
106839 ** next nested loop. The loop tests all FROM clause entries
106840 ** either once or twice.
106841 **
@@ -106638,12 +106847,12 @@
106847 ** were used as the innermost nested loop. In other words, a table
106848 ** is chosen such that the cost of running that table cannot be reduced
106849 ** by waiting for other tables to run first. This "optimal" test works
106850 ** by first assuming that the FROM clause is on the inner loop and finding
106851 ** its query plan, then checking to see if that query plan uses any
106852 ** other FROM clause terms that are sWBI.notValid. If no notValid terms
106853 ** are used then the "optimal" query plan works.
106854 **
106855 ** Note that the WhereCost.nRow parameter for an optimal scan might
106856 ** not be as small as it would be if the table really were the innermost
106857 ** join. The nRow value can be reduced by WHERE clause constraints
106858 ** that do not use indices. But this nRow reduction only happens if the
@@ -106670,59 +106879,52 @@
106879 ** costlier approach.
106880 */
106881 nUnconstrained = 0;
106882 notIndexed = 0;
106883 for(isOptimal=(iFrom<nTabList-1); isOptimal>=0 && bestJ<0; isOptimal--){
106884 for(j=iFrom, sWBI.pSrc=&pTabList->a[j]; j<nTabList; j++, sWBI.pSrc++){
 
106885 int doNotReorder; /* True if this table should not be reordered */
 
 
 
106886
106887 doNotReorder = (sWBI.pSrc->jointype & (JT_LEFT|JT_CROSS))!=0;
106888 if( j!=iFrom && doNotReorder ) break;
106889 m = getMask(pMaskSet, sWBI.pSrc->iCursor);
106890 if( (m & sWBI.notValid)==0 ){
106891 if( j==iFrom ) iFrom++;
106892 continue;
106893 }
106894 sWBI.notReady = (isOptimal ? m : sWBI.notValid);
106895 if( sWBI.pSrc->pIndex==0 ) nUnconstrained++;
 
 
106896
106897 WHERETRACE(("=== trying table %d with isOptimal=%d ===\n",
106898 j, isOptimal));
106899 assert( sWBI.pSrc->pTab );
106900 #ifndef SQLITE_OMIT_VIRTUALTABLE
106901 if( IsVirtual(sWBI.pSrc->pTab) ){
106902 sWBI.ppIdxInfo = &pWInfo->a[j].pIdxInfo;
106903 bestVirtualIndex(&sWBI);
 
106904 }else
106905 #endif
106906 {
106907 bestBtreeIndex(&sWBI);
 
106908 }
106909 assert( isOptimal || (sWBI.cost.used&sWBI.notValid)==0 );
106910
106911 /* If an INDEXED BY clause is present, then the plan must use that
106912 ** index if it uses any index at all */
106913 assert( sWBI.pSrc->pIndex==0
106914 || (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
106915 || sWBI.cost.plan.u.pIdx==sWBI.pSrc->pIndex );
106916
106917 if( isOptimal && (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
106918 notIndexed |= m;
106919 }
106920
106921 /* Conditions under which this table becomes the best so far:
106922 **
106923 ** (1) The table must not depend on other tables that have not
106924 ** yet run. (In other words, it must not depend on tables
106925 ** in inner loops.)
106926 **
106927 ** (2) A full-table-scan plan cannot supercede indexed plan unless
106928 ** the full-table-scan is an "optimal" plan as defined above.
106929 **
106930 ** (3) All tables have an INDEXED BY clause or this table lacks an
@@ -106735,37 +106937,38 @@
106937 ** An indexable full-table-scan from reaching rule (3).
106938 **
106939 ** (4) The plan cost must be lower than prior plans or else the
106940 ** cost must be the same and the number of rows must be lower.
106941 */
106942 if( (sWBI.cost.used&sWBI.notValid)==0 /* (1) */
106943 && (bestJ<0 || (notIndexed&m)!=0 /* (2) */
106944 || (bestPlan.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
106945 || (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
106946 && (nUnconstrained==0 || sWBI.pSrc->pIndex==0 /* (3) */
106947 || NEVER((sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
106948 && (bestJ<0 || sWBI.cost.rCost<bestPlan.rCost /* (4) */
106949 || (sWBI.cost.rCost<=bestPlan.rCost
106950 && sWBI.cost.plan.nRow<bestPlan.plan.nRow))
106951 ){
106952 WHERETRACE(("=== table %d is best so far"
106953 " with cost=%.1f, nRow=%.1f, nOBSat=%d\n",
106954 j, sWBI.cost.rCost, sWBI.cost.plan.nRow,
106955 sWBI.cost.plan.nOBSat));
106956 bestPlan = sWBI.cost;
106957 bestJ = j;
106958 }
106959 if( doNotReorder ) break;
106960 }
106961 }
106962 assert( bestJ>=0 );
106963 assert( sWBI.notValid & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
106964 WHERETRACE(("*** Optimizer selects table %d for loop %d with:\n"
106965 " cost=%.1f, nRow=%.1f, nOBSat=%d wsFlags=0x%08x\n",
106966 bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow,
106967 bestPlan.plan.nOBSat, bestPlan.plan.wsFlags));
106968 if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){
106969 pWInfo->nOBSat = pOrderBy->nExpr;
106970 }
106971 if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
106972 assert( pWInfo->eDistinct==0 );
106973 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
106974 }
@@ -106782,11 +106985,11 @@
106985 pLevel->iIdxCur = pParse->nTab++;
106986 }
106987 }else{
106988 pLevel->iIdxCur = -1;
106989 }
106990 sWBI.notValid &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
106991 pLevel->iFrom = (u8)bestJ;
106992 if( bestPlan.plan.nRow>=(double)1 ){
106993 pParse->nQueryLoop *= bestPlan.plan.nRow;
106994 }
106995
@@ -106814,12 +107017,12 @@
107017 }
107018
107019 /* If the total query only selects a single row, then the ORDER BY
107020 ** clause is irrelevant.
107021 */
107022 if( (andFlags & WHERE_UNIQUE)!=0 && pOrderBy ){
107023 pWInfo->nOBSat = pOrderBy->nExpr;
107024 }
107025
107026 /* If the caller is an UPDATE or DELETE statement that is requesting
107027 ** to use a one-pass algorithm, determine if this is appropriate.
107028 ** The one-pass algorithm only works if the WHERE clause constraints
@@ -106835,13 +107038,14 @@
107038 ** searching those tables.
107039 */
107040 sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
107041 notReady = ~(Bitmask)0;
107042 pWInfo->nRowOut = (double)1;
107043 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
107044 Table *pTab; /* Table to open */
107045 int iDb; /* Index of database containing table/index */
107046 struct SrcList_item *pTabItem;
107047
107048 pTabItem = &pTabList->a[pLevel->iFrom];
107049 pTab = pTabItem->pTab;
107050 pLevel->iTabCur = pTabItem->iCursor;
107051 pWInfo->nRowOut *= pLevel->plan.nRow;
@@ -106873,11 +107077,11 @@
107077 }else{
107078 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
107079 }
107080 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
107081 if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){
107082 constructAutomaticIndex(pParse, sWBI.pWC, pTabItem, notReady, pLevel);
107083 }else
107084 #endif
107085 if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
107086 Index *pIx = pLevel->plan.u.pIdx;
107087 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
@@ -106887,24 +107091,24 @@
107091 sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb,
107092 (char*)pKey, P4_KEYINFO_HANDOFF);
107093 VdbeComment((v, "%s", pIx->zName));
107094 }
107095 sqlite3CodeVerifySchema(pParse, iDb);
107096 notReady &= ~getMask(sWBI.pWC->pMaskSet, pTabItem->iCursor);
107097 }
107098 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
107099 if( db->mallocFailed ) goto whereBeginError;
107100
107101 /* Generate the code to do the search. Each iteration of the for
107102 ** loop below generates code for a single nested loop of the VM
107103 ** program.
107104 */
107105 notReady = ~(Bitmask)0;
107106 for(ii=0; ii<nTabList; ii++){
107107 pLevel = &pWInfo->a[ii];
107108 explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
107109 notReady = codeOneLoopStart(pWInfo, ii, wctrlFlags, notReady);
107110 pWInfo->iContinue = pLevel->addrCont;
107111 }
107112
107113 #ifdef SQLITE_TEST /* For testing and debugging use only */
107114 /* Record in the query plan information about the current table
@@ -106911,15 +107115,17 @@
107115 ** and the index used to access it (if any). If the table itself
107116 ** is not used, its name is just '{}'. If no index is used
107117 ** the index is listed as "{}". If the primary key is used the
107118 ** index name is '*'.
107119 */
107120 for(ii=0; ii<nTabList; ii++){
107121 char *z;
107122 int n;
107123 int w;
107124 struct SrcList_item *pTabItem;
107125
107126 pLevel = &pWInfo->a[ii];
107127 w = pLevel->plan.wsFlags;
107128 pTabItem = &pTabList->a[pLevel->iFrom];
107129 z = pTabItem->zAlias;
107130 if( z==0 ) z = pTabItem->pTab->zName;
107131 n = sqlite3Strlen30(z);
@@ -112874,10 +113080,11 @@
113080 ){
113081 sqlite3_mutex_enter(db->mutex);
113082 db->busyHandler.xFunc = xBusy;
113083 db->busyHandler.pArg = pArg;
113084 db->busyHandler.nBusy = 0;
113085 db->busyTimeout = 0;
113086 sqlite3_mutex_leave(db->mutex);
113087 return SQLITE_OK;
113088 }
113089
113090 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
@@ -112911,12 +113118,12 @@
113118 ** This routine installs a default busy handler that waits for the
113119 ** specified number of milliseconds before returning 0.
113120 */
113121 SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
113122 if( ms>0 ){
 
113123 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
113124 db->busyTimeout = ms;
113125 }else{
113126 sqlite3_busy_handler(db, 0, 0);
113127 }
113128 return SQLITE_OK;
113129 }
@@ -114771,12 +114978,11 @@
114978 ** with various optimizations disabled to verify that the same answer
114979 ** is obtained in every case.
114980 */
114981 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
114982 sqlite3 *db = va_arg(ap, sqlite3*);
114983 db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
 
114984 break;
114985 }
114986
114987 #ifdef SQLITE_N_KEYWORD
114988 /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
114989
+4 -1
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.7.15"
111111
#define SQLITE_VERSION_NUMBER 3007015
112
-#define SQLITE_SOURCE_ID "2012-09-17 21:24:01 698b2a28004a9a2f0eabaadf36d833da4400b2bf"
112
+#define SQLITE_SOURCE_ID "2012-09-28 00:44:28 1e874629d7cf568368b912b295bd3001147d0b52"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -4749,10 +4749,13 @@
47494749
** successfully. An [error code] is returned otherwise.)^
47504750
**
47514751
** ^Shared cache is disabled by default. But this might change in
47524752
** future releases of SQLite. Applications that care about shared
47534753
** cache setting should set it explicitly.
4754
+**
4755
+** This interface is threadsafe on processors where writing a
4756
+** 32-bit integer is atomic.
47544757
**
47554758
** See Also: [SQLite Shared-Cache Mode]
47564759
*/
47574760
SQLITE_API int sqlite3_enable_shared_cache(int);
47584761
47594762
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.15"
111 #define SQLITE_VERSION_NUMBER 3007015
112 #define SQLITE_SOURCE_ID "2012-09-17 21:24:01 698b2a28004a9a2f0eabaadf36d833da4400b2bf"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -4749,10 +4749,13 @@
4749 ** successfully. An [error code] is returned otherwise.)^
4750 **
4751 ** ^Shared cache is disabled by default. But this might change in
4752 ** future releases of SQLite. Applications that care about shared
4753 ** cache setting should set it explicitly.
 
 
 
4754 **
4755 ** See Also: [SQLite Shared-Cache Mode]
4756 */
4757 SQLITE_API int sqlite3_enable_shared_cache(int);
4758
4759
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.15"
111 #define SQLITE_VERSION_NUMBER 3007015
112 #define SQLITE_SOURCE_ID "2012-09-28 00:44:28 1e874629d7cf568368b912b295bd3001147d0b52"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -4749,10 +4749,13 @@
4749 ** successfully. An [error code] is returned otherwise.)^
4750 **
4751 ** ^Shared cache is disabled by default. But this might change in
4752 ** future releases of SQLite. Applications that care about shared
4753 ** cache setting should set it explicitly.
4754 **
4755 ** This interface is threadsafe on processors where writing a
4756 ** 32-bit integer is atomic.
4757 **
4758 ** See Also: [SQLite Shared-Cache Mode]
4759 */
4760 SQLITE_API int sqlite3_enable_shared_cache(int);
4761
4762

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button