| | @@ -155,10 +155,17 @@ |
| 155 | 155 | # ifndef _FILE_OFFSET_BITS |
| 156 | 156 | # define _FILE_OFFSET_BITS 64 |
| 157 | 157 | # endif |
| 158 | 158 | # define _LARGEFILE_SOURCE 1 |
| 159 | 159 | #endif |
| 160 | + |
| 161 | +/* What version of GCC is being used. 0 means GCC is not being used */ |
| 162 | +#ifdef __GNUC__ |
| 163 | +# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__) |
| 164 | +#else |
| 165 | +# define GCC_VERSION 0 |
| 166 | +#endif |
| 160 | 167 | |
| 161 | 168 | /* Needed for various definitions... */ |
| 162 | 169 | #if defined(__GNUC__) && !defined(_GNU_SOURCE) |
| 163 | 170 | # define _GNU_SOURCE |
| 164 | 171 | #endif |
| | @@ -228,11 +235,11 @@ |
| 228 | 235 | ** to experimental interfaces but reserve the right to make minor changes |
| 229 | 236 | ** if experience from use "in the wild" suggest such changes are prudent. |
| 230 | 237 | ** |
| 231 | 238 | ** The official C-language API documentation for SQLite is derived |
| 232 | 239 | ** from comments in this file. This file is the authoritative source |
| 233 | | -** on how SQLite interfaces are suppose to operate. |
| 240 | +** on how SQLite interfaces are supposed to operate. |
| 234 | 241 | ** |
| 235 | 242 | ** The name of this file under configuration management is "sqlite.h.in". |
| 236 | 243 | ** The makefile makes some minor changes to this file (such as inserting |
| 237 | 244 | ** the version number) and changes its name to "sqlite3.h" as |
| 238 | 245 | ** part of the build process. |
| | @@ -318,11 +325,11 @@ |
| 318 | 325 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 319 | 326 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 320 | 327 | */ |
| 321 | 328 | #define SQLITE_VERSION "3.8.11" |
| 322 | 329 | #define SQLITE_VERSION_NUMBER 3008011 |
| 323 | | -#define SQLITE_SOURCE_ID "2015-05-29 17:51:16 db4e9728fae5f7b0fad6aa0a5be317a7c9e7c417" |
| 330 | +#define SQLITE_SOURCE_ID "2015-06-30 15:10:29 8bfcda3d10aec864d71d12a1248c37e4db6f8899" |
| 324 | 331 | |
| 325 | 332 | /* |
| 326 | 333 | ** CAPI3REF: Run-Time Library Version Numbers |
| 327 | 334 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 328 | 335 | ** |
| | @@ -1161,11 +1168,11 @@ |
| 1161 | 1168 | ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This |
| 1162 | 1169 | ** opcode causes the xFileControl method to swap the file handle with the one |
| 1163 | 1170 | ** pointed to by the pArg argument. This capability is used during testing |
| 1164 | 1171 | ** and only needs to be supported when SQLITE_TEST is defined. |
| 1165 | 1172 | ** |
| 1166 | | -* <li>[[SQLITE_FCNTL_WAL_BLOCK]] |
| 1173 | +** <li>[[SQLITE_FCNTL_WAL_BLOCK]] |
| 1167 | 1174 | ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might |
| 1168 | 1175 | ** be advantageous to block on the next WAL lock if the lock is not immediately |
| 1169 | 1176 | ** available. The WAL subsystem issues this signal during rare |
| 1170 | 1177 | ** circumstances in order to fix a problem with priority inversion. |
| 1171 | 1178 | ** Applications should <em>not</em> use this file-control. |
| | @@ -9689,17 +9696,18 @@ |
| 9689 | 9696 | u8 opflags; /* Mask of the OPFLG_* flags in opcodes.h */ |
| 9690 | 9697 | u8 p5; /* Fifth parameter is an unsigned character */ |
| 9691 | 9698 | int p1; /* First operand */ |
| 9692 | 9699 | int p2; /* Second parameter (often the jump destination) */ |
| 9693 | 9700 | int p3; /* The third parameter */ |
| 9694 | | - union { /* fourth parameter */ |
| 9701 | + union p4union { /* fourth parameter */ |
| 9695 | 9702 | int i; /* Integer value if p4type==P4_INT32 */ |
| 9696 | 9703 | void *p; /* Generic pointer */ |
| 9697 | 9704 | char *z; /* Pointer to data for string (char array) types */ |
| 9698 | 9705 | i64 *pI64; /* Used when p4type is P4_INT64 */ |
| 9699 | 9706 | double *pReal; /* Used when p4type is P4_REAL */ |
| 9700 | 9707 | FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */ |
| 9708 | + sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */ |
| 9701 | 9709 | CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */ |
| 9702 | 9710 | Mem *pMem; /* Used when p4type is P4_MEM */ |
| 9703 | 9711 | VTable *pVtab; /* Used when p4type is P4_VTAB */ |
| 9704 | 9712 | KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */ |
| 9705 | 9713 | int *ai; /* Used when p4type is P4_INTARRAY */ |
| | @@ -9762,10 +9770,11 @@ |
| 9762 | 9770 | #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */ |
| 9763 | 9771 | #define P4_INT32 (-14) /* P4 is a 32-bit signed integer */ |
| 9764 | 9772 | #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */ |
| 9765 | 9773 | #define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */ |
| 9766 | 9774 | #define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */ |
| 9775 | +#define P4_FUNCCTX (-20) /* P4 is a pointer to an sqlite3_context object */ |
| 9767 | 9776 | |
| 9768 | 9777 | /* Error message codes for OP_Halt */ |
| 9769 | 9778 | #define P5_ConstraintNotNull 1 |
| 9770 | 9779 | #define P5_ConstraintUnique 2 |
| 9771 | 9780 | #define P5_ConstraintCheck 3 |
| | @@ -9804,46 +9813,46 @@ |
| 9804 | 9813 | */ |
| 9805 | 9814 | /************** Include opcodes.h in the middle of vdbe.h ********************/ |
| 9806 | 9815 | /************** Begin file opcodes.h *****************************************/ |
| 9807 | 9816 | /* Automatically generated. Do not edit */ |
| 9808 | 9817 | /* See the mkopcodeh.awk script for details */ |
| 9809 | | -#define OP_Function 1 /* synopsis: r[P3]=func(r[P2@P5]) */ |
| 9810 | | -#define OP_Savepoint 2 |
| 9811 | | -#define OP_AutoCommit 3 |
| 9812 | | -#define OP_Transaction 4 |
| 9813 | | -#define OP_SorterNext 5 |
| 9814 | | -#define OP_PrevIfOpen 6 |
| 9815 | | -#define OP_NextIfOpen 7 |
| 9816 | | -#define OP_Prev 8 |
| 9817 | | -#define OP_Next 9 |
| 9818 | | -#define OP_AggStep 10 /* synopsis: accum=r[P3] step(r[P2@P5]) */ |
| 9819 | | -#define OP_Checkpoint 11 |
| 9820 | | -#define OP_JournalMode 12 |
| 9821 | | -#define OP_Vacuum 13 |
| 9822 | | -#define OP_VFilter 14 /* synopsis: iplan=r[P3] zplan='P4' */ |
| 9823 | | -#define OP_VUpdate 15 /* synopsis: data=r[P3@P2] */ |
| 9824 | | -#define OP_Goto 16 |
| 9825 | | -#define OP_Gosub 17 |
| 9826 | | -#define OP_Return 18 |
| 9818 | +#define OP_Savepoint 1 |
| 9819 | +#define OP_AutoCommit 2 |
| 9820 | +#define OP_Transaction 3 |
| 9821 | +#define OP_SorterNext 4 |
| 9822 | +#define OP_PrevIfOpen 5 |
| 9823 | +#define OP_NextIfOpen 6 |
| 9824 | +#define OP_Prev 7 |
| 9825 | +#define OP_Next 8 |
| 9826 | +#define OP_Checkpoint 9 |
| 9827 | +#define OP_JournalMode 10 |
| 9828 | +#define OP_Vacuum 11 |
| 9829 | +#define OP_VFilter 12 /* synopsis: iplan=r[P3] zplan='P4' */ |
| 9830 | +#define OP_VUpdate 13 /* synopsis: data=r[P3@P2] */ |
| 9831 | +#define OP_Goto 14 |
| 9832 | +#define OP_Gosub 15 |
| 9833 | +#define OP_Return 16 |
| 9834 | +#define OP_InitCoroutine 17 |
| 9835 | +#define OP_EndCoroutine 18 |
| 9827 | 9836 | #define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */ |
| 9828 | | -#define OP_InitCoroutine 20 |
| 9829 | | -#define OP_EndCoroutine 21 |
| 9830 | | -#define OP_Yield 22 |
| 9831 | | -#define OP_HaltIfNull 23 /* synopsis: if r[P3]=null halt */ |
| 9832 | | -#define OP_Halt 24 |
| 9833 | | -#define OP_Integer 25 /* synopsis: r[P2]=P1 */ |
| 9834 | | -#define OP_Int64 26 /* synopsis: r[P2]=P4 */ |
| 9835 | | -#define OP_String 27 /* synopsis: r[P2]='P4' (len=P1) */ |
| 9836 | | -#define OP_Null 28 /* synopsis: r[P2..P3]=NULL */ |
| 9837 | | -#define OP_SoftNull 29 /* synopsis: r[P1]=NULL */ |
| 9838 | | -#define OP_Blob 30 /* synopsis: r[P2]=P4 (len=P1) */ |
| 9839 | | -#define OP_Variable 31 /* synopsis: r[P2]=parameter(P1,P4) */ |
| 9840 | | -#define OP_Move 32 /* synopsis: r[P2@P3]=r[P1@P3] */ |
| 9841 | | -#define OP_Copy 33 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */ |
| 9842 | | -#define OP_SCopy 34 /* synopsis: r[P2]=r[P1] */ |
| 9843 | | -#define OP_ResultRow 35 /* synopsis: output=r[P1@P2] */ |
| 9844 | | -#define OP_CollSeq 36 |
| 9837 | +#define OP_Yield 20 |
| 9838 | +#define OP_HaltIfNull 21 /* synopsis: if r[P3]=null halt */ |
| 9839 | +#define OP_Halt 22 |
| 9840 | +#define OP_Integer 23 /* synopsis: r[P2]=P1 */ |
| 9841 | +#define OP_Int64 24 /* synopsis: r[P2]=P4 */ |
| 9842 | +#define OP_String 25 /* synopsis: r[P2]='P4' (len=P1) */ |
| 9843 | +#define OP_Null 26 /* synopsis: r[P2..P3]=NULL */ |
| 9844 | +#define OP_SoftNull 27 /* synopsis: r[P1]=NULL */ |
| 9845 | +#define OP_Blob 28 /* synopsis: r[P2]=P4 (len=P1) */ |
| 9846 | +#define OP_Variable 29 /* synopsis: r[P2]=parameter(P1,P4) */ |
| 9847 | +#define OP_Move 30 /* synopsis: r[P2@P3]=r[P1@P3] */ |
| 9848 | +#define OP_Copy 31 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */ |
| 9849 | +#define OP_SCopy 32 /* synopsis: r[P2]=r[P1] */ |
| 9850 | +#define OP_ResultRow 33 /* synopsis: output=r[P1@P2] */ |
| 9851 | +#define OP_CollSeq 34 |
| 9852 | +#define OP_Function0 35 /* synopsis: r[P3]=func(r[P2@P5]) */ |
| 9853 | +#define OP_Function 36 /* synopsis: r[P3]=func(r[P2@P5]) */ |
| 9845 | 9854 | #define OP_AddImm 37 /* synopsis: r[P1]=r[P1]+P2 */ |
| 9846 | 9855 | #define OP_MustBeInt 38 |
| 9847 | 9856 | #define OP_RealAffinity 39 |
| 9848 | 9857 | #define OP_Cast 40 /* synopsis: affinity(r[P1]) */ |
| 9849 | 9858 | #define OP_Permutation 41 |
| | @@ -9865,33 +9874,33 @@ |
| 9865 | 9874 | #define OP_OpenEphemeral 57 /* synopsis: nColumn=P2 */ |
| 9866 | 9875 | #define OP_SorterOpen 58 |
| 9867 | 9876 | #define OP_SequenceTest 59 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */ |
| 9868 | 9877 | #define OP_OpenPseudo 60 /* synopsis: P3 columns in r[P2] */ |
| 9869 | 9878 | #define OP_Close 61 |
| 9870 | | -#define OP_SeekLT 62 /* synopsis: key=r[P3@P4] */ |
| 9871 | | -#define OP_SeekLE 63 /* synopsis: key=r[P3@P4] */ |
| 9872 | | -#define OP_SeekGE 64 /* synopsis: key=r[P3@P4] */ |
| 9873 | | -#define OP_SeekGT 65 /* synopsis: key=r[P3@P4] */ |
| 9874 | | -#define OP_Seek 66 /* synopsis: intkey=r[P2] */ |
| 9875 | | -#define OP_NoConflict 67 /* synopsis: key=r[P3@P4] */ |
| 9876 | | -#define OP_NotFound 68 /* synopsis: key=r[P3@P4] */ |
| 9877 | | -#define OP_Found 69 /* synopsis: key=r[P3@P4] */ |
| 9878 | | -#define OP_NotExists 70 /* synopsis: intkey=r[P3] */ |
| 9879 | +#define OP_ColumnsUsed 62 |
| 9880 | +#define OP_SeekLT 63 /* synopsis: key=r[P3@P4] */ |
| 9881 | +#define OP_SeekLE 64 /* synopsis: key=r[P3@P4] */ |
| 9882 | +#define OP_SeekGE 65 /* synopsis: key=r[P3@P4] */ |
| 9883 | +#define OP_SeekGT 66 /* synopsis: key=r[P3@P4] */ |
| 9884 | +#define OP_Seek 67 /* synopsis: intkey=r[P2] */ |
| 9885 | +#define OP_NoConflict 68 /* synopsis: key=r[P3@P4] */ |
| 9886 | +#define OP_NotFound 69 /* synopsis: key=r[P3@P4] */ |
| 9887 | +#define OP_Found 70 /* synopsis: key=r[P3@P4] */ |
| 9879 | 9888 | #define OP_Or 71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */ |
| 9880 | 9889 | #define OP_And 72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */ |
| 9881 | | -#define OP_Sequence 73 /* synopsis: r[P2]=cursor[P1].ctr++ */ |
| 9882 | | -#define OP_NewRowid 74 /* synopsis: r[P2]=rowid */ |
| 9883 | | -#define OP_Insert 75 /* synopsis: intkey=r[P3] data=r[P2] */ |
| 9890 | +#define OP_NotExists 73 /* synopsis: intkey=r[P3] */ |
| 9891 | +#define OP_Sequence 74 /* synopsis: r[P2]=cursor[P1].ctr++ */ |
| 9892 | +#define OP_NewRowid 75 /* synopsis: r[P2]=rowid */ |
| 9884 | 9893 | #define OP_IsNull 76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */ |
| 9885 | 9894 | #define OP_NotNull 77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */ |
| 9886 | 9895 | #define OP_Ne 78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */ |
| 9887 | 9896 | #define OP_Eq 79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */ |
| 9888 | 9897 | #define OP_Gt 80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */ |
| 9889 | 9898 | #define OP_Le 81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */ |
| 9890 | 9899 | #define OP_Lt 82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */ |
| 9891 | 9900 | #define OP_Ge 83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */ |
| 9892 | | -#define OP_InsertInt 84 /* synopsis: intkey=P3 data=r[P2] */ |
| 9901 | +#define OP_Insert 84 /* synopsis: intkey=r[P3] data=r[P2] */ |
| 9893 | 9902 | #define OP_BitAnd 85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */ |
| 9894 | 9903 | #define OP_BitOr 86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */ |
| 9895 | 9904 | #define OP_ShiftLeft 87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */ |
| 9896 | 9905 | #define OP_ShiftRight 88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */ |
| 9897 | 9906 | #define OP_Add 89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */ |
| | @@ -9898,73 +9907,76 @@ |
| 9898 | 9907 | #define OP_Subtract 90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */ |
| 9899 | 9908 | #define OP_Multiply 91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */ |
| 9900 | 9909 | #define OP_Divide 92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */ |
| 9901 | 9910 | #define OP_Remainder 93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */ |
| 9902 | 9911 | #define OP_Concat 94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */ |
| 9903 | | -#define OP_Delete 95 |
| 9912 | +#define OP_InsertInt 95 /* synopsis: intkey=P3 data=r[P2] */ |
| 9904 | 9913 | #define OP_BitNot 96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */ |
| 9905 | 9914 | #define OP_String8 97 /* same as TK_STRING, synopsis: r[P2]='P4' */ |
| 9906 | | -#define OP_ResetCount 98 |
| 9907 | | -#define OP_SorterCompare 99 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */ |
| 9908 | | -#define OP_SorterData 100 /* synopsis: r[P2]=data */ |
| 9909 | | -#define OP_RowKey 101 /* synopsis: r[P2]=key */ |
| 9910 | | -#define OP_RowData 102 /* synopsis: r[P2]=data */ |
| 9911 | | -#define OP_Rowid 103 /* synopsis: r[P2]=rowid */ |
| 9912 | | -#define OP_NullRow 104 |
| 9913 | | -#define OP_Last 105 |
| 9914 | | -#define OP_SorterSort 106 |
| 9915 | | -#define OP_Sort 107 |
| 9916 | | -#define OP_Rewind 108 |
| 9917 | | -#define OP_SorterInsert 109 |
| 9918 | | -#define OP_IdxInsert 110 /* synopsis: key=r[P2] */ |
| 9919 | | -#define OP_IdxDelete 111 /* synopsis: key=r[P2@P3] */ |
| 9920 | | -#define OP_IdxRowid 112 /* synopsis: r[P2]=rowid */ |
| 9921 | | -#define OP_IdxLE 113 /* synopsis: key=r[P3@P4] */ |
| 9922 | | -#define OP_IdxGT 114 /* synopsis: key=r[P3@P4] */ |
| 9923 | | -#define OP_IdxLT 115 /* synopsis: key=r[P3@P4] */ |
| 9924 | | -#define OP_IdxGE 116 /* synopsis: key=r[P3@P4] */ |
| 9925 | | -#define OP_Destroy 117 |
| 9926 | | -#define OP_Clear 118 |
| 9927 | | -#define OP_ResetSorter 119 |
| 9928 | | -#define OP_CreateIndex 120 /* synopsis: r[P2]=root iDb=P1 */ |
| 9929 | | -#define OP_CreateTable 121 /* synopsis: r[P2]=root iDb=P1 */ |
| 9930 | | -#define OP_ParseSchema 122 |
| 9931 | | -#define OP_LoadAnalysis 123 |
| 9932 | | -#define OP_DropTable 124 |
| 9933 | | -#define OP_DropIndex 125 |
| 9934 | | -#define OP_DropTrigger 126 |
| 9935 | | -#define OP_IntegrityCk 127 |
| 9936 | | -#define OP_RowSetAdd 128 /* synopsis: rowset(P1)=r[P2] */ |
| 9937 | | -#define OP_RowSetRead 129 /* synopsis: r[P3]=rowset(P1) */ |
| 9938 | | -#define OP_RowSetTest 130 /* synopsis: if r[P3] in rowset(P1) goto P2 */ |
| 9939 | | -#define OP_Program 131 |
| 9940 | | -#define OP_Param 132 |
| 9915 | +#define OP_Delete 98 |
| 9916 | +#define OP_ResetCount 99 |
| 9917 | +#define OP_SorterCompare 100 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */ |
| 9918 | +#define OP_SorterData 101 /* synopsis: r[P2]=data */ |
| 9919 | +#define OP_RowKey 102 /* synopsis: r[P2]=key */ |
| 9920 | +#define OP_RowData 103 /* synopsis: r[P2]=data */ |
| 9921 | +#define OP_Rowid 104 /* synopsis: r[P2]=rowid */ |
| 9922 | +#define OP_NullRow 105 |
| 9923 | +#define OP_Last 106 |
| 9924 | +#define OP_SorterSort 107 |
| 9925 | +#define OP_Sort 108 |
| 9926 | +#define OP_Rewind 109 |
| 9927 | +#define OP_SorterInsert 110 |
| 9928 | +#define OP_IdxInsert 111 /* synopsis: key=r[P2] */ |
| 9929 | +#define OP_IdxDelete 112 /* synopsis: key=r[P2@P3] */ |
| 9930 | +#define OP_IdxRowid 113 /* synopsis: r[P2]=rowid */ |
| 9931 | +#define OP_IdxLE 114 /* synopsis: key=r[P3@P4] */ |
| 9932 | +#define OP_IdxGT 115 /* synopsis: key=r[P3@P4] */ |
| 9933 | +#define OP_IdxLT 116 /* synopsis: key=r[P3@P4] */ |
| 9934 | +#define OP_IdxGE 117 /* synopsis: key=r[P3@P4] */ |
| 9935 | +#define OP_Destroy 118 |
| 9936 | +#define OP_Clear 119 |
| 9937 | +#define OP_ResetSorter 120 |
| 9938 | +#define OP_CreateIndex 121 /* synopsis: r[P2]=root iDb=P1 */ |
| 9939 | +#define OP_CreateTable 122 /* synopsis: r[P2]=root iDb=P1 */ |
| 9940 | +#define OP_ParseSchema 123 |
| 9941 | +#define OP_LoadAnalysis 124 |
| 9942 | +#define OP_DropTable 125 |
| 9943 | +#define OP_DropIndex 126 |
| 9944 | +#define OP_DropTrigger 127 |
| 9945 | +#define OP_IntegrityCk 128 |
| 9946 | +#define OP_RowSetAdd 129 /* synopsis: rowset(P1)=r[P2] */ |
| 9947 | +#define OP_RowSetRead 130 /* synopsis: r[P3]=rowset(P1) */ |
| 9948 | +#define OP_RowSetTest 131 /* synopsis: if r[P3] in rowset(P1) goto P2 */ |
| 9949 | +#define OP_Program 132 |
| 9941 | 9950 | #define OP_Real 133 /* same as TK_FLOAT, synopsis: r[P2]=P4 */ |
| 9942 | | -#define OP_FkCounter 134 /* synopsis: fkctr[P1]+=P2 */ |
| 9943 | | -#define OP_FkIfZero 135 /* synopsis: if fkctr[P1]==0 goto P2 */ |
| 9944 | | -#define OP_MemMax 136 /* synopsis: r[P1]=max(r[P1],r[P2]) */ |
| 9945 | | -#define OP_IfPos 137 /* synopsis: if r[P1]>0 goto P2 */ |
| 9946 | | -#define OP_IfNeg 138 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2 */ |
| 9947 | | -#define OP_IfNotZero 139 /* synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2 */ |
| 9948 | | -#define OP_DecrJumpZero 140 /* synopsis: if (--r[P1])==0 goto P2 */ |
| 9949 | | -#define OP_JumpZeroIncr 141 /* synopsis: if (r[P1]++)==0 ) goto P2 */ |
| 9950 | | -#define OP_AggFinal 142 /* synopsis: accum=r[P1] N=P2 */ |
| 9951 | | -#define OP_IncrVacuum 143 |
| 9952 | | -#define OP_Expire 144 |
| 9953 | | -#define OP_TableLock 145 /* synopsis: iDb=P1 root=P2 write=P3 */ |
| 9954 | | -#define OP_VBegin 146 |
| 9955 | | -#define OP_VCreate 147 |
| 9956 | | -#define OP_VDestroy 148 |
| 9957 | | -#define OP_VOpen 149 |
| 9958 | | -#define OP_VColumn 150 /* synopsis: r[P3]=vcolumn(P2) */ |
| 9959 | | -#define OP_VNext 151 |
| 9960 | | -#define OP_VRename 152 |
| 9961 | | -#define OP_Pagecount 153 |
| 9962 | | -#define OP_MaxPgcnt 154 |
| 9963 | | -#define OP_Init 155 /* synopsis: Start at P2 */ |
| 9964 | | -#define OP_Noop 156 |
| 9965 | | -#define OP_Explain 157 |
| 9951 | +#define OP_Param 134 |
| 9952 | +#define OP_FkCounter 135 /* synopsis: fkctr[P1]+=P2 */ |
| 9953 | +#define OP_FkIfZero 136 /* synopsis: if fkctr[P1]==0 goto P2 */ |
| 9954 | +#define OP_MemMax 137 /* synopsis: r[P1]=max(r[P1],r[P2]) */ |
| 9955 | +#define OP_IfPos 138 /* synopsis: if r[P1]>0 goto P2 */ |
| 9956 | +#define OP_IfNeg 139 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2 */ |
| 9957 | +#define OP_IfNotZero 140 /* synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2 */ |
| 9958 | +#define OP_DecrJumpZero 141 /* synopsis: if (--r[P1])==0 goto P2 */ |
| 9959 | +#define OP_JumpZeroIncr 142 /* synopsis: if (r[P1]++)==0 ) goto P2 */ |
| 9960 | +#define OP_AggStep0 143 /* synopsis: accum=r[P3] step(r[P2@P5]) */ |
| 9961 | +#define OP_AggStep 144 /* synopsis: accum=r[P3] step(r[P2@P5]) */ |
| 9962 | +#define OP_AggFinal 145 /* synopsis: accum=r[P1] N=P2 */ |
| 9963 | +#define OP_IncrVacuum 146 |
| 9964 | +#define OP_Expire 147 |
| 9965 | +#define OP_TableLock 148 /* synopsis: iDb=P1 root=P2 write=P3 */ |
| 9966 | +#define OP_VBegin 149 |
| 9967 | +#define OP_VCreate 150 |
| 9968 | +#define OP_VDestroy 151 |
| 9969 | +#define OP_VOpen 152 |
| 9970 | +#define OP_VColumn 153 /* synopsis: r[P3]=vcolumn(P2) */ |
| 9971 | +#define OP_VNext 154 |
| 9972 | +#define OP_VRename 155 |
| 9973 | +#define OP_Pagecount 156 |
| 9974 | +#define OP_MaxPgcnt 157 |
| 9975 | +#define OP_Init 158 /* synopsis: Start at P2 */ |
| 9976 | +#define OP_Noop 159 |
| 9977 | +#define OP_Explain 160 |
| 9966 | 9978 | |
| 9967 | 9979 | |
| 9968 | 9980 | /* Properties such as "out2" or "jump" that are specified in |
| 9969 | 9981 | ** comments following the "case" for each opcode in the vdbe.c |
| 9970 | 9982 | ** are encoded into bitvectors as follows: |
| | @@ -9974,30 +9986,31 @@ |
| 9974 | 9986 | #define OPFLG_IN2 0x0004 /* in2: P2 is an input */ |
| 9975 | 9987 | #define OPFLG_IN3 0x0008 /* in3: P3 is an input */ |
| 9976 | 9988 | #define OPFLG_OUT2 0x0010 /* out2: P2 is an output */ |
| 9977 | 9989 | #define OPFLG_OUT3 0x0020 /* out3: P3 is an output */ |
| 9978 | 9990 | #define OPFLG_INITIALIZER {\ |
| 9979 | | -/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,\ |
| 9980 | | -/* 8 */ 0x01, 0x01, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\ |
| 9981 | | -/* 16 */ 0x01, 0x01, 0x02, 0x12, 0x01, 0x02, 0x03, 0x08,\ |
| 9982 | | -/* 24 */ 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10, 0x10,\ |
| 9983 | | -/* 32 */ 0x00, 0x00, 0x10, 0x00, 0x00, 0x02, 0x03, 0x02,\ |
| 9991 | +/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01,\ |
| 9992 | +/* 8 */ 0x01, 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01,\ |
| 9993 | +/* 16 */ 0x02, 0x01, 0x02, 0x12, 0x03, 0x08, 0x00, 0x10,\ |
| 9994 | +/* 24 */ 0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00,\ |
| 9995 | +/* 32 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x02,\ |
| 9984 | 9996 | /* 40 */ 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x00,\ |
| 9985 | 9997 | /* 48 */ 0x00, 0x00, 0x10, 0x10, 0x08, 0x00, 0x00, 0x00,\ |
| 9986 | | -/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x09,\ |
| 9987 | | -/* 64 */ 0x09, 0x09, 0x04, 0x09, 0x09, 0x09, 0x09, 0x26,\ |
| 9988 | | -/* 72 */ 0x26, 0x10, 0x10, 0x00, 0x03, 0x03, 0x0b, 0x0b,\ |
| 9998 | +/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,\ |
| 9999 | +/* 64 */ 0x09, 0x09, 0x09, 0x04, 0x09, 0x09, 0x09, 0x26,\ |
| 10000 | +/* 72 */ 0x26, 0x09, 0x10, 0x10, 0x03, 0x03, 0x0b, 0x0b,\ |
| 9989 | 10001 | /* 80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x26, 0x26, 0x26,\ |
| 9990 | 10002 | /* 88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\ |
| 9991 | | -/* 96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\ |
| 9992 | | -/* 104 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x00,\ |
| 9993 | | -/* 112 */ 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00, 0x00,\ |
| 9994 | | -/* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 9995 | | -/* 128 */ 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00, 0x01,\ |
| 9996 | | -/* 136 */ 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x01,\ |
| 9997 | | -/* 144 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,\ |
| 9998 | | -/* 152 */ 0x00, 0x10, 0x10, 0x01, 0x00, 0x00,} |
| 10003 | +/* 96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 10004 | +/* 104 */ 0x10, 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04,\ |
| 10005 | +/* 112 */ 0x00, 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00,\ |
| 10006 | +/* 120 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 10007 | +/* 128 */ 0x00, 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00,\ |
| 10008 | +/* 136 */ 0x01, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00,\ |
| 10009 | +/* 144 */ 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 10010 | +/* 152 */ 0x00, 0x00, 0x01, 0x00, 0x10, 0x10, 0x01, 0x00,\ |
| 10011 | +/* 160 */ 0x00,} |
| 9999 | 10012 | |
| 10000 | 10013 | /************** End of opcodes.h *********************************************/ |
| 10001 | 10014 | /************** Continuing where we left off in vdbe.h ***********************/ |
| 10002 | 10015 | |
| 10003 | 10016 | /* |
| | @@ -10008,10 +10021,11 @@ |
| 10008 | 10021 | SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int); |
| 10009 | 10022 | SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int); |
| 10010 | 10023 | SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int); |
| 10011 | 10024 | SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int); |
| 10012 | 10025 | SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int); |
| 10026 | +SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int); |
| 10013 | 10027 | SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int); |
| 10014 | 10028 | SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno); |
| 10015 | 10029 | SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*); |
| 10016 | 10030 | SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1); |
| 10017 | 10031 | SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2); |
| | @@ -10400,18 +10414,18 @@ |
| 10400 | 10414 | PgHdr *pDirtyNext; /* Next element in list of dirty pages */ |
| 10401 | 10415 | PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */ |
| 10402 | 10416 | }; |
| 10403 | 10417 | |
| 10404 | 10418 | /* Bit values for PgHdr.flags */ |
| 10405 | | -#define PGHDR_DIRTY 0x002 /* Page has changed */ |
| 10406 | | -#define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before |
| 10407 | | - ** writing this page to the database */ |
| 10408 | | -#define PGHDR_NEED_READ 0x008 /* Content is unread */ |
| 10409 | | -#define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */ |
| 10410 | | -#define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */ |
| 10411 | | - |
| 10412 | | -#define PGHDR_MMAP 0x040 /* This is an mmap page object */ |
| 10419 | +#define PGHDR_CLEAN 0x001 /* Page not on the PCache.pDirty list */ |
| 10420 | +#define PGHDR_DIRTY 0x002 /* Page is on the PCache.pDirty list */ |
| 10421 | +#define PGHDR_WRITEABLE 0x004 /* Journaled and ready to modify */ |
| 10422 | +#define PGHDR_NEED_SYNC 0x008 /* Fsync the rollback journal before |
| 10423 | + ** writing this page to the database */ |
| 10424 | +#define PGHDR_NEED_READ 0x010 /* Content is unread */ |
| 10425 | +#define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */ |
| 10426 | +#define PGHDR_MMAP 0x040 /* This is an mmap page object */ |
| 10413 | 10427 | |
| 10414 | 10428 | /* Initialize and shutdown the page cache subsystem */ |
| 10415 | 10429 | SQLITE_PRIVATE int sqlite3PcacheInitialize(void); |
| 10416 | 10430 | SQLITE_PRIVATE void sqlite3PcacheShutdown(void); |
| 10417 | 10431 | |
| | @@ -11439,13 +11453,13 @@ |
| 11439 | 11453 | ** But rather than start with 0 or 1, we begin with 'A'. That way, |
| 11440 | 11454 | ** when multiple affinity types are concatenated into a string and |
| 11441 | 11455 | ** used as the P4 operand, they will be more readable. |
| 11442 | 11456 | ** |
| 11443 | 11457 | ** Note also that the numeric types are grouped together so that testing |
| 11444 | | -** for a numeric type is a single comparison. And the NONE type is first. |
| 11458 | +** for a numeric type is a single comparison. And the BLOB type is first. |
| 11445 | 11459 | */ |
| 11446 | | -#define SQLITE_AFF_NONE 'A' |
| 11460 | +#define SQLITE_AFF_BLOB 'A' |
| 11447 | 11461 | #define SQLITE_AFF_TEXT 'B' |
| 11448 | 11462 | #define SQLITE_AFF_NUMERIC 'C' |
| 11449 | 11463 | #define SQLITE_AFF_INTEGER 'D' |
| 11450 | 11464 | #define SQLITE_AFF_REAL 'E' |
| 11451 | 11465 | |
| | @@ -12198,11 +12212,11 @@ |
| 12198 | 12212 | #endif |
| 12199 | 12213 | int iCursor; /* The VDBE cursor number used to access this table */ |
| 12200 | 12214 | Expr *pOn; /* The ON clause of a join */ |
| 12201 | 12215 | IdList *pUsing; /* The USING clause of a join */ |
| 12202 | 12216 | Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */ |
| 12203 | | - char *zIndex; /* Identifier from "INDEXED BY <zIndex>" clause */ |
| 12217 | + char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */ |
| 12204 | 12218 | Index *pIndex; /* Index structure corresponding to zIndex, if any */ |
| 12205 | 12219 | } a[1]; /* One entry for each identifier on the list */ |
| 12206 | 12220 | }; |
| 12207 | 12221 | |
| 12208 | 12222 | /* |
| | @@ -13004,11 +13018,13 @@ |
| 13004 | 13018 | # define sqlite3Isalpha(x) isalpha((unsigned char)(x)) |
| 13005 | 13019 | # define sqlite3Isdigit(x) isdigit((unsigned char)(x)) |
| 13006 | 13020 | # define sqlite3Isxdigit(x) isxdigit((unsigned char)(x)) |
| 13007 | 13021 | # define sqlite3Tolower(x) tolower((unsigned char)(x)) |
| 13008 | 13022 | #endif |
| 13023 | +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 13009 | 13024 | SQLITE_PRIVATE int sqlite3IsIdChar(u8); |
| 13025 | +#endif |
| 13010 | 13026 | |
| 13011 | 13027 | /* |
| 13012 | 13028 | ** Internal function prototypes |
| 13013 | 13029 | */ |
| 13014 | 13030 | #define sqlite3StrICmp sqlite3_stricmp |
| | @@ -13032,11 +13048,13 @@ |
| 13032 | 13048 | SQLITE_PRIVATE void *sqlite3ScratchMalloc(int); |
| 13033 | 13049 | SQLITE_PRIVATE void sqlite3ScratchFree(void*); |
| 13034 | 13050 | SQLITE_PRIVATE void *sqlite3PageMalloc(int); |
| 13035 | 13051 | SQLITE_PRIVATE void sqlite3PageFree(void*); |
| 13036 | 13052 | SQLITE_PRIVATE void sqlite3MemSetDefault(void); |
| 13053 | +#ifndef SQLITE_OMIT_BUILTIN_TEST |
| 13037 | 13054 | SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void)); |
| 13055 | +#endif |
| 13038 | 13056 | SQLITE_PRIVATE int sqlite3HeapNearlyFull(void); |
| 13039 | 13057 | |
| 13040 | 13058 | /* |
| 13041 | 13059 | ** On systems with ample stack space and that support alloca(), make |
| 13042 | 13060 | ** use of alloca() to obtain space for large automatic objects. By default, |
| | @@ -13108,14 +13126,10 @@ |
| 13108 | 13126 | #if defined(SQLITE_TEST) |
| 13109 | 13127 | SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*); |
| 13110 | 13128 | #endif |
| 13111 | 13129 | |
| 13112 | 13130 | #if defined(SQLITE_DEBUG) |
| 13113 | | -SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView*,u8); |
| 13114 | | -SQLITE_PRIVATE void sqlite3TreeViewPop(TreeView*); |
| 13115 | | -SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView*, const char*, ...); |
| 13116 | | -SQLITE_PRIVATE void sqlite3TreeViewItem(TreeView*, const char*, u8); |
| 13117 | 13131 | SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView*, const Expr*, u8); |
| 13118 | 13132 | SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*); |
| 13119 | 13133 | SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8); |
| 13120 | 13134 | #endif |
| 13121 | 13135 | |
| | @@ -13176,15 +13190,18 @@ |
| 13176 | 13190 | SQLITE_PRIVATE int sqlite3FaultSim(int); |
| 13177 | 13191 | #endif |
| 13178 | 13192 | |
| 13179 | 13193 | SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32); |
| 13180 | 13194 | SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32); |
| 13195 | +SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec*, u32); |
| 13181 | 13196 | SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32); |
| 13182 | 13197 | SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*); |
| 13183 | 13198 | SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*); |
| 13184 | 13199 | SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*); |
| 13200 | +#ifndef SQLITE_OMIT_BUILTIN_TEST |
| 13185 | 13201 | SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*); |
| 13202 | +#endif |
| 13186 | 13203 | |
| 13187 | 13204 | SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int); |
| 13188 | 13205 | SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*); |
| 13189 | 13206 | SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64); |
| 13190 | 13207 | SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64); |
| | @@ -13268,10 +13285,11 @@ |
| 13268 | 13285 | SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8); |
| 13269 | 13286 | #define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */ |
| 13270 | 13287 | #define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */ |
| 13271 | 13288 | SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int); |
| 13272 | 13289 | SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int); |
| 13290 | +SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int); |
| 13273 | 13291 | SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*); |
| 13274 | 13292 | SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*); |
| 13275 | 13293 | SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *); |
| 13276 | 13294 | SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*); |
| 13277 | 13295 | SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*); |
| | @@ -13284,12 +13302,14 @@ |
| 13284 | 13302 | SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr*, Expr*, int); |
| 13285 | 13303 | SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*); |
| 13286 | 13304 | SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*); |
| 13287 | 13305 | SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*); |
| 13288 | 13306 | SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*); |
| 13307 | +#ifndef SQLITE_OMIT_BUILTIN_TEST |
| 13289 | 13308 | SQLITE_PRIVATE void sqlite3PrngSaveState(void); |
| 13290 | 13309 | SQLITE_PRIVATE void sqlite3PrngRestoreState(void); |
| 13310 | +#endif |
| 13291 | 13311 | SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int); |
| 13292 | 13312 | SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int); |
| 13293 | 13313 | SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb); |
| 13294 | 13314 | SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int); |
| 13295 | 13315 | SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*); |
| | @@ -13503,10 +13523,11 @@ |
| 13503 | 13523 | SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *); |
| 13504 | 13524 | SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...); |
| 13505 | 13525 | SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*); |
| 13506 | 13526 | SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int); |
| 13507 | 13527 | SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*); |
| 13528 | +SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p); |
| 13508 | 13529 | SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*); |
| 13509 | 13530 | SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*); |
| 13510 | 13531 | SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*); |
| 13511 | 13532 | SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*); |
| 13512 | 13533 | SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*); |
| | @@ -14620,10 +14641,13 @@ |
| 14620 | 14641 | Pgno pgnoRoot; /* Root page of the open btree cursor */ |
| 14621 | 14642 | sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */ |
| 14622 | 14643 | i64 seqCount; /* Sequence counter */ |
| 14623 | 14644 | i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ |
| 14624 | 14645 | VdbeSorter *pSorter; /* Sorter object for OP_SorterOpen cursors */ |
| 14646 | +#ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
| 14647 | + u64 maskUsed; /* Mask of columns used by this cursor */ |
| 14648 | +#endif |
| 14625 | 14649 | |
| 14626 | 14650 | /* Cached information about the header for the data record that the |
| 14627 | 14651 | ** cursor is currently pointing to. Only valid if cacheStatus matches |
| 14628 | 14652 | ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of |
| 14629 | 14653 | ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that |
| | @@ -14813,18 +14837,20 @@ |
| 14813 | 14837 | ** |
| 14814 | 14838 | ** This structure is defined inside of vdbeInt.h because it uses substructures |
| 14815 | 14839 | ** (Mem) which are only defined there. |
| 14816 | 14840 | */ |
| 14817 | 14841 | struct sqlite3_context { |
| 14818 | | - Mem *pOut; /* The return value is stored here */ |
| 14819 | | - FuncDef *pFunc; /* Pointer to function information */ |
| 14820 | | - Mem *pMem; /* Memory cell used to store aggregate context */ |
| 14821 | | - Vdbe *pVdbe; /* The VM that owns this context */ |
| 14822 | | - int iOp; /* Instruction number of OP_Function */ |
| 14823 | | - int isError; /* Error code returned by the function. */ |
| 14824 | | - u8 skipFlag; /* Skip accumulator loading if true */ |
| 14825 | | - u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */ |
| 14842 | + Mem *pOut; /* The return value is stored here */ |
| 14843 | + FuncDef *pFunc; /* Pointer to function information */ |
| 14844 | + Mem *pMem; /* Memory cell used to store aggregate context */ |
| 14845 | + Vdbe *pVdbe; /* The VM that owns this context */ |
| 14846 | + int iOp; /* Instruction number of OP_Function */ |
| 14847 | + int isError; /* Error code returned by the function. */ |
| 14848 | + u8 skipFlag; /* Skip accumulator loading if true */ |
| 14849 | + u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */ |
| 14850 | + u8 argc; /* Number of arguments */ |
| 14851 | + sqlite3_value *argv[1]; /* Argument set */ |
| 14826 | 14852 | }; |
| 14827 | 14853 | |
| 14828 | 14854 | /* |
| 14829 | 14855 | ** An Explain object accumulates indented output which is helpful |
| 14830 | 14856 | ** in describing recursive data structures. |
| | @@ -19193,13 +19219,18 @@ |
| 19193 | 19219 | if( sqlite3GlobalConfig.bCoreMutex ){ |
| 19194 | 19220 | pFrom = sqlite3DefaultMutex(); |
| 19195 | 19221 | }else{ |
| 19196 | 19222 | pFrom = sqlite3NoopMutex(); |
| 19197 | 19223 | } |
| 19198 | | - memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc)); |
| 19199 | | - memcpy(&pTo->xMutexFree, &pFrom->xMutexFree, |
| 19200 | | - sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree)); |
| 19224 | + pTo->xMutexInit = pFrom->xMutexInit; |
| 19225 | + pTo->xMutexEnd = pFrom->xMutexEnd; |
| 19226 | + pTo->xMutexFree = pFrom->xMutexFree; |
| 19227 | + pTo->xMutexEnter = pFrom->xMutexEnter; |
| 19228 | + pTo->xMutexTry = pFrom->xMutexTry; |
| 19229 | + pTo->xMutexLeave = pFrom->xMutexLeave; |
| 19230 | + pTo->xMutexHeld = pFrom->xMutexHeld; |
| 19231 | + pTo->xMutexNotheld = pFrom->xMutexNotheld; |
| 19201 | 19232 | pTo->xMutexAlloc = pFrom->xMutexAlloc; |
| 19202 | 19233 | } |
| 19203 | 19234 | rc = sqlite3GlobalConfig.mutex.xMutexInit(); |
| 19204 | 19235 | |
| 19205 | 19236 | #ifdef SQLITE_DEBUG |
| | @@ -21353,21 +21384,20 @@ |
| 21353 | 21384 | ** |
| 21354 | 21385 | ** The returned value is normally a copy of the second argument to this |
| 21355 | 21386 | ** function. However, if a malloc() failure has occurred since the previous |
| 21356 | 21387 | ** invocation SQLITE_NOMEM is returned instead. |
| 21357 | 21388 | ** |
| 21358 | | -** If the first argument, db, is not NULL and a malloc() error has occurred, |
| 21359 | | -** then the connection error-code (the value returned by sqlite3_errcode()) |
| 21360 | | -** is set to SQLITE_NOMEM. |
| 21389 | +** If an OOM as occurred, then the connection error-code (the value |
| 21390 | +** returned by sqlite3_errcode()) is set to SQLITE_NOMEM. |
| 21361 | 21391 | */ |
| 21362 | 21392 | SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){ |
| 21363 | | - /* If the db handle is not NULL, then we must hold the connection handle |
| 21364 | | - ** mutex here. Otherwise the read (and possible write) of db->mallocFailed |
| 21393 | + /* If the db handle must hold the connection handle mutex here. |
| 21394 | + ** Otherwise the read (and possible write) of db->mallocFailed |
| 21365 | 21395 | ** is unsafe, as is the call to sqlite3Error(). |
| 21366 | 21396 | */ |
| 21367 | | - assert( !db || sqlite3_mutex_held(db->mutex) ); |
| 21368 | | - if( db==0 ) return rc & 0xff; |
| 21397 | + assert( db!=0 ); |
| 21398 | + assert( sqlite3_mutex_held(db->mutex) ); |
| 21369 | 21399 | if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ |
| 21370 | 21400 | return apiOomError(db); |
| 21371 | 21401 | } |
| 21372 | 21402 | return rc & db->errMask; |
| 21373 | 21403 | } |
| | @@ -21374,21 +21404,18 @@ |
| 21374 | 21404 | |
| 21375 | 21405 | /************** End of malloc.c **********************************************/ |
| 21376 | 21406 | /************** Begin file printf.c ******************************************/ |
| 21377 | 21407 | /* |
| 21378 | 21408 | ** The "printf" code that follows dates from the 1980's. It is in |
| 21379 | | -** the public domain. The original comments are included here for |
| 21380 | | -** completeness. They are very out-of-date but might be useful as |
| 21381 | | -** an historical reference. Most of the "enhancements" have been backed |
| 21382 | | -** out so that the functionality is now the same as standard printf(). |
| 21409 | +** the public domain. |
| 21383 | 21410 | ** |
| 21384 | 21411 | ************************************************************************** |
| 21385 | 21412 | ** |
| 21386 | 21413 | ** This file contains code for a set of "printf"-like routines. These |
| 21387 | 21414 | ** routines format strings much like the printf() from the standard C |
| 21388 | 21415 | ** library, though the implementation here has enhancements to support |
| 21389 | | -** SQLlite. |
| 21416 | +** SQLite. |
| 21390 | 21417 | */ |
| 21391 | 21418 | |
| 21392 | 21419 | /* |
| 21393 | 21420 | ** Conversion types fall into various categories as defined by the |
| 21394 | 21421 | ** following enumeration. |
| | @@ -22431,26 +22458,49 @@ |
| 22431 | 22458 | fprintf(stdout,"%s", zBuf); |
| 22432 | 22459 | fflush(stdout); |
| 22433 | 22460 | } |
| 22434 | 22461 | #endif |
| 22435 | 22462 | |
| 22463 | + |
| 22464 | +/* |
| 22465 | +** variable-argument wrapper around sqlite3VXPrintf(). |
| 22466 | +*/ |
| 22467 | +SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){ |
| 22468 | + va_list ap; |
| 22469 | + va_start(ap,zFormat); |
| 22470 | + sqlite3VXPrintf(p, bFlags, zFormat, ap); |
| 22471 | + va_end(ap); |
| 22472 | +} |
| 22473 | + |
| 22474 | +/************** End of printf.c **********************************************/ |
| 22475 | +/************** Begin file treeview.c ****************************************/ |
| 22476 | +/* |
| 22477 | +** 2015-06-08 |
| 22478 | +** |
| 22479 | +** The author disclaims copyright to this source code. In place of |
| 22480 | +** a legal notice, here is a blessing: |
| 22481 | +** |
| 22482 | +** May you do good and not evil. |
| 22483 | +** May you find forgiveness for yourself and forgive others. |
| 22484 | +** May you share freely, never taking more than you give. |
| 22485 | +** |
| 22486 | +************************************************************************* |
| 22487 | +** |
| 22488 | +** This file contains C code to implement the TreeView debugging routines. |
| 22489 | +** These routines print a parse tree to standard output for debugging and |
| 22490 | +** analysis. |
| 22491 | +** |
| 22492 | +** The interfaces in this file is only available when compiling |
| 22493 | +** with SQLITE_DEBUG. |
| 22494 | +*/ |
| 22436 | 22495 | #ifdef SQLITE_DEBUG |
| 22437 | | -/************************************************************************* |
| 22438 | | -** Routines for implementing the "TreeView" display of hierarchical |
| 22439 | | -** data structures for debugging. |
| 22440 | | -** |
| 22441 | | -** The main entry points (coded elsewhere) are: |
| 22442 | | -** sqlite3TreeViewExpr(0, pExpr, 0); |
| 22443 | | -** sqlite3TreeViewExprList(0, pList, 0, 0); |
| 22444 | | -** sqlite3TreeViewSelect(0, pSelect, 0); |
| 22445 | | -** Insert calls to those routines while debugging in order to display |
| 22446 | | -** a diagram of Expr, ExprList, and Select objects. |
| 22447 | | -** |
| 22496 | + |
| 22497 | +/* |
| 22498 | +** Add a new subitem to the tree. The moreToFollow flag indicates that this |
| 22499 | +** is not the last item in the tree. |
| 22448 | 22500 | */ |
| 22449 | | -/* Add a new subitem to the tree. The moreToFollow flag indicates that this |
| 22450 | | -** is not the last item in the tree. */ |
| 22451 | | -SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){ |
| 22501 | +static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){ |
| 22452 | 22502 | if( p==0 ){ |
| 22453 | 22503 | p = sqlite3_malloc64( sizeof(*p) ); |
| 22454 | 22504 | if( p==0 ) return 0; |
| 22455 | 22505 | memset(p, 0, sizeof(*p)); |
| 22456 | 22506 | }else{ |
| | @@ -22458,19 +22508,25 @@ |
| 22458 | 22508 | } |
| 22459 | 22509 | assert( moreToFollow==0 || moreToFollow==1 ); |
| 22460 | 22510 | if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow; |
| 22461 | 22511 | return p; |
| 22462 | 22512 | } |
| 22463 | | -/* Finished with one layer of the tree */ |
| 22464 | | -SQLITE_PRIVATE void sqlite3TreeViewPop(TreeView *p){ |
| 22513 | + |
| 22514 | +/* |
| 22515 | +** Finished with one layer of the tree |
| 22516 | +*/ |
| 22517 | +static void sqlite3TreeViewPop(TreeView *p){ |
| 22465 | 22518 | if( p==0 ) return; |
| 22466 | 22519 | p->iLevel--; |
| 22467 | 22520 | if( p->iLevel<0 ) sqlite3_free(p); |
| 22468 | 22521 | } |
| 22469 | | -/* Generate a single line of output for the tree, with a prefix that contains |
| 22470 | | -** all the appropriate tree lines */ |
| 22471 | | -SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){ |
| 22522 | + |
| 22523 | +/* |
| 22524 | +** Generate a single line of output for the tree, with a prefix that contains |
| 22525 | +** all the appropriate tree lines |
| 22526 | +*/ |
| 22527 | +static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){ |
| 22472 | 22528 | va_list ap; |
| 22473 | 22529 | int i; |
| 22474 | 22530 | StrAccum acc; |
| 22475 | 22531 | char zBuf[500]; |
| 22476 | 22532 | sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
| | @@ -22486,28 +22542,371 @@ |
| 22486 | 22542 | if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1); |
| 22487 | 22543 | sqlite3StrAccumFinish(&acc); |
| 22488 | 22544 | fprintf(stdout,"%s", zBuf); |
| 22489 | 22545 | fflush(stdout); |
| 22490 | 22546 | } |
| 22491 | | -/* Shorthand for starting a new tree item that consists of a single label */ |
| 22492 | | -SQLITE_PRIVATE void sqlite3TreeViewItem(TreeView *p, const char *zLabel, u8 moreToFollow){ |
| 22493 | | - p = sqlite3TreeViewPush(p, moreToFollow); |
| 22547 | + |
| 22548 | +/* |
| 22549 | +** Shorthand for starting a new tree item that consists of a single label |
| 22550 | +*/ |
| 22551 | +static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){ |
| 22552 | + p = sqlite3TreeViewPush(p, moreFollows); |
| 22494 | 22553 | sqlite3TreeViewLine(p, "%s", zLabel); |
| 22495 | 22554 | } |
| 22555 | + |
| 22556 | + |
| 22557 | +/* |
| 22558 | +** Generate a human-readable description of a the Select object. |
| 22559 | +*/ |
| 22560 | +SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){ |
| 22561 | + int n = 0; |
| 22562 | + pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 22563 | + sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x", |
| 22564 | + ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), |
| 22565 | + ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags |
| 22566 | + ); |
| 22567 | + if( p->pSrc && p->pSrc->nSrc ) n++; |
| 22568 | + if( p->pWhere ) n++; |
| 22569 | + if( p->pGroupBy ) n++; |
| 22570 | + if( p->pHaving ) n++; |
| 22571 | + if( p->pOrderBy ) n++; |
| 22572 | + if( p->pLimit ) n++; |
| 22573 | + if( p->pOffset ) n++; |
| 22574 | + if( p->pPrior ) n++; |
| 22575 | + sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set"); |
| 22576 | + if( p->pSrc && p->pSrc->nSrc ){ |
| 22577 | + int i; |
| 22578 | + pView = sqlite3TreeViewPush(pView, (n--)>0); |
| 22579 | + sqlite3TreeViewLine(pView, "FROM"); |
| 22580 | + for(i=0; i<p->pSrc->nSrc; i++){ |
| 22581 | + struct SrcList_item *pItem = &p->pSrc->a[i]; |
| 22582 | + StrAccum x; |
| 22583 | + char zLine[100]; |
| 22584 | + sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); |
| 22585 | + sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor); |
| 22586 | + if( pItem->zDatabase ){ |
| 22587 | + sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName); |
| 22588 | + }else if( pItem->zName ){ |
| 22589 | + sqlite3XPrintf(&x, 0, " %s", pItem->zName); |
| 22590 | + } |
| 22591 | + if( pItem->pTab ){ |
| 22592 | + sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName); |
| 22593 | + } |
| 22594 | + if( pItem->zAlias ){ |
| 22595 | + sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias); |
| 22596 | + } |
| 22597 | + if( pItem->jointype & JT_LEFT ){ |
| 22598 | + sqlite3XPrintf(&x, 0, " LEFT-JOIN"); |
| 22599 | + } |
| 22600 | + sqlite3StrAccumFinish(&x); |
| 22601 | + sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); |
| 22602 | + if( pItem->pSelect ){ |
| 22603 | + sqlite3TreeViewSelect(pView, pItem->pSelect, 0); |
| 22604 | + } |
| 22605 | + sqlite3TreeViewPop(pView); |
| 22606 | + } |
| 22607 | + sqlite3TreeViewPop(pView); |
| 22608 | + } |
| 22609 | + if( p->pWhere ){ |
| 22610 | + sqlite3TreeViewItem(pView, "WHERE", (n--)>0); |
| 22611 | + sqlite3TreeViewExpr(pView, p->pWhere, 0); |
| 22612 | + sqlite3TreeViewPop(pView); |
| 22613 | + } |
| 22614 | + if( p->pGroupBy ){ |
| 22615 | + sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); |
| 22616 | + } |
| 22617 | + if( p->pHaving ){ |
| 22618 | + sqlite3TreeViewItem(pView, "HAVING", (n--)>0); |
| 22619 | + sqlite3TreeViewExpr(pView, p->pHaving, 0); |
| 22620 | + sqlite3TreeViewPop(pView); |
| 22621 | + } |
| 22622 | + if( p->pOrderBy ){ |
| 22623 | + sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); |
| 22624 | + } |
| 22625 | + if( p->pLimit ){ |
| 22626 | + sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); |
| 22627 | + sqlite3TreeViewExpr(pView, p->pLimit, 0); |
| 22628 | + sqlite3TreeViewPop(pView); |
| 22629 | + } |
| 22630 | + if( p->pOffset ){ |
| 22631 | + sqlite3TreeViewItem(pView, "OFFSET", (n--)>0); |
| 22632 | + sqlite3TreeViewExpr(pView, p->pOffset, 0); |
| 22633 | + sqlite3TreeViewPop(pView); |
| 22634 | + } |
| 22635 | + if( p->pPrior ){ |
| 22636 | + const char *zOp = "UNION"; |
| 22637 | + switch( p->op ){ |
| 22638 | + case TK_ALL: zOp = "UNION ALL"; break; |
| 22639 | + case TK_INTERSECT: zOp = "INTERSECT"; break; |
| 22640 | + case TK_EXCEPT: zOp = "EXCEPT"; break; |
| 22641 | + } |
| 22642 | + sqlite3TreeViewItem(pView, zOp, (n--)>0); |
| 22643 | + sqlite3TreeViewSelect(pView, p->pPrior, 0); |
| 22644 | + sqlite3TreeViewPop(pView); |
| 22645 | + } |
| 22646 | + sqlite3TreeViewPop(pView); |
| 22647 | +} |
| 22648 | + |
| 22649 | +/* |
| 22650 | +** Generate a human-readable explanation of an expression tree. |
| 22651 | +*/ |
| 22652 | +SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ |
| 22653 | + const char *zBinOp = 0; /* Binary operator */ |
| 22654 | + const char *zUniOp = 0; /* Unary operator */ |
| 22655 | + char zFlgs[30]; |
| 22656 | + pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 22657 | + if( pExpr==0 ){ |
| 22658 | + sqlite3TreeViewLine(pView, "nil"); |
| 22659 | + sqlite3TreeViewPop(pView); |
| 22660 | + return; |
| 22661 | + } |
| 22662 | + if( pExpr->flags ){ |
| 22663 | + sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags); |
| 22664 | + }else{ |
| 22665 | + zFlgs[0] = 0; |
| 22666 | + } |
| 22667 | + switch( pExpr->op ){ |
| 22668 | + case TK_AGG_COLUMN: { |
| 22669 | + sqlite3TreeViewLine(pView, "AGG{%d:%d}%s", |
| 22670 | + pExpr->iTable, pExpr->iColumn, zFlgs); |
| 22671 | + break; |
| 22672 | + } |
| 22673 | + case TK_COLUMN: { |
| 22674 | + if( pExpr->iTable<0 ){ |
| 22675 | + /* This only happens when coding check constraints */ |
| 22676 | + sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs); |
| 22677 | + }else{ |
| 22678 | + sqlite3TreeViewLine(pView, "{%d:%d}%s", |
| 22679 | + pExpr->iTable, pExpr->iColumn, zFlgs); |
| 22680 | + } |
| 22681 | + break; |
| 22682 | + } |
| 22683 | + case TK_INTEGER: { |
| 22684 | + if( pExpr->flags & EP_IntValue ){ |
| 22685 | + sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); |
| 22686 | + }else{ |
| 22687 | + sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); |
| 22688 | + } |
| 22689 | + break; |
| 22690 | + } |
| 22691 | +#ifndef SQLITE_OMIT_FLOATING_POINT |
| 22692 | + case TK_FLOAT: { |
| 22693 | + sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 22694 | + break; |
| 22695 | + } |
| 22696 | +#endif |
| 22697 | + case TK_STRING: { |
| 22698 | + sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); |
| 22699 | + break; |
| 22700 | + } |
| 22701 | + case TK_NULL: { |
| 22702 | + sqlite3TreeViewLine(pView,"NULL"); |
| 22703 | + break; |
| 22704 | + } |
| 22705 | +#ifndef SQLITE_OMIT_BLOB_LITERAL |
| 22706 | + case TK_BLOB: { |
| 22707 | + sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 22708 | + break; |
| 22709 | + } |
| 22710 | +#endif |
| 22711 | + case TK_VARIABLE: { |
| 22712 | + sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)", |
| 22713 | + pExpr->u.zToken, pExpr->iColumn); |
| 22714 | + break; |
| 22715 | + } |
| 22716 | + case TK_REGISTER: { |
| 22717 | + sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); |
| 22718 | + break; |
| 22719 | + } |
| 22720 | + case TK_AS: { |
| 22721 | + sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken); |
| 22722 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 22723 | + break; |
| 22724 | + } |
| 22725 | + case TK_ID: { |
| 22726 | + sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken); |
| 22727 | + break; |
| 22728 | + } |
| 22729 | +#ifndef SQLITE_OMIT_CAST |
| 22730 | + case TK_CAST: { |
| 22731 | + /* Expressions of the form: CAST(pLeft AS token) */ |
| 22732 | + sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); |
| 22733 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 22734 | + break; |
| 22735 | + } |
| 22736 | +#endif /* SQLITE_OMIT_CAST */ |
| 22737 | + case TK_LT: zBinOp = "LT"; break; |
| 22738 | + case TK_LE: zBinOp = "LE"; break; |
| 22739 | + case TK_GT: zBinOp = "GT"; break; |
| 22740 | + case TK_GE: zBinOp = "GE"; break; |
| 22741 | + case TK_NE: zBinOp = "NE"; break; |
| 22742 | + case TK_EQ: zBinOp = "EQ"; break; |
| 22743 | + case TK_IS: zBinOp = "IS"; break; |
| 22744 | + case TK_ISNOT: zBinOp = "ISNOT"; break; |
| 22745 | + case TK_AND: zBinOp = "AND"; break; |
| 22746 | + case TK_OR: zBinOp = "OR"; break; |
| 22747 | + case TK_PLUS: zBinOp = "ADD"; break; |
| 22748 | + case TK_STAR: zBinOp = "MUL"; break; |
| 22749 | + case TK_MINUS: zBinOp = "SUB"; break; |
| 22750 | + case TK_REM: zBinOp = "REM"; break; |
| 22751 | + case TK_BITAND: zBinOp = "BITAND"; break; |
| 22752 | + case TK_BITOR: zBinOp = "BITOR"; break; |
| 22753 | + case TK_SLASH: zBinOp = "DIV"; break; |
| 22754 | + case TK_LSHIFT: zBinOp = "LSHIFT"; break; |
| 22755 | + case TK_RSHIFT: zBinOp = "RSHIFT"; break; |
| 22756 | + case TK_CONCAT: zBinOp = "CONCAT"; break; |
| 22757 | + case TK_DOT: zBinOp = "DOT"; break; |
| 22758 | + |
| 22759 | + case TK_UMINUS: zUniOp = "UMINUS"; break; |
| 22760 | + case TK_UPLUS: zUniOp = "UPLUS"; break; |
| 22761 | + case TK_BITNOT: zUniOp = "BITNOT"; break; |
| 22762 | + case TK_NOT: zUniOp = "NOT"; break; |
| 22763 | + case TK_ISNULL: zUniOp = "ISNULL"; break; |
| 22764 | + case TK_NOTNULL: zUniOp = "NOTNULL"; break; |
| 22765 | + |
| 22766 | + case TK_COLLATE: { |
| 22767 | + sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken); |
| 22768 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 22769 | + break; |
| 22770 | + } |
| 22771 | + |
| 22772 | + case TK_AGG_FUNCTION: |
| 22773 | + case TK_FUNCTION: { |
| 22774 | + ExprList *pFarg; /* List of function arguments */ |
| 22775 | + if( ExprHasProperty(pExpr, EP_TokenOnly) ){ |
| 22776 | + pFarg = 0; |
| 22777 | + }else{ |
| 22778 | + pFarg = pExpr->x.pList; |
| 22779 | + } |
| 22780 | + if( pExpr->op==TK_AGG_FUNCTION ){ |
| 22781 | + sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q", |
| 22782 | + pExpr->op2, pExpr->u.zToken); |
| 22783 | + }else{ |
| 22784 | + sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken); |
| 22785 | + } |
| 22786 | + if( pFarg ){ |
| 22787 | + sqlite3TreeViewExprList(pView, pFarg, 0, 0); |
| 22788 | + } |
| 22789 | + break; |
| 22790 | + } |
| 22791 | +#ifndef SQLITE_OMIT_SUBQUERY |
| 22792 | + case TK_EXISTS: { |
| 22793 | + sqlite3TreeViewLine(pView, "EXISTS-expr"); |
| 22794 | + sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 22795 | + break; |
| 22796 | + } |
| 22797 | + case TK_SELECT: { |
| 22798 | + sqlite3TreeViewLine(pView, "SELECT-expr"); |
| 22799 | + sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 22800 | + break; |
| 22801 | + } |
| 22802 | + case TK_IN: { |
| 22803 | + sqlite3TreeViewLine(pView, "IN"); |
| 22804 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 22805 | + if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 22806 | + sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 22807 | + }else{ |
| 22808 | + sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 22809 | + } |
| 22810 | + break; |
| 22811 | + } |
| 22812 | +#endif /* SQLITE_OMIT_SUBQUERY */ |
| 22813 | + |
| 22814 | + /* |
| 22815 | + ** x BETWEEN y AND z |
| 22816 | + ** |
| 22817 | + ** This is equivalent to |
| 22818 | + ** |
| 22819 | + ** x>=y AND x<=z |
| 22820 | + ** |
| 22821 | + ** X is stored in pExpr->pLeft. |
| 22822 | + ** Y is stored in pExpr->pList->a[0].pExpr. |
| 22823 | + ** Z is stored in pExpr->pList->a[1].pExpr. |
| 22824 | + */ |
| 22825 | + case TK_BETWEEN: { |
| 22826 | + Expr *pX = pExpr->pLeft; |
| 22827 | + Expr *pY = pExpr->x.pList->a[0].pExpr; |
| 22828 | + Expr *pZ = pExpr->x.pList->a[1].pExpr; |
| 22829 | + sqlite3TreeViewLine(pView, "BETWEEN"); |
| 22830 | + sqlite3TreeViewExpr(pView, pX, 1); |
| 22831 | + sqlite3TreeViewExpr(pView, pY, 1); |
| 22832 | + sqlite3TreeViewExpr(pView, pZ, 0); |
| 22833 | + break; |
| 22834 | + } |
| 22835 | + case TK_TRIGGER: { |
| 22836 | + /* If the opcode is TK_TRIGGER, then the expression is a reference |
| 22837 | + ** to a column in the new.* or old.* pseudo-tables available to |
| 22838 | + ** trigger programs. In this case Expr.iTable is set to 1 for the |
| 22839 | + ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn |
| 22840 | + ** is set to the column of the pseudo-table to read, or to -1 to |
| 22841 | + ** read the rowid field. |
| 22842 | + */ |
| 22843 | + sqlite3TreeViewLine(pView, "%s(%d)", |
| 22844 | + pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); |
| 22845 | + break; |
| 22846 | + } |
| 22847 | + case TK_CASE: { |
| 22848 | + sqlite3TreeViewLine(pView, "CASE"); |
| 22849 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 22850 | + sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 22851 | + break; |
| 22852 | + } |
| 22853 | +#ifndef SQLITE_OMIT_TRIGGER |
| 22854 | + case TK_RAISE: { |
| 22855 | + const char *zType = "unk"; |
| 22856 | + switch( pExpr->affinity ){ |
| 22857 | + case OE_Rollback: zType = "rollback"; break; |
| 22858 | + case OE_Abort: zType = "abort"; break; |
| 22859 | + case OE_Fail: zType = "fail"; break; |
| 22860 | + case OE_Ignore: zType = "ignore"; break; |
| 22861 | + } |
| 22862 | + sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); |
| 22863 | + break; |
| 22864 | + } |
| 22865 | +#endif |
| 22866 | + default: { |
| 22867 | + sqlite3TreeViewLine(pView, "op=%d", pExpr->op); |
| 22868 | + break; |
| 22869 | + } |
| 22870 | + } |
| 22871 | + if( zBinOp ){ |
| 22872 | + sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs); |
| 22873 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 22874 | + sqlite3TreeViewExpr(pView, pExpr->pRight, 0); |
| 22875 | + }else if( zUniOp ){ |
| 22876 | + sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs); |
| 22877 | + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 22878 | + } |
| 22879 | + sqlite3TreeViewPop(pView); |
| 22880 | +} |
| 22881 | + |
| 22882 | +/* |
| 22883 | +** Generate a human-readable explanation of an expression list. |
| 22884 | +*/ |
| 22885 | +SQLITE_PRIVATE void sqlite3TreeViewExprList( |
| 22886 | + TreeView *pView, |
| 22887 | + const ExprList *pList, |
| 22888 | + u8 moreToFollow, |
| 22889 | + const char *zLabel |
| 22890 | +){ |
| 22891 | + int i; |
| 22892 | + pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 22893 | + if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST"; |
| 22894 | + if( pList==0 ){ |
| 22895 | + sqlite3TreeViewLine(pView, "%s (empty)", zLabel); |
| 22896 | + }else{ |
| 22897 | + sqlite3TreeViewLine(pView, "%s", zLabel); |
| 22898 | + for(i=0; i<pList->nExpr; i++){ |
| 22899 | + sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1); |
| 22900 | + } |
| 22901 | + } |
| 22902 | + sqlite3TreeViewPop(pView); |
| 22903 | +} |
| 22904 | + |
| 22496 | 22905 | #endif /* SQLITE_DEBUG */ |
| 22497 | 22906 | |
| 22498 | | -/* |
| 22499 | | -** variable-argument wrapper around sqlite3VXPrintf(). |
| 22500 | | -*/ |
| 22501 | | -SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){ |
| 22502 | | - va_list ap; |
| 22503 | | - va_start(ap,zFormat); |
| 22504 | | - sqlite3VXPrintf(p, bFlags, zFormat, ap); |
| 22505 | | - va_end(ap); |
| 22506 | | -} |
| 22507 | | - |
| 22508 | | -/************** End of printf.c **********************************************/ |
| 22907 | +/************** End of treeview.c ********************************************/ |
| 22509 | 22908 | /************** Begin file random.c ******************************************/ |
| 22510 | 22909 | /* |
| 22511 | 22910 | ** 2001 September 15 |
| 22512 | 22911 | ** |
| 22513 | 22912 | ** The author disclaims copyright to this source code. In place of |
| | @@ -23544,14 +23943,12 @@ |
| 23544 | 23943 | ** The value returned will never be negative. Nor will it ever be greater |
| 23545 | 23944 | ** than the actual length of the string. For very long strings (greater |
| 23546 | 23945 | ** than 1GiB) the value returned might be less than the true string length. |
| 23547 | 23946 | */ |
| 23548 | 23947 | SQLITE_PRIVATE int sqlite3Strlen30(const char *z){ |
| 23549 | | - const char *z2 = z; |
| 23550 | 23948 | if( z==0 ) return 0; |
| 23551 | | - while( *z2 ){ z2++; } |
| 23552 | | - return 0x3fffffff & (int)(z2 - z); |
| 23949 | + return 0x3fffffff & (int)strlen(z); |
| 23553 | 23950 | } |
| 23554 | 23951 | |
| 23555 | 23952 | /* |
| 23556 | 23953 | ** Set the current error code to err_code and clear any prior error message. |
| 23557 | 23954 | */ |
| | @@ -24519,18 +24916,35 @@ |
| 24519 | 24916 | |
| 24520 | 24917 | /* |
| 24521 | 24918 | ** Read or write a four-byte big-endian integer value. |
| 24522 | 24919 | */ |
| 24523 | 24920 | SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){ |
| 24921 | +#if SQLITE_BYTEORDER==4321 |
| 24922 | + u32 x; |
| 24923 | + memcpy(&x,p,4); |
| 24924 | + return x; |
| 24925 | +#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) |
| 24926 | + u32 x; |
| 24927 | + memcpy(&x,p,4); |
| 24928 | + return __builtin_bswap32(x); |
| 24929 | +#else |
| 24524 | 24930 | testcase( p[0]&0x80 ); |
| 24525 | 24931 | return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
| 24932 | +#endif |
| 24526 | 24933 | } |
| 24527 | 24934 | SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){ |
| 24935 | +#if SQLITE_BYTEORDER==4321 |
| 24936 | + memcpy(p,&v,4); |
| 24937 | +#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) |
| 24938 | + u32 x = __builtin_bswap32(v); |
| 24939 | + memcpy(p,&x,4); |
| 24940 | +#else |
| 24528 | 24941 | p[0] = (u8)(v>>24); |
| 24529 | 24942 | p[1] = (u8)(v>>16); |
| 24530 | 24943 | p[2] = (u8)(v>>8); |
| 24531 | 24944 | p[3] = (u8)v; |
| 24945 | +#endif |
| 24532 | 24946 | } |
| 24533 | 24947 | |
| 24534 | 24948 | |
| 24535 | 24949 | |
| 24536 | 24950 | /* |
| | @@ -25094,46 +25508,46 @@ |
| 25094 | 25508 | #else |
| 25095 | 25509 | # define OpHelp(X) |
| 25096 | 25510 | #endif |
| 25097 | 25511 | SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){ |
| 25098 | 25512 | static const char *const azName[] = { "?", |
| 25099 | | - /* 1 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"), |
| 25100 | | - /* 2 */ "Savepoint" OpHelp(""), |
| 25101 | | - /* 3 */ "AutoCommit" OpHelp(""), |
| 25102 | | - /* 4 */ "Transaction" OpHelp(""), |
| 25103 | | - /* 5 */ "SorterNext" OpHelp(""), |
| 25104 | | - /* 6 */ "PrevIfOpen" OpHelp(""), |
| 25105 | | - /* 7 */ "NextIfOpen" OpHelp(""), |
| 25106 | | - /* 8 */ "Prev" OpHelp(""), |
| 25107 | | - /* 9 */ "Next" OpHelp(""), |
| 25108 | | - /* 10 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"), |
| 25109 | | - /* 11 */ "Checkpoint" OpHelp(""), |
| 25110 | | - /* 12 */ "JournalMode" OpHelp(""), |
| 25111 | | - /* 13 */ "Vacuum" OpHelp(""), |
| 25112 | | - /* 14 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"), |
| 25113 | | - /* 15 */ "VUpdate" OpHelp("data=r[P3@P2]"), |
| 25114 | | - /* 16 */ "Goto" OpHelp(""), |
| 25115 | | - /* 17 */ "Gosub" OpHelp(""), |
| 25116 | | - /* 18 */ "Return" OpHelp(""), |
| 25513 | + /* 1 */ "Savepoint" OpHelp(""), |
| 25514 | + /* 2 */ "AutoCommit" OpHelp(""), |
| 25515 | + /* 3 */ "Transaction" OpHelp(""), |
| 25516 | + /* 4 */ "SorterNext" OpHelp(""), |
| 25517 | + /* 5 */ "PrevIfOpen" OpHelp(""), |
| 25518 | + /* 6 */ "NextIfOpen" OpHelp(""), |
| 25519 | + /* 7 */ "Prev" OpHelp(""), |
| 25520 | + /* 8 */ "Next" OpHelp(""), |
| 25521 | + /* 9 */ "Checkpoint" OpHelp(""), |
| 25522 | + /* 10 */ "JournalMode" OpHelp(""), |
| 25523 | + /* 11 */ "Vacuum" OpHelp(""), |
| 25524 | + /* 12 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"), |
| 25525 | + /* 13 */ "VUpdate" OpHelp("data=r[P3@P2]"), |
| 25526 | + /* 14 */ "Goto" OpHelp(""), |
| 25527 | + /* 15 */ "Gosub" OpHelp(""), |
| 25528 | + /* 16 */ "Return" OpHelp(""), |
| 25529 | + /* 17 */ "InitCoroutine" OpHelp(""), |
| 25530 | + /* 18 */ "EndCoroutine" OpHelp(""), |
| 25117 | 25531 | /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"), |
| 25118 | | - /* 20 */ "InitCoroutine" OpHelp(""), |
| 25119 | | - /* 21 */ "EndCoroutine" OpHelp(""), |
| 25120 | | - /* 22 */ "Yield" OpHelp(""), |
| 25121 | | - /* 23 */ "HaltIfNull" OpHelp("if r[P3]=null halt"), |
| 25122 | | - /* 24 */ "Halt" OpHelp(""), |
| 25123 | | - /* 25 */ "Integer" OpHelp("r[P2]=P1"), |
| 25124 | | - /* 26 */ "Int64" OpHelp("r[P2]=P4"), |
| 25125 | | - /* 27 */ "String" OpHelp("r[P2]='P4' (len=P1)"), |
| 25126 | | - /* 28 */ "Null" OpHelp("r[P2..P3]=NULL"), |
| 25127 | | - /* 29 */ "SoftNull" OpHelp("r[P1]=NULL"), |
| 25128 | | - /* 30 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"), |
| 25129 | | - /* 31 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"), |
| 25130 | | - /* 32 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"), |
| 25131 | | - /* 33 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"), |
| 25132 | | - /* 34 */ "SCopy" OpHelp("r[P2]=r[P1]"), |
| 25133 | | - /* 35 */ "ResultRow" OpHelp("output=r[P1@P2]"), |
| 25134 | | - /* 36 */ "CollSeq" OpHelp(""), |
| 25532 | + /* 20 */ "Yield" OpHelp(""), |
| 25533 | + /* 21 */ "HaltIfNull" OpHelp("if r[P3]=null halt"), |
| 25534 | + /* 22 */ "Halt" OpHelp(""), |
| 25535 | + /* 23 */ "Integer" OpHelp("r[P2]=P1"), |
| 25536 | + /* 24 */ "Int64" OpHelp("r[P2]=P4"), |
| 25537 | + /* 25 */ "String" OpHelp("r[P2]='P4' (len=P1)"), |
| 25538 | + /* 26 */ "Null" OpHelp("r[P2..P3]=NULL"), |
| 25539 | + /* 27 */ "SoftNull" OpHelp("r[P1]=NULL"), |
| 25540 | + /* 28 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"), |
| 25541 | + /* 29 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"), |
| 25542 | + /* 30 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"), |
| 25543 | + /* 31 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"), |
| 25544 | + /* 32 */ "SCopy" OpHelp("r[P2]=r[P1]"), |
| 25545 | + /* 33 */ "ResultRow" OpHelp("output=r[P1@P2]"), |
| 25546 | + /* 34 */ "CollSeq" OpHelp(""), |
| 25547 | + /* 35 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"), |
| 25548 | + /* 36 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"), |
| 25135 | 25549 | /* 37 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"), |
| 25136 | 25550 | /* 38 */ "MustBeInt" OpHelp(""), |
| 25137 | 25551 | /* 39 */ "RealAffinity" OpHelp(""), |
| 25138 | 25552 | /* 40 */ "Cast" OpHelp("affinity(r[P1])"), |
| 25139 | 25553 | /* 41 */ "Permutation" OpHelp(""), |
| | @@ -25155,33 +25569,33 @@ |
| 25155 | 25569 | /* 57 */ "OpenEphemeral" OpHelp("nColumn=P2"), |
| 25156 | 25570 | /* 58 */ "SorterOpen" OpHelp(""), |
| 25157 | 25571 | /* 59 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"), |
| 25158 | 25572 | /* 60 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"), |
| 25159 | 25573 | /* 61 */ "Close" OpHelp(""), |
| 25160 | | - /* 62 */ "SeekLT" OpHelp("key=r[P3@P4]"), |
| 25161 | | - /* 63 */ "SeekLE" OpHelp("key=r[P3@P4]"), |
| 25162 | | - /* 64 */ "SeekGE" OpHelp("key=r[P3@P4]"), |
| 25163 | | - /* 65 */ "SeekGT" OpHelp("key=r[P3@P4]"), |
| 25164 | | - /* 66 */ "Seek" OpHelp("intkey=r[P2]"), |
| 25165 | | - /* 67 */ "NoConflict" OpHelp("key=r[P3@P4]"), |
| 25166 | | - /* 68 */ "NotFound" OpHelp("key=r[P3@P4]"), |
| 25167 | | - /* 69 */ "Found" OpHelp("key=r[P3@P4]"), |
| 25168 | | - /* 70 */ "NotExists" OpHelp("intkey=r[P3]"), |
| 25574 | + /* 62 */ "ColumnsUsed" OpHelp(""), |
| 25575 | + /* 63 */ "SeekLT" OpHelp("key=r[P3@P4]"), |
| 25576 | + /* 64 */ "SeekLE" OpHelp("key=r[P3@P4]"), |
| 25577 | + /* 65 */ "SeekGE" OpHelp("key=r[P3@P4]"), |
| 25578 | + /* 66 */ "SeekGT" OpHelp("key=r[P3@P4]"), |
| 25579 | + /* 67 */ "Seek" OpHelp("intkey=r[P2]"), |
| 25580 | + /* 68 */ "NoConflict" OpHelp("key=r[P3@P4]"), |
| 25581 | + /* 69 */ "NotFound" OpHelp("key=r[P3@P4]"), |
| 25582 | + /* 70 */ "Found" OpHelp("key=r[P3@P4]"), |
| 25169 | 25583 | /* 71 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"), |
| 25170 | 25584 | /* 72 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"), |
| 25171 | | - /* 73 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"), |
| 25172 | | - /* 74 */ "NewRowid" OpHelp("r[P2]=rowid"), |
| 25173 | | - /* 75 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"), |
| 25585 | + /* 73 */ "NotExists" OpHelp("intkey=r[P3]"), |
| 25586 | + /* 74 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"), |
| 25587 | + /* 75 */ "NewRowid" OpHelp("r[P2]=rowid"), |
| 25174 | 25588 | /* 76 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"), |
| 25175 | 25589 | /* 77 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"), |
| 25176 | 25590 | /* 78 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"), |
| 25177 | 25591 | /* 79 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"), |
| 25178 | 25592 | /* 80 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"), |
| 25179 | 25593 | /* 81 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"), |
| 25180 | 25594 | /* 82 */ "Lt" OpHelp("if r[P1]<r[P3] goto P2"), |
| 25181 | 25595 | /* 83 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"), |
| 25182 | | - /* 84 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"), |
| 25596 | + /* 84 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"), |
| 25183 | 25597 | /* 85 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"), |
| 25184 | 25598 | /* 86 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"), |
| 25185 | 25599 | /* 87 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"), |
| 25186 | 25600 | /* 88 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"), |
| 25187 | 25601 | /* 89 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"), |
| | @@ -25188,73 +25602,76 @@ |
| 25188 | 25602 | /* 90 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"), |
| 25189 | 25603 | /* 91 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"), |
| 25190 | 25604 | /* 92 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"), |
| 25191 | 25605 | /* 93 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"), |
| 25192 | 25606 | /* 94 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"), |
| 25193 | | - /* 95 */ "Delete" OpHelp(""), |
| 25607 | + /* 95 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"), |
| 25194 | 25608 | /* 96 */ "BitNot" OpHelp("r[P1]= ~r[P1]"), |
| 25195 | 25609 | /* 97 */ "String8" OpHelp("r[P2]='P4'"), |
| 25196 | | - /* 98 */ "ResetCount" OpHelp(""), |
| 25197 | | - /* 99 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"), |
| 25198 | | - /* 100 */ "SorterData" OpHelp("r[P2]=data"), |
| 25199 | | - /* 101 */ "RowKey" OpHelp("r[P2]=key"), |
| 25200 | | - /* 102 */ "RowData" OpHelp("r[P2]=data"), |
| 25201 | | - /* 103 */ "Rowid" OpHelp("r[P2]=rowid"), |
| 25202 | | - /* 104 */ "NullRow" OpHelp(""), |
| 25203 | | - /* 105 */ "Last" OpHelp(""), |
| 25204 | | - /* 106 */ "SorterSort" OpHelp(""), |
| 25205 | | - /* 107 */ "Sort" OpHelp(""), |
| 25206 | | - /* 108 */ "Rewind" OpHelp(""), |
| 25207 | | - /* 109 */ "SorterInsert" OpHelp(""), |
| 25208 | | - /* 110 */ "IdxInsert" OpHelp("key=r[P2]"), |
| 25209 | | - /* 111 */ "IdxDelete" OpHelp("key=r[P2@P3]"), |
| 25210 | | - /* 112 */ "IdxRowid" OpHelp("r[P2]=rowid"), |
| 25211 | | - /* 113 */ "IdxLE" OpHelp("key=r[P3@P4]"), |
| 25212 | | - /* 114 */ "IdxGT" OpHelp("key=r[P3@P4]"), |
| 25213 | | - /* 115 */ "IdxLT" OpHelp("key=r[P3@P4]"), |
| 25214 | | - /* 116 */ "IdxGE" OpHelp("key=r[P3@P4]"), |
| 25215 | | - /* 117 */ "Destroy" OpHelp(""), |
| 25216 | | - /* 118 */ "Clear" OpHelp(""), |
| 25217 | | - /* 119 */ "ResetSorter" OpHelp(""), |
| 25218 | | - /* 120 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"), |
| 25219 | | - /* 121 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"), |
| 25220 | | - /* 122 */ "ParseSchema" OpHelp(""), |
| 25221 | | - /* 123 */ "LoadAnalysis" OpHelp(""), |
| 25222 | | - /* 124 */ "DropTable" OpHelp(""), |
| 25223 | | - /* 125 */ "DropIndex" OpHelp(""), |
| 25224 | | - /* 126 */ "DropTrigger" OpHelp(""), |
| 25225 | | - /* 127 */ "IntegrityCk" OpHelp(""), |
| 25226 | | - /* 128 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"), |
| 25227 | | - /* 129 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"), |
| 25228 | | - /* 130 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"), |
| 25229 | | - /* 131 */ "Program" OpHelp(""), |
| 25230 | | - /* 132 */ "Param" OpHelp(""), |
| 25610 | + /* 98 */ "Delete" OpHelp(""), |
| 25611 | + /* 99 */ "ResetCount" OpHelp(""), |
| 25612 | + /* 100 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"), |
| 25613 | + /* 101 */ "SorterData" OpHelp("r[P2]=data"), |
| 25614 | + /* 102 */ "RowKey" OpHelp("r[P2]=key"), |
| 25615 | + /* 103 */ "RowData" OpHelp("r[P2]=data"), |
| 25616 | + /* 104 */ "Rowid" OpHelp("r[P2]=rowid"), |
| 25617 | + /* 105 */ "NullRow" OpHelp(""), |
| 25618 | + /* 106 */ "Last" OpHelp(""), |
| 25619 | + /* 107 */ "SorterSort" OpHelp(""), |
| 25620 | + /* 108 */ "Sort" OpHelp(""), |
| 25621 | + /* 109 */ "Rewind" OpHelp(""), |
| 25622 | + /* 110 */ "SorterInsert" OpHelp(""), |
| 25623 | + /* 111 */ "IdxInsert" OpHelp("key=r[P2]"), |
| 25624 | + /* 112 */ "IdxDelete" OpHelp("key=r[P2@P3]"), |
| 25625 | + /* 113 */ "IdxRowid" OpHelp("r[P2]=rowid"), |
| 25626 | + /* 114 */ "IdxLE" OpHelp("key=r[P3@P4]"), |
| 25627 | + /* 115 */ "IdxGT" OpHelp("key=r[P3@P4]"), |
| 25628 | + /* 116 */ "IdxLT" OpHelp("key=r[P3@P4]"), |
| 25629 | + /* 117 */ "IdxGE" OpHelp("key=r[P3@P4]"), |
| 25630 | + /* 118 */ "Destroy" OpHelp(""), |
| 25631 | + /* 119 */ "Clear" OpHelp(""), |
| 25632 | + /* 120 */ "ResetSorter" OpHelp(""), |
| 25633 | + /* 121 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"), |
| 25634 | + /* 122 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"), |
| 25635 | + /* 123 */ "ParseSchema" OpHelp(""), |
| 25636 | + /* 124 */ "LoadAnalysis" OpHelp(""), |
| 25637 | + /* 125 */ "DropTable" OpHelp(""), |
| 25638 | + /* 126 */ "DropIndex" OpHelp(""), |
| 25639 | + /* 127 */ "DropTrigger" OpHelp(""), |
| 25640 | + /* 128 */ "IntegrityCk" OpHelp(""), |
| 25641 | + /* 129 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"), |
| 25642 | + /* 130 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"), |
| 25643 | + /* 131 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"), |
| 25644 | + /* 132 */ "Program" OpHelp(""), |
| 25231 | 25645 | /* 133 */ "Real" OpHelp("r[P2]=P4"), |
| 25232 | | - /* 134 */ "FkCounter" OpHelp("fkctr[P1]+=P2"), |
| 25233 | | - /* 135 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"), |
| 25234 | | - /* 136 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"), |
| 25235 | | - /* 137 */ "IfPos" OpHelp("if r[P1]>0 goto P2"), |
| 25236 | | - /* 138 */ "IfNeg" OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"), |
| 25237 | | - /* 139 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]+=P3, goto P2"), |
| 25238 | | - /* 140 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"), |
| 25239 | | - /* 141 */ "JumpZeroIncr" OpHelp("if (r[P1]++)==0 ) goto P2"), |
| 25240 | | - /* 142 */ "AggFinal" OpHelp("accum=r[P1] N=P2"), |
| 25241 | | - /* 143 */ "IncrVacuum" OpHelp(""), |
| 25242 | | - /* 144 */ "Expire" OpHelp(""), |
| 25243 | | - /* 145 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"), |
| 25244 | | - /* 146 */ "VBegin" OpHelp(""), |
| 25245 | | - /* 147 */ "VCreate" OpHelp(""), |
| 25246 | | - /* 148 */ "VDestroy" OpHelp(""), |
| 25247 | | - /* 149 */ "VOpen" OpHelp(""), |
| 25248 | | - /* 150 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"), |
| 25249 | | - /* 151 */ "VNext" OpHelp(""), |
| 25250 | | - /* 152 */ "VRename" OpHelp(""), |
| 25251 | | - /* 153 */ "Pagecount" OpHelp(""), |
| 25252 | | - /* 154 */ "MaxPgcnt" OpHelp(""), |
| 25253 | | - /* 155 */ "Init" OpHelp("Start at P2"), |
| 25254 | | - /* 156 */ "Noop" OpHelp(""), |
| 25255 | | - /* 157 */ "Explain" OpHelp(""), |
| 25646 | + /* 134 */ "Param" OpHelp(""), |
| 25647 | + /* 135 */ "FkCounter" OpHelp("fkctr[P1]+=P2"), |
| 25648 | + /* 136 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"), |
| 25649 | + /* 137 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"), |
| 25650 | + /* 138 */ "IfPos" OpHelp("if r[P1]>0 goto P2"), |
| 25651 | + /* 139 */ "IfNeg" OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"), |
| 25652 | + /* 140 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]+=P3, goto P2"), |
| 25653 | + /* 141 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"), |
| 25654 | + /* 142 */ "JumpZeroIncr" OpHelp("if (r[P1]++)==0 ) goto P2"), |
| 25655 | + /* 143 */ "AggStep0" OpHelp("accum=r[P3] step(r[P2@P5])"), |
| 25656 | + /* 144 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"), |
| 25657 | + /* 145 */ "AggFinal" OpHelp("accum=r[P1] N=P2"), |
| 25658 | + /* 146 */ "IncrVacuum" OpHelp(""), |
| 25659 | + /* 147 */ "Expire" OpHelp(""), |
| 25660 | + /* 148 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"), |
| 25661 | + /* 149 */ "VBegin" OpHelp(""), |
| 25662 | + /* 150 */ "VCreate" OpHelp(""), |
| 25663 | + /* 151 */ "VDestroy" OpHelp(""), |
| 25664 | + /* 152 */ "VOpen" OpHelp(""), |
| 25665 | + /* 153 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"), |
| 25666 | + /* 154 */ "VNext" OpHelp(""), |
| 25667 | + /* 155 */ "VRename" OpHelp(""), |
| 25668 | + /* 156 */ "Pagecount" OpHelp(""), |
| 25669 | + /* 157 */ "MaxPgcnt" OpHelp(""), |
| 25670 | + /* 158 */ "Init" OpHelp("Start at P2"), |
| 25671 | + /* 159 */ "Noop" OpHelp(""), |
| 25672 | + /* 160 */ "Explain" OpHelp(""), |
| 25256 | 25673 | }; |
| 25257 | 25674 | return azName[i]; |
| 25258 | 25675 | } |
| 25259 | 25676 | #endif |
| 25260 | 25677 | |
| | @@ -38989,14 +39406,14 @@ |
| 38989 | 39406 | /* |
| 38990 | 39407 | ** Check to see if the i-th bit is set. Return true or false. |
| 38991 | 39408 | ** If p is NULL (if the bitmap has not been created) or if |
| 38992 | 39409 | ** i is out of range, then return false. |
| 38993 | 39410 | */ |
| 38994 | | -SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){ |
| 38995 | | - if( p==0 ) return 0; |
| 38996 | | - if( i>p->iSize || i==0 ) return 0; |
| 39411 | +SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){ |
| 39412 | + assert( p!=0 ); |
| 38997 | 39413 | i--; |
| 39414 | + if( i>=p->iSize ) return 0; |
| 38998 | 39415 | while( p->iDivisor ){ |
| 38999 | 39416 | u32 bin = i/p->iDivisor; |
| 39000 | 39417 | i = i%p->iDivisor; |
| 39001 | 39418 | p = p->u.apSub[bin]; |
| 39002 | 39419 | if (!p) { |
| | @@ -39011,10 +39428,13 @@ |
| 39011 | 39428 | if( p->u.aHash[h]==i ) return 1; |
| 39012 | 39429 | h = (h+1) % BITVEC_NINT; |
| 39013 | 39430 | } |
| 39014 | 39431 | return 0; |
| 39015 | 39432 | } |
| 39433 | +} |
| 39434 | +SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){ |
| 39435 | + return p!=0 && sqlite3BitvecTestNotNull(p,i); |
| 39016 | 39436 | } |
| 39017 | 39437 | |
| 39018 | 39438 | /* |
| 39019 | 39439 | ** Set the i-th bit. Return 0 on success and an error code if |
| 39020 | 39440 | ** anything goes wrong. |
| | @@ -39300,11 +39720,10 @@ |
| 39300 | 39720 | u8 bPurgeable; /* True if pages are on backing store */ |
| 39301 | 39721 | u8 eCreate; /* eCreate value for for xFetch() */ |
| 39302 | 39722 | int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */ |
| 39303 | 39723 | void *pStress; /* Argument to xStress */ |
| 39304 | 39724 | sqlite3_pcache *pCache; /* Pluggable cache module */ |
| 39305 | | - PgHdr *pPage1; /* Reference to page 1 */ |
| 39306 | 39725 | }; |
| 39307 | 39726 | |
| 39308 | 39727 | /********************************** Linked List Management ********************/ |
| 39309 | 39728 | |
| 39310 | 39729 | /* Allowed values for second argument to pcacheManageDirtyList() */ |
| | @@ -39378,13 +39797,10 @@ |
| 39378 | 39797 | ** Wrapper around the pluggable caches xUnpin method. If the cache is |
| 39379 | 39798 | ** being used for an in-memory database, this function is a no-op. |
| 39380 | 39799 | */ |
| 39381 | 39800 | static void pcacheUnpin(PgHdr *p){ |
| 39382 | 39801 | if( p->pCache->bPurgeable ){ |
| 39383 | | - if( p->pgno==1 ){ |
| 39384 | | - p->pCache->pPage1 = 0; |
| 39385 | | - } |
| 39386 | 39802 | sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0); |
| 39387 | 39803 | } |
| 39388 | 39804 | } |
| 39389 | 39805 | |
| 39390 | 39806 | /* |
| | @@ -39473,11 +39889,10 @@ |
| 39473 | 39889 | sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache)); |
| 39474 | 39890 | if( pCache->pCache ){ |
| 39475 | 39891 | sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); |
| 39476 | 39892 | } |
| 39477 | 39893 | pCache->pCache = pNew; |
| 39478 | | - pCache->pPage1 = 0; |
| 39479 | 39894 | pCache->szPage = szPage; |
| 39480 | 39895 | } |
| 39481 | 39896 | return SQLITE_OK; |
| 39482 | 39897 | } |
| 39483 | 39898 | |
| | @@ -39598,17 +40013,18 @@ |
| 39598 | 40013 | ){ |
| 39599 | 40014 | PgHdr *pPgHdr; |
| 39600 | 40015 | assert( pPage!=0 ); |
| 39601 | 40016 | pPgHdr = (PgHdr*)pPage->pExtra; |
| 39602 | 40017 | assert( pPgHdr->pPage==0 ); |
| 39603 | | - memset(pPgHdr, 0, sizeof(PgHdr)); |
| 40018 | + memset(pPgHdr, 0, sizeof(PgHdr)); |
| 39604 | 40019 | pPgHdr->pPage = pPage; |
| 39605 | 40020 | pPgHdr->pData = pPage->pBuf; |
| 39606 | 40021 | pPgHdr->pExtra = (void *)&pPgHdr[1]; |
| 39607 | 40022 | memset(pPgHdr->pExtra, 0, pCache->szExtra); |
| 39608 | 40023 | pPgHdr->pCache = pCache; |
| 39609 | 40024 | pPgHdr->pgno = pgno; |
| 40025 | + pPgHdr->flags = PGHDR_CLEAN; |
| 39610 | 40026 | return sqlite3PcacheFetchFinish(pCache,pgno,pPage); |
| 39611 | 40027 | } |
| 39612 | 40028 | |
| 39613 | 40029 | /* |
| 39614 | 40030 | ** This routine converts the sqlite3_pcache_page object returned by |
| | @@ -39621,23 +40037,20 @@ |
| 39621 | 40037 | Pgno pgno, /* Page number obtained */ |
| 39622 | 40038 | sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */ |
| 39623 | 40039 | ){ |
| 39624 | 40040 | PgHdr *pPgHdr; |
| 39625 | 40041 | |
| 39626 | | - if( pPage==0 ) return 0; |
| 40042 | + assert( pPage!=0 ); |
| 39627 | 40043 | pPgHdr = (PgHdr *)pPage->pExtra; |
| 39628 | 40044 | |
| 39629 | 40045 | if( !pPgHdr->pPage ){ |
| 39630 | 40046 | return pcacheFetchFinishWithInit(pCache, pgno, pPage); |
| 39631 | 40047 | } |
| 39632 | 40048 | if( 0==pPgHdr->nRef ){ |
| 39633 | 40049 | pCache->nRef++; |
| 39634 | 40050 | } |
| 39635 | 40051 | pPgHdr->nRef++; |
| 39636 | | - if( pgno==1 ){ |
| 39637 | | - pCache->pPage1 = pPgHdr; |
| 39638 | | - } |
| 39639 | 40052 | return pPgHdr; |
| 39640 | 40053 | } |
| 39641 | 40054 | |
| 39642 | 40055 | /* |
| 39643 | 40056 | ** Decrement the reference count on a page. If the page is clean and the |
| | @@ -39646,11 +40059,11 @@ |
| 39646 | 40059 | SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){ |
| 39647 | 40060 | assert( p->nRef>0 ); |
| 39648 | 40061 | p->nRef--; |
| 39649 | 40062 | if( p->nRef==0 ){ |
| 39650 | 40063 | p->pCache->nRef--; |
| 39651 | | - if( (p->flags&PGHDR_DIRTY)==0 ){ |
| 40064 | + if( p->flags&PGHDR_CLEAN ){ |
| 39652 | 40065 | pcacheUnpin(p); |
| 39653 | 40066 | }else if( p->pDirtyPrev!=0 ){ |
| 39654 | 40067 | /* Move the page to the head of the dirty list. */ |
| 39655 | 40068 | pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); |
| 39656 | 40069 | } |
| | @@ -39674,37 +40087,39 @@ |
| 39674 | 40087 | assert( p->nRef==1 ); |
| 39675 | 40088 | if( p->flags&PGHDR_DIRTY ){ |
| 39676 | 40089 | pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); |
| 39677 | 40090 | } |
| 39678 | 40091 | p->pCache->nRef--; |
| 39679 | | - if( p->pgno==1 ){ |
| 39680 | | - p->pCache->pPage1 = 0; |
| 39681 | | - } |
| 39682 | 40092 | sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1); |
| 39683 | 40093 | } |
| 39684 | 40094 | |
| 39685 | 40095 | /* |
| 39686 | 40096 | ** Make sure the page is marked as dirty. If it isn't dirty already, |
| 39687 | 40097 | ** make it so. |
| 39688 | 40098 | */ |
| 39689 | 40099 | SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){ |
| 39690 | | - p->flags &= ~PGHDR_DONT_WRITE; |
| 39691 | 40100 | assert( p->nRef>0 ); |
| 39692 | | - if( 0==(p->flags & PGHDR_DIRTY) ){ |
| 39693 | | - p->flags |= PGHDR_DIRTY; |
| 39694 | | - pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD); |
| 40101 | + if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ |
| 40102 | + p->flags &= ~PGHDR_DONT_WRITE; |
| 40103 | + if( p->flags & PGHDR_CLEAN ){ |
| 40104 | + p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN); |
| 40105 | + assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY ); |
| 40106 | + pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD); |
| 40107 | + } |
| 39695 | 40108 | } |
| 39696 | 40109 | } |
| 39697 | 40110 | |
| 39698 | 40111 | /* |
| 39699 | 40112 | ** Make sure the page is marked as clean. If it isn't clean already, |
| 39700 | 40113 | ** make it so. |
| 39701 | 40114 | */ |
| 39702 | 40115 | SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){ |
| 39703 | 40116 | if( (p->flags & PGHDR_DIRTY) ){ |
| 40117 | + assert( (p->flags & PGHDR_CLEAN)==0 ); |
| 39704 | 40118 | pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); |
| 39705 | | - p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC); |
| 40119 | + p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE); |
| 40120 | + p->flags |= PGHDR_CLEAN; |
| 39706 | 40121 | if( p->nRef==0 ){ |
| 39707 | 40122 | pcacheUnpin(p); |
| 39708 | 40123 | } |
| 39709 | 40124 | } |
| 39710 | 40125 | } |
| | @@ -39767,13 +40182,18 @@ |
| 39767 | 40182 | if( ALWAYS(p->pgno>pgno) ){ |
| 39768 | 40183 | assert( p->flags&PGHDR_DIRTY ); |
| 39769 | 40184 | sqlite3PcacheMakeClean(p); |
| 39770 | 40185 | } |
| 39771 | 40186 | } |
| 39772 | | - if( pgno==0 && pCache->pPage1 ){ |
| 39773 | | - memset(pCache->pPage1->pData, 0, pCache->szPage); |
| 39774 | | - pgno = 1; |
| 40187 | + if( pgno==0 && pCache->nRef ){ |
| 40188 | + sqlite3_pcache_page *pPage1; |
| 40189 | + pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0); |
| 40190 | + if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because |
| 40191 | + ** pCache->nRef>0 */ |
| 40192 | + memset(pPage1->pBuf, 0, pCache->szPage); |
| 40193 | + pgno = 1; |
| 40194 | + } |
| 39775 | 40195 | } |
| 39776 | 40196 | sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1); |
| 39777 | 40197 | } |
| 39778 | 40198 | } |
| 39779 | 40199 | |
| | @@ -40092,12 +40512,19 @@ |
| 40092 | 40512 | #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g)) |
| 40093 | 40513 | |
| 40094 | 40514 | /* |
| 40095 | 40515 | ** Macros to enter and leave the PCache LRU mutex. |
| 40096 | 40516 | */ |
| 40097 | | -#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex) |
| 40098 | | -#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex) |
| 40517 | +#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0 |
| 40518 | +# define pcache1EnterMutex(X) assert((X)->mutex==0) |
| 40519 | +# define pcache1LeaveMutex(X) assert((X)->mutex==0) |
| 40520 | +# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0 |
| 40521 | +#else |
| 40522 | +# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex) |
| 40523 | +# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex) |
| 40524 | +# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1 |
| 40525 | +#endif |
| 40099 | 40526 | |
| 40100 | 40527 | /******************************************************************************/ |
| 40101 | 40528 | /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/ |
| 40102 | 40529 | |
| 40103 | 40530 | /* |
| | @@ -40369,45 +40796,45 @@ |
| 40369 | 40796 | ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup |
| 40370 | 40797 | ** LRU list, then this function is a no-op. |
| 40371 | 40798 | ** |
| 40372 | 40799 | ** The PGroup mutex must be held when this function is called. |
| 40373 | 40800 | */ |
| 40374 | | -static void pcache1PinPage(PgHdr1 *pPage){ |
| 40801 | +static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){ |
| 40375 | 40802 | PCache1 *pCache; |
| 40376 | | - PGroup *pGroup; |
| 40377 | 40803 | |
| 40378 | 40804 | assert( pPage!=0 ); |
| 40379 | 40805 | assert( pPage->isPinned==0 ); |
| 40380 | 40806 | pCache = pPage->pCache; |
| 40381 | | - pGroup = pCache->pGroup; |
| 40382 | | - assert( pPage->pLruNext || pPage==pGroup->pLruTail ); |
| 40383 | | - assert( pPage->pLruPrev || pPage==pGroup->pLruHead ); |
| 40384 | | - assert( sqlite3_mutex_held(pGroup->mutex) ); |
| 40807 | + assert( pPage->pLruNext || pPage==pCache->pGroup->pLruTail ); |
| 40808 | + assert( pPage->pLruPrev || pPage==pCache->pGroup->pLruHead ); |
| 40809 | + assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
| 40385 | 40810 | if( pPage->pLruPrev ){ |
| 40386 | 40811 | pPage->pLruPrev->pLruNext = pPage->pLruNext; |
| 40387 | 40812 | }else{ |
| 40388 | | - pGroup->pLruHead = pPage->pLruNext; |
| 40813 | + pCache->pGroup->pLruHead = pPage->pLruNext; |
| 40389 | 40814 | } |
| 40390 | 40815 | if( pPage->pLruNext ){ |
| 40391 | 40816 | pPage->pLruNext->pLruPrev = pPage->pLruPrev; |
| 40392 | 40817 | }else{ |
| 40393 | | - pGroup->pLruTail = pPage->pLruPrev; |
| 40818 | + pCache->pGroup->pLruTail = pPage->pLruPrev; |
| 40394 | 40819 | } |
| 40395 | 40820 | pPage->pLruNext = 0; |
| 40396 | 40821 | pPage->pLruPrev = 0; |
| 40397 | 40822 | pPage->isPinned = 1; |
| 40398 | 40823 | pCache->nRecyclable--; |
| 40824 | + return pPage; |
| 40399 | 40825 | } |
| 40400 | 40826 | |
| 40401 | 40827 | |
| 40402 | 40828 | /* |
| 40403 | 40829 | ** Remove the page supplied as an argument from the hash table |
| 40404 | 40830 | ** (PCache1.apHash structure) that it is currently stored in. |
| 40831 | +** Also free the page if freePage is true. |
| 40405 | 40832 | ** |
| 40406 | 40833 | ** The PGroup mutex must be held when this function is called. |
| 40407 | 40834 | */ |
| 40408 | | -static void pcache1RemoveFromHash(PgHdr1 *pPage){ |
| 40835 | +static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){ |
| 40409 | 40836 | unsigned int h; |
| 40410 | 40837 | PCache1 *pCache = pPage->pCache; |
| 40411 | 40838 | PgHdr1 **pp; |
| 40412 | 40839 | |
| 40413 | 40840 | assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
| | @@ -40414,10 +40841,11 @@ |
| 40414 | 40841 | h = pPage->iKey % pCache->nHash; |
| 40415 | 40842 | for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext); |
| 40416 | 40843 | *pp = (*pp)->pNext; |
| 40417 | 40844 | |
| 40418 | 40845 | pCache->nPage--; |
| 40846 | + if( freeFlag ) pcache1FreePage(pPage); |
| 40419 | 40847 | } |
| 40420 | 40848 | |
| 40421 | 40849 | /* |
| 40422 | 40850 | ** If there are currently more than nMaxPage pages allocated, try |
| 40423 | 40851 | ** to recycle pages to reduce the number allocated to nMaxPage. |
| | @@ -40427,12 +40855,11 @@ |
| 40427 | 40855 | while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){ |
| 40428 | 40856 | PgHdr1 *p = pGroup->pLruTail; |
| 40429 | 40857 | assert( p->pCache->pGroup==pGroup ); |
| 40430 | 40858 | assert( p->isPinned==0 ); |
| 40431 | 40859 | pcache1PinPage(p); |
| 40432 | | - pcache1RemoveFromHash(p); |
| 40433 | | - pcache1FreePage(p); |
| 40860 | + pcache1RemoveFromHash(p, 1); |
| 40434 | 40861 | } |
| 40435 | 40862 | } |
| 40436 | 40863 | |
| 40437 | 40864 | /* |
| 40438 | 40865 | ** Discard all pages from cache pCache with a page number (key value) |
| | @@ -40474,14 +40901,16 @@ |
| 40474 | 40901 | */ |
| 40475 | 40902 | static int pcache1Init(void *NotUsed){ |
| 40476 | 40903 | UNUSED_PARAMETER(NotUsed); |
| 40477 | 40904 | assert( pcache1.isInit==0 ); |
| 40478 | 40905 | memset(&pcache1, 0, sizeof(pcache1)); |
| 40906 | +#if SQLITE_THREADSAFE |
| 40479 | 40907 | if( sqlite3GlobalConfig.bCoreMutex ){ |
| 40480 | 40908 | pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU); |
| 40481 | 40909 | pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM); |
| 40482 | 40910 | } |
| 40911 | +#endif |
| 40483 | 40912 | pcache1.grp.mxPinned = 10; |
| 40484 | 40913 | pcache1.isInit = 1; |
| 40485 | 40914 | return SQLITE_OK; |
| 40486 | 40915 | } |
| 40487 | 40916 | |
| | @@ -40650,11 +41079,11 @@ |
| 40650 | 41079 | || pcache1UnderMemoryPressure(pCache) |
| 40651 | 41080 | )){ |
| 40652 | 41081 | PCache1 *pOther; |
| 40653 | 41082 | pPage = pGroup->pLruTail; |
| 40654 | 41083 | assert( pPage->isPinned==0 ); |
| 40655 | | - pcache1RemoveFromHash(pPage); |
| 41084 | + pcache1RemoveFromHash(pPage, 0); |
| 40656 | 41085 | pcache1PinPage(pPage); |
| 40657 | 41086 | pOther = pPage->pCache; |
| 40658 | 41087 | |
| 40659 | 41088 | /* We want to verify that szPage and szExtra are the same for pOther |
| 40660 | 41089 | ** and pCache. Assert that we can verify this by comparing sums. */ |
| | @@ -40673,13 +41102,13 @@ |
| 40673 | 41102 | |
| 40674 | 41103 | /* Step 5. If a usable page buffer has still not been found, |
| 40675 | 41104 | ** attempt to allocate a new one. |
| 40676 | 41105 | */ |
| 40677 | 41106 | if( !pPage ){ |
| 40678 | | - if( createFlag==1 ) sqlite3BeginBenignMalloc(); |
| 41107 | + if( createFlag==1 ){ sqlite3BeginBenignMalloc(); } |
| 40679 | 41108 | pPage = pcache1AllocPage(pCache); |
| 40680 | | - if( createFlag==1 ) sqlite3EndBenignMalloc(); |
| 41109 | + if( createFlag==1 ){ sqlite3EndBenignMalloc(); } |
| 40681 | 41110 | } |
| 40682 | 41111 | |
| 40683 | 41112 | if( pPage ){ |
| 40684 | 41113 | unsigned int h = iKey % pCache->nHash; |
| 40685 | 41114 | pCache->nPage++; |
| | @@ -40749,41 +41178,81 @@ |
| 40749 | 41178 | ** then attempt to recycle a page from the LRU list. If it is the right |
| 40750 | 41179 | ** size, return the recycled buffer. Otherwise, free the buffer and |
| 40751 | 41180 | ** proceed to step 5. |
| 40752 | 41181 | ** |
| 40753 | 41182 | ** 5. Otherwise, allocate and return a new page buffer. |
| 41183 | +** |
| 41184 | +** There are two versions of this routine. pcache1FetchWithMutex() is |
| 41185 | +** the general case. pcache1FetchNoMutex() is a faster implementation for |
| 41186 | +** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper |
| 41187 | +** invokes the appropriate routine. |
| 40754 | 41188 | */ |
| 40755 | | -static sqlite3_pcache_page *pcache1Fetch( |
| 41189 | +static PgHdr1 *pcache1FetchNoMutex( |
| 40756 | 41190 | sqlite3_pcache *p, |
| 40757 | 41191 | unsigned int iKey, |
| 40758 | 41192 | int createFlag |
| 40759 | 41193 | ){ |
| 40760 | 41194 | PCache1 *pCache = (PCache1 *)p; |
| 40761 | 41195 | PgHdr1 *pPage = 0; |
| 40762 | 41196 | |
| 40763 | | - assert( offsetof(PgHdr1,page)==0 ); |
| 40764 | | - assert( pCache->bPurgeable || createFlag!=1 ); |
| 40765 | | - assert( pCache->bPurgeable || pCache->nMin==0 ); |
| 40766 | | - assert( pCache->bPurgeable==0 || pCache->nMin==10 ); |
| 40767 | | - assert( pCache->nMin==0 || pCache->bPurgeable ); |
| 40768 | | - assert( pCache->nHash>0 ); |
| 40769 | | - pcache1EnterMutex(pCache->pGroup); |
| 40770 | | - |
| 40771 | 41197 | /* Step 1: Search the hash table for an existing entry. */ |
| 40772 | 41198 | pPage = pCache->apHash[iKey % pCache->nHash]; |
| 40773 | 41199 | while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; } |
| 40774 | 41200 | |
| 40775 | 41201 | /* Step 2: Abort if no existing page is found and createFlag is 0 */ |
| 40776 | 41202 | if( pPage ){ |
| 40777 | | - if( !pPage->isPinned ) pcache1PinPage(pPage); |
| 41203 | + if( !pPage->isPinned ){ |
| 41204 | + return pcache1PinPage(pPage); |
| 41205 | + }else{ |
| 41206 | + return pPage; |
| 41207 | + } |
| 40778 | 41208 | }else if( createFlag ){ |
| 40779 | 41209 | /* Steps 3, 4, and 5 implemented by this subroutine */ |
| 40780 | | - pPage = pcache1FetchStage2(pCache, iKey, createFlag); |
| 41210 | + return pcache1FetchStage2(pCache, iKey, createFlag); |
| 41211 | + }else{ |
| 41212 | + return 0; |
| 40781 | 41213 | } |
| 41214 | +} |
| 41215 | +#if PCACHE1_MIGHT_USE_GROUP_MUTEX |
| 41216 | +static PgHdr1 *pcache1FetchWithMutex( |
| 41217 | + sqlite3_pcache *p, |
| 41218 | + unsigned int iKey, |
| 41219 | + int createFlag |
| 41220 | +){ |
| 41221 | + PCache1 *pCache = (PCache1 *)p; |
| 41222 | + PgHdr1 *pPage; |
| 41223 | + |
| 41224 | + pcache1EnterMutex(pCache->pGroup); |
| 41225 | + pPage = pcache1FetchNoMutex(p, iKey, createFlag); |
| 40782 | 41226 | assert( pPage==0 || pCache->iMaxKey>=iKey ); |
| 40783 | 41227 | pcache1LeaveMutex(pCache->pGroup); |
| 40784 | | - return (sqlite3_pcache_page*)pPage; |
| 41228 | + return pPage; |
| 41229 | +} |
| 41230 | +#endif |
| 41231 | +static sqlite3_pcache_page *pcache1Fetch( |
| 41232 | + sqlite3_pcache *p, |
| 41233 | + unsigned int iKey, |
| 41234 | + int createFlag |
| 41235 | +){ |
| 41236 | +#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG) |
| 41237 | + PCache1 *pCache = (PCache1 *)p; |
| 41238 | +#endif |
| 41239 | + |
| 41240 | + assert( offsetof(PgHdr1,page)==0 ); |
| 41241 | + assert( pCache->bPurgeable || createFlag!=1 ); |
| 41242 | + assert( pCache->bPurgeable || pCache->nMin==0 ); |
| 41243 | + assert( pCache->bPurgeable==0 || pCache->nMin==10 ); |
| 41244 | + assert( pCache->nMin==0 || pCache->bPurgeable ); |
| 41245 | + assert( pCache->nHash>0 ); |
| 41246 | +#if PCACHE1_MIGHT_USE_GROUP_MUTEX |
| 41247 | + if( pCache->pGroup->mutex ){ |
| 41248 | + return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag); |
| 41249 | + }else |
| 41250 | +#endif |
| 41251 | + { |
| 41252 | + return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag); |
| 41253 | + } |
| 40785 | 41254 | } |
| 40786 | 41255 | |
| 40787 | 41256 | |
| 40788 | 41257 | /* |
| 40789 | 41258 | ** Implementation of the sqlite3_pcache.xUnpin method. |
| | @@ -40808,12 +41277,11 @@ |
| 40808 | 41277 | assert( pPage->pLruPrev==0 && pPage->pLruNext==0 ); |
| 40809 | 41278 | assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage ); |
| 40810 | 41279 | assert( pPage->isPinned==1 ); |
| 40811 | 41280 | |
| 40812 | 41281 | if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){ |
| 40813 | | - pcache1RemoveFromHash(pPage); |
| 40814 | | - pcache1FreePage(pPage); |
| 41282 | + pcache1RemoveFromHash(pPage, 1); |
| 40815 | 41283 | }else{ |
| 40816 | 41284 | /* Add the page to the PGroup LRU list. */ |
| 40817 | 41285 | if( pGroup->pLruHead ){ |
| 40818 | 41286 | pGroup->pLruHead->pLruPrev = pPage; |
| 40819 | 41287 | pPage->pLruNext = pGroup->pLruHead; |
| | @@ -40963,12 +41431,11 @@ |
| 40963 | 41431 | #ifdef SQLITE_PCACHE_SEPARATE_HEADER |
| 40964 | 41432 | nFree += sqlite3MemSize(p); |
| 40965 | 41433 | #endif |
| 40966 | 41434 | assert( p->isPinned==0 ); |
| 40967 | 41435 | pcache1PinPage(p); |
| 40968 | | - pcache1RemoveFromHash(p); |
| 40969 | | - pcache1FreePage(p); |
| 41436 | + pcache1RemoveFromHash(p, 1); |
| 40970 | 41437 | } |
| 40971 | 41438 | pcache1LeaveMutex(&pcache1.grp); |
| 40972 | 41439 | } |
| 40973 | 41440 | return nFree; |
| 40974 | 41441 | } |
| | @@ -42105,13 +42572,13 @@ |
| 42105 | 42572 | }; |
| 42106 | 42573 | |
| 42107 | 42574 | /* |
| 42108 | 42575 | ** Bits of the Pager.doNotSpill flag. See further description below. |
| 42109 | 42576 | */ |
| 42110 | | -#define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */ |
| 42111 | | -#define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */ |
| 42112 | | -#define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */ |
| 42577 | +#define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */ |
| 42578 | +#define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */ |
| 42579 | +#define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */ |
| 42113 | 42580 | |
| 42114 | 42581 | /* |
| 42115 | 42582 | ** An open page cache is an instance of struct Pager. A description of |
| 42116 | 42583 | ** some of the more important member variables follows: |
| 42117 | 42584 | ** |
| | @@ -42189,15 +42656,15 @@ |
| 42189 | 42656 | ** comes up during savepoint rollback that requires the pcache module |
| 42190 | 42657 | ** to allocate a new page to prevent the journal file from being written |
| 42191 | 42658 | ** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF |
| 42192 | 42659 | ** case is a user preference. |
| 42193 | 42660 | ** |
| 42194 | | -** If the SPILLFLAG_NOSYNC bit is set, writing to the database from pagerStress() |
| 42195 | | -** is permitted, but syncing the journal file is not. This flag is set |
| 42196 | | -** by sqlite3PagerWrite() when the file-system sector-size is larger than |
| 42197 | | -** the database page-size in order to prevent a journal sync from happening |
| 42198 | | -** in between the journalling of two pages on the same sector. |
| 42661 | +** If the SPILLFLAG_NOSYNC bit is set, writing to the database from |
| 42662 | +** pagerStress() is permitted, but syncing the journal file is not. |
| 42663 | +** This flag is set by sqlite3PagerWrite() when the file-system sector-size |
| 42664 | +** is larger than the database page-size in order to prevent a journal sync |
| 42665 | +** from happening in between the journalling of two pages on the same sector. |
| 42199 | 42666 | ** |
| 42200 | 42667 | ** subjInMemory |
| 42201 | 42668 | ** |
| 42202 | 42669 | ** This is a boolean variable. If true, then any required sub-journal |
| 42203 | 42670 | ** is opened as an in-memory journal file. If false, then in-memory |
| | @@ -42296,11 +42763,11 @@ |
| 42296 | 42763 | u8 changeCountDone; /* Set after incrementing the change-counter */ |
| 42297 | 42764 | u8 setMaster; /* True if a m-j name has been written to jrnl */ |
| 42298 | 42765 | u8 doNotSpill; /* Do not spill the cache when non-zero */ |
| 42299 | 42766 | u8 subjInMemory; /* True to use in-memory sub-journals */ |
| 42300 | 42767 | u8 bUseFetch; /* True to use xFetch() */ |
| 42301 | | - u8 hasBeenUsed; /* True if any content previously read from this pager*/ |
| 42768 | + u8 hasBeenUsed; /* True if any content previously read */ |
| 42302 | 42769 | Pgno dbSize; /* Number of pages in the database */ |
| 42303 | 42770 | Pgno dbOrigSize; /* dbSize before the current transaction */ |
| 42304 | 42771 | Pgno dbFileSize; /* Number of pages in the database file */ |
| 42305 | 42772 | Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */ |
| 42306 | 42773 | int errCode; /* One of several kinds of errors */ |
| | @@ -42457,11 +42924,11 @@ |
| 42457 | 42924 | ** |
| 42458 | 42925 | ** instead of |
| 42459 | 42926 | ** |
| 42460 | 42927 | ** if( pPager->jfd->pMethods ){ ... |
| 42461 | 42928 | */ |
| 42462 | | -#define isOpen(pFd) ((pFd)->pMethods) |
| 42929 | +#define isOpen(pFd) ((pFd)->pMethods!=0) |
| 42463 | 42930 | |
| 42464 | 42931 | /* |
| 42465 | 42932 | ** Return true if this pager uses a write-ahead log instead of the usual |
| 42466 | 42933 | ** rollback journal. Otherwise false. |
| 42467 | 42934 | */ |
| | @@ -42680,23 +43147,25 @@ |
| 42680 | 43147 | PagerSavepoint *p; |
| 42681 | 43148 | Pgno pgno = pPg->pgno; |
| 42682 | 43149 | int i; |
| 42683 | 43150 | for(i=0; i<pPager->nSavepoint; i++){ |
| 42684 | 43151 | p = &pPager->aSavepoint[i]; |
| 42685 | | - if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){ |
| 43152 | + if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){ |
| 42686 | 43153 | return 1; |
| 42687 | 43154 | } |
| 42688 | 43155 | } |
| 42689 | 43156 | return 0; |
| 42690 | 43157 | } |
| 42691 | 43158 | |
| 43159 | +#ifdef SQLITE_DEBUG |
| 42692 | 43160 | /* |
| 42693 | 43161 | ** Return true if the page is already in the journal file. |
| 42694 | 43162 | */ |
| 42695 | 43163 | static int pageInJournal(Pager *pPager, PgHdr *pPg){ |
| 42696 | 43164 | return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno); |
| 42697 | 43165 | } |
| 43166 | +#endif |
| 42698 | 43167 | |
| 42699 | 43168 | /* |
| 42700 | 43169 | ** Read a 32-bit integer from the given file descriptor. Store the integer |
| 42701 | 43170 | ** that is read in *pRes. Return SQLITE_OK if everything worked, or an |
| 42702 | 43171 | ** error code is something goes wrong. |
| | @@ -43304,11 +43773,12 @@ |
| 43304 | 43773 | */ |
| 43305 | 43774 | if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager)))) |
| 43306 | 43775 | || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4))) |
| 43307 | 43776 | || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster))) |
| 43308 | 43777 | || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum))) |
| 43309 | | - || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8))) |
| 43778 | + || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, |
| 43779 | + iHdrOff+4+nMaster+8))) |
| 43310 | 43780 | ){ |
| 43311 | 43781 | return rc; |
| 43312 | 43782 | } |
| 43313 | 43783 | pPager->journalOff += (nMaster+20); |
| 43314 | 43784 | |
| | @@ -43864,11 +44334,11 @@ |
| 43864 | 44334 | if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){ |
| 43865 | 44335 | return SQLITE_DONE; |
| 43866 | 44336 | } |
| 43867 | 44337 | } |
| 43868 | 44338 | |
| 43869 | | - /* If this page has already been played by before during the current |
| 44339 | + /* If this page has already been played back before during the current |
| 43870 | 44340 | ** rollback, then don't bother to play it back again. |
| 43871 | 44341 | */ |
| 43872 | 44342 | if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){ |
| 43873 | 44343 | return rc; |
| 43874 | 44344 | } |
| | @@ -45966,12 +46436,10 @@ |
| 45966 | 46436 | return rc; |
| 45967 | 46437 | } |
| 45968 | 46438 | |
| 45969 | 46439 | /* |
| 45970 | 46440 | ** Append a record of the current state of page pPg to the sub-journal. |
| 45971 | | -** It is the callers responsibility to use subjRequiresPage() to check |
| 45972 | | -** that it is really required before calling this function. |
| 45973 | 46441 | ** |
| 45974 | 46442 | ** If successful, set the bit corresponding to pPg->pgno in the bitvecs |
| 45975 | 46443 | ** for all open savepoints before returning. |
| 45976 | 46444 | ** |
| 45977 | 46445 | ** This function returns SQLITE_OK if everything is successful, an IO |
| | @@ -46013,10 +46481,17 @@ |
| 46013 | 46481 | pPager->nSubRec++; |
| 46014 | 46482 | assert( pPager->nSavepoint>0 ); |
| 46015 | 46483 | rc = addToSavepointBitvecs(pPager, pPg->pgno); |
| 46016 | 46484 | } |
| 46017 | 46485 | return rc; |
| 46486 | +} |
| 46487 | +static int subjournalPageIfRequired(PgHdr *pPg){ |
| 46488 | + if( subjRequiresPage(pPg) ){ |
| 46489 | + return subjournalPage(pPg); |
| 46490 | + }else{ |
| 46491 | + return SQLITE_OK; |
| 46492 | + } |
| 46018 | 46493 | } |
| 46019 | 46494 | |
| 46020 | 46495 | /* |
| 46021 | 46496 | ** This function is called by the pcache layer when it has reached some |
| 46022 | 46497 | ** soft memory limit. The first argument is a pointer to a Pager object |
| | @@ -46071,13 +46546,11 @@ |
| 46071 | 46546 | } |
| 46072 | 46547 | |
| 46073 | 46548 | pPg->pDirty = 0; |
| 46074 | 46549 | if( pagerUseWal(pPager) ){ |
| 46075 | 46550 | /* Write a single frame for this page to the log. */ |
| 46076 | | - if( subjRequiresPage(pPg) ){ |
| 46077 | | - rc = subjournalPage(pPg); |
| 46078 | | - } |
| 46551 | + rc = subjournalPageIfRequired(pPg); |
| 46079 | 46552 | if( rc==SQLITE_OK ){ |
| 46080 | 46553 | rc = pagerWalFrames(pPager, pPg, 0, 0); |
| 46081 | 46554 | } |
| 46082 | 46555 | }else{ |
| 46083 | 46556 | |
| | @@ -46086,43 +46559,10 @@ |
| 46086 | 46559 | || pPager->eState==PAGER_WRITER_CACHEMOD |
| 46087 | 46560 | ){ |
| 46088 | 46561 | rc = syncJournal(pPager, 1); |
| 46089 | 46562 | } |
| 46090 | 46563 | |
| 46091 | | - /* If the page number of this page is larger than the current size of |
| 46092 | | - ** the database image, it may need to be written to the sub-journal. |
| 46093 | | - ** This is because the call to pager_write_pagelist() below will not |
| 46094 | | - ** actually write data to the file in this case. |
| 46095 | | - ** |
| 46096 | | - ** Consider the following sequence of events: |
| 46097 | | - ** |
| 46098 | | - ** BEGIN; |
| 46099 | | - ** <journal page X> |
| 46100 | | - ** <modify page X> |
| 46101 | | - ** SAVEPOINT sp; |
| 46102 | | - ** <shrink database file to Y pages> |
| 46103 | | - ** pagerStress(page X) |
| 46104 | | - ** ROLLBACK TO sp; |
| 46105 | | - ** |
| 46106 | | - ** If (X>Y), then when pagerStress is called page X will not be written |
| 46107 | | - ** out to the database file, but will be dropped from the cache. Then, |
| 46108 | | - ** following the "ROLLBACK TO sp" statement, reading page X will read |
| 46109 | | - ** data from the database file. This will be the copy of page X as it |
| 46110 | | - ** was when the transaction started, not as it was when "SAVEPOINT sp" |
| 46111 | | - ** was executed. |
| 46112 | | - ** |
| 46113 | | - ** The solution is to write the current data for page X into the |
| 46114 | | - ** sub-journal file now (if it is not already there), so that it will |
| 46115 | | - ** be restored to its current value when the "ROLLBACK TO sp" is |
| 46116 | | - ** executed. |
| 46117 | | - */ |
| 46118 | | - if( NEVER( |
| 46119 | | - rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg) |
| 46120 | | - ) ){ |
| 46121 | | - rc = subjournalPage(pPg); |
| 46122 | | - } |
| 46123 | | - |
| 46124 | 46564 | /* Write the contents of the page out to the database file. */ |
| 46125 | 46565 | if( rc==SQLITE_OK ){ |
| 46126 | 46566 | assert( (pPg->flags&PGHDR_NEED_SYNC)==0 ); |
| 46127 | 46567 | rc = pager_write_pagelist(pPager, pPg); |
| 46128 | 46568 | } |
| | @@ -46374,11 +46814,11 @@ |
| 46374 | 46814 | ** This branch also runs for files marked as immutable. |
| 46375 | 46815 | */ |
| 46376 | 46816 | act_like_temp_file: |
| 46377 | 46817 | tempFile = 1; |
| 46378 | 46818 | pPager->eState = PAGER_READER; /* Pretend we already have a lock */ |
| 46379 | | - pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE locking mode */ |
| 46819 | + pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */ |
| 46380 | 46820 | pPager->noLock = 1; /* Do no locking */ |
| 46381 | 46821 | readOnly = (vfsFlags&SQLITE_OPEN_READONLY); |
| 46382 | 46822 | } |
| 46383 | 46823 | |
| 46384 | 46824 | /* The following call to PagerSetPagesize() serves to set the value of |
| | @@ -46393,11 +46833,11 @@ |
| 46393 | 46833 | /* Initialize the PCache object. */ |
| 46394 | 46834 | if( rc==SQLITE_OK ){ |
| 46395 | 46835 | assert( nExtra<1000 ); |
| 46396 | 46836 | nExtra = ROUND8(nExtra); |
| 46397 | 46837 | rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb, |
| 46398 | | - !memDb?pagerStress:0, (void *)pPager, pPager->pPCache); |
| 46838 | + !memDb?pagerStress:0, (void *)pPager, pPager->pPCache); |
| 46399 | 46839 | } |
| 46400 | 46840 | |
| 46401 | 46841 | /* If an error occurred above, free the Pager structure and close the file. |
| 46402 | 46842 | */ |
| 46403 | 46843 | if( rc!=SQLITE_OK ){ |
| | @@ -46780,11 +47220,11 @@ |
| 46780 | 47220 | ** see if the database has been modified. If the database has changed, |
| 46781 | 47221 | ** flush the cache. The pPager->hasBeenUsed flag prevents this from |
| 46782 | 47222 | ** occurring on the very first access to a file, in order to save a |
| 46783 | 47223 | ** single unnecessary sqlite3OsRead() call at the start-up. |
| 46784 | 47224 | ** |
| 46785 | | - ** Database changes is detected by looking at 15 bytes beginning |
| 47225 | + ** Database changes are detected by looking at 15 bytes beginning |
| 46786 | 47226 | ** at offset 24 into the file. The first 4 of these 16 bytes are |
| 46787 | 47227 | ** a 32-bit counter that is incremented with each change. The |
| 46788 | 47228 | ** other bytes change randomly with each file change when |
| 46789 | 47229 | ** a codec is in use. |
| 46790 | 47230 | ** |
| | @@ -46988,13 +47428,18 @@ |
| 46988 | 47428 | sqlite3_pcache_page *pBase; |
| 46989 | 47429 | pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3); |
| 46990 | 47430 | if( pBase==0 ){ |
| 46991 | 47431 | rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase); |
| 46992 | 47432 | if( rc!=SQLITE_OK ) goto pager_acquire_err; |
| 47433 | + if( pBase==0 ){ |
| 47434 | + pPg = *ppPage = 0; |
| 47435 | + rc = SQLITE_NOMEM; |
| 47436 | + goto pager_acquire_err; |
| 47437 | + } |
| 46993 | 47438 | } |
| 46994 | 47439 | pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase); |
| 46995 | | - if( pPg==0 ) rc = SQLITE_NOMEM; |
| 47440 | + assert( pPg!=0 ); |
| 46996 | 47441 | } |
| 46997 | 47442 | } |
| 46998 | 47443 | |
| 46999 | 47444 | if( rc!=SQLITE_OK ){ |
| 47000 | 47445 | /* Either the call to sqlite3PcacheFetch() returned an error or the |
| | @@ -47001,14 +47446,15 @@ |
| 47001 | 47446 | ** pager was already in the error-state when this function was called. |
| 47002 | 47447 | ** Set pPg to 0 and jump to the exception handler. */ |
| 47003 | 47448 | pPg = 0; |
| 47004 | 47449 | goto pager_acquire_err; |
| 47005 | 47450 | } |
| 47006 | | - assert( (*ppPage)->pgno==pgno ); |
| 47007 | | - assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 ); |
| 47451 | + assert( pPg==(*ppPage) ); |
| 47452 | + assert( pPg->pgno==pgno ); |
| 47453 | + assert( pPg->pPager==pPager || pPg->pPager==0 ); |
| 47008 | 47454 | |
| 47009 | | - if( (*ppPage)->pPager && !noContent ){ |
| 47455 | + if( pPg->pPager && !noContent ){ |
| 47010 | 47456 | /* In this case the pcache already contains an initialized copy of |
| 47011 | 47457 | ** the page. Return without further ado. */ |
| 47012 | 47458 | assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) ); |
| 47013 | 47459 | pPager->aStat[PAGER_STAT_HIT]++; |
| 47014 | 47460 | return SQLITE_OK; |
| | @@ -47015,11 +47461,10 @@ |
| 47015 | 47461 | |
| 47016 | 47462 | }else{ |
| 47017 | 47463 | /* The pager cache has created a new page. Its content needs to |
| 47018 | 47464 | ** be initialized. */ |
| 47019 | 47465 | |
| 47020 | | - pPg = *ppPage; |
| 47021 | 47466 | pPg->pPager = pPager; |
| 47022 | 47467 | |
| 47023 | 47468 | /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page |
| 47024 | 47469 | ** number greater than this, or the unused locking-page, is requested. */ |
| 47025 | 47470 | if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){ |
| | @@ -47094,10 +47539,11 @@ |
| 47094 | 47539 | assert( pPager!=0 ); |
| 47095 | 47540 | assert( pgno!=0 ); |
| 47096 | 47541 | assert( pPager->pPCache!=0 ); |
| 47097 | 47542 | pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0); |
| 47098 | 47543 | assert( pPage==0 || pPager->hasBeenUsed ); |
| 47544 | + if( pPage==0 ) return 0; |
| 47099 | 47545 | return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage); |
| 47100 | 47546 | } |
| 47101 | 47547 | |
| 47102 | 47548 | /* |
| 47103 | 47549 | ** Release a page reference. |
| | @@ -47296,10 +47742,63 @@ |
| 47296 | 47742 | } |
| 47297 | 47743 | |
| 47298 | 47744 | PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager))); |
| 47299 | 47745 | return rc; |
| 47300 | 47746 | } |
| 47747 | + |
| 47748 | +/* |
| 47749 | +** Write page pPg onto the end of the rollback journal. |
| 47750 | +*/ |
| 47751 | +static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){ |
| 47752 | + Pager *pPager = pPg->pPager; |
| 47753 | + int rc; |
| 47754 | + u32 cksum; |
| 47755 | + char *pData2; |
| 47756 | + i64 iOff = pPager->journalOff; |
| 47757 | + |
| 47758 | + /* We should never write to the journal file the page that |
| 47759 | + ** contains the database locks. The following assert verifies |
| 47760 | + ** that we do not. */ |
| 47761 | + assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); |
| 47762 | + |
| 47763 | + assert( pPager->journalHdr<=pPager->journalOff ); |
| 47764 | + CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2); |
| 47765 | + cksum = pager_cksum(pPager, (u8*)pData2); |
| 47766 | + |
| 47767 | + /* Even if an IO or diskfull error occurs while journalling the |
| 47768 | + ** page in the block above, set the need-sync flag for the page. |
| 47769 | + ** Otherwise, when the transaction is rolled back, the logic in |
| 47770 | + ** playback_one_page() will think that the page needs to be restored |
| 47771 | + ** in the database file. And if an IO error occurs while doing so, |
| 47772 | + ** then corruption may follow. |
| 47773 | + */ |
| 47774 | + pPg->flags |= PGHDR_NEED_SYNC; |
| 47775 | + |
| 47776 | + rc = write32bits(pPager->jfd, iOff, pPg->pgno); |
| 47777 | + if( rc!=SQLITE_OK ) return rc; |
| 47778 | + rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4); |
| 47779 | + if( rc!=SQLITE_OK ) return rc; |
| 47780 | + rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum); |
| 47781 | + if( rc!=SQLITE_OK ) return rc; |
| 47782 | + |
| 47783 | + IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, |
| 47784 | + pPager->journalOff, pPager->pageSize)); |
| 47785 | + PAGER_INCR(sqlite3_pager_writej_count); |
| 47786 | + PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n", |
| 47787 | + PAGERID(pPager), pPg->pgno, |
| 47788 | + ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg))); |
| 47789 | + |
| 47790 | + pPager->journalOff += 8 + pPager->pageSize; |
| 47791 | + pPager->nRec++; |
| 47792 | + assert( pPager->pInJournal!=0 ); |
| 47793 | + rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); |
| 47794 | + testcase( rc==SQLITE_NOMEM ); |
| 47795 | + assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| 47796 | + rc |= addToSavepointBitvecs(pPager, pPg->pgno); |
| 47797 | + assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| 47798 | + return rc; |
| 47799 | +} |
| 47301 | 47800 | |
| 47302 | 47801 | /* |
| 47303 | 47802 | ** Mark a single data page as writeable. The page is written into the |
| 47304 | 47803 | ** main journal or sub-journal as required. If the page is written into |
| 47305 | 47804 | ** one of the journals, the corresponding bit is set in the |
| | @@ -47307,11 +47806,10 @@ |
| 47307 | 47806 | ** of any open savepoints as appropriate. |
| 47308 | 47807 | */ |
| 47309 | 47808 | static int pager_write(PgHdr *pPg){ |
| 47310 | 47809 | Pager *pPager = pPg->pPager; |
| 47311 | 47810 | int rc = SQLITE_OK; |
| 47312 | | - int inJournal; |
| 47313 | 47811 | |
| 47314 | 47812 | /* This routine is not called unless a write-transaction has already |
| 47315 | 47813 | ** been started. The journal file may or may not be open at this point. |
| 47316 | 47814 | ** It is never called in the ERROR state. |
| 47317 | 47815 | */ |
| | @@ -47320,11 +47818,10 @@ |
| 47320 | 47818 | || pPager->eState==PAGER_WRITER_DBMOD |
| 47321 | 47819 | ); |
| 47322 | 47820 | assert( assert_pager_state(pPager) ); |
| 47323 | 47821 | assert( pPager->errCode==0 ); |
| 47324 | 47822 | assert( pPager->readOnly==0 ); |
| 47325 | | - |
| 47326 | 47823 | CHECK_PAGE(pPg); |
| 47327 | 47824 | |
| 47328 | 47825 | /* The journal file needs to be opened. Higher level routines have already |
| 47329 | 47826 | ** obtained the necessary locks to begin the write-transaction, but the |
| 47330 | 47827 | ** rollback journal might not yet be open. Open it now if this is the case. |
| | @@ -47339,95 +47836,52 @@ |
| 47339 | 47836 | if( rc!=SQLITE_OK ) return rc; |
| 47340 | 47837 | } |
| 47341 | 47838 | assert( pPager->eState>=PAGER_WRITER_CACHEMOD ); |
| 47342 | 47839 | assert( assert_pager_state(pPager) ); |
| 47343 | 47840 | |
| 47344 | | - /* Mark the page as dirty. If the page has already been written |
| 47345 | | - ** to the journal then we can return right away. |
| 47346 | | - */ |
| 47347 | | - sqlite3PcacheMakeDirty(pPg); |
| 47348 | | - inJournal = pageInJournal(pPager, pPg); |
| 47349 | | - if( inJournal && (pPager->nSavepoint==0 || !subjRequiresPage(pPg)) ){ |
| 47350 | | - assert( !pagerUseWal(pPager) ); |
| 47351 | | - }else{ |
| 47352 | | - |
| 47353 | | - /* The transaction journal now exists and we have a RESERVED or an |
| 47354 | | - ** EXCLUSIVE lock on the main database file. Write the current page to |
| 47355 | | - ** the transaction journal if it is not there already. |
| 47356 | | - */ |
| 47357 | | - if( !inJournal && !pagerUseWal(pPager) ){ |
| 47358 | | - assert( pagerUseWal(pPager)==0 ); |
| 47359 | | - if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){ |
| 47360 | | - u32 cksum; |
| 47361 | | - char *pData2; |
| 47362 | | - i64 iOff = pPager->journalOff; |
| 47363 | | - |
| 47364 | | - /* We should never write to the journal file the page that |
| 47365 | | - ** contains the database locks. The following assert verifies |
| 47366 | | - ** that we do not. */ |
| 47367 | | - assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); |
| 47368 | | - |
| 47369 | | - assert( pPager->journalHdr<=pPager->journalOff ); |
| 47370 | | - CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2); |
| 47371 | | - cksum = pager_cksum(pPager, (u8*)pData2); |
| 47372 | | - |
| 47373 | | - /* Even if an IO or diskfull error occurs while journalling the |
| 47374 | | - ** page in the block above, set the need-sync flag for the page. |
| 47375 | | - ** Otherwise, when the transaction is rolled back, the logic in |
| 47376 | | - ** playback_one_page() will think that the page needs to be restored |
| 47377 | | - ** in the database file. And if an IO error occurs while doing so, |
| 47378 | | - ** then corruption may follow. |
| 47379 | | - */ |
| 47380 | | - pPg->flags |= PGHDR_NEED_SYNC; |
| 47381 | | - |
| 47382 | | - rc = write32bits(pPager->jfd, iOff, pPg->pgno); |
| 47383 | | - if( rc!=SQLITE_OK ) return rc; |
| 47384 | | - rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4); |
| 47385 | | - if( rc!=SQLITE_OK ) return rc; |
| 47386 | | - rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum); |
| 47387 | | - if( rc!=SQLITE_OK ) return rc; |
| 47388 | | - |
| 47389 | | - IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, |
| 47390 | | - pPager->journalOff, pPager->pageSize)); |
| 47391 | | - PAGER_INCR(sqlite3_pager_writej_count); |
| 47392 | | - PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n", |
| 47393 | | - PAGERID(pPager), pPg->pgno, |
| 47394 | | - ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg))); |
| 47395 | | - |
| 47396 | | - pPager->journalOff += 8 + pPager->pageSize; |
| 47397 | | - pPager->nRec++; |
| 47398 | | - assert( pPager->pInJournal!=0 ); |
| 47399 | | - rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); |
| 47400 | | - testcase( rc==SQLITE_NOMEM ); |
| 47401 | | - assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| 47402 | | - rc |= addToSavepointBitvecs(pPager, pPg->pgno); |
| 47403 | | - if( rc!=SQLITE_OK ){ |
| 47404 | | - assert( rc==SQLITE_NOMEM ); |
| 47405 | | - return rc; |
| 47406 | | - } |
| 47407 | | - }else{ |
| 47408 | | - if( pPager->eState!=PAGER_WRITER_DBMOD ){ |
| 47409 | | - pPg->flags |= PGHDR_NEED_SYNC; |
| 47410 | | - } |
| 47411 | | - PAGERTRACE(("APPEND %d page %d needSync=%d\n", |
| 47412 | | - PAGERID(pPager), pPg->pgno, |
| 47413 | | - ((pPg->flags&PGHDR_NEED_SYNC)?1:0))); |
| 47414 | | - } |
| 47415 | | - } |
| 47416 | | - |
| 47417 | | - /* If the statement journal is open and the page is not in it, |
| 47418 | | - ** then write the current page to the statement journal. Note that |
| 47419 | | - ** the statement journal format differs from the standard journal format |
| 47420 | | - ** in that it omits the checksums and the header. |
| 47421 | | - */ |
| 47422 | | - if( pPager->nSavepoint>0 && subjRequiresPage(pPg) ){ |
| 47423 | | - rc = subjournalPage(pPg); |
| 47424 | | - } |
| 47425 | | - } |
| 47426 | | - |
| 47427 | | - /* Update the database size and return. |
| 47428 | | - */ |
| 47841 | + /* Mark the page that is about to be modified as dirty. */ |
| 47842 | + sqlite3PcacheMakeDirty(pPg); |
| 47843 | + |
| 47844 | + /* If a rollback journal is in use, them make sure the page that is about |
| 47845 | + ** to change is in the rollback journal, or if the page is a new page off |
| 47846 | + ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC. |
| 47847 | + */ |
| 47848 | + assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) ); |
| 47849 | + if( pPager->pInJournal!=0 |
| 47850 | + && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0 |
| 47851 | + ){ |
| 47852 | + assert( pagerUseWal(pPager)==0 ); |
| 47853 | + if( pPg->pgno<=pPager->dbOrigSize ){ |
| 47854 | + rc = pagerAddPageToRollbackJournal(pPg); |
| 47855 | + if( rc!=SQLITE_OK ){ |
| 47856 | + return rc; |
| 47857 | + } |
| 47858 | + }else{ |
| 47859 | + if( pPager->eState!=PAGER_WRITER_DBMOD ){ |
| 47860 | + pPg->flags |= PGHDR_NEED_SYNC; |
| 47861 | + } |
| 47862 | + PAGERTRACE(("APPEND %d page %d needSync=%d\n", |
| 47863 | + PAGERID(pPager), pPg->pgno, |
| 47864 | + ((pPg->flags&PGHDR_NEED_SYNC)?1:0))); |
| 47865 | + } |
| 47866 | + } |
| 47867 | + |
| 47868 | + /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list |
| 47869 | + ** and before writing the page into the rollback journal. Wait until now, |
| 47870 | + ** after the page has been successfully journalled, before setting the |
| 47871 | + ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified. |
| 47872 | + */ |
| 47873 | + pPg->flags |= PGHDR_WRITEABLE; |
| 47874 | + |
| 47875 | + /* If the statement journal is open and the page is not in it, |
| 47876 | + ** then write the page into the statement journal. |
| 47877 | + */ |
| 47878 | + if( pPager->nSavepoint>0 ){ |
| 47879 | + rc = subjournalPageIfRequired(pPg); |
| 47880 | + } |
| 47881 | + |
| 47882 | + /* Update the database size and return. */ |
| 47429 | 47883 | if( pPager->dbSize<pPg->pgno ){ |
| 47430 | 47884 | pPager->dbSize = pPg->pgno; |
| 47431 | 47885 | } |
| 47432 | 47886 | return rc; |
| 47433 | 47887 | } |
| | @@ -47438,21 +47892,21 @@ |
| 47438 | 47892 | ** all bytes of a sector are written together by hardware. Hence, all bytes of |
| 47439 | 47893 | ** a sector need to be journalled in case of a power loss in the middle of |
| 47440 | 47894 | ** a write. |
| 47441 | 47895 | ** |
| 47442 | 47896 | ** Usually, the sector size is less than or equal to the page size, in which |
| 47443 | | -** case pages can be individually written. This routine only runs in the exceptional |
| 47444 | | -** case where the page size is smaller than the sector size. |
| 47897 | +** case pages can be individually written. This routine only runs in the |
| 47898 | +** exceptional case where the page size is smaller than the sector size. |
| 47445 | 47899 | */ |
| 47446 | 47900 | static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){ |
| 47447 | | - int rc = SQLITE_OK; /* Return code */ |
| 47448 | | - Pgno nPageCount; /* Total number of pages in database file */ |
| 47449 | | - Pgno pg1; /* First page of the sector pPg is located on. */ |
| 47450 | | - int nPage = 0; /* Number of pages starting at pg1 to journal */ |
| 47451 | | - int ii; /* Loop counter */ |
| 47452 | | - int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */ |
| 47453 | | - Pager *pPager = pPg->pPager; /* The pager that owns pPg */ |
| 47901 | + int rc = SQLITE_OK; /* Return code */ |
| 47902 | + Pgno nPageCount; /* Total number of pages in database file */ |
| 47903 | + Pgno pg1; /* First page of the sector pPg is located on. */ |
| 47904 | + int nPage = 0; /* Number of pages starting at pg1 to journal */ |
| 47905 | + int ii; /* Loop counter */ |
| 47906 | + int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */ |
| 47907 | + Pager *pPager = pPg->pPager; /* The pager that owns pPg */ |
| 47454 | 47908 | Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize); |
| 47455 | 47909 | |
| 47456 | 47910 | /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow |
| 47457 | 47911 | ** a journal header to be written between the pages journaled by |
| 47458 | 47912 | ** this function. |
| | @@ -47536,15 +47990,19 @@ |
| 47536 | 47990 | ** |
| 47537 | 47991 | ** If an error occurs, SQLITE_NOMEM or an IO error code is returned |
| 47538 | 47992 | ** as appropriate. Otherwise, SQLITE_OK. |
| 47539 | 47993 | */ |
| 47540 | 47994 | SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){ |
| 47995 | + Pager *pPager = pPg->pPager; |
| 47541 | 47996 | assert( (pPg->flags & PGHDR_MMAP)==0 ); |
| 47542 | | - assert( pPg->pPager->eState>=PAGER_WRITER_LOCKED ); |
| 47543 | | - assert( pPg->pPager->eState!=PAGER_ERROR ); |
| 47544 | | - assert( assert_pager_state(pPg->pPager) ); |
| 47545 | | - if( pPg->pPager->sectorSize > (u32)pPg->pPager->pageSize ){ |
| 47997 | + assert( pPager->eState>=PAGER_WRITER_LOCKED ); |
| 47998 | + assert( pPager->eState!=PAGER_ERROR ); |
| 47999 | + assert( assert_pager_state(pPager) ); |
| 48000 | + if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){ |
| 48001 | + if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg); |
| 48002 | + return SQLITE_OK; |
| 48003 | + }else if( pPager->sectorSize > (u32)pPager->pageSize ){ |
| 47546 | 48004 | return pagerWriteLargeSector(pPg); |
| 47547 | 48005 | }else{ |
| 47548 | 48006 | return pager_write(pPg); |
| 47549 | 48007 | } |
| 47550 | 48008 | } |
| | @@ -47554,11 +48012,11 @@ |
| 47554 | 48012 | ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok |
| 47555 | 48013 | ** to change the content of the page. |
| 47556 | 48014 | */ |
| 47557 | 48015 | #ifndef NDEBUG |
| 47558 | 48016 | SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){ |
| 47559 | | - return pPg->flags&PGHDR_DIRTY; |
| 48017 | + return pPg->flags & PGHDR_WRITEABLE; |
| 47560 | 48018 | } |
| 47561 | 48019 | #endif |
| 47562 | 48020 | |
| 47563 | 48021 | /* |
| 47564 | 48022 | ** A call to this routine tells the pager that it is not necessary to |
| | @@ -47578,10 +48036,11 @@ |
| 47578 | 48036 | Pager *pPager = pPg->pPager; |
| 47579 | 48037 | if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){ |
| 47580 | 48038 | PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager))); |
| 47581 | 48039 | IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno)) |
| 47582 | 48040 | pPg->flags |= PGHDR_DONT_WRITE; |
| 48041 | + pPg->flags &= ~PGHDR_WRITEABLE; |
| 47583 | 48042 | pager_set_pagehash(pPg); |
| 47584 | 48043 | } |
| 47585 | 48044 | } |
| 47586 | 48045 | |
| 47587 | 48046 | /* |
| | @@ -48132,58 +48591,66 @@ |
| 48132 | 48591 | ** |
| 48133 | 48592 | ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error |
| 48134 | 48593 | ** occurs while opening the sub-journal file, then an IO error code is |
| 48135 | 48594 | ** returned. Otherwise, SQLITE_OK. |
| 48136 | 48595 | */ |
| 48137 | | -SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){ |
| 48596 | +static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){ |
| 48138 | 48597 | int rc = SQLITE_OK; /* Return code */ |
| 48139 | 48598 | int nCurrent = pPager->nSavepoint; /* Current number of savepoints */ |
| 48599 | + int ii; /* Iterator variable */ |
| 48600 | + PagerSavepoint *aNew; /* New Pager.aSavepoint array */ |
| 48140 | 48601 | |
| 48141 | 48602 | assert( pPager->eState>=PAGER_WRITER_LOCKED ); |
| 48603 | + assert( assert_pager_state(pPager) ); |
| 48604 | + assert( nSavepoint>nCurrent && pPager->useJournal ); |
| 48605 | + |
| 48606 | + /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM |
| 48607 | + ** if the allocation fails. Otherwise, zero the new portion in case a |
| 48608 | + ** malloc failure occurs while populating it in the for(...) loop below. |
| 48609 | + */ |
| 48610 | + aNew = (PagerSavepoint *)sqlite3Realloc( |
| 48611 | + pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint |
| 48612 | + ); |
| 48613 | + if( !aNew ){ |
| 48614 | + return SQLITE_NOMEM; |
| 48615 | + } |
| 48616 | + memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint)); |
| 48617 | + pPager->aSavepoint = aNew; |
| 48618 | + |
| 48619 | + /* Populate the PagerSavepoint structures just allocated. */ |
| 48620 | + for(ii=nCurrent; ii<nSavepoint; ii++){ |
| 48621 | + aNew[ii].nOrig = pPager->dbSize; |
| 48622 | + if( isOpen(pPager->jfd) && pPager->journalOff>0 ){ |
| 48623 | + aNew[ii].iOffset = pPager->journalOff; |
| 48624 | + }else{ |
| 48625 | + aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager); |
| 48626 | + } |
| 48627 | + aNew[ii].iSubRec = pPager->nSubRec; |
| 48628 | + aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize); |
| 48629 | + if( !aNew[ii].pInSavepoint ){ |
| 48630 | + return SQLITE_NOMEM; |
| 48631 | + } |
| 48632 | + if( pagerUseWal(pPager) ){ |
| 48633 | + sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData); |
| 48634 | + } |
| 48635 | + pPager->nSavepoint = ii+1; |
| 48636 | + } |
| 48637 | + assert( pPager->nSavepoint==nSavepoint ); |
| 48638 | + assertTruncateConstraint(pPager); |
| 48639 | + return rc; |
| 48640 | +} |
| 48641 | +SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){ |
| 48642 | + assert( pPager->eState>=PAGER_WRITER_LOCKED ); |
| 48142 | 48643 | assert( assert_pager_state(pPager) ); |
| 48143 | 48644 | |
| 48144 | | - if( nSavepoint>nCurrent && pPager->useJournal ){ |
| 48145 | | - int ii; /* Iterator variable */ |
| 48146 | | - PagerSavepoint *aNew; /* New Pager.aSavepoint array */ |
| 48147 | | - |
| 48148 | | - /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM |
| 48149 | | - ** if the allocation fails. Otherwise, zero the new portion in case a |
| 48150 | | - ** malloc failure occurs while populating it in the for(...) loop below. |
| 48151 | | - */ |
| 48152 | | - aNew = (PagerSavepoint *)sqlite3Realloc( |
| 48153 | | - pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint |
| 48154 | | - ); |
| 48155 | | - if( !aNew ){ |
| 48156 | | - return SQLITE_NOMEM; |
| 48157 | | - } |
| 48158 | | - memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint)); |
| 48159 | | - pPager->aSavepoint = aNew; |
| 48160 | | - |
| 48161 | | - /* Populate the PagerSavepoint structures just allocated. */ |
| 48162 | | - for(ii=nCurrent; ii<nSavepoint; ii++){ |
| 48163 | | - aNew[ii].nOrig = pPager->dbSize; |
| 48164 | | - if( isOpen(pPager->jfd) && pPager->journalOff>0 ){ |
| 48165 | | - aNew[ii].iOffset = pPager->journalOff; |
| 48166 | | - }else{ |
| 48167 | | - aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager); |
| 48168 | | - } |
| 48169 | | - aNew[ii].iSubRec = pPager->nSubRec; |
| 48170 | | - aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize); |
| 48171 | | - if( !aNew[ii].pInSavepoint ){ |
| 48172 | | - return SQLITE_NOMEM; |
| 48173 | | - } |
| 48174 | | - if( pagerUseWal(pPager) ){ |
| 48175 | | - sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData); |
| 48176 | | - } |
| 48177 | | - pPager->nSavepoint = ii+1; |
| 48178 | | - } |
| 48179 | | - assert( pPager->nSavepoint==nSavepoint ); |
| 48180 | | - assertTruncateConstraint(pPager); |
| 48181 | | - } |
| 48182 | | - |
| 48183 | | - return rc; |
| 48184 | | -} |
| 48645 | + if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){ |
| 48646 | + return pagerOpenSavepoint(pPager, nSavepoint); |
| 48647 | + }else{ |
| 48648 | + return SQLITE_OK; |
| 48649 | + } |
| 48650 | +} |
| 48651 | + |
| 48185 | 48652 | |
| 48186 | 48653 | /* |
| 48187 | 48654 | ** This function is called to rollback or release (commit) a savepoint. |
| 48188 | 48655 | ** The savepoint to release or rollback need not be the most recently |
| 48189 | 48656 | ** created savepoint. |
| | @@ -48410,13 +48877,12 @@ |
| 48410 | 48877 | ** |
| 48411 | 48878 | ** subjournalPage() may need to allocate space to store pPg->pgno into |
| 48412 | 48879 | ** one or more savepoint bitvecs. This is the reason this function |
| 48413 | 48880 | ** may return SQLITE_NOMEM. |
| 48414 | 48881 | */ |
| 48415 | | - if( pPg->flags&PGHDR_DIRTY |
| 48416 | | - && subjRequiresPage(pPg) |
| 48417 | | - && SQLITE_OK!=(rc = subjournalPage(pPg)) |
| 48882 | + if( (pPg->flags & PGHDR_DIRTY)!=0 |
| 48883 | + && SQLITE_OK!=(rc = subjournalPageIfRequired(pPg)) |
| 48418 | 48884 | ){ |
| 48419 | 48885 | return rc; |
| 48420 | 48886 | } |
| 48421 | 48887 | |
| 48422 | 48888 | PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n", |
| | @@ -52330,10 +52796,11 @@ |
| 52330 | 52796 | #define MX_CELL(pBt) ((pBt->pageSize-8)/6) |
| 52331 | 52797 | |
| 52332 | 52798 | /* Forward declarations */ |
| 52333 | 52799 | typedef struct MemPage MemPage; |
| 52334 | 52800 | typedef struct BtLock BtLock; |
| 52801 | +typedef struct CellInfo CellInfo; |
| 52335 | 52802 | |
| 52336 | 52803 | /* |
| 52337 | 52804 | ** This is a magic string that appears at the beginning of every |
| 52338 | 52805 | ** SQLite database in order to identify the file as a real database. |
| 52339 | 52806 | ** |
| | @@ -52393,11 +52860,14 @@ |
| 52393 | 52860 | u8 *apOvfl[5]; /* Pointers to the body of overflow cells */ |
| 52394 | 52861 | BtShared *pBt; /* Pointer to BtShared that this page is part of */ |
| 52395 | 52862 | u8 *aData; /* Pointer to disk image of the page data */ |
| 52396 | 52863 | u8 *aDataEnd; /* One byte past the end of usable data */ |
| 52397 | 52864 | u8 *aCellIdx; /* The cell index area */ |
| 52865 | + u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */ |
| 52398 | 52866 | DbPage *pDbPage; /* Pager page handle */ |
| 52867 | + u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */ |
| 52868 | + void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */ |
| 52399 | 52869 | Pgno pgno; /* Page number for this page */ |
| 52400 | 52870 | }; |
| 52401 | 52871 | |
| 52402 | 52872 | /* |
| 52403 | 52873 | ** The in-memory image of a disk page has the auxiliary information appended |
| | @@ -52449,10 +52919,11 @@ |
| 52449 | 52919 | sqlite3 *db; /* The database connection holding this btree */ |
| 52450 | 52920 | BtShared *pBt; /* Sharable content of this btree */ |
| 52451 | 52921 | u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */ |
| 52452 | 52922 | u8 sharable; /* True if we can share pBt with another db */ |
| 52453 | 52923 | u8 locked; /* True if db currently has pBt locked */ |
| 52924 | + u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */ |
| 52454 | 52925 | int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */ |
| 52455 | 52926 | int nBackup; /* Number of backup operations reading this btree */ |
| 52456 | 52927 | u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */ |
| 52457 | 52928 | Btree *pNext; /* List of other sharable Btrees from the same db */ |
| 52458 | 52929 | Btree *pPrev; /* Back pointer of the same list */ |
| | @@ -52559,11 +53030,10 @@ |
| 52559 | 53030 | /* |
| 52560 | 53031 | ** An instance of the following structure is used to hold information |
| 52561 | 53032 | ** about a cell. The parseCellPtr() function fills in this structure |
| 52562 | 53033 | ** based on information extract from the raw disk page. |
| 52563 | 53034 | */ |
| 52564 | | -typedef struct CellInfo CellInfo; |
| 52565 | 53035 | struct CellInfo { |
| 52566 | 53036 | i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */ |
| 52567 | 53037 | u8 *pPayload; /* Pointer to the start of payload */ |
| 52568 | 53038 | u32 nPayload; /* Bytes of payload */ |
| 52569 | 53039 | u16 nLocal; /* Amount of payload held locally, not on overflow */ |
| | @@ -52602,24 +53072,30 @@ |
| 52602 | 53072 | ** eState==FAULT: Cursor fault with skipNext as error code. |
| 52603 | 53073 | */ |
| 52604 | 53074 | struct BtCursor { |
| 52605 | 53075 | Btree *pBtree; /* The Btree to which this cursor belongs */ |
| 52606 | 53076 | BtShared *pBt; /* The BtShared this cursor points to */ |
| 52607 | | - BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ |
| 52608 | | - struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */ |
| 53077 | + BtCursor *pNext; /* Forms a linked list of all cursors */ |
| 52609 | 53078 | Pgno *aOverflow; /* Cache of overflow page locations */ |
| 52610 | 53079 | CellInfo info; /* A parse of the cell we are pointing at */ |
| 52611 | 53080 | i64 nKey; /* Size of pKey, or last integer key */ |
| 52612 | 53081 | void *pKey; /* Saved key that was cursor last known position */ |
| 52613 | 53082 | Pgno pgnoRoot; /* The root page of this tree */ |
| 52614 | 53083 | int nOvflAlloc; /* Allocated size of aOverflow[] array */ |
| 52615 | 53084 | int skipNext; /* Prev() is noop if negative. Next() is noop if positive. |
| 52616 | 53085 | ** Error code if eState==CURSOR_FAULT */ |
| 52617 | 53086 | u8 curFlags; /* zero or more BTCF_* flags defined below */ |
| 53087 | + u8 curPagerFlags; /* Flags to send to sqlite3PagerAcquire() */ |
| 52618 | 53088 | u8 eState; /* One of the CURSOR_XXX constants (see below) */ |
| 52619 | | - u8 hints; /* As configured by CursorSetHints() */ |
| 52620 | | - i16 iPage; /* Index of current page in apPage */ |
| 53089 | + u8 hints; /* As configured by CursorSetHints() */ |
| 53090 | + /* All fields above are zeroed when the cursor is allocated. See |
| 53091 | + ** sqlite3BtreeCursorZero(). Fields that follow must be manually |
| 53092 | + ** initialized. */ |
| 53093 | + i8 iPage; /* Index of current page in apPage */ |
| 53094 | + u8 curIntKey; /* Value of apPage[0]->intKey */ |
| 53095 | + struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */ |
| 53096 | + void *padding1; /* Make object size a multiple of 16 */ |
| 52621 | 53097 | u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */ |
| 52622 | 53098 | MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */ |
| 52623 | 53099 | }; |
| 52624 | 53100 | |
| 52625 | 53101 | /* |
| | @@ -52628,10 +53104,11 @@ |
| 52628 | 53104 | #define BTCF_WriteFlag 0x01 /* True if a write cursor */ |
| 52629 | 53105 | #define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */ |
| 52630 | 53106 | #define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */ |
| 52631 | 53107 | #define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */ |
| 52632 | 53108 | #define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */ |
| 53109 | +#define BTCF_Multiple 0x20 /* Maybe another cursor on the same btree */ |
| 52633 | 53110 | |
| 52634 | 53111 | /* |
| 52635 | 53112 | ** Potential values for BtCursor.eState. |
| 52636 | 53113 | ** |
| 52637 | 53114 | ** CURSOR_INVALID: |
| | @@ -52780,10 +53257,23 @@ |
| 52780 | 53257 | #define get2byte(x) ((x)[0]<<8 | (x)[1]) |
| 52781 | 53258 | #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v)) |
| 52782 | 53259 | #define get4byte sqlite3Get4byte |
| 52783 | 53260 | #define put4byte sqlite3Put4byte |
| 52784 | 53261 | |
| 53262 | +/* |
| 53263 | +** get2byteAligned(), unlike get2byte(), requires that its argument point to a |
| 53264 | +** two-byte aligned address. get2bytea() is only used for accessing the |
| 53265 | +** cell addresses in a btree header. |
| 53266 | +*/ |
| 53267 | +#if SQLITE_BYTEORDER==4321 |
| 53268 | +# define get2byteAligned(x) (*(u16*)(x)) |
| 53269 | +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000 |
| 53270 | +# define get2byteAligned(x) __builtin_bswap16(*(u16*)(x)) |
| 53271 | +#else |
| 53272 | +# define get2byteAligned(x) ((x)[0]<<8 | (x)[1]) |
| 53273 | +#endif |
| 53274 | + |
| 52785 | 53275 | /************** End of btreeInt.h ********************************************/ |
| 52786 | 53276 | /************** Continuing where we left off in btmutex.c ********************/ |
| 52787 | 53277 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 52788 | 53278 | #if SQLITE_THREADSAFE |
| 52789 | 53279 | |
| | @@ -53559,17 +54049,19 @@ |
| 53559 | 54049 | Btree *pBtree, /* The database file to check */ |
| 53560 | 54050 | i64 iRow, /* The rowid that might be changing */ |
| 53561 | 54051 | int isClearTable /* True if all rows are being deleted */ |
| 53562 | 54052 | ){ |
| 53563 | 54053 | BtCursor *p; |
| 53564 | | - BtShared *pBt = pBtree->pBt; |
| 54054 | + if( pBtree->hasIncrblobCur==0 ) return; |
| 53565 | 54055 | assert( sqlite3BtreeHoldsMutex(pBtree) ); |
| 53566 | | - for(p=pBt->pCursor; p; p=p->pNext){ |
| 53567 | | - if( (p->curFlags & BTCF_Incrblob)!=0 |
| 53568 | | - && (isClearTable || p->info.nKey==iRow) |
| 53569 | | - ){ |
| 53570 | | - p->eState = CURSOR_INVALID; |
| 54056 | + pBtree->hasIncrblobCur = 0; |
| 54057 | + for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
| 54058 | + if( (p->curFlags & BTCF_Incrblob)!=0 ){ |
| 54059 | + pBtree->hasIncrblobCur = 1; |
| 54060 | + if( isClearTable || p->info.nKey==iRow ){ |
| 54061 | + p->eState = CURSOR_INVALID; |
| 54062 | + } |
| 53571 | 54063 | } |
| 53572 | 54064 | } |
| 53573 | 54065 | } |
| 53574 | 54066 | |
| 53575 | 54067 | #else |
| | @@ -53687,11 +54179,11 @@ |
| 53687 | 54179 | ** stores the integer key in pCur->nKey. In this case this value is |
| 53688 | 54180 | ** all that is required. Otherwise, if pCur is not open on an intKey |
| 53689 | 54181 | ** table, then malloc space for and store the pCur->nKey bytes of key |
| 53690 | 54182 | ** data. |
| 53691 | 54183 | */ |
| 53692 | | - if( 0==pCur->apPage[0]->intKey ){ |
| 54184 | + if( 0==pCur->curIntKey ){ |
| 53693 | 54185 | void *pKey = sqlite3Malloc( pCur->nKey ); |
| 53694 | 54186 | if( pKey ){ |
| 53695 | 54187 | rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey); |
| 53696 | 54188 | if( rc==SQLITE_OK ){ |
| 53697 | 54189 | pCur->pKey = pKey; |
| | @@ -53700,11 +54192,11 @@ |
| 53700 | 54192 | } |
| 53701 | 54193 | }else{ |
| 53702 | 54194 | rc = SQLITE_NOMEM; |
| 53703 | 54195 | } |
| 53704 | 54196 | } |
| 53705 | | - assert( !pCur->apPage[0]->intKey || !pCur->pKey ); |
| 54197 | + assert( !pCur->curIntKey || !pCur->pKey ); |
| 53706 | 54198 | |
| 53707 | 54199 | if( rc==SQLITE_OK ){ |
| 53708 | 54200 | btreeReleaseAllCursorPages(pCur); |
| 53709 | 54201 | pCur->eState = CURSOR_REQUIRESEEK; |
| 53710 | 54202 | } |
| | @@ -53721,10 +54213,19 @@ |
| 53721 | 54213 | ** the table with root-page iRoot. "Saving the cursor position" means that |
| 53722 | 54214 | ** the location in the btree is remembered in such a way that it can be |
| 53723 | 54215 | ** moved back to the same spot after the btree has been modified. This |
| 53724 | 54216 | ** routine is called just before cursor pExcept is used to modify the |
| 53725 | 54217 | ** table, for example in BtreeDelete() or BtreeInsert(). |
| 54218 | +** |
| 54219 | +** If there are two or more cursors on the same btree, then all such |
| 54220 | +** cursors should have their BTCF_Multiple flag set. The btreeCursor() |
| 54221 | +** routine enforces that rule. This routine only needs to be called in |
| 54222 | +** the uncommon case when pExpect has the BTCF_Multiple flag set. |
| 54223 | +** |
| 54224 | +** If pExpect!=NULL and if no other cursors are found on the same root-page, |
| 54225 | +** then the BTCF_Multiple flag on pExpect is cleared, to avoid another |
| 54226 | +** pointless call to this routine. |
| 53726 | 54227 | ** |
| 53727 | 54228 | ** Implementation note: This routine merely checks to see if any cursors |
| 53728 | 54229 | ** need to be saved. It calls out to saveCursorsOnList() in the (unusual) |
| 53729 | 54230 | ** event that cursors are in need to being saved. |
| 53730 | 54231 | */ |
| | @@ -53733,11 +54234,13 @@ |
| 53733 | 54234 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 53734 | 54235 | assert( pExcept==0 || pExcept->pBt==pBt ); |
| 53735 | 54236 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 53736 | 54237 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break; |
| 53737 | 54238 | } |
| 53738 | | - return p ? saveCursorsOnList(p, iRoot, pExcept) : SQLITE_OK; |
| 54239 | + if( p ) return saveCursorsOnList(p, iRoot, pExcept); |
| 54240 | + if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple; |
| 54241 | + return SQLITE_OK; |
| 53739 | 54242 | } |
| 53740 | 54243 | |
| 53741 | 54244 | /* This helper routine to saveAllCursors does the actual work of saving |
| 53742 | 54245 | ** the cursors if and when a cursor is found that actually requires saving. |
| 53743 | 54246 | ** The common case is that no cursors need to be saved, so this routine is |
| | @@ -54020,71 +54523,184 @@ |
| 54020 | 54523 | |
| 54021 | 54524 | /* |
| 54022 | 54525 | ** Given a btree page and a cell index (0 means the first cell on |
| 54023 | 54526 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 54024 | 54527 | ** to the cell content. |
| 54528 | +** |
| 54529 | +** findCellPastPtr() does the same except it skips past the initial |
| 54530 | +** 4-byte child pointer found on interior pages, if there is one. |
| 54025 | 54531 | ** |
| 54026 | 54532 | ** This routine works only for pages that do not contain overflow cells. |
| 54027 | 54533 | */ |
| 54028 | 54534 | #define findCell(P,I) \ |
| 54029 | | - ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)]))) |
| 54030 | | -#define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I))))) |
| 54535 | + ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)]))) |
| 54536 | +#define findCellPastPtr(P,I) \ |
| 54537 | + ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)]))) |
| 54031 | 54538 | |
| 54032 | 54539 | |
| 54033 | 54540 | /* |
| 54034 | | -** This a more complex version of findCell() that works for |
| 54035 | | -** pages that do contain overflow cells. |
| 54541 | +** This is common tail processing for btreeParseCellPtr() and |
| 54542 | +** btreeParseCellPtrIndex() for the case when the cell does not fit entirely |
| 54543 | +** on a single B-tree page. Make necessary adjustments to the CellInfo |
| 54544 | +** structure. |
| 54036 | 54545 | */ |
| 54037 | | -static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 54038 | | - int i; |
| 54546 | +static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow( |
| 54547 | + MemPage *pPage, /* Page containing the cell */ |
| 54548 | + u8 *pCell, /* Pointer to the cell text. */ |
| 54549 | + CellInfo *pInfo /* Fill in this structure */ |
| 54550 | +){ |
| 54551 | + /* If the payload will not fit completely on the local page, we have |
| 54552 | + ** to decide how much to store locally and how much to spill onto |
| 54553 | + ** overflow pages. The strategy is to minimize the amount of unused |
| 54554 | + ** space on overflow pages while keeping the amount of local storage |
| 54555 | + ** in between minLocal and maxLocal. |
| 54556 | + ** |
| 54557 | + ** Warning: changing the way overflow payload is distributed in any |
| 54558 | + ** way will result in an incompatible file format. |
| 54559 | + */ |
| 54560 | + int minLocal; /* Minimum amount of payload held locally */ |
| 54561 | + int maxLocal; /* Maximum amount of payload held locally */ |
| 54562 | + int surplus; /* Overflow payload available for local storage */ |
| 54563 | + |
| 54564 | + minLocal = pPage->minLocal; |
| 54565 | + maxLocal = pPage->maxLocal; |
| 54566 | + surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4); |
| 54567 | + testcase( surplus==maxLocal ); |
| 54568 | + testcase( surplus==maxLocal+1 ); |
| 54569 | + if( surplus <= maxLocal ){ |
| 54570 | + pInfo->nLocal = (u16)surplus; |
| 54571 | + }else{ |
| 54572 | + pInfo->nLocal = (u16)minLocal; |
| 54573 | + } |
| 54574 | + pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell); |
| 54575 | + pInfo->nSize = pInfo->iOverflow + 4; |
| 54576 | +} |
| 54577 | + |
| 54578 | +/* |
| 54579 | +** The following routines are implementations of the MemPage.xParseCell() |
| 54580 | +** method. |
| 54581 | +** |
| 54582 | +** Parse a cell content block and fill in the CellInfo structure. |
| 54583 | +** |
| 54584 | +** btreeParseCellPtr() => table btree leaf nodes |
| 54585 | +** btreeParseCellNoPayload() => table btree internal nodes |
| 54586 | +** btreeParseCellPtrIndex() => index btree nodes |
| 54587 | +** |
| 54588 | +** There is also a wrapper function btreeParseCell() that works for |
| 54589 | +** all MemPage types and that references the cell by index rather than |
| 54590 | +** by pointer. |
| 54591 | +*/ |
| 54592 | +static void btreeParseCellPtrNoPayload( |
| 54593 | + MemPage *pPage, /* Page containing the cell */ |
| 54594 | + u8 *pCell, /* Pointer to the cell text. */ |
| 54595 | + CellInfo *pInfo /* Fill in this structure */ |
| 54596 | +){ |
| 54039 | 54597 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 54040 | | - for(i=pPage->nOverflow-1; i>=0; i--){ |
| 54041 | | - int k; |
| 54042 | | - k = pPage->aiOvfl[i]; |
| 54043 | | - if( k<=iCell ){ |
| 54044 | | - if( k==iCell ){ |
| 54045 | | - return pPage->apOvfl[i]; |
| 54046 | | - } |
| 54047 | | - iCell--; |
| 54048 | | - } |
| 54049 | | - } |
| 54050 | | - return findCell(pPage, iCell); |
| 54051 | | -} |
| 54052 | | - |
| 54053 | | -/* |
| 54054 | | -** Parse a cell content block and fill in the CellInfo structure. There |
| 54055 | | -** are two versions of this function. btreeParseCell() takes a |
| 54056 | | -** cell index as the second argument and btreeParseCellPtr() |
| 54057 | | -** takes a pointer to the body of the cell as its second argument. |
| 54058 | | -*/ |
| 54598 | + assert( pPage->leaf==0 ); |
| 54599 | + assert( pPage->noPayload ); |
| 54600 | + assert( pPage->childPtrSize==4 ); |
| 54601 | + pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey); |
| 54602 | + pInfo->nPayload = 0; |
| 54603 | + pInfo->nLocal = 0; |
| 54604 | + pInfo->iOverflow = 0; |
| 54605 | + pInfo->pPayload = 0; |
| 54606 | + return; |
| 54607 | +} |
| 54059 | 54608 | static void btreeParseCellPtr( |
| 54609 | + MemPage *pPage, /* Page containing the cell */ |
| 54610 | + u8 *pCell, /* Pointer to the cell text. */ |
| 54611 | + CellInfo *pInfo /* Fill in this structure */ |
| 54612 | +){ |
| 54613 | + u8 *pIter; /* For scanning through pCell */ |
| 54614 | + u32 nPayload; /* Number of bytes of cell payload */ |
| 54615 | + u64 iKey; /* Extracted Key value */ |
| 54616 | + |
| 54617 | + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 54618 | + assert( pPage->leaf==0 || pPage->leaf==1 ); |
| 54619 | + assert( pPage->intKeyLeaf || pPage->noPayload ); |
| 54620 | + assert( pPage->noPayload==0 ); |
| 54621 | + assert( pPage->intKeyLeaf ); |
| 54622 | + assert( pPage->childPtrSize==0 ); |
| 54623 | + pIter = pCell; |
| 54624 | + |
| 54625 | + /* The next block of code is equivalent to: |
| 54626 | + ** |
| 54627 | + ** pIter += getVarint32(pIter, nPayload); |
| 54628 | + ** |
| 54629 | + ** The code is inlined to avoid a function call. |
| 54630 | + */ |
| 54631 | + nPayload = *pIter; |
| 54632 | + if( nPayload>=0x80 ){ |
| 54633 | + u8 *pEnd = &pIter[8]; |
| 54634 | + nPayload &= 0x7f; |
| 54635 | + do{ |
| 54636 | + nPayload = (nPayload<<7) | (*++pIter & 0x7f); |
| 54637 | + }while( (*pIter)>=0x80 && pIter<pEnd ); |
| 54638 | + } |
| 54639 | + pIter++; |
| 54640 | + |
| 54641 | + /* The next block of code is equivalent to: |
| 54642 | + ** |
| 54643 | + ** pIter += getVarint(pIter, (u64*)&pInfo->nKey); |
| 54644 | + ** |
| 54645 | + ** The code is inlined to avoid a function call. |
| 54646 | + */ |
| 54647 | + iKey = *pIter; |
| 54648 | + if( iKey>=0x80 ){ |
| 54649 | + u8 *pEnd = &pIter[7]; |
| 54650 | + iKey &= 0x7f; |
| 54651 | + while(1){ |
| 54652 | + iKey = (iKey<<7) | (*++pIter & 0x7f); |
| 54653 | + if( (*pIter)<0x80 ) break; |
| 54654 | + if( pIter>=pEnd ){ |
| 54655 | + iKey = (iKey<<8) | *++pIter; |
| 54656 | + break; |
| 54657 | + } |
| 54658 | + } |
| 54659 | + } |
| 54660 | + pIter++; |
| 54661 | + |
| 54662 | + pInfo->nKey = *(i64*)&iKey; |
| 54663 | + pInfo->nPayload = nPayload; |
| 54664 | + pInfo->pPayload = pIter; |
| 54665 | + testcase( nPayload==pPage->maxLocal ); |
| 54666 | + testcase( nPayload==pPage->maxLocal+1 ); |
| 54667 | + if( nPayload<=pPage->maxLocal ){ |
| 54668 | + /* This is the (easy) common case where the entire payload fits |
| 54669 | + ** on the local page. No overflow is required. |
| 54670 | + */ |
| 54671 | + pInfo->nSize = nPayload + (u16)(pIter - pCell); |
| 54672 | + if( pInfo->nSize<4 ) pInfo->nSize = 4; |
| 54673 | + pInfo->nLocal = (u16)nPayload; |
| 54674 | + pInfo->iOverflow = 0; |
| 54675 | + }else{ |
| 54676 | + btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo); |
| 54677 | + } |
| 54678 | +} |
| 54679 | +static void btreeParseCellPtrIndex( |
| 54060 | 54680 | MemPage *pPage, /* Page containing the cell */ |
| 54061 | 54681 | u8 *pCell, /* Pointer to the cell text. */ |
| 54062 | 54682 | CellInfo *pInfo /* Fill in this structure */ |
| 54063 | 54683 | ){ |
| 54064 | 54684 | u8 *pIter; /* For scanning through pCell */ |
| 54065 | 54685 | u32 nPayload; /* Number of bytes of cell payload */ |
| 54066 | 54686 | |
| 54067 | 54687 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 54068 | 54688 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
| 54069 | | - if( pPage->intKeyLeaf ){ |
| 54070 | | - assert( pPage->childPtrSize==0 ); |
| 54071 | | - pIter = pCell + getVarint32(pCell, nPayload); |
| 54072 | | - pIter += getVarint(pIter, (u64*)&pInfo->nKey); |
| 54073 | | - }else if( pPage->noPayload ){ |
| 54074 | | - assert( pPage->childPtrSize==4 ); |
| 54075 | | - pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey); |
| 54076 | | - pInfo->nPayload = 0; |
| 54077 | | - pInfo->nLocal = 0; |
| 54078 | | - pInfo->iOverflow = 0; |
| 54079 | | - pInfo->pPayload = 0; |
| 54080 | | - return; |
| 54081 | | - }else{ |
| 54082 | | - pIter = pCell + pPage->childPtrSize; |
| 54083 | | - pIter += getVarint32(pIter, nPayload); |
| 54084 | | - pInfo->nKey = nPayload; |
| 54085 | | - } |
| 54689 | + assert( pPage->intKeyLeaf==0 ); |
| 54690 | + assert( pPage->noPayload==0 ); |
| 54691 | + pIter = pCell + pPage->childPtrSize; |
| 54692 | + nPayload = *pIter; |
| 54693 | + if( nPayload>=0x80 ){ |
| 54694 | + u8 *pEnd = &pIter[8]; |
| 54695 | + nPayload &= 0x7f; |
| 54696 | + do{ |
| 54697 | + nPayload = (nPayload<<7) | (*++pIter & 0x7f); |
| 54698 | + }while( *(pIter)>=0x80 && pIter<pEnd ); |
| 54699 | + } |
| 54700 | + pIter++; |
| 54701 | + pInfo->nKey = nPayload; |
| 54086 | 54702 | pInfo->nPayload = nPayload; |
| 54087 | 54703 | pInfo->pPayload = pIter; |
| 54088 | 54704 | testcase( nPayload==pPage->maxLocal ); |
| 54089 | 54705 | testcase( nPayload==pPage->maxLocal+1 ); |
| 54090 | 54706 | if( nPayload<=pPage->maxLocal ){ |
| | @@ -54094,50 +54710,32 @@ |
| 54094 | 54710 | pInfo->nSize = nPayload + (u16)(pIter - pCell); |
| 54095 | 54711 | if( pInfo->nSize<4 ) pInfo->nSize = 4; |
| 54096 | 54712 | pInfo->nLocal = (u16)nPayload; |
| 54097 | 54713 | pInfo->iOverflow = 0; |
| 54098 | 54714 | }else{ |
| 54099 | | - /* If the payload will not fit completely on the local page, we have |
| 54100 | | - ** to decide how much to store locally and how much to spill onto |
| 54101 | | - ** overflow pages. The strategy is to minimize the amount of unused |
| 54102 | | - ** space on overflow pages while keeping the amount of local storage |
| 54103 | | - ** in between minLocal and maxLocal. |
| 54104 | | - ** |
| 54105 | | - ** Warning: changing the way overflow payload is distributed in any |
| 54106 | | - ** way will result in an incompatible file format. |
| 54107 | | - */ |
| 54108 | | - int minLocal; /* Minimum amount of payload held locally */ |
| 54109 | | - int maxLocal; /* Maximum amount of payload held locally */ |
| 54110 | | - int surplus; /* Overflow payload available for local storage */ |
| 54111 | | - |
| 54112 | | - minLocal = pPage->minLocal; |
| 54113 | | - maxLocal = pPage->maxLocal; |
| 54114 | | - surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
| 54115 | | - testcase( surplus==maxLocal ); |
| 54116 | | - testcase( surplus==maxLocal+1 ); |
| 54117 | | - if( surplus <= maxLocal ){ |
| 54118 | | - pInfo->nLocal = (u16)surplus; |
| 54119 | | - }else{ |
| 54120 | | - pInfo->nLocal = (u16)minLocal; |
| 54121 | | - } |
| 54122 | | - pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell); |
| 54123 | | - pInfo->nSize = pInfo->iOverflow + 4; |
| 54715 | + btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo); |
| 54124 | 54716 | } |
| 54125 | 54717 | } |
| 54126 | 54718 | static void btreeParseCell( |
| 54127 | 54719 | MemPage *pPage, /* Page containing the cell */ |
| 54128 | 54720 | int iCell, /* The cell index. First cell is 0 */ |
| 54129 | 54721 | CellInfo *pInfo /* Fill in this structure */ |
| 54130 | 54722 | ){ |
| 54131 | | - btreeParseCellPtr(pPage, findCell(pPage, iCell), pInfo); |
| 54723 | + pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo); |
| 54132 | 54724 | } |
| 54133 | 54725 | |
| 54134 | 54726 | /* |
| 54727 | +** The following routines are implementations of the MemPage.xCellSize |
| 54728 | +** method. |
| 54729 | +** |
| 54135 | 54730 | ** Compute the total number of bytes that a Cell needs in the cell |
| 54136 | 54731 | ** data area of the btree-page. The return number includes the cell |
| 54137 | 54732 | ** data header and the local payload, but not any overflow page or |
| 54138 | 54733 | ** the space used by the cell pointer. |
| 54734 | +** |
| 54735 | +** cellSizePtrNoPayload() => table internal nodes |
| 54736 | +** cellSizePtr() => all index nodes & table leaf nodes |
| 54139 | 54737 | */ |
| 54140 | 54738 | static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
| 54141 | 54739 | u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */ |
| 54142 | 54740 | u8 *pEnd; /* End mark for a varint */ |
| 54143 | 54741 | u32 nSize; /* Size value to return */ |
| | @@ -54146,22 +54744,17 @@ |
| 54146 | 54744 | /* The value returned by this function should always be the same as |
| 54147 | 54745 | ** the (CellInfo.nSize) value found by doing a full parse of the |
| 54148 | 54746 | ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of |
| 54149 | 54747 | ** this function verifies that this invariant is not violated. */ |
| 54150 | 54748 | CellInfo debuginfo; |
| 54151 | | - btreeParseCellPtr(pPage, pCell, &debuginfo); |
| 54749 | + pPage->xParseCell(pPage, pCell, &debuginfo); |
| 54152 | 54750 | #endif |
| 54153 | 54751 | |
| 54154 | | - if( pPage->noPayload ){ |
| 54155 | | - pEnd = &pIter[9]; |
| 54156 | | - while( (*pIter++)&0x80 && pIter<pEnd ); |
| 54157 | | - assert( pPage->childPtrSize==4 ); |
| 54158 | | - return (u16)(pIter - pCell); |
| 54159 | | - } |
| 54752 | + assert( pPage->noPayload==0 ); |
| 54160 | 54753 | nSize = *pIter; |
| 54161 | 54754 | if( nSize>=0x80 ){ |
| 54162 | | - pEnd = &pIter[9]; |
| 54755 | + pEnd = &pIter[8]; |
| 54163 | 54756 | nSize &= 0x7f; |
| 54164 | 54757 | do{ |
| 54165 | 54758 | nSize = (nSize<<7) | (*++pIter & 0x7f); |
| 54166 | 54759 | }while( *(pIter)>=0x80 && pIter<pEnd ); |
| 54167 | 54760 | } |
| | @@ -54189,16 +54782,36 @@ |
| 54189 | 54782 | nSize += 4 + (u16)(pIter - pCell); |
| 54190 | 54783 | } |
| 54191 | 54784 | assert( nSize==debuginfo.nSize || CORRUPT_DB ); |
| 54192 | 54785 | return (u16)nSize; |
| 54193 | 54786 | } |
| 54787 | +static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){ |
| 54788 | + u8 *pIter = pCell + 4; /* For looping over bytes of pCell */ |
| 54789 | + u8 *pEnd; /* End mark for a varint */ |
| 54790 | + |
| 54791 | +#ifdef SQLITE_DEBUG |
| 54792 | + /* The value returned by this function should always be the same as |
| 54793 | + ** the (CellInfo.nSize) value found by doing a full parse of the |
| 54794 | + ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of |
| 54795 | + ** this function verifies that this invariant is not violated. */ |
| 54796 | + CellInfo debuginfo; |
| 54797 | + pPage->xParseCell(pPage, pCell, &debuginfo); |
| 54798 | +#endif |
| 54799 | + |
| 54800 | + assert( pPage->childPtrSize==4 ); |
| 54801 | + pEnd = pIter + 9; |
| 54802 | + while( (*pIter++)&0x80 && pIter<pEnd ); |
| 54803 | + assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB ); |
| 54804 | + return (u16)(pIter - pCell); |
| 54805 | +} |
| 54806 | + |
| 54194 | 54807 | |
| 54195 | 54808 | #ifdef SQLITE_DEBUG |
| 54196 | 54809 | /* This variation on cellSizePtr() is used inside of assert() statements |
| 54197 | 54810 | ** only. */ |
| 54198 | 54811 | static u16 cellSize(MemPage *pPage, int iCell){ |
| 54199 | | - return cellSizePtr(pPage, findCell(pPage, iCell)); |
| 54812 | + return pPage->xCellSize(pPage, findCell(pPage, iCell)); |
| 54200 | 54813 | } |
| 54201 | 54814 | #endif |
| 54202 | 54815 | |
| 54203 | 54816 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 54204 | 54817 | /* |
| | @@ -54208,11 +54821,11 @@ |
| 54208 | 54821 | */ |
| 54209 | 54822 | static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){ |
| 54210 | 54823 | CellInfo info; |
| 54211 | 54824 | if( *pRC ) return; |
| 54212 | 54825 | assert( pCell!=0 ); |
| 54213 | | - btreeParseCellPtr(pPage, pCell, &info); |
| 54826 | + pPage->xParseCell(pPage, pCell, &info); |
| 54214 | 54827 | if( info.iOverflow ){ |
| 54215 | 54828 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 54216 | 54829 | ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC); |
| 54217 | 54830 | } |
| 54218 | 54831 | } |
| | @@ -54272,11 +54885,11 @@ |
| 54272 | 54885 | */ |
| 54273 | 54886 | if( pc<iCellFirst || pc>iCellLast ){ |
| 54274 | 54887 | return SQLITE_CORRUPT_BKPT; |
| 54275 | 54888 | } |
| 54276 | 54889 | assert( pc>=iCellFirst && pc<=iCellLast ); |
| 54277 | | - size = cellSizePtr(pPage, &src[pc]); |
| 54890 | + size = pPage->xCellSize(pPage, &src[pc]); |
| 54278 | 54891 | cbrk -= size; |
| 54279 | 54892 | if( cbrk<iCellFirst || pc+size>usableSize ){ |
| 54280 | 54893 | return SQLITE_CORRUPT_BKPT; |
| 54281 | 54894 | } |
| 54282 | 54895 | assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); |
| | @@ -54314,22 +54927,24 @@ |
| 54314 | 54927 | ** If no suitable space can be found on the free-list, return NULL. |
| 54315 | 54928 | ** |
| 54316 | 54929 | ** This function may detect corruption within pPg. If corruption is |
| 54317 | 54930 | ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned. |
| 54318 | 54931 | ** |
| 54319 | | -** If a slot of at least nByte bytes is found but cannot be used because |
| 54320 | | -** there are already at least 60 fragmented bytes on the page, return NULL. |
| 54321 | | -** In this case, if pbDefrag parameter is not NULL, set *pbDefrag to true. |
| 54932 | +** Slots on the free list that are between 1 and 3 bytes larger than nByte |
| 54933 | +** will be ignored if adding the extra space to the fragmentation count |
| 54934 | +** causes the fragmentation count to exceed 60. |
| 54322 | 54935 | */ |
| 54323 | | -static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc, int *pbDefrag){ |
| 54936 | +static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){ |
| 54324 | 54937 | const int hdr = pPg->hdrOffset; |
| 54325 | 54938 | u8 * const aData = pPg->aData; |
| 54326 | | - int iAddr; |
| 54327 | | - int pc; |
| 54939 | + int iAddr = hdr + 1; |
| 54940 | + int pc = get2byte(&aData[iAddr]); |
| 54941 | + int x; |
| 54328 | 54942 | int usableSize = pPg->pBt->usableSize; |
| 54329 | 54943 | |
| 54330 | | - for(iAddr=hdr+1; (pc = get2byte(&aData[iAddr]))>0; iAddr=pc){ |
| 54944 | + assert( pc>0 ); |
| 54945 | + do{ |
| 54331 | 54946 | int size; /* Size of the free slot */ |
| 54332 | 54947 | /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of |
| 54333 | 54948 | ** increasing offset. */ |
| 54334 | 54949 | if( pc>usableSize-4 || pc<iAddr+4 ){ |
| 54335 | 54950 | *pRc = SQLITE_CORRUPT_BKPT; |
| | @@ -54337,36 +54952,35 @@ |
| 54337 | 54952 | } |
| 54338 | 54953 | /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each |
| 54339 | 54954 | ** freeblock form a big-endian integer which is the size of the freeblock |
| 54340 | 54955 | ** in bytes, including the 4-byte header. */ |
| 54341 | 54956 | size = get2byte(&aData[pc+2]); |
| 54342 | | - if( size>=nByte ){ |
| 54343 | | - int x = size - nByte; |
| 54957 | + if( (x = size - nByte)>=0 ){ |
| 54344 | 54958 | testcase( x==4 ); |
| 54345 | 54959 | testcase( x==3 ); |
| 54346 | | - if( x<4 ){ |
| 54960 | + if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){ |
| 54961 | + *pRc = SQLITE_CORRUPT_BKPT; |
| 54962 | + return 0; |
| 54963 | + }else if( x<4 ){ |
| 54347 | 54964 | /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total |
| 54348 | 54965 | ** number of bytes in fragments may not exceed 60. */ |
| 54349 | | - if( aData[hdr+7]>=60 ){ |
| 54350 | | - if( pbDefrag ) *pbDefrag = 1; |
| 54351 | | - return 0; |
| 54352 | | - } |
| 54966 | + if( aData[hdr+7]>57 ) return 0; |
| 54967 | + |
| 54353 | 54968 | /* Remove the slot from the free-list. Update the number of |
| 54354 | 54969 | ** fragmented bytes within the page. */ |
| 54355 | 54970 | memcpy(&aData[iAddr], &aData[pc], 2); |
| 54356 | 54971 | aData[hdr+7] += (u8)x; |
| 54357 | | - }else if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){ |
| 54358 | | - *pRc = SQLITE_CORRUPT_BKPT; |
| 54359 | | - return 0; |
| 54360 | 54972 | }else{ |
| 54361 | 54973 | /* The slot remains on the free-list. Reduce its size to account |
| 54362 | 54974 | ** for the portion used by the new allocation. */ |
| 54363 | 54975 | put2byte(&aData[pc+2], x); |
| 54364 | 54976 | } |
| 54365 | 54977 | return &aData[pc + x]; |
| 54366 | 54978 | } |
| 54367 | | - } |
| 54979 | + iAddr = pc; |
| 54980 | + pc = get2byte(&aData[pc]); |
| 54981 | + }while( pc ); |
| 54368 | 54982 | |
| 54369 | 54983 | return 0; |
| 54370 | 54984 | } |
| 54371 | 54985 | |
| 54372 | 54986 | /* |
| | @@ -54403,42 +55017,43 @@ |
| 54403 | 55017 | /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size |
| 54404 | 55018 | ** and the reserved space is zero (the usual value for reserved space) |
| 54405 | 55019 | ** then the cell content offset of an empty page wants to be 65536. |
| 54406 | 55020 | ** However, that integer is too large to be stored in a 2-byte unsigned |
| 54407 | 55021 | ** integer, so a value of 0 is used in its place. */ |
| 54408 | | - top = get2byteNotZero(&data[hdr+5]); |
| 54409 | | - if( gap>top || NEVER((u32)top>pPage->pBt->usableSize) ){ |
| 54410 | | - /* The NEVER() is because a oversize "top" value will be blocked from |
| 54411 | | - ** reaching this point by btreeInitPage() or btreeGetUnusedPage() */ |
| 54412 | | - return SQLITE_CORRUPT_BKPT; |
| 55022 | + top = get2byte(&data[hdr+5]); |
| 55023 | + assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */ |
| 55024 | + if( gap>top ){ |
| 55025 | + if( top==0 && pPage->pBt->usableSize==65536 ){ |
| 55026 | + top = 65536; |
| 55027 | + }else{ |
| 55028 | + return SQLITE_CORRUPT_BKPT; |
| 55029 | + } |
| 54413 | 55030 | } |
| 54414 | 55031 | |
| 54415 | 55032 | /* If there is enough space between gap and top for one more cell pointer |
| 54416 | 55033 | ** array entry offset, and if the freelist is not empty, then search the |
| 54417 | 55034 | ** freelist looking for a free slot big enough to satisfy the request. |
| 54418 | 55035 | */ |
| 54419 | 55036 | testcase( gap+2==top ); |
| 54420 | 55037 | testcase( gap+1==top ); |
| 54421 | 55038 | testcase( gap==top ); |
| 54422 | | - if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){ |
| 54423 | | - int bDefrag = 0; |
| 54424 | | - u8 *pSpace = pageFindSlot(pPage, nByte, &rc, &bDefrag); |
| 54425 | | - if( rc ) return rc; |
| 54426 | | - if( bDefrag ) goto defragment_page; |
| 55039 | + if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){ |
| 55040 | + u8 *pSpace = pageFindSlot(pPage, nByte, &rc); |
| 54427 | 55041 | if( pSpace ){ |
| 54428 | 55042 | assert( pSpace>=data && (pSpace - data)<65536 ); |
| 54429 | 55043 | *pIdx = (int)(pSpace - data); |
| 54430 | 55044 | return SQLITE_OK; |
| 55045 | + }else if( rc ){ |
| 55046 | + return rc; |
| 54431 | 55047 | } |
| 54432 | 55048 | } |
| 54433 | 55049 | |
| 54434 | 55050 | /* The request could not be fulfilled using a freelist slot. Check |
| 54435 | 55051 | ** to see if defragmentation is necessary. |
| 54436 | 55052 | */ |
| 54437 | 55053 | testcase( gap+2+nByte==top ); |
| 54438 | 55054 | if( gap+2+nByte>top ){ |
| 54439 | | - defragment_page: |
| 54440 | 55055 | assert( pPage->nCell>0 || CORRUPT_DB ); |
| 54441 | 55056 | rc = defragmentPage(pPage); |
| 54442 | 55057 | if( rc ) return rc; |
| 54443 | 55058 | top = get2byteNotZero(&data[hdr+5]); |
| 54444 | 55059 | assert( gap+nByte<=top ); |
| | @@ -54510,18 +55125,19 @@ |
| 54510 | 55125 | if( iFreeBlk>iLast ) return SQLITE_CORRUPT_BKPT; |
| 54511 | 55126 | assert( iFreeBlk>iPtr || iFreeBlk==0 ); |
| 54512 | 55127 | |
| 54513 | 55128 | /* At this point: |
| 54514 | 55129 | ** iFreeBlk: First freeblock after iStart, or zero if none |
| 54515 | | - ** iPtr: The address of a pointer iFreeBlk |
| 55130 | + ** iPtr: The address of a pointer to iFreeBlk |
| 54516 | 55131 | ** |
| 54517 | 55132 | ** Check to see if iFreeBlk should be coalesced onto the end of iStart. |
| 54518 | 55133 | */ |
| 54519 | 55134 | if( iFreeBlk && iEnd+3>=iFreeBlk ){ |
| 54520 | 55135 | nFrag = iFreeBlk - iEnd; |
| 54521 | 55136 | if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT; |
| 54522 | 55137 | iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]); |
| 55138 | + if( iEnd > pPage->pBt->usableSize ) return SQLITE_CORRUPT_BKPT; |
| 54523 | 55139 | iSize = iEnd - iStart; |
| 54524 | 55140 | iFreeBlk = get2byte(&data[iFreeBlk]); |
| 54525 | 55141 | } |
| 54526 | 55142 | |
| 54527 | 55143 | /* If iPtr is another freeblock (that is, if iPtr is not the freelist |
| | @@ -54575,21 +55191,30 @@ |
| 54575 | 55191 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
| 54576 | 55192 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 54577 | 55193 | pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); |
| 54578 | 55194 | flagByte &= ~PTF_LEAF; |
| 54579 | 55195 | pPage->childPtrSize = 4-4*pPage->leaf; |
| 55196 | + pPage->xCellSize = cellSizePtr; |
| 54580 | 55197 | pBt = pPage->pBt; |
| 54581 | 55198 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 54582 | 55199 | /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior |
| 54583 | 55200 | ** table b-tree page. */ |
| 54584 | 55201 | assert( (PTF_LEAFDATA|PTF_INTKEY)==5 ); |
| 54585 | 55202 | /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf |
| 54586 | 55203 | ** table b-tree page. */ |
| 54587 | 55204 | assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 ); |
| 54588 | 55205 | pPage->intKey = 1; |
| 54589 | | - pPage->intKeyLeaf = pPage->leaf; |
| 54590 | | - pPage->noPayload = !pPage->leaf; |
| 55206 | + if( pPage->leaf ){ |
| 55207 | + pPage->intKeyLeaf = 1; |
| 55208 | + pPage->noPayload = 0; |
| 55209 | + pPage->xParseCell = btreeParseCellPtr; |
| 55210 | + }else{ |
| 55211 | + pPage->intKeyLeaf = 0; |
| 55212 | + pPage->noPayload = 1; |
| 55213 | + pPage->xCellSize = cellSizePtrNoPayload; |
| 55214 | + pPage->xParseCell = btreeParseCellPtrNoPayload; |
| 55215 | + } |
| 54591 | 55216 | pPage->maxLocal = pBt->maxLeaf; |
| 54592 | 55217 | pPage->minLocal = pBt->minLeaf; |
| 54593 | 55218 | }else if( flagByte==PTF_ZERODATA ){ |
| 54594 | 55219 | /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior |
| 54595 | 55220 | ** index b-tree page. */ |
| | @@ -54598,10 +55223,11 @@ |
| 54598 | 55223 | ** index b-tree page. */ |
| 54599 | 55224 | assert( (PTF_ZERODATA|PTF_LEAF)==10 ); |
| 54600 | 55225 | pPage->intKey = 0; |
| 54601 | 55226 | pPage->intKeyLeaf = 0; |
| 54602 | 55227 | pPage->noPayload = 0; |
| 55228 | + pPage->xParseCell = btreeParseCellPtrIndex; |
| 54603 | 55229 | pPage->maxLocal = pBt->maxLocal; |
| 54604 | 55230 | pPage->minLocal = pBt->minLocal; |
| 54605 | 55231 | }else{ |
| 54606 | 55232 | /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is |
| 54607 | 55233 | ** an error. */ |
| | @@ -54653,10 +55279,11 @@ |
| 54653 | 55279 | pPage->nOverflow = 0; |
| 54654 | 55280 | usableSize = pBt->usableSize; |
| 54655 | 55281 | pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize; |
| 54656 | 55282 | pPage->aDataEnd = &data[usableSize]; |
| 54657 | 55283 | pPage->aCellIdx = &data[cellOffset]; |
| 55284 | + pPage->aDataOfst = &data[pPage->childPtrSize]; |
| 54658 | 55285 | /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates |
| 54659 | 55286 | ** the start of the cell content area. A zero value for this integer is |
| 54660 | 55287 | ** interpreted as 65536. */ |
| 54661 | 55288 | top = get2byteNotZero(&data[hdr+5]); |
| 54662 | 55289 | /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the |
| | @@ -54686,17 +55313,17 @@ |
| 54686 | 55313 | int i; /* Index into the cell pointer array */ |
| 54687 | 55314 | int sz; /* Size of a cell */ |
| 54688 | 55315 | |
| 54689 | 55316 | if( !pPage->leaf ) iCellLast--; |
| 54690 | 55317 | for(i=0; i<pPage->nCell; i++){ |
| 54691 | | - pc = get2byte(&data[cellOffset+i*2]); |
| 55318 | + pc = get2byteAligned(&data[cellOffset+i*2]); |
| 54692 | 55319 | testcase( pc==iCellFirst ); |
| 54693 | 55320 | testcase( pc==iCellLast ); |
| 54694 | 55321 | if( pc<iCellFirst || pc>iCellLast ){ |
| 54695 | 55322 | return SQLITE_CORRUPT_BKPT; |
| 54696 | 55323 | } |
| 54697 | | - sz = cellSizePtr(pPage, &data[pc]); |
| 55324 | + sz = pPage->xCellSize(pPage, &data[pc]); |
| 54698 | 55325 | testcase( pc+sz==usableSize ); |
| 54699 | 55326 | if( pc+sz>usableSize ){ |
| 54700 | 55327 | return SQLITE_CORRUPT_BKPT; |
| 54701 | 55328 | } |
| 54702 | 55329 | } |
| | @@ -54772,10 +55399,11 @@ |
| 54772 | 55399 | pPage->nFree = (u16)(pBt->usableSize - first); |
| 54773 | 55400 | decodeFlags(pPage, flags); |
| 54774 | 55401 | pPage->cellOffset = first; |
| 54775 | 55402 | pPage->aDataEnd = &data[pBt->usableSize]; |
| 54776 | 55403 | pPage->aCellIdx = &data[first]; |
| 55404 | + pPage->aDataOfst = &data[pPage->childPtrSize]; |
| 54777 | 55405 | pPage->nOverflow = 0; |
| 54778 | 55406 | assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); |
| 54779 | 55407 | pPage->maskPage = (u16)(pBt->pageSize - 1); |
| 54780 | 55408 | pPage->nCell = 0; |
| 54781 | 55409 | pPage->isInit = 1; |
| | @@ -54790,11 +55418,11 @@ |
| 54790 | 55418 | MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); |
| 54791 | 55419 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 54792 | 55420 | pPage->pDbPage = pDbPage; |
| 54793 | 55421 | pPage->pBt = pBt; |
| 54794 | 55422 | pPage->pgno = pgno; |
| 54795 | | - pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
| 55423 | + pPage->hdrOffset = pgno==1 ? 100 : 0; |
| 54796 | 55424 | return pPage; |
| 54797 | 55425 | } |
| 54798 | 55426 | |
| 54799 | 55427 | /* |
| 54800 | 55428 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| | @@ -54851,58 +55479,86 @@ |
| 54851 | 55479 | assert( ((p->pBt->nPage)&0x8000000)==0 ); |
| 54852 | 55480 | return btreePagecount(p->pBt); |
| 54853 | 55481 | } |
| 54854 | 55482 | |
| 54855 | 55483 | /* |
| 54856 | | -** Get a page from the pager and initialize it. This routine is just a |
| 54857 | | -** convenience wrapper around separate calls to btreeGetPage() and |
| 54858 | | -** btreeInitPage(). |
| 55484 | +** Get a page from the pager and initialize it. |
| 54859 | 55485 | ** |
| 54860 | | -** If an error occurs, then the value *ppPage is set to is undefined. It |
| 55486 | +** If pCur!=0 then the page is being fetched as part of a moveToChild() |
| 55487 | +** call. Do additional sanity checking on the page in this case. |
| 55488 | +** And if the fetch fails, this routine must decrement pCur->iPage. |
| 55489 | +** |
| 55490 | +** The page is fetched as read-write unless pCur is not NULL and is |
| 55491 | +** a read-only cursor. |
| 55492 | +** |
| 55493 | +** If an error occurs, then *ppPage is undefined. It |
| 54861 | 55494 | ** may remain unchanged, or it may be set to an invalid value. |
| 54862 | 55495 | */ |
| 54863 | 55496 | static int getAndInitPage( |
| 54864 | 55497 | BtShared *pBt, /* The database file */ |
| 54865 | 55498 | Pgno pgno, /* Number of the page to get */ |
| 54866 | 55499 | MemPage **ppPage, /* Write the page pointer here */ |
| 54867 | | - int bReadonly /* PAGER_GET_READONLY or 0 */ |
| 55500 | + BtCursor *pCur, /* Cursor to receive the page, or NULL */ |
| 55501 | + int bReadOnly /* True for a read-only page */ |
| 54868 | 55502 | ){ |
| 54869 | 55503 | int rc; |
| 55504 | + DbPage *pDbPage; |
| 54870 | 55505 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 54871 | | - assert( bReadonly==PAGER_GET_READONLY || bReadonly==0 ); |
| 55506 | + assert( pCur==0 || ppPage==&pCur->apPage[pCur->iPage] ); |
| 55507 | + assert( pCur==0 || bReadOnly==pCur->curPagerFlags ); |
| 55508 | + assert( pCur==0 || pCur->iPage>0 ); |
| 54872 | 55509 | |
| 54873 | 55510 | if( pgno>btreePagecount(pBt) ){ |
| 54874 | 55511 | rc = SQLITE_CORRUPT_BKPT; |
| 54875 | | - }else{ |
| 54876 | | - rc = btreeGetPage(pBt, pgno, ppPage, bReadonly); |
| 54877 | | - if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
| 54878 | | - rc = btreeInitPage(*ppPage); |
| 54879 | | - if( rc!=SQLITE_OK ){ |
| 54880 | | - releasePage(*ppPage); |
| 54881 | | - } |
| 55512 | + goto getAndInitPage_error; |
| 55513 | + } |
| 55514 | + rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly); |
| 55515 | + if( rc ){ |
| 55516 | + goto getAndInitPage_error; |
| 55517 | + } |
| 55518 | + *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt); |
| 55519 | + if( (*ppPage)->isInit==0 ){ |
| 55520 | + rc = btreeInitPage(*ppPage); |
| 55521 | + if( rc!=SQLITE_OK ){ |
| 55522 | + releasePage(*ppPage); |
| 55523 | + goto getAndInitPage_error; |
| 54882 | 55524 | } |
| 54883 | 55525 | } |
| 54884 | 55526 | |
| 55527 | + /* If obtaining a child page for a cursor, we must verify that the page is |
| 55528 | + ** compatible with the root page. */ |
| 55529 | + if( pCur |
| 55530 | + && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) |
| 55531 | + ){ |
| 55532 | + rc = SQLITE_CORRUPT_BKPT; |
| 55533 | + releasePage(*ppPage); |
| 55534 | + goto getAndInitPage_error; |
| 55535 | + } |
| 55536 | + return SQLITE_OK; |
| 55537 | + |
| 55538 | +getAndInitPage_error: |
| 55539 | + if( pCur ) pCur->iPage--; |
| 54885 | 55540 | testcase( pgno==0 ); |
| 54886 | 55541 | assert( pgno!=0 || rc==SQLITE_CORRUPT ); |
| 54887 | 55542 | return rc; |
| 54888 | 55543 | } |
| 54889 | 55544 | |
| 54890 | 55545 | /* |
| 54891 | 55546 | ** Release a MemPage. This should be called once for each prior |
| 54892 | 55547 | ** call to btreeGetPage. |
| 54893 | 55548 | */ |
| 55549 | +static void releasePageNotNull(MemPage *pPage){ |
| 55550 | + assert( pPage->aData ); |
| 55551 | + assert( pPage->pBt ); |
| 55552 | + assert( pPage->pDbPage!=0 ); |
| 55553 | + assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 55554 | + assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
| 55555 | + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 55556 | + sqlite3PagerUnrefNotNull(pPage->pDbPage); |
| 55557 | +} |
| 54894 | 55558 | static void releasePage(MemPage *pPage){ |
| 54895 | | - if( pPage ){ |
| 54896 | | - assert( pPage->aData ); |
| 54897 | | - assert( pPage->pBt ); |
| 54898 | | - assert( pPage->pDbPage!=0 ); |
| 54899 | | - assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 54900 | | - assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
| 54901 | | - assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 54902 | | - sqlite3PagerUnrefNotNull(pPage->pDbPage); |
| 54903 | | - } |
| 55559 | + if( pPage ) releasePageNotNull(pPage); |
| 54904 | 55560 | } |
| 54905 | 55561 | |
| 54906 | 55562 | /* |
| 54907 | 55563 | ** Get an unused page. |
| 54908 | 55564 | ** |
| | @@ -55873,11 +56529,11 @@ |
| 55873 | 56529 | if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){ |
| 55874 | 56530 | MemPage *pPage1 = pBt->pPage1; |
| 55875 | 56531 | assert( pPage1->aData ); |
| 55876 | 56532 | assert( sqlite3PagerRefcount(pBt->pPager)==1 ); |
| 55877 | 56533 | pBt->pPage1 = 0; |
| 55878 | | - releasePage(pPage1); |
| 56534 | + releasePageNotNull(pPage1); |
| 55879 | 56535 | } |
| 55880 | 56536 | } |
| 55881 | 56537 | |
| 55882 | 56538 | /* |
| 55883 | 56539 | ** If pBt points to an empty file then convert that empty file |
| | @@ -56188,11 +56844,11 @@ |
| 56188 | 56844 | |
| 56189 | 56845 | for(i=0; i<nCell; i++){ |
| 56190 | 56846 | u8 *pCell = findCell(pPage, i); |
| 56191 | 56847 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 56192 | 56848 | CellInfo info; |
| 56193 | | - btreeParseCellPtr(pPage, pCell, &info); |
| 56849 | + pPage->xParseCell(pPage, pCell, &info); |
| 56194 | 56850 | if( info.iOverflow |
| 56195 | 56851 | && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage |
| 56196 | 56852 | && iFrom==get4byte(&pCell[info.iOverflow]) |
| 56197 | 56853 | ){ |
| 56198 | 56854 | put4byte(&pCell[info.iOverflow], iTo); |
| | @@ -56929,10 +57585,11 @@ |
| 56929 | 57585 | int wrFlag, /* 1 to write. 0 read-only */ |
| 56930 | 57586 | struct KeyInfo *pKeyInfo, /* First arg to comparison function */ |
| 56931 | 57587 | BtCursor *pCur /* Space for new cursor */ |
| 56932 | 57588 | ){ |
| 56933 | 57589 | BtShared *pBt = p->pBt; /* Shared b-tree handle */ |
| 57590 | + BtCursor *pX; /* Looping over other all cursors */ |
| 56934 | 57591 | |
| 56935 | 57592 | assert( sqlite3BtreeHoldsMutex(p) ); |
| 56936 | 57593 | assert( wrFlag==0 || wrFlag==1 ); |
| 56937 | 57594 | |
| 56938 | 57595 | /* The following assert statements verify that if this is a sharable |
| | @@ -56944,14 +57601,12 @@ |
| 56944 | 57601 | |
| 56945 | 57602 | /* Assert that the caller has opened the required transaction. */ |
| 56946 | 57603 | assert( p->inTrans>TRANS_NONE ); |
| 56947 | 57604 | assert( wrFlag==0 || p->inTrans==TRANS_WRITE ); |
| 56948 | 57605 | assert( pBt->pPage1 && pBt->pPage1->aData ); |
| 57606 | + assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
| 56949 | 57607 | |
| 56950 | | - if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){ |
| 56951 | | - return SQLITE_READONLY; |
| 56952 | | - } |
| 56953 | 57608 | if( wrFlag ){ |
| 56954 | 57609 | allocateTempSpace(pBt); |
| 56955 | 57610 | if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM; |
| 56956 | 57611 | } |
| 56957 | 57612 | if( iTable==1 && btreePagecount(pBt)==0 ){ |
| | @@ -56966,14 +57621,20 @@ |
| 56966 | 57621 | pCur->pKeyInfo = pKeyInfo; |
| 56967 | 57622 | pCur->pBtree = p; |
| 56968 | 57623 | pCur->pBt = pBt; |
| 56969 | 57624 | assert( wrFlag==0 || wrFlag==BTCF_WriteFlag ); |
| 56970 | 57625 | pCur->curFlags = wrFlag; |
| 57626 | + pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY; |
| 57627 | + /* If there are two or more cursors on the same btree, then all such |
| 57628 | + ** cursors *must* have the BTCF_Multiple flag set. */ |
| 57629 | + for(pX=pBt->pCursor; pX; pX=pX->pNext){ |
| 57630 | + if( pX->pgnoRoot==(Pgno)iTable ){ |
| 57631 | + pX->curFlags |= BTCF_Multiple; |
| 57632 | + pCur->curFlags |= BTCF_Multiple; |
| 57633 | + } |
| 57634 | + } |
| 56971 | 57635 | pCur->pNext = pBt->pCursor; |
| 56972 | | - if( pCur->pNext ){ |
| 56973 | | - pCur->pNext->pPrev = pCur; |
| 56974 | | - } |
| 56975 | 57636 | pBt->pCursor = pCur; |
| 56976 | 57637 | pCur->eState = CURSOR_INVALID; |
| 56977 | 57638 | return SQLITE_OK; |
| 56978 | 57639 | } |
| 56979 | 57640 | SQLITE_PRIVATE int sqlite3BtreeCursor( |
| | @@ -57027,17 +57688,22 @@ |
| 57027 | 57688 | if( pBtree ){ |
| 57028 | 57689 | int i; |
| 57029 | 57690 | BtShared *pBt = pCur->pBt; |
| 57030 | 57691 | sqlite3BtreeEnter(pBtree); |
| 57031 | 57692 | sqlite3BtreeClearCursor(pCur); |
| 57032 | | - if( pCur->pPrev ){ |
| 57033 | | - pCur->pPrev->pNext = pCur->pNext; |
| 57034 | | - }else{ |
| 57693 | + assert( pBt->pCursor!=0 ); |
| 57694 | + if( pBt->pCursor==pCur ){ |
| 57035 | 57695 | pBt->pCursor = pCur->pNext; |
| 57036 | | - } |
| 57037 | | - if( pCur->pNext ){ |
| 57038 | | - pCur->pNext->pPrev = pCur->pPrev; |
| 57696 | + }else{ |
| 57697 | + BtCursor *pPrev = pBt->pCursor; |
| 57698 | + do{ |
| 57699 | + if( pPrev->pNext==pCur ){ |
| 57700 | + pPrev->pNext = pCur->pNext; |
| 57701 | + break; |
| 57702 | + } |
| 57703 | + pPrev = pPrev->pNext; |
| 57704 | + }while( ALWAYS(pPrev) ); |
| 57039 | 57705 | } |
| 57040 | 57706 | for(i=0; i<=pCur->iPage; i++){ |
| 57041 | 57707 | releasePage(pCur->apPage[i]); |
| 57042 | 57708 | } |
| 57043 | 57709 | unlockBtreeIfUnused(pBt); |
| | @@ -57053,17 +57719,10 @@ |
| 57053 | 57719 | ** BtCursor.info structure. If it is not already valid, call |
| 57054 | 57720 | ** btreeParseCell() to fill it in. |
| 57055 | 57721 | ** |
| 57056 | 57722 | ** BtCursor.info is a cache of the information in the current cell. |
| 57057 | 57723 | ** Using this cache reduces the number of calls to btreeParseCell(). |
| 57058 | | -** |
| 57059 | | -** 2007-06-25: There is a bug in some versions of MSVC that cause the |
| 57060 | | -** compiler to crash when getCellInfo() is implemented as a macro. |
| 57061 | | -** But there is a measureable speed advantage to using the macro on gcc |
| 57062 | | -** (when less compiler optimizations like -Os or -O0 are used and the |
| 57063 | | -** compiler is not doing aggressive inlining.) So we use a real function |
| 57064 | | -** for MSVC and a macro for everything else. Ticket #2457. |
| 57065 | 57724 | */ |
| 57066 | 57725 | #ifndef NDEBUG |
| 57067 | 57726 | static void assertCellInfo(BtCursor *pCur){ |
| 57068 | 57727 | CellInfo info; |
| 57069 | 57728 | int iPage = pCur->iPage; |
| | @@ -57072,32 +57731,19 @@ |
| 57072 | 57731 | assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 ); |
| 57073 | 57732 | } |
| 57074 | 57733 | #else |
| 57075 | 57734 | #define assertCellInfo(x) |
| 57076 | 57735 | #endif |
| 57077 | | -#ifdef _MSC_VER |
| 57078 | | - /* Use a real function in MSVC to work around bugs in that compiler. */ |
| 57079 | | - static void getCellInfo(BtCursor *pCur){ |
| 57080 | | - if( pCur->info.nSize==0 ){ |
| 57081 | | - int iPage = pCur->iPage; |
| 57082 | | - btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); |
| 57083 | | - pCur->curFlags |= BTCF_ValidNKey; |
| 57084 | | - }else{ |
| 57085 | | - assertCellInfo(pCur); |
| 57086 | | - } |
| 57087 | | - } |
| 57088 | | -#else /* if not _MSC_VER */ |
| 57089 | | - /* Use a macro in all other compilers so that the function is inlined */ |
| 57090 | | -#define getCellInfo(pCur) \ |
| 57091 | | - if( pCur->info.nSize==0 ){ \ |
| 57092 | | - int iPage = pCur->iPage; \ |
| 57093 | | - btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \ |
| 57094 | | - pCur->curFlags |= BTCF_ValidNKey; \ |
| 57095 | | - }else{ \ |
| 57096 | | - assertCellInfo(pCur); \ |
| 57097 | | - } |
| 57098 | | -#endif /* _MSC_VER */ |
| 57736 | +static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){ |
| 57737 | + if( pCur->info.nSize==0 ){ |
| 57738 | + int iPage = pCur->iPage; |
| 57739 | + pCur->curFlags |= BTCF_ValidNKey; |
| 57740 | + btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); |
| 57741 | + }else{ |
| 57742 | + assertCellInfo(pCur); |
| 57743 | + } |
| 57744 | +} |
| 57099 | 57745 | |
| 57100 | 57746 | #ifndef NDEBUG /* The next routine used only within assert() statements */ |
| 57101 | 57747 | /* |
| 57102 | 57748 | ** Return true if the given BtCursor is valid. A valid cursor is one |
| 57103 | 57749 | ** that is currently pointing to a row in a (non-empty) table. |
| | @@ -57599,35 +58245,25 @@ |
| 57599 | 58245 | ** the new child page does not match the flags field of the parent (i.e. |
| 57600 | 58246 | ** if an intkey page appears to be the parent of a non-intkey page, or |
| 57601 | 58247 | ** vice-versa). |
| 57602 | 58248 | */ |
| 57603 | 58249 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
| 57604 | | - int rc; |
| 57605 | | - int i = pCur->iPage; |
| 57606 | | - MemPage *pNewPage; |
| 57607 | 58250 | BtShared *pBt = pCur->pBt; |
| 57608 | 58251 | |
| 57609 | 58252 | assert( cursorHoldsMutex(pCur) ); |
| 57610 | 58253 | assert( pCur->eState==CURSOR_VALID ); |
| 57611 | 58254 | assert( pCur->iPage<BTCURSOR_MAX_DEPTH ); |
| 57612 | 58255 | assert( pCur->iPage>=0 ); |
| 57613 | 58256 | if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){ |
| 57614 | 58257 | return SQLITE_CORRUPT_BKPT; |
| 57615 | 58258 | } |
| 57616 | | - rc = getAndInitPage(pBt, newPgno, &pNewPage, |
| 57617 | | - (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0); |
| 57618 | | - if( rc ) return rc; |
| 57619 | | - pCur->apPage[i+1] = pNewPage; |
| 57620 | | - pCur->aiIdx[i+1] = 0; |
| 57621 | | - pCur->iPage++; |
| 57622 | | - |
| 57623 | 58259 | pCur->info.nSize = 0; |
| 57624 | 58260 | pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); |
| 57625 | | - if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){ |
| 57626 | | - return SQLITE_CORRUPT_BKPT; |
| 57627 | | - } |
| 57628 | | - return SQLITE_OK; |
| 58261 | + pCur->iPage++; |
| 58262 | + pCur->aiIdx[pCur->iPage] = 0; |
| 58263 | + return getAndInitPage(pBt, newPgno, &pCur->apPage[pCur->iPage], |
| 58264 | + pCur, pCur->curPagerFlags); |
| 57629 | 58265 | } |
| 57630 | 58266 | |
| 57631 | 58267 | #if SQLITE_DEBUG |
| 57632 | 58268 | /* |
| 57633 | 58269 | ** Page pParent is an internal (non-leaf) tree page. This function |
| | @@ -57667,15 +58303,13 @@ |
| 57667 | 58303 | pCur->apPage[pCur->iPage-1], |
| 57668 | 58304 | pCur->aiIdx[pCur->iPage-1], |
| 57669 | 58305 | pCur->apPage[pCur->iPage]->pgno |
| 57670 | 58306 | ); |
| 57671 | 58307 | testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell ); |
| 57672 | | - |
| 57673 | | - releasePage(pCur->apPage[pCur->iPage]); |
| 57674 | | - pCur->iPage--; |
| 57675 | 58308 | pCur->info.nSize = 0; |
| 57676 | 58309 | pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); |
| 58310 | + releasePageNotNull(pCur->apPage[pCur->iPage--]); |
| 57677 | 58311 | } |
| 57678 | 58312 | |
| 57679 | 58313 | /* |
| 57680 | 58314 | ** Move the cursor to point to the root page of its b-tree structure. |
| 57681 | 58315 | ** |
| | @@ -57712,22 +58346,27 @@ |
| 57712 | 58346 | } |
| 57713 | 58347 | sqlite3BtreeClearCursor(pCur); |
| 57714 | 58348 | } |
| 57715 | 58349 | |
| 57716 | 58350 | if( pCur->iPage>=0 ){ |
| 57717 | | - while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]); |
| 58351 | + while( pCur->iPage ){ |
| 58352 | + assert( pCur->apPage[pCur->iPage]!=0 ); |
| 58353 | + releasePageNotNull(pCur->apPage[pCur->iPage--]); |
| 58354 | + } |
| 57718 | 58355 | }else if( pCur->pgnoRoot==0 ){ |
| 57719 | 58356 | pCur->eState = CURSOR_INVALID; |
| 57720 | 58357 | return SQLITE_OK; |
| 57721 | 58358 | }else{ |
| 58359 | + assert( pCur->iPage==(-1) ); |
| 57722 | 58360 | rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0], |
| 57723 | | - (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0); |
| 58361 | + 0, pCur->curPagerFlags); |
| 57724 | 58362 | if( rc!=SQLITE_OK ){ |
| 57725 | 58363 | pCur->eState = CURSOR_INVALID; |
| 57726 | 58364 | return rc; |
| 57727 | 58365 | } |
| 57728 | 58366 | pCur->iPage = 0; |
| 58367 | + pCur->curIntKey = pCur->apPage[0]->intKey; |
| 57729 | 58368 | } |
| 57730 | 58369 | pRoot = pCur->apPage[0]; |
| 57731 | 58370 | assert( pRoot->pgno==pCur->pgnoRoot ); |
| 57732 | 58371 | |
| 57733 | 58372 | /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor |
| | @@ -57926,11 +58565,11 @@ |
| 57926 | 58565 | assert( (pIdxKey==0)==(pCur->pKeyInfo==0) ); |
| 57927 | 58566 | |
| 57928 | 58567 | /* If the cursor is already positioned at the point we are trying |
| 57929 | 58568 | ** to move to, then just return without doing any work */ |
| 57930 | 58569 | if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 |
| 57931 | | - && pCur->apPage[0]->intKey |
| 58570 | + && pCur->curIntKey |
| 57932 | 58571 | ){ |
| 57933 | 58572 | if( pCur->info.nKey==intKey ){ |
| 57934 | 58573 | *pRes = 0; |
| 57935 | 58574 | return SQLITE_OK; |
| 57936 | 58575 | } |
| | @@ -57961,11 +58600,12 @@ |
| 57961 | 58600 | if( pCur->eState==CURSOR_INVALID ){ |
| 57962 | 58601 | *pRes = -1; |
| 57963 | 58602 | assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); |
| 57964 | 58603 | return SQLITE_OK; |
| 57965 | 58604 | } |
| 57966 | | - assert( pCur->apPage[0]->intKey || pIdxKey ); |
| 58605 | + assert( pCur->apPage[0]->intKey==pCur->curIntKey ); |
| 58606 | + assert( pCur->curIntKey || pIdxKey ); |
| 57967 | 58607 | for(;;){ |
| 57968 | 58608 | int lwr, upr, idx, c; |
| 57969 | 58609 | Pgno chldPg; |
| 57970 | 58610 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
| 57971 | 58611 | u8 *pCell; /* Pointer to current cell in pPage */ |
| | @@ -57984,11 +58624,11 @@ |
| 57984 | 58624 | idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */ |
| 57985 | 58625 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
| 57986 | 58626 | if( xRecordCompare==0 ){ |
| 57987 | 58627 | for(;;){ |
| 57988 | 58628 | i64 nCellKey; |
| 57989 | | - pCell = findCell(pPage, idx) + pPage->childPtrSize; |
| 58629 | + pCell = findCellPastPtr(pPage, idx); |
| 57990 | 58630 | if( pPage->intKeyLeaf ){ |
| 57991 | 58631 | while( 0x80 <= *(pCell++) ){ |
| 57992 | 58632 | if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT; |
| 57993 | 58633 | } |
| 57994 | 58634 | } |
| | @@ -58017,11 +58657,11 @@ |
| 58017 | 58657 | idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */ |
| 58018 | 58658 | } |
| 58019 | 58659 | }else{ |
| 58020 | 58660 | for(;;){ |
| 58021 | 58661 | int nCell; /* Size of the pCell cell in bytes */ |
| 58022 | | - pCell = findCell(pPage, idx) + pPage->childPtrSize; |
| 58662 | + pCell = findCellPastPtr(pPage, idx); |
| 58023 | 58663 | |
| 58024 | 58664 | /* The maximum supported page-size is 65536 bytes. This means that |
| 58025 | 58665 | ** the maximum number of record bytes stored on an index B-Tree |
| 58026 | 58666 | ** page is less than 16384 bytes and may be stored as a 2-byte |
| 58027 | 58667 | ** varint. This information is used to attempt to avoid parsing |
| | @@ -58053,11 +58693,11 @@ |
| 58053 | 58693 | ** up to two varints past the end of the buffer. An extra 18 |
| 58054 | 58694 | ** bytes of padding is allocated at the end of the buffer in |
| 58055 | 58695 | ** case this happens. */ |
| 58056 | 58696 | void *pCellKey; |
| 58057 | 58697 | u8 * const pCellBody = pCell - pPage->childPtrSize; |
| 58058 | | - btreeParseCellPtr(pPage, pCellBody, &pCur->info); |
| 58698 | + pPage->xParseCell(pPage, pCellBody, &pCur->info); |
| 58059 | 58699 | nCell = (int)pCur->info.nKey; |
| 58060 | 58700 | testcase( nCell<0 ); /* True if key size is 2^32 or more */ |
| 58061 | 58701 | testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */ |
| 58062 | 58702 | testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */ |
| 58063 | 58703 | testcase( nCell==2 ); /* Minimum legal index key size */ |
| | @@ -58356,12 +58996,11 @@ |
| 58356 | 58996 | ** has already been called on the new page.) The new page has also |
| 58357 | 58997 | ** been referenced and the calling routine is responsible for calling |
| 58358 | 58998 | ** sqlite3PagerUnref() on the new page when it is done. |
| 58359 | 58999 | ** |
| 58360 | 59000 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 58361 | | -** an error. *ppPage and *pPgno are undefined in the event of an error. |
| 58362 | | -** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
| 59001 | +** an error. *ppPage is set to NULL in the event of an error. |
| 58363 | 59002 | ** |
| 58364 | 59003 | ** If the "nearby" parameter is not 0, then an effort is made to |
| 58365 | 59004 | ** locate a page close to the page number "nearby". This can be used in an |
| 58366 | 59005 | ** attempt to keep related pages close to each other in the database file, |
| 58367 | 59006 | ** which in turn can make database access faster. |
| | @@ -58400,10 +59039,11 @@ |
| 58400 | 59039 | } |
| 58401 | 59040 | if( n>0 ){ |
| 58402 | 59041 | /* There are pages on the freelist. Reuse one of those pages. */ |
| 58403 | 59042 | Pgno iTrunk; |
| 58404 | 59043 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 59044 | + u32 nSearch = 0; /* Count of the number of search attempts */ |
| 58405 | 59045 | |
| 58406 | 59046 | /* If eMode==BTALLOC_EXACT and a query of the pointer-map |
| 58407 | 59047 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 58408 | 59048 | ** the entire-list will be searched for that page. |
| 58409 | 59049 | */ |
| | @@ -58448,11 +59088,11 @@ |
| 58448 | 59088 | ** stores the page number of the first page of the freelist, or zero if |
| 58449 | 59089 | ** the freelist is empty. */ |
| 58450 | 59090 | iTrunk = get4byte(&pPage1->aData[32]); |
| 58451 | 59091 | } |
| 58452 | 59092 | testcase( iTrunk==mxPage ); |
| 58453 | | - if( iTrunk>mxPage ){ |
| 59093 | + if( iTrunk>mxPage || nSearch++ > n ){ |
| 58454 | 59094 | rc = SQLITE_CORRUPT_BKPT; |
| 58455 | 59095 | }else{ |
| 58456 | 59096 | rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0); |
| 58457 | 59097 | } |
| 58458 | 59098 | if( rc ){ |
| | @@ -58601,10 +59241,11 @@ |
| 58601 | 59241 | rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent); |
| 58602 | 59242 | if( rc==SQLITE_OK ){ |
| 58603 | 59243 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
| 58604 | 59244 | if( rc!=SQLITE_OK ){ |
| 58605 | 59245 | releasePage(*ppPage); |
| 59246 | + *ppPage = 0; |
| 58606 | 59247 | } |
| 58607 | 59248 | } |
| 58608 | 59249 | searchList = 0; |
| 58609 | 59250 | } |
| 58610 | 59251 | } |
| | @@ -58842,11 +59483,11 @@ |
| 58842 | 59483 | int rc; |
| 58843 | 59484 | int nOvfl; |
| 58844 | 59485 | u32 ovflPageSize; |
| 58845 | 59486 | |
| 58846 | 59487 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 58847 | | - btreeParseCellPtr(pPage, pCell, &info); |
| 59488 | + pPage->xParseCell(pPage, pCell, &info); |
| 58848 | 59489 | *pnSize = info.nSize; |
| 58849 | 59490 | if( info.iOverflow==0 ){ |
| 58850 | 59491 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
| 58851 | 59492 | } |
| 58852 | 59493 | if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){ |
| | @@ -58954,13 +59595,11 @@ |
| 58954 | 59595 | if( pPage->intKey ){ |
| 58955 | 59596 | pSrc = pData; |
| 58956 | 59597 | nSrc = nData; |
| 58957 | 59598 | nData = 0; |
| 58958 | 59599 | }else{ |
| 58959 | | - if( NEVER(nKey>0x7fffffff || pKey==0) ){ |
| 58960 | | - return SQLITE_CORRUPT_BKPT; |
| 58961 | | - } |
| 59600 | + assert( nKey<=0x7fffffff && pKey!=0 ); |
| 58962 | 59601 | nPayload = (int)nKey; |
| 58963 | 59602 | pSrc = pKey; |
| 58964 | 59603 | nSrc = (int)nKey; |
| 58965 | 59604 | } |
| 58966 | 59605 | if( nPayload<=pPage->maxLocal ){ |
| | @@ -58996,11 +59635,11 @@ |
| 58996 | 59635 | ** were computed correctly. |
| 58997 | 59636 | */ |
| 58998 | 59637 | #if SQLITE_DEBUG |
| 58999 | 59638 | { |
| 59000 | 59639 | CellInfo info; |
| 59001 | | - btreeParseCellPtr(pPage, pCell, &info); |
| 59640 | + pPage->xParseCell(pPage, pCell, &info); |
| 59002 | 59641 | assert( nHeader=(int)(info.pPayload - pCell) ); |
| 59003 | 59642 | assert( info.nKey==nKey ); |
| 59004 | 59643 | assert( *pnSize == info.nSize ); |
| 59005 | 59644 | assert( spaceLeft == info.nLocal ); |
| 59006 | 59645 | assert( pPrior == &pCell[info.iOverflow] ); |
| | @@ -59166,14 +59805,12 @@ |
| 59166 | 59805 | Pgno iChild, /* If non-zero, replace first 4 bytes with this value */ |
| 59167 | 59806 | int *pRC /* Read and write return code from here */ |
| 59168 | 59807 | ){ |
| 59169 | 59808 | int idx = 0; /* Where to write new cell content in data[] */ |
| 59170 | 59809 | int j; /* Loop counter */ |
| 59171 | | - int end; /* First byte past the last cell pointer in data[] */ |
| 59172 | | - int ins; /* Index in data[] where new cell pointer is inserted */ |
| 59173 | | - int cellOffset; /* Address of first cell pointer in data[] */ |
| 59174 | 59810 | u8 *data; /* The content of the whole page */ |
| 59811 | + u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */ |
| 59175 | 59812 | |
| 59176 | 59813 | if( *pRC ) return; |
| 59177 | 59814 | |
| 59178 | 59815 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 59179 | 59816 | assert( MX_CELL(pPage->pBt)<=10921 ); |
| | @@ -59184,11 +59821,11 @@ |
| 59184 | 59821 | /* The cell should normally be sized correctly. However, when moving a |
| 59185 | 59822 | ** malformed cell from a leaf page to an interior page, if the cell size |
| 59186 | 59823 | ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size |
| 59187 | 59824 | ** might be less than 8 (leaf-size + pointer) on the interior node. Hence |
| 59188 | 59825 | ** the term after the || in the following assert(). */ |
| 59189 | | - assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) ); |
| 59826 | + assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) ); |
| 59190 | 59827 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
| 59191 | 59828 | if( pTemp ){ |
| 59192 | 59829 | memcpy(pTemp, pCell, sz); |
| 59193 | 59830 | pCell = pTemp; |
| 59194 | 59831 | } |
| | @@ -59197,36 +59834,46 @@ |
| 59197 | 59834 | } |
| 59198 | 59835 | j = pPage->nOverflow++; |
| 59199 | 59836 | assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) ); |
| 59200 | 59837 | pPage->apOvfl[j] = pCell; |
| 59201 | 59838 | pPage->aiOvfl[j] = (u16)i; |
| 59839 | + |
| 59840 | + /* When multiple overflows occur, they are always sequential and in |
| 59841 | + ** sorted order. This invariants arise because multiple overflows can |
| 59842 | + ** only occur when inserting divider cells into the parent page during |
| 59843 | + ** balancing, and the dividers are adjacent and sorted. |
| 59844 | + */ |
| 59845 | + assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */ |
| 59846 | + assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */ |
| 59202 | 59847 | }else{ |
| 59203 | 59848 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 59204 | 59849 | if( rc!=SQLITE_OK ){ |
| 59205 | 59850 | *pRC = rc; |
| 59206 | 59851 | return; |
| 59207 | 59852 | } |
| 59208 | 59853 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 59209 | 59854 | data = pPage->aData; |
| 59210 | | - cellOffset = pPage->cellOffset; |
| 59211 | | - end = cellOffset + 2*pPage->nCell; |
| 59212 | | - ins = cellOffset + 2*i; |
| 59855 | + assert( &data[pPage->cellOffset]==pPage->aCellIdx ); |
| 59213 | 59856 | rc = allocateSpace(pPage, sz, &idx); |
| 59214 | 59857 | if( rc ){ *pRC = rc; return; } |
| 59215 | | - /* The allocateSpace() routine guarantees the following two properties |
| 59216 | | - ** if it returns success */ |
| 59217 | | - assert( idx >= end+2 ); |
| 59858 | + /* The allocateSpace() routine guarantees the following properties |
| 59859 | + ** if it returns successfully */ |
| 59860 | + assert( idx >= 0 ); |
| 59861 | + assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB ); |
| 59218 | 59862 | assert( idx+sz <= (int)pPage->pBt->usableSize ); |
| 59219 | | - pPage->nCell++; |
| 59220 | 59863 | pPage->nFree -= (u16)(2 + sz); |
| 59221 | 59864 | memcpy(&data[idx], pCell, sz); |
| 59222 | 59865 | if( iChild ){ |
| 59223 | 59866 | put4byte(&data[idx], iChild); |
| 59224 | 59867 | } |
| 59225 | | - memmove(&data[ins+2], &data[ins], end-ins); |
| 59226 | | - put2byte(&data[ins], idx); |
| 59227 | | - put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 59868 | + pIns = pPage->aCellIdx + i*2; |
| 59869 | + memmove(pIns+2, pIns, 2*(pPage->nCell - i)); |
| 59870 | + put2byte(pIns, idx); |
| 59871 | + pPage->nCell++; |
| 59872 | + /* increment the cell count */ |
| 59873 | + if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++; |
| 59874 | + assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell ); |
| 59228 | 59875 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 59229 | 59876 | if( pPage->pBt->autoVacuum ){ |
| 59230 | 59877 | /* The cell may contain a pointer to an overflow page. If so, write |
| 59231 | 59878 | ** the entry for the overflow page into the pointer map. |
| 59232 | 59879 | */ |
| | @@ -59233,10 +59880,56 @@ |
| 59233 | 59880 | ptrmapPutOvflPtr(pPage, pCell, pRC); |
| 59234 | 59881 | } |
| 59235 | 59882 | #endif |
| 59236 | 59883 | } |
| 59237 | 59884 | } |
| 59885 | + |
| 59886 | +/* |
| 59887 | +** A CellArray object contains a cache of pointers and sizes for a |
| 59888 | +** consecutive sequence of cells that might be held multiple pages. |
| 59889 | +*/ |
| 59890 | +typedef struct CellArray CellArray; |
| 59891 | +struct CellArray { |
| 59892 | + int nCell; /* Number of cells in apCell[] */ |
| 59893 | + MemPage *pRef; /* Reference page */ |
| 59894 | + u8 **apCell; /* All cells begin balanced */ |
| 59895 | + u16 *szCell; /* Local size of all cells in apCell[] */ |
| 59896 | +}; |
| 59897 | + |
| 59898 | +/* |
| 59899 | +** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been |
| 59900 | +** computed. |
| 59901 | +*/ |
| 59902 | +static void populateCellCache(CellArray *p, int idx, int N){ |
| 59903 | + assert( idx>=0 && idx+N<=p->nCell ); |
| 59904 | + while( N>0 ){ |
| 59905 | + assert( p->apCell[idx]!=0 ); |
| 59906 | + if( p->szCell[idx]==0 ){ |
| 59907 | + p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]); |
| 59908 | + }else{ |
| 59909 | + assert( CORRUPT_DB || |
| 59910 | + p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) ); |
| 59911 | + } |
| 59912 | + idx++; |
| 59913 | + N--; |
| 59914 | + } |
| 59915 | +} |
| 59916 | + |
| 59917 | +/* |
| 59918 | +** Return the size of the Nth element of the cell array |
| 59919 | +*/ |
| 59920 | +static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){ |
| 59921 | + assert( N>=0 && N<p->nCell ); |
| 59922 | + assert( p->szCell[N]==0 ); |
| 59923 | + p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]); |
| 59924 | + return p->szCell[N]; |
| 59925 | +} |
| 59926 | +static u16 cachedCellSize(CellArray *p, int N){ |
| 59927 | + assert( N>=0 && N<p->nCell ); |
| 59928 | + if( p->szCell[N] ) return p->szCell[N]; |
| 59929 | + return computeCellSize(p, N); |
| 59930 | +} |
| 59238 | 59931 | |
| 59239 | 59932 | /* |
| 59240 | 59933 | ** Array apCell[] contains pointers to nCell b-tree page cells. The |
| 59241 | 59934 | ** szCell[] array contains the size in bytes of each cell. This function |
| 59242 | 59935 | ** replaces the current contents of page pPg with the contents of the cell |
| | @@ -59247,11 +59940,11 @@ |
| 59247 | 59940 | ** such cells before overwriting the page data. |
| 59248 | 59941 | ** |
| 59249 | 59942 | ** The MemPage.nFree field is invalidated by this function. It is the |
| 59250 | 59943 | ** responsibility of the caller to set it correctly. |
| 59251 | 59944 | */ |
| 59252 | | -static void rebuildPage( |
| 59945 | +static int rebuildPage( |
| 59253 | 59946 | MemPage *pPg, /* Edit this page */ |
| 59254 | 59947 | int nCell, /* Final number of cells on page */ |
| 59255 | 59948 | u8 **apCell, /* Array of cells */ |
| 59256 | 59949 | u16 *szCell /* Array of cell sizes */ |
| 59257 | 59950 | ){ |
| | @@ -59272,15 +59965,16 @@ |
| 59272 | 59965 | u8 *pCell = apCell[i]; |
| 59273 | 59966 | if( pCell>aData && pCell<pEnd ){ |
| 59274 | 59967 | pCell = &pTmp[pCell - aData]; |
| 59275 | 59968 | } |
| 59276 | 59969 | pData -= szCell[i]; |
| 59277 | | - memcpy(pData, pCell, szCell[i]); |
| 59278 | 59970 | put2byte(pCellptr, (pData - aData)); |
| 59279 | 59971 | pCellptr += 2; |
| 59280 | | - assert( szCell[i]==cellSizePtr(pPg, pCell) || CORRUPT_DB ); |
| 59281 | | - testcase( szCell[i]==cellSizePtr(pPg,pCell) ); |
| 59972 | + if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT; |
| 59973 | + memcpy(pData, pCell, szCell[i]); |
| 59974 | + assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB ); |
| 59975 | + testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) ); |
| 59282 | 59976 | } |
| 59283 | 59977 | |
| 59284 | 59978 | /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ |
| 59285 | 59979 | pPg->nCell = nCell; |
| 59286 | 59980 | pPg->nOverflow = 0; |
| | @@ -59287,10 +59981,11 @@ |
| 59287 | 59981 | |
| 59288 | 59982 | put2byte(&aData[hdr+1], 0); |
| 59289 | 59983 | put2byte(&aData[hdr+3], pPg->nCell); |
| 59290 | 59984 | put2byte(&aData[hdr+5], pData - aData); |
| 59291 | 59985 | aData[hdr+7] = 0x00; |
| 59986 | + return SQLITE_OK; |
| 59292 | 59987 | } |
| 59293 | 59988 | |
| 59294 | 59989 | /* |
| 59295 | 59990 | ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell |
| 59296 | 59991 | ** contains the size in bytes of each such cell. This function attempts to |
| | @@ -59319,29 +60014,29 @@ |
| 59319 | 60014 | static int pageInsertArray( |
| 59320 | 60015 | MemPage *pPg, /* Page to add cells to */ |
| 59321 | 60016 | u8 *pBegin, /* End of cell-pointer array */ |
| 59322 | 60017 | u8 **ppData, /* IN/OUT: Page content -area pointer */ |
| 59323 | 60018 | u8 *pCellptr, /* Pointer to cell-pointer area */ |
| 60019 | + int iFirst, /* Index of first cell to add */ |
| 59324 | 60020 | int nCell, /* Number of cells to add to pPg */ |
| 59325 | | - u8 **apCell, /* Array of cells */ |
| 59326 | | - u16 *szCell /* Array of cell sizes */ |
| 60021 | + CellArray *pCArray /* Array of cells */ |
| 59327 | 60022 | ){ |
| 59328 | 60023 | int i; |
| 59329 | 60024 | u8 *aData = pPg->aData; |
| 59330 | 60025 | u8 *pData = *ppData; |
| 59331 | | - const int bFreelist = aData[1] || aData[2]; |
| 60026 | + int iEnd = iFirst + nCell; |
| 59332 | 60027 | assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */ |
| 59333 | | - for(i=0; i<nCell; i++){ |
| 59334 | | - int sz = szCell[i]; |
| 59335 | | - int rc; |
| 60028 | + for(i=iFirst; i<iEnd; i++){ |
| 60029 | + int sz, rc; |
| 59336 | 60030 | u8 *pSlot; |
| 59337 | | - if( bFreelist==0 || (pSlot = pageFindSlot(pPg, sz, &rc, 0))==0 ){ |
| 60031 | + sz = cachedCellSize(pCArray, i); |
| 60032 | + if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){ |
| 59338 | 60033 | pData -= sz; |
| 59339 | 60034 | if( pData<pBegin ) return 1; |
| 59340 | 60035 | pSlot = pData; |
| 59341 | 60036 | } |
| 59342 | | - memcpy(pSlot, apCell[i], sz); |
| 60037 | + memcpy(pSlot, pCArray->apCell[i], sz); |
| 59343 | 60038 | put2byte(pCellptr, (pSlot - aData)); |
| 59344 | 60039 | pCellptr += 2; |
| 59345 | 60040 | } |
| 59346 | 60041 | *ppData = pData; |
| 59347 | 60042 | return 0; |
| | @@ -59356,26 +60051,31 @@ |
| 59356 | 60051 | ** |
| 59357 | 60052 | ** This function returns the total number of cells added to the free-list. |
| 59358 | 60053 | */ |
| 59359 | 60054 | static int pageFreeArray( |
| 59360 | 60055 | MemPage *pPg, /* Page to edit */ |
| 60056 | + int iFirst, /* First cell to delete */ |
| 59361 | 60057 | int nCell, /* Cells to delete */ |
| 59362 | | - u8 **apCell, /* Array of cells */ |
| 59363 | | - u16 *szCell /* Array of cell sizes */ |
| 60058 | + CellArray *pCArray /* Array of cells */ |
| 59364 | 60059 | ){ |
| 59365 | 60060 | u8 * const aData = pPg->aData; |
| 59366 | 60061 | u8 * const pEnd = &aData[pPg->pBt->usableSize]; |
| 59367 | 60062 | u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize]; |
| 59368 | 60063 | int nRet = 0; |
| 59369 | 60064 | int i; |
| 60065 | + int iEnd = iFirst + nCell; |
| 59370 | 60066 | u8 *pFree = 0; |
| 59371 | 60067 | int szFree = 0; |
| 59372 | 60068 | |
| 59373 | | - for(i=0; i<nCell; i++){ |
| 59374 | | - u8 *pCell = apCell[i]; |
| 60069 | + for(i=iFirst; i<iEnd; i++){ |
| 60070 | + u8 *pCell = pCArray->apCell[i]; |
| 59375 | 60071 | if( pCell>=pStart && pCell<pEnd ){ |
| 59376 | | - int sz = szCell[i]; |
| 60072 | + int sz; |
| 60073 | + /* No need to use cachedCellSize() here. The sizes of all cells that |
| 60074 | + ** are to be freed have already been computing while deciding which |
| 60075 | + ** cells need freeing */ |
| 60076 | + sz = pCArray->szCell[i]; assert( sz>0 ); |
| 59377 | 60077 | if( pFree!=(pCell + sz) ){ |
| 59378 | 60078 | if( pFree ){ |
| 59379 | 60079 | assert( pFree>aData && (pFree - aData)<65536 ); |
| 59380 | 60080 | freeSpace(pPg, (u16)(pFree - aData), szFree); |
| 59381 | 60081 | } |
| | @@ -59406,17 +60106,16 @@ |
| 59406 | 60106 | ** the correct cells after being balanced. |
| 59407 | 60107 | ** |
| 59408 | 60108 | ** The pPg->nFree field is invalid when this function returns. It is the |
| 59409 | 60109 | ** responsibility of the caller to set it correctly. |
| 59410 | 60110 | */ |
| 59411 | | -static void editPage( |
| 60111 | +static int editPage( |
| 59412 | 60112 | MemPage *pPg, /* Edit this page */ |
| 59413 | 60113 | int iOld, /* Index of first cell currently on page */ |
| 59414 | 60114 | int iNew, /* Index of new first cell on page */ |
| 59415 | 60115 | int nNew, /* Final number of cells on page */ |
| 59416 | | - u8 **apCell, /* Array of cells */ |
| 59417 | | - u16 *szCell /* Array of cell sizes */ |
| 60116 | + CellArray *pCArray /* Array of cells and sizes */ |
| 59418 | 60117 | ){ |
| 59419 | 60118 | u8 * const aData = pPg->aData; |
| 59420 | 60119 | const int hdr = pPg->hdrOffset; |
| 59421 | 60120 | u8 *pBegin = &pPg->aCellIdx[nNew * 2]; |
| 59422 | 60121 | int nCell = pPg->nCell; /* Cells stored on pPg */ |
| | @@ -59431,20 +60130,16 @@ |
| 59431 | 60130 | memcpy(pTmp, aData, pPg->pBt->usableSize); |
| 59432 | 60131 | #endif |
| 59433 | 60132 | |
| 59434 | 60133 | /* Remove cells from the start and end of the page */ |
| 59435 | 60134 | if( iOld<iNew ){ |
| 59436 | | - int nShift = pageFreeArray( |
| 59437 | | - pPg, iNew-iOld, &apCell[iOld], &szCell[iOld] |
| 59438 | | - ); |
| 60135 | + int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray); |
| 59439 | 60136 | memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); |
| 59440 | 60137 | nCell -= nShift; |
| 59441 | 60138 | } |
| 59442 | 60139 | if( iNewEnd < iOldEnd ){ |
| 59443 | | - nCell -= pageFreeArray( |
| 59444 | | - pPg, iOldEnd-iNewEnd, &apCell[iNewEnd], &szCell[iNewEnd] |
| 59445 | | - ); |
| 60140 | + nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray); |
| 59446 | 60141 | } |
| 59447 | 60142 | |
| 59448 | 60143 | pData = &aData[get2byteNotZero(&aData[hdr+5])]; |
| 59449 | 60144 | if( pData<pBegin ) goto editpage_fail; |
| 59450 | 60145 | |
| | @@ -59454,11 +60149,11 @@ |
| 59454 | 60149 | assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB ); |
| 59455 | 60150 | pCellptr = pPg->aCellIdx; |
| 59456 | 60151 | memmove(&pCellptr[nAdd*2], pCellptr, nCell*2); |
| 59457 | 60152 | if( pageInsertArray( |
| 59458 | 60153 | pPg, pBegin, &pData, pCellptr, |
| 59459 | | - nAdd, &apCell[iNew], &szCell[iNew] |
| 60154 | + iNew, nAdd, pCArray |
| 59460 | 60155 | ) ) goto editpage_fail; |
| 59461 | 60156 | nCell += nAdd; |
| 59462 | 60157 | } |
| 59463 | 60158 | |
| 59464 | 60159 | /* Add any overflow cells */ |
| | @@ -59468,20 +60163,20 @@ |
| 59468 | 60163 | pCellptr = &pPg->aCellIdx[iCell * 2]; |
| 59469 | 60164 | memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); |
| 59470 | 60165 | nCell++; |
| 59471 | 60166 | if( pageInsertArray( |
| 59472 | 60167 | pPg, pBegin, &pData, pCellptr, |
| 59473 | | - 1, &apCell[iCell + iNew], &szCell[iCell + iNew] |
| 60168 | + iCell+iNew, 1, pCArray |
| 59474 | 60169 | ) ) goto editpage_fail; |
| 59475 | 60170 | } |
| 59476 | 60171 | } |
| 59477 | 60172 | |
| 59478 | 60173 | /* Append cells to the end of the page */ |
| 59479 | 60174 | pCellptr = &pPg->aCellIdx[nCell*2]; |
| 59480 | 60175 | if( pageInsertArray( |
| 59481 | 60176 | pPg, pBegin, &pData, pCellptr, |
| 59482 | | - nNew-nCell, &apCell[iNew+nCell], &szCell[iNew+nCell] |
| 60177 | + iNew+nCell, nNew-nCell, pCArray |
| 59483 | 60178 | ) ) goto editpage_fail; |
| 59484 | 60179 | |
| 59485 | 60180 | pPg->nCell = nNew; |
| 59486 | 60181 | pPg->nOverflow = 0; |
| 59487 | 60182 | |
| | @@ -59488,23 +60183,25 @@ |
| 59488 | 60183 | put2byte(&aData[hdr+3], pPg->nCell); |
| 59489 | 60184 | put2byte(&aData[hdr+5], pData - aData); |
| 59490 | 60185 | |
| 59491 | 60186 | #ifdef SQLITE_DEBUG |
| 59492 | 60187 | for(i=0; i<nNew && !CORRUPT_DB; i++){ |
| 59493 | | - u8 *pCell = apCell[i+iNew]; |
| 59494 | | - int iOff = get2byte(&pPg->aCellIdx[i*2]); |
| 60188 | + u8 *pCell = pCArray->apCell[i+iNew]; |
| 60189 | + int iOff = get2byteAligned(&pPg->aCellIdx[i*2]); |
| 59495 | 60190 | if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){ |
| 59496 | 60191 | pCell = &pTmp[pCell - aData]; |
| 59497 | 60192 | } |
| 59498 | | - assert( 0==memcmp(pCell, &aData[iOff], szCell[i+iNew]) ); |
| 60193 | + assert( 0==memcmp(pCell, &aData[iOff], |
| 60194 | + pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) ); |
| 59499 | 60195 | } |
| 59500 | 60196 | #endif |
| 59501 | 60197 | |
| 59502 | | - return; |
| 60198 | + return SQLITE_OK; |
| 59503 | 60199 | editpage_fail: |
| 59504 | 60200 | /* Unable to edit this page. Rebuild it from scratch instead. */ |
| 59505 | | - rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]); |
| 60201 | + populateCellCache(pCArray, iNew, nNew); |
| 60202 | + return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]); |
| 59506 | 60203 | } |
| 59507 | 60204 | |
| 59508 | 60205 | /* |
| 59509 | 60206 | ** The following parameters determine how many adjacent pages get involved |
| 59510 | 60207 | ** in a balancing operation. NN is the number of neighbors on either side |
| | @@ -59566,17 +60263,18 @@ |
| 59566 | 60263 | |
| 59567 | 60264 | if( rc==SQLITE_OK ){ |
| 59568 | 60265 | |
| 59569 | 60266 | u8 *pOut = &pSpace[4]; |
| 59570 | 60267 | u8 *pCell = pPage->apOvfl[0]; |
| 59571 | | - u16 szCell = cellSizePtr(pPage, pCell); |
| 60268 | + u16 szCell = pPage->xCellSize(pPage, pCell); |
| 59572 | 60269 | u8 *pStop; |
| 59573 | 60270 | |
| 59574 | 60271 | assert( sqlite3PagerIswriteable(pNew->pDbPage) ); |
| 59575 | 60272 | assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); |
| 59576 | 60273 | zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); |
| 59577 | | - rebuildPage(pNew, 1, &pCell, &szCell); |
| 60274 | + rc = rebuildPage(pNew, 1, &pCell, &szCell); |
| 60275 | + if( NEVER(rc) ) return rc; |
| 59578 | 60276 | pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell; |
| 59579 | 60277 | |
| 59580 | 60278 | /* If this is an auto-vacuum database, update the pointer map |
| 59581 | 60279 | ** with entries for the new page, and any pointer from the |
| 59582 | 60280 | ** cell on the page to an overflow page. If either of these |
| | @@ -59645,11 +60343,11 @@ |
| 59645 | 60343 | for(j=0; j<pPage->nCell; j++){ |
| 59646 | 60344 | CellInfo info; |
| 59647 | 60345 | u8 *z; |
| 59648 | 60346 | |
| 59649 | 60347 | z = findCell(pPage, j); |
| 59650 | | - btreeParseCellPtr(pPage, z, &info); |
| 60348 | + pPage->xParseCell(pPage, z, &info); |
| 59651 | 60349 | if( info.iOverflow ){ |
| 59652 | 60350 | Pgno ovfl = get4byte(&z[info.iOverflow]); |
| 59653 | 60351 | ptrmapGet(pBt, ovfl, &e, &n); |
| 59654 | 60352 | assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 ); |
| 59655 | 60353 | } |
| | @@ -59776,11 +60474,10 @@ |
| 59776 | 60474 | u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */ |
| 59777 | 60475 | int isRoot, /* True if pParent is a root-page */ |
| 59778 | 60476 | int bBulk /* True if this call is part of a bulk load */ |
| 59779 | 60477 | ){ |
| 59780 | 60478 | BtShared *pBt; /* The whole database */ |
| 59781 | | - int nCell = 0; /* Number of cells in apCell[] */ |
| 59782 | 60479 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
| 59783 | 60480 | int nNew = 0; /* Number of pages in apNew[] */ |
| 59784 | 60481 | int nOld; /* Number of pages in apOld[] */ |
| 59785 | 60482 | int i, j, k; /* Loop counters */ |
| 59786 | 60483 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
| | @@ -59787,31 +60484,31 @@ |
| 59787 | 60484 | int rc = SQLITE_OK; /* The return code */ |
| 59788 | 60485 | u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
| 59789 | 60486 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
| 59790 | 60487 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 59791 | 60488 | int pageFlags; /* Value of pPage->aData[0] */ |
| 59792 | | - int subtotal; /* Subtotal of bytes in cells on one page */ |
| 59793 | 60489 | int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
| 59794 | 60490 | int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ |
| 59795 | 60491 | int szScratch; /* Size of scratch memory requested */ |
| 59796 | 60492 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 59797 | 60493 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 59798 | 60494 | u8 *pRight; /* Location in parent of right-sibling pointer */ |
| 59799 | 60495 | u8 *apDiv[NB-1]; /* Divider cells in pParent */ |
| 59800 | | - int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 59801 | | - int cntOld[NB+2]; /* Old index in aCell[] after i-th page */ |
| 60496 | + int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */ |
| 60497 | + int cntOld[NB+2]; /* Old index in b.apCell[] */ |
| 59802 | 60498 | int szNew[NB+2]; /* Combined size of cells placed on i-th page */ |
| 59803 | | - u8 **apCell = 0; /* All cells begin balanced */ |
| 59804 | | - u16 *szCell; /* Local size of all cells in apCell[] */ |
| 59805 | 60499 | u8 *aSpace1; /* Space for copies of dividers cells */ |
| 59806 | 60500 | Pgno pgno; /* Temp var to store a page number in */ |
| 59807 | 60501 | u8 abDone[NB+2]; /* True after i'th new page is populated */ |
| 59808 | 60502 | Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */ |
| 59809 | 60503 | Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */ |
| 59810 | 60504 | u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */ |
| 60505 | + CellArray b; /* Parsed information on cells being balanced */ |
| 59811 | 60506 | |
| 59812 | 60507 | memset(abDone, 0, sizeof(abDone)); |
| 60508 | + b.nCell = 0; |
| 60509 | + b.apCell = 0; |
| 59813 | 60510 | pBt = pParent->pBt; |
| 59814 | 60511 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 59815 | 60512 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 59816 | 60513 | |
| 59817 | 60514 | #if 0 |
| | @@ -59861,11 +60558,11 @@ |
| 59861 | 60558 | }else{ |
| 59862 | 60559 | pRight = findCell(pParent, i+nxDiv-pParent->nOverflow); |
| 59863 | 60560 | } |
| 59864 | 60561 | pgno = get4byte(pRight); |
| 59865 | 60562 | while( 1 ){ |
| 59866 | | - rc = getAndInitPage(pBt, pgno, &apOld[i], 0); |
| 60563 | + rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0); |
| 59867 | 60564 | if( rc ){ |
| 59868 | 60565 | memset(apOld, 0, (i+1)*sizeof(MemPage*)); |
| 59869 | 60566 | goto balance_cleanup; |
| 59870 | 60567 | } |
| 59871 | 60568 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
| | @@ -59872,16 +60569,16 @@ |
| 59872 | 60569 | if( (i--)==0 ) break; |
| 59873 | 60570 | |
| 59874 | 60571 | if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){ |
| 59875 | 60572 | apDiv[i] = pParent->apOvfl[0]; |
| 59876 | 60573 | pgno = get4byte(apDiv[i]); |
| 59877 | | - szNew[i] = cellSizePtr(pParent, apDiv[i]); |
| 60574 | + szNew[i] = pParent->xCellSize(pParent, apDiv[i]); |
| 59878 | 60575 | pParent->nOverflow = 0; |
| 59879 | 60576 | }else{ |
| 59880 | 60577 | apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow); |
| 59881 | 60578 | pgno = get4byte(apDiv[i]); |
| 59882 | | - szNew[i] = cellSizePtr(pParent, apDiv[i]); |
| 60579 | + szNew[i] = pParent->xCellSize(pParent, apDiv[i]); |
| 59883 | 60580 | |
| 59884 | 60581 | /* Drop the cell from the parent page. apDiv[i] still points to |
| 59885 | 60582 | ** the cell within the parent, even though it has been dropped. |
| 59886 | 60583 | ** This is safe because dropping a cell only overwrites the first |
| 59887 | 60584 | ** four bytes of it, and this function does not need the first |
| | @@ -59916,142 +60613,205 @@ |
| 59916 | 60613 | |
| 59917 | 60614 | /* |
| 59918 | 60615 | ** Allocate space for memory structures |
| 59919 | 60616 | */ |
| 59920 | 60617 | szScratch = |
| 59921 | | - nMaxCells*sizeof(u8*) /* apCell */ |
| 59922 | | - + nMaxCells*sizeof(u16) /* szCell */ |
| 60618 | + nMaxCells*sizeof(u8*) /* b.apCell */ |
| 60619 | + + nMaxCells*sizeof(u16) /* b.szCell */ |
| 59923 | 60620 | + pBt->pageSize; /* aSpace1 */ |
| 59924 | 60621 | |
| 59925 | 60622 | /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer |
| 59926 | 60623 | ** that is more than 6 times the database page size. */ |
| 59927 | 60624 | assert( szScratch<=6*(int)pBt->pageSize ); |
| 59928 | | - apCell = sqlite3ScratchMalloc( szScratch ); |
| 59929 | | - if( apCell==0 ){ |
| 60625 | + b.apCell = sqlite3ScratchMalloc( szScratch ); |
| 60626 | + if( b.apCell==0 ){ |
| 59930 | 60627 | rc = SQLITE_NOMEM; |
| 59931 | 60628 | goto balance_cleanup; |
| 59932 | 60629 | } |
| 59933 | | - szCell = (u16*)&apCell[nMaxCells]; |
| 59934 | | - aSpace1 = (u8*)&szCell[nMaxCells]; |
| 60630 | + b.szCell = (u16*)&b.apCell[nMaxCells]; |
| 60631 | + aSpace1 = (u8*)&b.szCell[nMaxCells]; |
| 59935 | 60632 | assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); |
| 59936 | 60633 | |
| 59937 | 60634 | /* |
| 59938 | 60635 | ** Load pointers to all cells on sibling pages and the divider cells |
| 59939 | | - ** into the local apCell[] array. Make copies of the divider cells |
| 60636 | + ** into the local b.apCell[] array. Make copies of the divider cells |
| 59940 | 60637 | ** into space obtained from aSpace1[]. The divider cells have already |
| 59941 | 60638 | ** been removed from pParent. |
| 59942 | 60639 | ** |
| 59943 | 60640 | ** If the siblings are on leaf pages, then the child pointers of the |
| 59944 | 60641 | ** divider cells are stripped from the cells before they are copied |
| 59945 | | - ** into aSpace1[]. In this way, all cells in apCell[] are without |
| 60642 | + ** into aSpace1[]. In this way, all cells in b.apCell[] are without |
| 59946 | 60643 | ** child pointers. If siblings are not leaves, then all cell in |
| 59947 | | - ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 60644 | + ** b.apCell[] include child pointers. Either way, all cells in b.apCell[] |
| 59948 | 60645 | ** are alike. |
| 59949 | 60646 | ** |
| 59950 | 60647 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 59951 | 60648 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
| 59952 | 60649 | */ |
| 59953 | | - leafCorrection = apOld[0]->leaf*4; |
| 59954 | | - leafData = apOld[0]->intKeyLeaf; |
| 60650 | + b.pRef = apOld[0]; |
| 60651 | + leafCorrection = b.pRef->leaf*4; |
| 60652 | + leafData = b.pRef->intKeyLeaf; |
| 59955 | 60653 | for(i=0; i<nOld; i++){ |
| 59956 | | - int limit; |
| 59957 | 60654 | MemPage *pOld = apOld[i]; |
| 60655 | + int limit = pOld->nCell; |
| 60656 | + u8 *aData = pOld->aData; |
| 60657 | + u16 maskPage = pOld->maskPage; |
| 60658 | + u8 *piCell = aData + pOld->cellOffset; |
| 60659 | + u8 *piEnd; |
| 59958 | 60660 | |
| 59959 | 60661 | /* Verify that all sibling pages are of the same "type" (table-leaf, |
| 59960 | 60662 | ** table-interior, index-leaf, or index-interior). |
| 59961 | 60663 | */ |
| 59962 | 60664 | if( pOld->aData[0]!=apOld[0]->aData[0] ){ |
| 59963 | 60665 | rc = SQLITE_CORRUPT_BKPT; |
| 59964 | 60666 | goto balance_cleanup; |
| 59965 | 60667 | } |
| 59966 | 60668 | |
| 59967 | | - limit = pOld->nCell+pOld->nOverflow; |
| 59968 | | - if( pOld->nOverflow>0 ){ |
| 59969 | | - for(j=0; j<limit; j++){ |
| 59970 | | - assert( nCell<nMaxCells ); |
| 59971 | | - apCell[nCell] = findOverflowCell(pOld, j); |
| 59972 | | - szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
| 59973 | | - nCell++; |
| 59974 | | - } |
| 59975 | | - }else{ |
| 59976 | | - u8 *aData = pOld->aData; |
| 59977 | | - u16 maskPage = pOld->maskPage; |
| 59978 | | - u16 cellOffset = pOld->cellOffset; |
| 59979 | | - for(j=0; j<limit; j++){ |
| 59980 | | - assert( nCell<nMaxCells ); |
| 59981 | | - apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j); |
| 59982 | | - szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
| 59983 | | - nCell++; |
| 59984 | | - } |
| 59985 | | - } |
| 59986 | | - cntOld[i] = nCell; |
| 60669 | + /* Load b.apCell[] with pointers to all cells in pOld. If pOld |
| 60670 | + ** constains overflow cells, include them in the b.apCell[] array |
| 60671 | + ** in the correct spot. |
| 60672 | + ** |
| 60673 | + ** Note that when there are multiple overflow cells, it is always the |
| 60674 | + ** case that they are sequential and adjacent. This invariant arises |
| 60675 | + ** because multiple overflows can only occurs when inserting divider |
| 60676 | + ** cells into a parent on a prior balance, and divider cells are always |
| 60677 | + ** adjacent and are inserted in order. There is an assert() tagged |
| 60678 | + ** with "NOTE 1" in the overflow cell insertion loop to prove this |
| 60679 | + ** invariant. |
| 60680 | + ** |
| 60681 | + ** This must be done in advance. Once the balance starts, the cell |
| 60682 | + ** offset section of the btree page will be overwritten and we will no |
| 60683 | + ** long be able to find the cells if a pointer to each cell is not saved |
| 60684 | + ** first. |
| 60685 | + */ |
| 60686 | + memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*limit); |
| 60687 | + if( pOld->nOverflow>0 ){ |
| 60688 | + memset(&b.szCell[b.nCell+limit], 0, sizeof(b.szCell[0])*pOld->nOverflow); |
| 60689 | + limit = pOld->aiOvfl[0]; |
| 60690 | + for(j=0; j<limit; j++){ |
| 60691 | + b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)); |
| 60692 | + piCell += 2; |
| 60693 | + b.nCell++; |
| 60694 | + } |
| 60695 | + for(k=0; k<pOld->nOverflow; k++){ |
| 60696 | + assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */ |
| 60697 | + b.apCell[b.nCell] = pOld->apOvfl[k]; |
| 60698 | + b.nCell++; |
| 60699 | + } |
| 60700 | + } |
| 60701 | + piEnd = aData + pOld->cellOffset + 2*pOld->nCell; |
| 60702 | + while( piCell<piEnd ){ |
| 60703 | + assert( b.nCell<nMaxCells ); |
| 60704 | + b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)); |
| 60705 | + piCell += 2; |
| 60706 | + b.nCell++; |
| 60707 | + } |
| 60708 | + |
| 60709 | + cntOld[i] = b.nCell; |
| 59987 | 60710 | if( i<nOld-1 && !leafData){ |
| 59988 | 60711 | u16 sz = (u16)szNew[i]; |
| 59989 | 60712 | u8 *pTemp; |
| 59990 | | - assert( nCell<nMaxCells ); |
| 59991 | | - szCell[nCell] = sz; |
| 60713 | + assert( b.nCell<nMaxCells ); |
| 60714 | + b.szCell[b.nCell] = sz; |
| 59992 | 60715 | pTemp = &aSpace1[iSpace1]; |
| 59993 | 60716 | iSpace1 += sz; |
| 59994 | 60717 | assert( sz<=pBt->maxLocal+23 ); |
| 59995 | 60718 | assert( iSpace1 <= (int)pBt->pageSize ); |
| 59996 | 60719 | memcpy(pTemp, apDiv[i], sz); |
| 59997 | | - apCell[nCell] = pTemp+leafCorrection; |
| 60720 | + b.apCell[b.nCell] = pTemp+leafCorrection; |
| 59998 | 60721 | assert( leafCorrection==0 || leafCorrection==4 ); |
| 59999 | | - szCell[nCell] = szCell[nCell] - leafCorrection; |
| 60722 | + b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection; |
| 60000 | 60723 | if( !pOld->leaf ){ |
| 60001 | 60724 | assert( leafCorrection==0 ); |
| 60002 | 60725 | assert( pOld->hdrOffset==0 ); |
| 60003 | 60726 | /* The right pointer of the child page pOld becomes the left |
| 60004 | 60727 | ** pointer of the divider cell */ |
| 60005 | | - memcpy(apCell[nCell], &pOld->aData[8], 4); |
| 60728 | + memcpy(b.apCell[b.nCell], &pOld->aData[8], 4); |
| 60006 | 60729 | }else{ |
| 60007 | 60730 | assert( leafCorrection==4 ); |
| 60008 | | - while( szCell[nCell]<4 ){ |
| 60731 | + while( b.szCell[b.nCell]<4 ){ |
| 60009 | 60732 | /* Do not allow any cells smaller than 4 bytes. If a smaller cell |
| 60010 | 60733 | ** does exist, pad it with 0x00 bytes. */ |
| 60011 | | - assert( szCell[nCell]==3 || CORRUPT_DB ); |
| 60012 | | - assert( apCell[nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB ); |
| 60734 | + assert( b.szCell[b.nCell]==3 || CORRUPT_DB ); |
| 60735 | + assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB ); |
| 60013 | 60736 | aSpace1[iSpace1++] = 0x00; |
| 60014 | | - szCell[nCell]++; |
| 60737 | + b.szCell[b.nCell]++; |
| 60015 | 60738 | } |
| 60016 | 60739 | } |
| 60017 | | - nCell++; |
| 60740 | + b.nCell++; |
| 60018 | 60741 | } |
| 60019 | 60742 | } |
| 60020 | 60743 | |
| 60021 | 60744 | /* |
| 60022 | | - ** Figure out the number of pages needed to hold all nCell cells. |
| 60745 | + ** Figure out the number of pages needed to hold all b.nCell cells. |
| 60023 | 60746 | ** Store this number in "k". Also compute szNew[] which is the total |
| 60024 | 60747 | ** size of all cells on the i-th page and cntNew[] which is the index |
| 60025 | | - ** in apCell[] of the cell that divides page i from page i+1. |
| 60026 | | - ** cntNew[k] should equal nCell. |
| 60748 | + ** in b.apCell[] of the cell that divides page i from page i+1. |
| 60749 | + ** cntNew[k] should equal b.nCell. |
| 60027 | 60750 | ** |
| 60028 | 60751 | ** Values computed by this block: |
| 60029 | 60752 | ** |
| 60030 | 60753 | ** k: The total number of sibling pages |
| 60031 | 60754 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 60032 | | - ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 60755 | + ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to |
| 60033 | 60756 | ** the right of the i-th sibling page. |
| 60034 | 60757 | ** usableSpace: Number of bytes of space available on each sibling. |
| 60035 | 60758 | ** |
| 60036 | 60759 | */ |
| 60037 | 60760 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
| 60038 | | - for(subtotal=k=i=0; i<nCell; i++){ |
| 60039 | | - assert( i<nMaxCells ); |
| 60040 | | - subtotal += szCell[i] + 2; |
| 60041 | | - if( subtotal > usableSpace ){ |
| 60042 | | - szNew[k] = subtotal - szCell[i] - 2; |
| 60043 | | - cntNew[k] = i; |
| 60044 | | - if( leafData ){ i--; } |
| 60045 | | - subtotal = 0; |
| 60046 | | - k++; |
| 60047 | | - if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } |
| 60048 | | - } |
| 60049 | | - } |
| 60050 | | - szNew[k] = subtotal; |
| 60051 | | - cntNew[k] = nCell; |
| 60052 | | - k++; |
| 60761 | + for(i=0; i<nOld; i++){ |
| 60762 | + MemPage *p = apOld[i]; |
| 60763 | + szNew[i] = usableSpace - p->nFree; |
| 60764 | + if( szNew[i]<0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } |
| 60765 | + for(j=0; j<p->nOverflow; j++){ |
| 60766 | + szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]); |
| 60767 | + } |
| 60768 | + cntNew[i] = cntOld[i]; |
| 60769 | + } |
| 60770 | + k = nOld; |
| 60771 | + for(i=0; i<k; i++){ |
| 60772 | + int sz; |
| 60773 | + while( szNew[i]>usableSpace ){ |
| 60774 | + if( i+1>=k ){ |
| 60775 | + k = i+2; |
| 60776 | + if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } |
| 60777 | + szNew[k-1] = 0; |
| 60778 | + cntNew[k-1] = b.nCell; |
| 60779 | + } |
| 60780 | + sz = 2 + cachedCellSize(&b, cntNew[i]-1); |
| 60781 | + szNew[i] -= sz; |
| 60782 | + if( !leafData ){ |
| 60783 | + if( cntNew[i]<b.nCell ){ |
| 60784 | + sz = 2 + cachedCellSize(&b, cntNew[i]); |
| 60785 | + }else{ |
| 60786 | + sz = 0; |
| 60787 | + } |
| 60788 | + } |
| 60789 | + szNew[i+1] += sz; |
| 60790 | + cntNew[i]--; |
| 60791 | + } |
| 60792 | + while( cntNew[i]<b.nCell ){ |
| 60793 | + sz = 2 + cachedCellSize(&b, cntNew[i]); |
| 60794 | + if( szNew[i]+sz>usableSpace ) break; |
| 60795 | + szNew[i] += sz; |
| 60796 | + cntNew[i]++; |
| 60797 | + if( !leafData ){ |
| 60798 | + if( cntNew[i]<b.nCell ){ |
| 60799 | + sz = 2 + cachedCellSize(&b, cntNew[i]); |
| 60800 | + }else{ |
| 60801 | + sz = 0; |
| 60802 | + } |
| 60803 | + } |
| 60804 | + szNew[i+1] -= sz; |
| 60805 | + } |
| 60806 | + if( cntNew[i]>=b.nCell ){ |
| 60807 | + k = i+1; |
| 60808 | + }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){ |
| 60809 | + rc = SQLITE_CORRUPT_BKPT; |
| 60810 | + goto balance_cleanup; |
| 60811 | + } |
| 60812 | + } |
| 60053 | 60813 | |
| 60054 | 60814 | /* |
| 60055 | 60815 | ** The packing computed by the previous block is biased toward the siblings |
| 60056 | 60816 | ** on the left side (siblings with smaller keys). The left siblings are |
| 60057 | 60817 | ** always nearly full, while the right-most sibling might be nearly empty. |
| | @@ -60068,23 +60828,31 @@ |
| 60068 | 60828 | int r; /* Index of right-most cell in left sibling */ |
| 60069 | 60829 | int d; /* Index of first cell to the left of right sibling */ |
| 60070 | 60830 | |
| 60071 | 60831 | r = cntNew[i-1] - 1; |
| 60072 | 60832 | d = r + 1 - leafData; |
| 60073 | | - assert( d<nMaxCells ); |
| 60074 | | - assert( r<nMaxCells ); |
| 60075 | | - while( szRight==0 |
| 60076 | | - || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2)) |
| 60077 | | - ){ |
| 60078 | | - szRight += szCell[d] + 2; |
| 60079 | | - szLeft -= szCell[r] + 2; |
| 60080 | | - cntNew[i-1]--; |
| 60081 | | - r = cntNew[i-1] - 1; |
| 60082 | | - d = r + 1 - leafData; |
| 60083 | | - } |
| 60833 | + (void)cachedCellSize(&b, d); |
| 60834 | + do{ |
| 60835 | + assert( d<nMaxCells ); |
| 60836 | + assert( r<nMaxCells ); |
| 60837 | + (void)cachedCellSize(&b, r); |
| 60838 | + if( szRight!=0 |
| 60839 | + && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+2)) ){ |
| 60840 | + break; |
| 60841 | + } |
| 60842 | + szRight += b.szCell[d] + 2; |
| 60843 | + szLeft -= b.szCell[r] + 2; |
| 60844 | + cntNew[i-1] = r; |
| 60845 | + r--; |
| 60846 | + d--; |
| 60847 | + }while( r>=0 ); |
| 60084 | 60848 | szNew[i] = szRight; |
| 60085 | 60849 | szNew[i-1] = szLeft; |
| 60850 | + if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){ |
| 60851 | + rc = SQLITE_CORRUPT_BKPT; |
| 60852 | + goto balance_cleanup; |
| 60853 | + } |
| 60086 | 60854 | } |
| 60087 | 60855 | |
| 60088 | 60856 | /* Sanity check: For a non-corrupt database file one of the follwing |
| 60089 | 60857 | ** must be true: |
| 60090 | 60858 | ** (1) We found one or more cells (cntNew[0])>0), or |
| | @@ -60116,11 +60884,11 @@ |
| 60116 | 60884 | rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0); |
| 60117 | 60885 | if( rc ) goto balance_cleanup; |
| 60118 | 60886 | zeroPage(pNew, pageFlags); |
| 60119 | 60887 | apNew[i] = pNew; |
| 60120 | 60888 | nNew++; |
| 60121 | | - cntOld[i] = nCell; |
| 60889 | + cntOld[i] = b.nCell; |
| 60122 | 60890 | |
| 60123 | 60891 | /* Set the pointer-map entry for the new sibling page. */ |
| 60124 | 60892 | if( ISAUTOVACUUM ){ |
| 60125 | 60893 | ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc); |
| 60126 | 60894 | if( rc!=SQLITE_OK ){ |
| | @@ -60221,12 +60989,12 @@ |
| 60221 | 60989 | int cntOldNext = pNew->nCell + pNew->nOverflow; |
| 60222 | 60990 | int usableSize = pBt->usableSize; |
| 60223 | 60991 | int iNew = 0; |
| 60224 | 60992 | int iOld = 0; |
| 60225 | 60993 | |
| 60226 | | - for(i=0; i<nCell; i++){ |
| 60227 | | - u8 *pCell = apCell[i]; |
| 60994 | + for(i=0; i<b.nCell; i++){ |
| 60995 | + u8 *pCell = b.apCell[i]; |
| 60228 | 60996 | if( i==cntOldNext ){ |
| 60229 | 60997 | MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld]; |
| 60230 | 60998 | cntOldNext += pOld->nCell + pOld->nOverflow + !leafData; |
| 60231 | 60999 | aOld = pOld->aData; |
| 60232 | 61000 | } |
| | @@ -60247,13 +61015,14 @@ |
| 60247 | 61015 | || pCell>=&aOld[usableSize] |
| 60248 | 61016 | ){ |
| 60249 | 61017 | if( !leafCorrection ){ |
| 60250 | 61018 | ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); |
| 60251 | 61019 | } |
| 60252 | | - if( szCell[i]>pNew->minLocal ){ |
| 61020 | + if( cachedCellSize(&b,i)>pNew->minLocal ){ |
| 60253 | 61021 | ptrmapPutOvflPtr(pNew, pCell, &rc); |
| 60254 | 61022 | } |
| 61023 | + if( rc ) goto balance_cleanup; |
| 60255 | 61024 | } |
| 60256 | 61025 | } |
| 60257 | 61026 | } |
| 60258 | 61027 | |
| 60259 | 61028 | /* Insert new divider cells into pParent. */ |
| | @@ -60263,24 +61032,25 @@ |
| 60263 | 61032 | int sz; |
| 60264 | 61033 | MemPage *pNew = apNew[i]; |
| 60265 | 61034 | j = cntNew[i]; |
| 60266 | 61035 | |
| 60267 | 61036 | assert( j<nMaxCells ); |
| 60268 | | - pCell = apCell[j]; |
| 60269 | | - sz = szCell[j] + leafCorrection; |
| 61037 | + assert( b.apCell[j]!=0 ); |
| 61038 | + pCell = b.apCell[j]; |
| 61039 | + sz = b.szCell[j] + leafCorrection; |
| 60270 | 61040 | pTemp = &aOvflSpace[iOvflSpace]; |
| 60271 | 61041 | if( !pNew->leaf ){ |
| 60272 | 61042 | memcpy(&pNew->aData[8], pCell, 4); |
| 60273 | 61043 | }else if( leafData ){ |
| 60274 | 61044 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
| 60275 | | - ** then there is no divider cell in apCell[]. Instead, the divider |
| 61045 | + ** then there is no divider cell in b.apCell[]. Instead, the divider |
| 60276 | 61046 | ** cell consists of the integer key for the right-most cell of |
| 60277 | 61047 | ** the sibling-page assembled above only. |
| 60278 | 61048 | */ |
| 60279 | 61049 | CellInfo info; |
| 60280 | 61050 | j--; |
| 60281 | | - btreeParseCellPtr(pNew, apCell[j], &info); |
| 61051 | + pNew->xParseCell(pNew, b.apCell[j], &info); |
| 60282 | 61052 | pCell = pTemp; |
| 60283 | 61053 | sz = 4 + putVarint(&pCell[4], info.nKey); |
| 60284 | 61054 | pTemp = 0; |
| 60285 | 61055 | }else{ |
| 60286 | 61056 | pCell -= 4; |
| | @@ -60293,13 +61063,13 @@ |
| 60293 | 61063 | ** |
| 60294 | 61064 | ** Note that this can never happen in an SQLite data file, as all |
| 60295 | 61065 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 60296 | 61066 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 60297 | 61067 | */ |
| 60298 | | - if( szCell[j]==4 ){ |
| 61068 | + if( b.szCell[j]==4 ){ |
| 60299 | 61069 | assert(leafCorrection==4); |
| 60300 | | - sz = cellSizePtr(pParent, pCell); |
| 61070 | + sz = pParent->xCellSize(pParent, pCell); |
| 60301 | 61071 | } |
| 60302 | 61072 | } |
| 60303 | 61073 | iOvflSpace += sz; |
| 60304 | 61074 | assert( sz<=pBt->maxLocal+23 ); |
| 60305 | 61075 | assert( iOvflSpace <= (int)pBt->pageSize ); |
| | @@ -60351,16 +61121,17 @@ |
| 60351 | 61121 | |
| 60352 | 61122 | if( iPg==0 ){ |
| 60353 | 61123 | iNew = iOld = 0; |
| 60354 | 61124 | nNewCell = cntNew[0]; |
| 60355 | 61125 | }else{ |
| 60356 | | - iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : nCell; |
| 61126 | + iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell; |
| 60357 | 61127 | iNew = cntNew[iPg-1] + !leafData; |
| 60358 | 61128 | nNewCell = cntNew[iPg] - iNew; |
| 60359 | 61129 | } |
| 60360 | 61130 | |
| 60361 | | - editPage(apNew[iPg], iOld, iNew, nNewCell, apCell, szCell); |
| 61131 | + rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b); |
| 61132 | + if( rc ) goto balance_cleanup; |
| 60362 | 61133 | abDone[iPg]++; |
| 60363 | 61134 | apNew[iPg]->nFree = usableSpace-szNew[iPg]; |
| 60364 | 61135 | assert( apNew[iPg]->nOverflow==0 ); |
| 60365 | 61136 | assert( apNew[iPg]->nCell==nNewCell ); |
| 60366 | 61137 | } |
| | @@ -60407,11 +61178,11 @@ |
| 60407 | 61178 | } |
| 60408 | 61179 | } |
| 60409 | 61180 | |
| 60410 | 61181 | assert( pParent->isInit ); |
| 60411 | 61182 | TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", |
| 60412 | | - nOld, nNew, nCell)); |
| 61183 | + nOld, nNew, b.nCell)); |
| 60413 | 61184 | |
| 60414 | 61185 | /* Free any old pages that were not reused as new pages. |
| 60415 | 61186 | */ |
| 60416 | 61187 | for(i=nNew; i<nOld; i++){ |
| 60417 | 61188 | freePage(apOld[i], &rc); |
| | @@ -60430,11 +61201,11 @@ |
| 60430 | 61201 | |
| 60431 | 61202 | /* |
| 60432 | 61203 | ** Cleanup before returning. |
| 60433 | 61204 | */ |
| 60434 | 61205 | balance_cleanup: |
| 60435 | | - sqlite3ScratchFree(apCell); |
| 61206 | + sqlite3ScratchFree(b.apCell); |
| 60436 | 61207 | for(i=0; i<nOld; i++){ |
| 60437 | 61208 | releasePage(apOld[i]); |
| 60438 | 61209 | } |
| 60439 | 61210 | for(i=0; i<nNew; i++){ |
| 60440 | 61211 | releasePage(apNew[i]); |
| | @@ -60705,28 +61476,32 @@ |
| 60705 | 61476 | ** data into the intkey B-Tree. In this case btreeMoveto() recognizes |
| 60706 | 61477 | ** that the cursor is already where it needs to be and returns without |
| 60707 | 61478 | ** doing any work. To avoid thwarting these optimizations, it is important |
| 60708 | 61479 | ** not to clear the cursor here. |
| 60709 | 61480 | */ |
| 60710 | | - rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); |
| 60711 | | - if( rc ) return rc; |
| 61481 | + if( pCur->curFlags & BTCF_Multiple ){ |
| 61482 | + rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); |
| 61483 | + if( rc ) return rc; |
| 61484 | + } |
| 60712 | 61485 | |
| 60713 | 61486 | if( pCur->pKeyInfo==0 ){ |
| 61487 | + assert( pKey==0 ); |
| 60714 | 61488 | /* If this is an insert into a table b-tree, invalidate any incrblob |
| 60715 | 61489 | ** cursors open on the row being replaced */ |
| 60716 | 61490 | invalidateIncrblobCursors(p, nKey, 0); |
| 60717 | 61491 | |
| 60718 | 61492 | /* If the cursor is currently on the last row and we are appending a |
| 60719 | | - ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto() |
| 60720 | | - ** call */ |
| 61493 | + ** new row onto the end, set the "loc" to avoid an unnecessary |
| 61494 | + ** btreeMoveto() call */ |
| 60721 | 61495 | if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0 |
| 60722 | 61496 | && pCur->info.nKey==nKey-1 ){ |
| 60723 | | - loc = -1; |
| 61497 | + loc = -1; |
| 61498 | + }else if( loc==0 ){ |
| 61499 | + rc = sqlite3BtreeMovetoUnpacked(pCur, 0, nKey, appendBias, &loc); |
| 61500 | + if( rc ) return rc; |
| 60724 | 61501 | } |
| 60725 | | - } |
| 60726 | | - |
| 60727 | | - if( !loc ){ |
| 61502 | + }else if( loc==0 ){ |
| 60728 | 61503 | rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc); |
| 60729 | 61504 | if( rc ) return rc; |
| 60730 | 61505 | } |
| 60731 | 61506 | assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) ); |
| 60732 | 61507 | |
| | @@ -60740,11 +61515,11 @@ |
| 60740 | 61515 | assert( pPage->isInit ); |
| 60741 | 61516 | newCell = pBt->pTmpSpace; |
| 60742 | 61517 | assert( newCell!=0 ); |
| 60743 | 61518 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
| 60744 | 61519 | if( rc ) goto end_insert; |
| 60745 | | - assert( szNew==cellSizePtr(pPage, newCell) ); |
| 61520 | + assert( szNew==pPage->xCellSize(pPage, newCell) ); |
| 60746 | 61521 | assert( szNew <= MX_CELL_SIZE(pBt) ); |
| 60747 | 61522 | idx = pCur->aiIdx[pCur->iPage]; |
| 60748 | 61523 | if( loc==0 ){ |
| 60749 | 61524 | u16 szOld; |
| 60750 | 61525 | assert( idx<pPage->nCell ); |
| | @@ -60824,16 +61599,12 @@ |
| 60824 | 61599 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 60825 | 61600 | assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
| 60826 | 61601 | assert( pCur->curFlags & BTCF_WriteFlag ); |
| 60827 | 61602 | assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); |
| 60828 | 61603 | assert( !hasReadConflicts(p, pCur->pgnoRoot) ); |
| 60829 | | - |
| 60830 | | - if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) |
| 60831 | | - || NEVER(pCur->eState!=CURSOR_VALID) |
| 60832 | | - ){ |
| 60833 | | - return SQLITE_ERROR; /* Something has gone awry. */ |
| 60834 | | - } |
| 61604 | + assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
| 61605 | + assert( pCur->eState==CURSOR_VALID ); |
| 60835 | 61606 | |
| 60836 | 61607 | iCellDepth = pCur->iPage; |
| 60837 | 61608 | iCellIdx = pCur->aiIdx[iCellDepth]; |
| 60838 | 61609 | pPage = pCur->apPage[iCellDepth]; |
| 60839 | 61610 | pCell = findCell(pPage, iCellIdx); |
| | @@ -60854,12 +61625,14 @@ |
| 60854 | 61625 | /* Save the positions of any other cursors open on this table before |
| 60855 | 61626 | ** making any modifications. Make the page containing the entry to be |
| 60856 | 61627 | ** deleted writable. Then free any overflow pages associated with the |
| 60857 | 61628 | ** entry and finally remove the cell itself from within the page. |
| 60858 | 61629 | */ |
| 60859 | | - rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); |
| 60860 | | - if( rc ) return rc; |
| 61630 | + if( pCur->curFlags & BTCF_Multiple ){ |
| 61631 | + rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); |
| 61632 | + if( rc ) return rc; |
| 61633 | + } |
| 60861 | 61634 | |
| 60862 | 61635 | /* If this is a delete operation to remove a row from a table b-tree, |
| 60863 | 61636 | ** invalidate any incrblob cursors open on the row being deleted. */ |
| 60864 | 61637 | if( pCur->pKeyInfo==0 ){ |
| 60865 | 61638 | invalidateIncrblobCursors(p, pCur->info.nKey, 0); |
| | @@ -60882,11 +61655,11 @@ |
| 60882 | 61655 | Pgno n = pCur->apPage[iCellDepth+1]->pgno; |
| 60883 | 61656 | unsigned char *pTmp; |
| 60884 | 61657 | |
| 60885 | 61658 | pCell = findCell(pLeaf, pLeaf->nCell-1); |
| 60886 | 61659 | if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT; |
| 60887 | | - nCell = cellSizePtr(pLeaf, pCell); |
| 61660 | + nCell = pLeaf->xCellSize(pLeaf, pCell); |
| 60888 | 61661 | assert( MX_CELL_SIZE(pBt) >= nCell ); |
| 60889 | 61662 | pTmp = pBt->pTmpSpace; |
| 60890 | 61663 | assert( pTmp!=0 ); |
| 60891 | 61664 | rc = sqlite3PagerWrite(pLeaf->pDbPage); |
| 60892 | 61665 | insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc); |
| | @@ -61104,11 +61877,11 @@ |
| 61104 | 61877 | |
| 61105 | 61878 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 61106 | 61879 | if( pgno>btreePagecount(pBt) ){ |
| 61107 | 61880 | return SQLITE_CORRUPT_BKPT; |
| 61108 | 61881 | } |
| 61109 | | - rc = getAndInitPage(pBt, pgno, &pPage, 0); |
| 61882 | + rc = getAndInitPage(pBt, pgno, &pPage, 0, 0); |
| 61110 | 61883 | if( rc ) return rc; |
| 61111 | 61884 | if( pPage->bBusy ){ |
| 61112 | 61885 | rc = SQLITE_CORRUPT_BKPT; |
| 61113 | 61886 | goto cleardatabasepage_out; |
| 61114 | 61887 | } |
| | @@ -61776,11 +62549,11 @@ |
| 61776 | 62549 | */ |
| 61777 | 62550 | pCheck->zPfx = "On tree page %d cell %d: "; |
| 61778 | 62551 | pCheck->v1 = iPage; |
| 61779 | 62552 | pCheck->v2 = i; |
| 61780 | 62553 | pCell = findCell(pPage,i); |
| 61781 | | - btreeParseCellPtr(pPage, pCell, &info); |
| 62554 | + pPage->xParseCell(pPage, pCell, &info); |
| 61782 | 62555 | sz = info.nPayload; |
| 61783 | 62556 | /* For intKey pages, check that the keys are in order. |
| 61784 | 62557 | */ |
| 61785 | 62558 | if( pPage->intKey ){ |
| 61786 | 62559 | if( i==0 ){ |
| | @@ -61891,14 +62664,14 @@ |
| 61891 | 62664 | ** immediately follows the b-tree page header. */ |
| 61892 | 62665 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 61893 | 62666 | /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte |
| 61894 | 62667 | ** integer offsets to the cell contents. */ |
| 61895 | 62668 | for(i=0; i<nCell; i++){ |
| 61896 | | - int pc = get2byte(&data[cellStart+i*2]); |
| 62669 | + int pc = get2byteAligned(&data[cellStart+i*2]); |
| 61897 | 62670 | u32 size = 65536; |
| 61898 | 62671 | if( pc<=usableSize-4 ){ |
| 61899 | | - size = cellSizePtr(pPage, &data[pc]); |
| 62672 | + size = pPage->xCellSize(pPage, &data[pc]); |
| 61900 | 62673 | } |
| 61901 | 62674 | if( (int)(pc+size-1)>=usableSize ){ |
| 61902 | 62675 | pCheck->zPfx = 0; |
| 61903 | 62676 | checkAppendMsg(pCheck, |
| 61904 | 62677 | "Corruption detected in cell %d on page %d",i,iPage); |
| | @@ -62292,10 +63065,11 @@ |
| 62292 | 63065 | /* |
| 62293 | 63066 | ** Mark this cursor as an incremental blob cursor. |
| 62294 | 63067 | */ |
| 62295 | 63068 | SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){ |
| 62296 | 63069 | pCur->curFlags |= BTCF_Incrblob; |
| 63070 | + pCur->pBtree->hasIncrblobCur = 1; |
| 62297 | 63071 | } |
| 62298 | 63072 | #endif |
| 62299 | 63073 | |
| 62300 | 63074 | /* |
| 62301 | 63075 | ** Set both the "read version" (single byte at byte offset 18) and |
| | @@ -63739,11 +64513,11 @@ |
| 63739 | 64513 | ** used (for example) to implement the SQL "cast()" operator. |
| 63740 | 64514 | */ |
| 63741 | 64515 | SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){ |
| 63742 | 64516 | if( pMem->flags & MEM_Null ) return; |
| 63743 | 64517 | switch( aff ){ |
| 63744 | | - case SQLITE_AFF_NONE: { /* Really a cast to BLOB */ |
| 64518 | + case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */ |
| 63745 | 64519 | if( (pMem->flags & MEM_Blob)==0 ){ |
| 63746 | 64520 | sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding); |
| 63747 | 64521 | assert( pMem->flags & MEM_Str || pMem->db->mallocFailed ); |
| 63748 | 64522 | MemSetTypeFlag(pMem, MEM_Blob); |
| 63749 | 64523 | }else{ |
| | @@ -63928,14 +64702,19 @@ |
| 63928 | 64702 | ** Make an shallow copy of pFrom into pTo. Prior contents of |
| 63929 | 64703 | ** pTo are freed. The pFrom->z field is not duplicated. If |
| 63930 | 64704 | ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z |
| 63931 | 64705 | ** and flags gets srcType (either MEM_Ephem or MEM_Static). |
| 63932 | 64706 | */ |
| 64707 | +static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){ |
| 64708 | + vdbeMemClearExternAndSetNull(pTo); |
| 64709 | + assert( !VdbeMemDynamic(pTo) ); |
| 64710 | + sqlite3VdbeMemShallowCopy(pTo, pFrom, eType); |
| 64711 | +} |
| 63933 | 64712 | SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){ |
| 63934 | 64713 | assert( (pFrom->flags & MEM_RowSet)==0 ); |
| 63935 | 64714 | assert( pTo->db==pFrom->db ); |
| 63936 | | - if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo); |
| 64715 | + if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; } |
| 63937 | 64716 | memcpy(pTo, pFrom, MEMCELLSIZE); |
| 63938 | 64717 | if( (pFrom->flags&MEM_Static)==0 ){ |
| 63939 | 64718 | pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem); |
| 63940 | 64719 | assert( srcType==MEM_Ephem || srcType==MEM_Static ); |
| 63941 | 64720 | pTo->flags |= srcType; |
| | @@ -64097,10 +64876,36 @@ |
| 64097 | 64876 | ** destroyed. |
| 64098 | 64877 | ** |
| 64099 | 64878 | ** If this routine fails for any reason (malloc returns NULL or unable |
| 64100 | 64879 | ** to read from the disk) then the pMem is left in an inconsistent state. |
| 64101 | 64880 | */ |
| 64881 | +static SQLITE_NOINLINE int vdbeMemFromBtreeResize( |
| 64882 | + BtCursor *pCur, /* Cursor pointing at record to retrieve. */ |
| 64883 | + u32 offset, /* Offset from the start of data to return bytes from. */ |
| 64884 | + u32 amt, /* Number of bytes to return. */ |
| 64885 | + int key, /* If true, retrieve from the btree key, not data. */ |
| 64886 | + Mem *pMem /* OUT: Return data in this Mem structure. */ |
| 64887 | +){ |
| 64888 | + int rc; |
| 64889 | + pMem->flags = MEM_Null; |
| 64890 | + if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){ |
| 64891 | + if( key ){ |
| 64892 | + rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z); |
| 64893 | + }else{ |
| 64894 | + rc = sqlite3BtreeData(pCur, offset, amt, pMem->z); |
| 64895 | + } |
| 64896 | + if( rc==SQLITE_OK ){ |
| 64897 | + pMem->z[amt] = 0; |
| 64898 | + pMem->z[amt+1] = 0; |
| 64899 | + pMem->flags = MEM_Blob|MEM_Term; |
| 64900 | + pMem->n = (int)amt; |
| 64901 | + }else{ |
| 64902 | + sqlite3VdbeMemRelease(pMem); |
| 64903 | + } |
| 64904 | + } |
| 64905 | + return rc; |
| 64906 | +} |
| 64102 | 64907 | SQLITE_PRIVATE int sqlite3VdbeMemFromBtree( |
| 64103 | 64908 | BtCursor *pCur, /* Cursor pointing at record to retrieve. */ |
| 64104 | 64909 | u32 offset, /* Offset from the start of data to return bytes from. */ |
| 64105 | 64910 | u32 amt, /* Number of bytes to return. */ |
| 64106 | 64911 | int key, /* If true, retrieve from the btree key, not data. */ |
| | @@ -64126,26 +64931,11 @@ |
| 64126 | 64931 | if( offset+amt<=available ){ |
| 64127 | 64932 | pMem->z = &zData[offset]; |
| 64128 | 64933 | pMem->flags = MEM_Blob|MEM_Ephem; |
| 64129 | 64934 | pMem->n = (int)amt; |
| 64130 | 64935 | }else{ |
| 64131 | | - pMem->flags = MEM_Null; |
| 64132 | | - if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){ |
| 64133 | | - if( key ){ |
| 64134 | | - rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z); |
| 64135 | | - }else{ |
| 64136 | | - rc = sqlite3BtreeData(pCur, offset, amt, pMem->z); |
| 64137 | | - } |
| 64138 | | - if( rc==SQLITE_OK ){ |
| 64139 | | - pMem->z[amt] = 0; |
| 64140 | | - pMem->z[amt+1] = 0; |
| 64141 | | - pMem->flags = MEM_Blob|MEM_Term; |
| 64142 | | - pMem->n = (int)amt; |
| 64143 | | - }else{ |
| 64144 | | - sqlite3VdbeMemRelease(pMem); |
| 64145 | | - } |
| 64146 | | - } |
| 64936 | + rc = vdbeMemFromBtreeResize(pCur, offset, amt, key, pMem); |
| 64147 | 64937 | } |
| 64148 | 64938 | |
| 64149 | 64939 | return rc; |
| 64150 | 64940 | } |
| 64151 | 64941 | |
| | @@ -64462,11 +65252,11 @@ |
| 64462 | 65252 | }else{ |
| 64463 | 65253 | zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken); |
| 64464 | 65254 | if( zVal==0 ) goto no_mem; |
| 64465 | 65255 | sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC); |
| 64466 | 65256 | } |
| 64467 | | - if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){ |
| 65257 | + if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){ |
| 64468 | 65258 | sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8); |
| 64469 | 65259 | }else{ |
| 64470 | 65260 | sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8); |
| 64471 | 65261 | } |
| 64472 | 65262 | if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str; |
| | @@ -64829,23 +65619,32 @@ |
| 64829 | 65619 | sqlite3VdbeMemRelease((Mem *)v); |
| 64830 | 65620 | sqlite3DbFree(((Mem*)v)->db, v); |
| 64831 | 65621 | } |
| 64832 | 65622 | |
| 64833 | 65623 | /* |
| 64834 | | -** Return the number of bytes in the sqlite3_value object assuming |
| 64835 | | -** that it uses the encoding "enc" |
| 65624 | +** The sqlite3ValueBytes() routine returns the number of bytes in the |
| 65625 | +** sqlite3_value object assuming that it uses the encoding "enc". |
| 65626 | +** The valueBytes() routine is a helper function. |
| 64836 | 65627 | */ |
| 65628 | +static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){ |
| 65629 | + return valueToText(pVal, enc)!=0 ? pVal->n : 0; |
| 65630 | +} |
| 64837 | 65631 | SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){ |
| 64838 | 65632 | Mem *p = (Mem*)pVal; |
| 64839 | | - if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){ |
| 65633 | + assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 ); |
| 65634 | + if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){ |
| 65635 | + return p->n; |
| 65636 | + } |
| 65637 | + if( (p->flags & MEM_Blob)!=0 ){ |
| 64840 | 65638 | if( p->flags & MEM_Zero ){ |
| 64841 | 65639 | return p->n + p->u.nZero; |
| 64842 | 65640 | }else{ |
| 64843 | 65641 | return p->n; |
| 64844 | 65642 | } |
| 64845 | 65643 | } |
| 64846 | | - return 0; |
| 65644 | + if( p->flags & MEM_Null ) return 0; |
| 65645 | + return valueBytes(pVal, enc); |
| 64847 | 65646 | } |
| 64848 | 65647 | |
| 64849 | 65648 | /************** End of vdbemem.c *********************************************/ |
| 64850 | 65649 | /************** Begin file vdbeaux.c *****************************************/ |
| 64851 | 65650 | /* |
| | @@ -65078,10 +65877,27 @@ |
| 65078 | 65877 | ){ |
| 65079 | 65878 | int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); |
| 65080 | 65879 | sqlite3VdbeChangeP4(p, addr, zP4, p4type); |
| 65081 | 65880 | return addr; |
| 65082 | 65881 | } |
| 65882 | + |
| 65883 | +/* |
| 65884 | +** Add an opcode that includes the p4 value with a P4_INT64 type. |
| 65885 | +*/ |
| 65886 | +SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8( |
| 65887 | + Vdbe *p, /* Add the opcode to this VM */ |
| 65888 | + int op, /* The new opcode */ |
| 65889 | + int p1, /* The P1 operand */ |
| 65890 | + int p2, /* The P2 operand */ |
| 65891 | + int p3, /* The P3 operand */ |
| 65892 | + const u8 *zP4, /* The P4 operand */ |
| 65893 | + int p4type /* P4 operand type */ |
| 65894 | +){ |
| 65895 | + char *p4copy = sqlite3DbMallocRaw(sqlite3VdbeDb(p), 8); |
| 65896 | + if( p4copy ) memcpy(p4copy, zP4, 8); |
| 65897 | + return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type); |
| 65898 | +} |
| 65083 | 65899 | |
| 65084 | 65900 | /* |
| 65085 | 65901 | ** Add an OP_ParseSchema opcode. This routine is broken out from |
| 65086 | 65902 | ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees |
| 65087 | 65903 | ** as having been used. |
| | @@ -65243,10 +66059,11 @@ |
| 65243 | 66059 | ** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort. |
| 65244 | 66060 | ** * OP_Destroy |
| 65245 | 66061 | ** * OP_VUpdate |
| 65246 | 66062 | ** * OP_VRename |
| 65247 | 66063 | ** * OP_FkCounter with P2==0 (immediate foreign key constraint) |
| 66064 | +** * OP_CreateTable and OP_InitCoroutine (for CREATE TABLE AS SELECT ...) |
| 65248 | 66065 | ** |
| 65249 | 66066 | ** Then check that the value of Parse.mayAbort is true if an |
| 65250 | 66067 | ** ABORT may be thrown, or false otherwise. Return true if it does |
| 65251 | 66068 | ** match, or false otherwise. This function is intended to be used as |
| 65252 | 66069 | ** part of an assert statement in the compiler. Similar to: |
| | @@ -65254,10 +66071,12 @@ |
| 65254 | 66071 | ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) ); |
| 65255 | 66072 | */ |
| 65256 | 66073 | SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){ |
| 65257 | 66074 | int hasAbort = 0; |
| 65258 | 66075 | int hasFkCounter = 0; |
| 66076 | + int hasCreateTable = 0; |
| 66077 | + int hasInitCoroutine = 0; |
| 65259 | 66078 | Op *pOp; |
| 65260 | 66079 | VdbeOpIter sIter; |
| 65261 | 66080 | memset(&sIter, 0, sizeof(sIter)); |
| 65262 | 66081 | sIter.v = v; |
| 65263 | 66082 | |
| | @@ -65268,10 +66087,12 @@ |
| 65268 | 66087 | && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort)) |
| 65269 | 66088 | ){ |
| 65270 | 66089 | hasAbort = 1; |
| 65271 | 66090 | break; |
| 65272 | 66091 | } |
| 66092 | + if( opcode==OP_CreateTable ) hasCreateTable = 1; |
| 66093 | + if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1; |
| 65273 | 66094 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 65274 | 66095 | if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){ |
| 65275 | 66096 | hasFkCounter = 1; |
| 65276 | 66097 | } |
| 65277 | 66098 | #endif |
| | @@ -65281,11 +66102,12 @@ |
| 65281 | 66102 | /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred. |
| 65282 | 66103 | ** If malloc failed, then the while() loop above may not have iterated |
| 65283 | 66104 | ** through all opcodes and hasAbort may be set incorrectly. Return |
| 65284 | 66105 | ** true for this case to prevent the assert() in the callers frame |
| 65285 | 66106 | ** from failing. */ |
| 65286 | | - return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter ); |
| 66107 | + return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter |
| 66108 | + || (hasCreateTable && hasInitCoroutine) ); |
| 65287 | 66109 | } |
| 65288 | 66110 | #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */ |
| 65289 | 66111 | |
| 65290 | 66112 | /* |
| 65291 | 66113 | ** Loop through the program looking for P2 values that are negative |
| | @@ -65312,15 +66134,10 @@ |
| 65312 | 66134 | u8 opcode = pOp->opcode; |
| 65313 | 66135 | |
| 65314 | 66136 | /* NOTE: Be sure to update mkopcodeh.awk when adding or removing |
| 65315 | 66137 | ** cases from this switch! */ |
| 65316 | 66138 | switch( opcode ){ |
| 65317 | | - case OP_Function: |
| 65318 | | - case OP_AggStep: { |
| 65319 | | - if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5; |
| 65320 | | - break; |
| 65321 | | - } |
| 65322 | 66139 | case OP_Transaction: { |
| 65323 | 66140 | if( pOp->p2!=0 ) p->readOnly = 0; |
| 65324 | 66141 | /* fall thru */ |
| 65325 | 66142 | } |
| 65326 | 66143 | case OP_AutoCommit: |
| | @@ -65560,10 +66377,14 @@ |
| 65560 | 66377 | */ |
| 65561 | 66378 | static void freeP4(sqlite3 *db, int p4type, void *p4){ |
| 65562 | 66379 | if( p4 ){ |
| 65563 | 66380 | assert( db ); |
| 65564 | 66381 | switch( p4type ){ |
| 66382 | + case P4_FUNCCTX: { |
| 66383 | + freeEphemeralFunction(db, ((sqlite3_context*)p4)->pFunc); |
| 66384 | + /* Fall through into the next case */ |
| 66385 | + } |
| 65565 | 66386 | case P4_REAL: |
| 65566 | 66387 | case P4_INT64: |
| 65567 | 66388 | case P4_DYNAMIC: |
| 65568 | 66389 | case P4_INTARRAY: { |
| 65569 | 66390 | sqlite3DbFree(db, p4); |
| | @@ -65944,10 +66765,17 @@ |
| 65944 | 66765 | case P4_FUNCDEF: { |
| 65945 | 66766 | FuncDef *pDef = pOp->p4.pFunc; |
| 65946 | 66767 | sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); |
| 65947 | 66768 | break; |
| 65948 | 66769 | } |
| 66770 | +#ifdef SQLITE_DEBUG |
| 66771 | + case P4_FUNCCTX: { |
| 66772 | + FuncDef *pDef = pOp->p4.pCtx->pFunc; |
| 66773 | + sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); |
| 66774 | + break; |
| 66775 | + } |
| 66776 | +#endif |
| 65949 | 66777 | case P4_INT64: { |
| 65950 | 66778 | sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64); |
| 65951 | 66779 | break; |
| 65952 | 66780 | } |
| 65953 | 66781 | case P4_INT32: { |
| | @@ -66064,24 +66892,27 @@ |
| 66064 | 66892 | |
| 66065 | 66893 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 |
| 66066 | 66894 | /* |
| 66067 | 66895 | ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter(). |
| 66068 | 66896 | */ |
| 66069 | | -SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){ |
| 66897 | +static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){ |
| 66070 | 66898 | int i; |
| 66071 | 66899 | sqlite3 *db; |
| 66072 | 66900 | Db *aDb; |
| 66073 | 66901 | int nDb; |
| 66074 | | - if( DbMaskAllZero(p->lockMask) ) return; /* The common case */ |
| 66075 | 66902 | db = p->db; |
| 66076 | 66903 | aDb = db->aDb; |
| 66077 | 66904 | nDb = db->nDb; |
| 66078 | 66905 | for(i=0; i<nDb; i++){ |
| 66079 | 66906 | if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){ |
| 66080 | 66907 | sqlite3BtreeLeave(aDb[i].pBt); |
| 66081 | 66908 | } |
| 66082 | 66909 | } |
| 66910 | +} |
| 66911 | +SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){ |
| 66912 | + if( DbMaskAllZero(p->lockMask) ) return; /* The common case */ |
| 66913 | + vdbeLeave(p); |
| 66083 | 66914 | } |
| 66084 | 66915 | #endif |
| 66085 | 66916 | |
| 66086 | 66917 | #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) |
| 66087 | 66918 | /* |
| | @@ -67775,19 +68606,25 @@ |
| 67775 | 68606 | n += pMem->u.nZero; |
| 67776 | 68607 | } |
| 67777 | 68608 | return ((n*2) + 12 + ((flags&MEM_Str)!=0)); |
| 67778 | 68609 | } |
| 67779 | 68610 | |
| 68611 | +/* |
| 68612 | +** The sizes for serial types less than 12 |
| 68613 | +*/ |
| 68614 | +static const u8 sqlite3SmallTypeSizes[] = { |
| 68615 | + 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 |
| 68616 | +}; |
| 68617 | + |
| 67780 | 68618 | /* |
| 67781 | 68619 | ** Return the length of the data corresponding to the supplied serial-type. |
| 67782 | 68620 | */ |
| 67783 | 68621 | SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){ |
| 67784 | 68622 | if( serial_type>=12 ){ |
| 67785 | 68623 | return (serial_type-12)/2; |
| 67786 | 68624 | }else{ |
| 67787 | | - static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 }; |
| 67788 | | - return aSize[serial_type]; |
| 68625 | + return sqlite3SmallTypeSizes[serial_type]; |
| 67789 | 68626 | } |
| 67790 | 68627 | } |
| 67791 | 68628 | |
| 67792 | 68629 | /* |
| 67793 | 68630 | ** If we are on an architecture with mixed-endian floating |
| | @@ -67867,11 +68704,11 @@ |
| 67867 | 68704 | memcpy(&v, &pMem->u.r, sizeof(v)); |
| 67868 | 68705 | swapMixedEndianFloat(v); |
| 67869 | 68706 | }else{ |
| 67870 | 68707 | v = pMem->u.i; |
| 67871 | 68708 | } |
| 67872 | | - len = i = sqlite3VdbeSerialTypeLen(serial_type); |
| 68709 | + len = i = sqlite3SmallTypeSizes[serial_type]; |
| 67873 | 68710 | assert( i>0 ); |
| 67874 | 68711 | do{ |
| 67875 | 68712 | buf[--i] = (u8)(v&0xFF); |
| 67876 | 68713 | v >>= 8; |
| 67877 | 68714 | }while( i ); |
| | @@ -68896,11 +69733,11 @@ |
| 68896 | 69733 | testcase( typeRowid==8 ); |
| 68897 | 69734 | testcase( typeRowid==9 ); |
| 68898 | 69735 | if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){ |
| 68899 | 69736 | goto idx_rowid_corruption; |
| 68900 | 69737 | } |
| 68901 | | - lenRowid = sqlite3VdbeSerialTypeLen(typeRowid); |
| 69738 | + lenRowid = sqlite3SmallTypeSizes[typeRowid]; |
| 68902 | 69739 | testcase( (u32)m.n==szHdr+lenRowid ); |
| 68903 | 69740 | if( unlikely((u32)m.n<szHdr+lenRowid) ){ |
| 68904 | 69741 | goto idx_rowid_corruption; |
| 68905 | 69742 | } |
| 68906 | 69743 | |
| | @@ -71122,11 +71959,11 @@ |
| 71122 | 71959 | ** an integer representation is more space efficient on disk. |
| 71123 | 71960 | ** |
| 71124 | 71961 | ** SQLITE_AFF_TEXT: |
| 71125 | 71962 | ** Convert pRec to a text representation. |
| 71126 | 71963 | ** |
| 71127 | | -** SQLITE_AFF_NONE: |
| 71964 | +** SQLITE_AFF_BLOB: |
| 71128 | 71965 | ** No-op. pRec is unchanged. |
| 71129 | 71966 | */ |
| 71130 | 71967 | static void applyAffinity( |
| 71131 | 71968 | Mem *pRec, /* The value to apply affinity to */ |
| 71132 | 71969 | char affinity, /* The affinity to be applied */ |
| | @@ -71523,17 +72360,13 @@ |
| 71523 | 72360 | db->busyHandler.nBusy = 0; |
| 71524 | 72361 | if( db->u1.isInterrupted ) goto abort_due_to_interrupt; |
| 71525 | 72362 | sqlite3VdbeIOTraceSql(p); |
| 71526 | 72363 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 71527 | 72364 | if( db->xProgress ){ |
| 72365 | + u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; |
| 71528 | 72366 | assert( 0 < db->nProgressOps ); |
| 71529 | | - nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; |
| 71530 | | - if( nProgressLimit==0 ){ |
| 71531 | | - nProgressLimit = db->nProgressOps; |
| 71532 | | - }else{ |
| 71533 | | - nProgressLimit %= (unsigned)db->nProgressOps; |
| 71534 | | - } |
| 72367 | + nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps); |
| 71535 | 72368 | } |
| 71536 | 72369 | #endif |
| 71537 | 72370 | #ifdef SQLITE_DEBUG |
| 71538 | 72371 | sqlite3BeginBenignMalloc(); |
| 71539 | 72372 | if( p->pc==0 |
| | @@ -72491,14 +73324,14 @@ |
| 72491 | 73324 | sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); |
| 72492 | 73325 | } |
| 72493 | 73326 | break; |
| 72494 | 73327 | } |
| 72495 | 73328 | |
| 72496 | | -/* Opcode: Function P1 P2 P3 P4 P5 |
| 73329 | +/* Opcode: Function0 P1 P2 P3 P4 P5 |
| 72497 | 73330 | ** Synopsis: r[P3]=func(r[P2@P5]) |
| 72498 | 73331 | ** |
| 72499 | | -** Invoke a user function (P4 is a pointer to a Function structure that |
| 73332 | +** Invoke a user function (P4 is a pointer to a FuncDef object that |
| 72500 | 73333 | ** defines the function) with P5 arguments taken from register P2 and |
| 72501 | 73334 | ** successors. The result of the function is stored in register P3. |
| 72502 | 73335 | ** Register P3 must not be one of the function inputs. |
| 72503 | 73336 | ** |
| 72504 | 73337 | ** P1 is a 32-bit bitmask indicating whether or not each argument to the |
| | @@ -72506,63 +73339,104 @@ |
| 72506 | 73339 | ** argument was constant then bit 0 of P1 is set. This is used to determine |
| 72507 | 73340 | ** whether meta data associated with a user function argument using the |
| 72508 | 73341 | ** sqlite3_set_auxdata() API may be safely retained until the next |
| 72509 | 73342 | ** invocation of this opcode. |
| 72510 | 73343 | ** |
| 72511 | | -** See also: AggStep and AggFinal |
| 73344 | +** See also: Function, AggStep, AggFinal |
| 72512 | 73345 | */ |
| 72513 | | -case OP_Function: { |
| 72514 | | - int i; |
| 72515 | | - Mem *pArg; |
| 72516 | | - sqlite3_context ctx; |
| 72517 | | - sqlite3_value **apVal; |
| 73346 | +/* Opcode: Function P1 P2 P3 P4 P5 |
| 73347 | +** Synopsis: r[P3]=func(r[P2@P5]) |
| 73348 | +** |
| 73349 | +** Invoke a user function (P4 is a pointer to an sqlite3_context object that |
| 73350 | +** contains a pointer to the function to be run) with P5 arguments taken |
| 73351 | +** from register P2 and successors. The result of the function is stored |
| 73352 | +** in register P3. Register P3 must not be one of the function inputs. |
| 73353 | +** |
| 73354 | +** P1 is a 32-bit bitmask indicating whether or not each argument to the |
| 73355 | +** function was determined to be constant at compile time. If the first |
| 73356 | +** argument was constant then bit 0 of P1 is set. This is used to determine |
| 73357 | +** whether meta data associated with a user function argument using the |
| 73358 | +** sqlite3_set_auxdata() API may be safely retained until the next |
| 73359 | +** invocation of this opcode. |
| 73360 | +** |
| 73361 | +** SQL functions are initially coded as OP_Function0 with P4 pointing |
| 73362 | +** to a FuncDef object. But on first evaluation, the P4 operand is |
| 73363 | +** automatically converted into an sqlite3_context object and the operation |
| 73364 | +** changed to this OP_Function opcode. In this way, the initialization of |
| 73365 | +** the sqlite3_context object occurs only once, rather than once for each |
| 73366 | +** evaluation of the function. |
| 73367 | +** |
| 73368 | +** See also: Function0, AggStep, AggFinal |
| 73369 | +*/ |
| 73370 | +case OP_Function0: { |
| 72518 | 73371 | int n; |
| 72519 | | - |
| 72520 | | - n = pOp->p5; |
| 72521 | | - apVal = p->apArg; |
| 72522 | | - assert( apVal || n==0 ); |
| 72523 | | - assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); |
| 72524 | | - ctx.pOut = &aMem[pOp->p3]; |
| 72525 | | - memAboutToChange(p, ctx.pOut); |
| 72526 | | - |
| 72527 | | - assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) ); |
| 72528 | | - assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
| 72529 | | - pArg = &aMem[pOp->p2]; |
| 72530 | | - for(i=0; i<n; i++, pArg++){ |
| 72531 | | - assert( memIsValid(pArg) ); |
| 72532 | | - apVal[i] = pArg; |
| 72533 | | - Deephemeralize(pArg); |
| 72534 | | - REGISTER_TRACE(pOp->p2+i, pArg); |
| 72535 | | - } |
| 73372 | + sqlite3_context *pCtx; |
| 72536 | 73373 | |
| 72537 | 73374 | assert( pOp->p4type==P4_FUNCDEF ); |
| 72538 | | - ctx.pFunc = pOp->p4.pFunc; |
| 72539 | | - ctx.iOp = (int)(pOp - aOp); |
| 72540 | | - ctx.pVdbe = p; |
| 72541 | | - MemSetTypeFlag(ctx.pOut, MEM_Null); |
| 72542 | | - ctx.fErrorOrAux = 0; |
| 73375 | + n = pOp->p5; |
| 73376 | + assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); |
| 73377 | + assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) ); |
| 73378 | + assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
| 73379 | + pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); |
| 73380 | + if( pCtx==0 ) goto no_mem; |
| 73381 | + pCtx->pOut = 0; |
| 73382 | + pCtx->pFunc = pOp->p4.pFunc; |
| 73383 | + pCtx->iOp = (int)(pOp - aOp); |
| 73384 | + pCtx->pVdbe = p; |
| 73385 | + pCtx->argc = n; |
| 73386 | + pOp->p4type = P4_FUNCCTX; |
| 73387 | + pOp->p4.pCtx = pCtx; |
| 73388 | + pOp->opcode = OP_Function; |
| 73389 | + /* Fall through into OP_Function */ |
| 73390 | +} |
| 73391 | +case OP_Function: { |
| 73392 | + int i; |
| 73393 | + sqlite3_context *pCtx; |
| 73394 | + |
| 73395 | + assert( pOp->p4type==P4_FUNCCTX ); |
| 73396 | + pCtx = pOp->p4.pCtx; |
| 73397 | + |
| 73398 | + /* If this function is inside of a trigger, the register array in aMem[] |
| 73399 | + ** might change from one evaluation to the next. The next block of code |
| 73400 | + ** checks to see if the register array has changed, and if so it |
| 73401 | + ** reinitializes the relavant parts of the sqlite3_context object */ |
| 73402 | + pOut = &aMem[pOp->p3]; |
| 73403 | + if( pCtx->pOut != pOut ){ |
| 73404 | + pCtx->pOut = pOut; |
| 73405 | + for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
| 73406 | + } |
| 73407 | + |
| 73408 | + memAboutToChange(p, pCtx->pOut); |
| 73409 | +#ifdef SQLITE_DEBUG |
| 73410 | + for(i=0; i<pCtx->argc; i++){ |
| 73411 | + assert( memIsValid(pCtx->argv[i]) ); |
| 73412 | + REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
| 73413 | + } |
| 73414 | +#endif |
| 73415 | + MemSetTypeFlag(pCtx->pOut, MEM_Null); |
| 73416 | + pCtx->fErrorOrAux = 0; |
| 72543 | 73417 | db->lastRowid = lastRowid; |
| 72544 | | - (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */ |
| 73418 | + (*pCtx->pFunc->xFunc)(pCtx, pCtx->argc, pCtx->argv); /* IMP: R-24505-23230 */ |
| 72545 | 73419 | lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */ |
| 72546 | 73420 | |
| 72547 | 73421 | /* If the function returned an error, throw an exception */ |
| 72548 | | - if( ctx.fErrorOrAux ){ |
| 72549 | | - if( ctx.isError ){ |
| 72550 | | - sqlite3VdbeError(p, "%s", sqlite3_value_text(ctx.pOut)); |
| 72551 | | - rc = ctx.isError; |
| 73422 | + if( pCtx->fErrorOrAux ){ |
| 73423 | + if( pCtx->isError ){ |
| 73424 | + sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); |
| 73425 | + rc = pCtx->isError; |
| 72552 | 73426 | } |
| 72553 | | - sqlite3VdbeDeleteAuxData(p, (int)(pOp - aOp), pOp->p1); |
| 73427 | + sqlite3VdbeDeleteAuxData(p, pCtx->iOp, pOp->p1); |
| 72554 | 73428 | } |
| 72555 | 73429 | |
| 72556 | 73430 | /* Copy the result of the function into register P3 */ |
| 72557 | | - sqlite3VdbeChangeEncoding(ctx.pOut, encoding); |
| 72558 | | - if( sqlite3VdbeMemTooBig(ctx.pOut) ){ |
| 72559 | | - goto too_big; |
| 73431 | + if( pOut->flags & (MEM_Str|MEM_Blob) ){ |
| 73432 | + sqlite3VdbeChangeEncoding(pCtx->pOut, encoding); |
| 73433 | + if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big; |
| 72560 | 73434 | } |
| 72561 | 73435 | |
| 72562 | | - REGISTER_TRACE(pOp->p3, ctx.pOut); |
| 72563 | | - UPDATE_MAX_BLOBSIZE(ctx.pOut); |
| 73436 | + REGISTER_TRACE(pOp->p3, pCtx->pOut); |
| 73437 | + UPDATE_MAX_BLOBSIZE(pCtx->pOut); |
| 72564 | 73438 | break; |
| 72565 | 73439 | } |
| 72566 | 73440 | |
| 72567 | 73441 | /* Opcode: BitAnd P1 P2 P3 * * |
| 72568 | 73442 | ** Synopsis: r[P3]=r[P1]&r[P2] |
| | @@ -72721,13 +73595,13 @@ |
| 72721 | 73595 | ** </ul> |
| 72722 | 73596 | ** |
| 72723 | 73597 | ** A NULL value is not changed by this routine. It remains NULL. |
| 72724 | 73598 | */ |
| 72725 | 73599 | case OP_Cast: { /* in1 */ |
| 72726 | | - assert( pOp->p2>=SQLITE_AFF_NONE && pOp->p2<=SQLITE_AFF_REAL ); |
| 73600 | + assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL ); |
| 72727 | 73601 | testcase( pOp->p2==SQLITE_AFF_TEXT ); |
| 72728 | | - testcase( pOp->p2==SQLITE_AFF_NONE ); |
| 73602 | + testcase( pOp->p2==SQLITE_AFF_BLOB ); |
| 72729 | 73603 | testcase( pOp->p2==SQLITE_AFF_NUMERIC ); |
| 72730 | 73604 | testcase( pOp->p2==SQLITE_AFF_INTEGER ); |
| 72731 | 73605 | testcase( pOp->p2==SQLITE_AFF_REAL ); |
| 72732 | 73606 | pIn1 = &aMem[pOp->p1]; |
| 72733 | 73607 | memAboutToChange(p, pIn1); |
| | @@ -73534,11 +74408,11 @@ |
| 73534 | 74408 | ** field of the index key. |
| 73535 | 74409 | ** |
| 73536 | 74410 | ** The mapping from character to affinity is given by the SQLITE_AFF_ |
| 73537 | 74411 | ** macros defined in sqliteInt.h. |
| 73538 | 74412 | ** |
| 73539 | | -** If P4 is NULL then all index fields have the affinity NONE. |
| 74413 | +** If P4 is NULL then all index fields have the affinity BLOB. |
| 73540 | 74414 | */ |
| 73541 | 74415 | case OP_MakeRecord: { |
| 73542 | 74416 | u8 *zNewRecord; /* A buffer to hold the data for the new record */ |
| 73543 | 74417 | Mem *pRec; /* The new record */ |
| 73544 | 74418 | u64 nData; /* Number of bytes of data space */ |
| | @@ -74451,10 +75325,30 @@ |
| 74451 | 75325 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 74452 | 75326 | sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); |
| 74453 | 75327 | p->apCsr[pOp->p1] = 0; |
| 74454 | 75328 | break; |
| 74455 | 75329 | } |
| 75330 | + |
| 75331 | +#ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
| 75332 | +/* Opcode: ColumnsUsed P1 * * P4 * |
| 75333 | +** |
| 75334 | +** This opcode (which only exists if SQLite was compiled with |
| 75335 | +** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the |
| 75336 | +** table or index for cursor P1 are used. P4 is a 64-bit integer |
| 75337 | +** (P4_INT64) in which the first 63 bits are one for each of the |
| 75338 | +** first 63 columns of the table or index that are actually used |
| 75339 | +** by the cursor. The high-order bit is set if any column after |
| 75340 | +** the 64th is used. |
| 75341 | +*/ |
| 75342 | +case OP_ColumnsUsed: { |
| 75343 | + VdbeCursor *pC; |
| 75344 | + pC = p->apCsr[pOp->p1]; |
| 75345 | + assert( pC->pCursor ); |
| 75346 | + pC->maskUsed = *(u64*)pOp->p4.pI64; |
| 75347 | + break; |
| 75348 | +} |
| 75349 | +#endif |
| 74456 | 75350 | |
| 74457 | 75351 | /* Opcode: SeekGE P1 P2 P3 P4 * |
| 74458 | 75352 | ** Synopsis: key=r[P3@P4] |
| 74459 | 75353 | ** |
| 74460 | 75354 | ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), |
| | @@ -74940,13 +75834,12 @@ |
| 74940 | 75834 | res = 0; |
| 74941 | 75835 | pOut = out2Prerelease(p, pOp); |
| 74942 | 75836 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 74943 | 75837 | pC = p->apCsr[pOp->p1]; |
| 74944 | 75838 | assert( pC!=0 ); |
| 74945 | | - if( NEVER(pC->pCursor==0) ){ |
| 74946 | | - /* The zero initialization above is all that is needed */ |
| 74947 | | - }else{ |
| 75839 | + assert( pC->pCursor!=0 ); |
| 75840 | + { |
| 74948 | 75841 | /* The next rowid or record number (different terms for the same |
| 74949 | 75842 | ** thing) is obtained in a two-step algorithm. |
| 74950 | 75843 | ** |
| 74951 | 75844 | ** First we attempt to find the largest existing rowid and add one |
| 74952 | 75845 | ** to that. But if the largest existing rowid is already the maximum |
| | @@ -75681,32 +76574,30 @@ |
| 75681 | 76574 | ** for tables is OP_Insert. |
| 75682 | 76575 | */ |
| 75683 | 76576 | case OP_SorterInsert: /* in2 */ |
| 75684 | 76577 | case OP_IdxInsert: { /* in2 */ |
| 75685 | 76578 | VdbeCursor *pC; |
| 75686 | | - BtCursor *pCrsr; |
| 75687 | 76579 | int nKey; |
| 75688 | 76580 | const char *zKey; |
| 75689 | 76581 | |
| 75690 | 76582 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 75691 | 76583 | pC = p->apCsr[pOp->p1]; |
| 75692 | 76584 | assert( pC!=0 ); |
| 75693 | 76585 | assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) ); |
| 75694 | 76586 | pIn2 = &aMem[pOp->p2]; |
| 75695 | 76587 | assert( pIn2->flags & MEM_Blob ); |
| 75696 | | - pCrsr = pC->pCursor; |
| 75697 | 76588 | if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; |
| 75698 | | - assert( pCrsr!=0 ); |
| 76589 | + assert( pC->pCursor!=0 ); |
| 75699 | 76590 | assert( pC->isTable==0 ); |
| 75700 | 76591 | rc = ExpandBlob(pIn2); |
| 75701 | 76592 | if( rc==SQLITE_OK ){ |
| 75702 | | - if( isSorter(pC) ){ |
| 76593 | + if( pOp->opcode==OP_SorterInsert ){ |
| 75703 | 76594 | rc = sqlite3VdbeSorterWrite(pC, pIn2); |
| 75704 | 76595 | }else{ |
| 75705 | 76596 | nKey = pIn2->n; |
| 75706 | 76597 | zKey = pIn2->z; |
| 75707 | | - rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3, |
| 76598 | + rc = sqlite3BtreeInsert(pC->pCursor, zKey, nKey, "", 0, 0, pOp->p3, |
| 75708 | 76599 | ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) |
| 75709 | 76600 | ); |
| 75710 | 76601 | assert( pC->deferredMoveto==0 ); |
| 75711 | 76602 | pC->cacheStatus = CACHE_STALE; |
| 75712 | 76603 | } |
| | @@ -76622,61 +77513,105 @@ |
| 76622 | 77513 | VdbeBranchTaken(pIn1->u.i==0, 2); |
| 76623 | 77514 | if( (pIn1->u.i++)==0 ) goto jump_to_p2; |
| 76624 | 77515 | break; |
| 76625 | 77516 | } |
| 76626 | 77517 | |
| 76627 | | -/* Opcode: AggStep * P2 P3 P4 P5 |
| 77518 | +/* Opcode: AggStep0 * P2 P3 P4 P5 |
| 76628 | 77519 | ** Synopsis: accum=r[P3] step(r[P2@P5]) |
| 76629 | 77520 | ** |
| 76630 | 77521 | ** Execute the step function for an aggregate. The |
| 76631 | 77522 | ** function has P5 arguments. P4 is a pointer to the FuncDef |
| 76632 | | -** structure that specifies the function. Use register |
| 76633 | | -** P3 as the accumulator. |
| 77523 | +** structure that specifies the function. Register P3 is the |
| 77524 | +** accumulator. |
| 77525 | +** |
| 77526 | +** The P5 arguments are taken from register P2 and its |
| 77527 | +** successors. |
| 77528 | +*/ |
| 77529 | +/* Opcode: AggStep * P2 P3 P4 P5 |
| 77530 | +** Synopsis: accum=r[P3] step(r[P2@P5]) |
| 77531 | +** |
| 77532 | +** Execute the step function for an aggregate. The |
| 77533 | +** function has P5 arguments. P4 is a pointer to an sqlite3_context |
| 77534 | +** object that is used to run the function. Register P3 is |
| 77535 | +** as the accumulator. |
| 76634 | 77536 | ** |
| 76635 | 77537 | ** The P5 arguments are taken from register P2 and its |
| 76636 | 77538 | ** successors. |
| 77539 | +** |
| 77540 | +** This opcode is initially coded as OP_AggStep0. On first evaluation, |
| 77541 | +** the FuncDef stored in P4 is converted into an sqlite3_context and |
| 77542 | +** the opcode is changed. In this way, the initialization of the |
| 77543 | +** sqlite3_context only happens once, instead of on each call to the |
| 77544 | +** step function. |
| 76637 | 77545 | */ |
| 76638 | | -case OP_AggStep: { |
| 77546 | +case OP_AggStep0: { |
| 76639 | 77547 | int n; |
| 76640 | | - int i; |
| 76641 | | - Mem *pMem; |
| 76642 | | - Mem *pRec; |
| 76643 | | - Mem t; |
| 76644 | | - sqlite3_context ctx; |
| 76645 | | - sqlite3_value **apVal; |
| 77548 | + sqlite3_context *pCtx; |
| 76646 | 77549 | |
| 77550 | + assert( pOp->p4type==P4_FUNCDEF ); |
| 76647 | 77551 | n = pOp->p5; |
| 76648 | | - assert( n>=0 ); |
| 76649 | | - pRec = &aMem[pOp->p2]; |
| 76650 | | - apVal = p->apArg; |
| 76651 | | - assert( apVal || n==0 ); |
| 76652 | | - for(i=0; i<n; i++, pRec++){ |
| 76653 | | - assert( memIsValid(pRec) ); |
| 76654 | | - apVal[i] = pRec; |
| 76655 | | - memAboutToChange(p, pRec); |
| 76656 | | - } |
| 76657 | | - ctx.pFunc = pOp->p4.pFunc; |
| 76658 | 77552 | assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); |
| 76659 | | - ctx.pMem = pMem = &aMem[pOp->p3]; |
| 77553 | + assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) ); |
| 77554 | + assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); |
| 77555 | + pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); |
| 77556 | + if( pCtx==0 ) goto no_mem; |
| 77557 | + pCtx->pMem = 0; |
| 77558 | + pCtx->pFunc = pOp->p4.pFunc; |
| 77559 | + pCtx->iOp = (int)(pOp - aOp); |
| 77560 | + pCtx->pVdbe = p; |
| 77561 | + pCtx->argc = n; |
| 77562 | + pOp->p4type = P4_FUNCCTX; |
| 77563 | + pOp->p4.pCtx = pCtx; |
| 77564 | + pOp->opcode = OP_AggStep; |
| 77565 | + /* Fall through into OP_AggStep */ |
| 77566 | +} |
| 77567 | +case OP_AggStep: { |
| 77568 | + int i; |
| 77569 | + sqlite3_context *pCtx; |
| 77570 | + Mem *pMem; |
| 77571 | + Mem t; |
| 77572 | + |
| 77573 | + assert( pOp->p4type==P4_FUNCCTX ); |
| 77574 | + pCtx = pOp->p4.pCtx; |
| 77575 | + pMem = &aMem[pOp->p3]; |
| 77576 | + |
| 77577 | + /* If this function is inside of a trigger, the register array in aMem[] |
| 77578 | + ** might change from one evaluation to the next. The next block of code |
| 77579 | + ** checks to see if the register array has changed, and if so it |
| 77580 | + ** reinitializes the relavant parts of the sqlite3_context object */ |
| 77581 | + if( pCtx->pMem != pMem ){ |
| 77582 | + pCtx->pMem = pMem; |
| 77583 | + for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; |
| 77584 | + } |
| 77585 | + |
| 77586 | +#ifdef SQLITE_DEBUG |
| 77587 | + for(i=0; i<pCtx->argc; i++){ |
| 77588 | + assert( memIsValid(pCtx->argv[i]) ); |
| 77589 | + REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); |
| 77590 | + } |
| 77591 | +#endif |
| 77592 | + |
| 76660 | 77593 | pMem->n++; |
| 76661 | 77594 | sqlite3VdbeMemInit(&t, db, MEM_Null); |
| 76662 | | - ctx.pOut = &t; |
| 76663 | | - ctx.isError = 0; |
| 76664 | | - ctx.pVdbe = p; |
| 76665 | | - ctx.iOp = (int)(pOp - aOp); |
| 76666 | | - ctx.skipFlag = 0; |
| 76667 | | - (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */ |
| 76668 | | - if( ctx.isError ){ |
| 76669 | | - sqlite3VdbeError(p, "%s", sqlite3_value_text(&t)); |
| 76670 | | - rc = ctx.isError; |
| 76671 | | - } |
| 76672 | | - if( ctx.skipFlag ){ |
| 77595 | + pCtx->pOut = &t; |
| 77596 | + pCtx->fErrorOrAux = 0; |
| 77597 | + pCtx->skipFlag = 0; |
| 77598 | + (pCtx->pFunc->xStep)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */ |
| 77599 | + if( pCtx->fErrorOrAux ){ |
| 77600 | + if( pCtx->isError ){ |
| 77601 | + sqlite3VdbeError(p, "%s", sqlite3_value_text(&t)); |
| 77602 | + rc = pCtx->isError; |
| 77603 | + } |
| 77604 | + sqlite3VdbeMemRelease(&t); |
| 77605 | + }else{ |
| 77606 | + assert( t.flags==MEM_Null ); |
| 77607 | + } |
| 77608 | + if( pCtx->skipFlag ){ |
| 76673 | 77609 | assert( pOp[-1].opcode==OP_CollSeq ); |
| 76674 | 77610 | i = pOp[-1].p1; |
| 76675 | 77611 | if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); |
| 76676 | 77612 | } |
| 76677 | | - sqlite3VdbeMemRelease(&t); |
| 76678 | 77613 | break; |
| 76679 | 77614 | } |
| 76680 | 77615 | |
| 76681 | 77616 | /* Opcode: AggFinal P1 P2 * P4 * |
| 76682 | 77617 | ** Synopsis: accum=r[P1] N=P2 |
| | @@ -82721,10 +83656,17 @@ |
| 82721 | 83656 | "the GROUP BY clause"); |
| 82722 | 83657 | return WRC_Abort; |
| 82723 | 83658 | } |
| 82724 | 83659 | } |
| 82725 | 83660 | } |
| 83661 | + |
| 83662 | + /* If this is part of a compound SELECT, check that it has the right |
| 83663 | + ** number of expressions in the select list. */ |
| 83664 | + if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){ |
| 83665 | + sqlite3SelectWrongNumTermsError(pParse, p->pNext); |
| 83666 | + return WRC_Abort; |
| 83667 | + } |
| 82726 | 83668 | |
| 82727 | 83669 | /* Advance to the next term of the compound |
| 82728 | 83670 | */ |
| 82729 | 83671 | p = p->pPrior; |
| 82730 | 83672 | nCompound++; |
| | @@ -83089,17 +84031,17 @@ |
| 83089 | 84031 | ** affinity, use that. Otherwise use no affinity. |
| 83090 | 84032 | */ |
| 83091 | 84033 | if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ |
| 83092 | 84034 | return SQLITE_AFF_NUMERIC; |
| 83093 | 84035 | }else{ |
| 83094 | | - return SQLITE_AFF_NONE; |
| 84036 | + return SQLITE_AFF_BLOB; |
| 83095 | 84037 | } |
| 83096 | 84038 | }else if( !aff1 && !aff2 ){ |
| 83097 | 84039 | /* Neither side of the comparison is a column. Compare the |
| 83098 | 84040 | ** results directly. |
| 83099 | 84041 | */ |
| 83100 | | - return SQLITE_AFF_NONE; |
| 84042 | + return SQLITE_AFF_BLOB; |
| 83101 | 84043 | }else{ |
| 83102 | 84044 | /* One side is a column, the other is not. Use the columns affinity. */ |
| 83103 | 84045 | assert( aff1==0 || aff2==0 ); |
| 83104 | 84046 | return (aff1 + aff2); |
| 83105 | 84047 | } |
| | @@ -83119,11 +84061,11 @@ |
| 83119 | 84061 | if( pExpr->pRight ){ |
| 83120 | 84062 | aff = sqlite3CompareAffinity(pExpr->pRight, aff); |
| 83121 | 84063 | }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 83122 | 84064 | aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff); |
| 83123 | 84065 | }else if( !aff ){ |
| 83124 | | - aff = SQLITE_AFF_NONE; |
| 84066 | + aff = SQLITE_AFF_BLOB; |
| 83125 | 84067 | } |
| 83126 | 84068 | return aff; |
| 83127 | 84069 | } |
| 83128 | 84070 | |
| 83129 | 84071 | /* |
| | @@ -83133,11 +84075,11 @@ |
| 83133 | 84075 | ** the comparison in pExpr. |
| 83134 | 84076 | */ |
| 83135 | 84077 | SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ |
| 83136 | 84078 | char aff = comparisonAffinity(pExpr); |
| 83137 | 84079 | switch( aff ){ |
| 83138 | | - case SQLITE_AFF_NONE: |
| 84080 | + case SQLITE_AFF_BLOB: |
| 83139 | 84081 | return 1; |
| 83140 | 84082 | case SQLITE_AFF_TEXT: |
| 83141 | 84083 | return idx_affinity==SQLITE_AFF_TEXT; |
| 83142 | 84084 | default: |
| 83143 | 84085 | return sqlite3IsNumericAffinity(idx_affinity); |
| | @@ -83939,11 +84881,11 @@ |
| 83939 | 84881 | pNewItem->addrFillSub = pOldItem->addrFillSub; |
| 83940 | 84882 | pNewItem->regReturn = pOldItem->regReturn; |
| 83941 | 84883 | pNewItem->isCorrelated = pOldItem->isCorrelated; |
| 83942 | 84884 | pNewItem->viaCoroutine = pOldItem->viaCoroutine; |
| 83943 | 84885 | pNewItem->isRecursive = pOldItem->isRecursive; |
| 83944 | | - pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex); |
| 84886 | + pNewItem->zIndexedBy = sqlite3DbStrDup(db, pOldItem->zIndexedBy); |
| 83945 | 84887 | pNewItem->notIndexed = pOldItem->notIndexed; |
| 83946 | 84888 | pNewItem->pIndex = pOldItem->pIndex; |
| 83947 | 84889 | pTab = pNewItem->pTab = pOldItem->pTab; |
| 83948 | 84890 | if( pTab ){ |
| 83949 | 84891 | pTab->nRef++; |
| | @@ -84166,11 +85108,11 @@ |
| 84166 | 85108 | ** |
| 84167 | 85109 | ** These callback routines are used to implement the following: |
| 84168 | 85110 | ** |
| 84169 | 85111 | ** sqlite3ExprIsConstant() pWalker->eCode==1 |
| 84170 | 85112 | ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 |
| 84171 | | -** sqlite3ExprRefOneTableOnly() pWalker->eCode==3 |
| 85113 | +** sqlite3ExprIsTableConstant() pWalker->eCode==3 |
| 84172 | 85114 | ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 |
| 84173 | 85115 | ** |
| 84174 | 85116 | ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression |
| 84175 | 85117 | ** is found to not be a constant. |
| 84176 | 85118 | ** |
| | @@ -84274,11 +85216,11 @@ |
| 84274 | 85216 | SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){ |
| 84275 | 85217 | return exprIsConst(p, 2, 0); |
| 84276 | 85218 | } |
| 84277 | 85219 | |
| 84278 | 85220 | /* |
| 84279 | | -** Walk an expression tree. Return non-zero if the expression constant |
| 85221 | +** Walk an expression tree. Return non-zero if the expression is constant |
| 84280 | 85222 | ** for any single row of the table with cursor iCur. In other words, the |
| 84281 | 85223 | ** expression must not refer to any non-deterministic function nor any |
| 84282 | 85224 | ** table other than iCur. |
| 84283 | 85225 | */ |
| 84284 | 85226 | SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){ |
| | @@ -84380,11 +85322,11 @@ |
| 84380 | 85322 | ** is harmless. A false positive, however, can result in the wrong |
| 84381 | 85323 | ** answer. |
| 84382 | 85324 | */ |
| 84383 | 85325 | SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){ |
| 84384 | 85326 | u8 op; |
| 84385 | | - if( aff==SQLITE_AFF_NONE ) return 1; |
| 85327 | + if( aff==SQLITE_AFF_BLOB ) return 1; |
| 84386 | 85328 | while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } |
| 84387 | 85329 | op = p->op; |
| 84388 | 85330 | if( op==TK_REGISTER ) op = p->op2; |
| 84389 | 85331 | switch( op ){ |
| 84390 | 85332 | case TK_INTEGER: { |
| | @@ -84831,11 +85773,11 @@ |
| 84831 | 85773 | ExprList *pList = pExpr->x.pList; |
| 84832 | 85774 | struct ExprList_item *pItem; |
| 84833 | 85775 | int r1, r2, r3; |
| 84834 | 85776 | |
| 84835 | 85777 | if( !affinity ){ |
| 84836 | | - affinity = SQLITE_AFF_NONE; |
| 85778 | + affinity = SQLITE_AFF_BLOB; |
| 84837 | 85779 | } |
| 84838 | 85780 | if( pKeyInfo ){ |
| 84839 | 85781 | assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); |
| 84840 | 85782 | pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
| 84841 | 85783 | } |
| | @@ -85106,21 +86048,10 @@ |
| 85106 | 86048 | sqlite3ExprCachePop(pParse); |
| 85107 | 86049 | VdbeComment((v, "end IN expr")); |
| 85108 | 86050 | } |
| 85109 | 86051 | #endif /* SQLITE_OMIT_SUBQUERY */ |
| 85110 | 86052 | |
| 85111 | | -/* |
| 85112 | | -** Duplicate an 8-byte value |
| 85113 | | -*/ |
| 85114 | | -static char *dup8bytes(Vdbe *v, const char *in){ |
| 85115 | | - char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8); |
| 85116 | | - if( out ){ |
| 85117 | | - memcpy(out, in, 8); |
| 85118 | | - } |
| 85119 | | - return out; |
| 85120 | | -} |
| 85121 | | - |
| 85122 | 86053 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 85123 | 86054 | /* |
| 85124 | 86055 | ** Generate an instruction that will put the floating point |
| 85125 | 86056 | ** value described by z[0..n-1] into register iMem. |
| 85126 | 86057 | ** |
| | @@ -85129,16 +86060,14 @@ |
| 85129 | 86060 | ** like the continuation of the number. |
| 85130 | 86061 | */ |
| 85131 | 86062 | static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){ |
| 85132 | 86063 | if( ALWAYS(z!=0) ){ |
| 85133 | 86064 | double value; |
| 85134 | | - char *zV; |
| 85135 | 86065 | sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); |
| 85136 | 86066 | assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */ |
| 85137 | 86067 | if( negateFlag ) value = -value; |
| 85138 | | - zV = dup8bytes(v, (char*)&value); |
| 85139 | | - sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL); |
| 86068 | + sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL); |
| 85140 | 86069 | } |
| 85141 | 86070 | } |
| 85142 | 86071 | #endif |
| 85143 | 86072 | |
| 85144 | 86073 | |
| | @@ -85160,14 +86089,12 @@ |
| 85160 | 86089 | i64 value; |
| 85161 | 86090 | const char *z = pExpr->u.zToken; |
| 85162 | 86091 | assert( z!=0 ); |
| 85163 | 86092 | c = sqlite3DecOrHexToI64(z, &value); |
| 85164 | 86093 | if( c==0 || (c==2 && negFlag) ){ |
| 85165 | | - char *zV; |
| 85166 | 86094 | if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; } |
| 85167 | | - zV = dup8bytes(v, (char*)&value); |
| 85168 | | - sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64); |
| 86095 | + sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64); |
| 85169 | 86096 | }else{ |
| 85170 | 86097 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 85171 | 86098 | sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); |
| 85172 | 86099 | #else |
| 85173 | 86100 | #ifndef SQLITE_OMIT_HEX_INTEGER |
| | @@ -85768,11 +86695,11 @@ |
| 85768 | 86695 | /* The UNLIKELY() function is a no-op. The result is the value |
| 85769 | 86696 | ** of the first argument. |
| 85770 | 86697 | */ |
| 85771 | 86698 | if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ |
| 85772 | 86699 | assert( nFarg>=1 ); |
| 85773 | | - sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); |
| 86700 | + inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target); |
| 85774 | 86701 | break; |
| 85775 | 86702 | } |
| 85776 | 86703 | |
| 85777 | 86704 | for(i=0; i<nFarg; i++){ |
| 85778 | 86705 | if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){ |
| | @@ -85838,11 +86765,11 @@ |
| 85838 | 86765 | #endif |
| 85839 | 86766 | if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){ |
| 85840 | 86767 | if( !pColl ) pColl = db->pDfltColl; |
| 85841 | 86768 | sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); |
| 85842 | 86769 | } |
| 85843 | | - sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target, |
| 86770 | + sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target, |
| 85844 | 86771 | (char*)pDef, P4_FUNCDEF); |
| 85845 | 86772 | sqlite3VdbeChangeP5(v, (u8)nFarg); |
| 85846 | 86773 | if( nFarg && constMask==0 ){ |
| 85847 | 86774 | sqlite3ReleaseTempRange(pParse, r1, nFarg); |
| 85848 | 86775 | } |
| | @@ -86209,272 +87136,10 @@ |
| 86209 | 87136 | iMem = ++pParse->nMem; |
| 86210 | 87137 | sqlite3VdbeAddOp2(v, OP_Copy, target, iMem); |
| 86211 | 87138 | exprToRegister(pExpr, iMem); |
| 86212 | 87139 | } |
| 86213 | 87140 | |
| 86214 | | -#ifdef SQLITE_DEBUG |
| 86215 | | -/* |
| 86216 | | -** Generate a human-readable explanation of an expression tree. |
| 86217 | | -*/ |
| 86218 | | -SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ |
| 86219 | | - const char *zBinOp = 0; /* Binary operator */ |
| 86220 | | - const char *zUniOp = 0; /* Unary operator */ |
| 86221 | | - pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 86222 | | - if( pExpr==0 ){ |
| 86223 | | - sqlite3TreeViewLine(pView, "nil"); |
| 86224 | | - sqlite3TreeViewPop(pView); |
| 86225 | | - return; |
| 86226 | | - } |
| 86227 | | - switch( pExpr->op ){ |
| 86228 | | - case TK_AGG_COLUMN: { |
| 86229 | | - sqlite3TreeViewLine(pView, "AGG{%d:%d}", |
| 86230 | | - pExpr->iTable, pExpr->iColumn); |
| 86231 | | - break; |
| 86232 | | - } |
| 86233 | | - case TK_COLUMN: { |
| 86234 | | - if( pExpr->iTable<0 ){ |
| 86235 | | - /* This only happens when coding check constraints */ |
| 86236 | | - sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn); |
| 86237 | | - }else{ |
| 86238 | | - sqlite3TreeViewLine(pView, "{%d:%d}", |
| 86239 | | - pExpr->iTable, pExpr->iColumn); |
| 86240 | | - } |
| 86241 | | - break; |
| 86242 | | - } |
| 86243 | | - case TK_INTEGER: { |
| 86244 | | - if( pExpr->flags & EP_IntValue ){ |
| 86245 | | - sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); |
| 86246 | | - }else{ |
| 86247 | | - sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); |
| 86248 | | - } |
| 86249 | | - break; |
| 86250 | | - } |
| 86251 | | -#ifndef SQLITE_OMIT_FLOATING_POINT |
| 86252 | | - case TK_FLOAT: { |
| 86253 | | - sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 86254 | | - break; |
| 86255 | | - } |
| 86256 | | -#endif |
| 86257 | | - case TK_STRING: { |
| 86258 | | - sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); |
| 86259 | | - break; |
| 86260 | | - } |
| 86261 | | - case TK_NULL: { |
| 86262 | | - sqlite3TreeViewLine(pView,"NULL"); |
| 86263 | | - break; |
| 86264 | | - } |
| 86265 | | -#ifndef SQLITE_OMIT_BLOB_LITERAL |
| 86266 | | - case TK_BLOB: { |
| 86267 | | - sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 86268 | | - break; |
| 86269 | | - } |
| 86270 | | -#endif |
| 86271 | | - case TK_VARIABLE: { |
| 86272 | | - sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)", |
| 86273 | | - pExpr->u.zToken, pExpr->iColumn); |
| 86274 | | - break; |
| 86275 | | - } |
| 86276 | | - case TK_REGISTER: { |
| 86277 | | - sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); |
| 86278 | | - break; |
| 86279 | | - } |
| 86280 | | - case TK_AS: { |
| 86281 | | - sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken); |
| 86282 | | - sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 86283 | | - break; |
| 86284 | | - } |
| 86285 | | - case TK_ID: { |
| 86286 | | - sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken); |
| 86287 | | - break; |
| 86288 | | - } |
| 86289 | | -#ifndef SQLITE_OMIT_CAST |
| 86290 | | - case TK_CAST: { |
| 86291 | | - /* Expressions of the form: CAST(pLeft AS token) */ |
| 86292 | | - sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); |
| 86293 | | - sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 86294 | | - break; |
| 86295 | | - } |
| 86296 | | -#endif /* SQLITE_OMIT_CAST */ |
| 86297 | | - case TK_LT: zBinOp = "LT"; break; |
| 86298 | | - case TK_LE: zBinOp = "LE"; break; |
| 86299 | | - case TK_GT: zBinOp = "GT"; break; |
| 86300 | | - case TK_GE: zBinOp = "GE"; break; |
| 86301 | | - case TK_NE: zBinOp = "NE"; break; |
| 86302 | | - case TK_EQ: zBinOp = "EQ"; break; |
| 86303 | | - case TK_IS: zBinOp = "IS"; break; |
| 86304 | | - case TK_ISNOT: zBinOp = "ISNOT"; break; |
| 86305 | | - case TK_AND: zBinOp = "AND"; break; |
| 86306 | | - case TK_OR: zBinOp = "OR"; break; |
| 86307 | | - case TK_PLUS: zBinOp = "ADD"; break; |
| 86308 | | - case TK_STAR: zBinOp = "MUL"; break; |
| 86309 | | - case TK_MINUS: zBinOp = "SUB"; break; |
| 86310 | | - case TK_REM: zBinOp = "REM"; break; |
| 86311 | | - case TK_BITAND: zBinOp = "BITAND"; break; |
| 86312 | | - case TK_BITOR: zBinOp = "BITOR"; break; |
| 86313 | | - case TK_SLASH: zBinOp = "DIV"; break; |
| 86314 | | - case TK_LSHIFT: zBinOp = "LSHIFT"; break; |
| 86315 | | - case TK_RSHIFT: zBinOp = "RSHIFT"; break; |
| 86316 | | - case TK_CONCAT: zBinOp = "CONCAT"; break; |
| 86317 | | - case TK_DOT: zBinOp = "DOT"; break; |
| 86318 | | - |
| 86319 | | - case TK_UMINUS: zUniOp = "UMINUS"; break; |
| 86320 | | - case TK_UPLUS: zUniOp = "UPLUS"; break; |
| 86321 | | - case TK_BITNOT: zUniOp = "BITNOT"; break; |
| 86322 | | - case TK_NOT: zUniOp = "NOT"; break; |
| 86323 | | - case TK_ISNULL: zUniOp = "ISNULL"; break; |
| 86324 | | - case TK_NOTNULL: zUniOp = "NOTNULL"; break; |
| 86325 | | - |
| 86326 | | - case TK_COLLATE: { |
| 86327 | | - sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken); |
| 86328 | | - sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 86329 | | - break; |
| 86330 | | - } |
| 86331 | | - |
| 86332 | | - case TK_AGG_FUNCTION: |
| 86333 | | - case TK_FUNCTION: { |
| 86334 | | - ExprList *pFarg; /* List of function arguments */ |
| 86335 | | - if( ExprHasProperty(pExpr, EP_TokenOnly) ){ |
| 86336 | | - pFarg = 0; |
| 86337 | | - }else{ |
| 86338 | | - pFarg = pExpr->x.pList; |
| 86339 | | - } |
| 86340 | | - if( pExpr->op==TK_AGG_FUNCTION ){ |
| 86341 | | - sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q", |
| 86342 | | - pExpr->op2, pExpr->u.zToken); |
| 86343 | | - }else{ |
| 86344 | | - sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken); |
| 86345 | | - } |
| 86346 | | - if( pFarg ){ |
| 86347 | | - sqlite3TreeViewExprList(pView, pFarg, 0, 0); |
| 86348 | | - } |
| 86349 | | - break; |
| 86350 | | - } |
| 86351 | | -#ifndef SQLITE_OMIT_SUBQUERY |
| 86352 | | - case TK_EXISTS: { |
| 86353 | | - sqlite3TreeViewLine(pView, "EXISTS-expr"); |
| 86354 | | - sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 86355 | | - break; |
| 86356 | | - } |
| 86357 | | - case TK_SELECT: { |
| 86358 | | - sqlite3TreeViewLine(pView, "SELECT-expr"); |
| 86359 | | - sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 86360 | | - break; |
| 86361 | | - } |
| 86362 | | - case TK_IN: { |
| 86363 | | - sqlite3TreeViewLine(pView, "IN"); |
| 86364 | | - sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 86365 | | - if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 86366 | | - sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 86367 | | - }else{ |
| 86368 | | - sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 86369 | | - } |
| 86370 | | - break; |
| 86371 | | - } |
| 86372 | | -#endif /* SQLITE_OMIT_SUBQUERY */ |
| 86373 | | - |
| 86374 | | - /* |
| 86375 | | - ** x BETWEEN y AND z |
| 86376 | | - ** |
| 86377 | | - ** This is equivalent to |
| 86378 | | - ** |
| 86379 | | - ** x>=y AND x<=z |
| 86380 | | - ** |
| 86381 | | - ** X is stored in pExpr->pLeft. |
| 86382 | | - ** Y is stored in pExpr->pList->a[0].pExpr. |
| 86383 | | - ** Z is stored in pExpr->pList->a[1].pExpr. |
| 86384 | | - */ |
| 86385 | | - case TK_BETWEEN: { |
| 86386 | | - Expr *pX = pExpr->pLeft; |
| 86387 | | - Expr *pY = pExpr->x.pList->a[0].pExpr; |
| 86388 | | - Expr *pZ = pExpr->x.pList->a[1].pExpr; |
| 86389 | | - sqlite3TreeViewLine(pView, "BETWEEN"); |
| 86390 | | - sqlite3TreeViewExpr(pView, pX, 1); |
| 86391 | | - sqlite3TreeViewExpr(pView, pY, 1); |
| 86392 | | - sqlite3TreeViewExpr(pView, pZ, 0); |
| 86393 | | - break; |
| 86394 | | - } |
| 86395 | | - case TK_TRIGGER: { |
| 86396 | | - /* If the opcode is TK_TRIGGER, then the expression is a reference |
| 86397 | | - ** to a column in the new.* or old.* pseudo-tables available to |
| 86398 | | - ** trigger programs. In this case Expr.iTable is set to 1 for the |
| 86399 | | - ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn |
| 86400 | | - ** is set to the column of the pseudo-table to read, or to -1 to |
| 86401 | | - ** read the rowid field. |
| 86402 | | - */ |
| 86403 | | - sqlite3TreeViewLine(pView, "%s(%d)", |
| 86404 | | - pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); |
| 86405 | | - break; |
| 86406 | | - } |
| 86407 | | - case TK_CASE: { |
| 86408 | | - sqlite3TreeViewLine(pView, "CASE"); |
| 86409 | | - sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 86410 | | - sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 86411 | | - break; |
| 86412 | | - } |
| 86413 | | -#ifndef SQLITE_OMIT_TRIGGER |
| 86414 | | - case TK_RAISE: { |
| 86415 | | - const char *zType = "unk"; |
| 86416 | | - switch( pExpr->affinity ){ |
| 86417 | | - case OE_Rollback: zType = "rollback"; break; |
| 86418 | | - case OE_Abort: zType = "abort"; break; |
| 86419 | | - case OE_Fail: zType = "fail"; break; |
| 86420 | | - case OE_Ignore: zType = "ignore"; break; |
| 86421 | | - } |
| 86422 | | - sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); |
| 86423 | | - break; |
| 86424 | | - } |
| 86425 | | -#endif |
| 86426 | | - default: { |
| 86427 | | - sqlite3TreeViewLine(pView, "op=%d", pExpr->op); |
| 86428 | | - break; |
| 86429 | | - } |
| 86430 | | - } |
| 86431 | | - if( zBinOp ){ |
| 86432 | | - sqlite3TreeViewLine(pView, "%s", zBinOp); |
| 86433 | | - sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 86434 | | - sqlite3TreeViewExpr(pView, pExpr->pRight, 0); |
| 86435 | | - }else if( zUniOp ){ |
| 86436 | | - sqlite3TreeViewLine(pView, "%s", zUniOp); |
| 86437 | | - sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 86438 | | - } |
| 86439 | | - sqlite3TreeViewPop(pView); |
| 86440 | | -} |
| 86441 | | -#endif /* SQLITE_DEBUG */ |
| 86442 | | - |
| 86443 | | -#ifdef SQLITE_DEBUG |
| 86444 | | -/* |
| 86445 | | -** Generate a human-readable explanation of an expression list. |
| 86446 | | -*/ |
| 86447 | | -SQLITE_PRIVATE void sqlite3TreeViewExprList( |
| 86448 | | - TreeView *pView, |
| 86449 | | - const ExprList *pList, |
| 86450 | | - u8 moreToFollow, |
| 86451 | | - const char *zLabel |
| 86452 | | -){ |
| 86453 | | - int i; |
| 86454 | | - pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 86455 | | - if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST"; |
| 86456 | | - if( pList==0 ){ |
| 86457 | | - sqlite3TreeViewLine(pView, "%s (empty)", zLabel); |
| 86458 | | - }else{ |
| 86459 | | - sqlite3TreeViewLine(pView, "%s", zLabel); |
| 86460 | | - for(i=0; i<pList->nExpr; i++){ |
| 86461 | | - sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1); |
| 86462 | | -#if 0 |
| 86463 | | - if( pList->a[i].zName ){ |
| 86464 | | - sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName); |
| 86465 | | - } |
| 86466 | | - if( pList->a[i].bSpanIsTab ){ |
| 86467 | | - sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan); |
| 86468 | | - } |
| 86469 | | -#endif |
| 86470 | | - } |
| 86471 | | - } |
| 86472 | | - sqlite3TreeViewPop(pView); |
| 86473 | | -} |
| 86474 | | -#endif /* SQLITE_DEBUG */ |
| 86475 | | - |
| 86476 | 87141 | /* |
| 86477 | 87142 | ** Generate code that pushes the value of every element of the given |
| 86478 | 87143 | ** expression list into a sequence of registers beginning at target. |
| 86479 | 87144 | ** |
| 86480 | 87145 | ** Return the number of elements evaluated. |
| | @@ -86861,10 +87526,25 @@ |
| 86861 | 87526 | } |
| 86862 | 87527 | } |
| 86863 | 87528 | sqlite3ReleaseTempReg(pParse, regFree1); |
| 86864 | 87529 | sqlite3ReleaseTempReg(pParse, regFree2); |
| 86865 | 87530 | } |
| 87531 | + |
| 87532 | +/* |
| 87533 | +** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before |
| 87534 | +** code generation, and that copy is deleted after code generation. This |
| 87535 | +** ensures that the original pExpr is unchanged. |
| 87536 | +*/ |
| 87537 | +SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){ |
| 87538 | + sqlite3 *db = pParse->db; |
| 87539 | + Expr *pCopy = sqlite3ExprDup(db, pExpr, 0); |
| 87540 | + if( db->mallocFailed==0 ){ |
| 87541 | + sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull); |
| 87542 | + } |
| 87543 | + sqlite3ExprDelete(db, pCopy); |
| 87544 | +} |
| 87545 | + |
| 86866 | 87546 | |
| 86867 | 87547 | /* |
| 86868 | 87548 | ** Do a deep comparison of two expression trees. Return 0 if the two |
| 86869 | 87549 | ** expressions are completely identical. Return 1 if they differ only |
| 86870 | 87550 | ** by a COLLATE operator at the top level. Return 2 if there are differences |
| | @@ -88014,11 +88694,11 @@ |
| 88014 | 88694 | ** can handle (i.e. not CURRENT_TIME etc.) |
| 88015 | 88695 | */ |
| 88016 | 88696 | if( pDflt ){ |
| 88017 | 88697 | sqlite3_value *pVal = 0; |
| 88018 | 88698 | int rc; |
| 88019 | | - rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal); |
| 88699 | + rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal); |
| 88020 | 88700 | assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| 88021 | 88701 | if( rc!=SQLITE_OK ){ |
| 88022 | 88702 | db->mallocFailed = 1; |
| 88023 | 88703 | return; |
| 88024 | 88704 | } |
| | @@ -89099,11 +89779,11 @@ |
| 89099 | 89779 | #elif SQLITE_DEBUG |
| 89100 | 89780 | assert( iParam==STAT_GET_STAT1 ); |
| 89101 | 89781 | #else |
| 89102 | 89782 | UNUSED_PARAMETER( iParam ); |
| 89103 | 89783 | #endif |
| 89104 | | - sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4, regOut); |
| 89784 | + sqlite3VdbeAddOp3(v, OP_Function0, 0, regStat4, regOut); |
| 89105 | 89785 | sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF); |
| 89106 | 89786 | sqlite3VdbeChangeP5(v, 1 + IsStat34); |
| 89107 | 89787 | } |
| 89108 | 89788 | |
| 89109 | 89789 | /* |
| | @@ -89254,11 +89934,11 @@ |
| 89254 | 89934 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 89255 | 89935 | sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3); |
| 89256 | 89936 | #endif |
| 89257 | 89937 | sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1); |
| 89258 | 89938 | sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2); |
| 89259 | | - sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4); |
| 89939 | + sqlite3VdbeAddOp3(v, OP_Function0, 0, regStat4+1, regStat4); |
| 89260 | 89940 | sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF); |
| 89261 | 89941 | sqlite3VdbeChangeP5(v, 2+IsStat34); |
| 89262 | 89942 | |
| 89263 | 89943 | /* Implementation of the following: |
| 89264 | 89944 | ** |
| | @@ -89350,11 +90030,11 @@ |
| 89350 | 90030 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid); |
| 89351 | 90031 | sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol); |
| 89352 | 90032 | } |
| 89353 | 90033 | #endif |
| 89354 | 90034 | assert( regChng==(regStat4+1) ); |
| 89355 | | - sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp); |
| 90035 | + sqlite3VdbeAddOp3(v, OP_Function0, 1, regStat4, regTemp); |
| 89356 | 90036 | sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF); |
| 89357 | 90037 | sqlite3VdbeChangeP5(v, 2+IsStat34); |
| 89358 | 90038 | sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v); |
| 89359 | 90039 | |
| 89360 | 90040 | /* Add the entry to the stat1 table. */ |
| | @@ -90409,11 +91089,11 @@ |
| 90409 | 91089 | sqlite3ExprCode(pParse, pDbname, regArgs+1); |
| 90410 | 91090 | sqlite3ExprCode(pParse, pKey, regArgs+2); |
| 90411 | 91091 | |
| 90412 | 91092 | assert( v || db->mallocFailed ); |
| 90413 | 91093 | if( v ){ |
| 90414 | | - sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3); |
| 91094 | + sqlite3VdbeAddOp3(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3); |
| 90415 | 91095 | assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg ); |
| 90416 | 91096 | sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg)); |
| 90417 | 91097 | sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF); |
| 90418 | 91098 | |
| 90419 | 91099 | /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this |
| | @@ -91874,11 +92554,11 @@ |
| 91874 | 92554 | */ |
| 91875 | 92555 | if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ |
| 91876 | 92556 | int j1; |
| 91877 | 92557 | int fileFormat; |
| 91878 | 92558 | int reg1, reg2, reg3; |
| 91879 | | - sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 92559 | + sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 91880 | 92560 | |
| 91881 | 92561 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 91882 | 92562 | if( isVirtual ){ |
| 91883 | 92563 | sqlite3VdbeAddOp0(v, OP_VBegin); |
| 91884 | 92564 | } |
| | @@ -91990,14 +92670,14 @@ |
| 91990 | 92670 | pCol = &p->aCol[p->nCol]; |
| 91991 | 92671 | memset(pCol, 0, sizeof(p->aCol[0])); |
| 91992 | 92672 | pCol->zName = z; |
| 91993 | 92673 | |
| 91994 | 92674 | /* If there is no type specified, columns have the default affinity |
| 91995 | | - ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will |
| 92675 | + ** 'BLOB'. If there is a type specified, then sqlite3AddColumnType() will |
| 91996 | 92676 | ** be called next to set pCol->affinity correctly. |
| 91997 | 92677 | */ |
| 91998 | | - pCol->affinity = SQLITE_AFF_NONE; |
| 92678 | + pCol->affinity = SQLITE_AFF_BLOB; |
| 91999 | 92679 | pCol->szEst = 1; |
| 92000 | 92680 | p->nCol++; |
| 92001 | 92681 | } |
| 92002 | 92682 | |
| 92003 | 92683 | /* |
| | @@ -92028,11 +92708,11 @@ |
| 92028 | 92708 | ** -------------------------------- |
| 92029 | 92709 | ** 'INT' | SQLITE_AFF_INTEGER |
| 92030 | 92710 | ** 'CHAR' | SQLITE_AFF_TEXT |
| 92031 | 92711 | ** 'CLOB' | SQLITE_AFF_TEXT |
| 92032 | 92712 | ** 'TEXT' | SQLITE_AFF_TEXT |
| 92033 | | -** 'BLOB' | SQLITE_AFF_NONE |
| 92713 | +** 'BLOB' | SQLITE_AFF_BLOB |
| 92034 | 92714 | ** 'REAL' | SQLITE_AFF_REAL |
| 92035 | 92715 | ** 'FLOA' | SQLITE_AFF_REAL |
| 92036 | 92716 | ** 'DOUB' | SQLITE_AFF_REAL |
| 92037 | 92717 | ** |
| 92038 | 92718 | ** If none of the substrings in the above table are found, |
| | @@ -92054,11 +92734,11 @@ |
| 92054 | 92734 | aff = SQLITE_AFF_TEXT; |
| 92055 | 92735 | }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ |
| 92056 | 92736 | aff = SQLITE_AFF_TEXT; |
| 92057 | 92737 | }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */ |
| 92058 | 92738 | && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){ |
| 92059 | | - aff = SQLITE_AFF_NONE; |
| 92739 | + aff = SQLITE_AFF_BLOB; |
| 92060 | 92740 | if( zIn[0]=='(' ) zChar = zIn; |
| 92061 | 92741 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 92062 | 92742 | }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */ |
| 92063 | 92743 | && aff==SQLITE_AFF_NUMERIC ){ |
| 92064 | 92744 | aff = SQLITE_AFF_REAL; |
| | @@ -92446,11 +93126,11 @@ |
| 92446 | 93126 | k = sqlite3Strlen30(zStmt); |
| 92447 | 93127 | identPut(zStmt, &k, p->zName); |
| 92448 | 93128 | zStmt[k++] = '('; |
| 92449 | 93129 | for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ |
| 92450 | 93130 | static const char * const azType[] = { |
| 92451 | | - /* SQLITE_AFF_NONE */ "", |
| 93131 | + /* SQLITE_AFF_BLOB */ "", |
| 92452 | 93132 | /* SQLITE_AFF_TEXT */ " TEXT", |
| 92453 | 93133 | /* SQLITE_AFF_NUMERIC */ " NUM", |
| 92454 | 93134 | /* SQLITE_AFF_INTEGER */ " INT", |
| 92455 | 93135 | /* SQLITE_AFF_REAL */ " REAL" |
| 92456 | 93136 | }; |
| | @@ -92459,21 +93139,21 @@ |
| 92459 | 93139 | |
| 92460 | 93140 | sqlite3_snprintf(n-k, &zStmt[k], zSep); |
| 92461 | 93141 | k += sqlite3Strlen30(&zStmt[k]); |
| 92462 | 93142 | zSep = zSep2; |
| 92463 | 93143 | identPut(zStmt, &k, pCol->zName); |
| 92464 | | - assert( pCol->affinity-SQLITE_AFF_NONE >= 0 ); |
| 92465 | | - assert( pCol->affinity-SQLITE_AFF_NONE < ArraySize(azType) ); |
| 92466 | | - testcase( pCol->affinity==SQLITE_AFF_NONE ); |
| 93144 | + assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 ); |
| 93145 | + assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) ); |
| 93146 | + testcase( pCol->affinity==SQLITE_AFF_BLOB ); |
| 92467 | 93147 | testcase( pCol->affinity==SQLITE_AFF_TEXT ); |
| 92468 | 93148 | testcase( pCol->affinity==SQLITE_AFF_NUMERIC ); |
| 92469 | 93149 | testcase( pCol->affinity==SQLITE_AFF_INTEGER ); |
| 92470 | 93150 | testcase( pCol->affinity==SQLITE_AFF_REAL ); |
| 92471 | 93151 | |
| 92472 | | - zType = azType[pCol->affinity - SQLITE_AFF_NONE]; |
| 93152 | + zType = azType[pCol->affinity - SQLITE_AFF_BLOB]; |
| 92473 | 93153 | len = sqlite3Strlen30(zType); |
| 92474 | | - assert( pCol->affinity==SQLITE_AFF_NONE |
| 93154 | + assert( pCol->affinity==SQLITE_AFF_BLOB |
| 92475 | 93155 | || pCol->affinity==sqlite3AffinityType(zType, 0) ); |
| 92476 | 93156 | memcpy(&zStmt[k], zType, len); |
| 92477 | 93157 | k += len; |
| 92478 | 93158 | assert( k<=n ); |
| 92479 | 93159 | } |
| | @@ -92822,10 +93502,11 @@ |
| 92822 | 93502 | |
| 92823 | 93503 | regYield = ++pParse->nMem; |
| 92824 | 93504 | regRec = ++pParse->nMem; |
| 92825 | 93505 | regRowid = ++pParse->nMem; |
| 92826 | 93506 | assert(pParse->nTab==1); |
| 93507 | + sqlite3MayAbort(pParse); |
| 92827 | 93508 | sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); |
| 92828 | 93509 | sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); |
| 92829 | 93510 | pParse->nTab = 2; |
| 92830 | 93511 | addrTop = sqlite3VdbeCurrentAddr(v) + 1; |
| 92831 | 93512 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); |
| | @@ -94599,11 +95280,11 @@ |
| 94599 | 95280 | if( pList==0 ) return; |
| 94600 | 95281 | for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ |
| 94601 | 95282 | sqlite3DbFree(db, pItem->zDatabase); |
| 94602 | 95283 | sqlite3DbFree(db, pItem->zName); |
| 94603 | 95284 | sqlite3DbFree(db, pItem->zAlias); |
| 94604 | | - sqlite3DbFree(db, pItem->zIndex); |
| 95285 | + sqlite3DbFree(db, pItem->zIndexedBy); |
| 94605 | 95286 | sqlite3DeleteTable(db, pItem->pTab); |
| 94606 | 95287 | sqlite3SelectDelete(db, pItem->pSelect); |
| 94607 | 95288 | sqlite3ExprDelete(db, pItem->pOn); |
| 94608 | 95289 | sqlite3IdListDelete(db, pItem->pUsing); |
| 94609 | 95290 | } |
| | @@ -94672,17 +95353,17 @@ |
| 94672 | 95353 | */ |
| 94673 | 95354 | SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){ |
| 94674 | 95355 | assert( pIndexedBy!=0 ); |
| 94675 | 95356 | if( p && ALWAYS(p->nSrc>0) ){ |
| 94676 | 95357 | struct SrcList_item *pItem = &p->a[p->nSrc-1]; |
| 94677 | | - assert( pItem->notIndexed==0 && pItem->zIndex==0 ); |
| 95358 | + assert( pItem->notIndexed==0 && pItem->zIndexedBy==0 ); |
| 94678 | 95359 | if( pIndexedBy->n==1 && !pIndexedBy->z ){ |
| 94679 | 95360 | /* A "NOT INDEXED" clause was supplied. See parse.y |
| 94680 | 95361 | ** construct "indexed_opt" for details. */ |
| 94681 | 95362 | pItem->notIndexed = 1; |
| 94682 | 95363 | }else{ |
| 94683 | | - pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy); |
| 95364 | + pItem->zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy); |
| 94684 | 95365 | } |
| 94685 | 95366 | } |
| 94686 | 95367 | } |
| 94687 | 95368 | |
| 94688 | 95369 | /* |
| | @@ -96502,12 +97183,12 @@ |
| 96502 | 97183 | if( piPartIdxLabel ){ |
| 96503 | 97184 | if( pIdx->pPartIdxWhere ){ |
| 96504 | 97185 | *piPartIdxLabel = sqlite3VdbeMakeLabel(v); |
| 96505 | 97186 | pParse->iPartIdxTab = iDataCur; |
| 96506 | 97187 | sqlite3ExprCachePush(pParse); |
| 96507 | | - sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, |
| 96508 | | - SQLITE_JUMPIFNULL); |
| 97188 | + sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, |
| 97189 | + SQLITE_JUMPIFNULL); |
| 96509 | 97190 | }else{ |
| 96510 | 97191 | *piPartIdxLabel = 0; |
| 96511 | 97192 | } |
| 96512 | 97193 | } |
| 96513 | 97194 | nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn; |
| | @@ -97119,21 +97800,19 @@ |
| 97119 | 97800 | u8 noCase; |
| 97120 | 97801 | }; |
| 97121 | 97802 | |
| 97122 | 97803 | /* |
| 97123 | 97804 | ** For LIKE and GLOB matching on EBCDIC machines, assume that every |
| 97124 | | -** character is exactly one byte in size. Also, all characters are |
| 97125 | | -** able to participate in upper-case-to-lower-case mappings in EBCDIC |
| 97126 | | -** whereas only characters less than 0x80 do in ASCII. |
| 97805 | +** character is exactly one byte in size. Also, provde the Utf8Read() |
| 97806 | +** macro for fast reading of the next character in the common case where |
| 97807 | +** the next character is ASCII. |
| 97127 | 97808 | */ |
| 97128 | 97809 | #if defined(SQLITE_EBCDIC) |
| 97129 | 97810 | # define sqlite3Utf8Read(A) (*((*A)++)) |
| 97130 | | -# define GlobUpperToLower(A) A = sqlite3UpperToLower[A] |
| 97131 | | -# define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A] |
| 97811 | +# define Utf8Read(A) (*(A++)) |
| 97132 | 97812 | #else |
| 97133 | | -# define GlobUpperToLower(A) if( A<=0x7f ){ A = sqlite3UpperToLower[A]; } |
| 97134 | | -# define GlobUpperToLowerAscii(A) A = sqlite3UpperToLower[A] |
| 97813 | +# define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A)) |
| 97135 | 97814 | #endif |
| 97136 | 97815 | |
| 97137 | 97816 | static const struct compareInfo globInfo = { '*', '?', '[', 0 }; |
| 97138 | 97817 | /* The correct SQL-92 behavior is for the LIKE operator to ignore |
| 97139 | 97818 | ** case. Thus 'a' LIKE 'A' would be true. */ |
| | @@ -97171,11 +97850,11 @@ |
| 97171 | 97850 | *** '_' Matches any one character |
| 97172 | 97851 | ** |
| 97173 | 97852 | ** Ec Where E is the "esc" character and c is any other |
| 97174 | 97853 | ** character, including '%', '_', and esc, match exactly c. |
| 97175 | 97854 | ** |
| 97176 | | -** The comments through this routine usually assume glob matching. |
| 97855 | +** The comments within this routine usually assume glob matching. |
| 97177 | 97856 | ** |
| 97178 | 97857 | ** This routine is usually quick, but can be N**2 in the worst case. |
| 97179 | 97858 | */ |
| 97180 | 97859 | static int patternCompare( |
| 97181 | 97860 | const u8 *zPattern, /* The glob pattern */ |
| | @@ -97195,17 +97874,16 @@ |
| 97195 | 97874 | ** the other, never both. Hence the single variable matchOther is used |
| 97196 | 97875 | ** to store the one we have to look for. |
| 97197 | 97876 | */ |
| 97198 | 97877 | matchOther = esc ? esc : pInfo->matchSet; |
| 97199 | 97878 | |
| 97200 | | - while( (c = sqlite3Utf8Read(&zPattern))!=0 ){ |
| 97879 | + while( (c = Utf8Read(zPattern))!=0 ){ |
| 97201 | 97880 | if( c==matchAll ){ /* Match "*" */ |
| 97202 | 97881 | /* Skip over multiple "*" characters in the pattern. If there |
| 97203 | 97882 | ** are also "?" characters, skip those as well, but consume a |
| 97204 | 97883 | ** single character of the input string for each "?" skipped */ |
| 97205 | | - while( (c=sqlite3Utf8Read(&zPattern)) == matchAll |
| 97206 | | - || c == matchOne ){ |
| 97884 | + while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){ |
| 97207 | 97885 | if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){ |
| 97208 | 97886 | return 0; |
| 97209 | 97887 | } |
| 97210 | 97888 | } |
| 97211 | 97889 | if( c==0 ){ |
| | @@ -97246,11 +97924,11 @@ |
| 97246 | 97924 | while( (c2 = *(zString++))!=0 ){ |
| 97247 | 97925 | if( c2!=c && c2!=cx ) continue; |
| 97248 | 97926 | if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; |
| 97249 | 97927 | } |
| 97250 | 97928 | }else{ |
| 97251 | | - while( (c2 = sqlite3Utf8Read(&zString))!=0 ){ |
| 97929 | + while( (c2 = Utf8Read(zString))!=0 ){ |
| 97252 | 97930 | if( c2!=c ) continue; |
| 97253 | 97931 | if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; |
| 97254 | 97932 | } |
| 97255 | 97933 | } |
| 97256 | 97934 | return 0; |
| | @@ -97292,11 +97970,11 @@ |
| 97292 | 97970 | return 0; |
| 97293 | 97971 | } |
| 97294 | 97972 | continue; |
| 97295 | 97973 | } |
| 97296 | 97974 | } |
| 97297 | | - c2 = sqlite3Utf8Read(&zString); |
| 97975 | + c2 = Utf8Read(zString); |
| 97298 | 97976 | if( c==c2 ) continue; |
| 97299 | 97977 | if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){ |
| 97300 | 97978 | continue; |
| 97301 | 97979 | } |
| 97302 | 97980 | if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue; |
| | @@ -99806,11 +100484,11 @@ |
| 99806 | 100484 | ** pIdx. A column affinity string has one character for each column in |
| 99807 | 100485 | ** the table, according to the affinity of the column: |
| 99808 | 100486 | ** |
| 99809 | 100487 | ** Character Column affinity |
| 99810 | 100488 | ** ------------------------------ |
| 99811 | | -** 'A' NONE |
| 100489 | +** 'A' BLOB |
| 99812 | 100490 | ** 'B' TEXT |
| 99813 | 100491 | ** 'C' NUMERIC |
| 99814 | 100492 | ** 'D' INTEGER |
| 99815 | 100493 | ** 'F' REAL |
| 99816 | 100494 | ** |
| | @@ -99849,23 +100527,23 @@ |
| 99849 | 100527 | return pIdx->zColAff; |
| 99850 | 100528 | } |
| 99851 | 100529 | |
| 99852 | 100530 | /* |
| 99853 | 100531 | ** Compute the affinity string for table pTab, if it has not already been |
| 99854 | | -** computed. As an optimization, omit trailing SQLITE_AFF_NONE affinities. |
| 100532 | +** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities. |
| 99855 | 100533 | ** |
| 99856 | | -** If the affinity exists (if it is no entirely SQLITE_AFF_NONE values) and |
| 100534 | +** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and |
| 99857 | 100535 | ** if iReg>0 then code an OP_Affinity opcode that will set the affinities |
| 99858 | 100536 | ** for register iReg and following. Or if affinities exists and iReg==0, |
| 99859 | 100537 | ** then just set the P4 operand of the previous opcode (which should be |
| 99860 | 100538 | ** an OP_MakeRecord) to the affinity string. |
| 99861 | 100539 | ** |
| 99862 | 100540 | ** A column affinity string has one character per column: |
| 99863 | 100541 | ** |
| 99864 | 100542 | ** Character Column affinity |
| 99865 | 100543 | ** ------------------------------ |
| 99866 | | -** 'A' NONE |
| 100544 | +** 'A' BLOB |
| 99867 | 100545 | ** 'B' TEXT |
| 99868 | 100546 | ** 'C' NUMERIC |
| 99869 | 100547 | ** 'D' INTEGER |
| 99870 | 100548 | ** 'E' REAL |
| 99871 | 100549 | */ |
| | @@ -99883,11 +100561,11 @@ |
| 99883 | 100561 | for(i=0; i<pTab->nCol; i++){ |
| 99884 | 100562 | zColAff[i] = pTab->aCol[i].affinity; |
| 99885 | 100563 | } |
| 99886 | 100564 | do{ |
| 99887 | 100565 | zColAff[i--] = 0; |
| 99888 | | - }while( i>=0 && zColAff[i]==SQLITE_AFF_NONE ); |
| 100566 | + }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB ); |
| 99889 | 100567 | pTab->zColAff = zColAff; |
| 99890 | 100568 | } |
| 99891 | 100569 | i = sqlite3Strlen30(zColAff); |
| 99892 | 100570 | if( i ){ |
| 99893 | 100571 | if( iReg ){ |
| | @@ -101131,12 +101809,12 @@ |
| 101131 | 101809 | |
| 101132 | 101810 | /* Skip partial indices for which the WHERE clause is not true */ |
| 101133 | 101811 | if( pIdx->pPartIdxWhere ){ |
| 101134 | 101812 | sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]); |
| 101135 | 101813 | pParse->ckBase = regNewData+1; |
| 101136 | | - sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, addrUniqueOk, |
| 101137 | | - SQLITE_JUMPIFNULL); |
| 101814 | + sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk, |
| 101815 | + SQLITE_JUMPIFNULL); |
| 101138 | 101816 | pParse->ckBase = 0; |
| 101139 | 101817 | } |
| 101140 | 101818 | |
| 101141 | 101819 | /* Create a record for this index entry as it should appear after |
| 101142 | 101820 | ** the insert or update. Store that record in the aRegIdx[ix] register |
| | @@ -102242,11 +102920,12 @@ |
| 102242 | 102920 | void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64, |
| 102243 | 102921 | void(*)(void*)); |
| 102244 | 102922 | void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64, |
| 102245 | 102923 | void(*)(void*), unsigned char); |
| 102246 | 102924 | int (*strglob)(const char*,const char*); |
| 102247 | | - sqlite3_value (*value_dup)(const sqlite3_value*); |
| 102925 | + /* Version 3.8.11 and later */ |
| 102926 | + sqlite3_value *(*value_dup)(const sqlite3_value*); |
| 102248 | 102927 | void (*value_free)(sqlite3_value*); |
| 102249 | 102928 | }; |
| 102250 | 102929 | |
| 102251 | 102930 | /* |
| 102252 | 102931 | ** The following macros redefine the API routines so that they are |
| | @@ -102882,11 +103561,14 @@ |
| 102882 | 103561 | sqlite3_msize, |
| 102883 | 103562 | sqlite3_realloc64, |
| 102884 | 103563 | sqlite3_reset_auto_extension, |
| 102885 | 103564 | sqlite3_result_blob64, |
| 102886 | 103565 | sqlite3_result_text64, |
| 102887 | | - sqlite3_strglob |
| 103566 | + sqlite3_strglob, |
| 103567 | + /* Version 3.8.11 and later */ |
| 103568 | + (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup, |
| 103569 | + sqlite3_value_free |
| 102888 | 103570 | }; |
| 102889 | 103571 | |
| 102890 | 103572 | /* |
| 102891 | 103573 | ** Attempt to load an SQLite extension library contained in the file |
| 102892 | 103574 | ** zFile. The entry point is zProc. zProc may be 0 in which case a |
| | @@ -106617,11 +107299,12 @@ |
| 106617 | 107299 | */ |
| 106618 | 107300 | #if SELECTTRACE_ENABLED |
| 106619 | 107301 | /***/ int sqlite3SelectTrace = 0; |
| 106620 | 107302 | # define SELECTTRACE(K,P,S,X) \ |
| 106621 | 107303 | if(sqlite3SelectTrace&(K)) \ |
| 106622 | | - sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",(S)->zSelName,(S)),\ |
| 107304 | + sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\ |
| 107305 | + (S)->zSelName,(S)),\ |
| 106623 | 107306 | sqlite3DebugPrintf X |
| 106624 | 107307 | #else |
| 106625 | 107308 | # define SELECTTRACE(K,P,S,X) |
| 106626 | 107309 | #endif |
| 106627 | 107310 | |
| | @@ -106961,10 +107644,16 @@ |
| 106961 | 107644 | while( p ){ |
| 106962 | 107645 | ExprSetProperty(p, EP_FromJoin); |
| 106963 | 107646 | assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); |
| 106964 | 107647 | ExprSetVVAProperty(p, EP_NoReduce); |
| 106965 | 107648 | p->iRightJoinTable = (i16)iTable; |
| 107649 | + if( p->op==TK_FUNCTION && p->x.pList ){ |
| 107650 | + int i; |
| 107651 | + for(i=0; i<p->x.pList->nExpr; i++){ |
| 107652 | + setJoinExpr(p->x.pList->a[i].pExpr, iTable); |
| 107653 | + } |
| 107654 | + } |
| 106966 | 107655 | setJoinExpr(p->pLeft, iTable); |
| 106967 | 107656 | p = p->pRight; |
| 106968 | 107657 | } |
| 106969 | 107658 | } |
| 106970 | 107659 | |
| | @@ -107370,11 +108059,12 @@ |
| 107370 | 108059 | break; |
| 107371 | 108060 | } |
| 107372 | 108061 | |
| 107373 | 108062 | default: { |
| 107374 | 108063 | assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED ); |
| 107375 | | - codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol, regResult); |
| 108064 | + codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol, |
| 108065 | + regResult); |
| 107376 | 108066 | break; |
| 107377 | 108067 | } |
| 107378 | 108068 | } |
| 107379 | 108069 | if( pSort==0 ){ |
| 107380 | 108070 | codeOffset(v, p->iOffset, iContinue); |
| | @@ -107423,11 +108113,12 @@ |
| 107423 | 108113 | ** on an ephemeral index. If the current row is already present |
| 107424 | 108114 | ** in the index, do not write it to the output. If not, add the |
| 107425 | 108115 | ** current row to the index and proceed with writing it to the |
| 107426 | 108116 | ** output table as well. */ |
| 107427 | 108117 | int addr = sqlite3VdbeCurrentAddr(v) + 4; |
| 107428 | | - sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); VdbeCoverage(v); |
| 108118 | + sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); |
| 108119 | + VdbeCoverage(v); |
| 107429 | 108120 | sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1); |
| 107430 | 108121 | assert( pSort==0 ); |
| 107431 | 108122 | } |
| 107432 | 108123 | #endif |
| 107433 | 108124 | if( pSort ){ |
| | @@ -107906,32 +108597,31 @@ |
| 107906 | 108597 | ** This routine has either 3 or 6 parameters depending on whether or not |
| 107907 | 108598 | ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used. |
| 107908 | 108599 | */ |
| 107909 | 108600 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 107910 | 108601 | # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F) |
| 108602 | +#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */ |
| 108603 | +# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F) |
| 108604 | +#endif |
| 107911 | 108605 | static const char *columnTypeImpl( |
| 107912 | 108606 | NameContext *pNC, |
| 107913 | 108607 | Expr *pExpr, |
| 108608 | +#ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 107914 | 108609 | const char **pzOrigDb, |
| 107915 | 108610 | const char **pzOrigTab, |
| 107916 | 108611 | const char **pzOrigCol, |
| 108612 | +#endif |
| 107917 | 108613 | u8 *pEstWidth |
| 107918 | 108614 | ){ |
| 108615 | + char const *zType = 0; |
| 108616 | + int j; |
| 108617 | + u8 estWidth = 1; |
| 108618 | +#ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 107919 | 108619 | char const *zOrigDb = 0; |
| 107920 | 108620 | char const *zOrigTab = 0; |
| 107921 | 108621 | char const *zOrigCol = 0; |
| 107922 | | -#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */ |
| 107923 | | -# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F) |
| 107924 | | -static const char *columnTypeImpl( |
| 107925 | | - NameContext *pNC, |
| 107926 | | - Expr *pExpr, |
| 107927 | | - u8 *pEstWidth |
| 107928 | | -){ |
| 107929 | | -#endif /* !defined(SQLITE_ENABLE_COLUMN_METADATA) */ |
| 107930 | | - char const *zType = 0; |
| 107931 | | - int j; |
| 107932 | | - u8 estWidth = 1; |
| 108622 | +#endif |
| 107933 | 108623 | |
| 107934 | 108624 | if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0; |
| 107935 | 108625 | switch( pExpr->op ){ |
| 107936 | 108626 | case TK_AGG_COLUMN: |
| 107937 | 108627 | case TK_COLUMN: { |
| | @@ -107980,14 +108670,17 @@ |
| 107980 | 108670 | if( pS ){ |
| 107981 | 108671 | /* The "table" is actually a sub-select or a view in the FROM clause |
| 107982 | 108672 | ** of the SELECT statement. Return the declaration type and origin |
| 107983 | 108673 | ** data for the result-set column of the sub-select. |
| 107984 | 108674 | */ |
| 107985 | | - if( iCol>=0 && iCol<pS->pEList->nExpr ){ |
| 108675 | + if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){ |
| 107986 | 108676 | /* If iCol is less than zero, then the expression requests the |
| 107987 | 108677 | ** rowid of the sub-select or view. This expression is legal (see |
| 107988 | 108678 | ** test case misc2.2.2) - it always evaluates to NULL. |
| 108679 | + ** |
| 108680 | + ** The ALWAYS() is because iCol>=pS->pEList->nExpr will have been |
| 108681 | + ** caught already by name resolution. |
| 107989 | 108682 | */ |
| 107990 | 108683 | NameContext sNC; |
| 107991 | 108684 | Expr *p = pS->pEList->a[iCol].pExpr; |
| 107992 | 108685 | sNC.pSrcList = pS->pSrc; |
| 107993 | 108686 | sNC.pNext = pNC; |
| | @@ -108301,15 +108994,16 @@ |
| 108301 | 108994 | sNC.pSrcList = pSelect->pSrc; |
| 108302 | 108995 | a = pSelect->pEList->a; |
| 108303 | 108996 | for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){ |
| 108304 | 108997 | p = a[i].pExpr; |
| 108305 | 108998 | if( pCol->zType==0 ){ |
| 108306 | | - pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst)); |
| 108999 | + pCol->zType = sqlite3DbStrDup(db, |
| 109000 | + columnType(&sNC, p,0,0,0, &pCol->szEst)); |
| 108307 | 109001 | } |
| 108308 | 109002 | szAll += pCol->szEst; |
| 108309 | 109003 | pCol->affinity = sqlite3ExprAffinity(p); |
| 108310 | | - if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE; |
| 109004 | + if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB; |
| 108311 | 109005 | pColl = sqlite3ExprCollSeq(pParse, p); |
| 108312 | 109006 | if( pColl && pCol->zColl==0 ){ |
| 108313 | 109007 | pCol->zColl = sqlite3DbStrDup(db, pColl->zName); |
| 108314 | 109008 | } |
| 108315 | 109009 | } |
| | @@ -108461,11 +109155,14 @@ |
| 108461 | 109155 | pRet = multiSelectCollSeq(pParse, p->pPrior, iCol); |
| 108462 | 109156 | }else{ |
| 108463 | 109157 | pRet = 0; |
| 108464 | 109158 | } |
| 108465 | 109159 | assert( iCol>=0 ); |
| 108466 | | - if( pRet==0 && iCol<p->pEList->nExpr ){ |
| 109160 | + /* iCol must be less than p->pEList->nExpr. Otherwise an error would |
| 109161 | + ** have been thrown during name resolution and we would not have gotten |
| 109162 | + ** this far */ |
| 109163 | + if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){ |
| 108467 | 109164 | pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr); |
| 108468 | 109165 | } |
| 108469 | 109166 | return pRet; |
| 108470 | 109167 | } |
| 108471 | 109168 | |
| | @@ -108680,11 +109377,11 @@ |
| 108680 | 109377 | |
| 108681 | 109378 | /* |
| 108682 | 109379 | ** Error message for when two or more terms of a compound select have different |
| 108683 | 109380 | ** size result sets. |
| 108684 | 109381 | */ |
| 108685 | | -static void selectWrongNumTermsError(Parse *pParse, Select *p){ |
| 109382 | +SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){ |
| 108686 | 109383 | if( p->selFlags & SF_Values ){ |
| 108687 | 109384 | sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms"); |
| 108688 | 109385 | }else{ |
| 108689 | 109386 | sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s" |
| 108690 | 109387 | " do not have the same number of result columns", selectOpName(p->op)); |
| | @@ -108706,23 +109403,19 @@ |
| 108706 | 109403 | Parse *pParse, /* Parsing context */ |
| 108707 | 109404 | Select *p, /* The right-most of SELECTs to be coded */ |
| 108708 | 109405 | SelectDest *pDest /* What to do with query results */ |
| 108709 | 109406 | ){ |
| 108710 | 109407 | Select *pPrior; |
| 108711 | | - int nExpr = p->pEList->nExpr; |
| 108712 | 109408 | int nRow = 1; |
| 108713 | 109409 | int rc = 0; |
| 108714 | 109410 | assert( p->selFlags & SF_MultiValue ); |
| 108715 | 109411 | do{ |
| 108716 | 109412 | assert( p->selFlags & SF_Values ); |
| 108717 | 109413 | assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) ); |
| 108718 | 109414 | assert( p->pLimit==0 ); |
| 108719 | 109415 | assert( p->pOffset==0 ); |
| 108720 | | - if( p->pEList->nExpr!=nExpr ){ |
| 108721 | | - selectWrongNumTermsError(pParse, p); |
| 108722 | | - return 1; |
| 108723 | | - } |
| 109416 | + assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr ); |
| 108724 | 109417 | if( p->pPrior==0 ) break; |
| 108725 | 109418 | assert( p->pPrior->pNext==p ); |
| 108726 | 109419 | p = p->pPrior; |
| 108727 | 109420 | nRow++; |
| 108728 | 109421 | }while(1); |
| | @@ -108827,15 +109520,11 @@ |
| 108827 | 109520 | |
| 108828 | 109521 | /* Make sure all SELECTs in the statement have the same number of elements |
| 108829 | 109522 | ** in their result sets. |
| 108830 | 109523 | */ |
| 108831 | 109524 | assert( p->pEList && pPrior->pEList ); |
| 108832 | | - if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ |
| 108833 | | - selectWrongNumTermsError(pParse, p); |
| 108834 | | - rc = 1; |
| 108835 | | - goto multi_select_end; |
| 108836 | | - } |
| 109525 | + assert( p->pEList->nExpr==pPrior->pEList->nExpr ); |
| 108837 | 109526 | |
| 108838 | 109527 | #ifndef SQLITE_OMIT_CTE |
| 108839 | 109528 | if( p->selFlags & SF_Recursive ){ |
| 108840 | 109529 | generateWithRecursiveQuery(pParse, p, &dest); |
| 108841 | 109530 | }else |
| | @@ -109450,13 +110139,11 @@ |
| 109450 | 110139 | aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy); |
| 109451 | 110140 | if( aPermute ){ |
| 109452 | 110141 | struct ExprList_item *pItem; |
| 109453 | 110142 | for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){ |
| 109454 | 110143 | assert( pItem->u.x.iOrderByCol>0 ); |
| 109455 | | - /* assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr ) is also true |
| 109456 | | - ** but only for well-formed SELECT statements. */ |
| 109457 | | - testcase( pItem->u.x.iOrderByCol > p->pEList->nExpr ); |
| 110144 | + assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr ); |
| 109458 | 110145 | aPermute[i] = pItem->u.x.iOrderByCol - 1; |
| 109459 | 110146 | } |
| 109460 | 110147 | pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1); |
| 109461 | 110148 | }else{ |
| 109462 | 110149 | pKeyMerge = 0; |
| | @@ -109811,12 +110498,12 @@ |
| 109811 | 110498 | ** (9) The subquery does not use LIMIT or the outer query does not use |
| 109812 | 110499 | ** aggregates. |
| 109813 | 110500 | ** |
| 109814 | 110501 | ** (**) Restriction (10) was removed from the code on 2005-02-05 but we |
| 109815 | 110502 | ** accidently carried the comment forward until 2014-09-15. Original |
| 109816 | | -** text: "The subquery does not use aggregates or the outer query does not |
| 109817 | | -** use LIMIT." |
| 110503 | +** text: "The subquery does not use aggregates or the outer query |
| 110504 | +** does not use LIMIT." |
| 109818 | 110505 | ** |
| 109819 | 110506 | ** (11) The subquery and the outer query do not both have ORDER BY clauses. |
| 109820 | 110507 | ** |
| 109821 | 110508 | ** (**) Not implemented. Subsumed into restriction (3). Was previously |
| 109822 | 110509 | ** a separate restriction deriving from ticket #350. |
| | @@ -110022,14 +110709,14 @@ |
| 110022 | 110709 | } |
| 110023 | 110710 | for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){ |
| 110024 | 110711 | testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); |
| 110025 | 110712 | testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); |
| 110026 | 110713 | assert( pSub->pSrc!=0 ); |
| 110714 | + assert( pSub->pEList->nExpr==pSub1->pEList->nExpr ); |
| 110027 | 110715 | if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 |
| 110028 | 110716 | || (pSub1->pPrior && pSub1->op!=TK_ALL) |
| 110029 | 110717 | || pSub1->pSrc->nSrc<1 |
| 110030 | | - || pSub->pEList->nExpr!=pSub1->pEList->nExpr |
| 110031 | 110718 | ){ |
| 110032 | 110719 | return 0; |
| 110033 | 110720 | } |
| 110034 | 110721 | testcase( pSub1->pSrc->nSrc>1 ); |
| 110035 | 110722 | } |
| | @@ -110305,16 +110992,83 @@ |
| 110305 | 110992 | */ |
| 110306 | 110993 | sqlite3SelectDelete(db, pSub1); |
| 110307 | 110994 | |
| 110308 | 110995 | #if SELECTTRACE_ENABLED |
| 110309 | 110996 | if( sqlite3SelectTrace & 0x100 ){ |
| 110310 | | - sqlite3DebugPrintf("After flattening:\n"); |
| 110997 | + SELECTTRACE(0x100,pParse,p,("After flattening:\n")); |
| 110311 | 110998 | sqlite3TreeViewSelect(0, p, 0); |
| 110312 | 110999 | } |
| 110313 | 111000 | #endif |
| 110314 | 111001 | |
| 110315 | 111002 | return 1; |
| 111003 | +} |
| 111004 | +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 111005 | + |
| 111006 | + |
| 111007 | + |
| 111008 | +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 111009 | +/* |
| 111010 | +** Make copies of relevant WHERE clause terms of the outer query into |
| 111011 | +** the WHERE clause of subquery. Example: |
| 111012 | +** |
| 111013 | +** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10; |
| 111014 | +** |
| 111015 | +** Transformed into: |
| 111016 | +** |
| 111017 | +** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10) |
| 111018 | +** WHERE x=5 AND y=10; |
| 111019 | +** |
| 111020 | +** The hope is that the terms added to the inner query will make it more |
| 111021 | +** efficient. |
| 111022 | +** |
| 111023 | +** Do not attempt this optimization if: |
| 111024 | +** |
| 111025 | +** (1) The inner query is an aggregate. (In that case, we'd really want |
| 111026 | +** to copy the outer WHERE-clause terms onto the HAVING clause of the |
| 111027 | +** inner query. But they probably won't help there so do not bother.) |
| 111028 | +** |
| 111029 | +** (2) The inner query is the recursive part of a common table expression. |
| 111030 | +** |
| 111031 | +** (3) The inner query has a LIMIT clause (since the changes to the WHERE |
| 111032 | +** close would change the meaning of the LIMIT). |
| 111033 | +** |
| 111034 | +** (4) The inner query is the right operand of a LEFT JOIN. (The caller |
| 111035 | +** enforces this restriction since this routine does not have enough |
| 111036 | +** information to know.) |
| 111037 | +** |
| 111038 | +** Return 0 if no changes are made and non-zero if one or more WHERE clause |
| 111039 | +** terms are duplicated into the subquery. |
| 111040 | +*/ |
| 111041 | +static int pushDownWhereTerms( |
| 111042 | + sqlite3 *db, /* The database connection (for malloc()) */ |
| 111043 | + Select *pSubq, /* The subquery whose WHERE clause is to be augmented */ |
| 111044 | + Expr *pWhere, /* The WHERE clause of the outer query */ |
| 111045 | + int iCursor /* Cursor number of the subquery */ |
| 111046 | +){ |
| 111047 | + Expr *pNew; |
| 111048 | + int nChng = 0; |
| 111049 | + if( pWhere==0 ) return 0; |
| 111050 | + if( (pSubq->selFlags & (SF_Aggregate|SF_Recursive))!=0 ){ |
| 111051 | + return 0; /* restrictions (1) and (2) */ |
| 111052 | + } |
| 111053 | + if( pSubq->pLimit!=0 ){ |
| 111054 | + return 0; /* restriction (3) */ |
| 111055 | + } |
| 111056 | + while( pWhere->op==TK_AND ){ |
| 111057 | + nChng += pushDownWhereTerms(db, pSubq, pWhere->pRight, iCursor); |
| 111058 | + pWhere = pWhere->pLeft; |
| 111059 | + } |
| 111060 | + if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){ |
| 111061 | + nChng++; |
| 111062 | + while( pSubq ){ |
| 111063 | + pNew = sqlite3ExprDup(db, pWhere, 0); |
| 111064 | + pNew = substExpr(db, pNew, iCursor, pSubq->pEList); |
| 111065 | + pSubq->pWhere = sqlite3ExprAnd(db, pSubq->pWhere, pNew); |
| 111066 | + pSubq = pSubq->pPrior; |
| 111067 | + } |
| 111068 | + } |
| 111069 | + return nChng; |
| 110316 | 111070 | } |
| 110317 | 111071 | #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 110318 | 111072 | |
| 110319 | 111073 | /* |
| 110320 | 111074 | ** Based on the contents of the AggInfo structure indicated by the first |
| | @@ -110397,20 +111151,20 @@ |
| 110397 | 111151 | ** was such a clause and the named index cannot be found, return |
| 110398 | 111152 | ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate |
| 110399 | 111153 | ** pFrom->pIndex and return SQLITE_OK. |
| 110400 | 111154 | */ |
| 110401 | 111155 | SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){ |
| 110402 | | - if( pFrom->pTab && pFrom->zIndex ){ |
| 111156 | + if( pFrom->pTab && pFrom->zIndexedBy ){ |
| 110403 | 111157 | Table *pTab = pFrom->pTab; |
| 110404 | | - char *zIndex = pFrom->zIndex; |
| 111158 | + char *zIndexedBy = pFrom->zIndexedBy; |
| 110405 | 111159 | Index *pIdx; |
| 110406 | 111160 | for(pIdx=pTab->pIndex; |
| 110407 | | - pIdx && sqlite3StrICmp(pIdx->zName, zIndex); |
| 111161 | + pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy); |
| 110408 | 111162 | pIdx=pIdx->pNext |
| 110409 | 111163 | ); |
| 110410 | 111164 | if( !pIdx ){ |
| 110411 | | - sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0); |
| 111165 | + sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0); |
| 110412 | 111166 | pParse->checkSchema = 1; |
| 110413 | 111167 | return SQLITE_ERROR; |
| 110414 | 111168 | } |
| 110415 | 111169 | pFrom->pIndex = pIdx; |
| 110416 | 111170 | } |
| | @@ -111210,11 +111964,11 @@ |
| 111210 | 111964 | pColl = pParse->db->pDfltColl; |
| 111211 | 111965 | } |
| 111212 | 111966 | if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem; |
| 111213 | 111967 | sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ); |
| 111214 | 111968 | } |
| 111215 | | - sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem, |
| 111969 | + sqlite3VdbeAddOp4(v, OP_AggStep0, 0, regAgg, pF->iMem, |
| 111216 | 111970 | (void*)pF->pFunc, P4_FUNCDEF); |
| 111217 | 111971 | sqlite3VdbeChangeP5(v, (u8)nArg); |
| 111218 | 111972 | sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg); |
| 111219 | 111973 | sqlite3ReleaseTempRange(pParse, regAgg, nArg); |
| 111220 | 111974 | if( addrNext ){ |
| | @@ -111343,47 +112097,84 @@ |
| 111343 | 112097 | } |
| 111344 | 112098 | sqlite3SelectPrep(pParse, p, 0); |
| 111345 | 112099 | memset(&sSort, 0, sizeof(sSort)); |
| 111346 | 112100 | sSort.pOrderBy = p->pOrderBy; |
| 111347 | 112101 | pTabList = p->pSrc; |
| 111348 | | - pEList = p->pEList; |
| 111349 | 112102 | if( pParse->nErr || db->mallocFailed ){ |
| 111350 | 112103 | goto select_end; |
| 111351 | 112104 | } |
| 112105 | + assert( p->pEList!=0 ); |
| 111352 | 112106 | isAgg = (p->selFlags & SF_Aggregate)!=0; |
| 111353 | | - assert( pEList!=0 ); |
| 111354 | 112107 | #if SELECTTRACE_ENABLED |
| 111355 | 112108 | if( sqlite3SelectTrace & 0x100 ){ |
| 111356 | 112109 | SELECTTRACE(0x100,pParse,p, ("after name resolution:\n")); |
| 111357 | 112110 | sqlite3TreeViewSelect(0, p, 0); |
| 111358 | 112111 | } |
| 111359 | 112112 | #endif |
| 111360 | 112113 | |
| 111361 | 112114 | |
| 111362 | | - /* Begin generating code. |
| 111363 | | - */ |
| 111364 | | - v = sqlite3GetVdbe(pParse); |
| 111365 | | - if( v==0 ) goto select_end; |
| 111366 | | - |
| 111367 | 112115 | /* If writing to memory or generating a set |
| 111368 | 112116 | ** only a single column may be output. |
| 111369 | 112117 | */ |
| 111370 | 112118 | #ifndef SQLITE_OMIT_SUBQUERY |
| 111371 | | - if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){ |
| 112119 | + if( checkForMultiColumnSelectError(pParse, pDest, p->pEList->nExpr) ){ |
| 111372 | 112120 | goto select_end; |
| 111373 | 112121 | } |
| 111374 | 112122 | #endif |
| 112123 | + |
| 112124 | + /* Try to flatten subqueries in the FROM clause up into the main query |
| 112125 | + */ |
| 112126 | +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 112127 | + for(i=0; !p->pPrior && i<pTabList->nSrc; i++){ |
| 112128 | + struct SrcList_item *pItem = &pTabList->a[i]; |
| 112129 | + Select *pSub = pItem->pSelect; |
| 112130 | + int isAggSub; |
| 112131 | + if( pSub==0 ) continue; |
| 112132 | + isAggSub = (pSub->selFlags & SF_Aggregate)!=0; |
| 112133 | + if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){ |
| 112134 | + /* This subquery can be absorbed into its parent. */ |
| 112135 | + if( isAggSub ){ |
| 112136 | + isAgg = 1; |
| 112137 | + p->selFlags |= SF_Aggregate; |
| 112138 | + } |
| 112139 | + i = -1; |
| 112140 | + } |
| 112141 | + pTabList = p->pSrc; |
| 112142 | + if( db->mallocFailed ) goto select_end; |
| 112143 | + if( !IgnorableOrderby(pDest) ){ |
| 112144 | + sSort.pOrderBy = p->pOrderBy; |
| 112145 | + } |
| 112146 | + } |
| 112147 | +#endif |
| 112148 | + |
| 112149 | + /* Get a pointer the VDBE under construction, allocating a new VDBE if one |
| 112150 | + ** does not already exist */ |
| 112151 | + v = sqlite3GetVdbe(pParse); |
| 112152 | + if( v==0 ) goto select_end; |
| 112153 | + |
| 112154 | +#ifndef SQLITE_OMIT_COMPOUND_SELECT |
| 112155 | + /* Handle compound SELECT statements using the separate multiSelect() |
| 112156 | + ** procedure. |
| 112157 | + */ |
| 112158 | + if( p->pPrior ){ |
| 112159 | + rc = multiSelect(pParse, p, pDest); |
| 112160 | + explainSetInteger(pParse->iSelectId, iRestoreSelectId); |
| 112161 | +#if SELECTTRACE_ENABLED |
| 112162 | + SELECTTRACE(1,pParse,p,("end compound-select processing\n")); |
| 112163 | + pParse->nSelectIndent--; |
| 112164 | +#endif |
| 112165 | + return rc; |
| 112166 | + } |
| 112167 | +#endif |
| 111375 | 112168 | |
| 111376 | 112169 | /* Generate code for all sub-queries in the FROM clause |
| 111377 | 112170 | */ |
| 111378 | 112171 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 111379 | | - for(i=0; !p->pPrior && i<pTabList->nSrc; i++){ |
| 112172 | + for(i=0; i<pTabList->nSrc; i++){ |
| 111380 | 112173 | struct SrcList_item *pItem = &pTabList->a[i]; |
| 111381 | 112174 | SelectDest dest; |
| 111382 | 112175 | Select *pSub = pItem->pSelect; |
| 111383 | | - int isAggSub; |
| 111384 | | - |
| 111385 | 112176 | if( pSub==0 ) continue; |
| 111386 | 112177 | |
| 111387 | 112178 | /* Sometimes the code for a subquery will be generated more than |
| 111388 | 112179 | ** once, if the subquery is part of the WHERE clause in a LEFT JOIN, |
| 111389 | 112180 | ** for example. In that case, do not regenerate the code to manifest |
| | @@ -111404,21 +112195,29 @@ |
| 111404 | 112195 | ** more conservative than necessary, but much easier than enforcing |
| 111405 | 112196 | ** an exact limit. |
| 111406 | 112197 | */ |
| 111407 | 112198 | pParse->nHeight += sqlite3SelectExprHeight(p); |
| 111408 | 112199 | |
| 111409 | | - isAggSub = (pSub->selFlags & SF_Aggregate)!=0; |
| 111410 | | - if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){ |
| 111411 | | - /* This subquery can be absorbed into its parent. */ |
| 111412 | | - if( isAggSub ){ |
| 111413 | | - isAgg = 1; |
| 111414 | | - p->selFlags |= SF_Aggregate; |
| 111415 | | - } |
| 111416 | | - i = -1; |
| 111417 | | - }else if( pTabList->nSrc==1 |
| 111418 | | - && (p->selFlags & SF_All)==0 |
| 111419 | | - && OptimizationEnabled(db, SQLITE_SubqCoroutine) |
| 112200 | + /* Make copies of constant WHERE-clause terms in the outer query down |
| 112201 | + ** inside the subquery. This can help the subquery to run more efficiently. |
| 112202 | + */ |
| 112203 | + if( (pItem->jointype & JT_OUTER)==0 |
| 112204 | + && pushDownWhereTerms(db, pSub, p->pWhere, pItem->iCursor) |
| 112205 | + ){ |
| 112206 | +#if SELECTTRACE_ENABLED |
| 112207 | + if( sqlite3SelectTrace & 0x100 ){ |
| 112208 | + SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n")); |
| 112209 | + sqlite3TreeViewSelect(0, p, 0); |
| 112210 | + } |
| 112211 | +#endif |
| 112212 | + } |
| 112213 | + |
| 112214 | + /* Generate code to implement the subquery |
| 112215 | + */ |
| 112216 | + if( pTabList->nSrc==1 |
| 112217 | + && (p->selFlags & SF_All)==0 |
| 112218 | + && OptimizationEnabled(db, SQLITE_SubqCoroutine) |
| 111420 | 112219 | ){ |
| 111421 | 112220 | /* Implement a co-routine that will return a single row of the result |
| 111422 | 112221 | ** set on each invocation. |
| 111423 | 112222 | */ |
| 111424 | 112223 | int addrTop = sqlite3VdbeCurrentAddr(v)+1; |
| | @@ -111465,37 +112264,27 @@ |
| 111465 | 112264 | retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn); |
| 111466 | 112265 | VdbeComment((v, "end %s", pItem->pTab->zName)); |
| 111467 | 112266 | sqlite3VdbeChangeP1(v, topAddr, retAddr); |
| 111468 | 112267 | sqlite3ClearTempRegCache(pParse); |
| 111469 | 112268 | } |
| 111470 | | - if( /*pParse->nErr ||*/ db->mallocFailed ){ |
| 111471 | | - goto select_end; |
| 111472 | | - } |
| 112269 | + if( db->mallocFailed ) goto select_end; |
| 111473 | 112270 | pParse->nHeight -= sqlite3SelectExprHeight(p); |
| 111474 | | - pTabList = p->pSrc; |
| 111475 | | - if( !IgnorableOrderby(pDest) ){ |
| 111476 | | - sSort.pOrderBy = p->pOrderBy; |
| 111477 | | - } |
| 111478 | 112271 | } |
| 112272 | +#endif |
| 112273 | + |
| 112274 | + /* Various elements of the SELECT copied into local variables for |
| 112275 | + ** convenience */ |
| 111479 | 112276 | pEList = p->pEList; |
| 111480 | | -#endif |
| 111481 | 112277 | pWhere = p->pWhere; |
| 111482 | 112278 | pGroupBy = p->pGroupBy; |
| 111483 | 112279 | pHaving = p->pHaving; |
| 111484 | 112280 | sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0; |
| 111485 | 112281 | |
| 111486 | | -#ifndef SQLITE_OMIT_COMPOUND_SELECT |
| 111487 | | - /* If there is are a sequence of queries, do the earlier ones first. |
| 111488 | | - */ |
| 111489 | | - if( p->pPrior ){ |
| 111490 | | - rc = multiSelect(pParse, p, pDest); |
| 111491 | | - explainSetInteger(pParse->iSelectId, iRestoreSelectId); |
| 111492 | 112282 | #if SELECTTRACE_ENABLED |
| 111493 | | - SELECTTRACE(1,pParse,p,("end compound-select processing\n")); |
| 111494 | | - pParse->nSelectIndent--; |
| 111495 | | -#endif |
| 111496 | | - return rc; |
| 112283 | + if( sqlite3SelectTrace & 0x400 ){ |
| 112284 | + SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n")); |
| 112285 | + sqlite3TreeViewSelect(0, p, 0); |
| 111497 | 112286 | } |
| 111498 | 112287 | #endif |
| 111499 | 112288 | |
| 111500 | 112289 | /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and |
| 111501 | 112290 | ** if the select-list is the same as the ORDER BY list, then this query |
| | @@ -111511,27 +112300,27 @@ |
| 111511 | 112300 | ** used for both the ORDER BY and DISTINCT processing. As originally |
| 111512 | 112301 | ** written the query must use a temp-table for at least one of the ORDER |
| 111513 | 112302 | ** BY and DISTINCT, and an index or separate temp-table for the other. |
| 111514 | 112303 | */ |
| 111515 | 112304 | if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct |
| 111516 | | - && sqlite3ExprListCompare(sSort.pOrderBy, p->pEList, -1)==0 |
| 112305 | + && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0 |
| 111517 | 112306 | ){ |
| 111518 | 112307 | p->selFlags &= ~SF_Distinct; |
| 111519 | | - p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0); |
| 111520 | | - pGroupBy = p->pGroupBy; |
| 112308 | + pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0); |
| 111521 | 112309 | /* Notice that even thought SF_Distinct has been cleared from p->selFlags, |
| 111522 | 112310 | ** the sDistinct.isTnct is still set. Hence, isTnct represents the |
| 111523 | 112311 | ** original setting of the SF_Distinct flag, not the current setting */ |
| 111524 | 112312 | assert( sDistinct.isTnct ); |
| 111525 | 112313 | } |
| 111526 | 112314 | |
| 111527 | | - /* If there is an ORDER BY clause, then this sorting |
| 111528 | | - ** index might end up being unused if the data can be |
| 111529 | | - ** extracted in pre-sorted order. If that is the case, then the |
| 111530 | | - ** OP_OpenEphemeral instruction will be changed to an OP_Noop once |
| 111531 | | - ** we figure out that the sorting index is not needed. The addrSortIndex |
| 111532 | | - ** variable is used to facilitate that change. |
| 112315 | + /* If there is an ORDER BY clause, then create an ephemeral index to |
| 112316 | + ** do the sorting. But this sorting ephemeral index might end up |
| 112317 | + ** being unused if the data can be extracted in pre-sorted order. |
| 112318 | + ** If that is the case, then the OP_OpenEphemeral instruction will be |
| 112319 | + ** changed to an OP_Noop once we figure out that the sorting index is |
| 112320 | + ** not needed. The sSort.addrSortIndex variable is used to facilitate |
| 112321 | + ** that change. |
| 111533 | 112322 | */ |
| 111534 | 112323 | if( sSort.pOrderBy ){ |
| 111535 | 112324 | KeyInfo *pKeyInfo; |
| 111536 | 112325 | pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, pEList->nExpr); |
| 111537 | 112326 | sSort.iECursor = pParse->nTab++; |
| | @@ -111558,18 +112347,18 @@ |
| 111558 | 112347 | if( p->iLimit==0 && sSort.addrSortIndex>=0 ){ |
| 111559 | 112348 | sqlite3VdbeGetOp(v, sSort.addrSortIndex)->opcode = OP_SorterOpen; |
| 111560 | 112349 | sSort.sortFlags |= SORTFLAG_UseSorter; |
| 111561 | 112350 | } |
| 111562 | 112351 | |
| 111563 | | - /* Open a virtual index to use for the distinct set. |
| 112352 | + /* Open an ephemeral index to use for the distinct set. |
| 111564 | 112353 | */ |
| 111565 | 112354 | if( p->selFlags & SF_Distinct ){ |
| 111566 | 112355 | sDistinct.tabTnct = pParse->nTab++; |
| 111567 | 112356 | sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, |
| 111568 | | - sDistinct.tabTnct, 0, 0, |
| 111569 | | - (char*)keyInfoFromExprList(pParse, p->pEList,0,0), |
| 111570 | | - P4_KEYINFO); |
| 112357 | + sDistinct.tabTnct, 0, 0, |
| 112358 | + (char*)keyInfoFromExprList(pParse, p->pEList,0,0), |
| 112359 | + P4_KEYINFO); |
| 111571 | 112360 | sqlite3VdbeChangeP5(v, BTREE_UNORDERED); |
| 111572 | 112361 | sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED; |
| 111573 | 112362 | }else{ |
| 111574 | 112363 | sDistinct.eTnctType = WHERE_DISTINCT_NOOP; |
| 111575 | 112364 | } |
| | @@ -111643,15 +112432,14 @@ |
| 111643 | 112432 | if( p->nSelectRow>100 ) p->nSelectRow = 100; |
| 111644 | 112433 | }else{ |
| 111645 | 112434 | p->nSelectRow = 1; |
| 111646 | 112435 | } |
| 111647 | 112436 | |
| 111648 | | - |
| 111649 | 112437 | /* If there is both a GROUP BY and an ORDER BY clause and they are |
| 111650 | 112438 | ** identical, then it may be possible to disable the ORDER BY clause |
| 111651 | 112439 | ** on the grounds that the GROUP BY will cause elements to come out |
| 111652 | | - ** in the correct order. It also may not - the GROUP BY may use a |
| 112440 | + ** in the correct order. It also may not - the GROUP BY might use a |
| 111653 | 112441 | ** database index that causes rows to be grouped together as required |
| 111654 | 112442 | ** but not actually sorted. Either way, record the fact that the |
| 111655 | 112443 | ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp |
| 111656 | 112444 | ** variable. */ |
| 111657 | 112445 | if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){ |
| | @@ -111825,11 +112613,12 @@ |
| 111825 | 112613 | ** from the previous row currently stored in a0, a1, a2... |
| 111826 | 112614 | */ |
| 111827 | 112615 | addrTopOfLoop = sqlite3VdbeCurrentAddr(v); |
| 111828 | 112616 | sqlite3ExprCacheClear(pParse); |
| 111829 | 112617 | if( groupBySort ){ |
| 111830 | | - sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx, sortOut,sortPTab); |
| 112618 | + sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx, |
| 112619 | + sortOut, sortPTab); |
| 111831 | 112620 | } |
| 111832 | 112621 | for(j=0; j<pGroupBy->nExpr; j++){ |
| 111833 | 112622 | if( groupBySort ){ |
| 111834 | 112623 | sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j); |
| 111835 | 112624 | }else{ |
| | @@ -111897,11 +112686,12 @@ |
| 111897 | 112686 | sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag); |
| 111898 | 112687 | VdbeComment((v, "set abort flag")); |
| 111899 | 112688 | sqlite3VdbeAddOp1(v, OP_Return, regOutputRow); |
| 111900 | 112689 | sqlite3VdbeResolveLabel(v, addrOutputRow); |
| 111901 | 112690 | addrOutputRow = sqlite3VdbeCurrentAddr(v); |
| 111902 | | - sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); VdbeCoverage(v); |
| 112691 | + sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); |
| 112692 | + VdbeCoverage(v); |
| 111903 | 112693 | VdbeComment((v, "Groupby result generator entry point")); |
| 111904 | 112694 | sqlite3VdbeAddOp1(v, OP_Return, regOutputRow); |
| 111905 | 112695 | finalizeAggFunctions(pParse, &sAggInfo); |
| 111906 | 112696 | sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL); |
| 111907 | 112697 | selectInnerLoop(pParse, p, p->pEList, -1, &sSort, |
| | @@ -112061,11 +112851,12 @@ |
| 112061 | 112851 | |
| 112062 | 112852 | /* If there is an ORDER BY clause, then we need to sort the results |
| 112063 | 112853 | ** and send them to the callback one by one. |
| 112064 | 112854 | */ |
| 112065 | 112855 | if( sSort.pOrderBy ){ |
| 112066 | | - explainTempTable(pParse, sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY"); |
| 112856 | + explainTempTable(pParse, |
| 112857 | + sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY"); |
| 112067 | 112858 | generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest); |
| 112068 | 112859 | } |
| 112069 | 112860 | |
| 112070 | 112861 | /* Jump here to skip this query |
| 112071 | 112862 | */ |
| | @@ -112094,104 +112885,10 @@ |
| 112094 | 112885 | pParse->nSelectIndent--; |
| 112095 | 112886 | #endif |
| 112096 | 112887 | return rc; |
| 112097 | 112888 | } |
| 112098 | 112889 | |
| 112099 | | -#ifdef SQLITE_DEBUG |
| 112100 | | -/* |
| 112101 | | -** Generate a human-readable description of a the Select object. |
| 112102 | | -*/ |
| 112103 | | -SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){ |
| 112104 | | - int n = 0; |
| 112105 | | - pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 112106 | | - sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x", |
| 112107 | | - ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), |
| 112108 | | - ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags |
| 112109 | | - ); |
| 112110 | | - if( p->pSrc && p->pSrc->nSrc ) n++; |
| 112111 | | - if( p->pWhere ) n++; |
| 112112 | | - if( p->pGroupBy ) n++; |
| 112113 | | - if( p->pHaving ) n++; |
| 112114 | | - if( p->pOrderBy ) n++; |
| 112115 | | - if( p->pLimit ) n++; |
| 112116 | | - if( p->pOffset ) n++; |
| 112117 | | - if( p->pPrior ) n++; |
| 112118 | | - sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set"); |
| 112119 | | - if( p->pSrc && p->pSrc->nSrc ){ |
| 112120 | | - int i; |
| 112121 | | - pView = sqlite3TreeViewPush(pView, (n--)>0); |
| 112122 | | - sqlite3TreeViewLine(pView, "FROM"); |
| 112123 | | - for(i=0; i<p->pSrc->nSrc; i++){ |
| 112124 | | - struct SrcList_item *pItem = &p->pSrc->a[i]; |
| 112125 | | - StrAccum x; |
| 112126 | | - char zLine[100]; |
| 112127 | | - sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); |
| 112128 | | - sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor); |
| 112129 | | - if( pItem->zDatabase ){ |
| 112130 | | - sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName); |
| 112131 | | - }else if( pItem->zName ){ |
| 112132 | | - sqlite3XPrintf(&x, 0, " %s", pItem->zName); |
| 112133 | | - } |
| 112134 | | - if( pItem->pTab ){ |
| 112135 | | - sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName); |
| 112136 | | - } |
| 112137 | | - if( pItem->zAlias ){ |
| 112138 | | - sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias); |
| 112139 | | - } |
| 112140 | | - if( pItem->jointype & JT_LEFT ){ |
| 112141 | | - sqlite3XPrintf(&x, 0, " LEFT-JOIN"); |
| 112142 | | - } |
| 112143 | | - sqlite3StrAccumFinish(&x); |
| 112144 | | - sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); |
| 112145 | | - if( pItem->pSelect ){ |
| 112146 | | - sqlite3TreeViewSelect(pView, pItem->pSelect, 0); |
| 112147 | | - } |
| 112148 | | - sqlite3TreeViewPop(pView); |
| 112149 | | - } |
| 112150 | | - sqlite3TreeViewPop(pView); |
| 112151 | | - } |
| 112152 | | - if( p->pWhere ){ |
| 112153 | | - sqlite3TreeViewItem(pView, "WHERE", (n--)>0); |
| 112154 | | - sqlite3TreeViewExpr(pView, p->pWhere, 0); |
| 112155 | | - sqlite3TreeViewPop(pView); |
| 112156 | | - } |
| 112157 | | - if( p->pGroupBy ){ |
| 112158 | | - sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); |
| 112159 | | - } |
| 112160 | | - if( p->pHaving ){ |
| 112161 | | - sqlite3TreeViewItem(pView, "HAVING", (n--)>0); |
| 112162 | | - sqlite3TreeViewExpr(pView, p->pHaving, 0); |
| 112163 | | - sqlite3TreeViewPop(pView); |
| 112164 | | - } |
| 112165 | | - if( p->pOrderBy ){ |
| 112166 | | - sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); |
| 112167 | | - } |
| 112168 | | - if( p->pLimit ){ |
| 112169 | | - sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); |
| 112170 | | - sqlite3TreeViewExpr(pView, p->pLimit, 0); |
| 112171 | | - sqlite3TreeViewPop(pView); |
| 112172 | | - } |
| 112173 | | - if( p->pOffset ){ |
| 112174 | | - sqlite3TreeViewItem(pView, "OFFSET", (n--)>0); |
| 112175 | | - sqlite3TreeViewExpr(pView, p->pOffset, 0); |
| 112176 | | - sqlite3TreeViewPop(pView); |
| 112177 | | - } |
| 112178 | | - if( p->pPrior ){ |
| 112179 | | - const char *zOp = "UNION"; |
| 112180 | | - switch( p->op ){ |
| 112181 | | - case TK_ALL: zOp = "UNION ALL"; break; |
| 112182 | | - case TK_INTERSECT: zOp = "INTERSECT"; break; |
| 112183 | | - case TK_EXCEPT: zOp = "EXCEPT"; break; |
| 112184 | | - } |
| 112185 | | - sqlite3TreeViewItem(pView, zOp, (n--)>0); |
| 112186 | | - sqlite3TreeViewSelect(pView, p->pPrior, 0); |
| 112187 | | - sqlite3TreeViewPop(pView); |
| 112188 | | - } |
| 112189 | | - sqlite3TreeViewPop(pView); |
| 112190 | | -} |
| 112191 | | -#endif /* SQLITE_DEBUG */ |
| 112192 | | - |
| 112193 | 112890 | /************** End of select.c **********************************************/ |
| 112194 | 112891 | /************** Begin file table.c *******************************************/ |
| 112195 | 112892 | /* |
| 112196 | 112893 | ** 2001 September 15 |
| 112197 | 112894 | ** |
| | @@ -115499,24 +116196,25 @@ |
| 115499 | 116196 | ** The array is cleared after invoking the callbacks. |
| 115500 | 116197 | */ |
| 115501 | 116198 | static void callFinaliser(sqlite3 *db, int offset){ |
| 115502 | 116199 | int i; |
| 115503 | 116200 | if( db->aVTrans ){ |
| 116201 | + VTable **aVTrans = db->aVTrans; |
| 116202 | + db->aVTrans = 0; |
| 115504 | 116203 | for(i=0; i<db->nVTrans; i++){ |
| 115505 | | - VTable *pVTab = db->aVTrans[i]; |
| 116204 | + VTable *pVTab = aVTrans[i]; |
| 115506 | 116205 | sqlite3_vtab *p = pVTab->pVtab; |
| 115507 | 116206 | if( p ){ |
| 115508 | 116207 | int (*x)(sqlite3_vtab *); |
| 115509 | 116208 | x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset); |
| 115510 | 116209 | if( x ) x(p); |
| 115511 | 116210 | } |
| 115512 | 116211 | pVTab->iSavepoint = 0; |
| 115513 | 116212 | sqlite3VtabUnlock(pVTab); |
| 115514 | 116213 | } |
| 115515 | | - sqlite3DbFree(db, db->aVTrans); |
| 116214 | + sqlite3DbFree(db, aVTrans); |
| 115516 | 116215 | db->nVTrans = 0; |
| 115517 | | - db->aVTrans = 0; |
| 115518 | 116216 | } |
| 115519 | 116217 | } |
| 115520 | 116218 | |
| 115521 | 116219 | /* |
| 115522 | 116220 | ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans |
| | @@ -115812,13 +116510,13 @@ |
| 115812 | 116510 | } |
| 115813 | 116511 | |
| 115814 | 116512 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 115815 | 116513 | |
| 115816 | 116514 | /************** End of vtab.c ************************************************/ |
| 115817 | | -/************** Begin file where.c *******************************************/ |
| 116515 | +/************** Begin file wherecode.c ***************************************/ |
| 115818 | 116516 | /* |
| 115819 | | -** 2001 September 15 |
| 116517 | +** 2015-06-06 |
| 115820 | 116518 | ** |
| 115821 | 116519 | ** The author disclaims copyright to this source code. In place of |
| 115822 | 116520 | ** a legal notice, here is a blessing: |
| 115823 | 116521 | ** |
| 115824 | 116522 | ** May you do good and not evil. |
| | @@ -115825,17 +116523,18 @@ |
| 115825 | 116523 | ** May you find forgiveness for yourself and forgive others. |
| 115826 | 116524 | ** May you share freely, never taking more than you give. |
| 115827 | 116525 | ** |
| 115828 | 116526 | ************************************************************************* |
| 115829 | 116527 | ** This module contains C code that generates VDBE code used to process |
| 115830 | | -** the WHERE clause of SQL statements. This module is responsible for |
| 115831 | | -** generating the code that loops through a table looking for applicable |
| 115832 | | -** rows. Indices are selected and used to speed the search when doing |
| 115833 | | -** so is applicable. Because this module is responsible for selecting |
| 115834 | | -** indices, you might also think of this module as the "query optimizer". |
| 116528 | +** the WHERE clause of SQL statements. |
| 116529 | +** |
| 116530 | +** This file was split off from where.c on 2015-06-06 in order to reduce the |
| 116531 | +** size of where.c and make it easier to edit. This file contains the routines |
| 116532 | +** that actually generate the bulk of the WHERE loop code. The original where.c |
| 116533 | +** file retains the code that does query planning and analysis. |
| 115835 | 116534 | */ |
| 115836 | | -/************** Include whereInt.h in the middle of where.c ******************/ |
| 116535 | +/************** Include whereInt.h in the middle of wherecode.c **************/ |
| 115837 | 116536 | /************** Begin file whereInt.h ****************************************/ |
| 115838 | 116537 | /* |
| 115839 | 116538 | ** 2013-11-12 |
| 115840 | 116539 | ** |
| 115841 | 116540 | ** The author disclaims copyright to this source code. In place of |
| | @@ -115854,11 +116553,11 @@ |
| 115854 | 116553 | |
| 115855 | 116554 | /* |
| 115856 | 116555 | ** Trace output macros |
| 115857 | 116556 | */ |
| 115858 | 116557 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 115859 | | -/***/ int sqlite3WhereTrace = 0; |
| 116558 | +/***/ int sqlite3WhereTrace; |
| 115860 | 116559 | #endif |
| 115861 | 116560 | #if defined(SQLITE_DEBUG) \ |
| 115862 | 116561 | && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE)) |
| 115863 | 116562 | # define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X |
| 115864 | 116563 | # define WHERETRACE_ENABLED 1 |
| | @@ -115996,14 +116695,10 @@ |
| 115996 | 116695 | struct WhereOrSet { |
| 115997 | 116696 | u16 n; /* Number of valid a[] entries */ |
| 115998 | 116697 | WhereOrCost a[N_OR_COST]; /* Set of best costs */ |
| 115999 | 116698 | }; |
| 116000 | 116699 | |
| 116001 | | - |
| 116002 | | -/* Forward declaration of methods */ |
| 116003 | | -static int whereLoopResize(sqlite3*, WhereLoop*, int); |
| 116004 | | - |
| 116005 | 116700 | /* |
| 116006 | 116701 | ** Each instance of this object holds a sequence of WhereLoop objects |
| 116007 | 116702 | ** that implement some or all of a query plan. |
| 116008 | 116703 | ** |
| 116009 | 116704 | ** Think of each WhereLoop object as a node in a graph with arcs |
| | @@ -116207,10 +116902,15 @@ |
| 116207 | 116902 | struct WhereMaskSet { |
| 116208 | 116903 | int n; /* Number of assigned cursor values */ |
| 116209 | 116904 | int ix[BMS]; /* Cursor assigned to each bit */ |
| 116210 | 116905 | }; |
| 116211 | 116906 | |
| 116907 | +/* |
| 116908 | +** Initialize a WhereMaskSet object |
| 116909 | +*/ |
| 116910 | +#define initMaskSet(P) (P)->n=0 |
| 116911 | + |
| 116212 | 116912 | /* |
| 116213 | 116913 | ** This object is a convenience wrapper holding all information needed |
| 116214 | 116914 | ** to construct WhereLoop objects for a particular query. |
| 116215 | 116915 | */ |
| 116216 | 116916 | struct WhereLoopBuilder { |
| | @@ -116257,10 +116957,66 @@ |
| 116257 | 116957 | int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */ |
| 116258 | 116958 | WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ |
| 116259 | 116959 | WhereClause sWC; /* Decomposition of the WHERE clause */ |
| 116260 | 116960 | WhereLevel a[1]; /* Information about each nest loop in WHERE */ |
| 116261 | 116961 | }; |
| 116962 | + |
| 116963 | +/* |
| 116964 | +** Private interfaces - callable only by other where.c routines. |
| 116965 | +** |
| 116966 | +** where.c: |
| 116967 | +*/ |
| 116968 | +SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int); |
| 116969 | +SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm( |
| 116970 | + WhereClause *pWC, /* The WHERE clause to be searched */ |
| 116971 | + int iCur, /* Cursor number of LHS */ |
| 116972 | + int iColumn, /* Column number of LHS */ |
| 116973 | + Bitmask notReady, /* RHS must not overlap with this mask */ |
| 116974 | + u32 op, /* Mask of WO_xx values describing operator */ |
| 116975 | + Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 116976 | +); |
| 116977 | + |
| 116978 | +/* wherecode.c: */ |
| 116979 | +#ifndef SQLITE_OMIT_EXPLAIN |
| 116980 | +SQLITE_PRIVATE int sqlite3WhereExplainOneScan( |
| 116981 | + Parse *pParse, /* Parse context */ |
| 116982 | + SrcList *pTabList, /* Table list this loop refers to */ |
| 116983 | + WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 116984 | + int iLevel, /* Value for "level" column of output */ |
| 116985 | + int iFrom, /* Value for "from" column of output */ |
| 116986 | + u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 116987 | +); |
| 116988 | +#else |
| 116989 | +# define sqlite3WhereExplainOneScan(u,v,w,x,y,z) 0 |
| 116990 | +#endif /* SQLITE_OMIT_EXPLAIN */ |
| 116991 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 116992 | +SQLITE_PRIVATE void sqlite3WhereAddScanStatus( |
| 116993 | + Vdbe *v, /* Vdbe to add scanstatus entry to */ |
| 116994 | + SrcList *pSrclist, /* FROM clause pLvl reads data from */ |
| 116995 | + WhereLevel *pLvl, /* Level to add scanstatus() entry for */ |
| 116996 | + int addrExplain /* Address of OP_Explain (or 0) */ |
| 116997 | +); |
| 116998 | +#else |
| 116999 | +# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d) |
| 117000 | +#endif |
| 117001 | +SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart( |
| 117002 | + WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 117003 | + int iLevel, /* Which level of pWInfo->a[] should be coded */ |
| 117004 | + Bitmask notReady /* Which tables are currently available */ |
| 117005 | +); |
| 117006 | + |
| 117007 | +/* whereexpr.c: */ |
| 117008 | +SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*); |
| 117009 | +SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*); |
| 117010 | +SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8); |
| 117011 | +SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*); |
| 117012 | +SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*); |
| 117013 | +SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*); |
| 117014 | + |
| 117015 | + |
| 117016 | + |
| 117017 | + |
| 116262 | 117018 | |
| 116263 | 117019 | /* |
| 116264 | 117020 | ** Bitmasks for the operators on WhereTerm objects. These are all |
| 116265 | 117021 | ** operators that are of interest to the query planner. An |
| 116266 | 117022 | ** OR-ed combination of these values can be used when searching for |
| | @@ -116307,176 +117063,1532 @@ |
| 116307 | 117063 | #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */ |
| 116308 | 117064 | #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/ |
| 116309 | 117065 | #define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */ |
| 116310 | 117066 | |
| 116311 | 117067 | /************** End of whereInt.h ********************************************/ |
| 116312 | | -/************** Continuing where we left off in where.c **********************/ |
| 116313 | | - |
| 116314 | | -/* |
| 116315 | | -** Return the estimated number of output rows from a WHERE clause |
| 116316 | | -*/ |
| 116317 | | -SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
| 116318 | | - return sqlite3LogEstToInt(pWInfo->nRowOut); |
| 116319 | | -} |
| 116320 | | - |
| 116321 | | -/* |
| 116322 | | -** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this |
| 116323 | | -** WHERE clause returns outputs for DISTINCT processing. |
| 116324 | | -*/ |
| 116325 | | -SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ |
| 116326 | | - return pWInfo->eDistinct; |
| 116327 | | -} |
| 116328 | | - |
| 116329 | | -/* |
| 116330 | | -** Return TRUE if the WHERE clause returns rows in ORDER BY order. |
| 116331 | | -** Return FALSE if the output needs to be sorted. |
| 116332 | | -*/ |
| 116333 | | -SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ |
| 116334 | | - return pWInfo->nOBSat; |
| 116335 | | -} |
| 116336 | | - |
| 116337 | | -/* |
| 116338 | | -** Return the VDBE address or label to jump to in order to continue |
| 116339 | | -** immediately with the next row of a WHERE clause. |
| 116340 | | -*/ |
| 116341 | | -SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ |
| 116342 | | - assert( pWInfo->iContinue!=0 ); |
| 116343 | | - return pWInfo->iContinue; |
| 116344 | | -} |
| 116345 | | - |
| 116346 | | -/* |
| 116347 | | -** Return the VDBE address or label to jump to in order to break |
| 116348 | | -** out of a WHERE loop. |
| 116349 | | -*/ |
| 116350 | | -SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ |
| 116351 | | - return pWInfo->iBreak; |
| 116352 | | -} |
| 116353 | | - |
| 116354 | | -/* |
| 116355 | | -** Return TRUE if an UPDATE or DELETE statement can operate directly on |
| 116356 | | -** the rowids returned by a WHERE clause. Return FALSE if doing an |
| 116357 | | -** UPDATE or DELETE might change subsequent WHERE clause results. |
| 116358 | | -** |
| 116359 | | -** If the ONEPASS optimization is used (if this routine returns true) |
| 116360 | | -** then also write the indices of open cursors used by ONEPASS |
| 116361 | | -** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data |
| 116362 | | -** table and iaCur[1] gets the cursor used by an auxiliary index. |
| 116363 | | -** Either value may be -1, indicating that cursor is not used. |
| 116364 | | -** Any cursors returned will have been opened for writing. |
| 116365 | | -** |
| 116366 | | -** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is |
| 116367 | | -** unable to use the ONEPASS optimization. |
| 116368 | | -*/ |
| 116369 | | -SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){ |
| 116370 | | - memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2); |
| 116371 | | - return pWInfo->okOnePass; |
| 116372 | | -} |
| 116373 | | - |
| 116374 | | -/* |
| 116375 | | -** Move the content of pSrc into pDest |
| 116376 | | -*/ |
| 116377 | | -static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){ |
| 116378 | | - pDest->n = pSrc->n; |
| 116379 | | - memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0])); |
| 116380 | | -} |
| 116381 | | - |
| 116382 | | -/* |
| 116383 | | -** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet. |
| 116384 | | -** |
| 116385 | | -** The new entry might overwrite an existing entry, or it might be |
| 116386 | | -** appended, or it might be discarded. Do whatever is the right thing |
| 116387 | | -** so that pSet keeps the N_OR_COST best entries seen so far. |
| 116388 | | -*/ |
| 116389 | | -static int whereOrInsert( |
| 116390 | | - WhereOrSet *pSet, /* The WhereOrSet to be updated */ |
| 116391 | | - Bitmask prereq, /* Prerequisites of the new entry */ |
| 116392 | | - LogEst rRun, /* Run-cost of the new entry */ |
| 116393 | | - LogEst nOut /* Number of outputs for the new entry */ |
| 116394 | | -){ |
| 116395 | | - u16 i; |
| 116396 | | - WhereOrCost *p; |
| 116397 | | - for(i=pSet->n, p=pSet->a; i>0; i--, p++){ |
| 116398 | | - if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){ |
| 116399 | | - goto whereOrInsert_done; |
| 116400 | | - } |
| 116401 | | - if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){ |
| 116402 | | - return 0; |
| 116403 | | - } |
| 116404 | | - } |
| 116405 | | - if( pSet->n<N_OR_COST ){ |
| 116406 | | - p = &pSet->a[pSet->n++]; |
| 116407 | | - p->nOut = nOut; |
| 116408 | | - }else{ |
| 116409 | | - p = pSet->a; |
| 116410 | | - for(i=1; i<pSet->n; i++){ |
| 116411 | | - if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i; |
| 116412 | | - } |
| 116413 | | - if( p->rRun<=rRun ) return 0; |
| 116414 | | - } |
| 116415 | | -whereOrInsert_done: |
| 116416 | | - p->prereq = prereq; |
| 116417 | | - p->rRun = rRun; |
| 116418 | | - if( p->nOut>nOut ) p->nOut = nOut; |
| 116419 | | - return 1; |
| 116420 | | -} |
| 116421 | | - |
| 116422 | | -/* |
| 116423 | | -** Initialize a preallocated WhereClause structure. |
| 116424 | | -*/ |
| 116425 | | -static void whereClauseInit( |
| 116426 | | - WhereClause *pWC, /* The WhereClause to be initialized */ |
| 116427 | | - WhereInfo *pWInfo /* The WHERE processing context */ |
| 116428 | | -){ |
| 116429 | | - pWC->pWInfo = pWInfo; |
| 116430 | | - pWC->pOuter = 0; |
| 116431 | | - pWC->nTerm = 0; |
| 116432 | | - pWC->nSlot = ArraySize(pWC->aStatic); |
| 116433 | | - pWC->a = pWC->aStatic; |
| 116434 | | -} |
| 116435 | | - |
| 116436 | | -/* Forward reference */ |
| 116437 | | -static void whereClauseClear(WhereClause*); |
| 117068 | +/************** Continuing where we left off in wherecode.c ******************/ |
| 117069 | + |
| 117070 | +#ifndef SQLITE_OMIT_EXPLAIN |
| 117071 | +/* |
| 117072 | +** This routine is a helper for explainIndexRange() below |
| 117073 | +** |
| 117074 | +** pStr holds the text of an expression that we are building up one term |
| 117075 | +** at a time. This routine adds a new term to the end of the expression. |
| 117076 | +** Terms are separated by AND so add the "AND" text for second and subsequent |
| 117077 | +** terms only. |
| 117078 | +*/ |
| 117079 | +static void explainAppendTerm( |
| 117080 | + StrAccum *pStr, /* The text expression being built */ |
| 117081 | + int iTerm, /* Index of this term. First is zero */ |
| 117082 | + const char *zColumn, /* Name of the column */ |
| 117083 | + const char *zOp /* Name of the operator */ |
| 117084 | +){ |
| 117085 | + if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 117086 | + sqlite3StrAccumAppendAll(pStr, zColumn); |
| 117087 | + sqlite3StrAccumAppend(pStr, zOp, 1); |
| 117088 | + sqlite3StrAccumAppend(pStr, "?", 1); |
| 117089 | +} |
| 117090 | + |
| 117091 | +/* |
| 117092 | +** Argument pLevel describes a strategy for scanning table pTab. This |
| 117093 | +** function appends text to pStr that describes the subset of table |
| 117094 | +** rows scanned by the strategy in the form of an SQL expression. |
| 117095 | +** |
| 117096 | +** For example, if the query: |
| 117097 | +** |
| 117098 | +** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 117099 | +** |
| 117100 | +** is run and there is an index on (a, b), then this function returns a |
| 117101 | +** string similar to: |
| 117102 | +** |
| 117103 | +** "a=? AND b>?" |
| 117104 | +*/ |
| 117105 | +static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){ |
| 117106 | + Index *pIndex = pLoop->u.btree.pIndex; |
| 117107 | + u16 nEq = pLoop->u.btree.nEq; |
| 117108 | + u16 nSkip = pLoop->nSkip; |
| 117109 | + int i, j; |
| 117110 | + Column *aCol = pTab->aCol; |
| 117111 | + i16 *aiColumn = pIndex->aiColumn; |
| 117112 | + |
| 117113 | + if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; |
| 117114 | + sqlite3StrAccumAppend(pStr, " (", 2); |
| 117115 | + for(i=0; i<nEq; i++){ |
| 117116 | + char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName; |
| 117117 | + if( i>=nSkip ){ |
| 117118 | + explainAppendTerm(pStr, i, z, "="); |
| 117119 | + }else{ |
| 117120 | + if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 117121 | + sqlite3XPrintf(pStr, 0, "ANY(%s)", z); |
| 117122 | + } |
| 117123 | + } |
| 117124 | + |
| 117125 | + j = i; |
| 117126 | + if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
| 117127 | + char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName; |
| 117128 | + explainAppendTerm(pStr, i++, z, ">"); |
| 117129 | + } |
| 117130 | + if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
| 117131 | + char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName; |
| 117132 | + explainAppendTerm(pStr, i, z, "<"); |
| 117133 | + } |
| 117134 | + sqlite3StrAccumAppend(pStr, ")", 1); |
| 117135 | +} |
| 117136 | + |
| 117137 | +/* |
| 117138 | +** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 117139 | +** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was |
| 117140 | +** defined at compile-time. If it is not a no-op, a single OP_Explain opcode |
| 117141 | +** is added to the output to describe the table scan strategy in pLevel. |
| 117142 | +** |
| 117143 | +** If an OP_Explain opcode is added to the VM, its address is returned. |
| 117144 | +** Otherwise, if no OP_Explain is coded, zero is returned. |
| 117145 | +*/ |
| 117146 | +SQLITE_PRIVATE int sqlite3WhereExplainOneScan( |
| 117147 | + Parse *pParse, /* Parse context */ |
| 117148 | + SrcList *pTabList, /* Table list this loop refers to */ |
| 117149 | + WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 117150 | + int iLevel, /* Value for "level" column of output */ |
| 117151 | + int iFrom, /* Value for "from" column of output */ |
| 117152 | + u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 117153 | +){ |
| 117154 | + int ret = 0; |
| 117155 | +#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 117156 | + if( pParse->explain==2 ) |
| 117157 | +#endif |
| 117158 | + { |
| 117159 | + struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 117160 | + Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 117161 | + sqlite3 *db = pParse->db; /* Database handle */ |
| 117162 | + int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
| 117163 | + int isSearch; /* True for a SEARCH. False for SCAN. */ |
| 117164 | + WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 117165 | + u32 flags; /* Flags that describe this loop */ |
| 117166 | + char *zMsg; /* Text to add to EQP output */ |
| 117167 | + StrAccum str; /* EQP output string */ |
| 117168 | + char zBuf[100]; /* Initial space for EQP output string */ |
| 117169 | + |
| 117170 | + pLoop = pLevel->pWLoop; |
| 117171 | + flags = pLoop->wsFlags; |
| 117172 | + if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0; |
| 117173 | + |
| 117174 | + isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 117175 | + || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 117176 | + || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 117177 | + |
| 117178 | + sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); |
| 117179 | + sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN"); |
| 117180 | + if( pItem->pSelect ){ |
| 117181 | + sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId); |
| 117182 | + }else{ |
| 117183 | + sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName); |
| 117184 | + } |
| 117185 | + |
| 117186 | + if( pItem->zAlias ){ |
| 117187 | + sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias); |
| 117188 | + } |
| 117189 | + if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){ |
| 117190 | + const char *zFmt = 0; |
| 117191 | + Index *pIdx; |
| 117192 | + |
| 117193 | + assert( pLoop->u.btree.pIndex!=0 ); |
| 117194 | + pIdx = pLoop->u.btree.pIndex; |
| 117195 | + assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); |
| 117196 | + if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ |
| 117197 | + if( isSearch ){ |
| 117198 | + zFmt = "PRIMARY KEY"; |
| 117199 | + } |
| 117200 | + }else if( flags & WHERE_PARTIALIDX ){ |
| 117201 | + zFmt = "AUTOMATIC PARTIAL COVERING INDEX"; |
| 117202 | + }else if( flags & WHERE_AUTO_INDEX ){ |
| 117203 | + zFmt = "AUTOMATIC COVERING INDEX"; |
| 117204 | + }else if( flags & WHERE_IDX_ONLY ){ |
| 117205 | + zFmt = "COVERING INDEX %s"; |
| 117206 | + }else{ |
| 117207 | + zFmt = "INDEX %s"; |
| 117208 | + } |
| 117209 | + if( zFmt ){ |
| 117210 | + sqlite3StrAccumAppend(&str, " USING ", 7); |
| 117211 | + sqlite3XPrintf(&str, 0, zFmt, pIdx->zName); |
| 117212 | + explainIndexRange(&str, pLoop, pItem->pTab); |
| 117213 | + } |
| 117214 | + }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
| 117215 | + const char *zRange; |
| 117216 | + if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
| 117217 | + zRange = "(rowid=?)"; |
| 117218 | + }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
| 117219 | + zRange = "(rowid>? AND rowid<?)"; |
| 117220 | + }else if( flags&WHERE_BTM_LIMIT ){ |
| 117221 | + zRange = "(rowid>?)"; |
| 117222 | + }else{ |
| 117223 | + assert( flags&WHERE_TOP_LIMIT); |
| 117224 | + zRange = "(rowid<?)"; |
| 117225 | + } |
| 117226 | + sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY "); |
| 117227 | + sqlite3StrAccumAppendAll(&str, zRange); |
| 117228 | + } |
| 117229 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 117230 | + else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
| 117231 | + sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s", |
| 117232 | + pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
| 117233 | + } |
| 117234 | +#endif |
| 117235 | +#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS |
| 117236 | + if( pLoop->nOut>=10 ){ |
| 117237 | + sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut)); |
| 117238 | + }else{ |
| 117239 | + sqlite3StrAccumAppend(&str, " (~1 row)", 9); |
| 117240 | + } |
| 117241 | +#endif |
| 117242 | + zMsg = sqlite3StrAccumFinish(&str); |
| 117243 | + ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC); |
| 117244 | + } |
| 117245 | + return ret; |
| 117246 | +} |
| 117247 | +#endif /* SQLITE_OMIT_EXPLAIN */ |
| 117248 | + |
| 117249 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 117250 | +/* |
| 117251 | +** Configure the VM passed as the first argument with an |
| 117252 | +** sqlite3_stmt_scanstatus() entry corresponding to the scan used to |
| 117253 | +** implement level pLvl. Argument pSrclist is a pointer to the FROM |
| 117254 | +** clause that the scan reads data from. |
| 117255 | +** |
| 117256 | +** If argument addrExplain is not 0, it must be the address of an |
| 117257 | +** OP_Explain instruction that describes the same loop. |
| 117258 | +*/ |
| 117259 | +SQLITE_PRIVATE void sqlite3WhereAddScanStatus( |
| 117260 | + Vdbe *v, /* Vdbe to add scanstatus entry to */ |
| 117261 | + SrcList *pSrclist, /* FROM clause pLvl reads data from */ |
| 117262 | + WhereLevel *pLvl, /* Level to add scanstatus() entry for */ |
| 117263 | + int addrExplain /* Address of OP_Explain (or 0) */ |
| 117264 | +){ |
| 117265 | + const char *zObj = 0; |
| 117266 | + WhereLoop *pLoop = pLvl->pWLoop; |
| 117267 | + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){ |
| 117268 | + zObj = pLoop->u.btree.pIndex->zName; |
| 117269 | + }else{ |
| 117270 | + zObj = pSrclist->a[pLvl->iFrom].zName; |
| 117271 | + } |
| 117272 | + sqlite3VdbeScanStatus( |
| 117273 | + v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj |
| 117274 | + ); |
| 117275 | +} |
| 117276 | +#endif |
| 117277 | + |
| 117278 | + |
| 117279 | +/* |
| 117280 | +** Disable a term in the WHERE clause. Except, do not disable the term |
| 117281 | +** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 117282 | +** or USING clause of that join. |
| 117283 | +** |
| 117284 | +** Consider the term t2.z='ok' in the following queries: |
| 117285 | +** |
| 117286 | +** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 117287 | +** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 117288 | +** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 117289 | +** |
| 117290 | +** The t2.z='ok' is disabled in the in (2) because it originates |
| 117291 | +** in the ON clause. The term is disabled in (3) because it is not part |
| 117292 | +** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 117293 | +** |
| 117294 | +** Disabling a term causes that term to not be tested in the inner loop |
| 117295 | +** of the join. Disabling is an optimization. When terms are satisfied |
| 117296 | +** by indices, we disable them to prevent redundant tests in the inner |
| 117297 | +** loop. We would get the correct results if nothing were ever disabled, |
| 117298 | +** but joins might run a little slower. The trick is to disable as much |
| 117299 | +** as we can without disabling too much. If we disabled in (1), we'd get |
| 117300 | +** the wrong answer. See ticket #813. |
| 117301 | +** |
| 117302 | +** If all the children of a term are disabled, then that term is also |
| 117303 | +** automatically disabled. In this way, terms get disabled if derived |
| 117304 | +** virtual terms are tested first. For example: |
| 117305 | +** |
| 117306 | +** x GLOB 'abc*' AND x>='abc' AND x<'acd' |
| 117307 | +** \___________/ \______/ \_____/ |
| 117308 | +** parent child1 child2 |
| 117309 | +** |
| 117310 | +** Only the parent term was in the original WHERE clause. The child1 |
| 117311 | +** and child2 terms were added by the LIKE optimization. If both of |
| 117312 | +** the virtual child terms are valid, then testing of the parent can be |
| 117313 | +** skipped. |
| 117314 | +** |
| 117315 | +** Usually the parent term is marked as TERM_CODED. But if the parent |
| 117316 | +** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead. |
| 117317 | +** The TERM_LIKECOND marking indicates that the term should be coded inside |
| 117318 | +** a conditional such that is only evaluated on the second pass of a |
| 117319 | +** LIKE-optimization loop, when scanning BLOBs instead of strings. |
| 117320 | +*/ |
| 117321 | +static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
| 117322 | + int nLoop = 0; |
| 117323 | + while( pTerm |
| 117324 | + && (pTerm->wtFlags & TERM_CODED)==0 |
| 117325 | + && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 117326 | + && (pLevel->notReady & pTerm->prereqAll)==0 |
| 117327 | + ){ |
| 117328 | + if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){ |
| 117329 | + pTerm->wtFlags |= TERM_LIKECOND; |
| 117330 | + }else{ |
| 117331 | + pTerm->wtFlags |= TERM_CODED; |
| 117332 | + } |
| 117333 | + if( pTerm->iParent<0 ) break; |
| 117334 | + pTerm = &pTerm->pWC->a[pTerm->iParent]; |
| 117335 | + pTerm->nChild--; |
| 117336 | + if( pTerm->nChild!=0 ) break; |
| 117337 | + nLoop++; |
| 117338 | + } |
| 117339 | +} |
| 117340 | + |
| 117341 | +/* |
| 117342 | +** Code an OP_Affinity opcode to apply the column affinity string zAff |
| 117343 | +** to the n registers starting at base. |
| 117344 | +** |
| 117345 | +** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the |
| 117346 | +** beginning and end of zAff are ignored. If all entries in zAff are |
| 117347 | +** SQLITE_AFF_BLOB, then no code gets generated. |
| 117348 | +** |
| 117349 | +** This routine makes its own copy of zAff so that the caller is free |
| 117350 | +** to modify zAff after this routine returns. |
| 117351 | +*/ |
| 117352 | +static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ |
| 117353 | + Vdbe *v = pParse->pVdbe; |
| 117354 | + if( zAff==0 ){ |
| 117355 | + assert( pParse->db->mallocFailed ); |
| 117356 | + return; |
| 117357 | + } |
| 117358 | + assert( v!=0 ); |
| 117359 | + |
| 117360 | + /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning |
| 117361 | + ** and end of the affinity string. |
| 117362 | + */ |
| 117363 | + while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){ |
| 117364 | + n--; |
| 117365 | + base++; |
| 117366 | + zAff++; |
| 117367 | + } |
| 117368 | + while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){ |
| 117369 | + n--; |
| 117370 | + } |
| 117371 | + |
| 117372 | + /* Code the OP_Affinity opcode if there is anything left to do. */ |
| 117373 | + if( n>0 ){ |
| 117374 | + sqlite3VdbeAddOp2(v, OP_Affinity, base, n); |
| 117375 | + sqlite3VdbeChangeP4(v, -1, zAff, n); |
| 117376 | + sqlite3ExprCacheAffinityChange(pParse, base, n); |
| 117377 | + } |
| 117378 | +} |
| 117379 | + |
| 117380 | + |
| 117381 | +/* |
| 117382 | +** Generate code for a single equality term of the WHERE clause. An equality |
| 117383 | +** term can be either X=expr or X IN (...). pTerm is the term to be |
| 117384 | +** coded. |
| 117385 | +** |
| 117386 | +** The current value for the constraint is left in register iReg. |
| 117387 | +** |
| 117388 | +** For a constraint of the form X=expr, the expression is evaluated and its |
| 117389 | +** result is left on the stack. For constraints of the form X IN (...) |
| 117390 | +** this routine sets up a loop that will iterate over all values of X. |
| 117391 | +*/ |
| 117392 | +static int codeEqualityTerm( |
| 117393 | + Parse *pParse, /* The parsing context */ |
| 117394 | + WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
| 117395 | + WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 117396 | + int iEq, /* Index of the equality term within this level */ |
| 117397 | + int bRev, /* True for reverse-order IN operations */ |
| 117398 | + int iTarget /* Attempt to leave results in this register */ |
| 117399 | +){ |
| 117400 | + Expr *pX = pTerm->pExpr; |
| 117401 | + Vdbe *v = pParse->pVdbe; |
| 117402 | + int iReg; /* Register holding results */ |
| 117403 | + |
| 117404 | + assert( iTarget>0 ); |
| 117405 | + if( pX->op==TK_EQ || pX->op==TK_IS ){ |
| 117406 | + iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); |
| 117407 | + }else if( pX->op==TK_ISNULL ){ |
| 117408 | + iReg = iTarget; |
| 117409 | + sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); |
| 117410 | +#ifndef SQLITE_OMIT_SUBQUERY |
| 117411 | + }else{ |
| 117412 | + int eType; |
| 117413 | + int iTab; |
| 117414 | + struct InLoop *pIn; |
| 117415 | + WhereLoop *pLoop = pLevel->pWLoop; |
| 117416 | + |
| 117417 | + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 117418 | + && pLoop->u.btree.pIndex!=0 |
| 117419 | + && pLoop->u.btree.pIndex->aSortOrder[iEq] |
| 117420 | + ){ |
| 117421 | + testcase( iEq==0 ); |
| 117422 | + testcase( bRev ); |
| 117423 | + bRev = !bRev; |
| 117424 | + } |
| 117425 | + assert( pX->op==TK_IN ); |
| 117426 | + iReg = iTarget; |
| 117427 | + eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0); |
| 117428 | + if( eType==IN_INDEX_INDEX_DESC ){ |
| 117429 | + testcase( bRev ); |
| 117430 | + bRev = !bRev; |
| 117431 | + } |
| 117432 | + iTab = pX->iTable; |
| 117433 | + sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
| 117434 | + VdbeCoverageIf(v, bRev); |
| 117435 | + VdbeCoverageIf(v, !bRev); |
| 117436 | + assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 117437 | + pLoop->wsFlags |= WHERE_IN_ABLE; |
| 117438 | + if( pLevel->u.in.nIn==0 ){ |
| 117439 | + pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 117440 | + } |
| 117441 | + pLevel->u.in.nIn++; |
| 117442 | + pLevel->u.in.aInLoop = |
| 117443 | + sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, |
| 117444 | + sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); |
| 117445 | + pIn = pLevel->u.in.aInLoop; |
| 117446 | + if( pIn ){ |
| 117447 | + pIn += pLevel->u.in.nIn - 1; |
| 117448 | + pIn->iCur = iTab; |
| 117449 | + if( eType==IN_INDEX_ROWID ){ |
| 117450 | + pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); |
| 117451 | + }else{ |
| 117452 | + pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); |
| 117453 | + } |
| 117454 | + pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen; |
| 117455 | + sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v); |
| 117456 | + }else{ |
| 117457 | + pLevel->u.in.nIn = 0; |
| 117458 | + } |
| 117459 | +#endif |
| 117460 | + } |
| 117461 | + disableTerm(pLevel, pTerm); |
| 117462 | + return iReg; |
| 117463 | +} |
| 117464 | + |
| 117465 | +/* |
| 117466 | +** Generate code that will evaluate all == and IN constraints for an |
| 117467 | +** index scan. |
| 117468 | +** |
| 117469 | +** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 117470 | +** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 117471 | +** The index has as many as three equality constraints, but in this |
| 117472 | +** example, the third "c" value is an inequality. So only two |
| 117473 | +** constraints are coded. This routine will generate code to evaluate |
| 117474 | +** a==5 and b IN (1,2,3). The current values for a and b will be stored |
| 117475 | +** in consecutive registers and the index of the first register is returned. |
| 117476 | +** |
| 117477 | +** In the example above nEq==2. But this subroutine works for any value |
| 117478 | +** of nEq including 0. If nEq==0, this routine is nearly a no-op. |
| 117479 | +** The only thing it does is allocate the pLevel->iMem memory cell and |
| 117480 | +** compute the affinity string. |
| 117481 | +** |
| 117482 | +** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints |
| 117483 | +** are == or IN and are covered by the nEq. nExtraReg is 1 if there is |
| 117484 | +** an inequality constraint (such as the "c>=5 AND c<10" in the example) that |
| 117485 | +** occurs after the nEq quality constraints. |
| 117486 | +** |
| 117487 | +** This routine allocates a range of nEq+nExtraReg memory cells and returns |
| 117488 | +** the index of the first memory cell in that range. The code that |
| 117489 | +** calls this routine will use that memory range to store keys for |
| 117490 | +** start and termination conditions of the loop. |
| 117491 | +** key value of the loop. If one or more IN operators appear, then |
| 117492 | +** this routine allocates an additional nEq memory cells for internal |
| 117493 | +** use. |
| 117494 | +** |
| 117495 | +** Before returning, *pzAff is set to point to a buffer containing a |
| 117496 | +** copy of the column affinity string of the index allocated using |
| 117497 | +** sqlite3DbMalloc(). Except, entries in the copy of the string associated |
| 117498 | +** with equality constraints that use BLOB or NONE affinity are set to |
| 117499 | +** SQLITE_AFF_BLOB. This is to deal with SQL such as the following: |
| 117500 | +** |
| 117501 | +** CREATE TABLE t1(a TEXT PRIMARY KEY, b); |
| 117502 | +** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; |
| 117503 | +** |
| 117504 | +** In the example above, the index on t1(a) has TEXT affinity. But since |
| 117505 | +** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity, |
| 117506 | +** no conversion should be attempted before using a t2.b value as part of |
| 117507 | +** a key to search the index. Hence the first byte in the returned affinity |
| 117508 | +** string in this example would be set to SQLITE_AFF_BLOB. |
| 117509 | +*/ |
| 117510 | +static int codeAllEqualityTerms( |
| 117511 | + Parse *pParse, /* Parsing context */ |
| 117512 | + WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
| 117513 | + int bRev, /* Reverse the order of IN operators */ |
| 117514 | + int nExtraReg, /* Number of extra registers to allocate */ |
| 117515 | + char **pzAff /* OUT: Set to point to affinity string */ |
| 117516 | +){ |
| 117517 | + u16 nEq; /* The number of == or IN constraints to code */ |
| 117518 | + u16 nSkip; /* Number of left-most columns to skip */ |
| 117519 | + Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 117520 | + Index *pIdx; /* The index being used for this loop */ |
| 117521 | + WhereTerm *pTerm; /* A single constraint term */ |
| 117522 | + WhereLoop *pLoop; /* The WhereLoop object */ |
| 117523 | + int j; /* Loop counter */ |
| 117524 | + int regBase; /* Base register */ |
| 117525 | + int nReg; /* Number of registers to allocate */ |
| 117526 | + char *zAff; /* Affinity string to return */ |
| 117527 | + |
| 117528 | + /* This module is only called on query plans that use an index. */ |
| 117529 | + pLoop = pLevel->pWLoop; |
| 117530 | + assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 117531 | + nEq = pLoop->u.btree.nEq; |
| 117532 | + nSkip = pLoop->nSkip; |
| 117533 | + pIdx = pLoop->u.btree.pIndex; |
| 117534 | + assert( pIdx!=0 ); |
| 117535 | + |
| 117536 | + /* Figure out how many memory cells we will need then allocate them. |
| 117537 | + */ |
| 117538 | + regBase = pParse->nMem + 1; |
| 117539 | + nReg = pLoop->u.btree.nEq + nExtraReg; |
| 117540 | + pParse->nMem += nReg; |
| 117541 | + |
| 117542 | + zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 117543 | + if( !zAff ){ |
| 117544 | + pParse->db->mallocFailed = 1; |
| 117545 | + } |
| 117546 | + |
| 117547 | + if( nSkip ){ |
| 117548 | + int iIdxCur = pLevel->iIdxCur; |
| 117549 | + sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur); |
| 117550 | + VdbeCoverageIf(v, bRev==0); |
| 117551 | + VdbeCoverageIf(v, bRev!=0); |
| 117552 | + VdbeComment((v, "begin skip-scan on %s", pIdx->zName)); |
| 117553 | + j = sqlite3VdbeAddOp0(v, OP_Goto); |
| 117554 | + pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT), |
| 117555 | + iIdxCur, 0, regBase, nSkip); |
| 117556 | + VdbeCoverageIf(v, bRev==0); |
| 117557 | + VdbeCoverageIf(v, bRev!=0); |
| 117558 | + sqlite3VdbeJumpHere(v, j); |
| 117559 | + for(j=0; j<nSkip; j++){ |
| 117560 | + sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j); |
| 117561 | + assert( pIdx->aiColumn[j]>=0 ); |
| 117562 | + VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName)); |
| 117563 | + } |
| 117564 | + } |
| 117565 | + |
| 117566 | + /* Evaluate the equality constraints |
| 117567 | + */ |
| 117568 | + assert( zAff==0 || (int)strlen(zAff)>=nEq ); |
| 117569 | + for(j=nSkip; j<nEq; j++){ |
| 117570 | + int r1; |
| 117571 | + pTerm = pLoop->aLTerm[j]; |
| 117572 | + assert( pTerm!=0 ); |
| 117573 | + /* The following testcase is true for indices with redundant columns. |
| 117574 | + ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 117575 | + testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
| 117576 | + testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 117577 | + r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
| 117578 | + if( r1!=regBase+j ){ |
| 117579 | + if( nReg==1 ){ |
| 117580 | + sqlite3ReleaseTempReg(pParse, regBase); |
| 117581 | + regBase = r1; |
| 117582 | + }else{ |
| 117583 | + sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); |
| 117584 | + } |
| 117585 | + } |
| 117586 | + testcase( pTerm->eOperator & WO_ISNULL ); |
| 117587 | + testcase( pTerm->eOperator & WO_IN ); |
| 117588 | + if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ |
| 117589 | + Expr *pRight = pTerm->pExpr->pRight; |
| 117590 | + if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){ |
| 117591 | + sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk); |
| 117592 | + VdbeCoverage(v); |
| 117593 | + } |
| 117594 | + if( zAff ){ |
| 117595 | + if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){ |
| 117596 | + zAff[j] = SQLITE_AFF_BLOB; |
| 117597 | + } |
| 117598 | + if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ |
| 117599 | + zAff[j] = SQLITE_AFF_BLOB; |
| 117600 | + } |
| 117601 | + } |
| 117602 | + } |
| 117603 | + } |
| 117604 | + *pzAff = zAff; |
| 117605 | + return regBase; |
| 117606 | +} |
| 117607 | + |
| 117608 | +/* |
| 117609 | +** If the most recently coded instruction is a constant range contraint |
| 117610 | +** that originated from the LIKE optimization, then change the P3 to be |
| 117611 | +** pLoop->iLikeRepCntr and set P5. |
| 117612 | +** |
| 117613 | +** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range |
| 117614 | +** expression: "x>='ABC' AND x<'abd'". But this requires that the range |
| 117615 | +** scan loop run twice, once for strings and a second time for BLOBs. |
| 117616 | +** The OP_String opcodes on the second pass convert the upper and lower |
| 117617 | +** bound string contants to blobs. This routine makes the necessary changes |
| 117618 | +** to the OP_String opcodes for that to happen. |
| 117619 | +*/ |
| 117620 | +static void whereLikeOptimizationStringFixup( |
| 117621 | + Vdbe *v, /* prepared statement under construction */ |
| 117622 | + WhereLevel *pLevel, /* The loop that contains the LIKE operator */ |
| 117623 | + WhereTerm *pTerm /* The upper or lower bound just coded */ |
| 117624 | +){ |
| 117625 | + if( pTerm->wtFlags & TERM_LIKEOPT ){ |
| 117626 | + VdbeOp *pOp; |
| 117627 | + assert( pLevel->iLikeRepCntr>0 ); |
| 117628 | + pOp = sqlite3VdbeGetOp(v, -1); |
| 117629 | + assert( pOp!=0 ); |
| 117630 | + assert( pOp->opcode==OP_String8 |
| 117631 | + || pTerm->pWC->pWInfo->pParse->db->mallocFailed ); |
| 117632 | + pOp->p3 = pLevel->iLikeRepCntr; |
| 117633 | + pOp->p5 = 1; |
| 117634 | + } |
| 117635 | +} |
| 117636 | + |
| 117637 | + |
| 117638 | +/* |
| 117639 | +** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 117640 | +** implementation described by pWInfo. |
| 117641 | +*/ |
| 117642 | +SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart( |
| 117643 | + WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 117644 | + int iLevel, /* Which level of pWInfo->a[] should be coded */ |
| 117645 | + Bitmask notReady /* Which tables are currently available */ |
| 117646 | +){ |
| 117647 | + int j, k; /* Loop counters */ |
| 117648 | + int iCur; /* The VDBE cursor for the table */ |
| 117649 | + int addrNxt; /* Where to jump to continue with the next IN case */ |
| 117650 | + int omitTable; /* True if we use the index only */ |
| 117651 | + int bRev; /* True if we need to scan in reverse order */ |
| 117652 | + WhereLevel *pLevel; /* The where level to be coded */ |
| 117653 | + WhereLoop *pLoop; /* The WhereLoop object being coded */ |
| 117654 | + WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 117655 | + WhereTerm *pTerm; /* A WHERE clause term */ |
| 117656 | + Parse *pParse; /* Parsing context */ |
| 117657 | + sqlite3 *db; /* Database connection */ |
| 117658 | + Vdbe *v; /* The prepared stmt under constructions */ |
| 117659 | + struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
| 117660 | + int addrBrk; /* Jump here to break out of the loop */ |
| 117661 | + int addrCont; /* Jump here to continue with next cycle */ |
| 117662 | + int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ |
| 117663 | + int iReleaseReg = 0; /* Temp register to free before returning */ |
| 117664 | + |
| 117665 | + pParse = pWInfo->pParse; |
| 117666 | + v = pParse->pVdbe; |
| 117667 | + pWC = &pWInfo->sWC; |
| 117668 | + db = pParse->db; |
| 117669 | + pLevel = &pWInfo->a[iLevel]; |
| 117670 | + pLoop = pLevel->pWLoop; |
| 117671 | + pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 117672 | + iCur = pTabItem->iCursor; |
| 117673 | + pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); |
| 117674 | + bRev = (pWInfo->revMask>>iLevel)&1; |
| 117675 | + omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
| 117676 | + && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; |
| 117677 | + VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName)); |
| 117678 | + |
| 117679 | + /* Create labels for the "break" and "continue" instructions |
| 117680 | + ** for the current loop. Jump to addrBrk to break out of a loop. |
| 117681 | + ** Jump to cont to go immediately to the next iteration of the |
| 117682 | + ** loop. |
| 117683 | + ** |
| 117684 | + ** When there is an IN operator, we also have a "addrNxt" label that |
| 117685 | + ** means to continue with the next IN value combination. When |
| 117686 | + ** there are no IN operators in the constraints, the "addrNxt" label |
| 117687 | + ** is the same as "addrBrk". |
| 117688 | + */ |
| 117689 | + addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 117690 | + addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); |
| 117691 | + |
| 117692 | + /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 117693 | + ** initialize a memory cell that records if this table matches any |
| 117694 | + ** row of the left table of the join. |
| 117695 | + */ |
| 117696 | + if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){ |
| 117697 | + pLevel->iLeftJoin = ++pParse->nMem; |
| 117698 | + sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); |
| 117699 | + VdbeComment((v, "init LEFT JOIN no-match flag")); |
| 117700 | + } |
| 117701 | + |
| 117702 | + /* Special case of a FROM clause subquery implemented as a co-routine */ |
| 117703 | + if( pTabItem->viaCoroutine ){ |
| 117704 | + int regYield = pTabItem->regReturn; |
| 117705 | + sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub); |
| 117706 | + pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk); |
| 117707 | + VdbeCoverage(v); |
| 117708 | + VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName)); |
| 117709 | + pLevel->op = OP_Goto; |
| 117710 | + }else |
| 117711 | + |
| 117712 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 117713 | + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 117714 | + /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
| 117715 | + ** to access the data. |
| 117716 | + */ |
| 117717 | + int iReg; /* P3 Value for OP_VFilter */ |
| 117718 | + int addrNotFound; |
| 117719 | + int nConstraint = pLoop->nLTerm; |
| 117720 | + |
| 117721 | + sqlite3ExprCachePush(pParse); |
| 117722 | + iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
| 117723 | + addrNotFound = pLevel->addrBrk; |
| 117724 | + for(j=0; j<nConstraint; j++){ |
| 117725 | + int iTarget = iReg+j+2; |
| 117726 | + pTerm = pLoop->aLTerm[j]; |
| 117727 | + if( pTerm==0 ) continue; |
| 117728 | + if( pTerm->eOperator & WO_IN ){ |
| 117729 | + codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 117730 | + addrNotFound = pLevel->addrNxt; |
| 117731 | + }else{ |
| 117732 | + sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); |
| 117733 | + } |
| 117734 | + } |
| 117735 | + sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
| 117736 | + sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
| 117737 | + sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 117738 | + pLoop->u.vtab.idxStr, |
| 117739 | + pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); |
| 117740 | + VdbeCoverage(v); |
| 117741 | + pLoop->u.vtab.needFree = 0; |
| 117742 | + for(j=0; j<nConstraint && j<16; j++){ |
| 117743 | + if( (pLoop->u.vtab.omitMask>>j)&1 ){ |
| 117744 | + disableTerm(pLevel, pLoop->aLTerm[j]); |
| 117745 | + } |
| 117746 | + } |
| 117747 | + pLevel->op = OP_VNext; |
| 117748 | + pLevel->p1 = iCur; |
| 117749 | + pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 117750 | + sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
| 117751 | + sqlite3ExprCachePop(pParse); |
| 117752 | + }else |
| 117753 | +#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 117754 | + |
| 117755 | + if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 117756 | + && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 117757 | + ){ |
| 117758 | + /* Case 2: We can directly reference a single row using an |
| 117759 | + ** equality comparison against the ROWID field. Or |
| 117760 | + ** we reference multiple rows using a "rowid IN (...)" |
| 117761 | + ** construct. |
| 117762 | + */ |
| 117763 | + assert( pLoop->u.btree.nEq==1 ); |
| 117764 | + pTerm = pLoop->aLTerm[0]; |
| 117765 | + assert( pTerm!=0 ); |
| 117766 | + assert( pTerm->pExpr!=0 ); |
| 117767 | + assert( omitTable==0 ); |
| 117768 | + testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 117769 | + iReleaseReg = ++pParse->nMem; |
| 117770 | + iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
| 117771 | + if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg); |
| 117772 | + addrNxt = pLevel->addrNxt; |
| 117773 | + sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v); |
| 117774 | + sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
| 117775 | + VdbeCoverage(v); |
| 117776 | + sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
| 117777 | + sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 117778 | + VdbeComment((v, "pk")); |
| 117779 | + pLevel->op = OP_Noop; |
| 117780 | + }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 117781 | + && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 117782 | + ){ |
| 117783 | + /* Case 3: We have an inequality comparison against the ROWID field. |
| 117784 | + */ |
| 117785 | + int testOp = OP_Noop; |
| 117786 | + int start; |
| 117787 | + int memEndValue = 0; |
| 117788 | + WhereTerm *pStart, *pEnd; |
| 117789 | + |
| 117790 | + assert( omitTable==0 ); |
| 117791 | + j = 0; |
| 117792 | + pStart = pEnd = 0; |
| 117793 | + if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; |
| 117794 | + if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; |
| 117795 | + assert( pStart!=0 || pEnd!=0 ); |
| 117796 | + if( bRev ){ |
| 117797 | + pTerm = pStart; |
| 117798 | + pStart = pEnd; |
| 117799 | + pEnd = pTerm; |
| 117800 | + } |
| 117801 | + if( pStart ){ |
| 117802 | + Expr *pX; /* The expression that defines the start bound */ |
| 117803 | + int r1, rTemp; /* Registers for holding the start boundary */ |
| 117804 | + |
| 117805 | + /* The following constant maps TK_xx codes into corresponding |
| 117806 | + ** seek opcodes. It depends on a particular ordering of TK_xx |
| 117807 | + */ |
| 117808 | + const u8 aMoveOp[] = { |
| 117809 | + /* TK_GT */ OP_SeekGT, |
| 117810 | + /* TK_LE */ OP_SeekLE, |
| 117811 | + /* TK_LT */ OP_SeekLT, |
| 117812 | + /* TK_GE */ OP_SeekGE |
| 117813 | + }; |
| 117814 | + assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ |
| 117815 | + assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ |
| 117816 | + assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ |
| 117817 | + |
| 117818 | + assert( (pStart->wtFlags & TERM_VNULL)==0 ); |
| 117819 | + testcase( pStart->wtFlags & TERM_VIRTUAL ); |
| 117820 | + pX = pStart->pExpr; |
| 117821 | + assert( pX!=0 ); |
| 117822 | + testcase( pStart->leftCursor!=iCur ); /* transitive constraints */ |
| 117823 | + r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); |
| 117824 | + sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); |
| 117825 | + VdbeComment((v, "pk")); |
| 117826 | + VdbeCoverageIf(v, pX->op==TK_GT); |
| 117827 | + VdbeCoverageIf(v, pX->op==TK_LE); |
| 117828 | + VdbeCoverageIf(v, pX->op==TK_LT); |
| 117829 | + VdbeCoverageIf(v, pX->op==TK_GE); |
| 117830 | + sqlite3ExprCacheAffinityChange(pParse, r1, 1); |
| 117831 | + sqlite3ReleaseTempReg(pParse, rTemp); |
| 117832 | + disableTerm(pLevel, pStart); |
| 117833 | + }else{ |
| 117834 | + sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); |
| 117835 | + VdbeCoverageIf(v, bRev==0); |
| 117836 | + VdbeCoverageIf(v, bRev!=0); |
| 117837 | + } |
| 117838 | + if( pEnd ){ |
| 117839 | + Expr *pX; |
| 117840 | + pX = pEnd->pExpr; |
| 117841 | + assert( pX!=0 ); |
| 117842 | + assert( (pEnd->wtFlags & TERM_VNULL)==0 ); |
| 117843 | + testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */ |
| 117844 | + testcase( pEnd->wtFlags & TERM_VIRTUAL ); |
| 117845 | + memEndValue = ++pParse->nMem; |
| 117846 | + sqlite3ExprCode(pParse, pX->pRight, memEndValue); |
| 117847 | + if( pX->op==TK_LT || pX->op==TK_GT ){ |
| 117848 | + testOp = bRev ? OP_Le : OP_Ge; |
| 117849 | + }else{ |
| 117850 | + testOp = bRev ? OP_Lt : OP_Gt; |
| 117851 | + } |
| 117852 | + disableTerm(pLevel, pEnd); |
| 117853 | + } |
| 117854 | + start = sqlite3VdbeCurrentAddr(v); |
| 117855 | + pLevel->op = bRev ? OP_Prev : OP_Next; |
| 117856 | + pLevel->p1 = iCur; |
| 117857 | + pLevel->p2 = start; |
| 117858 | + assert( pLevel->p5==0 ); |
| 117859 | + if( testOp!=OP_Noop ){ |
| 117860 | + iRowidReg = ++pParse->nMem; |
| 117861 | + sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
| 117862 | + sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 117863 | + sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| 117864 | + VdbeCoverageIf(v, testOp==OP_Le); |
| 117865 | + VdbeCoverageIf(v, testOp==OP_Lt); |
| 117866 | + VdbeCoverageIf(v, testOp==OP_Ge); |
| 117867 | + VdbeCoverageIf(v, testOp==OP_Gt); |
| 117868 | + sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
| 117869 | + } |
| 117870 | + }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
| 117871 | + /* Case 4: A scan using an index. |
| 117872 | + ** |
| 117873 | + ** The WHERE clause may contain zero or more equality |
| 117874 | + ** terms ("==" or "IN" operators) that refer to the N |
| 117875 | + ** left-most columns of the index. It may also contain |
| 117876 | + ** inequality constraints (>, <, >= or <=) on the indexed |
| 117877 | + ** column that immediately follows the N equalities. Only |
| 117878 | + ** the right-most column can be an inequality - the rest must |
| 117879 | + ** use the "==" and "IN" operators. For example, if the |
| 117880 | + ** index is on (x,y,z), then the following clauses are all |
| 117881 | + ** optimized: |
| 117882 | + ** |
| 117883 | + ** x=5 |
| 117884 | + ** x=5 AND y=10 |
| 117885 | + ** x=5 AND y<10 |
| 117886 | + ** x=5 AND y>5 AND y<10 |
| 117887 | + ** x=5 AND y=5 AND z<=10 |
| 117888 | + ** |
| 117889 | + ** The z<10 term of the following cannot be used, only |
| 117890 | + ** the x=5 term: |
| 117891 | + ** |
| 117892 | + ** x=5 AND z<10 |
| 117893 | + ** |
| 117894 | + ** N may be zero if there are inequality constraints. |
| 117895 | + ** If there are no inequality constraints, then N is at |
| 117896 | + ** least one. |
| 117897 | + ** |
| 117898 | + ** This case is also used when there are no WHERE clause |
| 117899 | + ** constraints but an index is selected anyway, in order |
| 117900 | + ** to force the output order to conform to an ORDER BY. |
| 117901 | + */ |
| 117902 | + static const u8 aStartOp[] = { |
| 117903 | + 0, |
| 117904 | + 0, |
| 117905 | + OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 117906 | + OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
| 117907 | + OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */ |
| 117908 | + OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */ |
| 117909 | + OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */ |
| 117910 | + OP_SeekLE /* 7: (start_constraints && startEq && bRev) */ |
| 117911 | + }; |
| 117912 | + static const u8 aEndOp[] = { |
| 117913 | + OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */ |
| 117914 | + OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */ |
| 117915 | + OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */ |
| 117916 | + OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */ |
| 117917 | + }; |
| 117918 | + u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
| 117919 | + int regBase; /* Base register holding constraint values */ |
| 117920 | + WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 117921 | + WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 117922 | + int startEq; /* True if range start uses ==, >= or <= */ |
| 117923 | + int endEq; /* True if range end uses ==, >= or <= */ |
| 117924 | + int start_constraints; /* Start of range is constrained */ |
| 117925 | + int nConstraint; /* Number of constraint terms */ |
| 117926 | + Index *pIdx; /* The index we will be using */ |
| 117927 | + int iIdxCur; /* The VDBE cursor for the index */ |
| 117928 | + int nExtraReg = 0; /* Number of extra registers needed */ |
| 117929 | + int op; /* Instruction opcode */ |
| 117930 | + char *zStartAff; /* Affinity for start of range constraint */ |
| 117931 | + char cEndAff = 0; /* Affinity for end of range constraint */ |
| 117932 | + u8 bSeekPastNull = 0; /* True to seek past initial nulls */ |
| 117933 | + u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */ |
| 117934 | + |
| 117935 | + pIdx = pLoop->u.btree.pIndex; |
| 117936 | + iIdxCur = pLevel->iIdxCur; |
| 117937 | + assert( nEq>=pLoop->nSkip ); |
| 117938 | + |
| 117939 | + /* If this loop satisfies a sort order (pOrderBy) request that |
| 117940 | + ** was passed to this function to implement a "SELECT min(x) ..." |
| 117941 | + ** query, then the caller will only allow the loop to run for |
| 117942 | + ** a single iteration. This means that the first row returned |
| 117943 | + ** should not have a NULL value stored in 'x'. If column 'x' is |
| 117944 | + ** the first one after the nEq equality constraints in the index, |
| 117945 | + ** this requires some special handling. |
| 117946 | + */ |
| 117947 | + assert( pWInfo->pOrderBy==0 |
| 117948 | + || pWInfo->pOrderBy->nExpr==1 |
| 117949 | + || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 ); |
| 117950 | + if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
| 117951 | + && pWInfo->nOBSat>0 |
| 117952 | + && (pIdx->nKeyCol>nEq) |
| 117953 | + ){ |
| 117954 | + assert( pLoop->nSkip==0 ); |
| 117955 | + bSeekPastNull = 1; |
| 117956 | + nExtraReg = 1; |
| 117957 | + } |
| 117958 | + |
| 117959 | + /* Find any inequality constraint terms for the start and end |
| 117960 | + ** of the range. |
| 117961 | + */ |
| 117962 | + j = nEq; |
| 117963 | + if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
| 117964 | + pRangeStart = pLoop->aLTerm[j++]; |
| 117965 | + nExtraReg = 1; |
| 117966 | + /* Like optimization range constraints always occur in pairs */ |
| 117967 | + assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || |
| 117968 | + (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 ); |
| 117969 | + } |
| 117970 | + if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
| 117971 | + pRangeEnd = pLoop->aLTerm[j++]; |
| 117972 | + nExtraReg = 1; |
| 117973 | + if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){ |
| 117974 | + assert( pRangeStart!=0 ); /* LIKE opt constraints */ |
| 117975 | + assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */ |
| 117976 | + pLevel->iLikeRepCntr = ++pParse->nMem; |
| 117977 | + testcase( bRev ); |
| 117978 | + testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC ); |
| 117979 | + sqlite3VdbeAddOp2(v, OP_Integer, |
| 117980 | + bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC), |
| 117981 | + pLevel->iLikeRepCntr); |
| 117982 | + VdbeComment((v, "LIKE loop counter")); |
| 117983 | + pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v); |
| 117984 | + } |
| 117985 | + if( pRangeStart==0 |
| 117986 | + && (j = pIdx->aiColumn[nEq])>=0 |
| 117987 | + && pIdx->pTable->aCol[j].notNull==0 |
| 117988 | + ){ |
| 117989 | + bSeekPastNull = 1; |
| 117990 | + } |
| 117991 | + } |
| 117992 | + assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 ); |
| 117993 | + |
| 117994 | + /* Generate code to evaluate all constraint terms using == or IN |
| 117995 | + ** and store the values of those terms in an array of registers |
| 117996 | + ** starting at regBase. |
| 117997 | + */ |
| 117998 | + regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); |
| 117999 | + assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq ); |
| 118000 | + if( zStartAff ) cEndAff = zStartAff[nEq]; |
| 118001 | + addrNxt = pLevel->addrNxt; |
| 118002 | + |
| 118003 | + /* If we are doing a reverse order scan on an ascending index, or |
| 118004 | + ** a forward order scan on a descending index, interchange the |
| 118005 | + ** start and end terms (pRangeStart and pRangeEnd). |
| 118006 | + */ |
| 118007 | + if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) |
| 118008 | + || (bRev && pIdx->nKeyCol==nEq) |
| 118009 | + ){ |
| 118010 | + SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
| 118011 | + SWAP(u8, bSeekPastNull, bStopAtNull); |
| 118012 | + } |
| 118013 | + |
| 118014 | + testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 ); |
| 118015 | + testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 ); |
| 118016 | + testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 ); |
| 118017 | + testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 ); |
| 118018 | + startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); |
| 118019 | + endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); |
| 118020 | + start_constraints = pRangeStart || nEq>0; |
| 118021 | + |
| 118022 | + /* Seek the index cursor to the start of the range. */ |
| 118023 | + nConstraint = nEq; |
| 118024 | + if( pRangeStart ){ |
| 118025 | + Expr *pRight = pRangeStart->pExpr->pRight; |
| 118026 | + sqlite3ExprCode(pParse, pRight, regBase+nEq); |
| 118027 | + whereLikeOptimizationStringFixup(v, pLevel, pRangeStart); |
| 118028 | + if( (pRangeStart->wtFlags & TERM_VNULL)==0 |
| 118029 | + && sqlite3ExprCanBeNull(pRight) |
| 118030 | + ){ |
| 118031 | + sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 118032 | + VdbeCoverage(v); |
| 118033 | + } |
| 118034 | + if( zStartAff ){ |
| 118035 | + if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_BLOB){ |
| 118036 | + /* Since the comparison is to be performed with no conversions |
| 118037 | + ** applied to the operands, set the affinity to apply to pRight to |
| 118038 | + ** SQLITE_AFF_BLOB. */ |
| 118039 | + zStartAff[nEq] = SQLITE_AFF_BLOB; |
| 118040 | + } |
| 118041 | + if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ |
| 118042 | + zStartAff[nEq] = SQLITE_AFF_BLOB; |
| 118043 | + } |
| 118044 | + } |
| 118045 | + nConstraint++; |
| 118046 | + testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); |
| 118047 | + }else if( bSeekPastNull ){ |
| 118048 | + sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 118049 | + nConstraint++; |
| 118050 | + startEq = 0; |
| 118051 | + start_constraints = 1; |
| 118052 | + } |
| 118053 | + codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff); |
| 118054 | + op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; |
| 118055 | + assert( op!=0 ); |
| 118056 | + sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
| 118057 | + VdbeCoverage(v); |
| 118058 | + VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind ); |
| 118059 | + VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last ); |
| 118060 | + VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT ); |
| 118061 | + VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE ); |
| 118062 | + VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE ); |
| 118063 | + VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT ); |
| 118064 | + |
| 118065 | + /* Load the value for the inequality constraint at the end of the |
| 118066 | + ** range (if any). |
| 118067 | + */ |
| 118068 | + nConstraint = nEq; |
| 118069 | + if( pRangeEnd ){ |
| 118070 | + Expr *pRight = pRangeEnd->pExpr->pRight; |
| 118071 | + sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
| 118072 | + sqlite3ExprCode(pParse, pRight, regBase+nEq); |
| 118073 | + whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd); |
| 118074 | + if( (pRangeEnd->wtFlags & TERM_VNULL)==0 |
| 118075 | + && sqlite3ExprCanBeNull(pRight) |
| 118076 | + ){ |
| 118077 | + sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 118078 | + VdbeCoverage(v); |
| 118079 | + } |
| 118080 | + if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_BLOB |
| 118081 | + && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff) |
| 118082 | + ){ |
| 118083 | + codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff); |
| 118084 | + } |
| 118085 | + nConstraint++; |
| 118086 | + testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); |
| 118087 | + }else if( bStopAtNull ){ |
| 118088 | + sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 118089 | + endEq = 0; |
| 118090 | + nConstraint++; |
| 118091 | + } |
| 118092 | + sqlite3DbFree(db, zStartAff); |
| 118093 | + |
| 118094 | + /* Top of the loop body */ |
| 118095 | + pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 118096 | + |
| 118097 | + /* Check if the index cursor is past the end of the range. */ |
| 118098 | + if( nConstraint ){ |
| 118099 | + op = aEndOp[bRev*2 + endEq]; |
| 118100 | + sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
| 118101 | + testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT ); |
| 118102 | + testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE ); |
| 118103 | + testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT ); |
| 118104 | + testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE ); |
| 118105 | + } |
| 118106 | + |
| 118107 | + /* Seek the table cursor, if required */ |
| 118108 | + disableTerm(pLevel, pRangeStart); |
| 118109 | + disableTerm(pLevel, pRangeEnd); |
| 118110 | + if( omitTable ){ |
| 118111 | + /* pIdx is a covering index. No need to access the main table. */ |
| 118112 | + }else if( HasRowid(pIdx->pTable) ){ |
| 118113 | + iRowidReg = ++pParse->nMem; |
| 118114 | + sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); |
| 118115 | + sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 118116 | + sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */ |
| 118117 | + }else if( iCur!=iIdxCur ){ |
| 118118 | + Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); |
| 118119 | + iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol); |
| 118120 | + for(j=0; j<pPk->nKeyCol; j++){ |
| 118121 | + k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]); |
| 118122 | + sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j); |
| 118123 | + } |
| 118124 | + sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont, |
| 118125 | + iRowidReg, pPk->nKeyCol); VdbeCoverage(v); |
| 118126 | + } |
| 118127 | + |
| 118128 | + /* Record the instruction used to terminate the loop. Disable |
| 118129 | + ** WHERE clause terms made redundant by the index range scan. |
| 118130 | + */ |
| 118131 | + if( pLoop->wsFlags & WHERE_ONEROW ){ |
| 118132 | + pLevel->op = OP_Noop; |
| 118133 | + }else if( bRev ){ |
| 118134 | + pLevel->op = OP_Prev; |
| 118135 | + }else{ |
| 118136 | + pLevel->op = OP_Next; |
| 118137 | + } |
| 118138 | + pLevel->p1 = iIdxCur; |
| 118139 | + pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0; |
| 118140 | + if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ |
| 118141 | + pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 118142 | + }else{ |
| 118143 | + assert( pLevel->p5==0 ); |
| 118144 | + } |
| 118145 | + }else |
| 118146 | + |
| 118147 | +#ifndef SQLITE_OMIT_OR_OPTIMIZATION |
| 118148 | + if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 118149 | + /* Case 5: Two or more separately indexed terms connected by OR |
| 118150 | + ** |
| 118151 | + ** Example: |
| 118152 | + ** |
| 118153 | + ** CREATE TABLE t1(a,b,c,d); |
| 118154 | + ** CREATE INDEX i1 ON t1(a); |
| 118155 | + ** CREATE INDEX i2 ON t1(b); |
| 118156 | + ** CREATE INDEX i3 ON t1(c); |
| 118157 | + ** |
| 118158 | + ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) |
| 118159 | + ** |
| 118160 | + ** In the example, there are three indexed terms connected by OR. |
| 118161 | + ** The top of the loop looks like this: |
| 118162 | + ** |
| 118163 | + ** Null 1 # Zero the rowset in reg 1 |
| 118164 | + ** |
| 118165 | + ** Then, for each indexed term, the following. The arguments to |
| 118166 | + ** RowSetTest are such that the rowid of the current row is inserted |
| 118167 | + ** into the RowSet. If it is already present, control skips the |
| 118168 | + ** Gosub opcode and jumps straight to the code generated by WhereEnd(). |
| 118169 | + ** |
| 118170 | + ** sqlite3WhereBegin(<term>) |
| 118171 | + ** RowSetTest # Insert rowid into rowset |
| 118172 | + ** Gosub 2 A |
| 118173 | + ** sqlite3WhereEnd() |
| 118174 | + ** |
| 118175 | + ** Following the above, code to terminate the loop. Label A, the target |
| 118176 | + ** of the Gosub above, jumps to the instruction right after the Goto. |
| 118177 | + ** |
| 118178 | + ** Null 1 # Zero the rowset in reg 1 |
| 118179 | + ** Goto B # The loop is finished. |
| 118180 | + ** |
| 118181 | + ** A: <loop body> # Return data, whatever. |
| 118182 | + ** |
| 118183 | + ** Return 2 # Jump back to the Gosub |
| 118184 | + ** |
| 118185 | + ** B: <after the loop> |
| 118186 | + ** |
| 118187 | + ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then |
| 118188 | + ** use an ephemeral index instead of a RowSet to record the primary |
| 118189 | + ** keys of the rows we have already seen. |
| 118190 | + ** |
| 118191 | + */ |
| 118192 | + WhereClause *pOrWc; /* The OR-clause broken out into subterms */ |
| 118193 | + SrcList *pOrTab; /* Shortened table list or OR-clause generation */ |
| 118194 | + Index *pCov = 0; /* Potential covering index (or NULL) */ |
| 118195 | + int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ |
| 118196 | + |
| 118197 | + int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ |
| 118198 | + int regRowset = 0; /* Register for RowSet object */ |
| 118199 | + int regRowid = 0; /* Register holding rowid */ |
| 118200 | + int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ |
| 118201 | + int iRetInit; /* Address of regReturn init */ |
| 118202 | + int untestedTerms = 0; /* Some terms not completely tested */ |
| 118203 | + int ii; /* Loop counter */ |
| 118204 | + u16 wctrlFlags; /* Flags for sub-WHERE clause */ |
| 118205 | + Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
| 118206 | + Table *pTab = pTabItem->pTab; |
| 118207 | + |
| 118208 | + pTerm = pLoop->aLTerm[0]; |
| 118209 | + assert( pTerm!=0 ); |
| 118210 | + assert( pTerm->eOperator & WO_OR ); |
| 118211 | + assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 118212 | + pOrWc = &pTerm->u.pOrInfo->wc; |
| 118213 | + pLevel->op = OP_Return; |
| 118214 | + pLevel->p1 = regReturn; |
| 118215 | + |
| 118216 | + /* Set up a new SrcList in pOrTab containing the table being scanned |
| 118217 | + ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. |
| 118218 | + ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). |
| 118219 | + */ |
| 118220 | + if( pWInfo->nLevel>1 ){ |
| 118221 | + int nNotReady; /* The number of notReady tables */ |
| 118222 | + struct SrcList_item *origSrc; /* Original list of tables */ |
| 118223 | + nNotReady = pWInfo->nLevel - iLevel - 1; |
| 118224 | + pOrTab = sqlite3StackAllocRaw(db, |
| 118225 | + sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); |
| 118226 | + if( pOrTab==0 ) return notReady; |
| 118227 | + pOrTab->nAlloc = (u8)(nNotReady + 1); |
| 118228 | + pOrTab->nSrc = pOrTab->nAlloc; |
| 118229 | + memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| 118230 | + origSrc = pWInfo->pTabList->a; |
| 118231 | + for(k=1; k<=nNotReady; k++){ |
| 118232 | + memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); |
| 118233 | + } |
| 118234 | + }else{ |
| 118235 | + pOrTab = pWInfo->pTabList; |
| 118236 | + } |
| 118237 | + |
| 118238 | + /* Initialize the rowset register to contain NULL. An SQL NULL is |
| 118239 | + ** equivalent to an empty rowset. Or, create an ephemeral index |
| 118240 | + ** capable of holding primary keys in the case of a WITHOUT ROWID. |
| 118241 | + ** |
| 118242 | + ** Also initialize regReturn to contain the address of the instruction |
| 118243 | + ** immediately following the OP_Return at the bottom of the loop. This |
| 118244 | + ** is required in a few obscure LEFT JOIN cases where control jumps |
| 118245 | + ** over the top of the loop into the body of it. In this case the |
| 118246 | + ** correct response for the end-of-loop code (the OP_Return) is to |
| 118247 | + ** fall through to the next instruction, just as an OP_Next does if |
| 118248 | + ** called on an uninitialized cursor. |
| 118249 | + */ |
| 118250 | + if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 118251 | + if( HasRowid(pTab) ){ |
| 118252 | + regRowset = ++pParse->nMem; |
| 118253 | + sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 118254 | + }else{ |
| 118255 | + Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 118256 | + regRowset = pParse->nTab++; |
| 118257 | + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol); |
| 118258 | + sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
| 118259 | + } |
| 118260 | + regRowid = ++pParse->nMem; |
| 118261 | + } |
| 118262 | + iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 118263 | + |
| 118264 | + /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 118265 | + ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 118266 | + ** That way, terms in y that are factored into the disjunction will |
| 118267 | + ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
| 118268 | + ** |
| 118269 | + ** Actually, each subexpression is converted to "xN AND w" where w is |
| 118270 | + ** the "interesting" terms of z - terms that did not originate in the |
| 118271 | + ** ON or USING clause of a LEFT JOIN, and terms that are usable as |
| 118272 | + ** indices. |
| 118273 | + ** |
| 118274 | + ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 118275 | + ** is not contained in the ON clause of a LEFT JOIN. |
| 118276 | + ** See ticket http://www.sqlite.org/src/info/f2369304e4 |
| 118277 | + */ |
| 118278 | + if( pWC->nTerm>1 ){ |
| 118279 | + int iTerm; |
| 118280 | + for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 118281 | + Expr *pExpr = pWC->a[iTerm].pExpr; |
| 118282 | + if( &pWC->a[iTerm] == pTerm ) continue; |
| 118283 | + if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; |
| 118284 | + if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue; |
| 118285 | + if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
| 118286 | + testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO ); |
| 118287 | + pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 118288 | + pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr); |
| 118289 | + } |
| 118290 | + if( pAndExpr ){ |
| 118291 | + pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0); |
| 118292 | + } |
| 118293 | + } |
| 118294 | + |
| 118295 | + /* Run a separate WHERE clause for each term of the OR clause. After |
| 118296 | + ** eliminating duplicates from other WHERE clauses, the action for each |
| 118297 | + ** sub-WHERE clause is to to invoke the main loop body as a subroutine. |
| 118298 | + */ |
| 118299 | + wctrlFlags = WHERE_OMIT_OPEN_CLOSE |
| 118300 | + | WHERE_FORCE_TABLE |
| 118301 | + | WHERE_ONETABLE_ONLY |
| 118302 | + | WHERE_NO_AUTOINDEX; |
| 118303 | + for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 118304 | + WhereTerm *pOrTerm = &pOrWc->a[ii]; |
| 118305 | + if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
| 118306 | + WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
| 118307 | + Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */ |
| 118308 | + int j1 = 0; /* Address of jump operation */ |
| 118309 | + if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ |
| 118310 | + pAndExpr->pLeft = pOrExpr; |
| 118311 | + pOrExpr = pAndExpr; |
| 118312 | + } |
| 118313 | + /* Loop through table entries that match term pOrTerm. */ |
| 118314 | + WHERETRACE(0xffff, ("Subplan for OR-clause:\n")); |
| 118315 | + pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 118316 | + wctrlFlags, iCovCur); |
| 118317 | + assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
| 118318 | + if( pSubWInfo ){ |
| 118319 | + WhereLoop *pSubLoop; |
| 118320 | + int addrExplain = sqlite3WhereExplainOneScan( |
| 118321 | + pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
| 118322 | + ); |
| 118323 | + sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain); |
| 118324 | + |
| 118325 | + /* This is the sub-WHERE clause body. First skip over |
| 118326 | + ** duplicate rows from prior sub-WHERE clauses, and record the |
| 118327 | + ** rowid (or PRIMARY KEY) for the current row so that the same |
| 118328 | + ** row will be skipped in subsequent sub-WHERE clauses. |
| 118329 | + */ |
| 118330 | + if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 118331 | + int r; |
| 118332 | + int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 118333 | + if( HasRowid(pTab) ){ |
| 118334 | + r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0); |
| 118335 | + j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet); |
| 118336 | + VdbeCoverage(v); |
| 118337 | + }else{ |
| 118338 | + Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 118339 | + int nPk = pPk->nKeyCol; |
| 118340 | + int iPk; |
| 118341 | + |
| 118342 | + /* Read the PK into an array of temp registers. */ |
| 118343 | + r = sqlite3GetTempRange(pParse, nPk); |
| 118344 | + for(iPk=0; iPk<nPk; iPk++){ |
| 118345 | + int iCol = pPk->aiColumn[iPk]; |
| 118346 | + sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0); |
| 118347 | + } |
| 118348 | + |
| 118349 | + /* Check if the temp table already contains this key. If so, |
| 118350 | + ** the row has already been included in the result set and |
| 118351 | + ** can be ignored (by jumping past the Gosub below). Otherwise, |
| 118352 | + ** insert the key into the temp table and proceed with processing |
| 118353 | + ** the row. |
| 118354 | + ** |
| 118355 | + ** Use some of the same optimizations as OP_RowSetTest: If iSet |
| 118356 | + ** is zero, assume that the key cannot already be present in |
| 118357 | + ** the temp table. And if iSet is -1, assume that there is no |
| 118358 | + ** need to insert the key into the temp table, as it will never |
| 118359 | + ** be tested for. */ |
| 118360 | + if( iSet ){ |
| 118361 | + j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk); |
| 118362 | + VdbeCoverage(v); |
| 118363 | + } |
| 118364 | + if( iSet>=0 ){ |
| 118365 | + sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid); |
| 118366 | + sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0); |
| 118367 | + if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 118368 | + } |
| 118369 | + |
| 118370 | + /* Release the array of temp registers */ |
| 118371 | + sqlite3ReleaseTempRange(pParse, r, nPk); |
| 118372 | + } |
| 118373 | + } |
| 118374 | + |
| 118375 | + /* Invoke the main loop body as a subroutine */ |
| 118376 | + sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); |
| 118377 | + |
| 118378 | + /* Jump here (skipping the main loop body subroutine) if the |
| 118379 | + ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */ |
| 118380 | + if( j1 ) sqlite3VdbeJumpHere(v, j1); |
| 118381 | + |
| 118382 | + /* The pSubWInfo->untestedTerms flag means that this OR term |
| 118383 | + ** contained one or more AND term from a notReady table. The |
| 118384 | + ** terms from the notReady table could not be tested and will |
| 118385 | + ** need to be tested later. |
| 118386 | + */ |
| 118387 | + if( pSubWInfo->untestedTerms ) untestedTerms = 1; |
| 118388 | + |
| 118389 | + /* If all of the OR-connected terms are optimized using the same |
| 118390 | + ** index, and the index is opened using the same cursor number |
| 118391 | + ** by each call to sqlite3WhereBegin() made by this loop, it may |
| 118392 | + ** be possible to use that index as a covering index. |
| 118393 | + ** |
| 118394 | + ** If the call to sqlite3WhereBegin() above resulted in a scan that |
| 118395 | + ** uses an index, and this is either the first OR-connected term |
| 118396 | + ** processed or the index is the same as that used by all previous |
| 118397 | + ** terms, set pCov to the candidate covering index. Otherwise, set |
| 118398 | + ** pCov to NULL to indicate that no candidate covering index will |
| 118399 | + ** be available. |
| 118400 | + */ |
| 118401 | + pSubLoop = pSubWInfo->a[0].pWLoop; |
| 118402 | + assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
| 118403 | + if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
| 118404 | + && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
| 118405 | + && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex)) |
| 118406 | + ){ |
| 118407 | + assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
| 118408 | + pCov = pSubLoop->u.btree.pIndex; |
| 118409 | + wctrlFlags |= WHERE_REOPEN_IDX; |
| 118410 | + }else{ |
| 118411 | + pCov = 0; |
| 118412 | + } |
| 118413 | + |
| 118414 | + /* Finish the loop through table entries that match term pOrTerm. */ |
| 118415 | + sqlite3WhereEnd(pSubWInfo); |
| 118416 | + } |
| 118417 | + } |
| 118418 | + } |
| 118419 | + pLevel->u.pCovidx = pCov; |
| 118420 | + if( pCov ) pLevel->iIdxCur = iCovCur; |
| 118421 | + if( pAndExpr ){ |
| 118422 | + pAndExpr->pLeft = 0; |
| 118423 | + sqlite3ExprDelete(db, pAndExpr); |
| 118424 | + } |
| 118425 | + sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
| 118426 | + sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); |
| 118427 | + sqlite3VdbeResolveLabel(v, iLoopBody); |
| 118428 | + |
| 118429 | + if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab); |
| 118430 | + if( !untestedTerms ) disableTerm(pLevel, pTerm); |
| 118431 | + }else |
| 118432 | +#endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 118433 | + |
| 118434 | + { |
| 118435 | + /* Case 6: There is no usable index. We must do a complete |
| 118436 | + ** scan of the entire table. |
| 118437 | + */ |
| 118438 | + static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 118439 | + static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 118440 | + assert( bRev==0 || bRev==1 ); |
| 118441 | + if( pTabItem->isRecursive ){ |
| 118442 | + /* Tables marked isRecursive have only a single row that is stored in |
| 118443 | + ** a pseudo-cursor. No need to Rewind or Next such cursors. */ |
| 118444 | + pLevel->op = OP_Noop; |
| 118445 | + }else{ |
| 118446 | + pLevel->op = aStep[bRev]; |
| 118447 | + pLevel->p1 = iCur; |
| 118448 | + pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
| 118449 | + VdbeCoverageIf(v, bRev==0); |
| 118450 | + VdbeCoverageIf(v, bRev!=0); |
| 118451 | + pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 118452 | + } |
| 118453 | + } |
| 118454 | + |
| 118455 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 118456 | + pLevel->addrVisit = sqlite3VdbeCurrentAddr(v); |
| 118457 | +#endif |
| 118458 | + |
| 118459 | + /* Insert code to test every subexpression that can be completely |
| 118460 | + ** computed using the current set of tables. |
| 118461 | + */ |
| 118462 | + for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 118463 | + Expr *pE; |
| 118464 | + int skipLikeAddr = 0; |
| 118465 | + testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 118466 | + testcase( pTerm->wtFlags & TERM_CODED ); |
| 118467 | + if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 118468 | + if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
| 118469 | + testcase( pWInfo->untestedTerms==0 |
| 118470 | + && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); |
| 118471 | + pWInfo->untestedTerms = 1; |
| 118472 | + continue; |
| 118473 | + } |
| 118474 | + pE = pTerm->pExpr; |
| 118475 | + assert( pE!=0 ); |
| 118476 | + if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ |
| 118477 | + continue; |
| 118478 | + } |
| 118479 | + if( pTerm->wtFlags & TERM_LIKECOND ){ |
| 118480 | + assert( pLevel->iLikeRepCntr>0 ); |
| 118481 | + skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr); |
| 118482 | + VdbeCoverage(v); |
| 118483 | + } |
| 118484 | + sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); |
| 118485 | + if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr); |
| 118486 | + pTerm->wtFlags |= TERM_CODED; |
| 118487 | + } |
| 118488 | + |
| 118489 | + /* Insert code to test for implied constraints based on transitivity |
| 118490 | + ** of the "==" operator. |
| 118491 | + ** |
| 118492 | + ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" |
| 118493 | + ** and we are coding the t1 loop and the t2 loop has not yet coded, |
| 118494 | + ** then we cannot use the "t1.a=t2.b" constraint, but we can code |
| 118495 | + ** the implied "t1.a=123" constraint. |
| 118496 | + */ |
| 118497 | + for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 118498 | + Expr *pE, *pEAlt; |
| 118499 | + WhereTerm *pAlt; |
| 118500 | + if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 118501 | + if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue; |
| 118502 | + if( (pTerm->eOperator & WO_EQUIV)==0 ) continue; |
| 118503 | + if( pTerm->leftCursor!=iCur ) continue; |
| 118504 | + if( pLevel->iLeftJoin ) continue; |
| 118505 | + pE = pTerm->pExpr; |
| 118506 | + assert( !ExprHasProperty(pE, EP_FromJoin) ); |
| 118507 | + assert( (pTerm->prereqRight & pLevel->notReady)!=0 ); |
| 118508 | + pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady, |
| 118509 | + WO_EQ|WO_IN|WO_IS, 0); |
| 118510 | + if( pAlt==0 ) continue; |
| 118511 | + if( pAlt->wtFlags & (TERM_CODED) ) continue; |
| 118512 | + testcase( pAlt->eOperator & WO_EQ ); |
| 118513 | + testcase( pAlt->eOperator & WO_IS ); |
| 118514 | + testcase( pAlt->eOperator & WO_IN ); |
| 118515 | + VdbeModuleComment((v, "begin transitive constraint")); |
| 118516 | + pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt)); |
| 118517 | + if( pEAlt ){ |
| 118518 | + *pEAlt = *pAlt->pExpr; |
| 118519 | + pEAlt->pLeft = pE->pLeft; |
| 118520 | + sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL); |
| 118521 | + sqlite3StackFree(db, pEAlt); |
| 118522 | + } |
| 118523 | + } |
| 118524 | + |
| 118525 | + /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 118526 | + ** at least one row of the right table has matched the left table. |
| 118527 | + */ |
| 118528 | + if( pLevel->iLeftJoin ){ |
| 118529 | + pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); |
| 118530 | + sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); |
| 118531 | + VdbeComment((v, "record LEFT JOIN hit")); |
| 118532 | + sqlite3ExprCacheClear(pParse); |
| 118533 | + for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){ |
| 118534 | + testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 118535 | + testcase( pTerm->wtFlags & TERM_CODED ); |
| 118536 | + if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 118537 | + if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
| 118538 | + assert( pWInfo->untestedTerms ); |
| 118539 | + continue; |
| 118540 | + } |
| 118541 | + assert( pTerm->pExpr ); |
| 118542 | + sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); |
| 118543 | + pTerm->wtFlags |= TERM_CODED; |
| 118544 | + } |
| 118545 | + } |
| 118546 | + |
| 118547 | + return pLevel->notReady; |
| 118548 | +} |
| 118549 | + |
| 118550 | +/************** End of wherecode.c *******************************************/ |
| 118551 | +/************** Begin file whereexpr.c ***************************************/ |
| 118552 | +/* |
| 118553 | +** 2015-06-08 |
| 118554 | +** |
| 118555 | +** The author disclaims copyright to this source code. In place of |
| 118556 | +** a legal notice, here is a blessing: |
| 118557 | +** |
| 118558 | +** May you do good and not evil. |
| 118559 | +** May you find forgiveness for yourself and forgive others. |
| 118560 | +** May you share freely, never taking more than you give. |
| 118561 | +** |
| 118562 | +************************************************************************* |
| 118563 | +** This module contains C code that generates VDBE code used to process |
| 118564 | +** the WHERE clause of SQL statements. |
| 118565 | +** |
| 118566 | +** This file was originally part of where.c but was split out to improve |
| 118567 | +** readability and editabiliity. This file contains utility routines for |
| 118568 | +** analyzing Expr objects in the WHERE clause. |
| 118569 | +*/ |
| 118570 | + |
| 118571 | +/* Forward declarations */ |
| 118572 | +static void exprAnalyze(SrcList*, WhereClause*, int); |
| 116438 | 118573 | |
| 116439 | 118574 | /* |
| 116440 | 118575 | ** Deallocate all memory associated with a WhereOrInfo object. |
| 116441 | 118576 | */ |
| 116442 | 118577 | static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){ |
| 116443 | | - whereClauseClear(&p->wc); |
| 118578 | + sqlite3WhereClauseClear(&p->wc); |
| 116444 | 118579 | sqlite3DbFree(db, p); |
| 116445 | 118580 | } |
| 116446 | 118581 | |
| 116447 | 118582 | /* |
| 116448 | 118583 | ** Deallocate all memory associated with a WhereAndInfo object. |
| 116449 | 118584 | */ |
| 116450 | 118585 | static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){ |
| 116451 | | - whereClauseClear(&p->wc); |
| 118586 | + sqlite3WhereClauseClear(&p->wc); |
| 116452 | 118587 | sqlite3DbFree(db, p); |
| 116453 | 118588 | } |
| 116454 | 118589 | |
| 116455 | | -/* |
| 116456 | | -** Deallocate a WhereClause structure. The WhereClause structure |
| 116457 | | -** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 116458 | | -*/ |
| 116459 | | -static void whereClauseClear(WhereClause *pWC){ |
| 116460 | | - int i; |
| 116461 | | - WhereTerm *a; |
| 116462 | | - sqlite3 *db = pWC->pWInfo->pParse->db; |
| 116463 | | - for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
| 116464 | | - if( a->wtFlags & TERM_DYNAMIC ){ |
| 116465 | | - sqlite3ExprDelete(db, a->pExpr); |
| 116466 | | - } |
| 116467 | | - if( a->wtFlags & TERM_ORINFO ){ |
| 116468 | | - whereOrInfoDelete(db, a->u.pOrInfo); |
| 116469 | | - }else if( a->wtFlags & TERM_ANDINFO ){ |
| 116470 | | - whereAndInfoDelete(db, a->u.pAndInfo); |
| 116471 | | - } |
| 116472 | | - } |
| 116473 | | - if( pWC->a!=pWC->aStatic ){ |
| 116474 | | - sqlite3DbFree(db, pWC->a); |
| 116475 | | - } |
| 116476 | | -} |
| 116477 | | - |
| 116478 | 118590 | /* |
| 116479 | 118591 | ** Add a single new WhereTerm entry to the WhereClause object pWC. |
| 116480 | 118592 | ** The new WhereTerm object is constructed from Expr p and with wtFlags. |
| 116481 | 118593 | ** The index in pWC->a[] of the new WhereTerm is returned on success. |
| 116482 | 118594 | ** 0 is returned if the new WhereTerm could not be added due to a memory |
| | @@ -116527,126 +118639,10 @@ |
| 116527 | 118639 | pTerm->pWC = pWC; |
| 116528 | 118640 | pTerm->iParent = -1; |
| 116529 | 118641 | return idx; |
| 116530 | 118642 | } |
| 116531 | 118643 | |
| 116532 | | -/* |
| 116533 | | -** This routine identifies subexpressions in the WHERE clause where |
| 116534 | | -** each subexpression is separated by the AND operator or some other |
| 116535 | | -** operator specified in the op parameter. The WhereClause structure |
| 116536 | | -** is filled with pointers to subexpressions. For example: |
| 116537 | | -** |
| 116538 | | -** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 116539 | | -** \________/ \_______________/ \________________/ |
| 116540 | | -** slot[0] slot[1] slot[2] |
| 116541 | | -** |
| 116542 | | -** The original WHERE clause in pExpr is unaltered. All this routine |
| 116543 | | -** does is make slot[] entries point to substructure within pExpr. |
| 116544 | | -** |
| 116545 | | -** In the previous sentence and in the diagram, "slot[]" refers to |
| 116546 | | -** the WhereClause.a[] array. The slot[] array grows as needed to contain |
| 116547 | | -** all terms of the WHERE clause. |
| 116548 | | -*/ |
| 116549 | | -static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ |
| 116550 | | - Expr *pE2 = sqlite3ExprSkipCollate(pExpr); |
| 116551 | | - pWC->op = op; |
| 116552 | | - if( pE2==0 ) return; |
| 116553 | | - if( pE2->op!=op ){ |
| 116554 | | - whereClauseInsert(pWC, pExpr, 0); |
| 116555 | | - }else{ |
| 116556 | | - whereSplit(pWC, pE2->pLeft, op); |
| 116557 | | - whereSplit(pWC, pE2->pRight, op); |
| 116558 | | - } |
| 116559 | | -} |
| 116560 | | - |
| 116561 | | -/* |
| 116562 | | -** Initialize a WhereMaskSet object |
| 116563 | | -*/ |
| 116564 | | -#define initMaskSet(P) (P)->n=0 |
| 116565 | | - |
| 116566 | | -/* |
| 116567 | | -** Return the bitmask for the given cursor number. Return 0 if |
| 116568 | | -** iCursor is not in the set. |
| 116569 | | -*/ |
| 116570 | | -static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){ |
| 116571 | | - int i; |
| 116572 | | - assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
| 116573 | | - for(i=0; i<pMaskSet->n; i++){ |
| 116574 | | - if( pMaskSet->ix[i]==iCursor ){ |
| 116575 | | - return MASKBIT(i); |
| 116576 | | - } |
| 116577 | | - } |
| 116578 | | - return 0; |
| 116579 | | -} |
| 116580 | | - |
| 116581 | | -/* |
| 116582 | | -** Create a new mask for cursor iCursor. |
| 116583 | | -** |
| 116584 | | -** There is one cursor per table in the FROM clause. The number of |
| 116585 | | -** tables in the FROM clause is limited by a test early in the |
| 116586 | | -** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] |
| 116587 | | -** array will never overflow. |
| 116588 | | -*/ |
| 116589 | | -static void createMask(WhereMaskSet *pMaskSet, int iCursor){ |
| 116590 | | - assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
| 116591 | | - pMaskSet->ix[pMaskSet->n++] = iCursor; |
| 116592 | | -} |
| 116593 | | - |
| 116594 | | -/* |
| 116595 | | -** These routines walk (recursively) an expression tree and generate |
| 116596 | | -** a bitmask indicating which tables are used in that expression |
| 116597 | | -** tree. |
| 116598 | | -*/ |
| 116599 | | -static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); |
| 116600 | | -static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); |
| 116601 | | -static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ |
| 116602 | | - Bitmask mask = 0; |
| 116603 | | - if( p==0 ) return 0; |
| 116604 | | - if( p->op==TK_COLUMN ){ |
| 116605 | | - mask = getMask(pMaskSet, p->iTable); |
| 116606 | | - return mask; |
| 116607 | | - } |
| 116608 | | - mask = exprTableUsage(pMaskSet, p->pRight); |
| 116609 | | - mask |= exprTableUsage(pMaskSet, p->pLeft); |
| 116610 | | - if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 116611 | | - mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect); |
| 116612 | | - }else{ |
| 116613 | | - mask |= exprListTableUsage(pMaskSet, p->x.pList); |
| 116614 | | - } |
| 116615 | | - return mask; |
| 116616 | | -} |
| 116617 | | -static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){ |
| 116618 | | - int i; |
| 116619 | | - Bitmask mask = 0; |
| 116620 | | - if( pList ){ |
| 116621 | | - for(i=0; i<pList->nExpr; i++){ |
| 116622 | | - mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr); |
| 116623 | | - } |
| 116624 | | - } |
| 116625 | | - return mask; |
| 116626 | | -} |
| 116627 | | -static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){ |
| 116628 | | - Bitmask mask = 0; |
| 116629 | | - while( pS ){ |
| 116630 | | - SrcList *pSrc = pS->pSrc; |
| 116631 | | - mask |= exprListTableUsage(pMaskSet, pS->pEList); |
| 116632 | | - mask |= exprListTableUsage(pMaskSet, pS->pGroupBy); |
| 116633 | | - mask |= exprListTableUsage(pMaskSet, pS->pOrderBy); |
| 116634 | | - mask |= exprTableUsage(pMaskSet, pS->pWhere); |
| 116635 | | - mask |= exprTableUsage(pMaskSet, pS->pHaving); |
| 116636 | | - if( ALWAYS(pSrc!=0) ){ |
| 116637 | | - int i; |
| 116638 | | - for(i=0; i<pSrc->nSrc; i++){ |
| 116639 | | - mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect); |
| 116640 | | - mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn); |
| 116641 | | - } |
| 116642 | | - } |
| 116643 | | - pS = pS->pPrior; |
| 116644 | | - } |
| 116645 | | - return mask; |
| 116646 | | -} |
| 116647 | | - |
| 116648 | 118644 | /* |
| 116649 | 118645 | ** Return TRUE if the given operator is one of the operators that is |
| 116650 | 118646 | ** allowed for an indexable WHERE clause term. The allowed operators are |
| 116651 | 118647 | ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL" |
| 116652 | 118648 | */ |
| | @@ -116723,203 +118719,10 @@ |
| 116723 | 118719 | assert( op!=TK_GE || c==WO_GE ); |
| 116724 | 118720 | assert( op!=TK_IS || c==WO_IS ); |
| 116725 | 118721 | return c; |
| 116726 | 118722 | } |
| 116727 | 118723 | |
| 116728 | | -/* |
| 116729 | | -** Advance to the next WhereTerm that matches according to the criteria |
| 116730 | | -** established when the pScan object was initialized by whereScanInit(). |
| 116731 | | -** Return NULL if there are no more matching WhereTerms. |
| 116732 | | -*/ |
| 116733 | | -static WhereTerm *whereScanNext(WhereScan *pScan){ |
| 116734 | | - int iCur; /* The cursor on the LHS of the term */ |
| 116735 | | - int iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 116736 | | - Expr *pX; /* An expression being tested */ |
| 116737 | | - WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 116738 | | - WhereTerm *pTerm; /* The term being tested */ |
| 116739 | | - int k = pScan->k; /* Where to start scanning */ |
| 116740 | | - |
| 116741 | | - while( pScan->iEquiv<=pScan->nEquiv ){ |
| 116742 | | - iCur = pScan->aEquiv[pScan->iEquiv-2]; |
| 116743 | | - iColumn = pScan->aEquiv[pScan->iEquiv-1]; |
| 116744 | | - while( (pWC = pScan->pWC)!=0 ){ |
| 116745 | | - for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
| 116746 | | - if( pTerm->leftCursor==iCur |
| 116747 | | - && pTerm->u.leftColumn==iColumn |
| 116748 | | - && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 116749 | | - ){ |
| 116750 | | - if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 116751 | | - && pScan->nEquiv<ArraySize(pScan->aEquiv) |
| 116752 | | - ){ |
| 116753 | | - int j; |
| 116754 | | - pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); |
| 116755 | | - assert( pX->op==TK_COLUMN ); |
| 116756 | | - for(j=0; j<pScan->nEquiv; j+=2){ |
| 116757 | | - if( pScan->aEquiv[j]==pX->iTable |
| 116758 | | - && pScan->aEquiv[j+1]==pX->iColumn ){ |
| 116759 | | - break; |
| 116760 | | - } |
| 116761 | | - } |
| 116762 | | - if( j==pScan->nEquiv ){ |
| 116763 | | - pScan->aEquiv[j] = pX->iTable; |
| 116764 | | - pScan->aEquiv[j+1] = pX->iColumn; |
| 116765 | | - pScan->nEquiv += 2; |
| 116766 | | - } |
| 116767 | | - } |
| 116768 | | - if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 116769 | | - /* Verify the affinity and collating sequence match */ |
| 116770 | | - if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 116771 | | - CollSeq *pColl; |
| 116772 | | - Parse *pParse = pWC->pWInfo->pParse; |
| 116773 | | - pX = pTerm->pExpr; |
| 116774 | | - if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 116775 | | - continue; |
| 116776 | | - } |
| 116777 | | - assert(pX->pLeft); |
| 116778 | | - pColl = sqlite3BinaryCompareCollSeq(pParse, |
| 116779 | | - pX->pLeft, pX->pRight); |
| 116780 | | - if( pColl==0 ) pColl = pParse->db->pDfltColl; |
| 116781 | | - if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 116782 | | - continue; |
| 116783 | | - } |
| 116784 | | - } |
| 116785 | | - if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0 |
| 116786 | | - && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 116787 | | - && pX->iTable==pScan->aEquiv[0] |
| 116788 | | - && pX->iColumn==pScan->aEquiv[1] |
| 116789 | | - ){ |
| 116790 | | - testcase( pTerm->eOperator & WO_IS ); |
| 116791 | | - continue; |
| 116792 | | - } |
| 116793 | | - pScan->k = k+1; |
| 116794 | | - return pTerm; |
| 116795 | | - } |
| 116796 | | - } |
| 116797 | | - } |
| 116798 | | - pScan->pWC = pScan->pWC->pOuter; |
| 116799 | | - k = 0; |
| 116800 | | - } |
| 116801 | | - pScan->pWC = pScan->pOrigWC; |
| 116802 | | - k = 0; |
| 116803 | | - pScan->iEquiv += 2; |
| 116804 | | - } |
| 116805 | | - return 0; |
| 116806 | | -} |
| 116807 | | - |
| 116808 | | -/* |
| 116809 | | -** Initialize a WHERE clause scanner object. Return a pointer to the |
| 116810 | | -** first match. Return NULL if there are no matches. |
| 116811 | | -** |
| 116812 | | -** The scanner will be searching the WHERE clause pWC. It will look |
| 116813 | | -** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 116814 | | -** iCur. The <op> must be one of the operators described by opMask. |
| 116815 | | -** |
| 116816 | | -** If the search is for X and the WHERE clause contains terms of the |
| 116817 | | -** form X=Y then this routine might also return terms of the form |
| 116818 | | -** "Y <op> <expr>". The number of levels of transitivity is limited, |
| 116819 | | -** but is enough to handle most commonly occurring SQL statements. |
| 116820 | | -** |
| 116821 | | -** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 116822 | | -** index pIdx. |
| 116823 | | -*/ |
| 116824 | | -static WhereTerm *whereScanInit( |
| 116825 | | - WhereScan *pScan, /* The WhereScan object being initialized */ |
| 116826 | | - WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 116827 | | - int iCur, /* Cursor to scan for */ |
| 116828 | | - int iColumn, /* Column to scan for */ |
| 116829 | | - u32 opMask, /* Operator(s) to scan for */ |
| 116830 | | - Index *pIdx /* Must be compatible with this index */ |
| 116831 | | -){ |
| 116832 | | - int j; |
| 116833 | | - |
| 116834 | | - /* memset(pScan, 0, sizeof(*pScan)); */ |
| 116835 | | - pScan->pOrigWC = pWC; |
| 116836 | | - pScan->pWC = pWC; |
| 116837 | | - if( pIdx && iColumn>=0 ){ |
| 116838 | | - pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 116839 | | - for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
| 116840 | | - if( NEVER(j>pIdx->nColumn) ) return 0; |
| 116841 | | - } |
| 116842 | | - pScan->zCollName = pIdx->azColl[j]; |
| 116843 | | - }else{ |
| 116844 | | - pScan->idxaff = 0; |
| 116845 | | - pScan->zCollName = 0; |
| 116846 | | - } |
| 116847 | | - pScan->opMask = opMask; |
| 116848 | | - pScan->k = 0; |
| 116849 | | - pScan->aEquiv[0] = iCur; |
| 116850 | | - pScan->aEquiv[1] = iColumn; |
| 116851 | | - pScan->nEquiv = 2; |
| 116852 | | - pScan->iEquiv = 2; |
| 116853 | | - return whereScanNext(pScan); |
| 116854 | | -} |
| 116855 | | - |
| 116856 | | -/* |
| 116857 | | -** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 116858 | | -** where X is a reference to the iColumn of table iCur and <op> is one of |
| 116859 | | -** the WO_xx operator codes specified by the op parameter. |
| 116860 | | -** Return a pointer to the term. Return 0 if not found. |
| 116861 | | -** |
| 116862 | | -** The term returned might by Y=<expr> if there is another constraint in |
| 116863 | | -** the WHERE clause that specifies that X=Y. Any such constraints will be |
| 116864 | | -** identified by the WO_EQUIV bit in the pTerm->eOperator field. The |
| 116865 | | -** aEquiv[] array holds X and all its equivalents, with each SQL variable |
| 116866 | | -** taking up two slots in aEquiv[]. The first slot is for the cursor number |
| 116867 | | -** and the second is for the column number. There are 22 slots in aEquiv[] |
| 116868 | | -** so that means we can look for X plus up to 10 other equivalent values. |
| 116869 | | -** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3 |
| 116870 | | -** and ... and A9=A10 and A10=<expr>. |
| 116871 | | -** |
| 116872 | | -** If there are multiple terms in the WHERE clause of the form "X <op> <expr>" |
| 116873 | | -** then try for the one with no dependencies on <expr> - in other words where |
| 116874 | | -** <expr> is a constant expression of some kind. Only return entries of |
| 116875 | | -** the form "X <op> Y" where Y is a column in another table if no terms of |
| 116876 | | -** the form "X <op> <const-expr>" exist. If no terms with a constant RHS |
| 116877 | | -** exist, try to return a term that does not use WO_EQUIV. |
| 116878 | | -*/ |
| 116879 | | -static WhereTerm *findTerm( |
| 116880 | | - WhereClause *pWC, /* The WHERE clause to be searched */ |
| 116881 | | - int iCur, /* Cursor number of LHS */ |
| 116882 | | - int iColumn, /* Column number of LHS */ |
| 116883 | | - Bitmask notReady, /* RHS must not overlap with this mask */ |
| 116884 | | - u32 op, /* Mask of WO_xx values describing operator */ |
| 116885 | | - Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 116886 | | -){ |
| 116887 | | - WhereTerm *pResult = 0; |
| 116888 | | - WhereTerm *p; |
| 116889 | | - WhereScan scan; |
| 116890 | | - |
| 116891 | | - p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
| 116892 | | - op &= WO_EQ|WO_IS; |
| 116893 | | - while( p ){ |
| 116894 | | - if( (p->prereqRight & notReady)==0 ){ |
| 116895 | | - if( p->prereqRight==0 && (p->eOperator&op)!=0 ){ |
| 116896 | | - testcase( p->eOperator & WO_IS ); |
| 116897 | | - return p; |
| 116898 | | - } |
| 116899 | | - if( pResult==0 ) pResult = p; |
| 116900 | | - } |
| 116901 | | - p = whereScanNext(&scan); |
| 116902 | | - } |
| 116903 | | - return pResult; |
| 116904 | | -} |
| 116905 | | - |
| 116906 | | -/* Forward reference */ |
| 116907 | | -static void exprAnalyze(SrcList*, WhereClause*, int); |
| 116908 | | - |
| 116909 | | -/* |
| 116910 | | -** Call exprAnalyze on all terms in a WHERE clause. |
| 116911 | | -*/ |
| 116912 | | -static void exprAnalyzeAll( |
| 116913 | | - SrcList *pTabList, /* the FROM clause */ |
| 116914 | | - WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 116915 | | -){ |
| 116916 | | - int i; |
| 116917 | | - for(i=pWC->nTerm-1; i>=0; i--){ |
| 116918 | | - exprAnalyze(pTabList, pWC, i); |
| 116919 | | - } |
| 116920 | | -} |
| 116921 | 118724 | |
| 116922 | 118725 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 116923 | 118726 | /* |
| 116924 | 118727 | ** Check to see if the given expression is a LIKE or GLOB operator that |
| 116925 | 118728 | ** can be optimized using inequality constraints. Return TRUE if it is |
| | @@ -116970,11 +118773,11 @@ |
| 116970 | 118773 | pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr); |
| 116971 | 118774 | op = pRight->op; |
| 116972 | 118775 | if( op==TK_VARIABLE ){ |
| 116973 | 118776 | Vdbe *pReprepare = pParse->pReprepare; |
| 116974 | 118777 | int iCol = pRight->iColumn; |
| 116975 | | - pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE); |
| 118778 | + pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB); |
| 116976 | 118779 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 116977 | 118780 | z = (char *)sqlite3_value_text(pVal); |
| 116978 | 118781 | } |
| 116979 | 118782 | sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); |
| 116980 | 118783 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| | @@ -117181,11 +118984,11 @@ |
| 117181 | 118984 | ** |
| 117182 | 118985 | ** x IN (expr1,expr2,expr3) |
| 117183 | 118986 | ** |
| 117184 | 118987 | ** CASE 2: |
| 117185 | 118988 | ** |
| 117186 | | -** If there are exactly two disjuncts one side has x>A and the other side |
| 118989 | +** If there are exactly two disjuncts and one side has x>A and the other side |
| 117187 | 118990 | ** has x=A (for the same x and A) then add a new virtual conjunct term to the |
| 117188 | 118991 | ** WHERE clause of the form "x>=A". Example: |
| 117189 | 118992 | ** |
| 117190 | 118993 | ** x>A OR (x=A AND y>B) adds: x>=A |
| 117191 | 118994 | ** |
| | @@ -117210,26 +119013,26 @@ |
| 117210 | 119013 | ** potentially be used with an index if an appropriate index exists. |
| 117211 | 119014 | ** This analysis does not consider whether or not the index exists; that |
| 117212 | 119015 | ** is decided elsewhere. This analysis only looks at whether subterms |
| 117213 | 119016 | ** appropriate for indexing exist. |
| 117214 | 119017 | ** |
| 117215 | | -** All examples A through E above satisfy case 2. But if a term |
| 119018 | +** All examples A through E above satisfy case 3. But if a term |
| 117216 | 119019 | ** also satisfies case 1 (such as B) we know that the optimizer will |
| 117217 | | -** always prefer case 1, so in that case we pretend that case 2 is not |
| 119020 | +** always prefer case 1, so in that case we pretend that case 3 is not |
| 117218 | 119021 | ** satisfied. |
| 117219 | 119022 | ** |
| 117220 | 119023 | ** It might be the case that multiple tables are indexable. For example, |
| 117221 | 119024 | ** (E) above is indexable on tables P, Q, and R. |
| 117222 | 119025 | ** |
| 117223 | | -** Terms that satisfy case 2 are candidates for lookup by using |
| 119026 | +** Terms that satisfy case 3 are candidates for lookup by using |
| 117224 | 119027 | ** separate indices to find rowids for each subterm and composing |
| 117225 | 119028 | ** the union of all rowids using a RowSet object. This is similar |
| 117226 | 119029 | ** to "bitmap indices" in other database engines. |
| 117227 | 119030 | ** |
| 117228 | 119031 | ** OTHERWISE: |
| 117229 | 119032 | ** |
| 117230 | | -** If neither case 1 nor case 2 apply, then leave the eOperator set to |
| 119033 | +** If none of cases 1, 2, or 3 apply, then leave the eOperator set to |
| 117231 | 119034 | ** zero. This term is not useful for search. |
| 117232 | 119035 | */ |
| 117233 | 119036 | static void exprAnalyzeOrTerm( |
| 117234 | 119037 | SrcList *pSrc, /* the FROM clause */ |
| 117235 | 119038 | WhereClause *pWC, /* the complete WHERE clause */ |
| | @@ -117256,18 +119059,18 @@ |
| 117256 | 119059 | assert( pExpr->op==TK_OR ); |
| 117257 | 119060 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
| 117258 | 119061 | if( pOrInfo==0 ) return; |
| 117259 | 119062 | pTerm->wtFlags |= TERM_ORINFO; |
| 117260 | 119063 | pOrWc = &pOrInfo->wc; |
| 117261 | | - whereClauseInit(pOrWc, pWInfo); |
| 117262 | | - whereSplit(pOrWc, pExpr, TK_OR); |
| 117263 | | - exprAnalyzeAll(pSrc, pOrWc); |
| 119064 | + sqlite3WhereClauseInit(pOrWc, pWInfo); |
| 119065 | + sqlite3WhereSplit(pOrWc, pExpr, TK_OR); |
| 119066 | + sqlite3WhereExprAnalyze(pSrc, pOrWc); |
| 117264 | 119067 | if( db->mallocFailed ) return; |
| 117265 | 119068 | assert( pOrWc->nTerm>=2 ); |
| 117266 | 119069 | |
| 117267 | 119070 | /* |
| 117268 | | - ** Compute the set of tables that might satisfy cases 1 or 2. |
| 119071 | + ** Compute the set of tables that might satisfy cases 1 or 3. |
| 117269 | 119072 | */ |
| 117270 | 119073 | indexable = ~(Bitmask)0; |
| 117271 | 119074 | chngToIN = ~(Bitmask)0; |
| 117272 | 119075 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ |
| 117273 | 119076 | if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ |
| | @@ -117282,20 +119085,20 @@ |
| 117282 | 119085 | Bitmask b = 0; |
| 117283 | 119086 | pOrTerm->u.pAndInfo = pAndInfo; |
| 117284 | 119087 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 117285 | 119088 | pOrTerm->eOperator = WO_AND; |
| 117286 | 119089 | pAndWC = &pAndInfo->wc; |
| 117287 | | - whereClauseInit(pAndWC, pWC->pWInfo); |
| 117288 | | - whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 117289 | | - exprAnalyzeAll(pSrc, pAndWC); |
| 119090 | + sqlite3WhereClauseInit(pAndWC, pWC->pWInfo); |
| 119091 | + sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 119092 | + sqlite3WhereExprAnalyze(pSrc, pAndWC); |
| 117290 | 119093 | pAndWC->pOuter = pWC; |
| 117291 | 119094 | testcase( db->mallocFailed ); |
| 117292 | 119095 | if( !db->mallocFailed ){ |
| 117293 | 119096 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 117294 | 119097 | assert( pAndTerm->pExpr ); |
| 117295 | 119098 | if( allowedOp(pAndTerm->pExpr->op) ){ |
| 117296 | | - b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
| 119099 | + b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
| 117297 | 119100 | } |
| 117298 | 119101 | } |
| 117299 | 119102 | } |
| 117300 | 119103 | indexable &= b; |
| 117301 | 119104 | } |
| | @@ -117302,14 +119105,14 @@ |
| 117302 | 119105 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 117303 | 119106 | /* Skip this term for now. We revisit it when we process the |
| 117304 | 119107 | ** corresponding TERM_VIRTUAL term */ |
| 117305 | 119108 | }else{ |
| 117306 | 119109 | Bitmask b; |
| 117307 | | - b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
| 119110 | + b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
| 117308 | 119111 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 117309 | 119112 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
| 117310 | | - b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor); |
| 119113 | + b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor); |
| 117311 | 119114 | } |
| 117312 | 119115 | indexable &= b; |
| 117313 | 119116 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
| 117314 | 119117 | chngToIN = 0; |
| 117315 | 119118 | }else{ |
| | @@ -117381,11 +119184,12 @@ |
| 117381 | 119184 | /* This is the 2-bit case and we are on the second iteration and |
| 117382 | 119185 | ** current term is from the first iteration. So skip this term. */ |
| 117383 | 119186 | assert( j==1 ); |
| 117384 | 119187 | continue; |
| 117385 | 119188 | } |
| 117386 | | - if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){ |
| 119189 | + if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet, |
| 119190 | + pOrTerm->leftCursor))==0 ){ |
| 117387 | 119191 | /* This term must be of the form t1.a==t2.b where t2 is in the |
| 117388 | 119192 | ** chngToIN set but t1 is not. This term will be either preceded |
| 117389 | 119193 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 117390 | 119194 | ** and use its inversion. */ |
| 117391 | 119195 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| | @@ -117400,11 +119204,11 @@ |
| 117400 | 119204 | if( i<0 ){ |
| 117401 | 119205 | /* No candidate table+column was found. This can only occur |
| 117402 | 119206 | ** on the second iteration */ |
| 117403 | 119207 | assert( j==1 ); |
| 117404 | 119208 | assert( IsPowerOfTwo(chngToIN) ); |
| 117405 | | - assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) ); |
| 119209 | + assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) ); |
| 117406 | 119210 | break; |
| 117407 | 119211 | } |
| 117408 | 119212 | testcase( j==1 ); |
| 117409 | 119213 | |
| 117410 | 119214 | /* We have found a candidate table and column. Check to see if that |
| | @@ -117478,11 +119282,11 @@ |
| 117478 | 119282 | ** We already know that pExpr is a binary operator where both operands are |
| 117479 | 119283 | ** column references. This routine checks to see if pExpr is an equivalence |
| 117480 | 119284 | ** relation: |
| 117481 | 119285 | ** 1. The SQLITE_Transitive optimization must be enabled |
| 117482 | 119286 | ** 2. Must be either an == or an IS operator |
| 117483 | | -** 3. Not originating the ON clause of an OUTER JOIN |
| 119287 | +** 3. Not originating in the ON clause of an OUTER JOIN |
| 117484 | 119288 | ** 4. The affinities of A and B must be compatible |
| 117485 | 119289 | ** 5a. Both operands use the same collating sequence OR |
| 117486 | 119290 | ** 5b. The overall collating sequence is BINARY |
| 117487 | 119291 | ** If this routine returns TRUE, that means that the RHS can be substituted |
| 117488 | 119292 | ** for the LHS anyplace else in the WHERE clause where the LHS column occurs. |
| | @@ -117511,10 +119315,36 @@ |
| 117511 | 119315 | zColl1 = ALWAYS(pColl) ? pColl->zName : 0; |
| 117512 | 119316 | pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight); |
| 117513 | 119317 | zColl2 = ALWAYS(pColl) ? pColl->zName : 0; |
| 117514 | 119318 | return sqlite3StrICmp(zColl1, zColl2)==0; |
| 117515 | 119319 | } |
| 119320 | + |
| 119321 | +/* |
| 119322 | +** Recursively walk the expressions of a SELECT statement and generate |
| 119323 | +** a bitmask indicating which tables are used in that expression |
| 119324 | +** tree. |
| 119325 | +*/ |
| 119326 | +static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){ |
| 119327 | + Bitmask mask = 0; |
| 119328 | + while( pS ){ |
| 119329 | + SrcList *pSrc = pS->pSrc; |
| 119330 | + mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList); |
| 119331 | + mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy); |
| 119332 | + mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy); |
| 119333 | + mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere); |
| 119334 | + mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving); |
| 119335 | + if( ALWAYS(pSrc!=0) ){ |
| 119336 | + int i; |
| 119337 | + for(i=0; i<pSrc->nSrc; i++){ |
| 119338 | + mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect); |
| 119339 | + mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn); |
| 119340 | + } |
| 119341 | + } |
| 119342 | + pS = pS->pPrior; |
| 119343 | + } |
| 119344 | + return mask; |
| 119345 | +} |
| 117516 | 119346 | |
| 117517 | 119347 | /* |
| 117518 | 119348 | ** The input to this routine is an WhereTerm structure with only the |
| 117519 | 119349 | ** "pExpr" field filled in. The job of this routine is to analyze the |
| 117520 | 119350 | ** subexpression and populate all the other fields of the WhereTerm |
| | @@ -117556,27 +119386,27 @@ |
| 117556 | 119386 | } |
| 117557 | 119387 | pTerm = &pWC->a[idxTerm]; |
| 117558 | 119388 | pMaskSet = &pWInfo->sMaskSet; |
| 117559 | 119389 | pExpr = pTerm->pExpr; |
| 117560 | 119390 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
| 117561 | | - prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
| 119391 | + prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft); |
| 117562 | 119392 | op = pExpr->op; |
| 117563 | 119393 | if( op==TK_IN ){ |
| 117564 | 119394 | assert( pExpr->pRight==0 ); |
| 117565 | 119395 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 117566 | | - pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect); |
| 119396 | + pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect); |
| 117567 | 119397 | }else{ |
| 117568 | | - pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList); |
| 119398 | + pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList); |
| 117569 | 119399 | } |
| 117570 | 119400 | }else if( op==TK_ISNULL ){ |
| 117571 | 119401 | pTerm->prereqRight = 0; |
| 117572 | 119402 | }else{ |
| 117573 | | - pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); |
| 119403 | + pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight); |
| 117574 | 119404 | } |
| 117575 | | - prereqAll = exprTableUsage(pMaskSet, pExpr); |
| 119405 | + prereqAll = sqlite3WhereExprUsage(pMaskSet, pExpr); |
| 117576 | 119406 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 117577 | | - Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable); |
| 119407 | + Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable); |
| 117578 | 119408 | prereqAll |= x; |
| 117579 | 119409 | extraRight = x-1; /* ON clause terms may not be used with an index |
| 117580 | 119410 | ** on left table of a LEFT JOIN. Ticket #3015 */ |
| 117581 | 119411 | } |
| 117582 | 119412 | pTerm->prereqAll = prereqAll; |
| | @@ -117777,12 +119607,12 @@ |
| 117777 | 119607 | WhereTerm *pNewTerm; |
| 117778 | 119608 | Bitmask prereqColumn, prereqExpr; |
| 117779 | 119609 | |
| 117780 | 119610 | pRight = pExpr->x.pList->a[0].pExpr; |
| 117781 | 119611 | pLeft = pExpr->x.pList->a[1].pExpr; |
| 117782 | | - prereqExpr = exprTableUsage(pMaskSet, pRight); |
| 117783 | | - prereqColumn = exprTableUsage(pMaskSet, pLeft); |
| 119612 | + prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight); |
| 119613 | + prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft); |
| 117784 | 119614 | if( (prereqExpr & prereqColumn)==0 ){ |
| 117785 | 119615 | Expr *pNewExpr; |
| 117786 | 119616 | pNewExpr = sqlite3PExpr(pParse, TK_MATCH, |
| 117787 | 119617 | 0, sqlite3ExprDup(db, pRight, 0), 0); |
| 117788 | 119618 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
| | @@ -117841,10 +119671,477 @@ |
| 117841 | 119671 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 117842 | 119672 | ** an index for tables to the left of the join. |
| 117843 | 119673 | */ |
| 117844 | 119674 | pTerm->prereqRight |= extraRight; |
| 117845 | 119675 | } |
| 119676 | + |
| 119677 | +/*************************************************************************** |
| 119678 | +** Routines with file scope above. Interface to the rest of the where.c |
| 119679 | +** subsystem follows. |
| 119680 | +***************************************************************************/ |
| 119681 | + |
| 119682 | +/* |
| 119683 | +** This routine identifies subexpressions in the WHERE clause where |
| 119684 | +** each subexpression is separated by the AND operator or some other |
| 119685 | +** operator specified in the op parameter. The WhereClause structure |
| 119686 | +** is filled with pointers to subexpressions. For example: |
| 119687 | +** |
| 119688 | +** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 119689 | +** \________/ \_______________/ \________________/ |
| 119690 | +** slot[0] slot[1] slot[2] |
| 119691 | +** |
| 119692 | +** The original WHERE clause in pExpr is unaltered. All this routine |
| 119693 | +** does is make slot[] entries point to substructure within pExpr. |
| 119694 | +** |
| 119695 | +** In the previous sentence and in the diagram, "slot[]" refers to |
| 119696 | +** the WhereClause.a[] array. The slot[] array grows as needed to contain |
| 119697 | +** all terms of the WHERE clause. |
| 119698 | +*/ |
| 119699 | +SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ |
| 119700 | + Expr *pE2 = sqlite3ExprSkipCollate(pExpr); |
| 119701 | + pWC->op = op; |
| 119702 | + if( pE2==0 ) return; |
| 119703 | + if( pE2->op!=op ){ |
| 119704 | + whereClauseInsert(pWC, pExpr, 0); |
| 119705 | + }else{ |
| 119706 | + sqlite3WhereSplit(pWC, pE2->pLeft, op); |
| 119707 | + sqlite3WhereSplit(pWC, pE2->pRight, op); |
| 119708 | + } |
| 119709 | +} |
| 119710 | + |
| 119711 | +/* |
| 119712 | +** Initialize a preallocated WhereClause structure. |
| 119713 | +*/ |
| 119714 | +SQLITE_PRIVATE void sqlite3WhereClauseInit( |
| 119715 | + WhereClause *pWC, /* The WhereClause to be initialized */ |
| 119716 | + WhereInfo *pWInfo /* The WHERE processing context */ |
| 119717 | +){ |
| 119718 | + pWC->pWInfo = pWInfo; |
| 119719 | + pWC->pOuter = 0; |
| 119720 | + pWC->nTerm = 0; |
| 119721 | + pWC->nSlot = ArraySize(pWC->aStatic); |
| 119722 | + pWC->a = pWC->aStatic; |
| 119723 | +} |
| 119724 | + |
| 119725 | +/* |
| 119726 | +** Deallocate a WhereClause structure. The WhereClause structure |
| 119727 | +** itself is not freed. This routine is the inverse of sqlite3WhereClauseInit(). |
| 119728 | +*/ |
| 119729 | +SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause *pWC){ |
| 119730 | + int i; |
| 119731 | + WhereTerm *a; |
| 119732 | + sqlite3 *db = pWC->pWInfo->pParse->db; |
| 119733 | + for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
| 119734 | + if( a->wtFlags & TERM_DYNAMIC ){ |
| 119735 | + sqlite3ExprDelete(db, a->pExpr); |
| 119736 | + } |
| 119737 | + if( a->wtFlags & TERM_ORINFO ){ |
| 119738 | + whereOrInfoDelete(db, a->u.pOrInfo); |
| 119739 | + }else if( a->wtFlags & TERM_ANDINFO ){ |
| 119740 | + whereAndInfoDelete(db, a->u.pAndInfo); |
| 119741 | + } |
| 119742 | + } |
| 119743 | + if( pWC->a!=pWC->aStatic ){ |
| 119744 | + sqlite3DbFree(db, pWC->a); |
| 119745 | + } |
| 119746 | +} |
| 119747 | + |
| 119748 | + |
| 119749 | +/* |
| 119750 | +** These routines walk (recursively) an expression tree and generate |
| 119751 | +** a bitmask indicating which tables are used in that expression |
| 119752 | +** tree. |
| 119753 | +*/ |
| 119754 | +SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){ |
| 119755 | + Bitmask mask = 0; |
| 119756 | + if( p==0 ) return 0; |
| 119757 | + if( p->op==TK_COLUMN ){ |
| 119758 | + mask = sqlite3WhereGetMask(pMaskSet, p->iTable); |
| 119759 | + return mask; |
| 119760 | + } |
| 119761 | + mask = sqlite3WhereExprUsage(pMaskSet, p->pRight); |
| 119762 | + mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft); |
| 119763 | + if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 119764 | + mask |= exprSelectUsage(pMaskSet, p->x.pSelect); |
| 119765 | + }else{ |
| 119766 | + mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList); |
| 119767 | + } |
| 119768 | + return mask; |
| 119769 | +} |
| 119770 | +SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){ |
| 119771 | + int i; |
| 119772 | + Bitmask mask = 0; |
| 119773 | + if( pList ){ |
| 119774 | + for(i=0; i<pList->nExpr; i++){ |
| 119775 | + mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr); |
| 119776 | + } |
| 119777 | + } |
| 119778 | + return mask; |
| 119779 | +} |
| 119780 | + |
| 119781 | + |
| 119782 | +/* |
| 119783 | +** Call exprAnalyze on all terms in a WHERE clause. |
| 119784 | +** |
| 119785 | +** Note that exprAnalyze() might add new virtual terms onto the |
| 119786 | +** end of the WHERE clause. We do not want to analyze these new |
| 119787 | +** virtual terms, so start analyzing at the end and work forward |
| 119788 | +** so that the added virtual terms are never processed. |
| 119789 | +*/ |
| 119790 | +SQLITE_PRIVATE void sqlite3WhereExprAnalyze( |
| 119791 | + SrcList *pTabList, /* the FROM clause */ |
| 119792 | + WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 119793 | +){ |
| 119794 | + int i; |
| 119795 | + for(i=pWC->nTerm-1; i>=0; i--){ |
| 119796 | + exprAnalyze(pTabList, pWC, i); |
| 119797 | + } |
| 119798 | +} |
| 119799 | + |
| 119800 | +/************** End of whereexpr.c *******************************************/ |
| 119801 | +/************** Begin file where.c *******************************************/ |
| 119802 | +/* |
| 119803 | +** 2001 September 15 |
| 119804 | +** |
| 119805 | +** The author disclaims copyright to this source code. In place of |
| 119806 | +** a legal notice, here is a blessing: |
| 119807 | +** |
| 119808 | +** May you do good and not evil. |
| 119809 | +** May you find forgiveness for yourself and forgive others. |
| 119810 | +** May you share freely, never taking more than you give. |
| 119811 | +** |
| 119812 | +************************************************************************* |
| 119813 | +** This module contains C code that generates VDBE code used to process |
| 119814 | +** the WHERE clause of SQL statements. This module is responsible for |
| 119815 | +** generating the code that loops through a table looking for applicable |
| 119816 | +** rows. Indices are selected and used to speed the search when doing |
| 119817 | +** so is applicable. Because this module is responsible for selecting |
| 119818 | +** indices, you might also think of this module as the "query optimizer". |
| 119819 | +*/ |
| 119820 | + |
| 119821 | +/* Forward declaration of methods */ |
| 119822 | +static int whereLoopResize(sqlite3*, WhereLoop*, int); |
| 119823 | + |
| 119824 | +/* Test variable that can be set to enable WHERE tracing */ |
| 119825 | +#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 119826 | +/***/ int sqlite3WhereTrace = 0; |
| 119827 | +#endif |
| 119828 | + |
| 119829 | + |
| 119830 | +/* |
| 119831 | +** Return the estimated number of output rows from a WHERE clause |
| 119832 | +*/ |
| 119833 | +SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
| 119834 | + return sqlite3LogEstToInt(pWInfo->nRowOut); |
| 119835 | +} |
| 119836 | + |
| 119837 | +/* |
| 119838 | +** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this |
| 119839 | +** WHERE clause returns outputs for DISTINCT processing. |
| 119840 | +*/ |
| 119841 | +SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ |
| 119842 | + return pWInfo->eDistinct; |
| 119843 | +} |
| 119844 | + |
| 119845 | +/* |
| 119846 | +** Return TRUE if the WHERE clause returns rows in ORDER BY order. |
| 119847 | +** Return FALSE if the output needs to be sorted. |
| 119848 | +*/ |
| 119849 | +SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ |
| 119850 | + return pWInfo->nOBSat; |
| 119851 | +} |
| 119852 | + |
| 119853 | +/* |
| 119854 | +** Return the VDBE address or label to jump to in order to continue |
| 119855 | +** immediately with the next row of a WHERE clause. |
| 119856 | +*/ |
| 119857 | +SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ |
| 119858 | + assert( pWInfo->iContinue!=0 ); |
| 119859 | + return pWInfo->iContinue; |
| 119860 | +} |
| 119861 | + |
| 119862 | +/* |
| 119863 | +** Return the VDBE address or label to jump to in order to break |
| 119864 | +** out of a WHERE loop. |
| 119865 | +*/ |
| 119866 | +SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ |
| 119867 | + return pWInfo->iBreak; |
| 119868 | +} |
| 119869 | + |
| 119870 | +/* |
| 119871 | +** Return TRUE if an UPDATE or DELETE statement can operate directly on |
| 119872 | +** the rowids returned by a WHERE clause. Return FALSE if doing an |
| 119873 | +** UPDATE or DELETE might change subsequent WHERE clause results. |
| 119874 | +** |
| 119875 | +** If the ONEPASS optimization is used (if this routine returns true) |
| 119876 | +** then also write the indices of open cursors used by ONEPASS |
| 119877 | +** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data |
| 119878 | +** table and iaCur[1] gets the cursor used by an auxiliary index. |
| 119879 | +** Either value may be -1, indicating that cursor is not used. |
| 119880 | +** Any cursors returned will have been opened for writing. |
| 119881 | +** |
| 119882 | +** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is |
| 119883 | +** unable to use the ONEPASS optimization. |
| 119884 | +*/ |
| 119885 | +SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){ |
| 119886 | + memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2); |
| 119887 | + return pWInfo->okOnePass; |
| 119888 | +} |
| 119889 | + |
| 119890 | +/* |
| 119891 | +** Move the content of pSrc into pDest |
| 119892 | +*/ |
| 119893 | +static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){ |
| 119894 | + pDest->n = pSrc->n; |
| 119895 | + memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0])); |
| 119896 | +} |
| 119897 | + |
| 119898 | +/* |
| 119899 | +** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet. |
| 119900 | +** |
| 119901 | +** The new entry might overwrite an existing entry, or it might be |
| 119902 | +** appended, or it might be discarded. Do whatever is the right thing |
| 119903 | +** so that pSet keeps the N_OR_COST best entries seen so far. |
| 119904 | +*/ |
| 119905 | +static int whereOrInsert( |
| 119906 | + WhereOrSet *pSet, /* The WhereOrSet to be updated */ |
| 119907 | + Bitmask prereq, /* Prerequisites of the new entry */ |
| 119908 | + LogEst rRun, /* Run-cost of the new entry */ |
| 119909 | + LogEst nOut /* Number of outputs for the new entry */ |
| 119910 | +){ |
| 119911 | + u16 i; |
| 119912 | + WhereOrCost *p; |
| 119913 | + for(i=pSet->n, p=pSet->a; i>0; i--, p++){ |
| 119914 | + if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){ |
| 119915 | + goto whereOrInsert_done; |
| 119916 | + } |
| 119917 | + if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){ |
| 119918 | + return 0; |
| 119919 | + } |
| 119920 | + } |
| 119921 | + if( pSet->n<N_OR_COST ){ |
| 119922 | + p = &pSet->a[pSet->n++]; |
| 119923 | + p->nOut = nOut; |
| 119924 | + }else{ |
| 119925 | + p = pSet->a; |
| 119926 | + for(i=1; i<pSet->n; i++){ |
| 119927 | + if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i; |
| 119928 | + } |
| 119929 | + if( p->rRun<=rRun ) return 0; |
| 119930 | + } |
| 119931 | +whereOrInsert_done: |
| 119932 | + p->prereq = prereq; |
| 119933 | + p->rRun = rRun; |
| 119934 | + if( p->nOut>nOut ) p->nOut = nOut; |
| 119935 | + return 1; |
| 119936 | +} |
| 119937 | + |
| 119938 | +/* |
| 119939 | +** Return the bitmask for the given cursor number. Return 0 if |
| 119940 | +** iCursor is not in the set. |
| 119941 | +*/ |
| 119942 | +SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){ |
| 119943 | + int i; |
| 119944 | + assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
| 119945 | + for(i=0; i<pMaskSet->n; i++){ |
| 119946 | + if( pMaskSet->ix[i]==iCursor ){ |
| 119947 | + return MASKBIT(i); |
| 119948 | + } |
| 119949 | + } |
| 119950 | + return 0; |
| 119951 | +} |
| 119952 | + |
| 119953 | +/* |
| 119954 | +** Create a new mask for cursor iCursor. |
| 119955 | +** |
| 119956 | +** There is one cursor per table in the FROM clause. The number of |
| 119957 | +** tables in the FROM clause is limited by a test early in the |
| 119958 | +** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] |
| 119959 | +** array will never overflow. |
| 119960 | +*/ |
| 119961 | +static void createMask(WhereMaskSet *pMaskSet, int iCursor){ |
| 119962 | + assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
| 119963 | + pMaskSet->ix[pMaskSet->n++] = iCursor; |
| 119964 | +} |
| 119965 | + |
| 119966 | +/* |
| 119967 | +** Advance to the next WhereTerm that matches according to the criteria |
| 119968 | +** established when the pScan object was initialized by whereScanInit(). |
| 119969 | +** Return NULL if there are no more matching WhereTerms. |
| 119970 | +*/ |
| 119971 | +static WhereTerm *whereScanNext(WhereScan *pScan){ |
| 119972 | + int iCur; /* The cursor on the LHS of the term */ |
| 119973 | + int iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 119974 | + Expr *pX; /* An expression being tested */ |
| 119975 | + WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 119976 | + WhereTerm *pTerm; /* The term being tested */ |
| 119977 | + int k = pScan->k; /* Where to start scanning */ |
| 119978 | + |
| 119979 | + while( pScan->iEquiv<=pScan->nEquiv ){ |
| 119980 | + iCur = pScan->aEquiv[pScan->iEquiv-2]; |
| 119981 | + iColumn = pScan->aEquiv[pScan->iEquiv-1]; |
| 119982 | + while( (pWC = pScan->pWC)!=0 ){ |
| 119983 | + for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
| 119984 | + if( pTerm->leftCursor==iCur |
| 119985 | + && pTerm->u.leftColumn==iColumn |
| 119986 | + && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 119987 | + ){ |
| 119988 | + if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 119989 | + && pScan->nEquiv<ArraySize(pScan->aEquiv) |
| 119990 | + ){ |
| 119991 | + int j; |
| 119992 | + pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); |
| 119993 | + assert( pX->op==TK_COLUMN ); |
| 119994 | + for(j=0; j<pScan->nEquiv; j+=2){ |
| 119995 | + if( pScan->aEquiv[j]==pX->iTable |
| 119996 | + && pScan->aEquiv[j+1]==pX->iColumn ){ |
| 119997 | + break; |
| 119998 | + } |
| 119999 | + } |
| 120000 | + if( j==pScan->nEquiv ){ |
| 120001 | + pScan->aEquiv[j] = pX->iTable; |
| 120002 | + pScan->aEquiv[j+1] = pX->iColumn; |
| 120003 | + pScan->nEquiv += 2; |
| 120004 | + } |
| 120005 | + } |
| 120006 | + if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 120007 | + /* Verify the affinity and collating sequence match */ |
| 120008 | + if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 120009 | + CollSeq *pColl; |
| 120010 | + Parse *pParse = pWC->pWInfo->pParse; |
| 120011 | + pX = pTerm->pExpr; |
| 120012 | + if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 120013 | + continue; |
| 120014 | + } |
| 120015 | + assert(pX->pLeft); |
| 120016 | + pColl = sqlite3BinaryCompareCollSeq(pParse, |
| 120017 | + pX->pLeft, pX->pRight); |
| 120018 | + if( pColl==0 ) pColl = pParse->db->pDfltColl; |
| 120019 | + if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 120020 | + continue; |
| 120021 | + } |
| 120022 | + } |
| 120023 | + if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0 |
| 120024 | + && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 120025 | + && pX->iTable==pScan->aEquiv[0] |
| 120026 | + && pX->iColumn==pScan->aEquiv[1] |
| 120027 | + ){ |
| 120028 | + testcase( pTerm->eOperator & WO_IS ); |
| 120029 | + continue; |
| 120030 | + } |
| 120031 | + pScan->k = k+1; |
| 120032 | + return pTerm; |
| 120033 | + } |
| 120034 | + } |
| 120035 | + } |
| 120036 | + pScan->pWC = pScan->pWC->pOuter; |
| 120037 | + k = 0; |
| 120038 | + } |
| 120039 | + pScan->pWC = pScan->pOrigWC; |
| 120040 | + k = 0; |
| 120041 | + pScan->iEquiv += 2; |
| 120042 | + } |
| 120043 | + return 0; |
| 120044 | +} |
| 120045 | + |
| 120046 | +/* |
| 120047 | +** Initialize a WHERE clause scanner object. Return a pointer to the |
| 120048 | +** first match. Return NULL if there are no matches. |
| 120049 | +** |
| 120050 | +** The scanner will be searching the WHERE clause pWC. It will look |
| 120051 | +** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 120052 | +** iCur. The <op> must be one of the operators described by opMask. |
| 120053 | +** |
| 120054 | +** If the search is for X and the WHERE clause contains terms of the |
| 120055 | +** form X=Y then this routine might also return terms of the form |
| 120056 | +** "Y <op> <expr>". The number of levels of transitivity is limited, |
| 120057 | +** but is enough to handle most commonly occurring SQL statements. |
| 120058 | +** |
| 120059 | +** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 120060 | +** index pIdx. |
| 120061 | +*/ |
| 120062 | +static WhereTerm *whereScanInit( |
| 120063 | + WhereScan *pScan, /* The WhereScan object being initialized */ |
| 120064 | + WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 120065 | + int iCur, /* Cursor to scan for */ |
| 120066 | + int iColumn, /* Column to scan for */ |
| 120067 | + u32 opMask, /* Operator(s) to scan for */ |
| 120068 | + Index *pIdx /* Must be compatible with this index */ |
| 120069 | +){ |
| 120070 | + int j; |
| 120071 | + |
| 120072 | + /* memset(pScan, 0, sizeof(*pScan)); */ |
| 120073 | + pScan->pOrigWC = pWC; |
| 120074 | + pScan->pWC = pWC; |
| 120075 | + if( pIdx && iColumn>=0 ){ |
| 120076 | + pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 120077 | + for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
| 120078 | + if( NEVER(j>pIdx->nColumn) ) return 0; |
| 120079 | + } |
| 120080 | + pScan->zCollName = pIdx->azColl[j]; |
| 120081 | + }else{ |
| 120082 | + pScan->idxaff = 0; |
| 120083 | + pScan->zCollName = 0; |
| 120084 | + } |
| 120085 | + pScan->opMask = opMask; |
| 120086 | + pScan->k = 0; |
| 120087 | + pScan->aEquiv[0] = iCur; |
| 120088 | + pScan->aEquiv[1] = iColumn; |
| 120089 | + pScan->nEquiv = 2; |
| 120090 | + pScan->iEquiv = 2; |
| 120091 | + return whereScanNext(pScan); |
| 120092 | +} |
| 120093 | + |
| 120094 | +/* |
| 120095 | +** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 120096 | +** where X is a reference to the iColumn of table iCur and <op> is one of |
| 120097 | +** the WO_xx operator codes specified by the op parameter. |
| 120098 | +** Return a pointer to the term. Return 0 if not found. |
| 120099 | +** |
| 120100 | +** The term returned might by Y=<expr> if there is another constraint in |
| 120101 | +** the WHERE clause that specifies that X=Y. Any such constraints will be |
| 120102 | +** identified by the WO_EQUIV bit in the pTerm->eOperator field. The |
| 120103 | +** aEquiv[] array holds X and all its equivalents, with each SQL variable |
| 120104 | +** taking up two slots in aEquiv[]. The first slot is for the cursor number |
| 120105 | +** and the second is for the column number. There are 22 slots in aEquiv[] |
| 120106 | +** so that means we can look for X plus up to 10 other equivalent values. |
| 120107 | +** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3 |
| 120108 | +** and ... and A9=A10 and A10=<expr>. |
| 120109 | +** |
| 120110 | +** If there are multiple terms in the WHERE clause of the form "X <op> <expr>" |
| 120111 | +** then try for the one with no dependencies on <expr> - in other words where |
| 120112 | +** <expr> is a constant expression of some kind. Only return entries of |
| 120113 | +** the form "X <op> Y" where Y is a column in another table if no terms of |
| 120114 | +** the form "X <op> <const-expr>" exist. If no terms with a constant RHS |
| 120115 | +** exist, try to return a term that does not use WO_EQUIV. |
| 120116 | +*/ |
| 120117 | +SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm( |
| 120118 | + WhereClause *pWC, /* The WHERE clause to be searched */ |
| 120119 | + int iCur, /* Cursor number of LHS */ |
| 120120 | + int iColumn, /* Column number of LHS */ |
| 120121 | + Bitmask notReady, /* RHS must not overlap with this mask */ |
| 120122 | + u32 op, /* Mask of WO_xx values describing operator */ |
| 120123 | + Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 120124 | +){ |
| 120125 | + WhereTerm *pResult = 0; |
| 120126 | + WhereTerm *p; |
| 120127 | + WhereScan scan; |
| 120128 | + |
| 120129 | + p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
| 120130 | + op &= WO_EQ|WO_IS; |
| 120131 | + while( p ){ |
| 120132 | + if( (p->prereqRight & notReady)==0 ){ |
| 120133 | + if( p->prereqRight==0 && (p->eOperator&op)!=0 ){ |
| 120134 | + testcase( p->eOperator & WO_IS ); |
| 120135 | + return p; |
| 120136 | + } |
| 120137 | + if( pResult==0 ) pResult = p; |
| 120138 | + } |
| 120139 | + p = whereScanNext(&scan); |
| 120140 | + } |
| 120141 | + return pResult; |
| 120142 | +} |
| 117846 | 120143 | |
| 117847 | 120144 | /* |
| 117848 | 120145 | ** This function searches pList for an entry that matches the iCol-th column |
| 117849 | 120146 | ** of index pIdx. |
| 117850 | 120147 | ** |
| | @@ -117879,12 +120176,12 @@ |
| 117879 | 120176 | |
| 117880 | 120177 | /* |
| 117881 | 120178 | ** Return true if the DISTINCT expression-list passed as the third argument |
| 117882 | 120179 | ** is redundant. |
| 117883 | 120180 | ** |
| 117884 | | -** A DISTINCT list is redundant if the database contains some subset of |
| 117885 | | -** columns that are unique and non-null. |
| 120181 | +** A DISTINCT list is redundant if any subset of the columns in the |
| 120182 | +** DISTINCT list are collectively unique and individually non-null. |
| 117886 | 120183 | */ |
| 117887 | 120184 | static int isDistinctRedundant( |
| 117888 | 120185 | Parse *pParse, /* Parsing context */ |
| 117889 | 120186 | SrcList *pTabList, /* The FROM clause */ |
| 117890 | 120187 | WhereClause *pWC, /* The WHERE clause */ |
| | @@ -117926,11 +120223,11 @@ |
| 117926 | 120223 | */ |
| 117927 | 120224 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 117928 | 120225 | if( !IsUniqueIndex(pIdx) ) continue; |
| 117929 | 120226 | for(i=0; i<pIdx->nKeyCol; i++){ |
| 117930 | 120227 | i16 iCol = pIdx->aiColumn[i]; |
| 117931 | | - if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){ |
| 120228 | + if( 0==sqlite3WhereFindTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){ |
| 117932 | 120229 | int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i); |
| 117933 | 120230 | if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){ |
| 117934 | 120231 | break; |
| 117935 | 120232 | } |
| 117936 | 120233 | } |
| | @@ -118257,10 +120554,11 @@ |
| 118257 | 120554 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 118258 | 120555 | */ |
| 118259 | 120556 | static sqlite3_index_info *allocateIndexInfo( |
| 118260 | 120557 | Parse *pParse, |
| 118261 | 120558 | WhereClause *pWC, |
| 120559 | + Bitmask mUnusable, /* Ignore terms with these prereqs */ |
| 118262 | 120560 | struct SrcList_item *pSrc, |
| 118263 | 120561 | ExprList *pOrderBy |
| 118264 | 120562 | ){ |
| 118265 | 120563 | int i, j; |
| 118266 | 120564 | int nTerm; |
| | @@ -118273,10 +120571,11 @@ |
| 118273 | 120571 | |
| 118274 | 120572 | /* Count the number of possible WHERE clause constraints referring |
| 118275 | 120573 | ** to this virtual table */ |
| 118276 | 120574 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 118277 | 120575 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
| 120576 | + if( pTerm->prereqRight & mUnusable ) continue; |
| 118278 | 120577 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 118279 | 120578 | testcase( pTerm->eOperator & WO_IN ); |
| 118280 | 120579 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 118281 | 120580 | testcase( pTerm->eOperator & WO_IS ); |
| 118282 | 120581 | testcase( pTerm->eOperator & WO_ALL ); |
| | @@ -118327,10 +120626,11 @@ |
| 118327 | 120626 | pUsage; |
| 118328 | 120627 | |
| 118329 | 120628 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 118330 | 120629 | u8 op; |
| 118331 | 120630 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
| 120631 | + if( pTerm->prereqRight & mUnusable ) continue; |
| 118332 | 120632 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 118333 | 120633 | testcase( pTerm->eOperator & WO_IN ); |
| 118334 | 120634 | testcase( pTerm->eOperator & WO_IS ); |
| 118335 | 120635 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 118336 | 120636 | testcase( pTerm->eOperator & WO_ALL ); |
| | @@ -119048,1491 +121348,10 @@ |
| 119048 | 121348 | assert( pBuilder->nRecValid==nRecValid ); |
| 119049 | 121349 | return rc; |
| 119050 | 121350 | } |
| 119051 | 121351 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| 119052 | 121352 | |
| 119053 | | -/* |
| 119054 | | -** Disable a term in the WHERE clause. Except, do not disable the term |
| 119055 | | -** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 119056 | | -** or USING clause of that join. |
| 119057 | | -** |
| 119058 | | -** Consider the term t2.z='ok' in the following queries: |
| 119059 | | -** |
| 119060 | | -** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 119061 | | -** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 119062 | | -** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 119063 | | -** |
| 119064 | | -** The t2.z='ok' is disabled in the in (2) because it originates |
| 119065 | | -** in the ON clause. The term is disabled in (3) because it is not part |
| 119066 | | -** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 119067 | | -** |
| 119068 | | -** Disabling a term causes that term to not be tested in the inner loop |
| 119069 | | -** of the join. Disabling is an optimization. When terms are satisfied |
| 119070 | | -** by indices, we disable them to prevent redundant tests in the inner |
| 119071 | | -** loop. We would get the correct results if nothing were ever disabled, |
| 119072 | | -** but joins might run a little slower. The trick is to disable as much |
| 119073 | | -** as we can without disabling too much. If we disabled in (1), we'd get |
| 119074 | | -** the wrong answer. See ticket #813. |
| 119075 | | -** |
| 119076 | | -** If all the children of a term are disabled, then that term is also |
| 119077 | | -** automatically disabled. In this way, terms get disabled if derived |
| 119078 | | -** virtual terms are tested first. For example: |
| 119079 | | -** |
| 119080 | | -** x GLOB 'abc*' AND x>='abc' AND x<'acd' |
| 119081 | | -** \___________/ \______/ \_____/ |
| 119082 | | -** parent child1 child2 |
| 119083 | | -** |
| 119084 | | -** Only the parent term was in the original WHERE clause. The child1 |
| 119085 | | -** and child2 terms were added by the LIKE optimization. If both of |
| 119086 | | -** the virtual child terms are valid, then testing of the parent can be |
| 119087 | | -** skipped. |
| 119088 | | -** |
| 119089 | | -** Usually the parent term is marked as TERM_CODED. But if the parent |
| 119090 | | -** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead. |
| 119091 | | -** The TERM_LIKECOND marking indicates that the term should be coded inside |
| 119092 | | -** a conditional such that is only evaluated on the second pass of a |
| 119093 | | -** LIKE-optimization loop, when scanning BLOBs instead of strings. |
| 119094 | | -*/ |
| 119095 | | -static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
| 119096 | | - int nLoop = 0; |
| 119097 | | - while( pTerm |
| 119098 | | - && (pTerm->wtFlags & TERM_CODED)==0 |
| 119099 | | - && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 119100 | | - && (pLevel->notReady & pTerm->prereqAll)==0 |
| 119101 | | - ){ |
| 119102 | | - if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){ |
| 119103 | | - pTerm->wtFlags |= TERM_LIKECOND; |
| 119104 | | - }else{ |
| 119105 | | - pTerm->wtFlags |= TERM_CODED; |
| 119106 | | - } |
| 119107 | | - if( pTerm->iParent<0 ) break; |
| 119108 | | - pTerm = &pTerm->pWC->a[pTerm->iParent]; |
| 119109 | | - pTerm->nChild--; |
| 119110 | | - if( pTerm->nChild!=0 ) break; |
| 119111 | | - nLoop++; |
| 119112 | | - } |
| 119113 | | -} |
| 119114 | | - |
| 119115 | | -/* |
| 119116 | | -** Code an OP_Affinity opcode to apply the column affinity string zAff |
| 119117 | | -** to the n registers starting at base. |
| 119118 | | -** |
| 119119 | | -** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the |
| 119120 | | -** beginning and end of zAff are ignored. If all entries in zAff are |
| 119121 | | -** SQLITE_AFF_NONE, then no code gets generated. |
| 119122 | | -** |
| 119123 | | -** This routine makes its own copy of zAff so that the caller is free |
| 119124 | | -** to modify zAff after this routine returns. |
| 119125 | | -*/ |
| 119126 | | -static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ |
| 119127 | | - Vdbe *v = pParse->pVdbe; |
| 119128 | | - if( zAff==0 ){ |
| 119129 | | - assert( pParse->db->mallocFailed ); |
| 119130 | | - return; |
| 119131 | | - } |
| 119132 | | - assert( v!=0 ); |
| 119133 | | - |
| 119134 | | - /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning |
| 119135 | | - ** and end of the affinity string. |
| 119136 | | - */ |
| 119137 | | - while( n>0 && zAff[0]==SQLITE_AFF_NONE ){ |
| 119138 | | - n--; |
| 119139 | | - base++; |
| 119140 | | - zAff++; |
| 119141 | | - } |
| 119142 | | - while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){ |
| 119143 | | - n--; |
| 119144 | | - } |
| 119145 | | - |
| 119146 | | - /* Code the OP_Affinity opcode if there is anything left to do. */ |
| 119147 | | - if( n>0 ){ |
| 119148 | | - sqlite3VdbeAddOp2(v, OP_Affinity, base, n); |
| 119149 | | - sqlite3VdbeChangeP4(v, -1, zAff, n); |
| 119150 | | - sqlite3ExprCacheAffinityChange(pParse, base, n); |
| 119151 | | - } |
| 119152 | | -} |
| 119153 | | - |
| 119154 | | - |
| 119155 | | -/* |
| 119156 | | -** Generate code for a single equality term of the WHERE clause. An equality |
| 119157 | | -** term can be either X=expr or X IN (...). pTerm is the term to be |
| 119158 | | -** coded. |
| 119159 | | -** |
| 119160 | | -** The current value for the constraint is left in register iReg. |
| 119161 | | -** |
| 119162 | | -** For a constraint of the form X=expr, the expression is evaluated and its |
| 119163 | | -** result is left on the stack. For constraints of the form X IN (...) |
| 119164 | | -** this routine sets up a loop that will iterate over all values of X. |
| 119165 | | -*/ |
| 119166 | | -static int codeEqualityTerm( |
| 119167 | | - Parse *pParse, /* The parsing context */ |
| 119168 | | - WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
| 119169 | | - WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 119170 | | - int iEq, /* Index of the equality term within this level */ |
| 119171 | | - int bRev, /* True for reverse-order IN operations */ |
| 119172 | | - int iTarget /* Attempt to leave results in this register */ |
| 119173 | | -){ |
| 119174 | | - Expr *pX = pTerm->pExpr; |
| 119175 | | - Vdbe *v = pParse->pVdbe; |
| 119176 | | - int iReg; /* Register holding results */ |
| 119177 | | - |
| 119178 | | - assert( iTarget>0 ); |
| 119179 | | - if( pX->op==TK_EQ || pX->op==TK_IS ){ |
| 119180 | | - iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); |
| 119181 | | - }else if( pX->op==TK_ISNULL ){ |
| 119182 | | - iReg = iTarget; |
| 119183 | | - sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); |
| 119184 | | -#ifndef SQLITE_OMIT_SUBQUERY |
| 119185 | | - }else{ |
| 119186 | | - int eType; |
| 119187 | | - int iTab; |
| 119188 | | - struct InLoop *pIn; |
| 119189 | | - WhereLoop *pLoop = pLevel->pWLoop; |
| 119190 | | - |
| 119191 | | - if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 119192 | | - && pLoop->u.btree.pIndex!=0 |
| 119193 | | - && pLoop->u.btree.pIndex->aSortOrder[iEq] |
| 119194 | | - ){ |
| 119195 | | - testcase( iEq==0 ); |
| 119196 | | - testcase( bRev ); |
| 119197 | | - bRev = !bRev; |
| 119198 | | - } |
| 119199 | | - assert( pX->op==TK_IN ); |
| 119200 | | - iReg = iTarget; |
| 119201 | | - eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0); |
| 119202 | | - if( eType==IN_INDEX_INDEX_DESC ){ |
| 119203 | | - testcase( bRev ); |
| 119204 | | - bRev = !bRev; |
| 119205 | | - } |
| 119206 | | - iTab = pX->iTable; |
| 119207 | | - sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
| 119208 | | - VdbeCoverageIf(v, bRev); |
| 119209 | | - VdbeCoverageIf(v, !bRev); |
| 119210 | | - assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 119211 | | - pLoop->wsFlags |= WHERE_IN_ABLE; |
| 119212 | | - if( pLevel->u.in.nIn==0 ){ |
| 119213 | | - pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 119214 | | - } |
| 119215 | | - pLevel->u.in.nIn++; |
| 119216 | | - pLevel->u.in.aInLoop = |
| 119217 | | - sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, |
| 119218 | | - sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); |
| 119219 | | - pIn = pLevel->u.in.aInLoop; |
| 119220 | | - if( pIn ){ |
| 119221 | | - pIn += pLevel->u.in.nIn - 1; |
| 119222 | | - pIn->iCur = iTab; |
| 119223 | | - if( eType==IN_INDEX_ROWID ){ |
| 119224 | | - pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); |
| 119225 | | - }else{ |
| 119226 | | - pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); |
| 119227 | | - } |
| 119228 | | - pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen; |
| 119229 | | - sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v); |
| 119230 | | - }else{ |
| 119231 | | - pLevel->u.in.nIn = 0; |
| 119232 | | - } |
| 119233 | | -#endif |
| 119234 | | - } |
| 119235 | | - disableTerm(pLevel, pTerm); |
| 119236 | | - return iReg; |
| 119237 | | -} |
| 119238 | | - |
| 119239 | | -/* |
| 119240 | | -** Generate code that will evaluate all == and IN constraints for an |
| 119241 | | -** index scan. |
| 119242 | | -** |
| 119243 | | -** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 119244 | | -** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 119245 | | -** The index has as many as three equality constraints, but in this |
| 119246 | | -** example, the third "c" value is an inequality. So only two |
| 119247 | | -** constraints are coded. This routine will generate code to evaluate |
| 119248 | | -** a==5 and b IN (1,2,3). The current values for a and b will be stored |
| 119249 | | -** in consecutive registers and the index of the first register is returned. |
| 119250 | | -** |
| 119251 | | -** In the example above nEq==2. But this subroutine works for any value |
| 119252 | | -** of nEq including 0. If nEq==0, this routine is nearly a no-op. |
| 119253 | | -** The only thing it does is allocate the pLevel->iMem memory cell and |
| 119254 | | -** compute the affinity string. |
| 119255 | | -** |
| 119256 | | -** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints |
| 119257 | | -** are == or IN and are covered by the nEq. nExtraReg is 1 if there is |
| 119258 | | -** an inequality constraint (such as the "c>=5 AND c<10" in the example) that |
| 119259 | | -** occurs after the nEq quality constraints. |
| 119260 | | -** |
| 119261 | | -** This routine allocates a range of nEq+nExtraReg memory cells and returns |
| 119262 | | -** the index of the first memory cell in that range. The code that |
| 119263 | | -** calls this routine will use that memory range to store keys for |
| 119264 | | -** start and termination conditions of the loop. |
| 119265 | | -** key value of the loop. If one or more IN operators appear, then |
| 119266 | | -** this routine allocates an additional nEq memory cells for internal |
| 119267 | | -** use. |
| 119268 | | -** |
| 119269 | | -** Before returning, *pzAff is set to point to a buffer containing a |
| 119270 | | -** copy of the column affinity string of the index allocated using |
| 119271 | | -** sqlite3DbMalloc(). Except, entries in the copy of the string associated |
| 119272 | | -** with equality constraints that use NONE affinity are set to |
| 119273 | | -** SQLITE_AFF_NONE. This is to deal with SQL such as the following: |
| 119274 | | -** |
| 119275 | | -** CREATE TABLE t1(a TEXT PRIMARY KEY, b); |
| 119276 | | -** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; |
| 119277 | | -** |
| 119278 | | -** In the example above, the index on t1(a) has TEXT affinity. But since |
| 119279 | | -** the right hand side of the equality constraint (t2.b) has NONE affinity, |
| 119280 | | -** no conversion should be attempted before using a t2.b value as part of |
| 119281 | | -** a key to search the index. Hence the first byte in the returned affinity |
| 119282 | | -** string in this example would be set to SQLITE_AFF_NONE. |
| 119283 | | -*/ |
| 119284 | | -static int codeAllEqualityTerms( |
| 119285 | | - Parse *pParse, /* Parsing context */ |
| 119286 | | - WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
| 119287 | | - int bRev, /* Reverse the order of IN operators */ |
| 119288 | | - int nExtraReg, /* Number of extra registers to allocate */ |
| 119289 | | - char **pzAff /* OUT: Set to point to affinity string */ |
| 119290 | | -){ |
| 119291 | | - u16 nEq; /* The number of == or IN constraints to code */ |
| 119292 | | - u16 nSkip; /* Number of left-most columns to skip */ |
| 119293 | | - Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 119294 | | - Index *pIdx; /* The index being used for this loop */ |
| 119295 | | - WhereTerm *pTerm; /* A single constraint term */ |
| 119296 | | - WhereLoop *pLoop; /* The WhereLoop object */ |
| 119297 | | - int j; /* Loop counter */ |
| 119298 | | - int regBase; /* Base register */ |
| 119299 | | - int nReg; /* Number of registers to allocate */ |
| 119300 | | - char *zAff; /* Affinity string to return */ |
| 119301 | | - |
| 119302 | | - /* This module is only called on query plans that use an index. */ |
| 119303 | | - pLoop = pLevel->pWLoop; |
| 119304 | | - assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 119305 | | - nEq = pLoop->u.btree.nEq; |
| 119306 | | - nSkip = pLoop->nSkip; |
| 119307 | | - pIdx = pLoop->u.btree.pIndex; |
| 119308 | | - assert( pIdx!=0 ); |
| 119309 | | - |
| 119310 | | - /* Figure out how many memory cells we will need then allocate them. |
| 119311 | | - */ |
| 119312 | | - regBase = pParse->nMem + 1; |
| 119313 | | - nReg = pLoop->u.btree.nEq + nExtraReg; |
| 119314 | | - pParse->nMem += nReg; |
| 119315 | | - |
| 119316 | | - zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 119317 | | - if( !zAff ){ |
| 119318 | | - pParse->db->mallocFailed = 1; |
| 119319 | | - } |
| 119320 | | - |
| 119321 | | - if( nSkip ){ |
| 119322 | | - int iIdxCur = pLevel->iIdxCur; |
| 119323 | | - sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur); |
| 119324 | | - VdbeCoverageIf(v, bRev==0); |
| 119325 | | - VdbeCoverageIf(v, bRev!=0); |
| 119326 | | - VdbeComment((v, "begin skip-scan on %s", pIdx->zName)); |
| 119327 | | - j = sqlite3VdbeAddOp0(v, OP_Goto); |
| 119328 | | - pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT), |
| 119329 | | - iIdxCur, 0, regBase, nSkip); |
| 119330 | | - VdbeCoverageIf(v, bRev==0); |
| 119331 | | - VdbeCoverageIf(v, bRev!=0); |
| 119332 | | - sqlite3VdbeJumpHere(v, j); |
| 119333 | | - for(j=0; j<nSkip; j++){ |
| 119334 | | - sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j); |
| 119335 | | - assert( pIdx->aiColumn[j]>=0 ); |
| 119336 | | - VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName)); |
| 119337 | | - } |
| 119338 | | - } |
| 119339 | | - |
| 119340 | | - /* Evaluate the equality constraints |
| 119341 | | - */ |
| 119342 | | - assert( zAff==0 || (int)strlen(zAff)>=nEq ); |
| 119343 | | - for(j=nSkip; j<nEq; j++){ |
| 119344 | | - int r1; |
| 119345 | | - pTerm = pLoop->aLTerm[j]; |
| 119346 | | - assert( pTerm!=0 ); |
| 119347 | | - /* The following testcase is true for indices with redundant columns. |
| 119348 | | - ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 119349 | | - testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
| 119350 | | - testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 119351 | | - r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
| 119352 | | - if( r1!=regBase+j ){ |
| 119353 | | - if( nReg==1 ){ |
| 119354 | | - sqlite3ReleaseTempReg(pParse, regBase); |
| 119355 | | - regBase = r1; |
| 119356 | | - }else{ |
| 119357 | | - sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); |
| 119358 | | - } |
| 119359 | | - } |
| 119360 | | - testcase( pTerm->eOperator & WO_ISNULL ); |
| 119361 | | - testcase( pTerm->eOperator & WO_IN ); |
| 119362 | | - if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ |
| 119363 | | - Expr *pRight = pTerm->pExpr->pRight; |
| 119364 | | - if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){ |
| 119365 | | - sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk); |
| 119366 | | - VdbeCoverage(v); |
| 119367 | | - } |
| 119368 | | - if( zAff ){ |
| 119369 | | - if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){ |
| 119370 | | - zAff[j] = SQLITE_AFF_NONE; |
| 119371 | | - } |
| 119372 | | - if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ |
| 119373 | | - zAff[j] = SQLITE_AFF_NONE; |
| 119374 | | - } |
| 119375 | | - } |
| 119376 | | - } |
| 119377 | | - } |
| 119378 | | - *pzAff = zAff; |
| 119379 | | - return regBase; |
| 119380 | | -} |
| 119381 | | - |
| 119382 | | -#ifndef SQLITE_OMIT_EXPLAIN |
| 119383 | | -/* |
| 119384 | | -** This routine is a helper for explainIndexRange() below |
| 119385 | | -** |
| 119386 | | -** pStr holds the text of an expression that we are building up one term |
| 119387 | | -** at a time. This routine adds a new term to the end of the expression. |
| 119388 | | -** Terms are separated by AND so add the "AND" text for second and subsequent |
| 119389 | | -** terms only. |
| 119390 | | -*/ |
| 119391 | | -static void explainAppendTerm( |
| 119392 | | - StrAccum *pStr, /* The text expression being built */ |
| 119393 | | - int iTerm, /* Index of this term. First is zero */ |
| 119394 | | - const char *zColumn, /* Name of the column */ |
| 119395 | | - const char *zOp /* Name of the operator */ |
| 119396 | | -){ |
| 119397 | | - if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 119398 | | - sqlite3StrAccumAppendAll(pStr, zColumn); |
| 119399 | | - sqlite3StrAccumAppend(pStr, zOp, 1); |
| 119400 | | - sqlite3StrAccumAppend(pStr, "?", 1); |
| 119401 | | -} |
| 119402 | | - |
| 119403 | | -/* |
| 119404 | | -** Argument pLevel describes a strategy for scanning table pTab. This |
| 119405 | | -** function appends text to pStr that describes the subset of table |
| 119406 | | -** rows scanned by the strategy in the form of an SQL expression. |
| 119407 | | -** |
| 119408 | | -** For example, if the query: |
| 119409 | | -** |
| 119410 | | -** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 119411 | | -** |
| 119412 | | -** is run and there is an index on (a, b), then this function returns a |
| 119413 | | -** string similar to: |
| 119414 | | -** |
| 119415 | | -** "a=? AND b>?" |
| 119416 | | -*/ |
| 119417 | | -static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){ |
| 119418 | | - Index *pIndex = pLoop->u.btree.pIndex; |
| 119419 | | - u16 nEq = pLoop->u.btree.nEq; |
| 119420 | | - u16 nSkip = pLoop->nSkip; |
| 119421 | | - int i, j; |
| 119422 | | - Column *aCol = pTab->aCol; |
| 119423 | | - i16 *aiColumn = pIndex->aiColumn; |
| 119424 | | - |
| 119425 | | - if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; |
| 119426 | | - sqlite3StrAccumAppend(pStr, " (", 2); |
| 119427 | | - for(i=0; i<nEq; i++){ |
| 119428 | | - char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName; |
| 119429 | | - if( i>=nSkip ){ |
| 119430 | | - explainAppendTerm(pStr, i, z, "="); |
| 119431 | | - }else{ |
| 119432 | | - if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 119433 | | - sqlite3XPrintf(pStr, 0, "ANY(%s)", z); |
| 119434 | | - } |
| 119435 | | - } |
| 119436 | | - |
| 119437 | | - j = i; |
| 119438 | | - if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
| 119439 | | - char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName; |
| 119440 | | - explainAppendTerm(pStr, i++, z, ">"); |
| 119441 | | - } |
| 119442 | | - if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
| 119443 | | - char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName; |
| 119444 | | - explainAppendTerm(pStr, i, z, "<"); |
| 119445 | | - } |
| 119446 | | - sqlite3StrAccumAppend(pStr, ")", 1); |
| 119447 | | -} |
| 119448 | | - |
| 119449 | | -/* |
| 119450 | | -** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 119451 | | -** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was |
| 119452 | | -** defined at compile-time. If it is not a no-op, a single OP_Explain opcode |
| 119453 | | -** is added to the output to describe the table scan strategy in pLevel. |
| 119454 | | -** |
| 119455 | | -** If an OP_Explain opcode is added to the VM, its address is returned. |
| 119456 | | -** Otherwise, if no OP_Explain is coded, zero is returned. |
| 119457 | | -*/ |
| 119458 | | -static int explainOneScan( |
| 119459 | | - Parse *pParse, /* Parse context */ |
| 119460 | | - SrcList *pTabList, /* Table list this loop refers to */ |
| 119461 | | - WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 119462 | | - int iLevel, /* Value for "level" column of output */ |
| 119463 | | - int iFrom, /* Value for "from" column of output */ |
| 119464 | | - u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 119465 | | -){ |
| 119466 | | - int ret = 0; |
| 119467 | | -#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 119468 | | - if( pParse->explain==2 ) |
| 119469 | | -#endif |
| 119470 | | - { |
| 119471 | | - struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 119472 | | - Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 119473 | | - sqlite3 *db = pParse->db; /* Database handle */ |
| 119474 | | - int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
| 119475 | | - int isSearch; /* True for a SEARCH. False for SCAN. */ |
| 119476 | | - WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 119477 | | - u32 flags; /* Flags that describe this loop */ |
| 119478 | | - char *zMsg; /* Text to add to EQP output */ |
| 119479 | | - StrAccum str; /* EQP output string */ |
| 119480 | | - char zBuf[100]; /* Initial space for EQP output string */ |
| 119481 | | - |
| 119482 | | - pLoop = pLevel->pWLoop; |
| 119483 | | - flags = pLoop->wsFlags; |
| 119484 | | - if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0; |
| 119485 | | - |
| 119486 | | - isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 119487 | | - || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 119488 | | - || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 119489 | | - |
| 119490 | | - sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); |
| 119491 | | - sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN"); |
| 119492 | | - if( pItem->pSelect ){ |
| 119493 | | - sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId); |
| 119494 | | - }else{ |
| 119495 | | - sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName); |
| 119496 | | - } |
| 119497 | | - |
| 119498 | | - if( pItem->zAlias ){ |
| 119499 | | - sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias); |
| 119500 | | - } |
| 119501 | | - if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){ |
| 119502 | | - const char *zFmt = 0; |
| 119503 | | - Index *pIdx; |
| 119504 | | - |
| 119505 | | - assert( pLoop->u.btree.pIndex!=0 ); |
| 119506 | | - pIdx = pLoop->u.btree.pIndex; |
| 119507 | | - assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); |
| 119508 | | - if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ |
| 119509 | | - if( isSearch ){ |
| 119510 | | - zFmt = "PRIMARY KEY"; |
| 119511 | | - } |
| 119512 | | - }else if( flags & WHERE_PARTIALIDX ){ |
| 119513 | | - zFmt = "AUTOMATIC PARTIAL COVERING INDEX"; |
| 119514 | | - }else if( flags & WHERE_AUTO_INDEX ){ |
| 119515 | | - zFmt = "AUTOMATIC COVERING INDEX"; |
| 119516 | | - }else if( flags & WHERE_IDX_ONLY ){ |
| 119517 | | - zFmt = "COVERING INDEX %s"; |
| 119518 | | - }else{ |
| 119519 | | - zFmt = "INDEX %s"; |
| 119520 | | - } |
| 119521 | | - if( zFmt ){ |
| 119522 | | - sqlite3StrAccumAppend(&str, " USING ", 7); |
| 119523 | | - sqlite3XPrintf(&str, 0, zFmt, pIdx->zName); |
| 119524 | | - explainIndexRange(&str, pLoop, pItem->pTab); |
| 119525 | | - } |
| 119526 | | - }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
| 119527 | | - const char *zRange; |
| 119528 | | - if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
| 119529 | | - zRange = "(rowid=?)"; |
| 119530 | | - }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
| 119531 | | - zRange = "(rowid>? AND rowid<?)"; |
| 119532 | | - }else if( flags&WHERE_BTM_LIMIT ){ |
| 119533 | | - zRange = "(rowid>?)"; |
| 119534 | | - }else{ |
| 119535 | | - assert( flags&WHERE_TOP_LIMIT); |
| 119536 | | - zRange = "(rowid<?)"; |
| 119537 | | - } |
| 119538 | | - sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY "); |
| 119539 | | - sqlite3StrAccumAppendAll(&str, zRange); |
| 119540 | | - } |
| 119541 | | -#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 119542 | | - else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
| 119543 | | - sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s", |
| 119544 | | - pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
| 119545 | | - } |
| 119546 | | -#endif |
| 119547 | | -#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS |
| 119548 | | - if( pLoop->nOut>=10 ){ |
| 119549 | | - sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut)); |
| 119550 | | - }else{ |
| 119551 | | - sqlite3StrAccumAppend(&str, " (~1 row)", 9); |
| 119552 | | - } |
| 119553 | | -#endif |
| 119554 | | - zMsg = sqlite3StrAccumFinish(&str); |
| 119555 | | - ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC); |
| 119556 | | - } |
| 119557 | | - return ret; |
| 119558 | | -} |
| 119559 | | -#else |
| 119560 | | -# define explainOneScan(u,v,w,x,y,z) 0 |
| 119561 | | -#endif /* SQLITE_OMIT_EXPLAIN */ |
| 119562 | | - |
| 119563 | | -#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 119564 | | -/* |
| 119565 | | -** Configure the VM passed as the first argument with an |
| 119566 | | -** sqlite3_stmt_scanstatus() entry corresponding to the scan used to |
| 119567 | | -** implement level pLvl. Argument pSrclist is a pointer to the FROM |
| 119568 | | -** clause that the scan reads data from. |
| 119569 | | -** |
| 119570 | | -** If argument addrExplain is not 0, it must be the address of an |
| 119571 | | -** OP_Explain instruction that describes the same loop. |
| 119572 | | -*/ |
| 119573 | | -static void addScanStatus( |
| 119574 | | - Vdbe *v, /* Vdbe to add scanstatus entry to */ |
| 119575 | | - SrcList *pSrclist, /* FROM clause pLvl reads data from */ |
| 119576 | | - WhereLevel *pLvl, /* Level to add scanstatus() entry for */ |
| 119577 | | - int addrExplain /* Address of OP_Explain (or 0) */ |
| 119578 | | -){ |
| 119579 | | - const char *zObj = 0; |
| 119580 | | - WhereLoop *pLoop = pLvl->pWLoop; |
| 119581 | | - if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){ |
| 119582 | | - zObj = pLoop->u.btree.pIndex->zName; |
| 119583 | | - }else{ |
| 119584 | | - zObj = pSrclist->a[pLvl->iFrom].zName; |
| 119585 | | - } |
| 119586 | | - sqlite3VdbeScanStatus( |
| 119587 | | - v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj |
| 119588 | | - ); |
| 119589 | | -} |
| 119590 | | -#else |
| 119591 | | -# define addScanStatus(a, b, c, d) ((void)d) |
| 119592 | | -#endif |
| 119593 | | - |
| 119594 | | -/* |
| 119595 | | -** If the most recently coded instruction is a constant range contraint |
| 119596 | | -** that originated from the LIKE optimization, then change the P3 to be |
| 119597 | | -** pLoop->iLikeRepCntr and set P5. |
| 119598 | | -** |
| 119599 | | -** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range |
| 119600 | | -** expression: "x>='ABC' AND x<'abd'". But this requires that the range |
| 119601 | | -** scan loop run twice, once for strings and a second time for BLOBs. |
| 119602 | | -** The OP_String opcodes on the second pass convert the upper and lower |
| 119603 | | -** bound string contants to blobs. This routine makes the necessary changes |
| 119604 | | -** to the OP_String opcodes for that to happen. |
| 119605 | | -*/ |
| 119606 | | -static void whereLikeOptimizationStringFixup( |
| 119607 | | - Vdbe *v, /* prepared statement under construction */ |
| 119608 | | - WhereLevel *pLevel, /* The loop that contains the LIKE operator */ |
| 119609 | | - WhereTerm *pTerm /* The upper or lower bound just coded */ |
| 119610 | | -){ |
| 119611 | | - if( pTerm->wtFlags & TERM_LIKEOPT ){ |
| 119612 | | - VdbeOp *pOp; |
| 119613 | | - assert( pLevel->iLikeRepCntr>0 ); |
| 119614 | | - pOp = sqlite3VdbeGetOp(v, -1); |
| 119615 | | - assert( pOp!=0 ); |
| 119616 | | - assert( pOp->opcode==OP_String8 |
| 119617 | | - || pTerm->pWC->pWInfo->pParse->db->mallocFailed ); |
| 119618 | | - pOp->p3 = pLevel->iLikeRepCntr; |
| 119619 | | - pOp->p5 = 1; |
| 119620 | | - } |
| 119621 | | -} |
| 119622 | | - |
| 119623 | | -/* |
| 119624 | | -** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 119625 | | -** implementation described by pWInfo. |
| 119626 | | -*/ |
| 119627 | | -static Bitmask codeOneLoopStart( |
| 119628 | | - WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 119629 | | - int iLevel, /* Which level of pWInfo->a[] should be coded */ |
| 119630 | | - Bitmask notReady /* Which tables are currently available */ |
| 119631 | | -){ |
| 119632 | | - int j, k; /* Loop counters */ |
| 119633 | | - int iCur; /* The VDBE cursor for the table */ |
| 119634 | | - int addrNxt; /* Where to jump to continue with the next IN case */ |
| 119635 | | - int omitTable; /* True if we use the index only */ |
| 119636 | | - int bRev; /* True if we need to scan in reverse order */ |
| 119637 | | - WhereLevel *pLevel; /* The where level to be coded */ |
| 119638 | | - WhereLoop *pLoop; /* The WhereLoop object being coded */ |
| 119639 | | - WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 119640 | | - WhereTerm *pTerm; /* A WHERE clause term */ |
| 119641 | | - Parse *pParse; /* Parsing context */ |
| 119642 | | - sqlite3 *db; /* Database connection */ |
| 119643 | | - Vdbe *v; /* The prepared stmt under constructions */ |
| 119644 | | - struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
| 119645 | | - int addrBrk; /* Jump here to break out of the loop */ |
| 119646 | | - int addrCont; /* Jump here to continue with next cycle */ |
| 119647 | | - int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ |
| 119648 | | - int iReleaseReg = 0; /* Temp register to free before returning */ |
| 119649 | | - |
| 119650 | | - pParse = pWInfo->pParse; |
| 119651 | | - v = pParse->pVdbe; |
| 119652 | | - pWC = &pWInfo->sWC; |
| 119653 | | - db = pParse->db; |
| 119654 | | - pLevel = &pWInfo->a[iLevel]; |
| 119655 | | - pLoop = pLevel->pWLoop; |
| 119656 | | - pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 119657 | | - iCur = pTabItem->iCursor; |
| 119658 | | - pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur); |
| 119659 | | - bRev = (pWInfo->revMask>>iLevel)&1; |
| 119660 | | - omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
| 119661 | | - && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; |
| 119662 | | - VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName)); |
| 119663 | | - |
| 119664 | | - /* Create labels for the "break" and "continue" instructions |
| 119665 | | - ** for the current loop. Jump to addrBrk to break out of a loop. |
| 119666 | | - ** Jump to cont to go immediately to the next iteration of the |
| 119667 | | - ** loop. |
| 119668 | | - ** |
| 119669 | | - ** When there is an IN operator, we also have a "addrNxt" label that |
| 119670 | | - ** means to continue with the next IN value combination. When |
| 119671 | | - ** there are no IN operators in the constraints, the "addrNxt" label |
| 119672 | | - ** is the same as "addrBrk". |
| 119673 | | - */ |
| 119674 | | - addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 119675 | | - addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); |
| 119676 | | - |
| 119677 | | - /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 119678 | | - ** initialize a memory cell that records if this table matches any |
| 119679 | | - ** row of the left table of the join. |
| 119680 | | - */ |
| 119681 | | - if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){ |
| 119682 | | - pLevel->iLeftJoin = ++pParse->nMem; |
| 119683 | | - sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); |
| 119684 | | - VdbeComment((v, "init LEFT JOIN no-match flag")); |
| 119685 | | - } |
| 119686 | | - |
| 119687 | | - /* Special case of a FROM clause subquery implemented as a co-routine */ |
| 119688 | | - if( pTabItem->viaCoroutine ){ |
| 119689 | | - int regYield = pTabItem->regReturn; |
| 119690 | | - sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub); |
| 119691 | | - pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk); |
| 119692 | | - VdbeCoverage(v); |
| 119693 | | - VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName)); |
| 119694 | | - pLevel->op = OP_Goto; |
| 119695 | | - }else |
| 119696 | | - |
| 119697 | | -#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 119698 | | - if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 119699 | | - /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
| 119700 | | - ** to access the data. |
| 119701 | | - */ |
| 119702 | | - int iReg; /* P3 Value for OP_VFilter */ |
| 119703 | | - int addrNotFound; |
| 119704 | | - int nConstraint = pLoop->nLTerm; |
| 119705 | | - |
| 119706 | | - sqlite3ExprCachePush(pParse); |
| 119707 | | - iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
| 119708 | | - addrNotFound = pLevel->addrBrk; |
| 119709 | | - for(j=0; j<nConstraint; j++){ |
| 119710 | | - int iTarget = iReg+j+2; |
| 119711 | | - pTerm = pLoop->aLTerm[j]; |
| 119712 | | - if( pTerm==0 ) continue; |
| 119713 | | - if( pTerm->eOperator & WO_IN ){ |
| 119714 | | - codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 119715 | | - addrNotFound = pLevel->addrNxt; |
| 119716 | | - }else{ |
| 119717 | | - sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); |
| 119718 | | - } |
| 119719 | | - } |
| 119720 | | - sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
| 119721 | | - sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
| 119722 | | - sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 119723 | | - pLoop->u.vtab.idxStr, |
| 119724 | | - pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); |
| 119725 | | - VdbeCoverage(v); |
| 119726 | | - pLoop->u.vtab.needFree = 0; |
| 119727 | | - for(j=0; j<nConstraint && j<16; j++){ |
| 119728 | | - if( (pLoop->u.vtab.omitMask>>j)&1 ){ |
| 119729 | | - disableTerm(pLevel, pLoop->aLTerm[j]); |
| 119730 | | - } |
| 119731 | | - } |
| 119732 | | - pLevel->op = OP_VNext; |
| 119733 | | - pLevel->p1 = iCur; |
| 119734 | | - pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 119735 | | - sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
| 119736 | | - sqlite3ExprCachePop(pParse); |
| 119737 | | - }else |
| 119738 | | -#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 119739 | | - |
| 119740 | | - if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 119741 | | - && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 119742 | | - ){ |
| 119743 | | - /* Case 2: We can directly reference a single row using an |
| 119744 | | - ** equality comparison against the ROWID field. Or |
| 119745 | | - ** we reference multiple rows using a "rowid IN (...)" |
| 119746 | | - ** construct. |
| 119747 | | - */ |
| 119748 | | - assert( pLoop->u.btree.nEq==1 ); |
| 119749 | | - pTerm = pLoop->aLTerm[0]; |
| 119750 | | - assert( pTerm!=0 ); |
| 119751 | | - assert( pTerm->pExpr!=0 ); |
| 119752 | | - assert( omitTable==0 ); |
| 119753 | | - testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 119754 | | - iReleaseReg = ++pParse->nMem; |
| 119755 | | - iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
| 119756 | | - if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg); |
| 119757 | | - addrNxt = pLevel->addrNxt; |
| 119758 | | - sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v); |
| 119759 | | - sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
| 119760 | | - VdbeCoverage(v); |
| 119761 | | - sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
| 119762 | | - sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 119763 | | - VdbeComment((v, "pk")); |
| 119764 | | - pLevel->op = OP_Noop; |
| 119765 | | - }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 119766 | | - && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 119767 | | - ){ |
| 119768 | | - /* Case 3: We have an inequality comparison against the ROWID field. |
| 119769 | | - */ |
| 119770 | | - int testOp = OP_Noop; |
| 119771 | | - int start; |
| 119772 | | - int memEndValue = 0; |
| 119773 | | - WhereTerm *pStart, *pEnd; |
| 119774 | | - |
| 119775 | | - assert( omitTable==0 ); |
| 119776 | | - j = 0; |
| 119777 | | - pStart = pEnd = 0; |
| 119778 | | - if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; |
| 119779 | | - if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; |
| 119780 | | - assert( pStart!=0 || pEnd!=0 ); |
| 119781 | | - if( bRev ){ |
| 119782 | | - pTerm = pStart; |
| 119783 | | - pStart = pEnd; |
| 119784 | | - pEnd = pTerm; |
| 119785 | | - } |
| 119786 | | - if( pStart ){ |
| 119787 | | - Expr *pX; /* The expression that defines the start bound */ |
| 119788 | | - int r1, rTemp; /* Registers for holding the start boundary */ |
| 119789 | | - |
| 119790 | | - /* The following constant maps TK_xx codes into corresponding |
| 119791 | | - ** seek opcodes. It depends on a particular ordering of TK_xx |
| 119792 | | - */ |
| 119793 | | - const u8 aMoveOp[] = { |
| 119794 | | - /* TK_GT */ OP_SeekGT, |
| 119795 | | - /* TK_LE */ OP_SeekLE, |
| 119796 | | - /* TK_LT */ OP_SeekLT, |
| 119797 | | - /* TK_GE */ OP_SeekGE |
| 119798 | | - }; |
| 119799 | | - assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ |
| 119800 | | - assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ |
| 119801 | | - assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ |
| 119802 | | - |
| 119803 | | - assert( (pStart->wtFlags & TERM_VNULL)==0 ); |
| 119804 | | - testcase( pStart->wtFlags & TERM_VIRTUAL ); |
| 119805 | | - pX = pStart->pExpr; |
| 119806 | | - assert( pX!=0 ); |
| 119807 | | - testcase( pStart->leftCursor!=iCur ); /* transitive constraints */ |
| 119808 | | - r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); |
| 119809 | | - sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); |
| 119810 | | - VdbeComment((v, "pk")); |
| 119811 | | - VdbeCoverageIf(v, pX->op==TK_GT); |
| 119812 | | - VdbeCoverageIf(v, pX->op==TK_LE); |
| 119813 | | - VdbeCoverageIf(v, pX->op==TK_LT); |
| 119814 | | - VdbeCoverageIf(v, pX->op==TK_GE); |
| 119815 | | - sqlite3ExprCacheAffinityChange(pParse, r1, 1); |
| 119816 | | - sqlite3ReleaseTempReg(pParse, rTemp); |
| 119817 | | - disableTerm(pLevel, pStart); |
| 119818 | | - }else{ |
| 119819 | | - sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); |
| 119820 | | - VdbeCoverageIf(v, bRev==0); |
| 119821 | | - VdbeCoverageIf(v, bRev!=0); |
| 119822 | | - } |
| 119823 | | - if( pEnd ){ |
| 119824 | | - Expr *pX; |
| 119825 | | - pX = pEnd->pExpr; |
| 119826 | | - assert( pX!=0 ); |
| 119827 | | - assert( (pEnd->wtFlags & TERM_VNULL)==0 ); |
| 119828 | | - testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */ |
| 119829 | | - testcase( pEnd->wtFlags & TERM_VIRTUAL ); |
| 119830 | | - memEndValue = ++pParse->nMem; |
| 119831 | | - sqlite3ExprCode(pParse, pX->pRight, memEndValue); |
| 119832 | | - if( pX->op==TK_LT || pX->op==TK_GT ){ |
| 119833 | | - testOp = bRev ? OP_Le : OP_Ge; |
| 119834 | | - }else{ |
| 119835 | | - testOp = bRev ? OP_Lt : OP_Gt; |
| 119836 | | - } |
| 119837 | | - disableTerm(pLevel, pEnd); |
| 119838 | | - } |
| 119839 | | - start = sqlite3VdbeCurrentAddr(v); |
| 119840 | | - pLevel->op = bRev ? OP_Prev : OP_Next; |
| 119841 | | - pLevel->p1 = iCur; |
| 119842 | | - pLevel->p2 = start; |
| 119843 | | - assert( pLevel->p5==0 ); |
| 119844 | | - if( testOp!=OP_Noop ){ |
| 119845 | | - iRowidReg = ++pParse->nMem; |
| 119846 | | - sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
| 119847 | | - sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 119848 | | - sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| 119849 | | - VdbeCoverageIf(v, testOp==OP_Le); |
| 119850 | | - VdbeCoverageIf(v, testOp==OP_Lt); |
| 119851 | | - VdbeCoverageIf(v, testOp==OP_Ge); |
| 119852 | | - VdbeCoverageIf(v, testOp==OP_Gt); |
| 119853 | | - sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
| 119854 | | - } |
| 119855 | | - }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
| 119856 | | - /* Case 4: A scan using an index. |
| 119857 | | - ** |
| 119858 | | - ** The WHERE clause may contain zero or more equality |
| 119859 | | - ** terms ("==" or "IN" operators) that refer to the N |
| 119860 | | - ** left-most columns of the index. It may also contain |
| 119861 | | - ** inequality constraints (>, <, >= or <=) on the indexed |
| 119862 | | - ** column that immediately follows the N equalities. Only |
| 119863 | | - ** the right-most column can be an inequality - the rest must |
| 119864 | | - ** use the "==" and "IN" operators. For example, if the |
| 119865 | | - ** index is on (x,y,z), then the following clauses are all |
| 119866 | | - ** optimized: |
| 119867 | | - ** |
| 119868 | | - ** x=5 |
| 119869 | | - ** x=5 AND y=10 |
| 119870 | | - ** x=5 AND y<10 |
| 119871 | | - ** x=5 AND y>5 AND y<10 |
| 119872 | | - ** x=5 AND y=5 AND z<=10 |
| 119873 | | - ** |
| 119874 | | - ** The z<10 term of the following cannot be used, only |
| 119875 | | - ** the x=5 term: |
| 119876 | | - ** |
| 119877 | | - ** x=5 AND z<10 |
| 119878 | | - ** |
| 119879 | | - ** N may be zero if there are inequality constraints. |
| 119880 | | - ** If there are no inequality constraints, then N is at |
| 119881 | | - ** least one. |
| 119882 | | - ** |
| 119883 | | - ** This case is also used when there are no WHERE clause |
| 119884 | | - ** constraints but an index is selected anyway, in order |
| 119885 | | - ** to force the output order to conform to an ORDER BY. |
| 119886 | | - */ |
| 119887 | | - static const u8 aStartOp[] = { |
| 119888 | | - 0, |
| 119889 | | - 0, |
| 119890 | | - OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 119891 | | - OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
| 119892 | | - OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */ |
| 119893 | | - OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */ |
| 119894 | | - OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */ |
| 119895 | | - OP_SeekLE /* 7: (start_constraints && startEq && bRev) */ |
| 119896 | | - }; |
| 119897 | | - static const u8 aEndOp[] = { |
| 119898 | | - OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */ |
| 119899 | | - OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */ |
| 119900 | | - OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */ |
| 119901 | | - OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */ |
| 119902 | | - }; |
| 119903 | | - u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
| 119904 | | - int regBase; /* Base register holding constraint values */ |
| 119905 | | - WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 119906 | | - WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 119907 | | - int startEq; /* True if range start uses ==, >= or <= */ |
| 119908 | | - int endEq; /* True if range end uses ==, >= or <= */ |
| 119909 | | - int start_constraints; /* Start of range is constrained */ |
| 119910 | | - int nConstraint; /* Number of constraint terms */ |
| 119911 | | - Index *pIdx; /* The index we will be using */ |
| 119912 | | - int iIdxCur; /* The VDBE cursor for the index */ |
| 119913 | | - int nExtraReg = 0; /* Number of extra registers needed */ |
| 119914 | | - int op; /* Instruction opcode */ |
| 119915 | | - char *zStartAff; /* Affinity for start of range constraint */ |
| 119916 | | - char cEndAff = 0; /* Affinity for end of range constraint */ |
| 119917 | | - u8 bSeekPastNull = 0; /* True to seek past initial nulls */ |
| 119918 | | - u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */ |
| 119919 | | - |
| 119920 | | - pIdx = pLoop->u.btree.pIndex; |
| 119921 | | - iIdxCur = pLevel->iIdxCur; |
| 119922 | | - assert( nEq>=pLoop->nSkip ); |
| 119923 | | - |
| 119924 | | - /* If this loop satisfies a sort order (pOrderBy) request that |
| 119925 | | - ** was passed to this function to implement a "SELECT min(x) ..." |
| 119926 | | - ** query, then the caller will only allow the loop to run for |
| 119927 | | - ** a single iteration. This means that the first row returned |
| 119928 | | - ** should not have a NULL value stored in 'x'. If column 'x' is |
| 119929 | | - ** the first one after the nEq equality constraints in the index, |
| 119930 | | - ** this requires some special handling. |
| 119931 | | - */ |
| 119932 | | - assert( pWInfo->pOrderBy==0 |
| 119933 | | - || pWInfo->pOrderBy->nExpr==1 |
| 119934 | | - || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 ); |
| 119935 | | - if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
| 119936 | | - && pWInfo->nOBSat>0 |
| 119937 | | - && (pIdx->nKeyCol>nEq) |
| 119938 | | - ){ |
| 119939 | | - assert( pLoop->nSkip==0 ); |
| 119940 | | - bSeekPastNull = 1; |
| 119941 | | - nExtraReg = 1; |
| 119942 | | - } |
| 119943 | | - |
| 119944 | | - /* Find any inequality constraint terms for the start and end |
| 119945 | | - ** of the range. |
| 119946 | | - */ |
| 119947 | | - j = nEq; |
| 119948 | | - if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
| 119949 | | - pRangeStart = pLoop->aLTerm[j++]; |
| 119950 | | - nExtraReg = 1; |
| 119951 | | - /* Like optimization range constraints always occur in pairs */ |
| 119952 | | - assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || |
| 119953 | | - (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 ); |
| 119954 | | - } |
| 119955 | | - if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
| 119956 | | - pRangeEnd = pLoop->aLTerm[j++]; |
| 119957 | | - nExtraReg = 1; |
| 119958 | | - if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){ |
| 119959 | | - assert( pRangeStart!=0 ); /* LIKE opt constraints */ |
| 119960 | | - assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */ |
| 119961 | | - pLevel->iLikeRepCntr = ++pParse->nMem; |
| 119962 | | - testcase( bRev ); |
| 119963 | | - testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC ); |
| 119964 | | - sqlite3VdbeAddOp2(v, OP_Integer, |
| 119965 | | - bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC), |
| 119966 | | - pLevel->iLikeRepCntr); |
| 119967 | | - VdbeComment((v, "LIKE loop counter")); |
| 119968 | | - pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v); |
| 119969 | | - } |
| 119970 | | - if( pRangeStart==0 |
| 119971 | | - && (j = pIdx->aiColumn[nEq])>=0 |
| 119972 | | - && pIdx->pTable->aCol[j].notNull==0 |
| 119973 | | - ){ |
| 119974 | | - bSeekPastNull = 1; |
| 119975 | | - } |
| 119976 | | - } |
| 119977 | | - assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 ); |
| 119978 | | - |
| 119979 | | - /* Generate code to evaluate all constraint terms using == or IN |
| 119980 | | - ** and store the values of those terms in an array of registers |
| 119981 | | - ** starting at regBase. |
| 119982 | | - */ |
| 119983 | | - regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); |
| 119984 | | - assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq ); |
| 119985 | | - if( zStartAff ) cEndAff = zStartAff[nEq]; |
| 119986 | | - addrNxt = pLevel->addrNxt; |
| 119987 | | - |
| 119988 | | - /* If we are doing a reverse order scan on an ascending index, or |
| 119989 | | - ** a forward order scan on a descending index, interchange the |
| 119990 | | - ** start and end terms (pRangeStart and pRangeEnd). |
| 119991 | | - */ |
| 119992 | | - if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) |
| 119993 | | - || (bRev && pIdx->nKeyCol==nEq) |
| 119994 | | - ){ |
| 119995 | | - SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
| 119996 | | - SWAP(u8, bSeekPastNull, bStopAtNull); |
| 119997 | | - } |
| 119998 | | - |
| 119999 | | - testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 ); |
| 120000 | | - testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 ); |
| 120001 | | - testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 ); |
| 120002 | | - testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 ); |
| 120003 | | - startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); |
| 120004 | | - endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); |
| 120005 | | - start_constraints = pRangeStart || nEq>0; |
| 120006 | | - |
| 120007 | | - /* Seek the index cursor to the start of the range. */ |
| 120008 | | - nConstraint = nEq; |
| 120009 | | - if( pRangeStart ){ |
| 120010 | | - Expr *pRight = pRangeStart->pExpr->pRight; |
| 120011 | | - sqlite3ExprCode(pParse, pRight, regBase+nEq); |
| 120012 | | - whereLikeOptimizationStringFixup(v, pLevel, pRangeStart); |
| 120013 | | - if( (pRangeStart->wtFlags & TERM_VNULL)==0 |
| 120014 | | - && sqlite3ExprCanBeNull(pRight) |
| 120015 | | - ){ |
| 120016 | | - sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 120017 | | - VdbeCoverage(v); |
| 120018 | | - } |
| 120019 | | - if( zStartAff ){ |
| 120020 | | - if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){ |
| 120021 | | - /* Since the comparison is to be performed with no conversions |
| 120022 | | - ** applied to the operands, set the affinity to apply to pRight to |
| 120023 | | - ** SQLITE_AFF_NONE. */ |
| 120024 | | - zStartAff[nEq] = SQLITE_AFF_NONE; |
| 120025 | | - } |
| 120026 | | - if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ |
| 120027 | | - zStartAff[nEq] = SQLITE_AFF_NONE; |
| 120028 | | - } |
| 120029 | | - } |
| 120030 | | - nConstraint++; |
| 120031 | | - testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); |
| 120032 | | - }else if( bSeekPastNull ){ |
| 120033 | | - sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 120034 | | - nConstraint++; |
| 120035 | | - startEq = 0; |
| 120036 | | - start_constraints = 1; |
| 120037 | | - } |
| 120038 | | - codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff); |
| 120039 | | - op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; |
| 120040 | | - assert( op!=0 ); |
| 120041 | | - sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
| 120042 | | - VdbeCoverage(v); |
| 120043 | | - VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind ); |
| 120044 | | - VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last ); |
| 120045 | | - VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT ); |
| 120046 | | - VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE ); |
| 120047 | | - VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE ); |
| 120048 | | - VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT ); |
| 120049 | | - |
| 120050 | | - /* Load the value for the inequality constraint at the end of the |
| 120051 | | - ** range (if any). |
| 120052 | | - */ |
| 120053 | | - nConstraint = nEq; |
| 120054 | | - if( pRangeEnd ){ |
| 120055 | | - Expr *pRight = pRangeEnd->pExpr->pRight; |
| 120056 | | - sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
| 120057 | | - sqlite3ExprCode(pParse, pRight, regBase+nEq); |
| 120058 | | - whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd); |
| 120059 | | - if( (pRangeEnd->wtFlags & TERM_VNULL)==0 |
| 120060 | | - && sqlite3ExprCanBeNull(pRight) |
| 120061 | | - ){ |
| 120062 | | - sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 120063 | | - VdbeCoverage(v); |
| 120064 | | - } |
| 120065 | | - if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE |
| 120066 | | - && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff) |
| 120067 | | - ){ |
| 120068 | | - codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff); |
| 120069 | | - } |
| 120070 | | - nConstraint++; |
| 120071 | | - testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); |
| 120072 | | - }else if( bStopAtNull ){ |
| 120073 | | - sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 120074 | | - endEq = 0; |
| 120075 | | - nConstraint++; |
| 120076 | | - } |
| 120077 | | - sqlite3DbFree(db, zStartAff); |
| 120078 | | - |
| 120079 | | - /* Top of the loop body */ |
| 120080 | | - pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 120081 | | - |
| 120082 | | - /* Check if the index cursor is past the end of the range. */ |
| 120083 | | - if( nConstraint ){ |
| 120084 | | - op = aEndOp[bRev*2 + endEq]; |
| 120085 | | - sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
| 120086 | | - testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT ); |
| 120087 | | - testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE ); |
| 120088 | | - testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT ); |
| 120089 | | - testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE ); |
| 120090 | | - } |
| 120091 | | - |
| 120092 | | - /* Seek the table cursor, if required */ |
| 120093 | | - disableTerm(pLevel, pRangeStart); |
| 120094 | | - disableTerm(pLevel, pRangeEnd); |
| 120095 | | - if( omitTable ){ |
| 120096 | | - /* pIdx is a covering index. No need to access the main table. */ |
| 120097 | | - }else if( HasRowid(pIdx->pTable) ){ |
| 120098 | | - iRowidReg = ++pParse->nMem; |
| 120099 | | - sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); |
| 120100 | | - sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 120101 | | - sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */ |
| 120102 | | - }else if( iCur!=iIdxCur ){ |
| 120103 | | - Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); |
| 120104 | | - iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol); |
| 120105 | | - for(j=0; j<pPk->nKeyCol; j++){ |
| 120106 | | - k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]); |
| 120107 | | - sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j); |
| 120108 | | - } |
| 120109 | | - sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont, |
| 120110 | | - iRowidReg, pPk->nKeyCol); VdbeCoverage(v); |
| 120111 | | - } |
| 120112 | | - |
| 120113 | | - /* Record the instruction used to terminate the loop. Disable |
| 120114 | | - ** WHERE clause terms made redundant by the index range scan. |
| 120115 | | - */ |
| 120116 | | - if( pLoop->wsFlags & WHERE_ONEROW ){ |
| 120117 | | - pLevel->op = OP_Noop; |
| 120118 | | - }else if( bRev ){ |
| 120119 | | - pLevel->op = OP_Prev; |
| 120120 | | - }else{ |
| 120121 | | - pLevel->op = OP_Next; |
| 120122 | | - } |
| 120123 | | - pLevel->p1 = iIdxCur; |
| 120124 | | - pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0; |
| 120125 | | - if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ |
| 120126 | | - pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 120127 | | - }else{ |
| 120128 | | - assert( pLevel->p5==0 ); |
| 120129 | | - } |
| 120130 | | - }else |
| 120131 | | - |
| 120132 | | -#ifndef SQLITE_OMIT_OR_OPTIMIZATION |
| 120133 | | - if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 120134 | | - /* Case 5: Two or more separately indexed terms connected by OR |
| 120135 | | - ** |
| 120136 | | - ** Example: |
| 120137 | | - ** |
| 120138 | | - ** CREATE TABLE t1(a,b,c,d); |
| 120139 | | - ** CREATE INDEX i1 ON t1(a); |
| 120140 | | - ** CREATE INDEX i2 ON t1(b); |
| 120141 | | - ** CREATE INDEX i3 ON t1(c); |
| 120142 | | - ** |
| 120143 | | - ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) |
| 120144 | | - ** |
| 120145 | | - ** In the example, there are three indexed terms connected by OR. |
| 120146 | | - ** The top of the loop looks like this: |
| 120147 | | - ** |
| 120148 | | - ** Null 1 # Zero the rowset in reg 1 |
| 120149 | | - ** |
| 120150 | | - ** Then, for each indexed term, the following. The arguments to |
| 120151 | | - ** RowSetTest are such that the rowid of the current row is inserted |
| 120152 | | - ** into the RowSet. If it is already present, control skips the |
| 120153 | | - ** Gosub opcode and jumps straight to the code generated by WhereEnd(). |
| 120154 | | - ** |
| 120155 | | - ** sqlite3WhereBegin(<term>) |
| 120156 | | - ** RowSetTest # Insert rowid into rowset |
| 120157 | | - ** Gosub 2 A |
| 120158 | | - ** sqlite3WhereEnd() |
| 120159 | | - ** |
| 120160 | | - ** Following the above, code to terminate the loop. Label A, the target |
| 120161 | | - ** of the Gosub above, jumps to the instruction right after the Goto. |
| 120162 | | - ** |
| 120163 | | - ** Null 1 # Zero the rowset in reg 1 |
| 120164 | | - ** Goto B # The loop is finished. |
| 120165 | | - ** |
| 120166 | | - ** A: <loop body> # Return data, whatever. |
| 120167 | | - ** |
| 120168 | | - ** Return 2 # Jump back to the Gosub |
| 120169 | | - ** |
| 120170 | | - ** B: <after the loop> |
| 120171 | | - ** |
| 120172 | | - ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then |
| 120173 | | - ** use an ephemeral index instead of a RowSet to record the primary |
| 120174 | | - ** keys of the rows we have already seen. |
| 120175 | | - ** |
| 120176 | | - */ |
| 120177 | | - WhereClause *pOrWc; /* The OR-clause broken out into subterms */ |
| 120178 | | - SrcList *pOrTab; /* Shortened table list or OR-clause generation */ |
| 120179 | | - Index *pCov = 0; /* Potential covering index (or NULL) */ |
| 120180 | | - int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ |
| 120181 | | - |
| 120182 | | - int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ |
| 120183 | | - int regRowset = 0; /* Register for RowSet object */ |
| 120184 | | - int regRowid = 0; /* Register holding rowid */ |
| 120185 | | - int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ |
| 120186 | | - int iRetInit; /* Address of regReturn init */ |
| 120187 | | - int untestedTerms = 0; /* Some terms not completely tested */ |
| 120188 | | - int ii; /* Loop counter */ |
| 120189 | | - u16 wctrlFlags; /* Flags for sub-WHERE clause */ |
| 120190 | | - Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
| 120191 | | - Table *pTab = pTabItem->pTab; |
| 120192 | | - |
| 120193 | | - pTerm = pLoop->aLTerm[0]; |
| 120194 | | - assert( pTerm!=0 ); |
| 120195 | | - assert( pTerm->eOperator & WO_OR ); |
| 120196 | | - assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 120197 | | - pOrWc = &pTerm->u.pOrInfo->wc; |
| 120198 | | - pLevel->op = OP_Return; |
| 120199 | | - pLevel->p1 = regReturn; |
| 120200 | | - |
| 120201 | | - /* Set up a new SrcList in pOrTab containing the table being scanned |
| 120202 | | - ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. |
| 120203 | | - ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). |
| 120204 | | - */ |
| 120205 | | - if( pWInfo->nLevel>1 ){ |
| 120206 | | - int nNotReady; /* The number of notReady tables */ |
| 120207 | | - struct SrcList_item *origSrc; /* Original list of tables */ |
| 120208 | | - nNotReady = pWInfo->nLevel - iLevel - 1; |
| 120209 | | - pOrTab = sqlite3StackAllocRaw(db, |
| 120210 | | - sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); |
| 120211 | | - if( pOrTab==0 ) return notReady; |
| 120212 | | - pOrTab->nAlloc = (u8)(nNotReady + 1); |
| 120213 | | - pOrTab->nSrc = pOrTab->nAlloc; |
| 120214 | | - memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| 120215 | | - origSrc = pWInfo->pTabList->a; |
| 120216 | | - for(k=1; k<=nNotReady; k++){ |
| 120217 | | - memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); |
| 120218 | | - } |
| 120219 | | - }else{ |
| 120220 | | - pOrTab = pWInfo->pTabList; |
| 120221 | | - } |
| 120222 | | - |
| 120223 | | - /* Initialize the rowset register to contain NULL. An SQL NULL is |
| 120224 | | - ** equivalent to an empty rowset. Or, create an ephemeral index |
| 120225 | | - ** capable of holding primary keys in the case of a WITHOUT ROWID. |
| 120226 | | - ** |
| 120227 | | - ** Also initialize regReturn to contain the address of the instruction |
| 120228 | | - ** immediately following the OP_Return at the bottom of the loop. This |
| 120229 | | - ** is required in a few obscure LEFT JOIN cases where control jumps |
| 120230 | | - ** over the top of the loop into the body of it. In this case the |
| 120231 | | - ** correct response for the end-of-loop code (the OP_Return) is to |
| 120232 | | - ** fall through to the next instruction, just as an OP_Next does if |
| 120233 | | - ** called on an uninitialized cursor. |
| 120234 | | - */ |
| 120235 | | - if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 120236 | | - if( HasRowid(pTab) ){ |
| 120237 | | - regRowset = ++pParse->nMem; |
| 120238 | | - sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 120239 | | - }else{ |
| 120240 | | - Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 120241 | | - regRowset = pParse->nTab++; |
| 120242 | | - sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol); |
| 120243 | | - sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
| 120244 | | - } |
| 120245 | | - regRowid = ++pParse->nMem; |
| 120246 | | - } |
| 120247 | | - iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 120248 | | - |
| 120249 | | - /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 120250 | | - ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 120251 | | - ** That way, terms in y that are factored into the disjunction will |
| 120252 | | - ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
| 120253 | | - ** |
| 120254 | | - ** Actually, each subexpression is converted to "xN AND w" where w is |
| 120255 | | - ** the "interesting" terms of z - terms that did not originate in the |
| 120256 | | - ** ON or USING clause of a LEFT JOIN, and terms that are usable as |
| 120257 | | - ** indices. |
| 120258 | | - ** |
| 120259 | | - ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 120260 | | - ** is not contained in the ON clause of a LEFT JOIN. |
| 120261 | | - ** See ticket http://www.sqlite.org/src/info/f2369304e4 |
| 120262 | | - */ |
| 120263 | | - if( pWC->nTerm>1 ){ |
| 120264 | | - int iTerm; |
| 120265 | | - for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 120266 | | - Expr *pExpr = pWC->a[iTerm].pExpr; |
| 120267 | | - if( &pWC->a[iTerm] == pTerm ) continue; |
| 120268 | | - if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; |
| 120269 | | - if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue; |
| 120270 | | - if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
| 120271 | | - testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO ); |
| 120272 | | - pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 120273 | | - pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr); |
| 120274 | | - } |
| 120275 | | - if( pAndExpr ){ |
| 120276 | | - pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0); |
| 120277 | | - } |
| 120278 | | - } |
| 120279 | | - |
| 120280 | | - /* Run a separate WHERE clause for each term of the OR clause. After |
| 120281 | | - ** eliminating duplicates from other WHERE clauses, the action for each |
| 120282 | | - ** sub-WHERE clause is to to invoke the main loop body as a subroutine. |
| 120283 | | - */ |
| 120284 | | - wctrlFlags = WHERE_OMIT_OPEN_CLOSE |
| 120285 | | - | WHERE_FORCE_TABLE |
| 120286 | | - | WHERE_ONETABLE_ONLY |
| 120287 | | - | WHERE_NO_AUTOINDEX; |
| 120288 | | - for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 120289 | | - WhereTerm *pOrTerm = &pOrWc->a[ii]; |
| 120290 | | - if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
| 120291 | | - WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
| 120292 | | - Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */ |
| 120293 | | - int j1 = 0; /* Address of jump operation */ |
| 120294 | | - if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ |
| 120295 | | - pAndExpr->pLeft = pOrExpr; |
| 120296 | | - pOrExpr = pAndExpr; |
| 120297 | | - } |
| 120298 | | - /* Loop through table entries that match term pOrTerm. */ |
| 120299 | | - WHERETRACE(0xffff, ("Subplan for OR-clause:\n")); |
| 120300 | | - pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 120301 | | - wctrlFlags, iCovCur); |
| 120302 | | - assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
| 120303 | | - if( pSubWInfo ){ |
| 120304 | | - WhereLoop *pSubLoop; |
| 120305 | | - int addrExplain = explainOneScan( |
| 120306 | | - pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
| 120307 | | - ); |
| 120308 | | - addScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain); |
| 120309 | | - |
| 120310 | | - /* This is the sub-WHERE clause body. First skip over |
| 120311 | | - ** duplicate rows from prior sub-WHERE clauses, and record the |
| 120312 | | - ** rowid (or PRIMARY KEY) for the current row so that the same |
| 120313 | | - ** row will be skipped in subsequent sub-WHERE clauses. |
| 120314 | | - */ |
| 120315 | | - if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 120316 | | - int r; |
| 120317 | | - int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 120318 | | - if( HasRowid(pTab) ){ |
| 120319 | | - r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0); |
| 120320 | | - j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet); |
| 120321 | | - VdbeCoverage(v); |
| 120322 | | - }else{ |
| 120323 | | - Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 120324 | | - int nPk = pPk->nKeyCol; |
| 120325 | | - int iPk; |
| 120326 | | - |
| 120327 | | - /* Read the PK into an array of temp registers. */ |
| 120328 | | - r = sqlite3GetTempRange(pParse, nPk); |
| 120329 | | - for(iPk=0; iPk<nPk; iPk++){ |
| 120330 | | - int iCol = pPk->aiColumn[iPk]; |
| 120331 | | - sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0); |
| 120332 | | - } |
| 120333 | | - |
| 120334 | | - /* Check if the temp table already contains this key. If so, |
| 120335 | | - ** the row has already been included in the result set and |
| 120336 | | - ** can be ignored (by jumping past the Gosub below). Otherwise, |
| 120337 | | - ** insert the key into the temp table and proceed with processing |
| 120338 | | - ** the row. |
| 120339 | | - ** |
| 120340 | | - ** Use some of the same optimizations as OP_RowSetTest: If iSet |
| 120341 | | - ** is zero, assume that the key cannot already be present in |
| 120342 | | - ** the temp table. And if iSet is -1, assume that there is no |
| 120343 | | - ** need to insert the key into the temp table, as it will never |
| 120344 | | - ** be tested for. */ |
| 120345 | | - if( iSet ){ |
| 120346 | | - j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk); |
| 120347 | | - VdbeCoverage(v); |
| 120348 | | - } |
| 120349 | | - if( iSet>=0 ){ |
| 120350 | | - sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid); |
| 120351 | | - sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0); |
| 120352 | | - if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 120353 | | - } |
| 120354 | | - |
| 120355 | | - /* Release the array of temp registers */ |
| 120356 | | - sqlite3ReleaseTempRange(pParse, r, nPk); |
| 120357 | | - } |
| 120358 | | - } |
| 120359 | | - |
| 120360 | | - /* Invoke the main loop body as a subroutine */ |
| 120361 | | - sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); |
| 120362 | | - |
| 120363 | | - /* Jump here (skipping the main loop body subroutine) if the |
| 120364 | | - ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */ |
| 120365 | | - if( j1 ) sqlite3VdbeJumpHere(v, j1); |
| 120366 | | - |
| 120367 | | - /* The pSubWInfo->untestedTerms flag means that this OR term |
| 120368 | | - ** contained one or more AND term from a notReady table. The |
| 120369 | | - ** terms from the notReady table could not be tested and will |
| 120370 | | - ** need to be tested later. |
| 120371 | | - */ |
| 120372 | | - if( pSubWInfo->untestedTerms ) untestedTerms = 1; |
| 120373 | | - |
| 120374 | | - /* If all of the OR-connected terms are optimized using the same |
| 120375 | | - ** index, and the index is opened using the same cursor number |
| 120376 | | - ** by each call to sqlite3WhereBegin() made by this loop, it may |
| 120377 | | - ** be possible to use that index as a covering index. |
| 120378 | | - ** |
| 120379 | | - ** If the call to sqlite3WhereBegin() above resulted in a scan that |
| 120380 | | - ** uses an index, and this is either the first OR-connected term |
| 120381 | | - ** processed or the index is the same as that used by all previous |
| 120382 | | - ** terms, set pCov to the candidate covering index. Otherwise, set |
| 120383 | | - ** pCov to NULL to indicate that no candidate covering index will |
| 120384 | | - ** be available. |
| 120385 | | - */ |
| 120386 | | - pSubLoop = pSubWInfo->a[0].pWLoop; |
| 120387 | | - assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
| 120388 | | - if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
| 120389 | | - && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
| 120390 | | - && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex)) |
| 120391 | | - ){ |
| 120392 | | - assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
| 120393 | | - pCov = pSubLoop->u.btree.pIndex; |
| 120394 | | - wctrlFlags |= WHERE_REOPEN_IDX; |
| 120395 | | - }else{ |
| 120396 | | - pCov = 0; |
| 120397 | | - } |
| 120398 | | - |
| 120399 | | - /* Finish the loop through table entries that match term pOrTerm. */ |
| 120400 | | - sqlite3WhereEnd(pSubWInfo); |
| 120401 | | - } |
| 120402 | | - } |
| 120403 | | - } |
| 120404 | | - pLevel->u.pCovidx = pCov; |
| 120405 | | - if( pCov ) pLevel->iIdxCur = iCovCur; |
| 120406 | | - if( pAndExpr ){ |
| 120407 | | - pAndExpr->pLeft = 0; |
| 120408 | | - sqlite3ExprDelete(db, pAndExpr); |
| 120409 | | - } |
| 120410 | | - sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
| 120411 | | - sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); |
| 120412 | | - sqlite3VdbeResolveLabel(v, iLoopBody); |
| 120413 | | - |
| 120414 | | - if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab); |
| 120415 | | - if( !untestedTerms ) disableTerm(pLevel, pTerm); |
| 120416 | | - }else |
| 120417 | | -#endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 120418 | | - |
| 120419 | | - { |
| 120420 | | - /* Case 6: There is no usable index. We must do a complete |
| 120421 | | - ** scan of the entire table. |
| 120422 | | - */ |
| 120423 | | - static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 120424 | | - static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 120425 | | - assert( bRev==0 || bRev==1 ); |
| 120426 | | - if( pTabItem->isRecursive ){ |
| 120427 | | - /* Tables marked isRecursive have only a single row that is stored in |
| 120428 | | - ** a pseudo-cursor. No need to Rewind or Next such cursors. */ |
| 120429 | | - pLevel->op = OP_Noop; |
| 120430 | | - }else{ |
| 120431 | | - pLevel->op = aStep[bRev]; |
| 120432 | | - pLevel->p1 = iCur; |
| 120433 | | - pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
| 120434 | | - VdbeCoverageIf(v, bRev==0); |
| 120435 | | - VdbeCoverageIf(v, bRev!=0); |
| 120436 | | - pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 120437 | | - } |
| 120438 | | - } |
| 120439 | | - |
| 120440 | | -#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 120441 | | - pLevel->addrVisit = sqlite3VdbeCurrentAddr(v); |
| 120442 | | -#endif |
| 120443 | | - |
| 120444 | | - /* Insert code to test every subexpression that can be completely |
| 120445 | | - ** computed using the current set of tables. |
| 120446 | | - */ |
| 120447 | | - for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 120448 | | - Expr *pE; |
| 120449 | | - int skipLikeAddr = 0; |
| 120450 | | - testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 120451 | | - testcase( pTerm->wtFlags & TERM_CODED ); |
| 120452 | | - if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 120453 | | - if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
| 120454 | | - testcase( pWInfo->untestedTerms==0 |
| 120455 | | - && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); |
| 120456 | | - pWInfo->untestedTerms = 1; |
| 120457 | | - continue; |
| 120458 | | - } |
| 120459 | | - pE = pTerm->pExpr; |
| 120460 | | - assert( pE!=0 ); |
| 120461 | | - if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ |
| 120462 | | - continue; |
| 120463 | | - } |
| 120464 | | - if( pTerm->wtFlags & TERM_LIKECOND ){ |
| 120465 | | - assert( pLevel->iLikeRepCntr>0 ); |
| 120466 | | - skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr); |
| 120467 | | - VdbeCoverage(v); |
| 120468 | | - } |
| 120469 | | - sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); |
| 120470 | | - if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr); |
| 120471 | | - pTerm->wtFlags |= TERM_CODED; |
| 120472 | | - } |
| 120473 | | - |
| 120474 | | - /* Insert code to test for implied constraints based on transitivity |
| 120475 | | - ** of the "==" operator. |
| 120476 | | - ** |
| 120477 | | - ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" |
| 120478 | | - ** and we are coding the t1 loop and the t2 loop has not yet coded, |
| 120479 | | - ** then we cannot use the "t1.a=t2.b" constraint, but we can code |
| 120480 | | - ** the implied "t1.a=123" constraint. |
| 120481 | | - */ |
| 120482 | | - for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 120483 | | - Expr *pE, *pEAlt; |
| 120484 | | - WhereTerm *pAlt; |
| 120485 | | - if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 120486 | | - if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue; |
| 120487 | | - if( (pTerm->eOperator & WO_EQUIV)==0 ) continue; |
| 120488 | | - if( pTerm->leftCursor!=iCur ) continue; |
| 120489 | | - if( pLevel->iLeftJoin ) continue; |
| 120490 | | - pE = pTerm->pExpr; |
| 120491 | | - assert( !ExprHasProperty(pE, EP_FromJoin) ); |
| 120492 | | - assert( (pTerm->prereqRight & pLevel->notReady)!=0 ); |
| 120493 | | - pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, |
| 120494 | | - WO_EQ|WO_IN|WO_IS, 0); |
| 120495 | | - if( pAlt==0 ) continue; |
| 120496 | | - if( pAlt->wtFlags & (TERM_CODED) ) continue; |
| 120497 | | - testcase( pAlt->eOperator & WO_EQ ); |
| 120498 | | - testcase( pAlt->eOperator & WO_IS ); |
| 120499 | | - testcase( pAlt->eOperator & WO_IN ); |
| 120500 | | - VdbeModuleComment((v, "begin transitive constraint")); |
| 120501 | | - pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt)); |
| 120502 | | - if( pEAlt ){ |
| 120503 | | - *pEAlt = *pAlt->pExpr; |
| 120504 | | - pEAlt->pLeft = pE->pLeft; |
| 120505 | | - sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL); |
| 120506 | | - sqlite3StackFree(db, pEAlt); |
| 120507 | | - } |
| 120508 | | - } |
| 120509 | | - |
| 120510 | | - /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 120511 | | - ** at least one row of the right table has matched the left table. |
| 120512 | | - */ |
| 120513 | | - if( pLevel->iLeftJoin ){ |
| 120514 | | - pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); |
| 120515 | | - sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); |
| 120516 | | - VdbeComment((v, "record LEFT JOIN hit")); |
| 120517 | | - sqlite3ExprCacheClear(pParse); |
| 120518 | | - for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){ |
| 120519 | | - testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 120520 | | - testcase( pTerm->wtFlags & TERM_CODED ); |
| 120521 | | - if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 120522 | | - if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
| 120523 | | - assert( pWInfo->untestedTerms ); |
| 120524 | | - continue; |
| 120525 | | - } |
| 120526 | | - assert( pTerm->pExpr ); |
| 120527 | | - sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); |
| 120528 | | - pTerm->wtFlags |= TERM_CODED; |
| 120529 | | - } |
| 120530 | | - } |
| 120531 | | - |
| 120532 | | - return pLevel->notReady; |
| 120533 | | -} |
| 120534 | 121353 | |
| 120535 | 121354 | #ifdef WHERETRACE_ENABLED |
| 120536 | 121355 | /* |
| 120537 | 121356 | ** Print the content of a WhereTerm object |
| 120538 | 121357 | */ |
| | @@ -120695,11 +121514,11 @@ |
| 120695 | 121514 | WhereLevel *pLevel = &pWInfo->a[i]; |
| 120696 | 121515 | if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){ |
| 120697 | 121516 | sqlite3DbFree(db, pLevel->u.in.aInLoop); |
| 120698 | 121517 | } |
| 120699 | 121518 | } |
| 120700 | | - whereClauseClear(&pWInfo->sWC); |
| 121519 | + sqlite3WhereClauseClear(&pWInfo->sWC); |
| 120701 | 121520 | while( pWInfo->pLoops ){ |
| 120702 | 121521 | WhereLoop *p = pWInfo->pLoops; |
| 120703 | 121522 | pWInfo->pLoops = p->pNextLoop; |
| 120704 | 121523 | whereLoopDelete(db, p); |
| 120705 | 121524 | } |
| | @@ -121647,14 +122466,36 @@ |
| 121647 | 122466 | |
| 121648 | 122467 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 121649 | 122468 | /* |
| 121650 | 122469 | ** Add all WhereLoop objects for a table of the join identified by |
| 121651 | 122470 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
| 122471 | +** |
| 122472 | +** If there are no LEFT or CROSS JOIN joins in the query, both mExtra and |
| 122473 | +** mUnusable are set to 0. Otherwise, mExtra is a mask of all FROM clause |
| 122474 | +** entries that occur before the virtual table in the FROM clause and are |
| 122475 | +** separated from it by at least one LEFT or CROSS JOIN. Similarly, the |
| 122476 | +** mUnusable mask contains all FROM clause entries that occur after the |
| 122477 | +** virtual table and are separated from it by at least one LEFT or |
| 122478 | +** CROSS JOIN. |
| 122479 | +** |
| 122480 | +** For example, if the query were: |
| 122481 | +** |
| 122482 | +** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6; |
| 122483 | +** |
| 122484 | +** then mExtra corresponds to (t1, t2) and mUnusable to (t5, t6). |
| 122485 | +** |
| 122486 | +** All the tables in mExtra must be scanned before the current virtual |
| 122487 | +** table. So any terms for which all prerequisites are satisfied by |
| 122488 | +** mExtra may be specified as "usable" in all calls to xBestIndex. |
| 122489 | +** Conversely, all tables in mUnusable must be scanned after the current |
| 122490 | +** virtual table, so any terms for which the prerequisites overlap with |
| 122491 | +** mUnusable should always be configured as "not-usable" for xBestIndex. |
| 121652 | 122492 | */ |
| 121653 | 122493 | static int whereLoopAddVirtual( |
| 121654 | 122494 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
| 121655 | | - Bitmask mExtra |
| 122495 | + Bitmask mExtra, /* Tables that must be scanned before this one */ |
| 122496 | + Bitmask mUnusable /* Tables that must be scanned after this one */ |
| 121656 | 122497 | ){ |
| 121657 | 122498 | WhereInfo *pWInfo; /* WHERE analysis context */ |
| 121658 | 122499 | Parse *pParse; /* The parsing context */ |
| 121659 | 122500 | WhereClause *pWC; /* The WHERE clause */ |
| 121660 | 122501 | struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| | @@ -121671,19 +122512,20 @@ |
| 121671 | 122512 | int seenVar = 0; /* True if a non-constant constraint is seen */ |
| 121672 | 122513 | int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ |
| 121673 | 122514 | WhereLoop *pNew; |
| 121674 | 122515 | int rc = SQLITE_OK; |
| 121675 | 122516 | |
| 122517 | + assert( (mExtra & mUnusable)==0 ); |
| 121676 | 122518 | pWInfo = pBuilder->pWInfo; |
| 121677 | 122519 | pParse = pWInfo->pParse; |
| 121678 | 122520 | db = pParse->db; |
| 121679 | 122521 | pWC = pBuilder->pWC; |
| 121680 | 122522 | pNew = pBuilder->pNew; |
| 121681 | 122523 | pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
| 121682 | 122524 | pTab = pSrc->pTab; |
| 121683 | 122525 | assert( IsVirtual(pTab) ); |
| 121684 | | - pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy); |
| 122526 | + pIdxInfo = allocateIndexInfo(pParse, pWC, mUnusable, pSrc,pBuilder->pOrderBy); |
| 121685 | 122527 | if( pIdxInfo==0 ) return SQLITE_NOMEM; |
| 121686 | 122528 | pNew->prereq = 0; |
| 121687 | 122529 | pNew->rSetup = 0; |
| 121688 | 122530 | pNew->wsFlags = WHERE_VIRTUALTABLE; |
| 121689 | 122531 | pNew->nLTerm = 0; |
| | @@ -121709,19 +122551,19 @@ |
| 121709 | 122551 | case 0: /* Constants without IN operator */ |
| 121710 | 122552 | pIdxCons->usable = 0; |
| 121711 | 122553 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 121712 | 122554 | seenIn = 1; |
| 121713 | 122555 | } |
| 121714 | | - if( pTerm->prereqRight!=0 ){ |
| 122556 | + if( (pTerm->prereqRight & ~mExtra)!=0 ){ |
| 121715 | 122557 | seenVar = 1; |
| 121716 | 122558 | }else if( (pTerm->eOperator & WO_IN)==0 ){ |
| 121717 | 122559 | pIdxCons->usable = 1; |
| 121718 | 122560 | } |
| 121719 | 122561 | break; |
| 121720 | 122562 | case 1: /* Constants with IN operators */ |
| 121721 | 122563 | assert( seenIn ); |
| 121722 | | - pIdxCons->usable = (pTerm->prereqRight==0); |
| 122564 | + pIdxCons->usable = (pTerm->prereqRight & ~mExtra)==0; |
| 121723 | 122565 | break; |
| 121724 | 122566 | case 2: /* Variables without IN */ |
| 121725 | 122567 | assert( seenVar ); |
| 121726 | 122568 | pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; |
| 121727 | 122569 | break; |
| | @@ -121816,11 +122658,15 @@ |
| 121816 | 122658 | |
| 121817 | 122659 | /* |
| 121818 | 122660 | ** Add WhereLoop entries to handle OR terms. This works for either |
| 121819 | 122661 | ** btrees or virtual tables. |
| 121820 | 122662 | */ |
| 121821 | | -static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){ |
| 122663 | +static int whereLoopAddOr( |
| 122664 | + WhereLoopBuilder *pBuilder, |
| 122665 | + Bitmask mExtra, |
| 122666 | + Bitmask mUnusable |
| 122667 | +){ |
| 121822 | 122668 | WhereInfo *pWInfo = pBuilder->pWInfo; |
| 121823 | 122669 | WhereClause *pWC; |
| 121824 | 122670 | WhereLoop *pNew; |
| 121825 | 122671 | WhereTerm *pTerm, *pWCEnd; |
| 121826 | 122672 | int rc = SQLITE_OK; |
| | @@ -121875,18 +122721,18 @@ |
| 121875 | 122721 | } |
| 121876 | 122722 | } |
| 121877 | 122723 | #endif |
| 121878 | 122724 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 121879 | 122725 | if( IsVirtual(pItem->pTab) ){ |
| 121880 | | - rc = whereLoopAddVirtual(&sSubBuild, mExtra); |
| 122726 | + rc = whereLoopAddVirtual(&sSubBuild, mExtra, mUnusable); |
| 121881 | 122727 | }else |
| 121882 | 122728 | #endif |
| 121883 | 122729 | { |
| 121884 | 122730 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 121885 | 122731 | } |
| 121886 | 122732 | if( rc==SQLITE_OK ){ |
| 121887 | | - rc = whereLoopAddOr(&sSubBuild, mExtra); |
| 122733 | + rc = whereLoopAddOr(&sSubBuild, mExtra, mUnusable); |
| 121888 | 122734 | } |
| 121889 | 122735 | assert( rc==SQLITE_OK || sCur.n==0 ); |
| 121890 | 122736 | if( sCur.n==0 ){ |
| 121891 | 122737 | sSum.n = 0; |
| 121892 | 122738 | break; |
| | @@ -121944,37 +122790,47 @@ |
| 121944 | 122790 | Bitmask mExtra = 0; |
| 121945 | 122791 | Bitmask mPrior = 0; |
| 121946 | 122792 | int iTab; |
| 121947 | 122793 | SrcList *pTabList = pWInfo->pTabList; |
| 121948 | 122794 | struct SrcList_item *pItem; |
| 122795 | + struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel]; |
| 121949 | 122796 | sqlite3 *db = pWInfo->pParse->db; |
| 121950 | | - int nTabList = pWInfo->nLevel; |
| 121951 | 122797 | int rc = SQLITE_OK; |
| 121952 | | - u8 priorJoinType = 0; |
| 121953 | 122798 | WhereLoop *pNew; |
| 122799 | + u8 priorJointype = 0; |
| 121954 | 122800 | |
| 121955 | 122801 | /* Loop over the tables in the join, from left to right */ |
| 121956 | 122802 | pNew = pBuilder->pNew; |
| 121957 | 122803 | whereLoopInit(pNew); |
| 121958 | | - for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){ |
| 122804 | + for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){ |
| 122805 | + Bitmask mUnusable = 0; |
| 121959 | 122806 | pNew->iTab = iTab; |
| 121960 | | - pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor); |
| 121961 | | - if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){ |
| 122807 | + pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor); |
| 122808 | + if( ((pItem->jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){ |
| 122809 | + /* This condition is true when pItem is the FROM clause term on the |
| 122810 | + ** right-hand-side of a LEFT or CROSS JOIN. */ |
| 121962 | 122811 | mExtra = mPrior; |
| 121963 | 122812 | } |
| 121964 | | - priorJoinType = pItem->jointype; |
| 122813 | + priorJointype = pItem->jointype; |
| 121965 | 122814 | if( IsVirtual(pItem->pTab) ){ |
| 121966 | | - rc = whereLoopAddVirtual(pBuilder, mExtra); |
| 122815 | + struct SrcList_item *p; |
| 122816 | + for(p=&pItem[1]; p<pEnd; p++){ |
| 122817 | + if( mUnusable || (p->jointype & (JT_LEFT|JT_CROSS)) ){ |
| 122818 | + mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor); |
| 122819 | + } |
| 122820 | + } |
| 122821 | + rc = whereLoopAddVirtual(pBuilder, mExtra, mUnusable); |
| 121967 | 122822 | }else{ |
| 121968 | 122823 | rc = whereLoopAddBtree(pBuilder, mExtra); |
| 121969 | 122824 | } |
| 121970 | 122825 | if( rc==SQLITE_OK ){ |
| 121971 | | - rc = whereLoopAddOr(pBuilder, mExtra); |
| 122826 | + rc = whereLoopAddOr(pBuilder, mExtra, mUnusable); |
| 121972 | 122827 | } |
| 121973 | 122828 | mPrior |= pNew->maskSelf; |
| 121974 | 122829 | if( rc || db->mallocFailed ) break; |
| 121975 | 122830 | } |
| 122831 | + |
| 121976 | 122832 | whereLoopClear(db, pNew); |
| 121977 | 122833 | return rc; |
| 121978 | 122834 | } |
| 121979 | 122835 | |
| 121980 | 122836 | /* |
| | @@ -122076,11 +122932,11 @@ |
| 122076 | 122932 | for(i=0; i<nOrderBy; i++){ |
| 122077 | 122933 | if( MASKBIT(i) & obSat ) continue; |
| 122078 | 122934 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 122079 | 122935 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 122080 | 122936 | if( pOBExpr->iTable!=iCur ) continue; |
| 122081 | | - pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, |
| 122937 | + pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, |
| 122082 | 122938 | ~ready, WO_EQ|WO_ISNULL|WO_IS, 0); |
| 122083 | 122939 | if( pTerm==0 ) continue; |
| 122084 | 122940 | if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){ |
| 122085 | 122941 | const char *z1, *z2; |
| 122086 | 122942 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| | @@ -122213,11 +123069,11 @@ |
| 122213 | 123069 | for(i=0; i<nOrderBy; i++){ |
| 122214 | 123070 | Expr *p; |
| 122215 | 123071 | Bitmask mTerm; |
| 122216 | 123072 | if( MASKBIT(i) & obSat ) continue; |
| 122217 | 123073 | p = pOrderBy->a[i].pExpr; |
| 122218 | | - mTerm = exprTableUsage(&pWInfo->sMaskSet,p); |
| 123074 | + mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p); |
| 122219 | 123075 | if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue; |
| 122220 | 123076 | if( (mTerm&~orderDistinctMask)==0 ){ |
| 122221 | 123077 | obSat |= MASKBIT(i); |
| 122222 | 123078 | } |
| 122223 | 123079 | } |
| | @@ -122686,17 +123542,17 @@ |
| 122686 | 123542 | if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; |
| 122687 | 123543 | assert( pWInfo->pTabList->nSrc>=1 ); |
| 122688 | 123544 | pItem = pWInfo->pTabList->a; |
| 122689 | 123545 | pTab = pItem->pTab; |
| 122690 | 123546 | if( IsVirtual(pTab) ) return 0; |
| 122691 | | - if( pItem->zIndex ) return 0; |
| 123547 | + if( pItem->zIndexedBy ) return 0; |
| 122692 | 123548 | iCur = pItem->iCursor; |
| 122693 | 123549 | pWC = &pWInfo->sWC; |
| 122694 | 123550 | pLoop = pBuilder->pNew; |
| 122695 | 123551 | pLoop->wsFlags = 0; |
| 122696 | 123552 | pLoop->nSkip = 0; |
| 122697 | | - pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0); |
| 123553 | + pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0); |
| 122698 | 123554 | if( pTerm ){ |
| 122699 | 123555 | testcase( pTerm->eOperator & WO_IS ); |
| 122700 | 123556 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 122701 | 123557 | pLoop->aLTerm[0] = pTerm; |
| 122702 | 123558 | pLoop->nLTerm = 1; |
| | @@ -122711,11 +123567,11 @@ |
| 122711 | 123567 | || pIdx->pPartIdxWhere!=0 |
| 122712 | 123568 | || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) |
| 122713 | 123569 | ) continue; |
| 122714 | 123570 | opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ; |
| 122715 | 123571 | for(j=0; j<pIdx->nKeyCol; j++){ |
| 122716 | | - pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx); |
| 123572 | + pTerm = sqlite3WhereFindTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx); |
| 122717 | 123573 | if( pTerm==0 ) break; |
| 122718 | 123574 | testcase( pTerm->eOperator & WO_IS ); |
| 122719 | 123575 | pLoop->aLTerm[j] = pTerm; |
| 122720 | 123576 | } |
| 122721 | 123577 | if( j!=pIdx->nKeyCol ) continue; |
| | @@ -122732,11 +123588,11 @@ |
| 122732 | 123588 | } |
| 122733 | 123589 | } |
| 122734 | 123590 | if( pLoop->wsFlags ){ |
| 122735 | 123591 | pLoop->nOut = (LogEst)1; |
| 122736 | 123592 | pWInfo->a[0].pWLoop = pLoop; |
| 122737 | | - pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur); |
| 123593 | + pLoop->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); |
| 122738 | 123594 | pWInfo->a[0].iTabCur = iCur; |
| 122739 | 123595 | pWInfo->nRowOut = 1; |
| 122740 | 123596 | if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr; |
| 122741 | 123597 | if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 122742 | 123598 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| | @@ -122926,12 +123782,12 @@ |
| 122926 | 123782 | |
| 122927 | 123783 | /* Split the WHERE clause into separate subexpressions where each |
| 122928 | 123784 | ** subexpression is separated by an AND operator. |
| 122929 | 123785 | */ |
| 122930 | 123786 | initMaskSet(pMaskSet); |
| 122931 | | - whereClauseInit(&pWInfo->sWC, pWInfo); |
| 122932 | | - whereSplit(&pWInfo->sWC, pWhere, TK_AND); |
| 123787 | + sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo); |
| 123788 | + sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND); |
| 122933 | 123789 | |
| 122934 | 123790 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 122935 | 123791 | ** expression and either jump over all of the code or fall thru. |
| 122936 | 123792 | */ |
| 122937 | 123793 | for(ii=0; ii<sWLB.pWC->nTerm; ii++){ |
| | @@ -122972,26 +123828,20 @@ |
| 122972 | 123828 | } |
| 122973 | 123829 | #ifndef NDEBUG |
| 122974 | 123830 | { |
| 122975 | 123831 | Bitmask toTheLeft = 0; |
| 122976 | 123832 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 122977 | | - Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor); |
| 123833 | + Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor); |
| 122978 | 123834 | assert( (m-1)==toTheLeft ); |
| 122979 | 123835 | toTheLeft |= m; |
| 122980 | 123836 | } |
| 122981 | 123837 | } |
| 122982 | 123838 | #endif |
| 122983 | 123839 | |
| 122984 | | - /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 122985 | | - ** add new virtual terms onto the end of the WHERE clause. We do not |
| 122986 | | - ** want to analyze these virtual terms, so start analyzing at the end |
| 122987 | | - ** and work forward so that the added virtual terms are never processed. |
| 122988 | | - */ |
| 122989 | | - exprAnalyzeAll(pTabList, &pWInfo->sWC); |
| 122990 | | - if( db->mallocFailed ){ |
| 122991 | | - goto whereBeginError; |
| 122992 | | - } |
| 123840 | + /* Analyze all of the subexpressions. */ |
| 123841 | + sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC); |
| 123842 | + if( db->mallocFailed ) goto whereBeginError; |
| 122993 | 123843 | |
| 122994 | 123844 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 122995 | 123845 | if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){ |
| 122996 | 123846 | /* The DISTINCT marking is pointless. Ignore it. */ |
| 122997 | 123847 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| | @@ -123003,12 +123853,11 @@ |
| 123003 | 123853 | } |
| 123004 | 123854 | |
| 123005 | 123855 | /* Construct the WhereLoop objects */ |
| 123006 | 123856 | WHERETRACE(0xffff,("*** Optimizer Start ***\n")); |
| 123007 | 123857 | #if defined(WHERETRACE_ENABLED) |
| 123008 | | - /* Display all terms of the WHERE clause */ |
| 123009 | | - if( sqlite3WhereTrace & 0x100 ){ |
| 123858 | + if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */ |
| 123010 | 123859 | int i; |
| 123011 | 123860 | for(i=0; i<sWLB.pWC->nTerm; i++){ |
| 123012 | 123861 | whereTermPrint(&sWLB.pWC->a[i], i); |
| 123013 | 123862 | } |
| 123014 | 123863 | } |
| | @@ -123016,17 +123865,16 @@ |
| 123016 | 123865 | |
| 123017 | 123866 | if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
| 123018 | 123867 | rc = whereLoopAddAll(&sWLB); |
| 123019 | 123868 | if( rc ) goto whereBeginError; |
| 123020 | 123869 | |
| 123021 | | - /* Display all of the WhereLoop objects if wheretrace is enabled */ |
| 123022 | | -#ifdef WHERETRACE_ENABLED /* !=0 */ |
| 123023 | | - if( sqlite3WhereTrace ){ |
| 123870 | +#ifdef WHERETRACE_ENABLED |
| 123871 | + if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */ |
| 123024 | 123872 | WhereLoop *p; |
| 123025 | 123873 | int i; |
| 123026 | | - static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 123027 | | - "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
| 123874 | + static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 123875 | + "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
| 123028 | 123876 | for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ |
| 123029 | 123877 | p->cId = zLabel[i%sizeof(zLabel)]; |
| 123030 | 123878 | whereLoopPrint(p, sWLB.pWC); |
| 123031 | 123879 | } |
| 123032 | 123880 | } |
| | @@ -123043,11 +123891,11 @@ |
| 123043 | 123891 | pWInfo->revMask = (Bitmask)(-1); |
| 123044 | 123892 | } |
| 123045 | 123893 | if( pParse->nErr || NEVER(db->mallocFailed) ){ |
| 123046 | 123894 | goto whereBeginError; |
| 123047 | 123895 | } |
| 123048 | | -#ifdef WHERETRACE_ENABLED /* !=0 */ |
| 123896 | +#ifdef WHERETRACE_ENABLED |
| 123049 | 123897 | if( sqlite3WhereTrace ){ |
| 123050 | 123898 | sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
| 123051 | 123899 | if( pWInfo->nOBSat>0 ){ |
| 123052 | 123900 | sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask); |
| 123053 | 123901 | } |
| | @@ -123074,12 +123922,14 @@ |
| 123074 | 123922 | /* Attempt to omit tables from the join that do not effect the result */ |
| 123075 | 123923 | if( pWInfo->nLevel>=2 |
| 123076 | 123924 | && pResultSet!=0 |
| 123077 | 123925 | && OptimizationEnabled(db, SQLITE_OmitNoopJoin) |
| 123078 | 123926 | ){ |
| 123079 | | - Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet); |
| 123080 | | - if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy); |
| 123927 | + Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet); |
| 123928 | + if( sWLB.pOrderBy ){ |
| 123929 | + tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy); |
| 123930 | + } |
| 123081 | 123931 | while( pWInfo->nLevel>=2 ){ |
| 123082 | 123932 | WhereTerm *pTerm, *pEnd; |
| 123083 | 123933 | pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop; |
| 123084 | 123934 | if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break; |
| 123085 | 123935 | if( (wctrlFlags & WHERE_WANT_DISTINCT)==0 |
| | @@ -123106,11 +123956,11 @@ |
| 123106 | 123956 | pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; |
| 123107 | 123957 | |
| 123108 | 123958 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 123109 | 123959 | ** to use a one-pass algorithm, determine if this is appropriate. |
| 123110 | 123960 | ** The one-pass algorithm only works if the WHERE clause constrains |
| 123111 | | - ** the statement to update a single row. |
| 123961 | + ** the statement to update or delete a single row. |
| 123112 | 123962 | */ |
| 123113 | 123963 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
| 123114 | 123964 | if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 |
| 123115 | 123965 | && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){ |
| 123116 | 123966 | pWInfo->okOnePass = 1; |
| | @@ -123120,11 +123970,10 @@ |
| 123120 | 123970 | } |
| 123121 | 123971 | |
| 123122 | 123972 | /* Open all tables in the pTabList and any indices selected for |
| 123123 | 123973 | ** searching those tables. |
| 123124 | 123974 | */ |
| 123125 | | - notReady = ~(Bitmask)0; |
| 123126 | 123975 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
| 123127 | 123976 | Table *pTab; /* Table to open */ |
| 123128 | 123977 | int iDb; /* Index of database containing table/index */ |
| 123129 | 123978 | struct SrcList_item *pTabItem; |
| 123130 | 123979 | |
| | @@ -123161,10 +124010,14 @@ |
| 123161 | 124010 | for(; b; b=b>>1, n++){} |
| 123162 | 124011 | sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, |
| 123163 | 124012 | SQLITE_INT_TO_PTR(n), P4_INT32); |
| 123164 | 124013 | assert( n<=pTab->nCol ); |
| 123165 | 124014 | } |
| 124015 | +#ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
| 124016 | + sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0, |
| 124017 | + (const u8*)&pTabItem->colUsed, P4_INT64); |
| 124018 | +#endif |
| 123166 | 124019 | }else{ |
| 123167 | 124020 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 123168 | 124021 | } |
| 123169 | 124022 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
| 123170 | 124023 | Index *pIx = pLoop->u.btree.pIndex; |
| | @@ -123206,14 +124059,28 @@ |
| 123206 | 124059 | && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 |
| 123207 | 124060 | ){ |
| 123208 | 124061 | sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */ |
| 123209 | 124062 | } |
| 123210 | 124063 | VdbeComment((v, "%s", pIx->zName)); |
| 124064 | +#ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
| 124065 | + { |
| 124066 | + u64 colUsed = 0; |
| 124067 | + int ii, jj; |
| 124068 | + for(ii=0; ii<pIx->nColumn; ii++){ |
| 124069 | + jj = pIx->aiColumn[ii]; |
| 124070 | + if( jj<0 ) continue; |
| 124071 | + if( jj>63 ) jj = 63; |
| 124072 | + if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue; |
| 124073 | + colUsed |= ((u64)1)<<(ii<63 ? ii : 63); |
| 124074 | + } |
| 124075 | + sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, iIndexCur, 0, 0, |
| 124076 | + (u8*)&colUsed, P4_INT64); |
| 124077 | + } |
| 124078 | +#endif /* SQLITE_ENABLE_COLUMN_USED_MASK */ |
| 123211 | 124079 | } |
| 123212 | 124080 | } |
| 123213 | 124081 | if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb); |
| 123214 | | - notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor); |
| 123215 | 124082 | } |
| 123216 | 124083 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
| 123217 | 124084 | if( db->mallocFailed ) goto whereBeginError; |
| 123218 | 124085 | |
| 123219 | 124086 | /* Generate the code to do the search. Each iteration of the for |
| | @@ -123231,18 +124098,18 @@ |
| 123231 | 124098 | constructAutomaticIndex(pParse, &pWInfo->sWC, |
| 123232 | 124099 | &pTabList->a[pLevel->iFrom], notReady, pLevel); |
| 123233 | 124100 | if( db->mallocFailed ) goto whereBeginError; |
| 123234 | 124101 | } |
| 123235 | 124102 | #endif |
| 123236 | | - addrExplain = explainOneScan( |
| 124103 | + addrExplain = sqlite3WhereExplainOneScan( |
| 123237 | 124104 | pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags |
| 123238 | 124105 | ); |
| 123239 | 124106 | pLevel->addrBody = sqlite3VdbeCurrentAddr(v); |
| 123240 | | - notReady = codeOneLoopStart(pWInfo, ii, notReady); |
| 124107 | + notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady); |
| 123241 | 124108 | pWInfo->iContinue = pLevel->addrCont; |
| 123242 | 124109 | if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){ |
| 123243 | | - addScanStatus(v, pTabList, pLevel, addrExplain); |
| 124110 | + sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain); |
| 123244 | 124111 | } |
| 123245 | 124112 | } |
| 123246 | 124113 | |
| 123247 | 124114 | /* Done. */ |
| 123248 | 124115 | VdbeModuleComment((v, "Begin WHERE-core")); |
| | @@ -124924,11 +125791,11 @@ |
| 124924 | 125791 | YYCODETYPE yymajor; |
| 124925 | 125792 | yyStackEntry *yytos = &pParser->yystack[pParser->yyidx]; |
| 124926 | 125793 | |
| 124927 | 125794 | /* There is no mechanism by which the parser stack can be popped below |
| 124928 | 125795 | ** empty in SQLite. */ |
| 124929 | | - if( NEVER(pParser->yyidx<0) ) return 0; |
| 125796 | + assert( pParser->yyidx>=0 ); |
| 124930 | 125797 | #ifndef NDEBUG |
| 124931 | 125798 | if( yyTraceFILE && pParser->yyidx>=0 ){ |
| 124932 | 125799 | fprintf(yyTraceFILE,"%sPopping %s\n", |
| 124933 | 125800 | yyTracePrompt, |
| 124934 | 125801 | yyTokenName[yytos->major]); |
| | @@ -127396,11 +128263,15 @@ |
| 127396 | 128263 | 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */ |
| 127397 | 128264 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */ |
| 127398 | 128265 | }; |
| 127399 | 128266 | #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) |
| 127400 | 128267 | #endif |
| 128268 | + |
| 128269 | +/* Make the IdChar function accessible from ctime.c */ |
| 128270 | +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 127401 | 128271 | SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); } |
| 128272 | +#endif |
| 127402 | 128273 | |
| 127403 | 128274 | |
| 127404 | 128275 | /* |
| 127405 | 128276 | ** Return the length of the token that begins at z[0]. |
| 127406 | 128277 | ** Store the token type in *tokenType before returning. |
| | @@ -128104,11 +128975,11 @@ |
| 128104 | 128975 | rc = sqlite3_complete(zSql8); |
| 128105 | 128976 | }else{ |
| 128106 | 128977 | rc = SQLITE_NOMEM; |
| 128107 | 128978 | } |
| 128108 | 128979 | sqlite3ValueFree(pVal); |
| 128109 | | - return sqlite3ApiExit(0, rc); |
| 128980 | + return rc & 0xff; |
| 128110 | 128981 | } |
| 128111 | 128982 | #endif /* SQLITE_OMIT_UTF16 */ |
| 128112 | 128983 | #endif /* SQLITE_OMIT_COMPLETE */ |
| 128113 | 128984 | |
| 128114 | 128985 | /************** End of complete.c ********************************************/ |
| | @@ -130282,13 +131153,15 @@ |
| 130282 | 131153 | #endif |
| 130283 | 131154 | #if SQLITE_TEMP_STORE==2 |
| 130284 | 131155 | return ( db->temp_store!=1 ); |
| 130285 | 131156 | #endif |
| 130286 | 131157 | #if SQLITE_TEMP_STORE==3 |
| 131158 | + UNUSED_PARAMETER(db); |
| 130287 | 131159 | return 1; |
| 130288 | 131160 | #endif |
| 130289 | 131161 | #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3 |
| 131162 | + UNUSED_PARAMETER(db); |
| 130290 | 131163 | return 0; |
| 130291 | 131164 | #endif |
| 130292 | 131165 | } |
| 130293 | 131166 | |
| 130294 | 131167 | /* |
| | @@ -131126,11 +131999,11 @@ |
| 131126 | 131999 | /* Opening a db handle. Fourth parameter is passed 0. */ |
| 131127 | 132000 | void *pArg = sqlite3GlobalConfig.pSqllogArg; |
| 131128 | 132001 | sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0); |
| 131129 | 132002 | } |
| 131130 | 132003 | #endif |
| 131131 | | - return sqlite3ApiExit(0, rc); |
| 132004 | + return rc & 0xff; |
| 131132 | 132005 | } |
| 131133 | 132006 | |
| 131134 | 132007 | /* |
| 131135 | 132008 | ** Open a new database handle. |
| 131136 | 132009 | */ |
| | @@ -131184,11 +132057,11 @@ |
| 131184 | 132057 | }else{ |
| 131185 | 132058 | rc = SQLITE_NOMEM; |
| 131186 | 132059 | } |
| 131187 | 132060 | sqlite3ValueFree(pVal); |
| 131188 | 132061 | |
| 131189 | | - return sqlite3ApiExit(0, rc); |
| 132062 | + return rc & 0xff; |
| 131190 | 132063 | } |
| 131191 | 132064 | #endif /* SQLITE_OMIT_UTF16 */ |
| 131192 | 132065 | |
| 131193 | 132066 | /* |
| 131194 | 132067 | ** Register a new collation sequence with the database handle db. |
| | @@ -131556,11 +132429,13 @@ |
| 131556 | 132429 | /* |
| 131557 | 132430 | ** Interface to the testing logic. |
| 131558 | 132431 | */ |
| 131559 | 132432 | SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){ |
| 131560 | 132433 | int rc = 0; |
| 131561 | | -#ifndef SQLITE_OMIT_BUILTIN_TEST |
| 132434 | +#ifdef SQLITE_OMIT_BUILTIN_TEST |
| 132435 | + UNUSED_PARAMETER(op); |
| 132436 | +#else |
| 131562 | 132437 | va_list ap; |
| 131563 | 132438 | va_start(ap, op); |
| 131564 | 132439 | switch( op ){ |
| 131565 | 132440 | |
| 131566 | 132441 | /* |
| | @@ -154904,11 +155779,10 @@ |
| 154904 | 155779 | while( zPattern[iPattern]!=0 ){ |
| 154905 | 155780 | |
| 154906 | 155781 | /* Read (and consume) the next character from the input pattern. */ |
| 154907 | 155782 | UChar32 uPattern; |
| 154908 | 155783 | U8_NEXT_UNSAFE(zPattern, iPattern, uPattern); |
| 154909 | | - assert(uPattern!=0); |
| 154910 | 155784 | |
| 154911 | 155785 | /* There are now 4 possibilities: |
| 154912 | 155786 | ** |
| 154913 | 155787 | ** 1. uPattern is an unescaped match-all character "%", |
| 154914 | 155788 | ** 2. uPattern is an unescaped match-one character "_", |
| | @@ -155243,10 +156117,11 @@ |
| 155243 | 156117 | const char *zName; /* SQL Collation sequence name (eg. "japanese") */ |
| 155244 | 156118 | UCollator *pUCollator; /* ICU library collation object */ |
| 155245 | 156119 | int rc; /* Return code from sqlite3_create_collation_x() */ |
| 155246 | 156120 | |
| 155247 | 156121 | assert(nArg==2); |
| 156122 | + (void)nArg; /* Unused parameter */ |
| 155248 | 156123 | zLocale = (const char *)sqlite3_value_text(apArg[0]); |
| 155249 | 156124 | zName = (const char *)sqlite3_value_text(apArg[1]); |
| 155250 | 156125 | |
| 155251 | 156126 | if( !zLocale || !zName ){ |
| 155252 | 156127 | return; |
| | @@ -155566,16 +156441,17 @@ |
| 155566 | 156441 | |
| 155567 | 156442 | /* |
| 155568 | 156443 | ** The set of routines that implement the simple tokenizer |
| 155569 | 156444 | */ |
| 155570 | 156445 | static const sqlite3_tokenizer_module icuTokenizerModule = { |
| 155571 | | - 0, /* iVersion */ |
| 155572 | | - icuCreate, /* xCreate */ |
| 155573 | | - icuDestroy, /* xCreate */ |
| 155574 | | - icuOpen, /* xOpen */ |
| 155575 | | - icuClose, /* xClose */ |
| 155576 | | - icuNext, /* xNext */ |
| 156446 | + 0, /* iVersion */ |
| 156447 | + icuCreate, /* xCreate */ |
| 156448 | + icuDestroy, /* xCreate */ |
| 156449 | + icuOpen, /* xOpen */ |
| 156450 | + icuClose, /* xClose */ |
| 156451 | + icuNext, /* xNext */ |
| 156452 | + 0, /* xLanguageid */ |
| 155577 | 156453 | }; |
| 155578 | 156454 | |
| 155579 | 156455 | /* |
| 155580 | 156456 | ** Set *ppModule to point at the implementation of the ICU tokenizer. |
| 155581 | 156457 | */ |
| | @@ -159131,12 +160007,12 @@ |
| 159131 | 160007 | static int otaVfsFileControl(sqlite3_file *pFile, int op, void *pArg){ |
| 159132 | 160008 | ota_file *p = (ota_file *)pFile; |
| 159133 | 160009 | int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl; |
| 159134 | 160010 | int rc; |
| 159135 | 160011 | |
| 159136 | | - assert( p->openFlags & |
| 159137 | | - (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB|SQLITE_OPEN_TRANSIENT_DB) |
| 160012 | + assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) |
| 160013 | + || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL) |
| 159138 | 160014 | ); |
| 159139 | 160015 | if( op==SQLITE_FCNTL_OTA ){ |
| 159140 | 160016 | sqlite3ota *pOta = (sqlite3ota*)pArg; |
| 159141 | 160017 | |
| 159142 | 160018 | /* First try to find another OTA vfs lower down in the vfs stack. If |
| 159143 | 160019 | |