blob: 81f552998a3fddf7b598c2c3f767b04d1764b582 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
drh0fd61352014-02-07 02:29:45 +000012** The code in this file implements the function that runs the
13** bytecode of a prepared statement.
drh75897232000-05-29 14:26:00 +000014**
drhac82fcf2002-09-08 17:23:41 +000015** Various scripts scan this source file in order to generate HTML
16** documentation, headers files, or other derived files. The formatting
17** of the code in this file is, therefore, important. See other comments
18** in this file for details. If in doubt, do not deviate from existing
19** commenting and indentation practices when changing or adding code.
drh75897232000-05-29 14:26:00 +000020*/
21#include "sqliteInt.h"
drh9a324642003-09-06 20:12:01 +000022#include "vdbeInt.h"
drh8f619cc2002-09-08 00:04:50 +000023
24/*
drh2b4ded92010-09-27 21:09:31 +000025** Invoke this macro on memory cells just prior to changing the
26** value of the cell. This macro verifies that shallow copies are
drh0fd61352014-02-07 02:29:45 +000027** not misused. A shallow copy of a string or blob just copies a
28** pointer to the string or blob, not the content. If the original
29** is changed while the copy is still in use, the string or blob might
30** be changed out from under the copy. This macro verifies that nothing
drhb6e8fd12014-03-06 01:56:33 +000031** like that ever happens.
drh2b4ded92010-09-27 21:09:31 +000032*/
33#ifdef SQLITE_DEBUG
drhe4c88c02012-01-04 12:57:45 +000034# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
drh2b4ded92010-09-27 21:09:31 +000035#else
36# define memAboutToChange(P,M)
37#endif
38
39/*
drh487ab3c2001-11-08 00:45:21 +000040** The following global variable is incremented every time a cursor
drh959403f2008-12-12 17:56:16 +000041** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
drh487ab3c2001-11-08 00:45:21 +000042** procedures use this information to make sure that indices are
drhac82fcf2002-09-08 17:23:41 +000043** working correctly. This variable has no function other than to
44** help verify the correct operation of the library.
drh487ab3c2001-11-08 00:45:21 +000045*/
drh0f7eb612006-08-08 13:51:43 +000046#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000047int sqlite3_search_count = 0;
drh0f7eb612006-08-08 13:51:43 +000048#endif
drh487ab3c2001-11-08 00:45:21 +000049
drhf6038712004-02-08 18:07:34 +000050/*
51** When this global variable is positive, it gets decremented once before
drhe4c88c02012-01-04 12:57:45 +000052** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
53** field of the sqlite3 structure is set in order to simulate an interrupt.
drhf6038712004-02-08 18:07:34 +000054**
55** This facility is used for testing purposes only. It does not function
56** in an ordinary build.
57*/
drh0f7eb612006-08-08 13:51:43 +000058#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000059int sqlite3_interrupt_count = 0;
drh0f7eb612006-08-08 13:51:43 +000060#endif
drh1350b032002-02-27 19:00:20 +000061
danielk19777e18c252004-05-25 11:47:24 +000062/*
drh6bf89572004-11-03 16:27:01 +000063** The next global variable is incremented each type the OP_Sort opcode
64** is executed. The test procedures use this information to make sure that
shane21e7feb2008-05-30 15:59:49 +000065** sorting is occurring or not occurring at appropriate times. This variable
drh6bf89572004-11-03 16:27:01 +000066** has no function other than to help verify the correct operation of the
67** library.
68*/
drh0f7eb612006-08-08 13:51:43 +000069#ifdef SQLITE_TEST
drh6bf89572004-11-03 16:27:01 +000070int sqlite3_sort_count = 0;
drh0f7eb612006-08-08 13:51:43 +000071#endif
drh6bf89572004-11-03 16:27:01 +000072
73/*
drhae7e1512007-05-02 16:51:59 +000074** The next global variable records the size of the largest MEM_Blob
drh9cbf3422008-01-17 16:22:13 +000075** or MEM_Str that has been used by a VDBE opcode. The test procedures
drhae7e1512007-05-02 16:51:59 +000076** use this information to make sure that the zero-blob functionality
77** is working correctly. This variable has no function other than to
78** help verify the correct operation of the library.
79*/
80#ifdef SQLITE_TEST
81int sqlite3_max_blobsize = 0;
drhca48c902008-01-18 14:08:24 +000082static void updateMaxBlobsize(Mem *p){
83 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
84 sqlite3_max_blobsize = p->n;
85 }
86}
drhae7e1512007-05-02 16:51:59 +000087#endif
88
89/*
drh9b1c62d2011-03-30 21:04:43 +000090** This macro evaluates to true if either the update hook or the preupdate
91** hook are enabled for database connect DB.
92*/
93#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
drh74c33022016-03-30 12:56:55 +000094# define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
drh9b1c62d2011-03-30 21:04:43 +000095#else
drh74c33022016-03-30 12:56:55 +000096# define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
drh9b1c62d2011-03-30 21:04:43 +000097#endif
98
99/*
drh0fd61352014-02-07 02:29:45 +0000100** The next global variable is incremented each time the OP_Found opcode
dan0ff297e2009-09-25 17:03:14 +0000101** is executed. This is used to test whether or not the foreign key
102** operation implemented using OP_FkIsZero is working. This variable
103** has no function other than to help verify the correct operation of the
104** library.
105*/
106#ifdef SQLITE_TEST
107int sqlite3_found_count = 0;
108#endif
109
110/*
drhb7654112008-01-12 12:48:07 +0000111** Test a register to see if it exceeds the current maximum blob size.
112** If it does, record the new maximum blob size.
113*/
drhd12602a2016-12-07 15:49:02 +0000114#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
drhca48c902008-01-18 14:08:24 +0000115# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
drhb7654112008-01-12 12:48:07 +0000116#else
117# define UPDATE_MAX_BLOBSIZE(P)
118#endif
119
drh52f11b82020-01-02 13:26:49 +0000120#ifdef SQLITE_DEBUG
121/* This routine provides a convenient place to set a breakpoint during
122** tracing with PRAGMA vdbe_trace=on. The breakpoint fires right after
123** each opcode is printed. Variables "pc" (program counter) and pOp are
124** available to add conditionals to the breakpoint. GDB example:
125**
126** break test_trace_breakpoint if pc=22
127**
128** Other useful labels for breakpoints include:
129** test_addop_breakpoint(pc,pOp)
130** sqlite3CorruptError(lineno)
131** sqlite3MisuseError(lineno)
132** sqlite3CantopenError(lineno)
133*/
drh22e95fb2020-01-02 14:42:42 +0000134static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){
drh52f11b82020-01-02 13:26:49 +0000135 static int n = 0;
136 n++;
137}
138#endif
139
drhb7654112008-01-12 12:48:07 +0000140/*
drh5655c542014-02-19 19:14:34 +0000141** Invoke the VDBE coverage callback, if that callback is defined. This
142** feature is used for test suite validation only and does not appear an
143** production builds.
144**
drhc9065332019-04-01 14:01:21 +0000145** M is the type of branch. I is the direction taken for this instance of
146** the branch.
147**
148** M: 2 - two-way branch (I=0: fall-thru 1: jump )
149** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
150** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
151**
152** In other words, if M is 2, then I is either 0 (for fall-through) or
153** 1 (for when the branch is taken). If M is 3, the I is 0 for an
154** ordinary fall-through, I is 1 if the branch was taken, and I is 2
155** if the result of comparison is NULL. For M=3, I=2 the jump may or
156** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
157** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2
158** depending on if the operands are less than, equal, or greater than.
drh4336b0e2014-08-05 00:53:51 +0000159**
160** iSrcLine is the source code line (from the __LINE__ macro) that
drh7083a482018-07-10 16:04:04 +0000161** generated the VDBE instruction combined with flag bits. The source
162** code line number is in the lower 24 bits of iSrcLine and the upper
163** 8 bytes are flags. The lower three bits of the flags indicate
164** values for I that should never occur. For example, if the branch is
165** always taken, the flags should be 0x05 since the fall-through and
166** alternate branch are never taken. If a branch is never taken then
167** flags should be 0x06 since only the fall-through approach is allowed.
168**
drhc9065332019-04-01 14:01:21 +0000169** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
drh7083a482018-07-10 16:04:04 +0000170** interested in equal or not-equal. In other words, I==0 and I==2
drhc9065332019-04-01 14:01:21 +0000171** should be treated as equivalent
drh7083a482018-07-10 16:04:04 +0000172**
173** Since only a line number is retained, not the filename, this macro
174** only works for amalgamation builds. But that is ok, since these macros
175** should be no-ops except for special builds used to measure test coverage.
drh688852a2014-02-17 22:40:43 +0000176*/
177#if !defined(SQLITE_VDBE_COVERAGE)
178# define VdbeBranchTaken(I,M)
179#else
drh5655c542014-02-19 19:14:34 +0000180# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
drh7083a482018-07-10 16:04:04 +0000181 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
182 u8 mNever;
183 assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */
184 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
185 assert( I<M ); /* I can only be 2 if M is 3 or 4 */
186 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
187 I = 1<<I;
188 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
189 ** the flags indicate directions that the branch can never go. If
190 ** a branch really does go in one of those directions, assert right
191 ** away. */
192 mNever = iSrcLine >> 24;
193 assert( (I & mNever)==0 );
194 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
drhc9065332019-04-01 14:01:21 +0000195 /* Invoke the branch coverage callback with three arguments:
196 ** iSrcLine - the line number of the VdbeCoverage() macro, with
197 ** flags removed.
198 ** I - Mask of bits 0x07 indicating which cases are are
199 ** fulfilled by this instance of the jump. 0x01 means
200 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
201 ** impossible cases (ex: if the comparison is never NULL)
202 ** are filled in automatically so that the coverage
203 ** measurement logic does not flag those impossible cases
204 ** as missed coverage.
205 ** M - Type of jump. Same as M argument above
206 */
drh7083a482018-07-10 16:04:04 +0000207 I |= mNever;
208 if( M==2 ) I |= 0x04;
209 if( M==4 ){
210 I |= 0x08;
drh6ccbd272018-07-10 17:10:44 +0000211 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
drh5655c542014-02-19 19:14:34 +0000212 }
drh7083a482018-07-10 16:04:04 +0000213 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
214 iSrcLine&0xffffff, I, M);
drh5655c542014-02-19 19:14:34 +0000215 }
drh688852a2014-02-17 22:40:43 +0000216#endif
217
218/*
danielk1977bd7e4602004-05-24 07:34:48 +0000219** An ephemeral string value (signified by the MEM_Ephem flag) contains
220** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000221** is responsible for deallocating that string. Because the register
222** does not control the string, it might be deleted without the register
223** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000224**
225** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000226** string that the register itself controls. In other words, it
drhc91b2fd2014-03-01 18:13:23 +0000227** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
danielk1977bd7e4602004-05-24 07:34:48 +0000228*/
drhb21c8cd2007-08-21 19:33:56 +0000229#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000230 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000231 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000232
dan689ab892011-08-12 15:02:00 +0000233/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
drhc960dcb2015-11-20 19:22:01 +0000234#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
danielk19778a6b5412004-05-24 07:04:25 +0000235
236/*
drhdfe88ec2008-11-03 20:55:06 +0000237** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000238** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000239*/
drhdfe88ec2008-11-03 20:55:06 +0000240static VdbeCursor *allocateCursor(
241 Vdbe *p, /* The virtual machine */
242 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000243 int nField, /* Number of fields in the table or index */
drhc960dcb2015-11-20 19:22:01 +0000244 u8 eCurType /* Type of the new cursor */
danielk1977cd3e8f72008-03-25 09:47:35 +0000245){
246 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000247 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000248 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000249 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000250 **
251 ** * Sometimes cursor numbers are used for a couple of different
252 ** purposes in a vdbe program. The different uses might require
253 ** different sized allocations. Memory cells provide growable
254 ** allocations.
255 **
256 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
257 ** be freed lazily via the sqlite3_release_memory() API. This
258 ** minimizes the number of malloc calls made by the system.
259 **
drh3cdce922016-03-21 00:30:40 +0000260 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
drh9f6168b2016-03-19 23:32:58 +0000261 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
262 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
danielk1977cd3e8f72008-03-25 09:47:35 +0000263 */
drh9f6168b2016-03-19 23:32:58 +0000264 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
danielk1977cd3e8f72008-03-25 09:47:35 +0000265
danielk19775f096132008-03-28 15:44:09 +0000266 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000267 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000268 nByte =
drhcf6e3fd2022-04-01 18:45:11 +0000269 ROUND8P(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
drhc960dcb2015-11-20 19:22:01 +0000270 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
danielk1977cd3e8f72008-03-25 09:47:35 +0000271
drh9f6168b2016-03-19 23:32:58 +0000272 assert( iCur>=0 && iCur<p->nCursor );
drha3fa1402016-04-29 02:55:05 +0000273 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
drh473571b2022-04-01 18:19:04 +0000274 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000275 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000276 }
drh2454e4a2021-05-15 19:36:36 +0000277
278 /* There used to be a call to sqlite3VdbeMemClearAndResize() to make sure
279 ** the pMem used to hold space for the cursor has enough storage available
280 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
281 ** to hold cursors, it is faster to in-line the logic. */
282 assert( pMem->flags==MEM_Undefined );
283 assert( (pMem->flags & MEM_Dyn)==0 );
284 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc );
285 if( pMem->szMalloc<nByte ){
286 if( pMem->szMalloc>0 ){
287 sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
danielk1977cd3e8f72008-03-25 09:47:35 +0000288 }
drh2454e4a2021-05-15 19:36:36 +0000289 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte);
290 if( pMem->zMalloc==0 ){
291 pMem->szMalloc = 0;
292 return 0;
293 }
294 pMem->szMalloc = nByte;
295 }
296
297 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
298 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
299 pCx->eCurType = eCurType;
drh2454e4a2021-05-15 19:36:36 +0000300 pCx->nField = nField;
301 pCx->aOffset = &pCx->aType[nField];
302 if( eCurType==CURTYPE_BTREE ){
303 pCx->uc.pCursor = (BtCursor*)
drhcf6e3fd2022-04-01 18:45:11 +0000304 &pMem->z[ROUND8P(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drh2454e4a2021-05-15 19:36:36 +0000305 sqlite3BtreeCursorZero(pCx->uc.pCursor);
danielk197794eb6a12005-12-15 15:22:08 +0000306 }
drh4774b132004-06-12 20:12:51 +0000307 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000308}
309
danielk19773d1bfea2004-05-14 11:00:53 +0000310/*
drh8a3884e2019-05-29 21:18:27 +0000311** The string in pRec is known to look like an integer and to have a
312** floating point value of rValue. Return true and set *piValue to the
313** integer value if the string is in range to be an integer. Otherwise,
314** return false.
315*/
316static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
317 i64 iValue = (double)rValue;
318 if( sqlite3RealSameAsInt(rValue,iValue) ){
drhc285ded2019-06-10 18:33:16 +0000319 *piValue = iValue;
320 return 1;
drh8a3884e2019-05-29 21:18:27 +0000321 }
322 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
323}
324
325/*
drh29d72102006-02-09 22:13:41 +0000326** Try to convert a value into a numeric representation if we can
327** do so without loss of information. In other words, if the string
328** looks like a number, convert it into a number. If it does not
329** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000330**
331** If the bTryForInt flag is true, then extra effort is made to give
332** an integer representation. Strings that look like floating point
333** values but which have no fractional component (example: '48.00')
334** will have a MEM_Int representation when bTryForInt is true.
335**
336** If bTryForInt is false, then if the input string contains a decimal
337** point or exponential notation, the result is only MEM_Real, even
338** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000339*/
drhbd9507c2014-08-23 17:21:37 +0000340static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000341 double rValue;
drh975b4c62014-07-26 16:47:23 +0000342 u8 enc = pRec->enc;
drh8a3884e2019-05-29 21:18:27 +0000343 int rc;
drh169f0772019-05-02 21:36:26 +0000344 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
drh8a3884e2019-05-29 21:18:27 +0000345 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
drh9a278222019-06-07 22:26:08 +0000346 if( rc<=0 ) return;
drh8a3884e2019-05-29 21:18:27 +0000347 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
drh975b4c62014-07-26 16:47:23 +0000348 pRec->flags |= MEM_Int;
349 }else{
drh74eaba42014-09-18 17:52:15 +0000350 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000351 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000352 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000353 }
drh06b3bd52018-02-01 01:13:33 +0000354 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
355 ** string representation after computing a numeric equivalent, because the
356 ** string representation might not be the canonical representation for the
357 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
358 pRec->flags &= ~MEM_Str;
drh29d72102006-02-09 22:13:41 +0000359}
360
361/*
drh8a512562005-11-14 22:29:05 +0000362** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000363**
drh8a512562005-11-14 22:29:05 +0000364** SQLITE_AFF_INTEGER:
365** SQLITE_AFF_REAL:
366** SQLITE_AFF_NUMERIC:
367** Try to convert pRec to an integer representation or a
368** floating-point representation if an integer representation
369** is not possible. Note that the integer representation is
370** always preferred, even if the affinity is REAL, because
371** an integer representation is more space efficient on disk.
372**
373** SQLITE_AFF_TEXT:
374** Convert pRec to a text representation.
375**
drh05883a32015-06-02 15:32:08 +0000376** SQLITE_AFF_BLOB:
drh96fb16e2019-08-06 14:37:24 +0000377** SQLITE_AFF_NONE:
drh8a512562005-11-14 22:29:05 +0000378** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000379*/
drh17435752007-08-16 04:30:38 +0000380static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000381 Mem *pRec, /* The value to apply affinity to */
382 char affinity, /* The affinity to be applied */
383 u8 enc /* Use this text encoding */
384){
drh7ea31cc2014-09-18 14:36:00 +0000385 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000386 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
387 || affinity==SQLITE_AFF_NUMERIC );
drha3fa1402016-04-29 02:55:05 +0000388 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
drhbd9507c2014-08-23 17:21:37 +0000389 if( (pRec->flags & MEM_Real)==0 ){
drh11a6eee2014-09-19 22:01:54 +0000390 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drhbd9507c2014-08-23 17:21:37 +0000391 }else{
392 sqlite3VdbeIntegerAffinity(pRec);
393 }
drh17c40292004-07-21 02:53:29 +0000394 }
drh7ea31cc2014-09-18 14:36:00 +0000395 }else if( affinity==SQLITE_AFF_TEXT ){
danielk19773d1bfea2004-05-14 11:00:53 +0000396 /* Only attempt the conversion to TEXT if there is an integer or real
drhf4479502004-05-27 03:12:53 +0000397 ** representation (blob and NULL do not get converted) but no string
drha3fa1402016-04-29 02:55:05 +0000398 ** representation. It would be harmless to repeat the conversion if
399 ** there is already a string rep, but it is pointless to waste those
400 ** CPU cycles. */
401 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
drh169f0772019-05-02 21:36:26 +0000402 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
drh3242c692019-05-04 01:29:13 +0000403 testcase( pRec->flags & MEM_Int );
404 testcase( pRec->flags & MEM_Real );
405 testcase( pRec->flags & MEM_IntReal );
drha3fa1402016-04-29 02:55:05 +0000406 sqlite3VdbeMemStringify(pRec, enc, 1);
407 }
danielk19773d1bfea2004-05-14 11:00:53 +0000408 }
drh169f0772019-05-02 21:36:26 +0000409 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
danielk19773d1bfea2004-05-14 11:00:53 +0000410 }
411}
412
danielk1977aee18ef2005-03-09 12:26:50 +0000413/*
drh29d72102006-02-09 22:13:41 +0000414** Try to convert the type of a function argument or a result column
415** into a numeric representation. Use either INTEGER or REAL whichever
416** is appropriate. But only do the conversion if it is possible without
417** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000418*/
419int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000420 int eType = sqlite3_value_type(pVal);
421 if( eType==SQLITE_TEXT ){
422 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000423 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000424 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000425 }
drh1b27b8c2014-02-10 03:21:57 +0000426 return eType;
drh29d72102006-02-09 22:13:41 +0000427}
428
429/*
danielk1977aee18ef2005-03-09 12:26:50 +0000430** Exported version of applyAffinity(). This one works on sqlite3_value*,
431** not the internal Mem* type.
432*/
danielk19771e536952007-08-16 10:09:01 +0000433void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000434 sqlite3_value *pVal,
435 u8 affinity,
436 u8 enc
437){
drhb21c8cd2007-08-21 19:33:56 +0000438 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000439}
440
drh3d1d90a2014-03-24 15:00:15 +0000441/*
drhf1a89ed2014-08-23 17:41:15 +0000442** pMem currently only holds a string type (or maybe a BLOB that we can
443** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000444** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000445** accordingly.
446*/
447static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
drh9a278222019-06-07 22:26:08 +0000448 int rc;
449 sqlite3_int64 ix;
drh169f0772019-05-02 21:36:26 +0000450 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
drhf1a89ed2014-08-23 17:41:15 +0000451 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh1fd1cc42021-04-10 15:34:30 +0000452 if( ExpandBlob(pMem) ){
453 pMem->u.i = 0;
454 return MEM_Int;
455 }
drh9a278222019-06-07 22:26:08 +0000456 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
457 if( rc<=0 ){
458 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
459 pMem->u.i = ix;
460 return MEM_Int;
461 }else{
462 return MEM_Real;
463 }
464 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
465 pMem->u.i = ix;
drhf1a89ed2014-08-23 17:41:15 +0000466 return MEM_Int;
467 }
468 return MEM_Real;
469}
470
471/*
drh3d1d90a2014-03-24 15:00:15 +0000472** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
473** none.
474**
475** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000476** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000477*/
478static u16 numericType(Mem *pMem){
drh169f0772019-05-02 21:36:26 +0000479 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){
drh3242c692019-05-04 01:29:13 +0000480 testcase( pMem->flags & MEM_Int );
481 testcase( pMem->flags & MEM_Real );
482 testcase( pMem->flags & MEM_IntReal );
drh169f0772019-05-02 21:36:26 +0000483 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal);
drh3d1d90a2014-03-24 15:00:15 +0000484 }
485 if( pMem->flags & (MEM_Str|MEM_Blob) ){
drh3242c692019-05-04 01:29:13 +0000486 testcase( pMem->flags & MEM_Str );
487 testcase( pMem->flags & MEM_Blob );
drhf1a89ed2014-08-23 17:41:15 +0000488 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000489 }
490 return 0;
491}
492
danielk1977b5402fb2005-01-12 07:15:04 +0000493#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000494/*
danielk1977ca6b2912004-05-21 10:49:47 +0000495** Write a nice string representation of the contents of cell pMem
496** into buffer zBuf, length nBuf.
497*/
drh5ca06322020-01-06 19:23:41 +0000498void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){
danielk1977ca6b2912004-05-21 10:49:47 +0000499 int f = pMem->flags;
drh57196282004-10-06 15:41:16 +0000500 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977ca6b2912004-05-21 10:49:47 +0000501 if( f&MEM_Blob ){
502 int i;
503 char c;
504 if( f & MEM_Dyn ){
505 c = 'z';
506 assert( (f & (MEM_Static|MEM_Ephem))==0 );
507 }else if( f & MEM_Static ){
508 c = 't';
509 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
510 }else if( f & MEM_Ephem ){
511 c = 'e';
512 assert( (f & (MEM_Static|MEM_Dyn))==0 );
513 }else{
514 c = 's';
515 }
drhded33cc2020-01-08 11:36:30 +0000516 sqlite3_str_appendf(pStr, "%cx[", c);
drhefb5f9a2019-08-30 21:52:13 +0000517 for(i=0; i<25 && i<pMem->n; i++){
drh5ca06322020-01-06 19:23:41 +0000518 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
danielk1977ca6b2912004-05-21 10:49:47 +0000519 }
drh5ca06322020-01-06 19:23:41 +0000520 sqlite3_str_appendf(pStr, "|");
drhefb5f9a2019-08-30 21:52:13 +0000521 for(i=0; i<25 && i<pMem->n; i++){
danielk1977ca6b2912004-05-21 10:49:47 +0000522 char z = pMem->z[i];
drh5ca06322020-01-06 19:23:41 +0000523 sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z);
danielk1977ca6b2912004-05-21 10:49:47 +0000524 }
drh5ca06322020-01-06 19:23:41 +0000525 sqlite3_str_appendf(pStr,"]");
drhfdf972a2007-05-02 13:30:27 +0000526 if( f & MEM_Zero ){
drh5ca06322020-01-06 19:23:41 +0000527 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
drhfdf972a2007-05-02 13:30:27 +0000528 }
danielk1977b1bc9532004-05-22 03:05:33 +0000529 }else if( f & MEM_Str ){
drh5ca06322020-01-06 19:23:41 +0000530 int j;
mistachkin59171172020-01-18 19:02:20 +0000531 u8 c;
danielk1977b1bc9532004-05-22 03:05:33 +0000532 if( f & MEM_Dyn ){
drh5ca06322020-01-06 19:23:41 +0000533 c = 'z';
danielk1977b1bc9532004-05-22 03:05:33 +0000534 assert( (f & (MEM_Static|MEM_Ephem))==0 );
535 }else if( f & MEM_Static ){
drh5ca06322020-01-06 19:23:41 +0000536 c = 't';
danielk1977b1bc9532004-05-22 03:05:33 +0000537 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
538 }else if( f & MEM_Ephem ){
drh5ca06322020-01-06 19:23:41 +0000539 c = 'e';
danielk1977b1bc9532004-05-22 03:05:33 +0000540 assert( (f & (MEM_Static|MEM_Dyn))==0 );
541 }else{
drh5ca06322020-01-06 19:23:41 +0000542 c = 's';
danielk1977b1bc9532004-05-22 03:05:33 +0000543 }
drh5ca06322020-01-06 19:23:41 +0000544 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
drhefb5f9a2019-08-30 21:52:13 +0000545 for(j=0; j<25 && j<pMem->n; j++){
mistachkin59171172020-01-18 19:02:20 +0000546 c = pMem->z[j];
drh5ca06322020-01-06 19:23:41 +0000547 sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.');
danielk1977b1bc9532004-05-22 03:05:33 +0000548 }
drh5ca06322020-01-06 19:23:41 +0000549 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
danielk1977ca6b2912004-05-21 10:49:47 +0000550 }
danielk1977ca6b2912004-05-21 10:49:47 +0000551}
552#endif
553
drh5b6afba2008-01-05 16:29:28 +0000554#ifdef SQLITE_DEBUG
555/*
556** Print the value of a register for tracing purposes:
557*/
drh84e55a82013-11-13 17:58:23 +0000558static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000559 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000560 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000561 }else if( p->flags & MEM_Null ){
drhce2fbd12018-01-12 21:00:14 +0000562 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
drh5b6afba2008-01-05 16:29:28 +0000563 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000564 printf(" si:%lld", p->u.i);
drh169f0772019-05-02 21:36:26 +0000565 }else if( (p->flags & (MEM_IntReal))!=0 ){
drh83a1daf2019-05-01 18:59:33 +0000566 printf(" ir:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000567 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000568 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000569#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000570 }else if( p->flags & MEM_Real ){
drhd1c472d2019-10-03 14:51:59 +0000571 printf(" r:%.17g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000572#endif
drh9d67afc2018-08-29 20:24:03 +0000573 }else if( sqlite3VdbeMemIsRowSet(p) ){
drh84e55a82013-11-13 17:58:23 +0000574 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000575 }else{
drh5ca06322020-01-06 19:23:41 +0000576 StrAccum acc;
577 char zBuf[1000];
578 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
579 sqlite3VdbeMemPrettyPrint(p, &acc);
580 printf(" %s", sqlite3StrAccumFinish(&acc));
drh5b6afba2008-01-05 16:29:28 +0000581 }
dan5b6c8e42016-01-30 15:46:03 +0000582 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
drh5b6afba2008-01-05 16:29:28 +0000583}
drh84e55a82013-11-13 17:58:23 +0000584static void registerTrace(int iReg, Mem *p){
drh22e95fb2020-01-02 14:42:42 +0000585 printf("R[%d] = ", iReg);
drh84e55a82013-11-13 17:58:23 +0000586 memTracePrint(p);
drh22e95fb2020-01-02 14:42:42 +0000587 if( p->pScopyFrom ){
588 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
589 }
drh84e55a82013-11-13 17:58:23 +0000590 printf("\n");
drhe2bc6552017-04-17 20:50:34 +0000591 sqlite3VdbeCheckMemInvariants(p);
drh5b6afba2008-01-05 16:29:28 +0000592}
drh93ffb502021-05-18 19:10:10 +0000593/**/ void sqlite3PrintMem(Mem *pMem){
drh59df3e92021-05-05 19:46:50 +0000594 memTracePrint(pMem);
595 printf("\n");
596 fflush(stdout);
597}
drh5b6afba2008-01-05 16:29:28 +0000598#endif
599
600#ifdef SQLITE_DEBUG
drh22e95fb2020-01-02 14:42:42 +0000601/*
602** Show the values of all registers in the virtual machine. Used for
603** interactive debugging.
604*/
605void sqlite3VdbeRegisterDump(Vdbe *v){
606 int i;
607 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
608}
609#endif /* SQLITE_DEBUG */
610
611
612#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000613# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000614#else
615# define REGISTER_TRACE(R,M)
616#endif
617
danielk197784ac9d02004-05-18 09:58:06 +0000618
drh7b396862003-01-01 23:06:20 +0000619#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000620
621/*
622** hwtime.h contains inline assembler code for implementing
623** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000624*/
shane9bcbdad2008-05-29 20:22:37 +0000625#include "hwtime.h"
626
drh7b396862003-01-01 23:06:20 +0000627#endif
628
danielk1977fd7f0452008-12-17 17:30:26 +0000629#ifndef NDEBUG
630/*
631** This function is only called from within an assert() expression. It
632** checks that the sqlite3.nTransaction variable is correctly set to
633** the number of non-transaction savepoints currently in the
634** linked list starting at sqlite3.pSavepoint.
635**
636** Usage:
637**
638** assert( checkSavepointCount(db) );
639*/
640static int checkSavepointCount(sqlite3 *db){
641 int n = 0;
642 Savepoint *p;
643 for(p=db->pSavepoint; p; p=p->pNext) n++;
644 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
645 return 1;
646}
647#endif
648
drh27a348c2015-04-13 19:14:06 +0000649/*
650** Return the register of pOp->p2 after first preparing it to be
651** overwritten with an integer value.
drh9eef8c62015-10-15 17:31:41 +0000652*/
653static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
654 sqlite3VdbeMemSetNull(pOut);
655 pOut->flags = MEM_Int;
656 return pOut;
657}
drh27a348c2015-04-13 19:14:06 +0000658static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
659 Mem *pOut;
660 assert( pOp->p2>0 );
drh9f6168b2016-03-19 23:32:58 +0000661 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
drh27a348c2015-04-13 19:14:06 +0000662 pOut = &p->aMem[pOp->p2];
663 memAboutToChange(p, pOut);
drha3fa1402016-04-29 02:55:05 +0000664 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
drh9eef8c62015-10-15 17:31:41 +0000665 return out2PrereleaseWithClear(pOut);
666 }else{
667 pOut->flags = MEM_Int;
668 return pOut;
669 }
drh27a348c2015-04-13 19:14:06 +0000670}
671
drhfaf9c772021-08-20 08:05:42 +0000672/*
drh2db144c2021-12-01 16:31:02 +0000673** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
674** with pOp->p3. Return the hash.
675*/
drh5baaf402021-12-06 13:07:28 +0000676static u64 filterHash(const Mem *aMem, const Op *pOp){
drh2db144c2021-12-01 16:31:02 +0000677 int i, mx;
drh5baaf402021-12-06 13:07:28 +0000678 u64 h = 0;
drh2db144c2021-12-01 16:31:02 +0000679
drh2db144c2021-12-01 16:31:02 +0000680 assert( pOp->p4type==P4_INT32 );
drh2db144c2021-12-01 16:31:02 +0000681 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
682 const Mem *p = &aMem[i];
683 if( p->flags & (MEM_Int|MEM_IntReal) ){
drh5baaf402021-12-06 13:07:28 +0000684 h += p->u.i;
drh2db144c2021-12-01 16:31:02 +0000685 }else if( p->flags & MEM_Real ){
drh5baaf402021-12-06 13:07:28 +0000686 h += sqlite3VdbeIntValue(p);
drh2db144c2021-12-01 16:31:02 +0000687 }else if( p->flags & (MEM_Str|MEM_Blob) ){
688 h += p->n;
drh067c60c2021-12-04 18:45:08 +0000689 if( p->flags & MEM_Zero ) h += p->u.nZero;
drh2db144c2021-12-01 16:31:02 +0000690 }
691 }
drh5baaf402021-12-06 13:07:28 +0000692 return h;
drh2db144c2021-12-01 16:31:02 +0000693}
694
695/*
drhfaf9c772021-08-20 08:05:42 +0000696** Return the symbolic name for the data type of a pMem
697*/
698static const char *vdbeMemTypeName(Mem *pMem){
699 static const char *azTypes[] = {
700 /* SQLITE_INTEGER */ "INT",
701 /* SQLITE_FLOAT */ "REAL",
702 /* SQLITE_TEXT */ "TEXT",
703 /* SQLITE_BLOB */ "BLOB",
704 /* SQLITE_NULL */ "NULL"
705 };
706 return azTypes[sqlite3_value_type(pMem)-1];
707}
drhb9755982010-07-24 16:34:37 +0000708
709/*
drh0fd61352014-02-07 02:29:45 +0000710** Execute as much of a VDBE program as we can.
711** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000712*/
danielk19774adee202004-05-08 08:23:19 +0000713int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000714 Vdbe *p /* The VDBE */
715){
drhbbe879d2009-11-14 18:04:35 +0000716 Op *aOp = p->aOp; /* Copy of p->aOp */
mistachkin5f7b95f2017-02-01 23:03:54 +0000717 Op *pOp = aOp; /* Current operation */
drh6dc41482015-04-16 17:31:02 +0000718#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
719 Op *pOrigOp; /* Value of pOp at the top of the loop */
720#endif
drhb89aeb62016-01-27 15:49:32 +0000721#ifdef SQLITE_DEBUG
drhdef19e32016-01-27 16:26:25 +0000722 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
drhb89aeb62016-01-27 15:49:32 +0000723#endif
drhb86ccfb2003-01-28 23:13:10 +0000724 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000725 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000726 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000727 u8 encoding = ENC(db); /* The database encoding */
drh0f825a72016-08-13 14:17:02 +0000728 int iCompare = 0; /* Result of last comparison */
drhd1d89142020-07-06 12:13:05 +0000729 u64 nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000730#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhd1d89142020-07-06 12:13:05 +0000731 u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000732#endif
drha6c2ed92009-11-14 23:22:23 +0000733 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000734 Mem *pIn1 = 0; /* 1st input operand */
735 Mem *pIn2 = 0; /* 2nd input operand */
736 Mem *pIn3 = 0; /* 3rd input operand */
737 Mem *pOut = 0; /* Output operand */
drhb86ccfb2003-01-28 23:13:10 +0000738#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000739 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000740#endif
drh856c1032009-06-02 15:21:42 +0000741 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000742
drh66181ce2022-03-31 20:04:49 +0000743 assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000744 sqlite3VdbeEnter(p);
drh82642f82019-02-12 22:58:32 +0000745#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
746 if( db->xProgress ){
747 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
748 assert( 0 < db->nProgressOps );
749 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
750 }else{
drhd1d89142020-07-06 12:13:05 +0000751 nProgressLimit = LARGEST_UINT64;
drh82642f82019-02-12 22:58:32 +0000752 }
753#endif
danielk19772e588c72005-12-09 14:25:08 +0000754 if( p->rc==SQLITE_NOMEM ){
755 /* This happens if a malloc() inside a call to sqlite3_column_text() or
756 ** sqlite3_column_text16() failed. */
757 goto no_mem;
758 }
drhcbd8db32015-08-20 17:18:32 +0000759 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
drha5f3fb32020-06-03 19:28:10 +0000760 testcase( p->rc!=SQLITE_OK );
761 p->rc = SQLITE_OK;
drh1713afb2013-06-28 01:24:57 +0000762 assert( p->bIsReader || p->readOnly!=0 );
drh95a7b3e2013-09-16 12:57:19 +0000763 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000764 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000765 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000766 db->busyHandler.nBusy = 0;
dan892edb62020-03-30 13:35:05 +0000767 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000768 sqlite3VdbeIOTraceSql(p);
drh3c23a882007-01-09 14:01:13 +0000769#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000770 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000771 if( p->pc==0
772 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
773 ){
drh3c23a882007-01-09 14:01:13 +0000774 int i;
drh84e55a82013-11-13 17:58:23 +0000775 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000776 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000777 if( p->db->flags & SQLITE_VdbeListing ){
778 printf("VDBE Program Listing:\n");
779 for(i=0; i<p->nOp; i++){
780 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
781 }
drh3c23a882007-01-09 14:01:13 +0000782 }
drh84e55a82013-11-13 17:58:23 +0000783 if( p->db->flags & SQLITE_VdbeEQP ){
784 for(i=0; i<p->nOp; i++){
785 if( aOp[i].opcode==OP_Explain ){
786 if( once ) printf("VDBE Query Plan:\n");
787 printf("%s\n", aOp[i].p4.z);
788 once = 0;
789 }
790 }
791 }
792 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000793 }
danielk19772d1d86f2008-06-20 14:59:51 +0000794 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000795#endif
drh9467abf2016-02-17 18:44:11 +0000796 for(pOp=&aOp[p->pc]; 1; pOp++){
797 /* Errors are detected by individual opcodes, with an immediate
798 ** jumps to abort_due_to_error. */
799 assert( rc==SQLITE_OK );
800
drhf56fa462015-04-13 21:39:54 +0000801 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
drh7b396862003-01-01 23:06:20 +0000802#ifdef VDBE_PROFILE
drh35043cc2018-02-12 20:27:34 +0000803 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000804#endif
drhbf159fa2013-06-25 22:01:22 +0000805 nVmStep++;
dan6f9702e2014-11-01 20:38:06 +0000806#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
drhf56fa462015-04-13 21:39:54 +0000807 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
dan6f9702e2014-11-01 20:38:06 +0000808#endif
drh6e142f52000-06-08 13:36:40 +0000809
danielk19778b60e0f2005-01-12 09:10:39 +0000810 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000811 */
danielk19778b60e0f2005-01-12 09:10:39 +0000812#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000813 if( db->flags & SQLITE_VdbeTrace ){
drhf56fa462015-04-13 21:39:54 +0000814 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
drh22e95fb2020-01-02 14:42:42 +0000815 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
drh75897232000-05-29 14:26:00 +0000816 }
drh3f7d4e42004-07-24 14:35:58 +0000817#endif
818
drh6e142f52000-06-08 13:36:40 +0000819
drhf6038712004-02-08 18:07:34 +0000820 /* Check to see if we need to simulate an interrupt. This only happens
821 ** if we have a special test build.
822 */
823#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000824 if( sqlite3_interrupt_count>0 ){
825 sqlite3_interrupt_count--;
826 if( sqlite3_interrupt_count==0 ){
827 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000828 }
829 }
830#endif
831
drh3c657212009-11-17 23:59:58 +0000832 /* Sanity checking on other operands */
833#ifdef SQLITE_DEBUG
drh7cc84c22016-04-11 13:36:42 +0000834 {
835 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
836 if( (opProperty & OPFLG_IN1)!=0 ){
837 assert( pOp->p1>0 );
838 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
839 assert( memIsValid(&aMem[pOp->p1]) );
840 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
841 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
842 }
843 if( (opProperty & OPFLG_IN2)!=0 ){
844 assert( pOp->p2>0 );
845 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
846 assert( memIsValid(&aMem[pOp->p2]) );
847 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
848 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
849 }
850 if( (opProperty & OPFLG_IN3)!=0 ){
851 assert( pOp->p3>0 );
852 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
853 assert( memIsValid(&aMem[pOp->p3]) );
854 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
855 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
856 }
857 if( (opProperty & OPFLG_OUT2)!=0 ){
858 assert( pOp->p2>0 );
859 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
860 memAboutToChange(p, &aMem[pOp->p2]);
861 }
862 if( (opProperty & OPFLG_OUT3)!=0 ){
863 assert( pOp->p3>0 );
864 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
865 memAboutToChange(p, &aMem[pOp->p3]);
866 }
drh3c657212009-11-17 23:59:58 +0000867 }
868#endif
drh6dc41482015-04-16 17:31:02 +0000869#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
870 pOrigOp = pOp;
871#endif
drh93952eb2009-11-13 19:43:43 +0000872
drh75897232000-05-29 14:26:00 +0000873 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000874
drh5e00f6c2001-09-13 13:46:56 +0000875/*****************************************************************************
876** What follows is a massive switch statement where each case implements a
877** separate instruction in the virtual machine. If we follow the usual
878** indentation conventions, each case should be indented by 6 spaces. But
879** that is a lot of wasted space on the left margin. So the code within
880** the switch statement will break with convention and be flush-left. Another
881** big comment (similar to this one) will mark the point in the code where
882** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000883**
884** The formatting of each case is important. The makefile for SQLite
885** generates two C files "opcodes.h" and "opcodes.c" by scanning this
886** file looking for lines that begin with "case OP_". The opcodes.h files
887** will be filled with #defines that give unique integer values to each
888** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000889** each string is the symbolic name for the corresponding opcode. If the
890** case statement is followed by a comment of the form "/# same as ... #/"
891** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000892**
drh9cbf3422008-01-17 16:22:13 +0000893** Other keywords in the comment that follows each case are used to
894** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
drh27a348c2015-04-13 19:14:06 +0000895** Keywords include: in1, in2, in3, out2, out3. See
drh9cbf3422008-01-17 16:22:13 +0000896** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000897**
drhac82fcf2002-09-08 17:23:41 +0000898** Documentation about VDBE opcodes is generated by scanning this file
899** for lines of that contain "Opcode:". That line and all subsequent
900** comment lines are used in the generation of the opcode.html documentation
901** file.
902**
903** SUMMARY:
904**
905** Formatting is important to scripts that scan this file.
906** Do not deviate from the formatting style currently in use.
907**
drh5e00f6c2001-09-13 13:46:56 +0000908*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000909
drh9cbf3422008-01-17 16:22:13 +0000910/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000911**
912** An unconditional jump to address P2.
913** The next instruction executed will be
914** the one at index P2 from the beginning of
915** the program.
drhfe705102014-03-06 13:38:37 +0000916**
917** The P1 parameter is not actually used by this opcode. However, it
918** is sometimes set to 1 instead of 0 as a hint to the command-line shell
919** that this Goto is the bottom of a loop and that the lines from P2 down
920** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000921*/
drh9cbf3422008-01-17 16:22:13 +0000922case OP_Goto: { /* jump */
drhd9670ab2019-12-28 01:52:46 +0000923
924#ifdef SQLITE_DEBUG
925 /* In debuggging mode, when the p5 flags is set on an OP_Goto, that
926 ** means we should really jump back to the preceeding OP_ReleaseReg
927 ** instruction. */
928 if( pOp->p5 ){
929 assert( pOp->p2 < (int)(pOp - aOp) );
930 assert( pOp->p2 > 1 );
931 pOp = &aOp[pOp->p2 - 2];
932 assert( pOp[1].opcode==OP_ReleaseReg );
933 goto check_for_interrupt;
934 }
935#endif
936
drhf56fa462015-04-13 21:39:54 +0000937jump_to_p2_and_check_for_interrupt:
938 pOp = &aOp[pOp->p2 - 1];
drh49afe3a2013-07-10 03:05:14 +0000939
940 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
drhbb6783b2017-04-29 18:02:49 +0000941 ** OP_VNext, or OP_SorterNext) all jump here upon
drh49afe3a2013-07-10 03:05:14 +0000942 ** completion. Check to see if sqlite3_interrupt() has been called
943 ** or if the progress callback needs to be invoked.
944 **
945 ** This code uses unstructured "goto" statements and does not look clean.
946 ** But that is not due to sloppy coding habits. The code is written this
947 ** way for performance, to avoid having to run the interrupt and progress
948 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
949 ** faster according to "valgrind --tool=cachegrind" */
950check_for_interrupt:
dan892edb62020-03-30 13:35:05 +0000951 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000952#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
953 /* Call the progress callback if it is configured and the required number
954 ** of VDBE ops have been executed (either since this invocation of
955 ** sqlite3VdbeExec() or since last time the progress callback was called).
956 ** If the progress callback returns non-zero, exit the virtual machine with
957 ** a return code SQLITE_ABORT.
958 */
drhb1af9c62019-02-20 13:55:45 +0000959 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
drh400fcba2013-11-14 00:09:48 +0000960 assert( db->nProgressOps!=0 );
drhb1af9c62019-02-20 13:55:45 +0000961 nProgressLimit += db->nProgressOps;
drh400fcba2013-11-14 00:09:48 +0000962 if( db->xProgress(db->pProgressArg) ){
drhd1d89142020-07-06 12:13:05 +0000963 nProgressLimit = LARGEST_UINT64;
drh49afe3a2013-07-10 03:05:14 +0000964 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +0000965 goto abort_due_to_error;
drh49afe3a2013-07-10 03:05:14 +0000966 }
drh49afe3a2013-07-10 03:05:14 +0000967 }
968#endif
969
drh5e00f6c2001-09-13 13:46:56 +0000970 break;
971}
drh75897232000-05-29 14:26:00 +0000972
drh2eb95372008-06-06 15:04:36 +0000973/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000974**
drh2eb95372008-06-06 15:04:36 +0000975** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000976** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000977*/
drhb8475df2011-12-09 16:21:19 +0000978case OP_Gosub: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000979 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000980 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000981 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000982 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000983 pIn1->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000984 pIn1->u.i = (int)(pOp-aOp);
drh2eb95372008-06-06 15:04:36 +0000985 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000986
987 /* Most jump operations do a goto to this spot in order to update
988 ** the pOp pointer. */
989jump_to_p2:
990 pOp = &aOp[pOp->p2 - 1];
drh8c74a8c2002-08-25 19:20:40 +0000991 break;
992}
993
drhe603ab02022-04-07 19:06:31 +0000994/* Opcode: Return P1 P2 P3 * *
drh8c74a8c2002-08-25 19:20:40 +0000995**
drh6134b2d2022-04-11 17:27:38 +0000996** Jump to the next instruction after the address stored in register P1.
997**
998** It used to be that after the jump, register P1 would become undefined.
999** However, for the subroutine used for the inner loop of a RIGHT JOIN,
1000** it is useful for R1 register to be unchanged, so that is what happens
1001** now.
drh19025162022-03-03 15:00:44 +00001002**
drhe603ab02022-04-07 19:06:31 +00001003** P2 is not used by the byte-code engine. However, if P2 is positive
1004** and also less than the current address, then the "EXPLAIN" output
1005** formatter in the CLI will indent all opcodes from the P2 opcode up
1006** to be not including the current Return. P2 should be the first opcode
1007** in the subroutine from which this opcode is returnning. Thus the P2
1008** value is a byte-code indentation hint. See tag-20220407a in
1009** wherecode.c and shell.c.
1010**
drh19025162022-03-03 15:00:44 +00001011** P3 is not used by the byte-code engine. However, the code generator
1012** sets P3 to address of the associated OP_BeginSubrtn opcode, if there is
1013** one.
drh8c74a8c2002-08-25 19:20:40 +00001014*/
drh2eb95372008-06-06 15:04:36 +00001015case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001016 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +00001017 assert( pIn1->flags==MEM_Int );
drhf56fa462015-04-13 21:39:54 +00001018 pOp = &aOp[pIn1->u.i];
drh6134b2d2022-04-11 17:27:38 +00001019 /* pIn1->flags = MEM_Undefined; */
drh8c74a8c2002-08-25 19:20:40 +00001020 break;
1021}
1022
drhed71a832014-02-07 19:18:10 +00001023/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +00001024**
drh5dad9a32014-07-25 18:37:42 +00001025** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +00001026** located at address P3.
1027**
drh5dad9a32014-07-25 18:37:42 +00001028** If P2!=0 then the coroutine implementation immediately follows
1029** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +00001030** address P2.
drh5dad9a32014-07-25 18:37:42 +00001031**
1032** See also: EndCoroutine
drh81cf13e2014-02-07 18:27:53 +00001033*/
1034case OP_InitCoroutine: { /* jump */
drh9f6168b2016-03-19 23:32:58 +00001035 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drhed71a832014-02-07 19:18:10 +00001036 assert( pOp->p2>=0 && pOp->p2<p->nOp );
1037 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +00001038 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +00001039 assert( !VdbeMemDynamic(pOut) );
1040 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +00001041 pOut->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +00001042 if( pOp->p2 ) goto jump_to_p2;
drh81cf13e2014-02-07 18:27:53 +00001043 break;
1044}
1045
1046/* Opcode: EndCoroutine P1 * * * *
1047**
drhbc5cf382014-08-06 01:08:07 +00001048** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +00001049** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +00001050** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +00001051**
1052** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +00001053*/
1054case OP_EndCoroutine: { /* in1 */
1055 VdbeOp *pCaller;
1056 pIn1 = &aMem[pOp->p1];
1057 assert( pIn1->flags==MEM_Int );
1058 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
1059 pCaller = &aOp[pIn1->u.i];
1060 assert( pCaller->opcode==OP_Yield );
1061 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
drhf56fa462015-04-13 21:39:54 +00001062 pOp = &aOp[pCaller->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +00001063 pIn1->flags = MEM_Undefined;
1064 break;
1065}
1066
1067/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +00001068**
drh5dad9a32014-07-25 18:37:42 +00001069** Swap the program counter with the value in register P1. This
1070** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +00001071**
drh5dad9a32014-07-25 18:37:42 +00001072** If the coroutine that is launched by this instruction ends with
1073** Yield or Return then continue to the next instruction. But if
1074** the coroutine launched by this instruction ends with
1075** EndCoroutine, then jump to P2 rather than continuing with the
1076** next instruction.
1077**
1078** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +00001079*/
drh81cf13e2014-02-07 18:27:53 +00001080case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +00001081 int pcDest;
drh3c657212009-11-17 23:59:58 +00001082 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +00001083 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +00001084 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +00001085 pcDest = (int)pIn1->u.i;
drhf56fa462015-04-13 21:39:54 +00001086 pIn1->u.i = (int)(pOp - aOp);
drhe00ee6e2008-06-20 15:24:01 +00001087 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +00001088 pOp = &aOp[pcDest];
drhe00ee6e2008-06-20 15:24:01 +00001089 break;
1090}
1091
drhf9c8ce32013-11-05 13:33:55 +00001092/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00001093** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +00001094**
drhef8662b2011-06-20 21:47:58 +00001095** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +00001096** parameter P1, P2, and P4 as if this were a Halt instruction. If the
1097** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +00001098** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +00001099*/
1100case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +00001101 pIn3 = &aMem[pOp->p3];
drh4031baf2018-05-28 17:31:20 +00001102#ifdef SQLITE_DEBUG
1103 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
1104#endif
drh5053a792009-02-20 03:02:23 +00001105 if( (pIn3->flags & MEM_Null)==0 ) break;
1106 /* Fall through into OP_Halt */
drh08b92082020-08-10 14:18:00 +00001107 /* no break */ deliberate_fall_through
drh5053a792009-02-20 03:02:23 +00001108}
drhe00ee6e2008-06-20 15:24:01 +00001109
drhf9c8ce32013-11-05 13:33:55 +00001110/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001111**
drh3d4501e2008-12-04 20:40:10 +00001112** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +00001113** automatically.
drhb19a2bc2001-09-16 00:13:26 +00001114**
drh92f02c32004-09-02 14:57:08 +00001115** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
1116** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
1117** For errors, it can be some other value. If P1!=0 then P2 will determine
1118** whether or not to rollback the current transaction. Do not rollback
1119** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
1120** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +00001121** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +00001122**
drh66a51672008-01-03 00:01:23 +00001123** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +00001124**
drhf9c8ce32013-11-05 13:33:55 +00001125** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
1126**
1127** 0: (no change)
1128** 1: NOT NULL contraint failed: P4
1129** 2: UNIQUE constraint failed: P4
1130** 3: CHECK constraint failed: P4
1131** 4: FOREIGN KEY constraint failed: P4
1132**
1133** If P5 is not zero and P4 is NULL, then everything after the ":" is
1134** omitted.
1135**
drh9cfcf5d2002-01-29 18:41:24 +00001136** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +00001137** every program. So a jump past the last instruction of the program
1138** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +00001139*/
drh9cbf3422008-01-17 16:22:13 +00001140case OP_Halt: {
drhf56fa462015-04-13 21:39:54 +00001141 VdbeFrame *pFrame;
1142 int pcx;
drhf9c8ce32013-11-05 13:33:55 +00001143
drh4031baf2018-05-28 17:31:20 +00001144#ifdef SQLITE_DEBUG
1145 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
1146#endif
drh8bb93da2022-04-03 20:39:48 +00001147 if( p->pFrame && pOp->p1==SQLITE_OK ){
dan2832ad42009-08-31 15:27:27 +00001148 /* Halt the sub-program. Return control to the parent frame. */
drhf56fa462015-04-13 21:39:54 +00001149 pFrame = p->pFrame;
dan165921a2009-08-28 18:53:45 +00001150 p->pFrame = pFrame->pParent;
1151 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +00001152 sqlite3VdbeSetChanges(db, p->nChange);
drhf56fa462015-04-13 21:39:54 +00001153 pcx = sqlite3VdbeFrameRestore(pFrame);
dan165921a2009-08-28 18:53:45 +00001154 if( pOp->p2==OE_Ignore ){
drhf56fa462015-04-13 21:39:54 +00001155 /* Instruction pcx is the OP_Program that invoked the sub-program
dan2832ad42009-08-31 15:27:27 +00001156 ** currently being halted. If the p2 instruction of this OP_Halt
1157 ** instruction is set to OE_Ignore, then the sub-program is throwing
1158 ** an IGNORE exception. In this case jump to the address specified
1159 ** as the p2 of the calling OP_Program. */
drhf56fa462015-04-13 21:39:54 +00001160 pcx = p->aOp[pcx].p2-1;
dan165921a2009-08-28 18:53:45 +00001161 }
drhbbe879d2009-11-14 18:04:35 +00001162 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +00001163 aMem = p->aMem;
drhf56fa462015-04-13 21:39:54 +00001164 pOp = &aOp[pcx];
dan165921a2009-08-28 18:53:45 +00001165 break;
1166 }
drh92f02c32004-09-02 14:57:08 +00001167 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +00001168 p->errorAction = (u8)pOp->p2;
drhfb4e3a32016-12-30 00:09:14 +00001169 assert( pOp->p5<=4 );
drhf9c8ce32013-11-05 13:33:55 +00001170 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +00001171 if( pOp->p5 ){
1172 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
1173 "FOREIGN KEY" };
drhd9b7ec92013-11-06 14:05:21 +00001174 testcase( pOp->p5==1 );
1175 testcase( pOp->p5==2 );
1176 testcase( pOp->p5==3 );
1177 testcase( pOp->p5==4 );
drh99f5de72016-04-30 02:59:15 +00001178 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
1179 if( pOp->p4.z ){
1180 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
1181 }
drhd9b7ec92013-11-06 14:05:21 +00001182 }else{
drh22c17b82015-05-15 04:13:15 +00001183 sqlite3VdbeError(p, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +00001184 }
drh8bb93da2022-04-03 20:39:48 +00001185 pcx = (int)(pOp - aOp);
drh99f5de72016-04-30 02:59:15 +00001186 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +00001187 }
drh92f02c32004-09-02 14:57:08 +00001188 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +00001189 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +00001190 if( rc==SQLITE_BUSY ){
drh99f5de72016-04-30 02:59:15 +00001191 p->rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00001192 }else{
drhd91c1a12013-02-09 13:58:25 +00001193 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
dancb3e4b72013-07-03 19:53:05 +00001194 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +00001195 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +00001196 }
drh900b31e2007-08-28 02:27:51 +00001197 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +00001198}
drhc61053b2000-06-04 12:58:36 +00001199
drh19025162022-03-03 15:00:44 +00001200/* Opcode: BeginSubrtn P1 P2 * * *
1201** Synopsis: r[P2]=P1
1202**
1203** Mark the beginning of a subroutine by loading the integer value P1
1204** into register r[P2]. The P2 register is used to store the return
1205** address of the subroutine call.
1206**
1207** This opcode is identical to OP_Integer. It has a different name
1208** only to make the byte code easier to read and verify.
1209*/
drh4c583122008-01-04 22:01:03 +00001210/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001211** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +00001212**
drh9cbf3422008-01-17 16:22:13 +00001213** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +00001214*/
drh19025162022-03-03 15:00:44 +00001215case OP_BeginSubrtn:
drh27a348c2015-04-13 19:14:06 +00001216case OP_Integer: { /* out2 */
1217 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001218 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +00001219 break;
1220}
1221
drh4c583122008-01-04 22:01:03 +00001222/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001223** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +00001224**
drh66a51672008-01-03 00:01:23 +00001225** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +00001226** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +00001227*/
drh27a348c2015-04-13 19:14:06 +00001228case OP_Int64: { /* out2 */
1229 pOut = out2Prerelease(p, pOp);
danielk19772dca4ac2008-01-03 11:50:29 +00001230 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +00001231 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +00001232 break;
1233}
drh4f26d6c2004-05-26 23:25:30 +00001234
drh13573c72010-01-12 17:04:07 +00001235#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001236/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001237** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001238**
drh4c583122008-01-04 22:01:03 +00001239** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001240** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001241*/
drh27a348c2015-04-13 19:14:06 +00001242case OP_Real: { /* same as TK_FLOAT, out2 */
1243 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001244 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001245 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001246 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001247 break;
1248}
drh13573c72010-01-12 17:04:07 +00001249#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001250
drh3c84ddf2008-01-09 02:15:38 +00001251/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001252** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001253**
drh66a51672008-01-03 00:01:23 +00001254** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhf07cf6e2015-03-06 16:45:16 +00001255** into a String opcode before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001256** this transformation, the length of string P4 is computed and stored
1257** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001258*/
drh27a348c2015-04-13 19:14:06 +00001259case OP_String8: { /* same as TK_STRING, out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001260 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001261 pOut = out2Prerelease(p, pOp);
drhea678832008-12-10 19:26:22 +00001262 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001263
1264#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001265 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001266 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
drh2f555112016-04-30 18:10:34 +00001267 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
drhdbdddc92019-02-21 16:41:34 +00001268 if( rc ) goto too_big;
drh4c583122008-01-04 22:01:03 +00001269 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001270 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001271 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001272 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001273 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001274 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001275 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001276 }
drh66a51672008-01-03 00:01:23 +00001277 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001278 pOp->p4.z = pOut->z;
1279 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001280 }
danielk197793758c82005-01-21 08:13:14 +00001281#endif
drhbb4957f2008-03-20 14:03:29 +00001282 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001283 goto too_big;
1284 }
drhec722c12019-09-17 21:28:54 +00001285 pOp->opcode = OP_String;
drh2f555112016-04-30 18:10:34 +00001286 assert( rc==SQLITE_OK );
drhcbd2da92007-12-17 16:20:06 +00001287 /* Fall through to the next case, OP_String */
drh08b92082020-08-10 14:18:00 +00001288 /* no break */ deliberate_fall_through
danielk1977cbb18d22004-05-28 11:37:27 +00001289}
drhf4479502004-05-27 03:12:53 +00001290
drhf07cf6e2015-03-06 16:45:16 +00001291/* Opcode: String P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00001292** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001293**
drh9cbf3422008-01-17 16:22:13 +00001294** The string value P4 of length P1 (bytes) is stored in register P2.
drhf07cf6e2015-03-06 16:45:16 +00001295**
drh44aebff2016-05-02 10:25:42 +00001296** If P3 is not zero and the content of register P3 is equal to P5, then
drha9c18a92015-03-06 20:49:52 +00001297** the datatype of the register P2 is converted to BLOB. The content is
1298** the same sequence of bytes, it is merely interpreted as a BLOB instead
drh44aebff2016-05-02 10:25:42 +00001299** of a string, as if it had been CAST. In other words:
1300**
1301** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
drhf4479502004-05-27 03:12:53 +00001302*/
drh27a348c2015-04-13 19:14:06 +00001303case OP_String: { /* out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001304 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001305 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001306 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1307 pOut->z = pOp->p4.z;
1308 pOut->n = pOp->p1;
1309 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001310 UPDATE_MAX_BLOBSIZE(pOut);
drh41d2e662015-12-01 21:23:07 +00001311#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh44aebff2016-05-02 10:25:42 +00001312 if( pOp->p3>0 ){
drh9f6168b2016-03-19 23:32:58 +00001313 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhf07cf6e2015-03-06 16:45:16 +00001314 pIn3 = &aMem[pOp->p3];
1315 assert( pIn3->flags & MEM_Int );
drh44aebff2016-05-02 10:25:42 +00001316 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
drhf07cf6e2015-03-06 16:45:16 +00001317 }
drh41d2e662015-12-01 21:23:07 +00001318#endif
danielk1977c572ef72004-05-27 09:28:41 +00001319 break;
1320}
1321
drh053a1282012-09-19 21:15:46 +00001322/* Opcode: Null P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001323** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001324**
drhb8475df2011-12-09 16:21:19 +00001325** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001326** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001327** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001328** set to NULL.
1329**
1330** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1331** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1332** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001333*/
drh27a348c2015-04-13 19:14:06 +00001334case OP_Null: { /* out2 */
drhb8475df2011-12-09 16:21:19 +00001335 int cnt;
drh053a1282012-09-19 21:15:46 +00001336 u16 nullFlag;
drh27a348c2015-04-13 19:14:06 +00001337 pOut = out2Prerelease(p, pOp);
drhb8475df2011-12-09 16:21:19 +00001338 cnt = pOp->p3-pOp->p2;
drh9f6168b2016-03-19 23:32:58 +00001339 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001340 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drh2a1df932016-09-30 17:46:44 +00001341 pOut->n = 0;
drh2c885d02018-07-07 19:36:04 +00001342#ifdef SQLITE_DEBUG
1343 pOut->uTemp = 0;
1344#endif
drhb8475df2011-12-09 16:21:19 +00001345 while( cnt>0 ){
1346 pOut++;
1347 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001348 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001349 pOut->flags = nullFlag;
drh2a1df932016-09-30 17:46:44 +00001350 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001351 cnt--;
1352 }
drhf0863fe2005-06-12 21:35:51 +00001353 break;
1354}
1355
drh05a86c52014-02-16 01:55:49 +00001356/* Opcode: SoftNull P1 * * * *
drh72e26de2016-08-24 21:24:04 +00001357** Synopsis: r[P1]=NULL
drh05a86c52014-02-16 01:55:49 +00001358**
1359** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1360** instruction, but do not free any string or blob memory associated with
1361** the register, so that if the value was a string or blob that was
1362** previously copied using OP_SCopy, the copies will continue to be valid.
1363*/
1364case OP_SoftNull: {
drh9f6168b2016-03-19 23:32:58 +00001365 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh05a86c52014-02-16 01:55:49 +00001366 pOut = &aMem[pOp->p1];
drhe2bc6552017-04-17 20:50:34 +00001367 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
drh05a86c52014-02-16 01:55:49 +00001368 break;
1369}
drhf0863fe2005-06-12 21:35:51 +00001370
drha5750cf2014-02-07 13:20:31 +00001371/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001372** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001373**
drh9de221d2008-01-05 06:51:30 +00001374** P4 points to a blob of data P1 bytes long. Store this
drh50fb7e02021-12-06 20:16:53 +00001375** blob in register P2. If P4 is a NULL pointer, then construct
1376** a zero-filled blob that is P1 bytes long in P2.
danielk1977c572ef72004-05-27 09:28:41 +00001377*/
drh27a348c2015-04-13 19:14:06 +00001378case OP_Blob: { /* out2 */
drhcbd2da92007-12-17 16:20:06 +00001379 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh27a348c2015-04-13 19:14:06 +00001380 pOut = out2Prerelease(p, pOp);
drh50fb7e02021-12-06 20:16:53 +00001381 if( pOp->p4.z==0 ){
1382 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1);
1383 if( sqlite3VdbeMemExpandBlob(pOut) ) goto no_mem;
1384 }else{
1385 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
1386 }
drh9de221d2008-01-05 06:51:30 +00001387 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001388 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001389 break;
1390}
1391
drheaf52d82010-05-12 13:50:23 +00001392/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001393** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001394**
drheaf52d82010-05-12 13:50:23 +00001395** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001396**
drh0fd61352014-02-07 02:29:45 +00001397** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001398** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001399*/
drh27a348c2015-04-13 19:14:06 +00001400case OP_Variable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00001401 Mem *pVar; /* Value being transferred */
1402
drheaf52d82010-05-12 13:50:23 +00001403 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh9bf755c2016-12-23 03:59:31 +00001404 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
drheaf52d82010-05-12 13:50:23 +00001405 pVar = &p->aVar[pOp->p1 - 1];
1406 if( sqlite3VdbeMemTooBig(pVar) ){
1407 goto too_big;
drh023ae032007-05-08 12:12:16 +00001408 }
drh7441df72017-01-09 19:27:04 +00001409 pOut = &aMem[pOp->p2];
drhe0f20b42019-04-01 20:57:11 +00001410 if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
1411 memcpy(pOut, pVar, MEMCELLSIZE);
1412 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
1413 pOut->flags |= MEM_Static|MEM_FromBind;
drheaf52d82010-05-12 13:50:23 +00001414 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001415 break;
1416}
danielk1977295ba552004-05-19 10:34:51 +00001417
drhb21e7c72008-06-22 12:37:57 +00001418/* Opcode: Move P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001419** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001420**
drh079a3072014-03-19 14:10:55 +00001421** Move the P3 values in register P1..P1+P3-1 over into
1422** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001423** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001424** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1425** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001426*/
drhe1349cb2008-04-01 00:36:10 +00001427case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001428 int n; /* Number of registers left to copy */
1429 int p1; /* Register to copy from */
1430 int p2; /* Register to copy to */
1431
drhe09f43f2013-11-21 04:18:31 +00001432 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001433 p1 = pOp->p1;
1434 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001435 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001436 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001437
drha6c2ed92009-11-14 23:22:23 +00001438 pIn1 = &aMem[p1];
1439 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001440 do{
drh9f6168b2016-03-19 23:32:58 +00001441 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
1442 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001443 assert( memIsValid(pIn1) );
1444 memAboutToChange(p, pOut);
drh17bcb102014-09-18 21:25:33 +00001445 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001446#ifdef SQLITE_DEBUG
drh4cbd8472020-01-02 15:02:08 +00001447 pIn1->pScopyFrom = 0;
1448 { int i;
1449 for(i=1; i<p->nMem; i++){
1450 if( aMem[i].pScopyFrom==pIn1 ){
1451 aMem[i].pScopyFrom = pOut;
1452 }
1453 }
drh52043d72011-08-03 16:40:15 +00001454 }
1455#endif
drhbd6789e2015-04-28 14:00:02 +00001456 Deephemeralize(pOut);
drhb21e7c72008-06-22 12:37:57 +00001457 REGISTER_TRACE(p2++, pOut);
1458 pIn1++;
1459 pOut++;
drh079a3072014-03-19 14:10:55 +00001460 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001461 break;
1462}
1463
drhe8e4af72012-09-21 00:04:28 +00001464/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001465** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001466**
drhe8e4af72012-09-21 00:04:28 +00001467** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001468**
1469** This instruction makes a deep copy of the value. A duplicate
1470** is made of any string or blob constant. See also OP_SCopy.
1471*/
drhe8e4af72012-09-21 00:04:28 +00001472case OP_Copy: {
1473 int n;
1474
1475 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001476 pIn1 = &aMem[pOp->p1];
1477 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001478 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001479 while( 1 ){
drh58773a52018-06-12 13:52:23 +00001480 memAboutToChange(p, pOut);
drhe8e4af72012-09-21 00:04:28 +00001481 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1482 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001483#ifdef SQLITE_DEBUG
1484 pOut->pScopyFrom = 0;
1485#endif
drhe8e4af72012-09-21 00:04:28 +00001486 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1487 if( (n--)==0 ) break;
1488 pOut++;
1489 pIn1++;
1490 }
drhe1349cb2008-04-01 00:36:10 +00001491 break;
1492}
1493
drhb1fdb2a2008-01-05 04:06:03 +00001494/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001495** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001496**
drh9cbf3422008-01-17 16:22:13 +00001497** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001498**
1499** This instruction makes a shallow copy of the value. If the value
1500** is a string or blob, then the copy is only a pointer to the
1501** original and hence if the original changes so will the copy.
1502** Worse, if the original is deallocated, the copy becomes invalid.
1503** Thus the program must guarantee that the original will not change
1504** during the lifetime of the copy. Use OP_Copy to make a complete
1505** copy.
1506*/
drh26198bb2013-10-31 11:15:09 +00001507case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001508 pIn1 = &aMem[pOp->p1];
1509 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001510 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001511 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001512#ifdef SQLITE_DEBUG
drh58773a52018-06-12 13:52:23 +00001513 pOut->pScopyFrom = pIn1;
1514 pOut->mScopyFlags = pIn1->flags;
drh2b4ded92010-09-27 21:09:31 +00001515#endif
drh5e00f6c2001-09-13 13:46:56 +00001516 break;
1517}
drh75897232000-05-29 14:26:00 +00001518
drhfed7ac62015-10-15 18:04:59 +00001519/* Opcode: IntCopy P1 P2 * * *
1520** Synopsis: r[P2]=r[P1]
1521**
1522** Transfer the integer value held in register P1 into register P2.
1523**
1524** This is an optimized version of SCopy that works only for integer
1525** values.
1526*/
1527case OP_IntCopy: { /* out2 */
1528 pIn1 = &aMem[pOp->p1];
1529 assert( (pIn1->flags & MEM_Int)!=0 );
1530 pOut = &aMem[pOp->p2];
1531 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1532 break;
1533}
1534
drh3b26b2b2021-12-01 19:17:14 +00001535/* Opcode: FkCheck * * * * *
drh18e56072021-01-31 15:50:36 +00001536**
drh3b26b2b2021-12-01 19:17:14 +00001537** Halt with an SQLITE_CONSTRAINT error if there are any unresolved
1538** foreign key constraint violations. If there are no foreign key
1539** constraint violations, this is a no-op.
drh18e56072021-01-31 15:50:36 +00001540**
drh3b26b2b2021-12-01 19:17:14 +00001541** FK constraint violations are also checked when the prepared statement
1542** exits. This opcode is used to raise foreign key constraint errors prior
1543** to returning results such as a row change count or the result of a
1544** RETURNING clause.
drh18e56072021-01-31 15:50:36 +00001545*/
drh3b26b2b2021-12-01 19:17:14 +00001546case OP_FkCheck: {
drh18e56072021-01-31 15:50:36 +00001547 if( (rc = sqlite3VdbeCheckFk(p,0))!=SQLITE_OK ){
1548 goto abort_due_to_error;
1549 }
drh3b26b2b2021-12-01 19:17:14 +00001550 break;
drh18e56072021-01-31 15:50:36 +00001551}
1552
drh9cbf3422008-01-17 16:22:13 +00001553/* Opcode: ResultRow P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001554** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001555**
shane21e7feb2008-05-30 15:59:49 +00001556** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001557** results. This opcode causes the sqlite3_step() call to terminate
1558** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001559** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001560** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001561*/
drh9cbf3422008-01-17 16:22:13 +00001562case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001563 assert( p->nResColumn==pOp->p2 );
drh9ce612a2021-04-05 22:30:56 +00001564 assert( pOp->p1>0 || CORRUPT_DB );
drh9f6168b2016-03-19 23:32:58 +00001565 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001566
drhd4e70eb2008-01-02 00:34:36 +00001567 p->cacheCtr = (p->cacheCtr + 2)|1;
drh3b8b5be2022-04-01 20:19:36 +00001568 p->pResultSet = &aMem[pOp->p1];
drh02ff7472019-12-31 12:18:24 +00001569#ifdef SQLITE_DEBUG
drh3b8b5be2022-04-01 20:19:36 +00001570 {
1571 Mem *pMem = p->pResultSet;
1572 int i;
1573 for(i=0; i<pOp->p2; i++){
1574 assert( memIsValid(&pMem[i]) );
1575 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
1576 /* The registers in the result will not be used again when the
1577 ** prepared statement restarts. This is because sqlite3_column()
1578 ** APIs might have caused type conversions of made other changes to
1579 ** the register values. Therefore, we can go ahead and break any
1580 ** OP_SCopy dependencies. */
1581 pMem[i].pScopyFrom = 0;
1582 }
drhd4e70eb2008-01-02 00:34:36 +00001583 }
drh3b8b5be2022-04-01 20:19:36 +00001584#endif
drh28039692008-03-17 16:54:01 +00001585 if( db->mallocFailed ) goto no_mem;
drh3d2a5292016-07-13 22:55:01 +00001586 if( db->mTrace & SQLITE_TRACE_ROW ){
drh08b92082020-08-10 14:18:00 +00001587 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
drh3d2a5292016-07-13 22:55:01 +00001588 }
drhf56fa462015-04-13 21:39:54 +00001589 p->pc = (int)(pOp - aOp) + 1;
drhd4e70eb2008-01-02 00:34:36 +00001590 rc = SQLITE_ROW;
1591 goto vdbe_return;
1592}
1593
drh5b6afba2008-01-05 16:29:28 +00001594/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001595** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001596**
drh5b6afba2008-01-05 16:29:28 +00001597** Add the text in register P1 onto the end of the text in
1598** register P2 and store the result in register P3.
1599** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001600**
1601** P3 = P2 || P1
1602**
1603** It is illegal for P1 and P3 to be the same register. Sometimes,
1604** if P3 is the same register as P2, the implementation is able
1605** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001606*/
drh5b6afba2008-01-05 16:29:28 +00001607case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh8a7e11f2019-05-01 15:32:40 +00001608 i64 nByte; /* Total size of the output string or blob */
1609 u16 flags1; /* Initial flags for P1 */
1610 u16 flags2; /* Initial flags for P2 */
danielk19778a6b5412004-05-24 07:04:25 +00001611
drh3c657212009-11-17 23:59:58 +00001612 pIn1 = &aMem[pOp->p1];
1613 pIn2 = &aMem[pOp->p2];
1614 pOut = &aMem[pOp->p3];
drh8a7e11f2019-05-01 15:32:40 +00001615 testcase( pOut==pIn2 );
danielk1977a7a8e142008-02-13 18:25:27 +00001616 assert( pIn1!=pOut );
drh8a7e11f2019-05-01 15:32:40 +00001617 flags1 = pIn1->flags;
1618 testcase( flags1 & MEM_Null );
1619 testcase( pIn2->flags & MEM_Null );
1620 if( (flags1 | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001621 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001622 break;
drh5e00f6c2001-09-13 13:46:56 +00001623 }
drh8a7e11f2019-05-01 15:32:40 +00001624 if( (flags1 & (MEM_Str|MEM_Blob))==0 ){
1625 if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001626 flags1 = pIn1->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001627 }else if( (flags1 & MEM_Zero)!=0 ){
1628 if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001629 flags1 = pIn1->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001630 }
1631 flags2 = pIn2->flags;
1632 if( (flags2 & (MEM_Str|MEM_Blob))==0 ){
1633 if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001634 flags2 = pIn2->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001635 }else if( (flags2 & MEM_Zero)!=0 ){
1636 if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001637 flags2 = pIn2->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001638 }
drh5b6afba2008-01-05 16:29:28 +00001639 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001640 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001641 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001642 }
drhdf82afc2019-05-16 01:22:21 +00001643 if( sqlite3VdbeMemGrow(pOut, (int)nByte+3, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001644 goto no_mem;
1645 }
drhc91b2fd2014-03-01 18:13:23 +00001646 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001647 if( pOut!=pIn2 ){
1648 memcpy(pOut->z, pIn2->z, pIn2->n);
drh8a7e11f2019-05-01 15:32:40 +00001649 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
1650 pIn2->flags = flags2;
danielk1977a7a8e142008-02-13 18:25:27 +00001651 }
1652 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh8a7e11f2019-05-01 15:32:40 +00001653 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
1654 pIn1->flags = flags1;
drh81316f82013-10-29 20:40:47 +00001655 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001656 pOut->z[nByte+1] = 0;
drhdf82afc2019-05-16 01:22:21 +00001657 pOut->z[nByte+2] = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001658 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001659 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001660 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001661 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001662 break;
1663}
drh75897232000-05-29 14:26:00 +00001664
drh3c84ddf2008-01-09 02:15:38 +00001665/* Opcode: Add P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001666** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001667**
drh60a713c2008-01-21 16:22:45 +00001668** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001669** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001670** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001671*/
drh3c84ddf2008-01-09 02:15:38 +00001672/* Opcode: Multiply P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001673** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001674**
drh3c84ddf2008-01-09 02:15:38 +00001675**
shane21e7feb2008-05-30 15:59:49 +00001676** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001677** and store the result in register P3.
1678** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001679*/
drh3c84ddf2008-01-09 02:15:38 +00001680/* Opcode: Subtract P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001681** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001682**
drh60a713c2008-01-21 16:22:45 +00001683** Subtract the value in register P1 from the value in register P2
1684** and store the result in register P3.
1685** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001686*/
drh9cbf3422008-01-17 16:22:13 +00001687/* Opcode: Divide P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001688** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001689**
drh60a713c2008-01-21 16:22:45 +00001690** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001691** and store the result in register P3 (P3=P2/P1). If the value in
1692** register P1 is zero, then the result is NULL. If either input is
1693** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001694*/
drh9cbf3422008-01-17 16:22:13 +00001695/* Opcode: Remainder P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001696** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001697**
drh40864a12013-11-15 18:58:37 +00001698** Compute the remainder after integer register P2 is divided by
1699** register P1 and store the result in register P3.
1700** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001701** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001702*/
drh5b6afba2008-01-05 16:29:28 +00001703case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1704case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1705case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1706case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1707case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drh3d1d90a2014-03-24 15:00:15 +00001708 u16 flags; /* Combined MEM_* flags from both inputs */
1709 u16 type1; /* Numeric type of left operand */
1710 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001711 i64 iA; /* Integer value of left operand */
1712 i64 iB; /* Integer value of right operand */
1713 double rA; /* Real value of left operand */
1714 double rB; /* Real value of right operand */
1715
drh3c657212009-11-17 23:59:58 +00001716 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001717 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001718 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001719 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001720 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001721 flags = pIn1->flags | pIn2->flags;
drh3d1d90a2014-03-24 15:00:15 +00001722 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001723 iA = pIn1->u.i;
1724 iB = pIn2->u.i;
drh5e00f6c2001-09-13 13:46:56 +00001725 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001726 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1727 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1728 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001729 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001730 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001731 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001732 iB /= iA;
drh75897232000-05-29 14:26:00 +00001733 break;
1734 }
drhbf4133c2001-10-13 02:59:08 +00001735 default: {
drh856c1032009-06-02 15:21:42 +00001736 if( iA==0 ) goto arithmetic_result_is_null;
1737 if( iA==-1 ) iA = 1;
1738 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001739 break;
1740 }
drh75897232000-05-29 14:26:00 +00001741 }
drh856c1032009-06-02 15:21:42 +00001742 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001743 MemSetTypeFlag(pOut, MEM_Int);
drhcfcca022017-04-17 23:23:17 +00001744 }else if( (flags & MEM_Null)!=0 ){
1745 goto arithmetic_result_is_null;
drh5e00f6c2001-09-13 13:46:56 +00001746 }else{
drh158b9cb2011-03-05 20:59:46 +00001747fp_math:
drh856c1032009-06-02 15:21:42 +00001748 rA = sqlite3VdbeRealValue(pIn1);
1749 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001750 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001751 case OP_Add: rB += rA; break;
1752 case OP_Subtract: rB -= rA; break;
1753 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001754 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001755 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001756 if( rA==(double)0 ) goto arithmetic_result_is_null;
1757 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001758 break;
1759 }
drhbf4133c2001-10-13 02:59:08 +00001760 default: {
drhe3b89d22019-01-18 17:53:50 +00001761 iA = sqlite3VdbeIntValue(pIn1);
1762 iB = sqlite3VdbeIntValue(pIn2);
drh856c1032009-06-02 15:21:42 +00001763 if( iA==0 ) goto arithmetic_result_is_null;
1764 if( iA==-1 ) iA = 1;
1765 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001766 break;
1767 }
drh5e00f6c2001-09-13 13:46:56 +00001768 }
drhc5a7b512010-01-13 16:25:42 +00001769#ifdef SQLITE_OMIT_FLOATING_POINT
1770 pOut->u.i = rB;
1771 MemSetTypeFlag(pOut, MEM_Int);
1772#else
drh856c1032009-06-02 15:21:42 +00001773 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001774 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001775 }
drh74eaba42014-09-18 17:52:15 +00001776 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001777 MemSetTypeFlag(pOut, MEM_Real);
drhc5a7b512010-01-13 16:25:42 +00001778#endif
drh5e00f6c2001-09-13 13:46:56 +00001779 }
1780 break;
1781
drha05a7222008-01-19 03:35:58 +00001782arithmetic_result_is_null:
1783 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001784 break;
1785}
1786
drh7a957892012-02-02 17:35:43 +00001787/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001788**
drhbb6783b2017-04-29 18:02:49 +00001789** P4 is a pointer to a CollSeq object. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001790** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1791** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001792** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001793**
drh7a957892012-02-02 17:35:43 +00001794** If P1 is not zero, then it is a register that a subsequent min() or
1795** max() aggregate will set to 1 if the current row is not the minimum or
1796** maximum. The P1 register is initialized to 0 by this instruction.
1797**
danielk1977dc1bdc42004-06-11 10:51:27 +00001798** The interface used by the implementation of the aforementioned functions
1799** to retrieve the collation sequence set by this opcode is not available
drh0a0d0562015-03-12 05:08:34 +00001800** publicly. Only built-in functions have access to this feature.
danielk1977dc1bdc42004-06-11 10:51:27 +00001801*/
drh9cbf3422008-01-17 16:22:13 +00001802case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001803 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001804 if( pOp->p1 ){
1805 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1806 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001807 break;
1808}
1809
drh98757152008-01-09 23:04:12 +00001810/* Opcode: BitAnd P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001811** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001812**
drh98757152008-01-09 23:04:12 +00001813** Take the bit-wise AND of the values in register P1 and P2 and
1814** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001815** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001816*/
drh98757152008-01-09 23:04:12 +00001817/* Opcode: BitOr P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001818** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001819**
drh98757152008-01-09 23:04:12 +00001820** Take the bit-wise OR of the values in register P1 and P2 and
1821** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001822** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001823*/
drh98757152008-01-09 23:04:12 +00001824/* Opcode: ShiftLeft P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001825** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001826**
drh98757152008-01-09 23:04:12 +00001827** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001828** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001829** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001830** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001831*/
drh98757152008-01-09 23:04:12 +00001832/* Opcode: ShiftRight P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001833** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001834**
drh98757152008-01-09 23:04:12 +00001835** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001836** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001837** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001838** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001839*/
drh5b6afba2008-01-05 16:29:28 +00001840case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1841case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1842case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1843case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001844 i64 iA;
1845 u64 uA;
1846 i64 iB;
1847 u8 op;
drh6810ce62004-01-31 19:22:56 +00001848
drh3c657212009-11-17 23:59:58 +00001849 pIn1 = &aMem[pOp->p1];
1850 pIn2 = &aMem[pOp->p2];
1851 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001852 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001853 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001854 break;
1855 }
drh158b9cb2011-03-05 20:59:46 +00001856 iA = sqlite3VdbeIntValue(pIn2);
1857 iB = sqlite3VdbeIntValue(pIn1);
1858 op = pOp->opcode;
1859 if( op==OP_BitAnd ){
1860 iA &= iB;
1861 }else if( op==OP_BitOr ){
1862 iA |= iB;
1863 }else if( iB!=0 ){
1864 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1865
1866 /* If shifting by a negative amount, shift in the other direction */
1867 if( iB<0 ){
1868 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1869 op = 2*OP_ShiftLeft + 1 - op;
1870 iB = iB>(-64) ? -iB : 64;
1871 }
1872
1873 if( iB>=64 ){
1874 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1875 }else{
1876 memcpy(&uA, &iA, sizeof(uA));
1877 if( op==OP_ShiftLeft ){
1878 uA <<= iB;
1879 }else{
1880 uA >>= iB;
1881 /* Sign-extend on a right shift of a negative number */
1882 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1883 }
1884 memcpy(&iA, &uA, sizeof(iA));
1885 }
drhbf4133c2001-10-13 02:59:08 +00001886 }
drh158b9cb2011-03-05 20:59:46 +00001887 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001888 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001889 break;
1890}
1891
drh8558cde2008-01-05 05:20:10 +00001892/* Opcode: AddImm P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001893** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001894**
danielk19770cdc0222008-06-26 18:04:03 +00001895** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001896** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001897**
drh8558cde2008-01-05 05:20:10 +00001898** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001899*/
drh9cbf3422008-01-17 16:22:13 +00001900case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001901 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001902 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001903 sqlite3VdbeMemIntegerify(pIn1);
1904 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001905 break;
1906}
1907
dane5166e02019-03-19 11:56:39 +00001908/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001909**
dane5166e02019-03-19 11:56:39 +00001910** Force the value in register P1 to be an integer. If the value
1911** in P1 is not an integer and cannot be converted into an integer
1912** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001913** raise an SQLITE_MISMATCH exception.
1914*/
drh9cbf3422008-01-17 16:22:13 +00001915case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001916 pIn1 = &aMem[pOp->p1];
dane5166e02019-03-19 11:56:39 +00001917 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001918 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
dane5166e02019-03-19 11:56:39 +00001919 if( (pIn1->flags & MEM_Int)==0 ){
drhc9065332019-04-01 14:01:21 +00001920 VdbeBranchTaken(1, 2);
drh83b301b2013-11-20 00:59:02 +00001921 if( pOp->p2==0 ){
1922 rc = SQLITE_MISMATCH;
1923 goto abort_due_to_error;
1924 }else{
drhf56fa462015-04-13 21:39:54 +00001925 goto jump_to_p2;
drh83b301b2013-11-20 00:59:02 +00001926 }
drh8aff1012001-12-22 14:49:24 +00001927 }
drh8aff1012001-12-22 14:49:24 +00001928 }
drhc9065332019-04-01 14:01:21 +00001929 VdbeBranchTaken(0, 2);
dane5166e02019-03-19 11:56:39 +00001930 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001931 break;
1932}
1933
drh13573c72010-01-12 17:04:07 +00001934#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001935/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001936**
drh2133d822008-01-03 18:44:59 +00001937** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001938**
drh8a512562005-11-14 22:29:05 +00001939** This opcode is used when extracting information from a column that
1940** has REAL affinity. Such column values may still be stored as
1941** integers, for space efficiency, but after extraction we want them
1942** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001943*/
drh9cbf3422008-01-17 16:22:13 +00001944case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001945 pIn1 = &aMem[pOp->p1];
drh169f0772019-05-02 21:36:26 +00001946 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
drh3242c692019-05-04 01:29:13 +00001947 testcase( pIn1->flags & MEM_Int );
1948 testcase( pIn1->flags & MEM_IntReal );
drh8558cde2008-01-05 05:20:10 +00001949 sqlite3VdbeMemRealify(pIn1);
drhefb5f9a2019-08-30 21:52:13 +00001950 REGISTER_TRACE(pOp->p1, pIn1);
drh8a512562005-11-14 22:29:05 +00001951 }
drh487e2622005-06-25 18:42:14 +00001952 break;
1953}
drh13573c72010-01-12 17:04:07 +00001954#endif
drh487e2622005-06-25 18:42:14 +00001955
drh8df447f2005-11-01 15:48:24 +00001956#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001957/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001958** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001959**
drh4169e432014-08-25 20:11:52 +00001960** Force the value in register P1 to be the type defined by P2.
1961**
1962** <ul>
drhbb6783b2017-04-29 18:02:49 +00001963** <li> P2=='A' &rarr; BLOB
1964** <li> P2=='B' &rarr; TEXT
1965** <li> P2=='C' &rarr; NUMERIC
1966** <li> P2=='D' &rarr; INTEGER
1967** <li> P2=='E' &rarr; REAL
drh4169e432014-08-25 20:11:52 +00001968** </ul>
drh487e2622005-06-25 18:42:14 +00001969**
1970** A NULL value is not changed by this routine. It remains NULL.
1971*/
drh4169e432014-08-25 20:11:52 +00001972case OP_Cast: { /* in1 */
drh05883a32015-06-02 15:32:08 +00001973 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001974 testcase( pOp->p2==SQLITE_AFF_TEXT );
drh05883a32015-06-02 15:32:08 +00001975 testcase( pOp->p2==SQLITE_AFF_BLOB );
drh05bbb2e2014-08-25 22:37:19 +00001976 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1977 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1978 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001979 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001980 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001981 rc = ExpandBlob(pIn1);
drh9467abf2016-02-17 18:44:11 +00001982 if( rc ) goto abort_due_to_error;
drh0af6ddd2019-12-23 03:37:46 +00001983 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
1984 if( rc ) goto abort_due_to_error;
1985 UPDATE_MAX_BLOBSIZE(pIn1);
drh5d732722019-12-20 17:25:10 +00001986 REGISTER_TRACE(pOp->p1, pIn1);
drh487e2622005-06-25 18:42:14 +00001987 break;
1988}
drh8a512562005-11-14 22:29:05 +00001989#endif /* SQLITE_OMIT_CAST */
1990
drh79752b62016-08-13 10:02:17 +00001991/* Opcode: Eq P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001992** Synopsis: IF r[P3]==r[P1]
drh79752b62016-08-13 10:02:17 +00001993**
1994** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
drh4bc20452021-03-29 18:53:47 +00001995** jump to address P2.
drh79752b62016-08-13 10:02:17 +00001996**
1997** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1998** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1999** to coerce both inputs according to this affinity before the
2000** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
2001** affinity is used. Note that the affinity conversions are stored
2002** back into the input registers P1 and P3. So this opcode can cause
2003** persistent changes to registers P1 and P3.
2004**
2005** Once any conversions have taken place, and neither value is NULL,
2006** the values are compared. If both values are blobs then memcmp() is
2007** used to determine the results of the comparison. If both values
2008** are text, then the appropriate collating function specified in
2009** P4 is used to do the comparison. If P4 is not specified then
2010** memcmp() is used to compare text string. If both values are
2011** numeric, then a numeric comparison is used. If the two values
2012** are of different types, then numbers are considered less than
2013** strings and strings are considered less than blobs.
2014**
2015** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
2016** true or false and is never NULL. If both operands are NULL then the result
2017** of comparison is true. If either operand is NULL then the result is false.
2018** If neither operand is NULL the result is the same as it would be if
2019** the SQLITE_NULLEQ flag were omitted from P5.
2020**
drh1f97d262021-03-29 13:47:20 +00002021** This opcode saves the result of comparison for use by the new
2022** OP_Jump opcode.
drh79752b62016-08-13 10:02:17 +00002023*/
2024/* Opcode: Ne P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002025** Synopsis: IF r[P3]!=r[P1]
drh79752b62016-08-13 10:02:17 +00002026**
2027** This works just like the Eq opcode except that the jump is taken if
2028** the operands in registers P1 and P3 are not equal. See the Eq opcode for
2029** additional information.
drh79752b62016-08-13 10:02:17 +00002030*/
drh35573352008-01-08 23:54:25 +00002031/* Opcode: Lt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002032** Synopsis: IF r[P3]<r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002033**
drh35573352008-01-08 23:54:25 +00002034** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
drh4bc20452021-03-29 18:53:47 +00002035** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00002036**
drh35573352008-01-08 23:54:25 +00002037** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
drh79752b62016-08-13 10:02:17 +00002038** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00002039** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00002040**
drh35573352008-01-08 23:54:25 +00002041** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00002042** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00002043** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00002044** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00002045** affinity is used. Note that the affinity conversions are stored
2046** back into the input registers P1 and P3. So this opcode can cause
2047** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00002048**
2049** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00002050** the values are compared. If both values are blobs then memcmp() is
2051** used to determine the results of the comparison. If both values
2052** are text, then the appropriate collating function specified in
2053** P4 is used to do the comparison. If P4 is not specified then
2054** memcmp() is used to compare text string. If both values are
2055** numeric, then a numeric comparison is used. If the two values
2056** are of different types, then numbers are considered less than
2057** strings and strings are considered less than blobs.
drh1f97d262021-03-29 13:47:20 +00002058**
2059** This opcode saves the result of comparison for use by the new
2060** OP_Jump opcode.
drh5e00f6c2001-09-13 13:46:56 +00002061*/
drh9cbf3422008-01-17 16:22:13 +00002062/* Opcode: Le P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002063** Synopsis: IF r[P3]<=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002064**
drh35573352008-01-08 23:54:25 +00002065** This works just like the Lt opcode except that the jump is taken if
2066** the content of register P3 is less than or equal to the content of
2067** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00002068*/
drh9cbf3422008-01-17 16:22:13 +00002069/* Opcode: Gt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002070** Synopsis: IF r[P3]>r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002071**
drh35573352008-01-08 23:54:25 +00002072** This works just like the Lt opcode except that the jump is taken if
2073** the content of register P3 is greater than the content of
2074** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00002075*/
drh9cbf3422008-01-17 16:22:13 +00002076/* Opcode: Ge P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002077** Synopsis: IF r[P3]>=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002078**
drh35573352008-01-08 23:54:25 +00002079** This works just like the Lt opcode except that the jump is taken if
2080** the content of register P3 is greater than or equal to the content of
2081** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00002082*/
drh9cbf3422008-01-17 16:22:13 +00002083case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
2084case OP_Ne: /* same as TK_NE, jump, in1, in3 */
2085case OP_Lt: /* same as TK_LT, jump, in1, in3 */
2086case OP_Le: /* same as TK_LE, jump, in1, in3 */
2087case OP_Gt: /* same as TK_GT, jump, in1, in3 */
2088case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh4910a762016-09-03 01:46:15 +00002089 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
drh6a2fe092009-09-23 02:29:36 +00002090 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00002091 u16 flags1; /* Copy of initial value of pIn1->flags */
2092 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00002093
drh3c657212009-11-17 23:59:58 +00002094 pIn1 = &aMem[pOp->p1];
2095 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00002096 flags1 = pIn1->flags;
2097 flags3 = pIn3->flags;
drh85464972021-05-17 16:54:52 +00002098 if( (flags1 & flags3 & MEM_Int)!=0 ){
drh8ed1da12021-05-18 00:52:06 +00002099 assert( (pOp->p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_TEXT || CORRUPT_DB );
drh85464972021-05-17 16:54:52 +00002100 /* Common case of comparison of two integers */
2101 if( pIn3->u.i > pIn1->u.i ){
drh85464972021-05-17 16:54:52 +00002102 if( sqlite3aGTb[pOp->opcode] ){
2103 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
2104 goto jump_to_p2;
2105 }
drha81a9f72022-04-04 11:38:49 +00002106 iCompare = +1;
drh85464972021-05-17 16:54:52 +00002107 }else if( pIn3->u.i < pIn1->u.i ){
drh85464972021-05-17 16:54:52 +00002108 if( sqlite3aLTb[pOp->opcode] ){
2109 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
2110 goto jump_to_p2;
2111 }
drha81a9f72022-04-04 11:38:49 +00002112 iCompare = -1;
drh85464972021-05-17 16:54:52 +00002113 }else{
drh85464972021-05-17 16:54:52 +00002114 if( sqlite3aEQb[pOp->opcode] ){
2115 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
2116 goto jump_to_p2;
2117 }
drha81a9f72022-04-04 11:38:49 +00002118 iCompare = 0;
drh85464972021-05-17 16:54:52 +00002119 }
2120 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
2121 break;
2122 }
drhc3f1d5f2011-05-30 23:42:16 +00002123 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00002124 /* One or both operands are NULL */
2125 if( pOp->p5 & SQLITE_NULLEQ ){
2126 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
2127 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
2128 ** or not both operands are null.
2129 */
drh053a1282012-09-19 21:15:46 +00002130 assert( (flags1 & MEM_Cleared)==0 );
drha42325e2018-12-22 00:34:30 +00002131 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
2132 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
drhc3191d22016-10-18 16:36:15 +00002133 if( (flags1&flags3&MEM_Null)!=0
drh053a1282012-09-19 21:15:46 +00002134 && (flags3&MEM_Cleared)==0
2135 ){
drh4910a762016-09-03 01:46:15 +00002136 res = 0; /* Operands are equal */
drh053a1282012-09-19 21:15:46 +00002137 }else{
danbdabe742019-03-18 16:51:24 +00002138 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
drh053a1282012-09-19 21:15:46 +00002139 }
drh6a2fe092009-09-23 02:29:36 +00002140 }else{
2141 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
2142 ** then the result is always NULL.
2143 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
2144 */
drh4bc20452021-03-29 18:53:47 +00002145 VdbeBranchTaken(2,3);
2146 if( pOp->p5 & SQLITE_JUMPIFNULL ){
2147 goto jump_to_p2;
drh6a2fe092009-09-23 02:29:36 +00002148 }
drha81a9f72022-04-04 11:38:49 +00002149 iCompare = 1; /* Operands are not equal */
drh6a2fe092009-09-23 02:29:36 +00002150 break;
danielk1977a37cdde2004-05-16 11:15:36 +00002151 }
drh6a2fe092009-09-23 02:29:36 +00002152 }else{
drh85464972021-05-17 16:54:52 +00002153 /* Neither operand is NULL and we couldn't do the special high-speed
2154 ** integer comparison case. So do a general-case comparison. */
drh6a2fe092009-09-23 02:29:36 +00002155 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00002156 if( affinity>=SQLITE_AFF_NUMERIC ){
drh5fd0c122016-04-04 13:46:24 +00002157 if( (flags1 | flags3)&MEM_Str ){
drh169f0772019-05-02 21:36:26 +00002158 if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
drh5fd0c122016-04-04 13:46:24 +00002159 applyNumericAffinity(pIn1,0);
drh86d2de22020-06-14 13:40:13 +00002160 testcase( flags3==pIn3->flags );
drh4b37cd42016-06-25 11:43:47 +00002161 flags3 = pIn3->flags;
drh5fd0c122016-04-04 13:46:24 +00002162 }
drh169f0772019-05-02 21:36:26 +00002163 if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
drh5fd0c122016-04-04 13:46:24 +00002164 applyNumericAffinity(pIn3,0);
2165 }
drh24a09622014-09-18 16:28:59 +00002166 }
2167 }else if( affinity==SQLITE_AFF_TEXT ){
drh169f0772019-05-02 21:36:26 +00002168 if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002169 testcase( pIn1->flags & MEM_Int );
2170 testcase( pIn1->flags & MEM_Real );
drh169f0772019-05-02 21:36:26 +00002171 testcase( pIn1->flags & MEM_IntReal );
drh24a09622014-09-18 16:28:59 +00002172 sqlite3VdbeMemStringify(pIn1, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002173 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
2174 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
drhc4bb9992022-01-14 21:34:49 +00002175 if( pIn1==pIn3 ) flags3 = flags1 | MEM_Str;
drh24a09622014-09-18 16:28:59 +00002176 }
drhb44fec62019-12-24 21:42:22 +00002177 if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002178 testcase( pIn3->flags & MEM_Int );
2179 testcase( pIn3->flags & MEM_Real );
drh169f0772019-05-02 21:36:26 +00002180 testcase( pIn3->flags & MEM_IntReal );
drh24a09622014-09-18 16:28:59 +00002181 sqlite3VdbeMemStringify(pIn3, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002182 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
2183 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00002184 }
drh6a2fe092009-09-23 02:29:36 +00002185 }
drh6a2fe092009-09-23 02:29:36 +00002186 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh4910a762016-09-03 01:46:15 +00002187 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00002188 }
drh85464972021-05-17 16:54:52 +00002189
drh58596362017-08-03 00:29:23 +00002190 /* At this point, res is negative, zero, or positive if reg[P1] is
2191 ** less than, equal to, or greater than reg[P3], respectively. Compute
2192 ** the answer to this operator in res2, depending on what the comparison
2193 ** operator actually is. The next block of code depends on the fact
2194 ** that the 6 comparison operators are consecutive integers in this
2195 ** order: NE, EQ, GT, LE, LT, GE */
2196 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
2197 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
drh1af3fd52021-03-28 23:37:56 +00002198 if( res<0 ){
2199 res2 = sqlite3aLTb[pOp->opcode];
drh58596362017-08-03 00:29:23 +00002200 }else if( res==0 ){
drh1af3fd52021-03-28 23:37:56 +00002201 res2 = sqlite3aEQb[pOp->opcode];
drh58596362017-08-03 00:29:23 +00002202 }else{
drh1af3fd52021-03-28 23:37:56 +00002203 res2 = sqlite3aGTb[pOp->opcode];
danielk1977a37cdde2004-05-16 11:15:36 +00002204 }
drh1f97d262021-03-29 13:47:20 +00002205 iCompare = res;
danielk1977a37cdde2004-05-16 11:15:36 +00002206
drhf56fa462015-04-13 21:39:54 +00002207 /* Undo any changes made by applyAffinity() to the input registers. */
drhf56fa462015-04-13 21:39:54 +00002208 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2209 pIn3->flags = flags3;
drhb44fec62019-12-24 21:42:22 +00002210 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2211 pIn1->flags = flags1;
drhf56fa462015-04-13 21:39:54 +00002212
drh4bc20452021-03-29 18:53:47 +00002213 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
2214 if( res2 ){
2215 goto jump_to_p2;
danielk1977a37cdde2004-05-16 11:15:36 +00002216 }
2217 break;
2218}
drhc9b84a12002-06-20 11:36:48 +00002219
drh4bc20452021-03-29 18:53:47 +00002220/* Opcode: ElseEq * P2 * * *
drh79752b62016-08-13 10:02:17 +00002221**
drh13d79502019-12-23 02:18:49 +00002222** This opcode must follow an OP_Lt or OP_Gt comparison operator. There
2223** can be zero or more OP_ReleaseReg opcodes intervening, but no other
2224** opcodes are allowed to occur between this instruction and the previous
drh4bc20452021-03-29 18:53:47 +00002225** OP_Lt or OP_Gt.
drh13d79502019-12-23 02:18:49 +00002226**
2227** If result of an OP_Eq comparison on the same two operands as the
drh4bc20452021-03-29 18:53:47 +00002228** prior OP_Lt or OP_Gt would have been true, then jump to P2.
2229** If the result of an OP_Eq comparison on the two previous
2230** operands would have been false or NULL, then fall through.
drh79752b62016-08-13 10:02:17 +00002231*/
drh4bc20452021-03-29 18:53:47 +00002232case OP_ElseEq: { /* same as TK_ESCAPE, jump */
drh13d79502019-12-23 02:18:49 +00002233
2234#ifdef SQLITE_DEBUG
2235 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
drh4bc20452021-03-29 18:53:47 +00002236 ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */
drh13d79502019-12-23 02:18:49 +00002237 int iAddr;
2238 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
2239 if( aOp[iAddr].opcode==OP_ReleaseReg ) continue;
2240 assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt );
drh13d79502019-12-23 02:18:49 +00002241 break;
2242 }
2243#endif /* SQLITE_DEBUG */
drh4bc20452021-03-29 18:53:47 +00002244 VdbeBranchTaken(iCompare==0, 2);
2245 if( iCompare==0 ) goto jump_to_p2;
drh79752b62016-08-13 10:02:17 +00002246 break;
2247}
2248
2249
drh0acb7e42008-06-25 00:12:41 +00002250/* Opcode: Permutation * * * P4 *
2251**
drhb7dab702017-01-26 18:00:00 +00002252** Set the permutation used by the OP_Compare operator in the next
2253** instruction. The permutation is stored in the P4 operand.
drh0acb7e42008-06-25 00:12:41 +00002254**
drha81a9f72022-04-04 11:38:49 +00002255** The permutation is only valid for the next opcode which must be
2256** an OP_Compare that has the OPFLAG_PERMUTE bit set in P5.
drhb1702022016-01-30 00:45:18 +00002257**
2258** The first integer in the P4 integer array is the length of the array
2259** and does not become part of the permutation.
drh0acb7e42008-06-25 00:12:41 +00002260*/
2261case OP_Permutation: {
2262 assert( pOp->p4type==P4_INTARRAY );
2263 assert( pOp->p4.ai );
drhb7dab702017-01-26 18:00:00 +00002264 assert( pOp[1].opcode==OP_Compare );
2265 assert( pOp[1].p5 & OPFLAG_PERMUTE );
drh0acb7e42008-06-25 00:12:41 +00002266 break;
2267}
2268
drh953f7612012-12-07 22:18:54 +00002269/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00002270** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00002271**
drh710c4842010-08-30 01:17:20 +00002272** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2273** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00002274** the comparison for use by the next OP_Jump instruct.
2275**
drh0ca10df2012-12-08 13:26:23 +00002276** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2277** determined by the most recent OP_Permutation operator. If the
2278** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2279** order.
2280**
drh0acb7e42008-06-25 00:12:41 +00002281** P4 is a KeyInfo structure that defines collating sequences and sort
2282** orders for the comparison. The permutation applies to registers
2283** only. The KeyInfo elements are used sequentially.
2284**
2285** The comparison is a sort comparison, so NULLs compare equal,
2286** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00002287** and strings are less than blobs.
drha81a9f72022-04-04 11:38:49 +00002288**
2289** This opcode must be immediately followed by an OP_Jump opcode.
drh16ee60f2008-06-20 18:13:25 +00002290*/
2291case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002292 int n;
2293 int i;
2294 int p1;
2295 int p2;
2296 const KeyInfo *pKeyInfo;
drhabc38152020-07-22 13:38:04 +00002297 u32 idx;
drh856c1032009-06-02 15:21:42 +00002298 CollSeq *pColl; /* Collating sequence to use on this term */
2299 int bRev; /* True for DESCENDING sort order */
drhabc38152020-07-22 13:38:04 +00002300 u32 *aPermute; /* The permutation */
drh856c1032009-06-02 15:21:42 +00002301
drhb7dab702017-01-26 18:00:00 +00002302 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
2303 aPermute = 0;
2304 }else{
2305 assert( pOp>aOp );
2306 assert( pOp[-1].opcode==OP_Permutation );
2307 assert( pOp[-1].p4type==P4_INTARRAY );
2308 aPermute = pOp[-1].p4.ai + 1;
2309 assert( aPermute!=0 );
2310 }
drh856c1032009-06-02 15:21:42 +00002311 n = pOp->p3;
2312 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002313 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002314 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002315 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002316 p2 = pOp->p2;
drhd879e3e2017-02-13 13:35:55 +00002317#ifdef SQLITE_DEBUG
drh6a2fe092009-09-23 02:29:36 +00002318 if( aPermute ){
2319 int k, mx = 0;
mistachkincec5f1d2020-08-04 16:11:37 +00002320 for(k=0; k<n; k++) if( aPermute[k]>(u32)mx ) mx = aPermute[k];
drh9f6168b2016-03-19 23:32:58 +00002321 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
2322 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002323 }else{
drh9f6168b2016-03-19 23:32:58 +00002324 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
2325 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002326 }
2327#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002328 for(i=0; i<n; i++){
drh8deae5a2020-07-29 12:23:20 +00002329 idx = aPermute ? aPermute[i] : (u32)i;
drh2b4ded92010-09-27 21:09:31 +00002330 assert( memIsValid(&aMem[p1+idx]) );
2331 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002332 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2333 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drha485ad12017-08-02 22:43:14 +00002334 assert( i<pKeyInfo->nKeyField );
drh93a960a2008-07-10 00:32:42 +00002335 pColl = pKeyInfo->aColl[i];
dan6e118922019-08-12 16:36:38 +00002336 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
drha6c2ed92009-11-14 23:22:23 +00002337 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002338 if( iCompare ){
dan6e118922019-08-12 16:36:38 +00002339 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
2340 && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null))
2341 ){
2342 iCompare = -iCompare;
2343 }
drh0acb7e42008-06-25 00:12:41 +00002344 if( bRev ) iCompare = -iCompare;
2345 break;
2346 }
drh16ee60f2008-06-20 18:13:25 +00002347 }
drha81a9f72022-04-04 11:38:49 +00002348 assert( pOp[1].opcode==OP_Jump );
drh16ee60f2008-06-20 18:13:25 +00002349 break;
2350}
2351
2352/* Opcode: Jump P1 P2 P3 * *
2353**
2354** Jump to the instruction at address P1, P2, or P3 depending on whether
2355** in the most recent OP_Compare instruction the P1 vector was less than
2356** equal to, or greater than the P2 vector, respectively.
drha81a9f72022-04-04 11:38:49 +00002357**
2358** This opcode must immediately follow an OP_Compare opcode.
drh16ee60f2008-06-20 18:13:25 +00002359*/
drh0acb7e42008-06-25 00:12:41 +00002360case OP_Jump: { /* jump */
drha81a9f72022-04-04 11:38:49 +00002361 assert( pOp>aOp && pOp[-1].opcode==OP_Compare );
drh0acb7e42008-06-25 00:12:41 +00002362 if( iCompare<0 ){
drh7083a482018-07-10 16:04:04 +00002363 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
drh0acb7e42008-06-25 00:12:41 +00002364 }else if( iCompare==0 ){
drh7083a482018-07-10 16:04:04 +00002365 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
drh16ee60f2008-06-20 18:13:25 +00002366 }else{
drh7083a482018-07-10 16:04:04 +00002367 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
drh16ee60f2008-06-20 18:13:25 +00002368 }
2369 break;
2370}
2371
drh5b6afba2008-01-05 16:29:28 +00002372/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002373** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002374**
drh5b6afba2008-01-05 16:29:28 +00002375** Take the logical AND of the values in registers P1 and P2 and
2376** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002377**
drh5b6afba2008-01-05 16:29:28 +00002378** If either P1 or P2 is 0 (false) then the result is 0 even if
2379** the other input is NULL. A NULL and true or two NULLs give
2380** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002381*/
drh5b6afba2008-01-05 16:29:28 +00002382/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002383** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002384**
2385** Take the logical OR of the values in register P1 and P2 and
2386** store the answer in register P3.
2387**
2388** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2389** even if the other input is NULL. A NULL and false or two NULLs
2390** give a NULL output.
2391*/
2392case OP_And: /* same as TK_AND, in1, in2, out3 */
2393case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002394 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2395 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002396
drh1fcfa722018-02-26 15:27:31 +00002397 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
2398 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
drhbb113512002-05-27 01:04:51 +00002399 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002400 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002401 v1 = and_logic[v1*3+v2];
2402 }else{
drh5b6afba2008-01-05 16:29:28 +00002403 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002404 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002405 }
drh3c657212009-11-17 23:59:58 +00002406 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002407 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002408 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002409 }else{
drh5b6afba2008-01-05 16:29:28 +00002410 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002411 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002412 }
drh5e00f6c2001-09-13 13:46:56 +00002413 break;
2414}
2415
drh8abed7b2018-02-26 18:49:05 +00002416/* Opcode: IsTrue P1 P2 P3 P4 *
2417** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
2418**
2419** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
2420** IS NOT FALSE operators.
2421**
drh96acafb2018-02-27 14:49:25 +00002422** Interpret the value in register P1 as a boolean value. Store that
drh8abed7b2018-02-26 18:49:05 +00002423** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
2424** NULL, then the P3 is stored in register P2. Invert the answer if P4
2425** is 1.
2426**
2427** The logic is summarized like this:
2428**
2429** <ul>
drh96acafb2018-02-27 14:49:25 +00002430** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
2431** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
2432** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
2433** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
drh8abed7b2018-02-26 18:49:05 +00002434** </ul>
2435*/
2436case OP_IsTrue: { /* in1, out2 */
2437 assert( pOp->p4type==P4_INT32 );
2438 assert( pOp->p4.i==0 || pOp->p4.i==1 );
drh96acafb2018-02-27 14:49:25 +00002439 assert( pOp->p3==0 || pOp->p3==1 );
drh8abed7b2018-02-26 18:49:05 +00002440 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
2441 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
2442 break;
2443}
2444
drhe99fa2a2008-12-15 15:27:51 +00002445/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002446** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002447**
drhe99fa2a2008-12-15 15:27:51 +00002448** Interpret the value in register P1 as a boolean value. Store the
2449** boolean complement in register P2. If the value in register P1 is
2450** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002451*/
drh93952eb2009-11-13 19:43:43 +00002452case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002453 pIn1 = &aMem[pOp->p1];
2454 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002455 if( (pIn1->flags & MEM_Null)==0 ){
drhbc8f68a2018-02-26 15:31:39 +00002456 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
drh007c8432018-02-26 03:20:18 +00002457 }else{
2458 sqlite3VdbeMemSetNull(pOut);
drhe99fa2a2008-12-15 15:27:51 +00002459 }
drh5e00f6c2001-09-13 13:46:56 +00002460 break;
2461}
2462
drhe99fa2a2008-12-15 15:27:51 +00002463/* Opcode: BitNot P1 P2 * * *
drhcd9e0142018-06-12 13:16:57 +00002464** Synopsis: r[P2]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002465**
drhe99fa2a2008-12-15 15:27:51 +00002466** Interpret the content of register P1 as an integer. Store the
2467** ones-complement of the P1 value into register P2. If P1 holds
2468** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002469*/
drh93952eb2009-11-13 19:43:43 +00002470case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002471 pIn1 = &aMem[pOp->p1];
2472 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002473 sqlite3VdbeMemSetNull(pOut);
2474 if( (pIn1->flags & MEM_Null)==0 ){
2475 pOut->flags = MEM_Int;
2476 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002477 }
drhbf4133c2001-10-13 02:59:08 +00002478 break;
2479}
2480
drh48f2d3b2011-09-16 01:34:43 +00002481/* Opcode: Once P1 P2 * * *
2482**
drhab087d42017-03-24 17:59:56 +00002483** Fall through to the next instruction the first time this opcode is
2484** encountered on each invocation of the byte-code program. Jump to P2
2485** on the second and all subsequent encounters during the same invocation.
2486**
2487** Top-level programs determine first invocation by comparing the P1
2488** operand against the P1 operand on the OP_Init opcode at the beginning
2489** of the program. If the P1 values differ, then fall through and make
2490** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2491** the same then take the jump.
2492**
2493** For subprograms, there is a bitmask in the VdbeFrame that determines
2494** whether or not the jump should be taken. The bitmask is necessary
2495** because the self-altering code trick does not work for recursive
2496** triggers.
drh48f2d3b2011-09-16 01:34:43 +00002497*/
dan1d8cb212011-12-09 13:24:16 +00002498case OP_Once: { /* jump */
drhab087d42017-03-24 17:59:56 +00002499 u32 iAddr; /* Address of this instruction */
drh9e5eb9c2016-09-18 16:08:10 +00002500 assert( p->aOp[0].opcode==OP_Init );
drhab087d42017-03-24 17:59:56 +00002501 if( p->pFrame ){
2502 iAddr = (int)(pOp - p->aOp);
2503 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
2504 VdbeBranchTaken(1, 2);
drhab087d42017-03-24 17:59:56 +00002505 goto jump_to_p2;
2506 }
drh18333ef2017-03-24 18:38:41 +00002507 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
dan1d8cb212011-12-09 13:24:16 +00002508 }else{
drhab087d42017-03-24 17:59:56 +00002509 if( p->aOp[0].p1==pOp->p1 ){
2510 VdbeBranchTaken(1, 2);
2511 goto jump_to_p2;
2512 }
dan1d8cb212011-12-09 13:24:16 +00002513 }
drhab087d42017-03-24 17:59:56 +00002514 VdbeBranchTaken(0, 2);
2515 pOp->p1 = p->aOp[0].p1;
dan1d8cb212011-12-09 13:24:16 +00002516 break;
2517}
2518
drh3c84ddf2008-01-09 02:15:38 +00002519/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002520**
drhef8662b2011-06-20 21:47:58 +00002521** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002522** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002523** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002524*/
drh1fcfa722018-02-26 15:27:31 +00002525case OP_If: { /* jump, in1 */
2526 int c;
2527 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
2528 VdbeBranchTaken(c!=0, 2);
2529 if( c ) goto jump_to_p2;
2530 break;
2531}
2532
drh3c84ddf2008-01-09 02:15:38 +00002533/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002534**
drhef8662b2011-06-20 21:47:58 +00002535** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002536** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002537** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002538*/
drh9cbf3422008-01-17 16:22:13 +00002539case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002540 int c;
drh1fcfa722018-02-26 15:27:31 +00002541 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
drh688852a2014-02-17 22:40:43 +00002542 VdbeBranchTaken(c!=0, 2);
drh1fcfa722018-02-26 15:27:31 +00002543 if( c ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00002544 break;
2545}
2546
drh830ecf92009-06-18 00:41:55 +00002547/* Opcode: IsNull P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00002548** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002549**
drh830ecf92009-06-18 00:41:55 +00002550** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002551*/
drh9cbf3422008-01-17 16:22:13 +00002552case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002553 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002554 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002555 if( (pIn1->flags & MEM_Null)!=0 ){
drhf56fa462015-04-13 21:39:54 +00002556 goto jump_to_p2;
drh830ecf92009-06-18 00:41:55 +00002557 }
drh477df4b2008-01-05 18:48:24 +00002558 break;
2559}
2560
drh71c770f2021-08-19 16:29:33 +00002561/* Opcode: IsNullOrType P1 P2 P3 * *
2562** Synopsis: if typeof(r[P1]) IN (P3,5) goto P2
2563**
2564** Jump to P2 if the value in register P1 is NULL or has a datatype P3.
2565** P3 is an integer which should be one of SQLITE_INTEGER, SQLITE_FLOAT,
2566** SQLITE_BLOB, SQLITE_NULL, or SQLITE_TEXT.
2567*/
2568case OP_IsNullOrType: { /* jump, in1 */
2569 int doTheJump;
2570 pIn1 = &aMem[pOp->p1];
2571 doTheJump = (pIn1->flags & MEM_Null)!=0 || sqlite3_value_type(pIn1)==pOp->p3;
2572 VdbeBranchTaken( doTheJump, 2);
2573 if( doTheJump ) goto jump_to_p2;
2574 break;
2575}
2576
drh871e7ff2021-03-29 14:40:48 +00002577/* Opcode: ZeroOrNull P1 P2 P3 * *
drh4bc20452021-03-29 18:53:47 +00002578** Synopsis: r[P2] = 0 OR NULL
drh871e7ff2021-03-29 14:40:48 +00002579**
drh4bc20452021-03-29 18:53:47 +00002580** If all both registers P1 and P3 are NOT NULL, then store a zero in
2581** register P2. If either registers P1 or P3 are NULL then put
2582** a NULL in register P2.
drh871e7ff2021-03-29 14:40:48 +00002583*/
drh4bc20452021-03-29 18:53:47 +00002584case OP_ZeroOrNull: { /* in1, in2, out2, in3 */
drh871e7ff2021-03-29 14:40:48 +00002585 if( (aMem[pOp->p1].flags & MEM_Null)!=0
2586 || (aMem[pOp->p3].flags & MEM_Null)!=0
2587 ){
2588 sqlite3VdbeMemSetNull(aMem + pOp->p2);
2589 }else{
2590 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0);
2591 }
2592 break;
2593}
2594
drh98757152008-01-09 23:04:12 +00002595/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002596** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002597**
drh6a288a32008-01-07 19:20:24 +00002598** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002599*/
drh9cbf3422008-01-17 16:22:13 +00002600case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002601 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002602 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002603 if( (pIn1->flags & MEM_Null)==0 ){
drhf56fa462015-04-13 21:39:54 +00002604 goto jump_to_p2;
drh6a288a32008-01-07 19:20:24 +00002605 }
drh5e00f6c2001-09-13 13:46:56 +00002606 break;
2607}
2608
drh31d6fd52017-04-14 19:03:10 +00002609/* Opcode: IfNullRow P1 P2 P3 * *
2610** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
2611**
2612** Check the cursor P1 to see if it is currently pointing at a NULL row.
2613** If it is, then set register P3 to NULL and jump immediately to P2.
2614** If P1 is not on a NULL row, then fall through without making any
2615** changes.
2616*/
2617case OP_IfNullRow: { /* jump */
2618 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh3f1e9e02017-05-23 01:21:07 +00002619 assert( p->apCsr[pOp->p1]!=0 );
drh31d6fd52017-04-14 19:03:10 +00002620 if( p->apCsr[pOp->p1]->nullRow ){
2621 sqlite3VdbeMemSetNull(aMem + pOp->p3);
2622 goto jump_to_p2;
2623 }
2624 break;
2625}
2626
drh092457b2017-12-29 15:04:49 +00002627#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
2628/* Opcode: Offset P1 P2 P3 * *
2629** Synopsis: r[P3] = sqlite_offset(P1)
drh2fc865c2017-12-16 20:20:37 +00002630**
drh092457b2017-12-29 15:04:49 +00002631** Store in register r[P3] the byte offset into the database file that is the
drh2fc865c2017-12-16 20:20:37 +00002632** start of the payload for the record at which that cursor P1 is currently
2633** pointing.
drhfe6d20e2017-12-29 14:33:54 +00002634**
drh092457b2017-12-29 15:04:49 +00002635** P2 is the column number for the argument to the sqlite_offset() function.
drhfe6d20e2017-12-29 14:33:54 +00002636** This opcode does not use P2 itself, but the P2 value is used by the
2637** code generator. The P1, P2, and P3 operands to this opcode are the
mistachkin5e9825e2018-03-01 18:09:02 +00002638** same as for OP_Column.
drh092457b2017-12-29 15:04:49 +00002639**
2640** This opcode is only available if SQLite is compiled with the
2641** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
drh2fc865c2017-12-16 20:20:37 +00002642*/
drh092457b2017-12-29 15:04:49 +00002643case OP_Offset: { /* out3 */
drh2fc865c2017-12-16 20:20:37 +00002644 VdbeCursor *pC; /* The VDBE cursor */
2645 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2646 pC = p->apCsr[pOp->p1];
drhfe6d20e2017-12-29 14:33:54 +00002647 pOut = &p->aMem[pOp->p3];
drh8f32d922022-03-05 19:36:29 +00002648 if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){
drhfe6d20e2017-12-29 14:33:54 +00002649 sqlite3VdbeMemSetNull(pOut);
drh2fc865c2017-12-16 20:20:37 +00002650 }else{
drh8f32d922022-03-05 19:36:29 +00002651 if( pC->deferredMoveto ){
2652 rc = sqlite3VdbeFinishMoveto(pC);
2653 if( rc ) goto abort_due_to_error;
2654 }
drhdc951362022-03-05 23:52:05 +00002655 if( sqlite3BtreeEof(pC->uc.pCursor) ){
drh8f32d922022-03-05 19:36:29 +00002656 sqlite3VdbeMemSetNull(pOut);
2657 }else{
2658 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
2659 }
drh2fc865c2017-12-16 20:20:37 +00002660 }
2661 break;
2662}
drh092457b2017-12-29 15:04:49 +00002663#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
drh2fc865c2017-12-16 20:20:37 +00002664
drh3e9ca092009-09-08 01:14:48 +00002665/* Opcode: Column P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00002666** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002667**
danielk1977cfcdaef2004-05-12 07:33:33 +00002668** Interpret the data that cursor P1 points to as a structure built using
2669** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002670** information about the format of the data.) Extract the P2-th column
2671** from this record. If there are less that (P2+1)
2672** values in the record, extract a NULL.
2673**
drh9cbf3422008-01-17 16:22:13 +00002674** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002675**
drh1cc3a362017-04-03 13:17:31 +00002676** If the record contains fewer than P2 fields, then extract a NULL. Or,
danielk19771f4aa332008-01-03 09:51:55 +00002677** if the P4 argument is a P4_MEM use the value of the P4 argument as
2678** the result.
drh3e9ca092009-09-08 01:14:48 +00002679**
drh1cc3a362017-04-03 13:17:31 +00002680** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
drhdda5c082012-03-28 13:41:10 +00002681** the result is guaranteed to only be used as the argument of a length()
2682** or typeof() function, respectively. The loading of large blobs can be
2683** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002684*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002685case OP_Column: {
drhabc38152020-07-22 13:38:04 +00002686 u32 p2; /* column number to retrieve */
drh856c1032009-06-02 15:21:42 +00002687 VdbeCursor *pC; /* The VDBE cursor */
drhfc569502022-02-25 20:11:59 +00002688 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
drhd3194f52004-05-27 19:59:32 +00002689 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002690 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002691 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002692 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002693 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002694 const u8 *zData; /* Part of the record being decoded */
2695 const u8 *zHdr; /* Next unparsed byte of the header */
2696 const u8 *zEndHdr; /* Pointer to first byte after the header */
drhc6ce38832015-10-15 21:30:24 +00002697 u64 offset64; /* 64-bit offset */
drh5a077b72011-08-29 02:16:18 +00002698 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002699 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002700
drh8c7715d2019-12-20 14:37:56 +00002701 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhfc569502022-02-25 20:11:59 +00002702 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
dande892d92016-01-29 19:29:45 +00002703 pC = p->apCsr[pOp->p1];
drhabc38152020-07-22 13:38:04 +00002704 p2 = (u32)pOp->p2;
dande892d92016-01-29 19:29:45 +00002705
drhfc569502022-02-25 20:11:59 +00002706op_column_restart:
danielk19776c924092007-11-12 08:09:34 +00002707 assert( pC!=0 );
drh7c960392022-04-13 18:20:23 +00002708 assert( p2<(u32)pC->nField
2709 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) );
drhb53a5a92014-10-12 22:37:22 +00002710 aOffset = pC->aOffset;
drhb2486682022-01-03 01:43:28 +00002711 assert( aOffset==pC->aType+pC->nField );
drh62aaa6c2015-11-21 17:27:42 +00002712 assert( pC->eCurType!=CURTYPE_VTAB );
drhc960dcb2015-11-20 19:22:01 +00002713 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2714 assert( pC->eCurType!=CURTYPE_SORTER );
drh399af1d2013-11-20 17:25:55 +00002715
drha43a02e2016-05-19 17:51:19 +00002716 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977192ac1d2004-05-10 07:17:30 +00002717 if( pC->nullRow ){
drh3ac62432022-04-13 17:41:03 +00002718 if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){
drhfe0cf7a2017-08-16 19:20:20 +00002719 /* For the special case of as pseudo-cursor, the seekResult field
2720 ** identifies the register that holds the record */
drhfe0cf7a2017-08-16 19:20:20 +00002721 pReg = &aMem[pC->seekResult];
drhc8606e42013-11-20 19:28:03 +00002722 assert( pReg->flags & MEM_Blob );
2723 assert( memIsValid(pReg) );
drh6cd8c8c2017-08-15 14:14:36 +00002724 pC->payloadSize = pC->szRow = pReg->n;
drhc8606e42013-11-20 19:28:03 +00002725 pC->aRow = (u8*)pReg->z;
2726 }else{
drhfc569502022-02-25 20:11:59 +00002727 pDest = &aMem[pOp->p3];
2728 memAboutToChange(p, pDest);
drh6b5631e2014-11-05 15:57:39 +00002729 sqlite3VdbeMemSetNull(pDest);
drh399af1d2013-11-20 17:25:55 +00002730 goto op_column_out;
2731 }
danielk1977192ac1d2004-05-10 07:17:30 +00002732 }else{
drh06a09a82016-11-25 17:03:03 +00002733 pCrsr = pC->uc.pCursor;
drhfc569502022-02-25 20:11:59 +00002734 if( pC->deferredMoveto ){
2735 u32 iMap;
2736 assert( !pC->isEphemeral );
2737 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){
2738 pC = pC->pAltCursor;
2739 p2 = iMap - 1;
2740 goto op_column_restart;
2741 }
2742 rc = sqlite3VdbeFinishMoveto(pC);
2743 if( rc ) goto abort_due_to_error;
2744 }else if( sqlite3BtreeCursorHasMoved(pCrsr) ){
2745 rc = sqlite3VdbeHandleMovedCursor(pC);
2746 if( rc ) goto abort_due_to_error;
2747 goto op_column_restart;
2748 }
drhc960dcb2015-11-20 19:22:01 +00002749 assert( pC->eCurType==CURTYPE_BTREE );
drhc8606e42013-11-20 19:28:03 +00002750 assert( pCrsr );
drha7c90c42016-06-04 20:37:10 +00002751 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2752 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
drh6cd8c8c2017-08-15 14:14:36 +00002753 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
2754 assert( pC->szRow<=pC->payloadSize );
2755 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
danielk1977192ac1d2004-05-10 07:17:30 +00002756 }
drhb73857f2006-03-17 00:25:59 +00002757 pC->cacheStatus = p->cacheCtr;
drhc2808f32022-04-02 22:47:47 +00002758 if( (aOffset[0] = pC->aRow[0])<0x80 ){
2759 pC->iHdrOffset = 1;
2760 }else{
2761 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset);
2762 }
drh399af1d2013-11-20 17:25:55 +00002763 pC->nHdrParsed = 0;
drh35cd6432009-06-05 14:17:21 +00002764
drh1f613c42017-08-16 14:16:19 +00002765 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
drhc81aa2e2014-10-11 23:31:52 +00002766 /* pC->aRow does not have to hold the entire row, but it does at least
2767 ** need to cover the header of the record. If pC->aRow does not contain
2768 ** the complete header, then set it to zero, forcing the header to be
2769 ** dynamically allocated. */
2770 pC->aRow = 0;
2771 pC->szRow = 0;
drh848a3322015-10-16 12:53:47 +00002772
2773 /* Make sure a corrupt database has not given us an oversize header.
2774 ** Do this now to avoid an oversize memory allocation.
2775 **
2776 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2777 ** types use so much data space that there can only be 4096 and 32 of
2778 ** them, respectively. So the maximum header length results from a
2779 ** 3-byte type for each of the maximum of 32768 columns plus three
2780 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2781 */
drh1f613c42017-08-16 14:16:19 +00002782 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
drh74588ce2017-09-13 00:13:05 +00002783 goto op_column_corrupt;
drh848a3322015-10-16 12:53:47 +00002784 }
drh95b225a2017-08-16 11:04:22 +00002785 }else{
2786 /* This is an optimization. By skipping over the first few tests
2787 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
2788 ** measurable performance gain.
2789 **
drh1f613c42017-08-16 14:16:19 +00002790 ** This branch is taken even if aOffset[0]==0. Such a record is never
drh95b225a2017-08-16 11:04:22 +00002791 ** generated by SQLite, and could be considered corruption, but we
drh1f613c42017-08-16 14:16:19 +00002792 ** accept it for historical reasons. When aOffset[0]==0, the code this
drh95b225a2017-08-16 11:04:22 +00002793 ** branch jumps to reads past the end of the record, but never more
2794 ** than a few bytes. Even if the record occurs at the end of the page
2795 ** content area, the "page header" comes after the page content and so
2796 ** this overread is harmless. Similar overreads can occur for a corrupt
2797 ** database file.
drh0eda6cd2016-05-19 16:58:42 +00002798 */
2799 zData = pC->aRow;
2800 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
drh1f613c42017-08-16 14:16:19 +00002801 testcase( aOffset[0]==0 );
drh0eda6cd2016-05-19 16:58:42 +00002802 goto op_column_read_header;
drhc81aa2e2014-10-11 23:31:52 +00002803 }
drhfc569502022-02-25 20:11:59 +00002804 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){
2805 rc = sqlite3VdbeHandleMovedCursor(pC);
2806 if( rc ) goto abort_due_to_error;
2807 goto op_column_restart;
drh399af1d2013-11-20 17:25:55 +00002808 }
drh35cd6432009-06-05 14:17:21 +00002809
drh399af1d2013-11-20 17:25:55 +00002810 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002811 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002812 */
drhc8606e42013-11-20 19:28:03 +00002813 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002814 /* If there is more header available for parsing in the record, try
2815 ** to extract additional fields up through the p2+1-th field
drh35cd6432009-06-05 14:17:21 +00002816 */
drhc8606e42013-11-20 19:28:03 +00002817 if( pC->iHdrOffset<aOffset[0] ){
2818 /* Make sure zData points to enough of the record to cover the header. */
2819 if( pC->aRow==0 ){
2820 memset(&sMem, 0, sizeof(sMem));
drh2a740062020-02-05 18:28:17 +00002821 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
drh9467abf2016-02-17 18:44:11 +00002822 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhc8606e42013-11-20 19:28:03 +00002823 zData = (u8*)sMem.z;
2824 }else{
2825 zData = pC->aRow;
drh9188b382004-05-14 21:12:22 +00002826 }
drhc8606e42013-11-20 19:28:03 +00002827
drh0c8f7602014-09-19 16:56:45 +00002828 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drh0eda6cd2016-05-19 16:58:42 +00002829 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002830 i = pC->nHdrParsed;
drhc6ce38832015-10-15 21:30:24 +00002831 offset64 = aOffset[i];
drhc8606e42013-11-20 19:28:03 +00002832 zHdr = zData + pC->iHdrOffset;
2833 zEndHdr = zData + aOffset[0];
drh95b225a2017-08-16 11:04:22 +00002834 testcase( zHdr>=zEndHdr );
drhc8606e42013-11-20 19:28:03 +00002835 do{
drhc332e042019-02-12 21:04:33 +00002836 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
drhc8606e42013-11-20 19:28:03 +00002837 zHdr++;
drhfaf37272015-10-16 14:23:42 +00002838 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002839 }else{
drhc8606e42013-11-20 19:28:03 +00002840 zHdr += sqlite3GetVarint32(zHdr, &t);
drhc332e042019-02-12 21:04:33 +00002841 pC->aType[i] = t;
drhfaf37272015-10-16 14:23:42 +00002842 offset64 += sqlite3VdbeSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002843 }
drhc332e042019-02-12 21:04:33 +00002844 aOffset[++i] = (u32)(offset64 & 0xffffffff);
drh8deae5a2020-07-29 12:23:20 +00002845 }while( (u32)i<=p2 && zHdr<zEndHdr );
drh170c2762016-05-20 21:40:11 +00002846
drh8dd83622014-10-13 23:39:02 +00002847 /* The record is corrupt if any of the following are true:
2848 ** (1) the bytes of the header extend past the declared header size
drh8dd83622014-10-13 23:39:02 +00002849 ** (2) the entire header was used but not all data was used
drh8dd83622014-10-13 23:39:02 +00002850 ** (3) the end of the data extends beyond the end of the record.
drhc8606e42013-11-20 19:28:03 +00002851 */
drhc6ce38832015-10-15 21:30:24 +00002852 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2853 || (offset64 > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002854 ){
drh95b225a2017-08-16 11:04:22 +00002855 if( aOffset[0]==0 ){
2856 i = 0;
2857 zHdr = zEndHdr;
2858 }else{
2859 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
drh74588ce2017-09-13 00:13:05 +00002860 goto op_column_corrupt;
drh95b225a2017-08-16 11:04:22 +00002861 }
danielk1977dedf45b2006-01-13 17:12:01 +00002862 }
drhddb2b4a2016-03-25 12:10:32 +00002863
drh170c2762016-05-20 21:40:11 +00002864 pC->nHdrParsed = i;
2865 pC->iHdrOffset = (u32)(zHdr - zData);
2866 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
mistachkin8c7cd6a2015-12-16 21:09:53 +00002867 }else{
drh9fbc8852016-01-04 03:48:46 +00002868 t = 0;
drh9188b382004-05-14 21:12:22 +00002869 }
drhd3194f52004-05-27 19:59:32 +00002870
drhf2db3382015-04-30 20:33:25 +00002871 /* If after trying to extract new entries from the header, nHdrParsed is
drh380d6852013-11-20 20:58:00 +00002872 ** still not up to p2, that means that the record has fewer than p2
2873 ** columns. So the result will be either the default value or a NULL.
drhd3194f52004-05-27 19:59:32 +00002874 */
drhc8606e42013-11-20 19:28:03 +00002875 if( pC->nHdrParsed<=p2 ){
drhfc569502022-02-25 20:11:59 +00002876 pDest = &aMem[pOp->p3];
2877 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002878 if( pOp->p4type==P4_MEM ){
2879 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2880 }else{
drh22e8d832014-10-29 00:58:38 +00002881 sqlite3VdbeMemSetNull(pDest);
drhc8606e42013-11-20 19:28:03 +00002882 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002883 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002884 }
drh95fa6062015-10-16 13:50:08 +00002885 }else{
2886 t = pC->aType[p2];
danielk1977cfcdaef2004-05-12 07:33:33 +00002887 }
danielk1977192ac1d2004-05-10 07:17:30 +00002888
drh380d6852013-11-20 20:58:00 +00002889 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002890 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002891 ** all valid.
drh9188b382004-05-14 21:12:22 +00002892 */
drhc8606e42013-11-20 19:28:03 +00002893 assert( p2<pC->nHdrParsed );
2894 assert( rc==SQLITE_OK );
drhfc569502022-02-25 20:11:59 +00002895 pDest = &aMem[pOp->p3];
2896 memAboutToChange(p, pDest);
drh75fd0542014-03-01 16:24:44 +00002897 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drha1851ef2016-05-20 19:51:28 +00002898 if( VdbeMemDynamic(pDest) ){
2899 sqlite3VdbeMemSetNull(pDest);
2900 }
drh95fa6062015-10-16 13:50:08 +00002901 assert( t==pC->aType[p2] );
drhc8606e42013-11-20 19:28:03 +00002902 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002903 /* This is the common case where the desired content fits on the original
2904 ** page - where the content is not on an overflow page */
drh69f6e252016-01-11 18:05:00 +00002905 zData = pC->aRow + aOffset[p2];
2906 if( t<12 ){
2907 sqlite3VdbeSerialGet(zData, t, pDest);
2908 }else{
2909 /* If the column value is a string, we need a persistent value, not
2910 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2911 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2912 */
2913 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
2914 pDest->n = len = (t-12)/2;
drha1851ef2016-05-20 19:51:28 +00002915 pDest->enc = encoding;
drh69f6e252016-01-11 18:05:00 +00002916 if( pDest->szMalloc < len+2 ){
drh9090df62022-02-26 14:39:08 +00002917 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
drh69f6e252016-01-11 18:05:00 +00002918 pDest->flags = MEM_Null;
2919 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
2920 }else{
2921 pDest->z = pDest->zMalloc;
2922 }
2923 memcpy(pDest->z, zData, len);
2924 pDest->z[len] = 0;
2925 pDest->z[len+1] = 0;
2926 pDest->flags = aFlag[t&1];
2927 }
danielk197736963fd2005-02-19 08:18:05 +00002928 }else{
drha1851ef2016-05-20 19:51:28 +00002929 pDest->enc = encoding;
drh58c96082013-12-23 11:33:32 +00002930 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002931 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2932 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2933 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002934 ){
drh2a2a6962014-09-16 18:22:44 +00002935 /* Content is irrelevant for
2936 ** 1. the typeof() function,
2937 ** 2. the length(X) function if X is a blob, and
2938 ** 3. if the content length is zero.
2939 ** So we might as well use bogus content rather than reading
dan1f9144e2017-03-17 13:59:06 +00002940 ** content from disk.
2941 **
2942 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
2943 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
drhcbae3f82020-01-06 20:48:45 +00002944 ** read more. Use the global constant sqlite3CtypeMap[] as the array,
2945 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
2946 ** and it begins with a bunch of zeros.
dan1f9144e2017-03-17 13:59:06 +00002947 */
drhcbae3f82020-01-06 20:48:45 +00002948 sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002949 }else{
drh9090df62022-02-26 14:39:08 +00002950 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
drhcb3cabd2016-11-25 19:18:28 +00002951 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
drh9467abf2016-02-17 18:44:11 +00002952 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2953 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2954 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002955 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002956 }
drhd3194f52004-05-27 19:59:32 +00002957
danielk19773c9cc8d2005-01-17 03:40:08 +00002958op_column_out:
drhb7654112008-01-12 12:48:07 +00002959 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002960 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002961 break;
drh74588ce2017-09-13 00:13:05 +00002962
2963op_column_corrupt:
2964 if( aOp[0].p3>0 ){
2965 pOp = &aOp[aOp[0].p3-1];
2966 break;
2967 }else{
2968 rc = SQLITE_CORRUPT_BKPT;
2969 goto abort_due_to_error;
2970 }
danielk1977192ac1d2004-05-10 07:17:30 +00002971}
2972
drh926aac52021-11-03 14:02:48 +00002973/* Opcode: TypeCheck P1 P2 P3 P4 *
drh72532f52021-08-18 19:22:27 +00002974** Synopsis: typecheck(r[P1@P2])
2975**
2976** Apply affinities to the range of P2 registers beginning with P1.
2977** Take the affinities from the Table object in P4. If any value
2978** cannot be coerced into the correct type, then raise an error.
2979**
2980** This opcode is similar to OP_Affinity except that this opcode
2981** forces the register type to the Table column type. This is used
2982** to implement "strict affinity".
drh71c770f2021-08-19 16:29:33 +00002983**
drh926aac52021-11-03 14:02:48 +00002984** GENERATED ALWAYS AS ... STATIC columns are only checked if P3
2985** is zero. When P3 is non-zero, no type checking occurs for
2986** static generated columns. Virtual columns are computed at query time
2987** and so they are never checked.
2988**
drh71c770f2021-08-19 16:29:33 +00002989** Preconditions:
2990**
2991** <ul>
2992** <li> P2 should be the number of non-virtual columns in the
2993** table of P4.
2994** <li> Table P4 should be a STRICT table.
2995** </ul>
2996**
2997** If any precondition is false, an assertion fault occurs.
drh72532f52021-08-18 19:22:27 +00002998*/
2999case OP_TypeCheck: {
3000 Table *pTab;
3001 Column *aCol;
3002 int i;
3003
3004 assert( pOp->p4type==P4_TABLE );
3005 pTab = pOp->p4.pTab;
3006 assert( pTab->tabFlags & TF_Strict );
drh71c770f2021-08-19 16:29:33 +00003007 assert( pTab->nNVCol==pOp->p2 );
drh72532f52021-08-18 19:22:27 +00003008 aCol = pTab->aCol;
3009 pIn1 = &aMem[pOp->p1];
3010 for(i=0; i<pTab->nCol; i++){
drh926aac52021-11-03 14:02:48 +00003011 if( aCol[i].colFlags & COLFLAG_GENERATED ){
3012 if( aCol[i].colFlags & COLFLAG_VIRTUAL ) continue;
3013 if( pOp->p3 ){ pIn1++; continue; }
3014 }
drh72532f52021-08-18 19:22:27 +00003015 assert( pIn1 < &aMem[pOp->p1+pOp->p2] );
3016 applyAffinity(pIn1, aCol[i].affinity, encoding);
3017 if( (pIn1->flags & MEM_Null)==0 ){
3018 switch( aCol[i].eCType ){
3019 case COLTYPE_BLOB: {
3020 if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error;
3021 break;
3022 }
3023 case COLTYPE_INTEGER:
3024 case COLTYPE_INT: {
3025 if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error;
3026 break;
3027 }
3028 case COLTYPE_TEXT: {
3029 if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error;
3030 break;
3031 }
drh2a0eefd2021-08-21 20:54:19 +00003032 case COLTYPE_REAL: {
drh1f3366c2022-01-17 23:37:25 +00003033 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real );
3034 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_IntReal );
drh72532f52021-08-18 19:22:27 +00003035 if( pIn1->flags & MEM_Int ){
3036 /* When applying REAL affinity, if the result is still an MEM_Int
3037 ** that will fit in 6 bytes, then change the type to MEM_IntReal
3038 ** so that we keep the high-resolution integer value but know that
3039 ** the type really wants to be REAL. */
3040 testcase( pIn1->u.i==140737488355328LL );
3041 testcase( pIn1->u.i==140737488355327LL );
3042 testcase( pIn1->u.i==-140737488355328LL );
3043 testcase( pIn1->u.i==-140737488355329LL );
3044 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
3045 pIn1->flags |= MEM_IntReal;
3046 pIn1->flags &= ~MEM_Int;
3047 }else{
3048 pIn1->u.r = (double)pIn1->u.i;
3049 pIn1->flags |= MEM_Real;
3050 pIn1->flags &= ~MEM_Int;
3051 }
drh1f3366c2022-01-17 23:37:25 +00003052 }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){
drh72532f52021-08-18 19:22:27 +00003053 goto vdbe_type_error;
3054 }
3055 break;
3056 }
drh2a0eefd2021-08-21 20:54:19 +00003057 default: {
drhb9fd0102021-08-23 10:28:02 +00003058 /* COLTYPE_ANY. Accept anything. */
drh2a0eefd2021-08-21 20:54:19 +00003059 break;
3060 }
drh72532f52021-08-18 19:22:27 +00003061 }
3062 }
3063 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
3064 pIn1++;
3065 }
3066 assert( pIn1 == &aMem[pOp->p1+pOp->p2] );
3067 break;
3068
3069vdbe_type_error:
drhfaf9c772021-08-20 08:05:42 +00003070 sqlite3VdbeError(p, "cannot store %s value in %s column %s.%s",
3071 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1],
3072 pTab->zName, aCol[i].zCnName);
drh72532f52021-08-18 19:22:27 +00003073 rc = SQLITE_CONSTRAINT_DATATYPE;
3074 goto abort_due_to_error;
3075}
3076
danielk1977751de562008-04-18 09:01:15 +00003077/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00003078** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00003079**
3080** Apply affinities to a range of P2 registers starting with P1.
3081**
drhbb6783b2017-04-29 18:02:49 +00003082** P4 is a string that is P2 characters long. The N-th character of the
3083** string indicates the column affinity that should be used for the N-th
danielk1977751de562008-04-18 09:01:15 +00003084** memory cell in the range.
3085*/
3086case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00003087 const char *zAffinity; /* The affinity to be applied */
danielk1977751de562008-04-18 09:01:15 +00003088
drh856c1032009-06-02 15:21:42 +00003089 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00003090 assert( zAffinity!=0 );
drh662c50e2017-04-01 20:14:01 +00003091 assert( pOp->p2>0 );
drh039fc322009-11-17 18:31:47 +00003092 assert( zAffinity[pOp->p2]==0 );
3093 pIn1 = &aMem[pOp->p1];
drh122c5142019-07-29 05:23:01 +00003094 while( 1 /*exit-by-break*/ ){
drh9f6168b2016-03-19 23:32:58 +00003095 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
drhb5f62432019-12-10 02:48:41 +00003096 assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) );
drh83a1daf2019-05-01 18:59:33 +00003097 applyAffinity(pIn1, zAffinity[0], encoding);
3098 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
drh337cc392019-07-29 06:06:53 +00003099 /* When applying REAL affinity, if the result is still an MEM_Int
3100 ** that will fit in 6 bytes, then change the type to MEM_IntReal
3101 ** so that we keep the high-resolution integer value but know that
3102 ** the type really wants to be REAL. */
3103 testcase( pIn1->u.i==140737488355328LL );
3104 testcase( pIn1->u.i==140737488355327LL );
3105 testcase( pIn1->u.i==-140737488355328LL );
3106 testcase( pIn1->u.i==-140737488355329LL );
3107 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
3108 pIn1->flags |= MEM_IntReal;
3109 pIn1->flags &= ~MEM_Int;
3110 }else{
3111 pIn1->u.r = (double)pIn1->u.i;
3112 pIn1->flags |= MEM_Real;
3113 pIn1->flags &= ~MEM_Int;
3114 }
drh83a1daf2019-05-01 18:59:33 +00003115 }
drh6fcc1ec2019-05-01 14:41:47 +00003116 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
drh83a1daf2019-05-01 18:59:33 +00003117 zAffinity++;
3118 if( zAffinity[0]==0 ) break;
drh039fc322009-11-17 18:31:47 +00003119 pIn1++;
drh83a1daf2019-05-01 18:59:33 +00003120 }
danielk1977751de562008-04-18 09:01:15 +00003121 break;
3122}
3123
drh1db639c2008-01-17 02:36:28 +00003124/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003125** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00003126**
drh710c4842010-08-30 01:17:20 +00003127** Convert P2 registers beginning with P1 into the [record format]
3128** use as a data record in a database table or as a key
3129** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00003130**
drhbb6783b2017-04-29 18:02:49 +00003131** P4 may be a string that is P2 characters long. The N-th character of the
3132** string indicates the column affinity that should be used for the N-th
drh9cbf3422008-01-17 16:22:13 +00003133** field of the index key.
drh7a224de2004-06-02 01:22:02 +00003134**
drh8a512562005-11-14 22:29:05 +00003135** The mapping from character to affinity is given by the SQLITE_AFF_
3136** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00003137**
drh05883a32015-06-02 15:32:08 +00003138** If P4 is NULL then all index fields have the affinity BLOB.
drhda369332020-06-29 20:09:04 +00003139**
3140** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM
3141** compile-time option is enabled:
3142**
3143** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index
3144** of the right-most table that can be null-trimmed.
3145**
3146** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value
3147** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to
3148** accept no-change records with serial_type 10. This value is
3149** only used inside an assert() and does not affect the end result.
drh7f057c92005-06-24 03:53:06 +00003150*/
drh1db639c2008-01-17 02:36:28 +00003151case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00003152 Mem *pRec; /* The new record */
3153 u64 nData; /* Number of bytes of data space */
3154 int nHdr; /* Number of bytes of header space */
3155 i64 nByte; /* Data space required for this record */
drh4a335072015-04-11 02:08:48 +00003156 i64 nZero; /* Number of zero bytes at the end of the record */
drh856c1032009-06-02 15:21:42 +00003157 int nVarint; /* Number of bytes in a varint */
3158 u32 serial_type; /* Type field */
3159 Mem *pData0; /* First field to be combined into the record */
3160 Mem *pLast; /* Last field of the record */
3161 int nField; /* Number of fields in the record */
3162 char *zAffinity; /* The affinity string for the record */
drhbe37c122015-10-16 14:54:17 +00003163 u32 len; /* Length of a field */
drhb70b0df2019-04-30 01:08:42 +00003164 u8 *zHdr; /* Where to write next byte of the header */
3165 u8 *zPayload; /* Where to write next byte of the payload */
drh856c1032009-06-02 15:21:42 +00003166
drhf3218fe2004-05-28 08:21:02 +00003167 /* Assuming the record contains N fields, the record format looks
3168 ** like this:
3169 **
drh7a224de2004-06-02 01:22:02 +00003170 ** ------------------------------------------------------------------------
3171 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
3172 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00003173 **
drh9cbf3422008-01-17 16:22:13 +00003174 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00003175 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00003176 **
3177 ** Each type field is a varint representing the serial type of the
3178 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00003179 ** hdr-size field is also a varint which is the offset from the beginning
3180 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00003181 */
drh856c1032009-06-02 15:21:42 +00003182 nData = 0; /* Number of bytes of data space */
3183 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00003184 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00003185 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00003186 zAffinity = pOp->p4.z;
drh9f6168b2016-03-19 23:32:58 +00003187 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00003188 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00003189 nField = pOp->p2;
3190 pLast = &pData0[nField-1];
danielk19778d059842004-05-12 11:24:02 +00003191
drh2b4ded92010-09-27 21:09:31 +00003192 /* Identify the output register */
3193 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
3194 pOut = &aMem[pOp->p3];
3195 memAboutToChange(p, pOut);
3196
drh3e6c0602013-12-10 20:53:01 +00003197 /* Apply the requested affinity to all inputs
3198 */
3199 assert( pData0<=pLast );
3200 if( zAffinity ){
3201 pRec = pData0;
3202 do{
drh5ad12512019-05-09 16:22:51 +00003203 applyAffinity(pRec, zAffinity[0], encoding);
danbe812622019-05-17 15:59:11 +00003204 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
3205 pRec->flags |= MEM_IntReal;
3206 pRec->flags &= ~(MEM_Int);
3207 }
drh5ad12512019-05-09 16:22:51 +00003208 REGISTER_TRACE((int)(pRec-aMem), pRec);
3209 zAffinity++;
3210 pRec++;
drh57bf4a82014-02-17 14:59:22 +00003211 assert( zAffinity[0]==0 || pRec<=pLast );
3212 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00003213 }
3214
drhd447dce2017-01-25 20:55:11 +00003215#ifdef SQLITE_ENABLE_NULL_TRIM
drh585ce192017-01-25 14:58:27 +00003216 /* NULLs can be safely trimmed from the end of the record, as long as
3217 ** as the schema format is 2 or more and none of the omitted columns
3218 ** have a non-NULL default value. Also, the record must be left with
3219 ** at least one field. If P5>0 then it will be one more than the
3220 ** index of the right-most column with a non-NULL default value */
3221 if( pOp->p5 ){
3222 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
3223 pLast--;
3224 nField--;
3225 }
3226 }
drhd447dce2017-01-25 20:55:11 +00003227#endif
drh585ce192017-01-25 14:58:27 +00003228
drhf3218fe2004-05-28 08:21:02 +00003229 /* Loop through the elements that will make up the record to figure
drh76fd7be2019-07-11 19:50:18 +00003230 ** out how much space is required for the new record. After this loop,
3231 ** the Mem.uTemp field of each term should hold the serial-type that will
3232 ** be used for that term in the generated record:
3233 **
3234 ** Mem.uTemp value type
3235 ** --------------- ---------------
3236 ** 0 NULL
3237 ** 1 1-byte signed integer
3238 ** 2 2-byte signed integer
3239 ** 3 3-byte signed integer
3240 ** 4 4-byte signed integer
3241 ** 5 6-byte signed integer
3242 ** 6 8-byte signed integer
3243 ** 7 IEEE float
3244 ** 8 Integer constant 0
3245 ** 9 Integer constant 1
3246 ** 10,11 reserved for expansion
3247 ** N>=12 and even BLOB
3248 ** N>=13 and odd text
3249 **
3250 ** The following additional values are computed:
3251 ** nHdr Number of bytes needed for the record header
3252 ** nData Number of bytes of data space needed for the record
3253 ** nZero Zero bytes at the end of the record
danielk19778d059842004-05-12 11:24:02 +00003254 */
drh038b7bc2013-12-09 23:17:22 +00003255 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00003256 do{
drh2b4ded92010-09-27 21:09:31 +00003257 assert( memIsValid(pRec) );
drhc1da4392019-07-11 19:22:36 +00003258 if( pRec->flags & MEM_Null ){
3259 if( pRec->flags & MEM_Zero ){
drh41fb3672018-01-12 23:18:38 +00003260 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
3261 ** table methods that never invoke sqlite3_result_xxxxx() while
3262 ** computing an unchanging column value in an UPDATE statement.
3263 ** Give such values a special internal-use-only serial-type of 10
3264 ** so that they can be passed through to xUpdate and have
3265 ** a true sqlite3_value_nochange(). */
drhda369332020-06-29 20:09:04 +00003266#ifndef SQLITE_ENABLE_NULL_TRIM
drh41fb3672018-01-12 23:18:38 +00003267 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
drhda369332020-06-29 20:09:04 +00003268#endif
drhc1da4392019-07-11 19:22:36 +00003269 pRec->uTemp = 10;
drh038b7bc2013-12-09 23:17:22 +00003270 }else{
drh76fd7be2019-07-11 19:50:18 +00003271 pRec->uTemp = 0;
drh038b7bc2013-12-09 23:17:22 +00003272 }
drhc1da4392019-07-11 19:22:36 +00003273 nHdr++;
3274 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
3275 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
3276 i64 i = pRec->u.i;
drh9c3bb592019-07-30 21:00:13 +00003277 u64 uu;
drhc1da4392019-07-11 19:22:36 +00003278 testcase( pRec->flags & MEM_Int );
3279 testcase( pRec->flags & MEM_IntReal );
3280 if( i<0 ){
drh9c3bb592019-07-30 21:00:13 +00003281 uu = ~i;
drhc1da4392019-07-11 19:22:36 +00003282 }else{
drh9c3bb592019-07-30 21:00:13 +00003283 uu = i;
drhc1da4392019-07-11 19:22:36 +00003284 }
3285 nHdr++;
drh9c3bb592019-07-30 21:00:13 +00003286 testcase( uu==127 ); testcase( uu==128 );
3287 testcase( uu==32767 ); testcase( uu==32768 );
3288 testcase( uu==8388607 ); testcase( uu==8388608 );
drh16f56e82022-02-21 14:30:59 +00003289 testcase( uu==2147483647 ); testcase( uu==2147483648LL );
drh9c3bb592019-07-30 21:00:13 +00003290 testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL );
3291 if( uu<=127 ){
drha0318fd2022-02-26 23:01:25 +00003292 if( (i&1)==i && p->minWriteFileFormat>=4 ){
drh9c3bb592019-07-30 21:00:13 +00003293 pRec->uTemp = 8+(u32)uu;
drhc1da4392019-07-11 19:22:36 +00003294 }else{
3295 nData++;
3296 pRec->uTemp = 1;
3297 }
drh9c3bb592019-07-30 21:00:13 +00003298 }else if( uu<=32767 ){
drhc1da4392019-07-11 19:22:36 +00003299 nData += 2;
3300 pRec->uTemp = 2;
drh9c3bb592019-07-30 21:00:13 +00003301 }else if( uu<=8388607 ){
drhc1da4392019-07-11 19:22:36 +00003302 nData += 3;
3303 pRec->uTemp = 3;
drh9c3bb592019-07-30 21:00:13 +00003304 }else if( uu<=2147483647 ){
drhc1da4392019-07-11 19:22:36 +00003305 nData += 4;
3306 pRec->uTemp = 4;
drh9c3bb592019-07-30 21:00:13 +00003307 }else if( uu<=140737488355327LL ){
drhc1da4392019-07-11 19:22:36 +00003308 nData += 6;
3309 pRec->uTemp = 5;
3310 }else{
3311 nData += 8;
3312 if( pRec->flags & MEM_IntReal ){
3313 /* If the value is IntReal and is going to take up 8 bytes to store
3314 ** as an integer, then we might as well make it an 8-byte floating
3315 ** point value */
3316 pRec->u.r = (double)pRec->u.i;
3317 pRec->flags &= ~MEM_IntReal;
3318 pRec->flags |= MEM_Real;
3319 pRec->uTemp = 7;
3320 }else{
3321 pRec->uTemp = 6;
3322 }
3323 }
3324 }else if( pRec->flags & MEM_Real ){
3325 nHdr++;
3326 nData += 8;
3327 pRec->uTemp = 7;
3328 }else{
3329 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
3330 assert( pRec->n>=0 );
3331 len = (u32)pRec->n;
3332 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
3333 if( pRec->flags & MEM_Zero ){
3334 serial_type += pRec->u.nZero*2;
3335 if( nData ){
3336 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
3337 len += pRec->u.nZero;
3338 }else{
3339 nZero += pRec->u.nZero;
3340 }
3341 }
3342 nData += len;
3343 nHdr += sqlite3VarintLen(serial_type);
3344 pRec->uTemp = serial_type;
drhfdf972a2007-05-02 13:30:27 +00003345 }
drh45c3c662016-04-07 14:16:16 +00003346 if( pRec==pData0 ) break;
3347 pRec--;
3348 }while(1);
danielk19773d1bfea2004-05-14 11:00:53 +00003349
drh654858d2014-11-20 02:18:14 +00003350 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
3351 ** which determines the total number of bytes in the header. The varint
3352 ** value is the size of the header in bytes including the size varint
3353 ** itself. */
drh59bf00c2013-12-08 23:33:28 +00003354 testcase( nHdr==126 );
3355 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00003356 if( nHdr<=126 ){
3357 /* The common case */
3358 nHdr += 1;
3359 }else{
3360 /* Rare case of a really large header */
3361 nVarint = sqlite3VarintLen(nHdr);
3362 nHdr += nVarint;
3363 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00003364 }
drh038b7bc2013-12-09 23:17:22 +00003365 nByte = nHdr+nData;
drhf3218fe2004-05-28 08:21:02 +00003366
danielk1977a7a8e142008-02-13 18:25:27 +00003367 /* Make sure the output register has a buffer large enough to store
3368 ** the new record. The output register (pOp->p3) is not allowed to
3369 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00003370 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00003371 */
drh0d7f0cc2018-09-21 13:07:14 +00003372 if( nByte+nZero<=pOut->szMalloc ){
3373 /* The output register is already large enough to hold the record.
3374 ** No error checks or buffer enlargement is required */
3375 pOut->z = pOut->zMalloc;
3376 }else{
3377 /* Need to make sure that the output is not too big and then enlarge
3378 ** the output register to hold the full result */
3379 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
3380 goto too_big;
3381 }
3382 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
3383 goto no_mem;
3384 }
danielk19778d059842004-05-12 11:24:02 +00003385 }
drh9c1905f2008-12-10 22:32:56 +00003386 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00003387 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00003388 if( nZero ){
drh8df32842008-12-09 02:51:23 +00003389 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00003390 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00003391 }
drhb7654112008-01-12 12:48:07 +00003392 UPDATE_MAX_BLOBSIZE(pOut);
drhb70b0df2019-04-30 01:08:42 +00003393 zHdr = (u8 *)pOut->z;
3394 zPayload = zHdr + nHdr;
3395
3396 /* Write the record */
drhb47b1f62022-04-01 21:01:37 +00003397 if( nHdr<0x80 ){
3398 *(zHdr++) = nHdr;
3399 }else{
3400 zHdr += sqlite3PutVarint(zHdr,nHdr);
3401 }
drhb70b0df2019-04-30 01:08:42 +00003402 assert( pData0<=pLast );
3403 pRec = pData0;
drh759e5072022-04-01 20:39:40 +00003404 while( 1 /*exit-by-break*/ ){
drhb70b0df2019-04-30 01:08:42 +00003405 serial_type = pRec->uTemp;
3406 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
drhd859dc22022-04-02 14:30:58 +00003407 ** additional varints, one per column.
3408 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
3409 ** immediately follow the header. */
3410 if( serial_type<=7 ){
drhb47b1f62022-04-01 21:01:37 +00003411 *(zHdr++) = serial_type;
drhd859dc22022-04-02 14:30:58 +00003412 if( serial_type==0 ){
3413 /* NULL value. No change in zPayload */
3414 }else{
3415 u64 v;
drh1f416db2022-04-02 20:08:48 +00003416 u32 i;
drhd859dc22022-04-02 14:30:58 +00003417 if( serial_type==7 ){
3418 assert( sizeof(v)==sizeof(pRec->u.r) );
3419 memcpy(&v, &pRec->u.r, sizeof(v));
3420 swapMixedEndianFloat(v);
3421 }else{
3422 v = pRec->u.i;
3423 }
3424 len = i = sqlite3SmallTypeSizes[serial_type];
3425 assert( i>0 );
drh2c144b02022-04-02 15:19:02 +00003426 while( 1 /*exit-by-break*/ ){
drhd859dc22022-04-02 14:30:58 +00003427 zPayload[--i] = (u8)(v&0xFF);
drh2c144b02022-04-02 15:19:02 +00003428 if( i==0 ) break;
drhd859dc22022-04-02 14:30:58 +00003429 v >>= 8;
drh2c144b02022-04-02 15:19:02 +00003430 }
drhd859dc22022-04-02 14:30:58 +00003431 zPayload += len;
3432 }
3433 }else if( serial_type<0x80 ){
3434 *(zHdr++) = serial_type;
drhd13527d2022-04-02 19:21:58 +00003435 if( serial_type>=14 && pRec->n>0 ){
3436 assert( pRec->z!=0 );
drhd859dc22022-04-02 14:30:58 +00003437 memcpy(zPayload, pRec->z, pRec->n);
3438 zPayload += pRec->n;
3439 }
drhb47b1f62022-04-01 21:01:37 +00003440 }else{
3441 zHdr += sqlite3PutVarint(zHdr, serial_type);
drhd13527d2022-04-02 19:21:58 +00003442 if( pRec->n ){
3443 assert( pRec->z!=0 );
3444 memcpy(zPayload, pRec->z, pRec->n);
3445 zPayload += pRec->n;
3446 }
drhb47b1f62022-04-01 21:01:37 +00003447 }
drh759e5072022-04-01 20:39:40 +00003448 if( pRec==pLast ) break;
3449 pRec++;
3450 }
drhb70b0df2019-04-30 01:08:42 +00003451 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
3452 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
3453
3454 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
3455 REGISTER_TRACE(pOp->p3, pOut);
danielk19778d059842004-05-12 11:24:02 +00003456 break;
3457}
3458
drh50fb7e02021-12-06 20:16:53 +00003459/* Opcode: Count P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00003460** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00003461**
3462** Store the number of entries (an integer value) in the table or index
drh9f274632020-03-17 17:11:23 +00003463** opened by cursor P1 in register P2.
3464**
3465** If P3==0, then an exact count is obtained, which involves visiting
3466** every btree page of the table. But if P3 is non-zero, an estimate
3467** is returned based on the current cursor position.
danielk1977a5533162009-02-24 10:01:51 +00003468*/
drh27a348c2015-04-13 19:14:06 +00003469case OP_Count: { /* out2 */
danielk1977a5533162009-02-24 10:01:51 +00003470 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00003471 BtCursor *pCrsr;
3472
drhc960dcb2015-11-20 19:22:01 +00003473 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
3474 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00003475 assert( pCrsr );
drh9f274632020-03-17 17:11:23 +00003476 if( pOp->p3 ){
3477 nEntry = sqlite3BtreeRowCountEst(pCrsr);
3478 }else{
3479 nEntry = 0; /* Not needed. Only used to silence a warning. */
3480 rc = sqlite3BtreeCount(db, pCrsr, &nEntry);
3481 if( rc ) goto abort_due_to_error;
3482 }
drh27a348c2015-04-13 19:14:06 +00003483 pOut = out2Prerelease(p, pOp);
danielk1977a5533162009-02-24 10:01:51 +00003484 pOut->u.i = nEntry;
drh21f6daa2019-10-11 14:21:48 +00003485 goto check_for_interrupt;
danielk1977a5533162009-02-24 10:01:51 +00003486}
danielk1977a5533162009-02-24 10:01:51 +00003487
danielk1977fd7f0452008-12-17 17:30:26 +00003488/* Opcode: Savepoint P1 * * P4 *
3489**
3490** Open, release or rollback the savepoint named by parameter P4, depending
drh2ce9b6b2019-05-10 14:03:07 +00003491** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
3492** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
3493** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
danielk1977fd7f0452008-12-17 17:30:26 +00003494*/
3495case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00003496 int p1; /* Value of P1 operand */
3497 char *zName; /* Name of savepoint */
3498 int nName;
3499 Savepoint *pNew;
3500 Savepoint *pSavepoint;
3501 Savepoint *pTmp;
3502 int iSavepoint;
3503 int ii;
3504
3505 p1 = pOp->p1;
3506 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00003507
3508 /* Assert that the p1 parameter is valid. Also that if there is no open
3509 ** transaction, then there cannot be any savepoints.
3510 */
3511 assert( db->pSavepoint==0 || db->autoCommit==0 );
3512 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
3513 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
3514 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00003515 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00003516
3517 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00003518 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00003519 /* A new savepoint cannot be created if there are active write
3520 ** statements (i.e. open read/write incremental blob handles).
3521 */
drh22c17b82015-05-15 04:13:15 +00003522 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003523 rc = SQLITE_BUSY;
3524 }else{
drh856c1032009-06-02 15:21:42 +00003525 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003526
drhbe07ec52011-06-03 12:15:26 +00003527#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00003528 /* This call is Ok even if this savepoint is actually a transaction
3529 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
3530 ** If this is a transaction savepoint being opened, it is guaranteed
3531 ** that the db->aVTrans[] array is empty. */
3532 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00003533 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
3534 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00003535 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00003536#endif
dand9495cd2011-04-27 12:08:04 +00003537
danielk1977fd7f0452008-12-17 17:30:26 +00003538 /* Create a new savepoint structure. */
drh575fad62016-02-05 13:38:36 +00003539 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
danielk1977fd7f0452008-12-17 17:30:26 +00003540 if( pNew ){
3541 pNew->zName = (char *)&pNew[1];
3542 memcpy(pNew->zName, zName, nName+1);
3543
3544 /* If there is no open transaction, then mark this as a special
3545 ** "transaction savepoint". */
3546 if( db->autoCommit ){
3547 db->autoCommit = 0;
3548 db->isTransactionSavepoint = 1;
3549 }else{
3550 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00003551 }
dan21e8d012011-03-03 20:05:59 +00003552
danielk1977fd7f0452008-12-17 17:30:26 +00003553 /* Link the new savepoint into the database handle's list. */
3554 pNew->pNext = db->pSavepoint;
3555 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00003556 pNew->nDeferredCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003557 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003558 }
3559 }
3560 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003561 assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK );
drh856c1032009-06-02 15:21:42 +00003562 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00003563
3564 /* Find the named savepoint. If there is no such savepoint, then an
3565 ** an error is returned to the user. */
3566 for(
drh856c1032009-06-02 15:21:42 +00003567 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003568 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00003569 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00003570 ){
3571 iSavepoint++;
3572 }
3573 if( !pSavepoint ){
drh22c17b82015-05-15 04:13:15 +00003574 sqlite3VdbeError(p, "no such savepoint: %s", zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003575 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00003576 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00003577 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00003578 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00003579 */
drh22c17b82015-05-15 04:13:15 +00003580 sqlite3VdbeError(p, "cannot release savepoint - "
3581 "SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003582 rc = SQLITE_BUSY;
3583 }else{
3584
3585 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00003586 ** and this is a RELEASE command, then the current transaction
3587 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00003588 */
3589 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
3590 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00003591 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003592 goto vdbe_return;
3593 }
danielk1977fd7f0452008-12-17 17:30:26 +00003594 db->autoCommit = 1;
3595 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00003596 p->pc = (int)(pOp - aOp);
danielk1977fd7f0452008-12-17 17:30:26 +00003597 db->autoCommit = 0;
3598 p->rc = rc = SQLITE_BUSY;
3599 goto vdbe_return;
3600 }
danielk197734cf35d2008-12-18 18:31:38 +00003601 rc = p->rc;
drh94649b62019-12-18 02:12:04 +00003602 if( rc ){
3603 db->autoCommit = 0;
3604 }else{
3605 db->isTransactionSavepoint = 0;
3606 }
danielk1977fd7f0452008-12-17 17:30:26 +00003607 }else{
drh47b7fc72014-11-11 01:33:57 +00003608 int isSchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003609 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00003610 if( p1==SAVEPOINT_ROLLBACK ){
drh8257aa82017-07-26 19:59:13 +00003611 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
drh31f10052012-03-31 17:17:26 +00003612 for(ii=0; ii<db->nDb; ii++){
drh77b1dee2014-11-17 17:13:06 +00003613 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
3614 SQLITE_ABORT_ROLLBACK,
drh47b7fc72014-11-11 01:33:57 +00003615 isSchemaChange==0);
dan80231042014-11-12 14:56:02 +00003616 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh31f10052012-03-31 17:17:26 +00003617 }
drh47b7fc72014-11-11 01:33:57 +00003618 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003619 assert( p1==SAVEPOINT_RELEASE );
drh47b7fc72014-11-11 01:33:57 +00003620 isSchemaChange = 0;
drh0f198a72012-02-13 16:43:16 +00003621 }
3622 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00003623 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
3624 if( rc!=SQLITE_OK ){
3625 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00003626 }
danielk1977fd7f0452008-12-17 17:30:26 +00003627 }
drh47b7fc72014-11-11 01:33:57 +00003628 if( isSchemaChange ){
drhba968db2018-07-24 22:02:12 +00003629 sqlite3ExpirePreparedStatements(db, 0);
drh81028a42012-05-15 18:28:27 +00003630 sqlite3ResetAllSchemasOfConnection(db);
drh8257aa82017-07-26 19:59:13 +00003631 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003632 }
3633 }
drh95866af2019-12-15 00:36:33 +00003634 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003635
3636 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3637 ** savepoints nested inside of the savepoint being operated on. */
3638 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00003639 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003640 db->pSavepoint = pTmp->pNext;
3641 sqlite3DbFree(db, pTmp);
3642 db->nSavepoint--;
3643 }
3644
dan1da40a32009-09-19 17:00:31 +00003645 /* If it is a RELEASE, then destroy the savepoint being operated on
3646 ** too. If it is a ROLLBACK TO, then set the number of deferred
3647 ** constraint violations present in the database to the value stored
3648 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00003649 if( p1==SAVEPOINT_RELEASE ){
3650 assert( pSavepoint==db->pSavepoint );
3651 db->pSavepoint = pSavepoint->pNext;
3652 sqlite3DbFree(db, pSavepoint);
3653 if( !isTransaction ){
3654 db->nSavepoint--;
3655 }
dan1da40a32009-09-19 17:00:31 +00003656 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003657 assert( p1==SAVEPOINT_ROLLBACK );
dan1da40a32009-09-19 17:00:31 +00003658 db->nDeferredCons = pSavepoint->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003659 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003660 }
dand9495cd2011-04-27 12:08:04 +00003661
danea8562e2015-04-18 16:25:54 +00003662 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
dand9495cd2011-04-27 12:08:04 +00003663 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
3664 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3665 }
danielk1977fd7f0452008-12-17 17:30:26 +00003666 }
3667 }
drh9467abf2016-02-17 18:44:11 +00003668 if( rc ) goto abort_due_to_error;
drh8703edd2022-04-03 22:35:13 +00003669 if( p->eVdbeState==VDBE_HALT_STATE ){
3670 rc = SQLITE_DONE;
3671 goto vdbe_return;
3672 }
danielk1977fd7f0452008-12-17 17:30:26 +00003673 break;
3674}
3675
drh98757152008-01-09 23:04:12 +00003676/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00003677**
3678** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00003679** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00003680** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3681** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00003682**
3683** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00003684*/
drh9cbf3422008-01-17 16:22:13 +00003685case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00003686 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00003687 int iRollback;
danielk19771d850a72004-05-31 08:26:49 +00003688
drh856c1032009-06-02 15:21:42 +00003689 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00003690 iRollback = pOp->p2;
drhad4a4b82008-11-05 16:37:34 +00003691 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00003692 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00003693 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00003694 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00003695
drhb0c88652016-02-01 13:21:13 +00003696 if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00003697 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00003698 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00003699 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00003700 db->autoCommit = 1;
drhb0c88652016-02-01 13:21:13 +00003701 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3702 /* If this instruction implements a COMMIT and other VMs are writing
3703 ** return an error indicating that the other VMs must complete first.
3704 */
3705 sqlite3VdbeError(p, "cannot commit transaction - "
3706 "SQL statements in progress");
3707 rc = SQLITE_BUSY;
drh9467abf2016-02-17 18:44:11 +00003708 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00003709 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003710 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00003711 }else{
shane7d3846a2008-12-11 02:58:26 +00003712 db->autoCommit = (u8)desiredAutoCommit;
drh8ff25872015-07-31 18:59:56 +00003713 }
3714 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3715 p->pc = (int)(pOp - aOp);
3716 db->autoCommit = (u8)(1-desiredAutoCommit);
3717 p->rc = rc = SQLITE_BUSY;
3718 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003719 }
danielk1977fd7f0452008-12-17 17:30:26 +00003720 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00003721 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00003722 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00003723 }else{
drh900b31e2007-08-28 02:27:51 +00003724 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00003725 }
drh900b31e2007-08-28 02:27:51 +00003726 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003727 }else{
drh22c17b82015-05-15 04:13:15 +00003728 sqlite3VdbeError(p,
drhad4a4b82008-11-05 16:37:34 +00003729 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00003730 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00003731 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00003732
3733 rc = SQLITE_ERROR;
drh9467abf2016-02-17 18:44:11 +00003734 goto abort_due_to_error;
drh663fc632002-02-02 18:49:19 +00003735 }
drh8616cff2019-07-13 16:15:23 +00003736 /*NOTREACHED*/ assert(0);
drh663fc632002-02-02 18:49:19 +00003737}
3738
drhb22f7c82014-02-06 23:56:27 +00003739/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003740**
drh05a86c52014-02-16 01:55:49 +00003741** Begin a transaction on database P1 if a transaction is not already
3742** active.
3743** If P2 is non-zero, then a write-transaction is started, or if a
3744** read-transaction is already active, it is upgraded to a write-transaction.
drh1ca037f2020-10-12 13:24:00 +00003745** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
3746** then an exclusive transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00003747**
drh001bbcb2003-03-19 03:14:00 +00003748** P1 is the index of the database file on which the transaction is
3749** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00003750** file used for temporary tables. Indices of 2 or more are used for
3751** attached databases.
drhcabb0812002-09-14 13:47:32 +00003752**
dane0af83a2009-09-08 19:15:01 +00003753** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3754** true (this flag is set if the Vdbe may modify more than one row and may
3755** throw an ABORT exception), a statement transaction may also be opened.
3756** More specifically, a statement transaction is opened iff the database
3757** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00003758** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00003759** VDBE to be rolled back after an error without having to roll back the
3760** entire transaction. If no error is encountered, the statement transaction
3761** will automatically commit when the VDBE halts.
3762**
drhb22f7c82014-02-06 23:56:27 +00003763** If P5!=0 then this opcode also checks the schema cookie against P3
3764** and the schema generation counter against P4.
3765** The cookie changes its value whenever the database schema changes.
3766** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00003767** and that the current process needs to reread the schema. If the schema
3768** cookie in P3 differs from the schema cookie in the database header or
3769** if the schema generation counter in P4 differs from the current
3770** generation counter, then an SQLITE_SCHEMA error is raised and execution
3771** halts. The sqlite3_step() wrapper function might then reprepare the
3772** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003773*/
drh9cbf3422008-01-17 16:22:13 +00003774case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003775 Btree *pBt;
drh8ee75f72022-04-03 10:42:06 +00003776 Db *pDb;
drhbb2d9b12018-06-06 16:28:40 +00003777 int iMeta = 0;
danielk19771d850a72004-05-31 08:26:49 +00003778
drh1713afb2013-06-28 01:24:57 +00003779 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003780 assert( p->readOnly==0 || pOp->p2==0 );
drh1ca037f2020-10-12 13:24:00 +00003781 assert( pOp->p2>=0 && pOp->p2<=2 );
drh653b82a2009-06-22 11:10:47 +00003782 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003783 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh8cb63f52021-10-29 09:59:06 +00003784 assert( rc==SQLITE_OK );
drh46c425b2021-11-10 10:59:10 +00003785 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){
3786 if( db->flags & SQLITE_QueryOnly ){
3787 /* Writes prohibited by the "PRAGMA query_only=TRUE" statement */
3788 rc = SQLITE_READONLY;
3789 }else{
3790 /* Writes prohibited due to a prior SQLITE_CORRUPT in the current
3791 ** transaction */
3792 rc = SQLITE_CORRUPT;
3793 }
drh13447bf2013-07-10 13:33:49 +00003794 goto abort_due_to_error;
3795 }
drh8ee75f72022-04-03 10:42:06 +00003796 pDb = &db->aDb[pOp->p1];
3797 pBt = pDb->pBt;
danielk19771d850a72004-05-31 08:26:49 +00003798
danielk197724162fe2004-06-04 06:22:00 +00003799 if( pBt ){
drhbb2d9b12018-06-06 16:28:40 +00003800 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
drhcbd8db32015-08-20 17:18:32 +00003801 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3802 testcase( rc==SQLITE_BUSY_RECOVERY );
drh9e9f1bd2009-10-13 15:36:51 +00003803 if( rc!=SQLITE_OK ){
drhfadd2b12016-09-19 23:39:34 +00003804 if( (rc&0xff)==SQLITE_BUSY ){
3805 p->pc = (int)(pOp - aOp);
3806 p->rc = rc;
3807 goto vdbe_return;
3808 }
danielk197724162fe2004-06-04 06:22:00 +00003809 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003810 }
dane0af83a2009-09-08 19:15:01 +00003811
drh4d294482019-10-05 15:28:24 +00003812 if( p->usesStmtJournal
3813 && pOp->p2
danc0537fe2013-06-28 19:41:43 +00003814 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003815 ){
drh99744fa2020-08-25 19:09:07 +00003816 assert( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE );
dane0af83a2009-09-08 19:15:01 +00003817 if( p->iStatement==0 ){
3818 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3819 db->nStatement++;
3820 p->iStatement = db->nSavepoint + db->nStatement;
3821 }
dana311b802011-04-26 19:21:34 +00003822
drh346506f2011-05-25 01:16:42 +00003823 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003824 if( rc==SQLITE_OK ){
3825 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3826 }
dan1da40a32009-09-19 17:00:31 +00003827
3828 /* Store the current value of the database handles deferred constraint
3829 ** counter. If the statement transaction needs to be rolled back,
3830 ** the value of this counter needs to be restored too. */
3831 p->nStmtDefCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003832 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003833 }
drh397776a2018-06-06 17:45:51 +00003834 }
3835 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
drhe4e1af52021-10-29 16:19:03 +00003836 if( rc==SQLITE_OK
3837 && pOp->p5
drh8ee75f72022-04-03 10:42:06 +00003838 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i)
drh397776a2018-06-06 17:45:51 +00003839 ){
dand2ffc972020-12-10 19:20:15 +00003840 /*
3841 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3842 ** version is checked to ensure that the schema has not changed since the
3843 ** SQL statement was prepared.
3844 */
3845 sqlite3DbFree(db, p->zErrMsg);
3846 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
drhb22f7c82014-02-06 23:56:27 +00003847 /* If the schema-cookie from the database file matches the cookie
3848 ** stored with the in-memory representation of the schema, do
3849 ** not reload the schema from the database file.
3850 **
3851 ** If virtual-tables are in use, this is not just an optimization.
3852 ** Often, v-tables store their data in other SQLite tables, which
3853 ** are queried from within xNext() and other v-table methods using
3854 ** prepared queries. If such a query is out-of-date, we do not want to
3855 ** discard the database schema, as the user code implementing the
3856 ** v-table would have to be ready for the sqlite3_vtab structure itself
3857 ** to be invalidated whenever sqlite3_step() is called from within
3858 ** a v-table method.
3859 */
3860 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3861 sqlite3ResetOneSchema(db, pOp->p1);
3862 }
3863 p->expired = 1;
3864 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003865 }
drh9467abf2016-02-17 18:44:11 +00003866 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003867 break;
3868}
3869
drhb1fdb2a2008-01-05 04:06:03 +00003870/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003871**
drh9cbf3422008-01-17 16:22:13 +00003872** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003873** P3==1 is the schema version. P3==2 is the database format.
3874** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003875** the main database file and P1==1 is the database file used to store
3876** temporary tables.
drh4a324312001-12-21 14:30:42 +00003877**
drh50e5dad2001-09-15 00:57:28 +00003878** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003879** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003880** executing this instruction.
3881*/
drh27a348c2015-04-13 19:14:06 +00003882case OP_ReadCookie: { /* out2 */
drhf328bc82004-05-10 23:29:49 +00003883 int iMeta;
drh856c1032009-06-02 15:21:42 +00003884 int iDb;
3885 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003886
drh1713afb2013-06-28 01:24:57 +00003887 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003888 iDb = pOp->p1;
3889 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003890 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003891 assert( iDb>=0 && iDb<db->nDb );
3892 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003893 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003894
danielk1977602b4662009-07-02 07:47:33 +00003895 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh27a348c2015-04-13 19:14:06 +00003896 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00003897 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003898 break;
3899}
3900
drhe3863b52020-07-01 16:19:14 +00003901/* Opcode: SetCookie P1 P2 P3 * P5
drh50e5dad2001-09-15 00:57:28 +00003902**
drh1861afc2016-02-01 21:48:34 +00003903** Write the integer value P3 into cookie number P2 of database P1.
3904** P2==1 is the schema version. P2==2 is the database format.
3905** P2==3 is the recommended pager cache
danielk19770d19f7a2009-06-03 11:25:07 +00003906** size, and so forth. P1==0 is the main database file and P1==1 is the
3907** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003908**
3909** A transaction must be started before executing this opcode.
drhe3863b52020-07-01 16:19:14 +00003910**
3911** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal
3912** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
3913** has P5 set to 1, so that the internal schema version will be different
3914** from the database schema version, resulting in a schema reset.
drh50e5dad2001-09-15 00:57:28 +00003915*/
drh1861afc2016-02-01 21:48:34 +00003916case OP_SetCookie: {
drh3f7d4e42004-07-24 14:35:58 +00003917 Db *pDb;
drh4031baf2018-05-28 17:31:20 +00003918
3919 sqlite3VdbeIncrWriteCounter(p, 0);
drh4a324312001-12-21 14:30:42 +00003920 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003921 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003922 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003923 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003924 pDb = &db->aDb[pOp->p1];
3925 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003926 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drha3b321d2004-05-11 09:31:31 +00003927 /* See note about index shifting on OP_ReadCookie */
drh1861afc2016-02-01 21:48:34 +00003928 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
danielk19770d19f7a2009-06-03 11:25:07 +00003929 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003930 /* When the schema cookie changes, record the new cookie internally */
drhe3863b52020-07-01 16:19:14 +00003931 pDb->pSchema->schema_cookie = pOp->p3 - pOp->p5;
drh8257aa82017-07-26 19:59:13 +00003932 db->mDbFlags |= DBFLAG_SchemaChange;
drh44a5c022022-01-02 12:01:03 +00003933 sqlite3FkClearTriggerCache(db, pOp->p1);
danielk19770d19f7a2009-06-03 11:25:07 +00003934 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003935 /* Record changes in the file format */
drh1861afc2016-02-01 21:48:34 +00003936 pDb->pSchema->file_format = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00003937 }
drhfd426c62006-01-30 15:34:22 +00003938 if( pOp->p1==1 ){
3939 /* Invalidate all prepared statements whenever the TEMP database
3940 ** schema is changed. Ticket #1644 */
drhba968db2018-07-24 22:02:12 +00003941 sqlite3ExpirePreparedStatements(db, 0);
danfa401de2009-10-16 14:55:03 +00003942 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003943 }
drh9467abf2016-02-17 18:44:11 +00003944 if( rc ) goto abort_due_to_error;
drh50e5dad2001-09-15 00:57:28 +00003945 break;
3946}
3947
drh98757152008-01-09 23:04:12 +00003948/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003949** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003950**
drhecdc7532001-09-23 02:35:53 +00003951** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003952** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003953** P3==0 means the main database, P3==1 means the database used for
3954** temporary tables, and P3>1 means used the corresponding attached
3955** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003956** values need not be contiguous but all P1 values should be small integers.
3957** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003958**
drh8e9deb62018-06-05 13:43:02 +00003959** Allowed P5 bits:
3960** <ul>
3961** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3962** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
drh576d0a92020-03-12 17:28:27 +00003963** of OP_SeekLE/OP_IdxLT)
drh8e9deb62018-06-05 13:43:02 +00003964** </ul>
drhb19a2bc2001-09-16 00:13:26 +00003965**
danielk1977d336e222009-02-20 10:58:41 +00003966** The P4 value may be either an integer (P4_INT32) or a pointer to
3967** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00003968** object, then table being opened must be an [index b-tree] where the
3969** KeyInfo object defines the content and collating
3970** sequence of that index b-tree. Otherwise, if P4 is an integer
3971** value, then the table being opened must be a [table b-tree] with a
3972** number of columns no less than the value of P4.
drhf57b3392001-10-08 13:22:32 +00003973**
drh35263192014-07-22 20:02:19 +00003974** See also: OpenWrite, ReopenIdx
3975*/
3976/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3977** Synopsis: root=P2 iDb=P3
3978**
drh8e9deb62018-06-05 13:43:02 +00003979** The ReopenIdx opcode works like OP_OpenRead except that it first
3980** checks to see if the cursor on P1 is already open on the same
3981** b-tree and if it is this opcode becomes a no-op. In other words,
drh35263192014-07-22 20:02:19 +00003982** if the cursor is already open, do not reopen it.
3983**
drh8e9deb62018-06-05 13:43:02 +00003984** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
3985** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
3986** be the same as every other ReopenIdx or OpenRead for the same cursor
3987** number.
drh35263192014-07-22 20:02:19 +00003988**
drh8e9deb62018-06-05 13:43:02 +00003989** Allowed P5 bits:
3990** <ul>
3991** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
3992** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
drh576d0a92020-03-12 17:28:27 +00003993** of OP_SeekLE/OP_IdxLT)
drh8e9deb62018-06-05 13:43:02 +00003994** </ul>
3995**
3996** See also: OP_OpenRead, OP_OpenWrite
drh5e00f6c2001-09-13 13:46:56 +00003997*/
drh98757152008-01-09 23:04:12 +00003998/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003999** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00004000**
4001** Open a read/write cursor named P1 on the table or index whose root
drh8e9deb62018-06-05 13:43:02 +00004002** page is P2 (or whose root page is held in register P2 if the
4003** OPFLAG_P2ISREG bit is set in P5 - see below).
drhecdc7532001-09-23 02:35:53 +00004004**
danielk1977d336e222009-02-20 10:58:41 +00004005** The P4 value may be either an integer (P4_INT32) or a pointer to
4006** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00004007** object, then table being opened must be an [index b-tree] where the
4008** KeyInfo object defines the content and collating
4009** sequence of that index b-tree. Otherwise, if P4 is an integer
4010** value, then the table being opened must be a [table b-tree] with a
4011** number of columns no less than the value of P4.
jplyon5a564222003-06-02 06:15:58 +00004012**
drh8e9deb62018-06-05 13:43:02 +00004013** Allowed P5 bits:
4014** <ul>
4015** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
4016** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
drh576d0a92020-03-12 17:28:27 +00004017** of OP_SeekLE/OP_IdxLT)
drh8e9deb62018-06-05 13:43:02 +00004018** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
4019** and subsequently delete entries in an index btree. This is a
4020** hint to the storage engine that the storage engine is allowed to
4021** ignore. The hint is not used by the official SQLite b*tree storage
4022** engine, but is used by COMDB2.
4023** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
4024** as the root page, not the value of P2 itself.
4025** </ul>
drhf57b3392001-10-08 13:22:32 +00004026**
drh8e9deb62018-06-05 13:43:02 +00004027** This instruction works like OpenRead except that it opens the cursor
4028** in read/write mode.
4029**
4030** See also: OP_OpenRead, OP_ReopenIdx
drhecdc7532001-09-23 02:35:53 +00004031*/
drh35263192014-07-22 20:02:19 +00004032case OP_ReopenIdx: {
drh856c1032009-06-02 15:21:42 +00004033 int nField;
4034 KeyInfo *pKeyInfo;
drhabc38152020-07-22 13:38:04 +00004035 u32 p2;
drh856c1032009-06-02 15:21:42 +00004036 int iDb;
drhf57b3392001-10-08 13:22:32 +00004037 int wrFlag;
4038 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00004039 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00004040 Db *pDb;
drh856c1032009-06-02 15:21:42 +00004041
drhe0997b32015-03-20 14:57:50 +00004042 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh35263192014-07-22 20:02:19 +00004043 assert( pOp->p4type==P4_KEYINFO );
4044 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00004045 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00004046 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
drh4422b3a2021-06-25 14:48:24 +00004047 assert( pCur->eCurType==CURTYPE_BTREE );
4048 sqlite3BtreeClearCursor(pCur->uc.pCursor);
drhe0997b32015-03-20 14:57:50 +00004049 goto open_cursor_set_hints;
drh35263192014-07-22 20:02:19 +00004050 }
4051 /* If the cursor is not currently open or is open on a different
4052 ** index, then fall through into OP_OpenRead to force a reopen */
drh5e00f6c2001-09-13 13:46:56 +00004053case OP_OpenRead:
drh1fa509a2015-03-20 16:34:49 +00004054case OP_OpenWrite:
drh856c1032009-06-02 15:21:42 +00004055
drhe0997b32015-03-20 14:57:50 +00004056 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh1713afb2013-06-28 01:24:57 +00004057 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00004058 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
4059 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00004060
drhba968db2018-07-24 22:02:12 +00004061 if( p->expired==1 ){
drh47b7fc72014-11-11 01:33:57 +00004062 rc = SQLITE_ABORT_ROLLBACK;
drh9467abf2016-02-17 18:44:11 +00004063 goto abort_due_to_error;
danfa401de2009-10-16 14:55:03 +00004064 }
4065
drh856c1032009-06-02 15:21:42 +00004066 nField = 0;
4067 pKeyInfo = 0;
drhabc38152020-07-22 13:38:04 +00004068 p2 = (u32)pOp->p2;
drh856c1032009-06-02 15:21:42 +00004069 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00004070 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00004071 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00004072 pDb = &db->aDb[iDb];
4073 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00004074 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00004075 if( pOp->opcode==OP_OpenWrite ){
danfd261ec2015-10-22 20:54:33 +00004076 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
4077 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
drh21206082011-04-04 18:22:02 +00004078 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00004079 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
4080 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00004081 }
4082 }else{
4083 wrFlag = 0;
4084 }
dan428c2182012-08-06 18:50:11 +00004085 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00004086 assert( p2>0 );
mistachkincec5f1d2020-08-04 16:11:37 +00004087 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
drh8e9deb62018-06-05 13:43:02 +00004088 assert( pOp->opcode==OP_OpenWrite );
drha6c2ed92009-11-14 23:22:23 +00004089 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00004090 assert( memIsValid(pIn2) );
4091 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00004092 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00004093 p2 = (int)pIn2->u.i;
drh0f3f7662017-08-18 14:34:28 +00004094 /* The p2 value always comes from a prior OP_CreateBtree opcode and
drh9a65f2c2009-06-22 19:05:40 +00004095 ** that opcode will always set the p2 value to 2 or more or else fail.
4096 ** If there were a failure, the prepared statement would have halted
4097 ** before reaching this instruction. */
drh9467abf2016-02-17 18:44:11 +00004098 assert( p2>=2 );
drh5edc3122001-09-13 21:53:09 +00004099 }
danielk1977d336e222009-02-20 10:58:41 +00004100 if( pOp->p4type==P4_KEYINFO ){
4101 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00004102 assert( pKeyInfo->enc==ENC(db) );
4103 assert( pKeyInfo->db==db );
drha485ad12017-08-02 22:43:14 +00004104 nField = pKeyInfo->nAllField;
danielk1977d336e222009-02-20 10:58:41 +00004105 }else if( pOp->p4type==P4_INT32 ){
4106 nField = pOp->p4.i;
4107 }
drh653b82a2009-06-22 11:10:47 +00004108 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00004109 assert( nField>=0 );
4110 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drhb2486682022-01-03 01:43:28 +00004111 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00004112 if( pCur==0 ) goto no_mem;
drhb2486682022-01-03 01:43:28 +00004113 pCur->iDb = iDb;
drhf328bc82004-05-10 23:29:49 +00004114 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00004115 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00004116 pCur->pgnoRoot = p2;
drhb89aeb62016-01-27 15:49:32 +00004117#ifdef SQLITE_DEBUG
4118 pCur->wrFlag = wrFlag;
4119#endif
drhc960dcb2015-11-20 19:22:01 +00004120 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
danielk1977d336e222009-02-20 10:58:41 +00004121 pCur->pKeyInfo = pKeyInfo;
drh14da87f2013-11-20 21:51:33 +00004122 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00004123 ** SQLite used to check if the root-page flags were sane at this point
4124 ** and report database corruption if they were not, but this check has
4125 ** since moved into the btree layer. */
4126 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhe0997b32015-03-20 14:57:50 +00004127
4128open_cursor_set_hints:
4129 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
4130 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
drh0403cb32015-08-14 23:57:04 +00004131 testcase( pOp->p5 & OPFLAG_BULKCSR );
drh0403cb32015-08-14 23:57:04 +00004132 testcase( pOp->p2 & OPFLAG_SEEKEQ );
drhc960dcb2015-11-20 19:22:01 +00004133 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
drhf7854c72015-10-27 13:24:37 +00004134 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
drh9467abf2016-02-17 18:44:11 +00004135 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00004136 break;
4137}
4138
drhe08e8d62017-05-01 15:15:41 +00004139/* Opcode: OpenDup P1 P2 * * *
4140**
4141** Open a new cursor P1 that points to the same ephemeral table as
4142** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
4143** opcode. Only ephemeral cursors may be duplicated.
4144**
4145** Duplicate ephemeral cursors are used for self-joins of materialized views.
4146*/
4147case OP_OpenDup: {
4148 VdbeCursor *pOrig; /* The original cursor to be duplicated */
4149 VdbeCursor *pCx; /* The new cursor */
4150
4151 pOrig = p->apCsr[pOp->p2];
dan2811ea62019-12-23 14:20:46 +00004152 assert( pOrig );
drh5a4a15f2021-03-18 15:42:59 +00004153 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */
drhe08e8d62017-05-01 15:15:41 +00004154
drhb2486682022-01-03 01:43:28 +00004155 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE);
drhe08e8d62017-05-01 15:15:41 +00004156 if( pCx==0 ) goto no_mem;
4157 pCx->nullRow = 1;
4158 pCx->isEphemeral = 1;
4159 pCx->pKeyInfo = pOrig->pKeyInfo;
4160 pCx->isTable = pOrig->isTable;
drh2c041312018-12-24 02:34:49 +00004161 pCx->pgnoRoot = pOrig->pgnoRoot;
dana0f6b832019-03-14 16:36:20 +00004162 pCx->isOrdered = pOrig->isOrdered;
drhb2486682022-01-03 01:43:28 +00004163 pCx->ub.pBtx = pOrig->ub.pBtx;
drh5a4a15f2021-03-18 15:42:59 +00004164 pCx->hasBeenDuped = 1;
4165 pOrig->hasBeenDuped = 1;
drhb2486682022-01-03 01:43:28 +00004166 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
drhe08e8d62017-05-01 15:15:41 +00004167 pCx->pKeyInfo, pCx->uc.pCursor);
drh3f4df4c2017-05-02 17:54:19 +00004168 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
4169 ** opened for a database. Since there is already an open cursor when this
4170 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
4171 assert( rc==SQLITE_OK );
drhe08e8d62017-05-01 15:15:41 +00004172 break;
4173}
4174
4175
drh32881be2020-11-17 21:26:13 +00004176/* Opcode: OpenEphemeral P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004177** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00004178**
drhb9bb7c12006-06-11 23:41:55 +00004179** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00004180** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00004181** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00004182** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00004183**
drhdfe3b582019-01-04 12:35:50 +00004184** If the cursor P1 is already opened on an ephemeral table, the table
drh4afdfa12018-12-31 16:36:42 +00004185** is cleared (all content is erased).
4186**
drh25d3adb2010-04-05 15:11:08 +00004187** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00004188** The cursor points to a BTree table if P4==0 and to a BTree index
4189** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00004190** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00004191**
drh2a5d9902011-08-26 00:34:45 +00004192** The P5 parameter can be a mask of the BTREE_* flags defined
4193** in btree.h. These flags control aspects of the operation of
4194** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
4195** added automatically.
drh32881be2020-11-17 21:26:13 +00004196**
4197** If P3 is positive, then reg[P3] is modified slightly so that it
4198** can be used as zero-length data for OP_Insert. This is an optimization
4199** that avoids an extra OP_Blob opcode to initialize that register.
drh5e00f6c2001-09-13 13:46:56 +00004200*/
drha21a64d2010-04-06 22:33:55 +00004201/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00004202** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00004203**
4204** This opcode works the same as OP_OpenEphemeral. It has a
4205** different name to distinguish its use. Tables created using
4206** by this opcode will be used for automatically created transient
4207** indices in joins.
4208*/
4209case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00004210case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00004211 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00004212 KeyInfo *pKeyInfo;
4213
drhd4187c72010-08-30 22:15:45 +00004214 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00004215 SQLITE_OPEN_READWRITE |
4216 SQLITE_OPEN_CREATE |
4217 SQLITE_OPEN_EXCLUSIVE |
4218 SQLITE_OPEN_DELETEONCLOSE |
4219 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00004220 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00004221 assert( pOp->p2>=0 );
drh32881be2020-11-17 21:26:13 +00004222 if( pOp->p3>0 ){
4223 /* Make register reg[P3] into a value that can be used as the data
4224 ** form sqlite3BtreeInsert() where the length of the data is zero. */
4225 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
4226 assert( pOp->opcode==OP_OpenEphemeral );
4227 assert( aMem[pOp->p3].flags & MEM_Null );
4228 aMem[pOp->p3].n = 0;
4229 aMem[pOp->p3].z = "";
4230 }
drh4afdfa12018-12-31 16:36:42 +00004231 pCx = p->apCsr[pOp->p1];
drh7132f432021-10-20 12:52:12 +00004232 if( pCx && !pCx->hasBeenDuped && ALWAYS(pOp->p2<=pCx->nField) ){
drh5a4a15f2021-03-18 15:42:59 +00004233 /* If the ephermeral table is already open and has no duplicates from
4234 ** OP_OpenDup, then erase all existing content so that the table is
4235 ** empty again, rather than creating a new table. */
dana5129722019-05-03 18:50:24 +00004236 assert( pCx->isEphemeral );
dan855b5d12019-06-26 21:04:30 +00004237 pCx->seqCount = 0;
4238 pCx->cacheStatus = CACHE_STALE;
drhb2486682022-01-03 01:43:28 +00004239 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0);
drhd0fb7962018-12-31 17:58:05 +00004240 }else{
drhb2486682022-01-03 01:43:28 +00004241 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE);
drhd0fb7962018-12-31 17:58:05 +00004242 if( pCx==0 ) goto no_mem;
drhd0fb7962018-12-31 17:58:05 +00004243 pCx->isEphemeral = 1;
drhb2486682022-01-03 01:43:28 +00004244 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx,
drhd0fb7962018-12-31 17:58:05 +00004245 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
4246 vfsFlags);
4247 if( rc==SQLITE_OK ){
drhb2486682022-01-03 01:43:28 +00004248 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0);
daneeee8a52021-03-18 14:31:37 +00004249 if( rc==SQLITE_OK ){
4250 /* If a transient index is required, create it by calling
4251 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
4252 ** opening it. If a transient table is required, just use the
4253 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
4254 */
4255 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
4256 assert( pOp->p4type==P4_KEYINFO );
drhb2486682022-01-03 01:43:28 +00004257 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot,
daneeee8a52021-03-18 14:31:37 +00004258 BTREE_BLOBKEY | pOp->p5);
4259 if( rc==SQLITE_OK ){
4260 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
4261 assert( pKeyInfo->db==db );
4262 assert( pKeyInfo->enc==ENC(db) );
drhb2486682022-01-03 01:43:28 +00004263 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
daneeee8a52021-03-18 14:31:37 +00004264 pKeyInfo, pCx->uc.pCursor);
4265 }
4266 pCx->isTable = 0;
4267 }else{
4268 pCx->pgnoRoot = SCHEMA_ROOT;
drhb2486682022-01-03 01:43:28 +00004269 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR,
daneeee8a52021-03-18 14:31:37 +00004270 0, pCx->uc.pCursor);
4271 pCx->isTable = 1;
drhd0fb7962018-12-31 17:58:05 +00004272 }
daneeee8a52021-03-18 14:31:37 +00004273 }
4274 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
4275 if( rc ){
drhb2486682022-01-03 01:43:28 +00004276 sqlite3BtreeClose(pCx->ub.pBtx);
drhd0fb7962018-12-31 17:58:05 +00004277 }
4278 }
drh5e00f6c2001-09-13 13:46:56 +00004279 }
drh9467abf2016-02-17 18:44:11 +00004280 if( rc ) goto abort_due_to_error;
dan855b5d12019-06-26 21:04:30 +00004281 pCx->nullRow = 1;
dan5134d132011-09-02 10:31:11 +00004282 break;
4283}
4284
danfad9f9a2014-04-01 18:41:51 +00004285/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00004286**
4287** This opcode works like OP_OpenEphemeral except that it opens
4288** a transient index that is specifically designed to sort large
4289** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00004290**
4291** If argument P3 is non-zero, then it indicates that the sorter may
4292** assume that a stable sort considering the first P3 fields of each
4293** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00004294*/
drhca892a72011-09-03 00:17:51 +00004295case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00004296 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00004297
drh399af1d2013-11-20 17:25:55 +00004298 assert( pOp->p1>=0 );
4299 assert( pOp->p2>=0 );
drhb2486682022-01-03 01:43:28 +00004300 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER);
dan5134d132011-09-02 10:31:11 +00004301 if( pCx==0 ) goto no_mem;
4302 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00004303 assert( pCx->pKeyInfo->db==db );
4304 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00004305 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh9467abf2016-02-17 18:44:11 +00004306 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00004307 break;
4308}
4309
dan78d58432014-03-25 15:04:07 +00004310/* Opcode: SequenceTest P1 P2 * * *
4311** Synopsis: if( cursor[P1].ctr++ ) pc = P2
4312**
4313** P1 is a sorter cursor. If the sequence counter is currently zero, jump
4314** to P2. Regardless of whether or not the jump is taken, increment the
4315** the sequence value.
4316*/
4317case OP_SequenceTest: {
4318 VdbeCursor *pC;
4319 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4320 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004321 assert( isSorter(pC) );
dan78d58432014-03-25 15:04:07 +00004322 if( (pC->seqCount++)==0 ){
drhf56fa462015-04-13 21:39:54 +00004323 goto jump_to_p2;
dan78d58432014-03-25 15:04:07 +00004324 }
drh5e00f6c2001-09-13 13:46:56 +00004325 break;
4326}
4327
drh5f612292014-02-08 23:20:32 +00004328/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00004329** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00004330**
4331** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00004332** row of data. The content of that one row is the content of memory
4333** register P2. In other words, cursor P1 becomes an alias for the
4334** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00004335**
drh2d8d7ce2010-02-15 15:17:05 +00004336** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00004337** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00004338** individual columns using the OP_Column opcode. The OP_Column opcode
4339** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00004340**
4341** P3 is the number of fields in the records that will be stored by
4342** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004343*/
drh9cbf3422008-01-17 16:22:13 +00004344case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00004345 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00004346
drh653b82a2009-06-22 11:10:47 +00004347 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00004348 assert( pOp->p3>=0 );
drhb2486682022-01-03 01:43:28 +00004349 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO);
drh4774b132004-06-12 20:12:51 +00004350 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00004351 pCx->nullRow = 1;
drhfe0cf7a2017-08-16 19:20:20 +00004352 pCx->seekResult = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00004353 pCx->isTable = 1;
drhfe0cf7a2017-08-16 19:20:20 +00004354 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
4355 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
4356 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
4357 ** which is a performance optimization */
4358 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
drh5f612292014-02-08 23:20:32 +00004359 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00004360 break;
4361}
4362
drh98757152008-01-09 23:04:12 +00004363/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00004364**
4365** Close a cursor previously opened as P1. If P1 is not
4366** currently open, this instruction is a no-op.
4367*/
drh9cbf3422008-01-17 16:22:13 +00004368case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00004369 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4370 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
4371 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00004372 break;
4373}
4374
drh97bae792015-06-05 15:59:57 +00004375#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
4376/* Opcode: ColumnsUsed P1 * * P4 *
4377**
4378** This opcode (which only exists if SQLite was compiled with
4379** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
4380** table or index for cursor P1 are used. P4 is a 64-bit integer
4381** (P4_INT64) in which the first 63 bits are one for each of the
4382** first 63 columns of the table or index that are actually used
4383** by the cursor. The high-order bit is set if any column after
4384** the 64th is used.
4385*/
4386case OP_ColumnsUsed: {
4387 VdbeCursor *pC;
4388 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004389 assert( pC->eCurType==CURTYPE_BTREE );
drh97bae792015-06-05 15:59:57 +00004390 pC->maskUsed = *(u64*)pOp->p4.pI64;
4391 break;
4392}
4393#endif
4394
drh8af3f772014-07-25 18:01:06 +00004395/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004396** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004397**
danielk1977b790c6c2008-04-18 10:25:24 +00004398** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004399** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004400** to an SQL index, then P3 is the first in an array of P4 registers
4401** that are used as an unpacked index key.
4402**
4403** Reposition cursor P1 so that it points to the smallest entry that
4404** is greater than or equal to the key value. If there are no records
4405** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00004406**
drhb1d607d2015-11-05 22:30:54 +00004407** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
drh576d0a92020-03-12 17:28:27 +00004408** opcode will either land on a record that exactly matches the key, or
4409** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4410** this opcode must be followed by an IdxLE opcode with the same arguments.
4411** The IdxGT opcode will be skipped if this opcode succeeds, but the
4412** IdxGT opcode will be used on subsequent loop iterations. The
4413** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4414** is an equality search.
drhb1d607d2015-11-05 22:30:54 +00004415**
drh8af3f772014-07-25 18:01:06 +00004416** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00004417** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004418** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00004419**
drh935850e2014-05-24 17:15:15 +00004420** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00004421*/
drh8af3f772014-07-25 18:01:06 +00004422/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004423** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00004424**
danielk1977b790c6c2008-04-18 10:25:24 +00004425** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004426** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004427** to an SQL index, then P3 is the first in an array of P4 registers
4428** that are used as an unpacked index key.
4429**
drh576d0a92020-03-12 17:28:27 +00004430** Reposition cursor P1 so that it points to the smallest entry that
danielk1977b790c6c2008-04-18 10:25:24 +00004431** is greater than the key value. If there are no records greater than
4432** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00004433**
drh8af3f772014-07-25 18:01:06 +00004434** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00004435** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004436** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00004437**
drh935850e2014-05-24 17:15:15 +00004438** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00004439*/
drh8af3f772014-07-25 18:01:06 +00004440/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004441** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00004442**
danielk1977b790c6c2008-04-18 10:25:24 +00004443** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004444** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004445** to an SQL index, then P3 is the first in an array of P4 registers
4446** that are used as an unpacked index key.
4447**
4448** Reposition cursor P1 so that it points to the largest entry that
4449** is less than the key value. If there are no records less than
4450** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00004451**
drh8af3f772014-07-25 18:01:06 +00004452** This opcode leaves the cursor configured to move in reverse order,
4453** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004454** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00004455**
drh935850e2014-05-24 17:15:15 +00004456** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00004457*/
drh8af3f772014-07-25 18:01:06 +00004458/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004459** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00004460**
danielk1977b790c6c2008-04-18 10:25:24 +00004461** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004462** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004463** to an SQL index, then P3 is the first in an array of P4 registers
4464** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00004465**
danielk1977b790c6c2008-04-18 10:25:24 +00004466** Reposition cursor P1 so that it points to the largest entry that
4467** is less than or equal to the key value. If there are no records
4468** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00004469**
drh8af3f772014-07-25 18:01:06 +00004470** This opcode leaves the cursor configured to move in reverse order,
4471** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004472** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00004473**
drhb1d607d2015-11-05 22:30:54 +00004474** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
drh576d0a92020-03-12 17:28:27 +00004475** opcode will either land on a record that exactly matches the key, or
4476** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4477** this opcode must be followed by an IdxLE opcode with the same arguments.
drhb1d607d2015-11-05 22:30:54 +00004478** The IdxGE opcode will be skipped if this opcode succeeds, but the
drh576d0a92020-03-12 17:28:27 +00004479** IdxGE opcode will be used on subsequent loop iterations. The
4480** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4481** is an equality search.
drhb1d607d2015-11-05 22:30:54 +00004482**
drh935850e2014-05-24 17:15:15 +00004483** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00004484*/
mistachkin758784d2018-07-25 15:12:29 +00004485case OP_SeekLT: /* jump, in3, group */
4486case OP_SeekLE: /* jump, in3, group */
4487case OP_SeekGE: /* jump, in3, group */
4488case OP_SeekGT: { /* jump, in3, group */
drhb1d607d2015-11-05 22:30:54 +00004489 int res; /* Comparison result */
4490 int oc; /* Opcode */
4491 VdbeCursor *pC; /* The cursor to seek */
4492 UnpackedRecord r; /* The key to seek for */
4493 int nField; /* Number of columns or fields in the key */
4494 i64 iKey; /* The rowid we are to seek to */
drhd6b79462015-11-07 01:19:00 +00004495 int eqOnly; /* Only interested in == results */
drh80ff32f2001-11-04 18:32:46 +00004496
drh653b82a2009-06-22 11:10:47 +00004497 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00004498 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00004499 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004500 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004501 assert( pC->eCurType==CURTYPE_BTREE );
drh4a1d3652014-02-14 15:13:36 +00004502 assert( OP_SeekLE == OP_SeekLT+1 );
4503 assert( OP_SeekGE == OP_SeekLT+2 );
4504 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00004505 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00004506 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004507 oc = pOp->opcode;
drhd6b79462015-11-07 01:19:00 +00004508 eqOnly = 0;
drh3da046d2013-11-11 03:24:11 +00004509 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00004510#ifdef SQLITE_DEBUG
4511 pC->seekOp = pOp->opcode;
4512#endif
drhe0997b32015-03-20 14:57:50 +00004513
dana40cb962019-05-14 20:25:22 +00004514 pC->deferredMoveto = 0;
4515 pC->cacheStatus = CACHE_STALE;
drh3da046d2013-11-11 03:24:11 +00004516 if( pC->isTable ){
drh3e364802019-08-22 00:53:16 +00004517 u16 flags3, newType;
drh576d0a92020-03-12 17:28:27 +00004518 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
drh218c66e2016-12-27 12:35:36 +00004519 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
4520 || CORRUPT_DB );
drhd6b79462015-11-07 01:19:00 +00004521
drh3da046d2013-11-11 03:24:11 +00004522 /* The input value in P3 might be of any type: integer, real, string,
4523 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00004524 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00004525 pIn3 = &aMem[pOp->p3];
drh3e364802019-08-22 00:53:16 +00004526 flags3 = pIn3->flags;
4527 if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00004528 applyNumericAffinity(pIn3, 0);
4529 }
drh3e364802019-08-22 00:53:16 +00004530 iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */
4531 newType = pIn3->flags; /* Record the type after applying numeric affinity */
4532 pIn3->flags = flags3; /* But convert the type back to its original */
drh959403f2008-12-12 17:56:16 +00004533
drh3da046d2013-11-11 03:24:11 +00004534 /* If the P3 value could not be converted into an integer without
4535 ** loss of information, then special processing is required... */
drh3e364802019-08-22 00:53:16 +00004536 if( (newType & (MEM_Int|MEM_IntReal))==0 ){
drhde324612021-07-19 20:52:31 +00004537 int c;
drh3e364802019-08-22 00:53:16 +00004538 if( (newType & MEM_Real)==0 ){
4539 if( (newType & MEM_Null) || oc>=OP_SeekGE ){
drh8616cff2019-07-13 16:15:23 +00004540 VdbeBranchTaken(1,2);
4541 goto jump_to_p2;
dan9edd8c12019-05-08 11:42:49 +00004542 }else{
dan873b0192019-05-09 11:19:27 +00004543 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
4544 if( rc!=SQLITE_OK ) goto abort_due_to_error;
dan9edd8c12019-05-08 11:42:49 +00004545 goto seek_not_found;
4546 }
drhde324612021-07-19 20:52:31 +00004547 }
4548 c = sqlite3IntFloatCompare(iKey, pIn3->u.r);
drh959403f2008-12-12 17:56:16 +00004549
danaa1776f2013-11-26 18:22:59 +00004550 /* If the approximation iKey is larger than the actual real search
4551 ** term, substitute >= for > and < for <=. e.g. if the search term
4552 ** is 4.9 and the integer approximation 5:
4553 **
4554 ** (x > 4.9) -> (x >= 5)
4555 ** (x <= 4.9) -> (x < 5)
4556 */
drhde324612021-07-19 20:52:31 +00004557 if( c>0 ){
drh4a1d3652014-02-14 15:13:36 +00004558 assert( OP_SeekGE==(OP_SeekGT-1) );
4559 assert( OP_SeekLT==(OP_SeekLE-1) );
4560 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
4561 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00004562 }
4563
4564 /* If the approximation iKey is smaller than the actual real search
4565 ** term, substitute <= for < and > for >=. */
drhde324612021-07-19 20:52:31 +00004566 else if( c<0 ){
drh4a1d3652014-02-14 15:13:36 +00004567 assert( OP_SeekLE==(OP_SeekLT+1) );
4568 assert( OP_SeekGT==(OP_SeekGE+1) );
4569 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
4570 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00004571 }
dan9edd8c12019-05-08 11:42:49 +00004572 }
drh42a410d2021-06-19 18:32:20 +00004573 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00004574 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004575 if( rc!=SQLITE_OK ){
4576 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00004577 }
drhaa736092009-06-22 00:55:30 +00004578 }else{
drh576d0a92020-03-12 17:28:27 +00004579 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
4580 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
4581 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
4582 ** with the same key.
drhd6b79462015-11-07 01:19:00 +00004583 */
drhc960dcb2015-11-20 19:22:01 +00004584 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
drhd6b79462015-11-07 01:19:00 +00004585 eqOnly = 1;
4586 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
4587 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
drh576d0a92020-03-12 17:28:27 +00004588 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
4589 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
drhd6b79462015-11-07 01:19:00 +00004590 assert( pOp[1].p1==pOp[0].p1 );
4591 assert( pOp[1].p2==pOp[0].p2 );
4592 assert( pOp[1].p3==pOp[0].p3 );
4593 assert( pOp[1].p4.i==pOp[0].p4.i );
4594 }
4595
drh3da046d2013-11-11 03:24:11 +00004596 nField = pOp->p4.i;
4597 assert( pOp->p4type==P4_INT32 );
4598 assert( nField>0 );
4599 r.pKeyInfo = pC->pKeyInfo;
4600 r.nField = (u16)nField;
4601
4602 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00004603 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00004604 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00004605 ** }else{
dan1fed5da2014-02-25 21:01:25 +00004606 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00004607 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00004608 */
dan1fed5da2014-02-25 21:01:25 +00004609 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
4610 assert( oc!=OP_SeekGT || r.default_rc==-1 );
4611 assert( oc!=OP_SeekLE || r.default_rc==-1 );
4612 assert( oc!=OP_SeekGE || r.default_rc==+1 );
4613 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00004614
4615 r.aMem = &aMem[pOp->p3];
4616#ifdef SQLITE_DEBUG
4617 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4618#endif
drh70528d72015-11-05 20:25:09 +00004619 r.eqSeen = 0;
drh42a410d2021-06-19 18:32:20 +00004620 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
drh3da046d2013-11-11 03:24:11 +00004621 if( rc!=SQLITE_OK ){
4622 goto abort_due_to_error;
4623 }
drhb1d607d2015-11-05 22:30:54 +00004624 if( eqOnly && r.eqSeen==0 ){
4625 assert( res!=0 );
4626 goto seek_not_found;
drh70528d72015-11-05 20:25:09 +00004627 }
drh3da046d2013-11-11 03:24:11 +00004628 }
drh3da046d2013-11-11 03:24:11 +00004629#ifdef SQLITE_TEST
4630 sqlite3_search_count++;
4631#endif
drh4a1d3652014-02-14 15:13:36 +00004632 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
4633 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00004634 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004635 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
4636 if( rc!=SQLITE_OK ){
4637 if( rc==SQLITE_DONE ){
4638 rc = SQLITE_OK;
4639 res = 1;
4640 }else{
4641 goto abort_due_to_error;
4642 }
4643 }
drh3da046d2013-11-11 03:24:11 +00004644 }else{
4645 res = 0;
4646 }
4647 }else{
drh4a1d3652014-02-14 15:13:36 +00004648 assert( oc==OP_SeekLT || oc==OP_SeekLE );
4649 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00004650 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004651 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
4652 if( rc!=SQLITE_OK ){
4653 if( rc==SQLITE_DONE ){
4654 rc = SQLITE_OK;
4655 res = 1;
4656 }else{
4657 goto abort_due_to_error;
4658 }
4659 }
drh3da046d2013-11-11 03:24:11 +00004660 }else{
4661 /* res might be negative because the table is empty. Check to
4662 ** see if this is the case.
4663 */
drhc960dcb2015-11-20 19:22:01 +00004664 res = sqlite3BtreeEof(pC->uc.pCursor);
drh3da046d2013-11-11 03:24:11 +00004665 }
4666 }
drhb1d607d2015-11-05 22:30:54 +00004667seek_not_found:
drh3da046d2013-11-11 03:24:11 +00004668 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00004669 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004670 if( res ){
drhf56fa462015-04-13 21:39:54 +00004671 goto jump_to_p2;
drhb1d607d2015-11-05 22:30:54 +00004672 }else if( eqOnly ){
4673 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
4674 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
drh5e00f6c2001-09-13 13:46:56 +00004675 }
drh5e00f6c2001-09-13 13:46:56 +00004676 break;
4677}
dan71c57db2016-07-09 20:23:55 +00004678
drh68cf0ac2020-09-28 19:51:54 +00004679
drh04e70ce2020-10-02 11:55:07 +00004680/* Opcode: SeekScan P1 P2 * * *
drh68cf0ac2020-09-28 19:51:54 +00004681** Synopsis: Scan-ahead up to P1 rows
4682**
drhdfbaae72020-09-29 17:29:11 +00004683** This opcode is a prefix opcode to OP_SeekGE. In other words, this
drh04e70ce2020-10-02 11:55:07 +00004684** opcode must be immediately followed by OP_SeekGE. This constraint is
drhdfbaae72020-09-29 17:29:11 +00004685** checked by assert() statements.
4686**
4687** This opcode uses the P1 through P4 operands of the subsequent
4688** OP_SeekGE. In the text that follows, the operands of the subsequent
4689** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
drh04e70ce2020-10-02 11:55:07 +00004690** the P1 and P2 operands of this opcode are also used, and are called
4691** This.P1 and This.P2.
drh68cf0ac2020-09-28 19:51:54 +00004692**
4693** This opcode helps to optimize IN operators on a multi-column index
drhdfbaae72020-09-29 17:29:11 +00004694** where the IN operator is on the later terms of the index by avoiding
4695** unnecessary seeks on the btree, substituting steps to the next row
4696** of the b-tree instead. A correct answer is obtained if this opcode
4697** is omitted or is a no-op.
drh68cf0ac2020-09-28 19:51:54 +00004698**
drhdfbaae72020-09-29 17:29:11 +00004699** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
4700** is the desired entry that we want the cursor SeekGE.P1 to be pointing
4701** to. Call this SeekGE.P4/P5 row the "target".
drh68cf0ac2020-09-28 19:51:54 +00004702**
drha54e1b12020-09-29 23:52:25 +00004703** If the SeekGE.P1 cursor is not currently pointing to a valid row,
4704** then this opcode is a no-op and control passes through into the OP_SeekGE.
drh68cf0ac2020-09-28 19:51:54 +00004705**
drhdfbaae72020-09-29 17:29:11 +00004706** If the SeekGE.P1 cursor is pointing to a valid row, then that row
4707** might be the target row, or it might be near and slightly before the
4708** target row. This opcode attempts to position the cursor on the target
drh04e70ce2020-10-02 11:55:07 +00004709** row by, perhaps by invoking sqlite3BtreeStep() on the cursor
drhdfbaae72020-09-29 17:29:11 +00004710** between 0 and This.P1 times.
drh68cf0ac2020-09-28 19:51:54 +00004711**
drhdfbaae72020-09-29 17:29:11 +00004712** There are three possible outcomes from this opcode:<ol>
drh68cf0ac2020-09-28 19:51:54 +00004713**
drh3c48ee92021-03-20 01:00:26 +00004714** <li> If after This.P1 steps, the cursor is still pointing to a place that
4715** is earlier in the btree than the target row, then fall through
4716** into the subsquence OP_SeekGE opcode.
drh68cf0ac2020-09-28 19:51:54 +00004717**
drhdfbaae72020-09-29 17:29:11 +00004718** <li> If the cursor is successfully moved to the target row by 0 or more
drh04e70ce2020-10-02 11:55:07 +00004719** sqlite3BtreeNext() calls, then jump to This.P2, which will land just
drh3c48ee92021-03-20 01:00:26 +00004720** past the OP_IdxGT or OP_IdxGE opcode that follows the OP_SeekGE.
drhdfbaae72020-09-29 17:29:11 +00004721**
4722** <li> If the cursor ends up past the target row (indicating the the target
4723** row does not exist in the btree) then jump to SeekOP.P2.
4724** </ol>
drh68cf0ac2020-09-28 19:51:54 +00004725*/
4726case OP_SeekScan: {
drhf761d932020-09-29 01:48:46 +00004727 VdbeCursor *pC;
4728 int res;
drhdeaa6102020-10-01 15:46:21 +00004729 int nStep;
drhf761d932020-09-29 01:48:46 +00004730 UnpackedRecord r;
4731
drh68cf0ac2020-09-28 19:51:54 +00004732 assert( pOp[1].opcode==OP_SeekGE );
drh04e70ce2020-10-02 11:55:07 +00004733
4734 /* pOp->p2 points to the first instruction past the OP_IdxGT that
4735 ** follows the OP_SeekGE. */
4736 assert( pOp->p2>=(int)(pOp-aOp)+2 );
drh0d084022021-06-13 19:14:14 +00004737 assert( aOp[pOp->p2-1].opcode==OP_IdxGT || aOp[pOp->p2-1].opcode==OP_IdxGE );
4738 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
drh04e70ce2020-10-02 11:55:07 +00004739 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
4740 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
4741 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
drh04e70ce2020-10-02 11:55:07 +00004742
drh68cf0ac2020-09-28 19:51:54 +00004743 assert( pOp->p1>0 );
drhf761d932020-09-29 01:48:46 +00004744 pC = p->apCsr[pOp[1].p1];
4745 assert( pC!=0 );
4746 assert( pC->eCurType==CURTYPE_BTREE );
4747 assert( !pC->isTable );
drha54e1b12020-09-29 23:52:25 +00004748 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
drhf761d932020-09-29 01:48:46 +00004749#ifdef SQLITE_DEBUG
4750 if( db->flags&SQLITE_VdbeTrace ){
drha54e1b12020-09-29 23:52:25 +00004751 printf("... cursor not valid - fall through\n");
drhf761d932020-09-29 01:48:46 +00004752 }
4753#endif
4754 break;
4755 }
drhdeaa6102020-10-01 15:46:21 +00004756 nStep = pOp->p1;
4757 assert( nStep>=1 );
drhf761d932020-09-29 01:48:46 +00004758 r.pKeyInfo = pC->pKeyInfo;
4759 r.nField = (u16)pOp[1].p4.i;
4760 r.default_rc = 0;
4761 r.aMem = &aMem[pOp[1].p3];
4762#ifdef SQLITE_DEBUG
4763 {
4764 int i;
4765 for(i=0; i<r.nField; i++){
4766 assert( memIsValid(&r.aMem[i]) );
4767 REGISTER_TRACE(pOp[1].p3+i, &aMem[pOp[1].p3+i]);
4768 }
4769 }
4770#endif
4771 res = 0; /* Not needed. Only used to silence a warning. */
4772 while(1){
4773 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
4774 if( rc ) goto abort_due_to_error;
4775 if( res>0 ){
drh0b2949c2020-09-29 20:22:19 +00004776 seekscan_search_fail:
drhf761d932020-09-29 01:48:46 +00004777#ifdef SQLITE_DEBUG
drh0b2949c2020-09-29 20:22:19 +00004778 if( db->flags&SQLITE_VdbeTrace ){
drhdeaa6102020-10-01 15:46:21 +00004779 printf("... %d steps and then skip\n", pOp->p1 - nStep);
drh0b2949c2020-09-29 20:22:19 +00004780 }
drhf761d932020-09-29 01:48:46 +00004781#endif
drh0b2949c2020-09-29 20:22:19 +00004782 VdbeBranchTaken(1,3);
drhf287d002020-09-30 00:10:22 +00004783 pOp++;
drhf761d932020-09-29 01:48:46 +00004784 goto jump_to_p2;
4785 }
4786 if( res==0 ){
4787#ifdef SQLITE_DEBUG
drh0b2949c2020-09-29 20:22:19 +00004788 if( db->flags&SQLITE_VdbeTrace ){
drhdeaa6102020-10-01 15:46:21 +00004789 printf("... %d steps and then success\n", pOp->p1 - nStep);
drh0b2949c2020-09-29 20:22:19 +00004790 }
drhf761d932020-09-29 01:48:46 +00004791#endif
drh0b2949c2020-09-29 20:22:19 +00004792 VdbeBranchTaken(2,3);
drh04e70ce2020-10-02 11:55:07 +00004793 goto jump_to_p2;
drhf761d932020-09-29 01:48:46 +00004794 break;
4795 }
drhdeaa6102020-10-01 15:46:21 +00004796 if( nStep<=0 ){
drh0b2949c2020-09-29 20:22:19 +00004797#ifdef SQLITE_DEBUG
4798 if( db->flags&SQLITE_VdbeTrace ){
4799 printf("... fall through after %d steps\n", pOp->p1);
4800 }
4801#endif
4802 VdbeBranchTaken(0,3);
4803 break;
4804 }
drhdeaa6102020-10-01 15:46:21 +00004805 nStep--;
drhf761d932020-09-29 01:48:46 +00004806 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
drh0b2949c2020-09-29 20:22:19 +00004807 if( rc ){
4808 if( rc==SQLITE_DONE ){
4809 rc = SQLITE_OK;
4810 goto seekscan_search_fail;
4811 }else{
4812 goto abort_due_to_error;
4813 }
4814 }
drhf761d932020-09-29 01:48:46 +00004815 }
drh0b2949c2020-09-29 20:22:19 +00004816
drhf761d932020-09-29 01:48:46 +00004817 break;
drh68cf0ac2020-09-28 19:51:54 +00004818}
4819
4820
drhfa17e132020-09-01 01:52:03 +00004821/* Opcode: SeekHit P1 P2 P3 * *
4822** Synopsis: set P2<=seekHit<=P3
drh8c2b6d72018-06-05 20:45:20 +00004823**
drhfa17e132020-09-01 01:52:03 +00004824** Increase or decrease the seekHit value for cursor P1, if necessary,
4825** so that it is no less than P2 and no greater than P3.
drh8c2b6d72018-06-05 20:45:20 +00004826**
drhfa17e132020-09-01 01:52:03 +00004827** The seekHit integer represents the maximum of terms in an index for which
4828** there is known to be at least one match. If the seekHit value is smaller
4829** than the total number of equality terms in an index lookup, then the
4830** OP_IfNoHope opcode might run to see if the IN loop can be abandoned
4831** early, thus saving work. This is part of the IN-early-out optimization.
4832**
4833** P1 must be a valid b-tree cursor.
drh8c2b6d72018-06-05 20:45:20 +00004834*/
4835case OP_SeekHit: {
4836 VdbeCursor *pC;
4837 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4838 pC = p->apCsr[pOp->p1];
4839 assert( pC!=0 );
drhfa17e132020-09-01 01:52:03 +00004840 assert( pOp->p3>=pOp->p2 );
4841 if( pC->seekHit<pOp->p2 ){
drh7bfccfe2021-04-29 13:58:28 +00004842#ifdef SQLITE_DEBUG
4843 if( db->flags&SQLITE_VdbeTrace ){
4844 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
4845 }
4846#endif
drhfa17e132020-09-01 01:52:03 +00004847 pC->seekHit = pOp->p2;
4848 }else if( pC->seekHit>pOp->p3 ){
drh7bfccfe2021-04-29 13:58:28 +00004849#ifdef SQLITE_DEBUG
4850 if( db->flags&SQLITE_VdbeTrace ){
4851 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
4852 }
4853#endif
drhfa17e132020-09-01 01:52:03 +00004854 pC->seekHit = pOp->p3;
4855 }
drh8c2b6d72018-06-05 20:45:20 +00004856 break;
4857}
4858
dan74ebaad2020-01-04 16:55:57 +00004859/* Opcode: IfNotOpen P1 P2 * * *
4860** Synopsis: if( !csr[P1] ) goto P2
4861**
4862** If cursor P1 is not open, jump to instruction P2. Otherwise, fall through.
4863*/
4864case OP_IfNotOpen: { /* jump */
4865 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh56ea69b2020-01-04 18:33:20 +00004866 VdbeBranchTaken(p->apCsr[pOp->p1]==0, 2);
dan74ebaad2020-01-04 16:55:57 +00004867 if( !p->apCsr[pOp->p1] ){
4868 goto jump_to_p2_and_check_for_interrupt;
4869 }
4870 break;
4871}
4872
drh8cff69d2009-11-12 19:59:44 +00004873/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004874** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004875**
drh8cff69d2009-11-12 19:59:44 +00004876** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4877** P4>0 then register P3 is the first of P4 registers that form an unpacked
4878** record.
4879**
4880** Cursor P1 is on an index btree. If the record identified by P3 and P4
4881** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00004882** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00004883**
drhcefc87f2014-08-01 01:40:33 +00004884** This operation leaves the cursor in a state where it can be
4885** advanced in the forward direction. The Next instruction will work,
4886** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00004887**
drh6f225d02013-10-26 13:36:51 +00004888** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00004889*/
drh8cff69d2009-11-12 19:59:44 +00004890/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004891** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004892**
drh8cff69d2009-11-12 19:59:44 +00004893** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4894** P4>0 then register P3 is the first of P4 registers that form an unpacked
4895** record.
4896**
4897** Cursor P1 is on an index btree. If the record identified by P3 and P4
4898** is not the prefix of any entry in P1 then a jump is made to P2. If P1
4899** does contain an entry whose prefix matches the P3/P4 record then control
4900** falls through to the next instruction and P1 is left pointing at the
4901** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00004902**
drh8af3f772014-07-25 18:01:06 +00004903** This operation leaves the cursor in a state where it cannot be
4904** advanced in either direction. In other words, the Next and Prev
4905** opcodes do not work after this operation.
4906**
drh8c2b6d72018-06-05 20:45:20 +00004907** See also: Found, NotExists, NoConflict, IfNoHope
4908*/
4909/* Opcode: IfNoHope P1 P2 P3 P4 *
4910** Synopsis: key=r[P3@P4]
4911**
4912** Register P3 is the first of P4 registers that form an unpacked
drhfa17e132020-09-01 01:52:03 +00004913** record. Cursor P1 is an index btree. P2 is a jump destination.
4914** In other words, the operands to this opcode are the same as the
4915** operands to OP_NotFound and OP_IdxGT.
drh8c2b6d72018-06-05 20:45:20 +00004916**
drhfa17e132020-09-01 01:52:03 +00004917** This opcode is an optimization attempt only. If this opcode always
4918** falls through, the correct answer is still obtained, but extra works
4919** is performed.
drh8c2b6d72018-06-05 20:45:20 +00004920**
drhfa17e132020-09-01 01:52:03 +00004921** A value of N in the seekHit flag of cursor P1 means that there exists
4922** a key P3:N that will match some record in the index. We want to know
4923** if it is possible for a record P3:P4 to match some record in the
4924** index. If it is not possible, we can skips some work. So if seekHit
4925** is less than P4, attempt to find out if a match is possible by running
4926** OP_NotFound.
drh8c2b6d72018-06-05 20:45:20 +00004927**
4928** This opcode is used in IN clause processing for a multi-column key.
4929** If an IN clause is attached to an element of the key other than the
4930** left-most element, and if there are no matches on the most recent
4931** seek over the whole key, then it might be that one of the key element
4932** to the left is prohibiting a match, and hence there is "no hope" of
4933** any match regardless of how many IN clause elements are checked.
4934** In such a case, we abandon the IN clause search early, using this
4935** opcode. The opcode name comes from the fact that the
4936** jump is taken if there is "no hope" of achieving a match.
4937**
4938** See also: NotFound, SeekHit
drh5e00f6c2001-09-13 13:46:56 +00004939*/
drh6f225d02013-10-26 13:36:51 +00004940/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00004941** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00004942**
4943** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
4944** P4>0 then register P3 is the first of P4 registers that form an unpacked
4945** record.
4946**
4947** Cursor P1 is on an index btree. If the record identified by P3 and P4
4948** contains any NULL value, jump immediately to P2. If all terms of the
4949** record are not-NULL then a check is done to determine if any row in the
4950** P1 index btree has a matching key prefix. If there are no matches, jump
4951** immediately to P2. If there is a match, fall through and leave the P1
4952** cursor pointing to the matching row.
4953**
4954** This opcode is similar to OP_NotFound with the exceptions that the
4955** branch is always taken if any part of the search key input is NULL.
4956**
drh8af3f772014-07-25 18:01:06 +00004957** This operation leaves the cursor in a state where it cannot be
4958** advanced in either direction. In other words, the Next and Prev
4959** opcodes do not work after this operation.
4960**
drh6f225d02013-10-26 13:36:51 +00004961** See also: NotFound, Found, NotExists
4962*/
drh8c2b6d72018-06-05 20:45:20 +00004963case OP_IfNoHope: { /* jump, in3 */
4964 VdbeCursor *pC;
4965 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4966 pC = p->apCsr[pOp->p1];
4967 assert( pC!=0 );
drh7bfccfe2021-04-29 13:58:28 +00004968#ifdef SQLITE_DEBUG
4969 if( db->flags&SQLITE_VdbeTrace ){
4970 printf("seekHit is %d\n", pC->seekHit);
4971 }
4972#endif
drhfa17e132020-09-01 01:52:03 +00004973 if( pC->seekHit>=pOp->p4.i ) break;
drh8c2b6d72018-06-05 20:45:20 +00004974 /* Fall through into OP_NotFound */
drh08b92082020-08-10 14:18:00 +00004975 /* no break */ deliberate_fall_through
drh8c2b6d72018-06-05 20:45:20 +00004976}
drh6f225d02013-10-26 13:36:51 +00004977case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00004978case OP_NotFound: /* jump, in3 */
4979case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00004980 int alreadyExists;
drh6f225d02013-10-26 13:36:51 +00004981 int ii;
drhdfe88ec2008-11-03 20:55:06 +00004982 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004983 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00004984 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004985
dan0ff297e2009-09-25 17:03:14 +00004986#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00004987 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00004988#endif
4989
drhaa736092009-06-22 00:55:30 +00004990 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00004991 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00004992 pC = p->apCsr[pOp->p1];
4993 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004994#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00004995 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00004996#endif
drh95b10362022-04-13 19:00:57 +00004997 r.aMem = &aMem[pOp->p3];
drhc960dcb2015-11-20 19:22:01 +00004998 assert( pC->eCurType==CURTYPE_BTREE );
4999 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00005000 assert( pC->isTable==0 );
drh95b10362022-04-13 19:00:57 +00005001 r.nField = (u16)pOp->p4.i;
5002 if( r.nField>0 ){
drhb834e0d2022-04-04 19:43:57 +00005003 /* Key values in an array of registers */
drhb834e0d2022-04-04 19:43:57 +00005004 r.pKeyInfo = pC->pKeyInfo;
drhb834e0d2022-04-04 19:43:57 +00005005 r.default_rc = 0;
drh8aaf7bc2016-09-20 01:19:18 +00005006#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00005007 for(ii=0; ii<r.nField; ii++){
5008 assert( memIsValid(&r.aMem[ii]) );
drh8aaf7bc2016-09-20 01:19:18 +00005009 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
drh826af372014-02-08 19:12:21 +00005010 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh826af372014-02-08 19:12:21 +00005011 }
drh8aaf7bc2016-09-20 01:19:18 +00005012#endif
drhec534e62022-04-04 20:20:22 +00005013 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult);
drh3da046d2013-11-11 03:24:11 +00005014 }else{
drhb834e0d2022-04-04 19:43:57 +00005015 /* Composite key generated by OP_MakeRecord */
drh95b10362022-04-13 19:00:57 +00005016 assert( r.aMem->flags & MEM_Blob );
drhb834e0d2022-04-04 19:43:57 +00005017 assert( pOp->opcode!=OP_NoConflict );
drh95b10362022-04-13 19:00:57 +00005018 rc = ExpandBlob(r.aMem);
drhe46515b2017-05-19 22:51:00 +00005019 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
5020 if( rc ) goto no_mem;
drhb834e0d2022-04-04 19:43:57 +00005021 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
drh3da046d2013-11-11 03:24:11 +00005022 if( pIdxKey==0 ) goto no_mem;
drh95b10362022-04-13 19:00:57 +00005023 sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey);
drhb834e0d2022-04-04 19:43:57 +00005024 pIdxKey->default_rc = 0;
drhec534e62022-04-04 20:20:22 +00005025 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult);
drhb834e0d2022-04-04 19:43:57 +00005026 sqlite3DbFreeNN(db, pIdxKey);
drh5e00f6c2001-09-13 13:46:56 +00005027 }
drh3da046d2013-11-11 03:24:11 +00005028 if( rc!=SQLITE_OK ){
drh9467abf2016-02-17 18:44:11 +00005029 goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00005030 }
drhec534e62022-04-04 20:20:22 +00005031 alreadyExists = (pC->seekResult==0);
drh3da046d2013-11-11 03:24:11 +00005032 pC->nullRow = 1-alreadyExists;
5033 pC->deferredMoveto = 0;
5034 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00005035 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00005036 VdbeBranchTaken(alreadyExists!=0,2);
drhf56fa462015-04-13 21:39:54 +00005037 if( alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00005038 }else{
drhb834e0d2022-04-04 19:43:57 +00005039 if( !alreadyExists ){
5040 VdbeBranchTaken(1,2);
5041 goto jump_to_p2;
5042 }
5043 if( pOp->opcode==OP_NoConflict ){
5044 /* For the OP_NoConflict opcode, take the jump if any of the
5045 ** input fields are NULL, since any key with a NULL will not
5046 ** conflict */
5047 for(ii=0; ii<r.nField; ii++){
5048 if( r.aMem[ii].flags & MEM_Null ){
5049 VdbeBranchTaken(1,2);
5050 goto jump_to_p2;
5051 }
5052 }
5053 }
5054 VdbeBranchTaken(0,2);
5055 if( pOp->opcode==OP_IfNoHope ){
5056 pC->seekHit = pOp->p4.i;
5057 }
drh5e00f6c2001-09-13 13:46:56 +00005058 }
drh5e00f6c2001-09-13 13:46:56 +00005059 break;
5060}
5061
drheeb95652016-05-26 20:56:38 +00005062/* Opcode: SeekRowid P1 P2 P3 * *
5063** Synopsis: intkey=r[P3]
5064**
5065** P1 is the index of a cursor open on an SQL table btree (with integer
5066** keys). If register P3 does not contain an integer or if P1 does not
5067** contain a record with rowid P3 then jump immediately to P2.
5068** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
5069** a record with rowid P3 then
5070** leave the cursor pointing at that record and fall through to the next
5071** instruction.
5072**
5073** The OP_NotExists opcode performs the same operation, but with OP_NotExists
5074** the P3 register must be guaranteed to contain an integer value. With this
5075** opcode, register P3 might not contain an integer.
5076**
5077** The OP_NotFound opcode performs the same operation on index btrees
5078** (with arbitrary multi-value keys).
5079**
5080** This opcode leaves the cursor in a state where it cannot be advanced
5081** in either direction. In other words, the Next and Prev opcodes will
5082** not work following this opcode.
5083**
5084** See also: Found, NotFound, NoConflict, SeekRowid
5085*/
drh9cbf3422008-01-17 16:22:13 +00005086/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005087** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00005088**
drh261c02d2013-10-25 14:46:15 +00005089** P1 is the index of a cursor open on an SQL table btree (with integer
5090** keys). P3 is an integer rowid. If P1 does not contain a record with
danc6157e12015-09-14 09:23:47 +00005091** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
5092** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
5093** leave the cursor pointing at that record and fall through to the next
5094** instruction.
drh6b125452002-01-28 15:53:03 +00005095**
drheeb95652016-05-26 20:56:38 +00005096** The OP_SeekRowid opcode performs the same operation but also allows the
5097** P3 register to contain a non-integer value, in which case the jump is
5098** always taken. This opcode requires that P3 always contain an integer.
5099**
drh261c02d2013-10-25 14:46:15 +00005100** The OP_NotFound opcode performs the same operation on index btrees
5101** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00005102**
drh8af3f772014-07-25 18:01:06 +00005103** This opcode leaves the cursor in a state where it cannot be advanced
5104** in either direction. In other words, the Next and Prev opcodes will
5105** not work following this opcode.
5106**
drheeb95652016-05-26 20:56:38 +00005107** See also: Found, NotFound, NoConflict, SeekRowid
drh6b125452002-01-28 15:53:03 +00005108*/
drheeb95652016-05-26 20:56:38 +00005109case OP_SeekRowid: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00005110 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00005111 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00005112 int res;
5113 u64 iKey;
5114
drh3c657212009-11-17 23:59:58 +00005115 pIn3 = &aMem[pOp->p3];
drh3242c692019-05-04 01:29:13 +00005116 testcase( pIn3->flags & MEM_Int );
5117 testcase( pIn3->flags & MEM_IntReal );
drhb29ef5e2019-10-07 01:05:57 +00005118 testcase( pIn3->flags & MEM_Real );
5119 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
drh169f0772019-05-02 21:36:26 +00005120 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
drhb29ef5e2019-10-07 01:05:57 +00005121 /* If pIn3->u.i does not contain an integer, compute iKey as the
5122 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted
5123 ** into an integer without loss of information. Take care to avoid
5124 ** changing the datatype of pIn3, however, as it is used by other
5125 ** parts of the prepared statement. */
5126 Mem x = pIn3[0];
5127 applyAffinity(&x, SQLITE_AFF_NUMERIC, encoding);
5128 if( (x.flags & MEM_Int)==0 ) goto jump_to_p2;
5129 iKey = x.u.i;
5130 goto notExistsWithKey;
drheeb95652016-05-26 20:56:38 +00005131 }
5132 /* Fall through into OP_NotExists */
drh08b92082020-08-10 14:18:00 +00005133 /* no break */ deliberate_fall_through
drheeb95652016-05-26 20:56:38 +00005134case OP_NotExists: /* jump, in3 */
5135 pIn3 = &aMem[pOp->p3];
drhe4fe6d42018-08-03 15:58:07 +00005136 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
drhaa736092009-06-22 00:55:30 +00005137 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhb29ef5e2019-10-07 01:05:57 +00005138 iKey = pIn3->u.i;
5139notExistsWithKey:
drhaa736092009-06-22 00:55:30 +00005140 pC = p->apCsr[pOp->p1];
5141 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00005142#ifdef SQLITE_DEBUG
drh94f4f872018-12-20 22:08:32 +00005143 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
drh8af3f772014-07-25 18:01:06 +00005144#endif
drhaa736092009-06-22 00:55:30 +00005145 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00005146 assert( pC->eCurType==CURTYPE_BTREE );
5147 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00005148 assert( pCrsr!=0 );
5149 res = 0;
drh42a410d2021-06-19 18:32:20 +00005150 rc = sqlite3BtreeTableMoveto(pCrsr, iKey, 0, &res);
drhb79d5522015-09-14 19:26:37 +00005151 assert( rc==SQLITE_OK || res==0 );
drhb53a5a92014-10-12 22:37:22 +00005152 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00005153 pC->nullRow = 0;
5154 pC->cacheStatus = CACHE_STALE;
5155 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00005156 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00005157 pC->seekResult = res;
danc6157e12015-09-14 09:23:47 +00005158 if( res!=0 ){
drhb79d5522015-09-14 19:26:37 +00005159 assert( rc==SQLITE_OK );
5160 if( pOp->p2==0 ){
5161 rc = SQLITE_CORRUPT_BKPT;
5162 }else{
5163 goto jump_to_p2;
5164 }
danc6157e12015-09-14 09:23:47 +00005165 }
drh9467abf2016-02-17 18:44:11 +00005166 if( rc ) goto abort_due_to_error;
drh6b125452002-01-28 15:53:03 +00005167 break;
5168}
5169
drh4c583122008-01-04 22:01:03 +00005170/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00005171** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00005172**
drh4c583122008-01-04 22:01:03 +00005173** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00005174** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00005175** The sequence number on the cursor is incremented after this
5176** instruction.
drh4db38a72005-09-01 12:16:28 +00005177*/
drh27a348c2015-04-13 19:14:06 +00005178case OP_Sequence: { /* out2 */
drh653b82a2009-06-22 11:10:47 +00005179 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5180 assert( p->apCsr[pOp->p1]!=0 );
drhc960dcb2015-11-20 19:22:01 +00005181 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
drh27a348c2015-04-13 19:14:06 +00005182 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00005183 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00005184 break;
5185}
5186
5187
drh98757152008-01-09 23:04:12 +00005188/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005189** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00005190**
drhf0863fe2005-06-12 21:35:51 +00005191** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00005192** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00005193** table that cursor P1 points to. The new record number is written
5194** written to register P2.
drh205f48e2004-11-05 00:43:11 +00005195**
dan76d462e2009-08-30 11:42:51 +00005196** If P3>0 then P3 is a register in the root frame of this VDBE that holds
5197** the largest previously generated record number. No new record numbers are
5198** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00005199** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00005200** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00005201** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00005202*/
drh27a348c2015-04-13 19:14:06 +00005203case OP_NewRowid: { /* out2 */
drhaa736092009-06-22 00:55:30 +00005204 i64 v; /* The new rowid */
5205 VdbeCursor *pC; /* Cursor of table to get the new rowid */
5206 int res; /* Result of an sqlite3BtreeLast() */
5207 int cnt; /* Counter to limit the number of searches */
mistachkind6665c52021-01-18 19:28:56 +00005208#ifndef SQLITE_OMIT_AUTOINCREMENT
drhaa736092009-06-22 00:55:30 +00005209 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00005210 VdbeFrame *pFrame; /* Root frame of VDBE */
mistachkind6665c52021-01-18 19:28:56 +00005211#endif
drh856c1032009-06-02 15:21:42 +00005212
drh856c1032009-06-02 15:21:42 +00005213 v = 0;
5214 res = 0;
drh27a348c2015-04-13 19:14:06 +00005215 pOut = out2Prerelease(p, pOp);
drhaa736092009-06-22 00:55:30 +00005216 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5217 pC = p->apCsr[pOp->p1];
5218 assert( pC!=0 );
drh4c57e322018-05-23 17:53:07 +00005219 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00005220 assert( pC->eCurType==CURTYPE_BTREE );
5221 assert( pC->uc.pCursor!=0 );
drh98ef0f62015-06-30 01:25:52 +00005222 {
drh5cf8e8c2002-02-19 22:42:05 +00005223 /* The next rowid or record number (different terms for the same
5224 ** thing) is obtained in a two-step algorithm.
5225 **
5226 ** First we attempt to find the largest existing rowid and add one
5227 ** to that. But if the largest existing rowid is already the maximum
5228 ** positive integer, we have to fall through to the second
5229 ** probabilistic algorithm
5230 **
5231 ** The second algorithm is to select a rowid at random and see if
5232 ** it already exists in the table. If it does not exist, we have
5233 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00005234 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00005235 */
drhaa736092009-06-22 00:55:30 +00005236 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00005237
drh75f86a42005-02-17 00:03:06 +00005238#ifdef SQLITE_32BIT_ROWID
5239# define MAX_ROWID 0x7fffffff
5240#else
drhfe2093d2005-01-20 22:48:47 +00005241 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
5242 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
5243 ** to provide the constant while making all compilers happy.
5244 */
danielk197764202cf2008-11-17 15:31:47 +00005245# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00005246#endif
drhfe2093d2005-01-20 22:48:47 +00005247
drh5cf8e8c2002-02-19 22:42:05 +00005248 if( !pC->useRandomRowid ){
drhc960dcb2015-11-20 19:22:01 +00005249 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
drhe0670b62014-02-12 21:31:12 +00005250 if( rc!=SQLITE_OK ){
5251 goto abort_due_to_error;
5252 }
5253 if( res ){
5254 v = 1; /* IMP: R-61914-48074 */
5255 }else{
drhc960dcb2015-11-20 19:22:01 +00005256 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
drha7c90c42016-06-04 20:37:10 +00005257 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drhe0670b62014-02-12 21:31:12 +00005258 if( v>=MAX_ROWID ){
5259 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00005260 }else{
drhe0670b62014-02-12 21:31:12 +00005261 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00005262 }
drh3fc190c2001-09-14 03:24:23 +00005263 }
drhe0670b62014-02-12 21:31:12 +00005264 }
drh205f48e2004-11-05 00:43:11 +00005265
5266#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00005267 if( pOp->p3 ){
5268 /* Assert that P3 is a valid memory cell. */
5269 assert( pOp->p3>0 );
5270 if( p->pFrame ){
5271 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00005272 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00005273 assert( pOp->p3<=pFrame->nMem );
5274 pMem = &pFrame->aMem[pOp->p3];
5275 }else{
5276 /* Assert that P3 is a valid memory cell. */
drh9f6168b2016-03-19 23:32:58 +00005277 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhe0670b62014-02-12 21:31:12 +00005278 pMem = &aMem[pOp->p3];
5279 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00005280 }
drhe0670b62014-02-12 21:31:12 +00005281 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00005282
drhe0670b62014-02-12 21:31:12 +00005283 REGISTER_TRACE(pOp->p3, pMem);
5284 sqlite3VdbeMemIntegerify(pMem);
5285 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
5286 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhe77caa12016-11-02 13:18:46 +00005287 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
drhe0670b62014-02-12 21:31:12 +00005288 goto abort_due_to_error;
5289 }
5290 if( v<pMem->u.i+1 ){
5291 v = pMem->u.i + 1;
5292 }
5293 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00005294 }
drhe0670b62014-02-12 21:31:12 +00005295#endif
drh5cf8e8c2002-02-19 22:42:05 +00005296 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00005297 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00005298 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00005299 ** engine starts picking positive candidate ROWIDs at random until
5300 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00005301 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
5302 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00005303 cnt = 0;
drh2c4dc632014-09-25 12:31:28 +00005304 do{
5305 sqlite3_randomness(sizeof(v), &v);
drhd8633462014-09-25 17:42:41 +00005306 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
drh42a410d2021-06-19 18:32:20 +00005307 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v,
drh748a52c2010-09-01 11:50:08 +00005308 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00005309 && (res==0)
drh2c4dc632014-09-25 12:31:28 +00005310 && (++cnt<100));
drh9467abf2016-02-17 18:44:11 +00005311 if( rc ) goto abort_due_to_error;
5312 if( res==0 ){
drhc79c7612010-01-01 18:57:48 +00005313 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00005314 goto abort_due_to_error;
5315 }
drh748a52c2010-09-01 11:50:08 +00005316 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00005317 }
drha11846b2004-01-07 18:52:56 +00005318 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00005319 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00005320 }
drh4c583122008-01-04 22:01:03 +00005321 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00005322 break;
5323}
5324
danielk19771f4aa332008-01-03 09:51:55 +00005325/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00005326** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005327**
jplyon5a564222003-06-02 06:15:58 +00005328** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00005329** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00005330** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00005331** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00005332** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00005333**
danielk19771f4aa332008-01-03 09:51:55 +00005334** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
5335** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00005336** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00005337** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00005338**
drheaf6ae22016-11-09 20:14:34 +00005339** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5340** run faster by avoiding an unnecessary seek on cursor P1. However,
5341** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5342** seeks on the cursor or if the most recent seek used a key equal to P3.
drh3e9ca092009-09-08 01:14:48 +00005343**
5344** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
5345** UPDATE operation. Otherwise (if the flag is clear) then this opcode
5346** is part of an INSERT operation. The difference is only important to
5347** the update hook.
5348**
dan319eeb72011-03-19 08:38:50 +00005349** Parameter P4 may point to a Table structure, or may be NULL. If it is
5350** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
5351** following a successful insert.
danielk19771f6eec52006-06-16 06:17:47 +00005352**
drh93aed5a2008-01-16 17:46:38 +00005353** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
5354** allocated, then ownership of P2 is transferred to the pseudo-cursor
5355** and register P2 becomes ephemeral. If the cursor is changed, the
5356** value of register P2 will then change. Make sure this does not
5357** cause any problems.)
5358**
drhf0863fe2005-06-12 21:35:51 +00005359** This instruction only works on tables. The equivalent instruction
5360** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00005361*/
drh50ef6712019-02-22 23:29:56 +00005362case OP_Insert: {
drh3e9ca092009-09-08 01:14:48 +00005363 Mem *pData; /* MEM cell holding data for the record to be inserted */
5364 Mem *pKey; /* MEM cell holding key for the record */
drh3e9ca092009-09-08 01:14:48 +00005365 VdbeCursor *pC; /* Cursor to table into which insert is written */
drh3e9ca092009-09-08 01:14:48 +00005366 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
5367 const char *zDb; /* database name - used by the update hook */
dan319eeb72011-03-19 08:38:50 +00005368 Table *pTab; /* Table structure - used by update and pre-update hooks */
drh8eeb4462016-05-21 20:03:42 +00005369 BtreePayload x; /* Payload to be inserted */
drh856c1032009-06-02 15:21:42 +00005370
drha6c2ed92009-11-14 23:22:23 +00005371 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00005372 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00005373 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00005374 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00005375 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005376 assert( pC->eCurType==CURTYPE_BTREE );
drhbe3da242019-12-29 00:52:41 +00005377 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00005378 assert( pC->uc.pCursor!=0 );
dancb9a3642017-01-30 19:44:53 +00005379 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
drhcbf1b8e2013-11-11 22:55:26 +00005380 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
drh5b6afba2008-01-05 16:29:28 +00005381 REGISTER_TRACE(pOp->p2, pData);
drh4031baf2018-05-28 17:31:20 +00005382 sqlite3VdbeIncrWriteCounter(p, pC);
danielk19775f8d8a82004-05-11 00:28:42 +00005383
drh50ef6712019-02-22 23:29:56 +00005384 pKey = &aMem[pOp->p3];
5385 assert( pKey->flags & MEM_Int );
5386 assert( memIsValid(pKey) );
5387 REGISTER_TRACE(pOp->p3, pKey);
5388 x.nKey = pKey->u.i;
drhe05c9292009-10-29 13:48:10 +00005389
drh9b1c62d2011-03-30 21:04:43 +00005390 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00005391 assert( pC->iDb>=0 );
drh69c33822016-08-18 14:33:11 +00005392 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00005393 pTab = pOp->p4.pTab;
dancb9a3642017-01-30 19:44:53 +00005394 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
drh74c33022016-03-30 12:56:55 +00005395 }else{
drh4ec6f3a2018-01-12 19:33:18 +00005396 pTab = 0;
drhe1e1a432021-10-05 11:11:43 +00005397 zDb = 0;
dan46c47d42011-03-01 18:42:07 +00005398 }
5399
drh9b1c62d2011-03-30 21:04:43 +00005400#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00005401 /* Invoke the pre-update hook, if any */
drh4ec6f3a2018-01-12 19:33:18 +00005402 if( pTab ){
drh84ebe2b2018-01-12 18:46:52 +00005403 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
dana23a8732021-04-21 20:52:17 +00005404 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1);
drh84ebe2b2018-01-12 18:46:52 +00005405 }
drh4ec6f3a2018-01-12 19:33:18 +00005406 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
5407 /* Prevent post-update hook from running in cases when it should not */
5408 pTab = 0;
drh84ebe2b2018-01-12 18:46:52 +00005409 }
dan46c47d42011-03-01 18:42:07 +00005410 }
dancb9a3642017-01-30 19:44:53 +00005411 if( pOp->p5 & OPFLAG_ISNOOP ) break;
drh9b1c62d2011-03-30 21:04:43 +00005412#endif
dan46c47d42011-03-01 18:42:07 +00005413
drha05a7222008-01-19 03:35:58 +00005414 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhfae58d52017-01-26 17:26:44 +00005415 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
drh32881be2020-11-17 21:26:13 +00005416 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
dan21cd29a2017-10-23 16:03:54 +00005417 x.pData = pData->z;
5418 x.nData = pData->n;
drh3e9ca092009-09-08 01:14:48 +00005419 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
5420 if( pData->flags & MEM_Zero ){
drh8eeb4462016-05-21 20:03:42 +00005421 x.nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00005422 }else{
drh8eeb4462016-05-21 20:03:42 +00005423 x.nZero = 0;
drha05a7222008-01-19 03:35:58 +00005424 }
drh8eeb4462016-05-21 20:03:42 +00005425 x.pKey = 0;
5426 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
dan7aae7352020-12-10 18:06:24 +00005427 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
5428 seekResult
drh3e9ca092009-09-08 01:14:48 +00005429 );
drha05a7222008-01-19 03:35:58 +00005430 pC->deferredMoveto = 0;
5431 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00005432
drha05a7222008-01-19 03:35:58 +00005433 /* Invoke the update-hook if required. */
drh9467abf2016-02-17 18:44:11 +00005434 if( rc ) goto abort_due_to_error;
drh4ec6f3a2018-01-12 19:33:18 +00005435 if( pTab ){
5436 assert( db->xUpdateCallback!=0 );
5437 assert( pTab->aCol!=0 );
5438 db->xUpdateCallback(db->pUpdateArg,
5439 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
5440 zDb, pTab->zName, x.nKey);
drha05a7222008-01-19 03:35:58 +00005441 }
drh5e00f6c2001-09-13 13:46:56 +00005442 break;
5443}
5444
dan7aae7352020-12-10 18:06:24 +00005445/* Opcode: RowCell P1 P2 P3 * *
dand2ffc972020-12-10 19:20:15 +00005446**
5447** P1 and P2 are both open cursors. Both must be opened on the same type
5448** of table - intkey or index. This opcode is used as part of copying
5449** the current row from P2 into P1. If the cursors are opened on intkey
5450** tables, register P3 contains the rowid to use with the new record in
5451** P1. If they are opened on index tables, P3 is not used.
5452**
5453** This opcode must be followed by either an Insert or InsertIdx opcode
5454** with the OPFLAG_PREFORMAT flag set to complete the insert operation.
dan036e0672020-12-08 20:19:07 +00005455*/
dan7aae7352020-12-10 18:06:24 +00005456case OP_RowCell: {
dan036e0672020-12-08 20:19:07 +00005457 VdbeCursor *pDest; /* Cursor to write to */
5458 VdbeCursor *pSrc; /* Cursor to read from */
5459 i64 iKey; /* Rowid value to insert with */
dan7aae7352020-12-10 18:06:24 +00005460 assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert );
drha06eafc2020-12-29 15:06:26 +00005461 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
5462 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
dand2ffc972020-12-10 19:20:15 +00005463 assert( pOp[1].p5 & OPFLAG_PREFORMAT );
dan036e0672020-12-08 20:19:07 +00005464 pDest = p->apCsr[pOp->p1];
5465 pSrc = p->apCsr[pOp->p2];
dancd1b2d02020-12-09 20:33:51 +00005466 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
dan7aae7352020-12-10 18:06:24 +00005467 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
dan036e0672020-12-08 20:19:07 +00005468 if( rc!=SQLITE_OK ) goto abort_due_to_error;
5469 break;
dan7aae7352020-12-10 18:06:24 +00005470};
dan036e0672020-12-08 20:19:07 +00005471
dan438b8812015-09-15 15:55:15 +00005472/* Opcode: Delete P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005473**
drh5edc3122001-09-13 21:53:09 +00005474** Delete the record at which the P1 cursor is currently pointing.
5475**
drhe807bdb2016-01-21 17:06:33 +00005476** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
5477** the cursor will be left pointing at either the next or the previous
5478** record in the table. If it is left pointing at the next record, then
5479** the next Next instruction will be a no-op. As a result, in this case
5480** it is ok to delete a record from within a Next loop. If
5481** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
5482** left in an undefined state.
drhc8d30ac2002-04-12 10:08:59 +00005483**
drhdef19e32016-01-27 16:26:25 +00005484** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
5485** delete one of several associated with deleting a table row and all its
5486** associated index entries. Exactly one of those deletes is the "primary"
5487** delete. The others are all on OPFLAG_FORDELETE cursors or else are
5488** marked with the AUXDELETE flag.
drhe807bdb2016-01-21 17:06:33 +00005489**
5490** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
5491** change count is incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00005492**
drh91fd4d42008-01-19 20:11:25 +00005493** P1 must not be pseudo-table. It has to be a real table with
5494** multiple rows.
5495**
drh5e769a52016-09-28 16:05:53 +00005496** If P4 is not NULL then it points to a Table object. In this case either
dan319eeb72011-03-19 08:38:50 +00005497** the update or pre-update hook, or both, may be invoked. The P1 cursor must
5498** have been positioned using OP_NotFound prior to invoking this opcode in
5499** this case. Specifically, if one is configured, the pre-update hook is
5500** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
5501** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
dan46c47d42011-03-01 18:42:07 +00005502**
5503** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
5504** of the memory cell that contains the value that the rowid of the row will
5505** be set to by the update.
drh5e00f6c2001-09-13 13:46:56 +00005506*/
drh9cbf3422008-01-17 16:22:13 +00005507case OP_Delete: {
drhdfe88ec2008-11-03 20:55:06 +00005508 VdbeCursor *pC;
dan46c47d42011-03-01 18:42:07 +00005509 const char *zDb;
dan319eeb72011-03-19 08:38:50 +00005510 Table *pTab;
dan46c47d42011-03-01 18:42:07 +00005511 int opflags;
drh91fd4d42008-01-19 20:11:25 +00005512
dan46c47d42011-03-01 18:42:07 +00005513 opflags = pOp->p2;
drh653b82a2009-06-22 11:10:47 +00005514 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5515 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005516 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005517 assert( pC->eCurType==CURTYPE_BTREE );
5518 assert( pC->uc.pCursor!=0 );
drh9a65f2c2009-06-22 19:05:40 +00005519 assert( pC->deferredMoveto==0 );
drh4031baf2018-05-28 17:31:20 +00005520 sqlite3VdbeIncrWriteCounter(p, pC);
drh9a65f2c2009-06-22 19:05:40 +00005521
drhb53a5a92014-10-12 22:37:22 +00005522#ifdef SQLITE_DEBUG
drh6b559f32020-01-02 19:50:50 +00005523 if( pOp->p4type==P4_TABLE
5524 && HasRowid(pOp->p4.pTab)
5525 && pOp->p5==0
5526 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
5527 ){
dan438b8812015-09-15 15:55:15 +00005528 /* If p5 is zero, the seek operation that positioned the cursor prior to
5529 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
5530 ** the row that is being deleted */
drha7c90c42016-06-04 20:37:10 +00005531 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan0971ef42019-05-16 20:13:32 +00005532 assert( CORRUPT_DB || pC->movetoTarget==iKey );
drhb53a5a92014-10-12 22:37:22 +00005533 }
5534#endif
drh91fd4d42008-01-19 20:11:25 +00005535
dan438b8812015-09-15 15:55:15 +00005536 /* If the update-hook or pre-update-hook will be invoked, set zDb to
5537 ** the name of the db to pass as to it. Also set local pTab to a copy
5538 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
5539 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
5540 ** VdbeCursor.movetoTarget to the current rowid. */
drhc556f3c2016-03-30 15:30:07 +00005541 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00005542 assert( pC->iDb>=0 );
drhc556f3c2016-03-30 15:30:07 +00005543 assert( pOp->p4.pTab!=0 );
drh69c33822016-08-18 14:33:11 +00005544 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00005545 pTab = pOp->p4.pTab;
drhc556f3c2016-03-30 15:30:07 +00005546 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
drha7c90c42016-06-04 20:37:10 +00005547 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan438b8812015-09-15 15:55:15 +00005548 }
drh74c33022016-03-30 12:56:55 +00005549 }else{
drhe1e1a432021-10-05 11:11:43 +00005550 zDb = 0;
5551 pTab = 0;
drh92fe38e2014-10-14 13:41:32 +00005552 }
dan46c47d42011-03-01 18:42:07 +00005553
drh9b1c62d2011-03-30 21:04:43 +00005554#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00005555 /* Invoke the pre-update-hook if required. */
drhe1e1a432021-10-05 11:11:43 +00005556 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab );
5557 if( db->xPreUpdateCallback && pTab ){
dancb9a3642017-01-30 19:44:53 +00005558 assert( !(opflags & OPFLAG_ISUPDATE)
5559 || HasRowid(pTab)==0
5560 || (aMem[pOp->p3].flags & MEM_Int)
5561 );
dan46c47d42011-03-01 18:42:07 +00005562 sqlite3VdbePreUpdateHook(p, pC,
5563 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
drh92fe38e2014-10-14 13:41:32 +00005564 zDb, pTab, pC->movetoTarget,
dana23a8732021-04-21 20:52:17 +00005565 pOp->p3, -1
dan46c47d42011-03-01 18:42:07 +00005566 );
5567 }
dan46c47d42011-03-01 18:42:07 +00005568 if( opflags & OPFLAG_ISNOOP ) break;
drhc556f3c2016-03-30 15:30:07 +00005569#endif
drhb53a5a92014-10-12 22:37:22 +00005570
drhdef19e32016-01-27 16:26:25 +00005571 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
5572 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
drhe807bdb2016-01-21 17:06:33 +00005573 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
drhdef19e32016-01-27 16:26:25 +00005574 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
drhb89aeb62016-01-27 15:49:32 +00005575
5576#ifdef SQLITE_DEBUG
dane61bbf42016-01-28 17:06:17 +00005577 if( p->pFrame==0 ){
5578 if( pC->isEphemeral==0
5579 && (pOp->p5 & OPFLAG_AUXDELETE)==0
5580 && (pC->wrFlag & OPFLAG_FORDELETE)==0
5581 ){
5582 nExtraDelete++;
5583 }
5584 if( pOp->p2 & OPFLAG_NCHANGE ){
5585 nExtraDelete--;
5586 }
drhb89aeb62016-01-27 15:49:32 +00005587 }
5588#endif
5589
drhc960dcb2015-11-20 19:22:01 +00005590 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
drh91fd4d42008-01-19 20:11:25 +00005591 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00005592 pC->seekResult = 0;
drhd3e1af42016-02-25 18:54:30 +00005593 if( rc ) goto abort_due_to_error;
danielk197794eb6a12005-12-15 15:22:08 +00005594
drh91fd4d42008-01-19 20:11:25 +00005595 /* Invoke the update-hook if required. */
dan46c47d42011-03-01 18:42:07 +00005596 if( opflags & OPFLAG_NCHANGE ){
5597 p->nChange++;
drh7d4c94b2021-10-04 22:34:38 +00005598 if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){
drh92fe38e2014-10-14 13:41:32 +00005599 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
dan438b8812015-09-15 15:55:15 +00005600 pC->movetoTarget);
5601 assert( pC->iDb>=0 );
dan46c47d42011-03-01 18:42:07 +00005602 }
drh5e00f6c2001-09-13 13:46:56 +00005603 }
dan438b8812015-09-15 15:55:15 +00005604
rdcb0c374f2004-02-20 22:53:38 +00005605 break;
5606}
drhb7f1d9a2009-09-08 02:27:58 +00005607/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00005608**
drhb7f1d9a2009-09-08 02:27:58 +00005609** The value of the change counter is copied to the database handle
5610** change counter (returned by subsequent calls to sqlite3_changes()).
5611** Then the VMs internal change counter resets to 0.
5612** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00005613*/
drh9cbf3422008-01-17 16:22:13 +00005614case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00005615 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00005616 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00005617 break;
5618}
5619
drh1153c7b2013-11-01 22:02:56 +00005620/* Opcode: SorterCompare P1 P2 P3 P4
drh72e26de2016-08-24 21:24:04 +00005621** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00005622**
drh1153c7b2013-11-01 22:02:56 +00005623** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00005624** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00005625** the sorter cursor currently points to. Only the first P4 fields
5626** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00005627**
5628** If either P3 or the sorter contains a NULL in one of their significant
5629** fields (not counting the P4 fields at the end which are ignored) then
5630** the comparison is assumed to be equal.
5631**
5632** Fall through to next instruction if the two records compare equal to
5633** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00005634*/
5635case OP_SorterCompare: {
5636 VdbeCursor *pC;
5637 int res;
drhac502322014-07-30 13:56:48 +00005638 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00005639
5640 pC = p->apCsr[pOp->p1];
5641 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00005642 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00005643 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00005644 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00005645 res = 0;
drhac502322014-07-30 13:56:48 +00005646 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00005647 VdbeBranchTaken(res!=0,2);
drh9467abf2016-02-17 18:44:11 +00005648 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005649 if( res ) goto jump_to_p2;
dan5134d132011-09-02 10:31:11 +00005650 break;
5651};
5652
drh6cf4a7d2014-10-13 13:00:58 +00005653/* Opcode: SorterData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005654** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00005655**
5656** Write into register P2 the current sorter data for sorter cursor P1.
drh6cf4a7d2014-10-13 13:00:58 +00005657** Then clear the column header cache on cursor P3.
5658**
5659** This opcode is normally use to move a record out of the sorter and into
5660** a register that is the source for a pseudo-table cursor created using
5661** OpenPseudo. That pseudo-table cursor is the one that is identified by
5662** parameter P3. Clearing the P3 column cache as part of this opcode saves
5663** us from having to issue a separate NullRow instruction to clear that cache.
dan5134d132011-09-02 10:31:11 +00005664*/
5665case OP_SorterData: {
5666 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00005667
dan5134d132011-09-02 10:31:11 +00005668 pOut = &aMem[pOp->p2];
5669 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00005670 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00005671 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00005672 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
drh6cf4a7d2014-10-13 13:00:58 +00005673 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9467abf2016-02-17 18:44:11 +00005674 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00005675 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
dan5134d132011-09-02 10:31:11 +00005676 break;
5677}
5678
drhe7b554d2017-01-09 15:44:25 +00005679/* Opcode: RowData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005680** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00005681**
drh9057fc72016-11-25 19:32:32 +00005682** Write into register P2 the complete row content for the row at
5683** which cursor P1 is currently pointing.
drh98757152008-01-09 23:04:12 +00005684** There is no interpretation of the data.
5685** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00005686** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00005687**
drh9057fc72016-11-25 19:32:32 +00005688** If cursor P1 is an index, then the content is the key of the row.
5689** If cursor P2 is a table, then the content extracted is the data.
drh143f3c42004-01-07 20:37:52 +00005690**
drhde4fcfd2008-01-19 23:50:26 +00005691** If the P1 cursor must be pointing to a valid row (not a NULL row)
5692** of a real table, not a pseudo-table.
drhe7b554d2017-01-09 15:44:25 +00005693**
drh8cdafc32018-05-31 19:00:20 +00005694** If P3!=0 then this opcode is allowed to make an ephemeral pointer
drhe7b554d2017-01-09 15:44:25 +00005695** into the database page. That means that the content of the output
5696** register will be invalidated as soon as the cursor moves - including
drh416a8012018-05-31 19:14:52 +00005697** moves caused by other cursors that "save" the current cursors
drhe7b554d2017-01-09 15:44:25 +00005698** position in order that they can write to the same table. If P3==0
5699** then a copy of the data is made into memory. P3!=0 is faster, but
5700** P3==0 is safer.
5701**
5702** If P3!=0 then the content of the P2 register is unsuitable for use
5703** in OP_Result and any OP_Result will invalidate the P2 register content.
mistachkinab61cf72017-01-09 18:22:54 +00005704** The P2 register content is invalidated by opcodes like OP_Function or
drhe7b554d2017-01-09 15:44:25 +00005705** by any use of another cursor pointing to the same table.
drh143f3c42004-01-07 20:37:52 +00005706*/
danielk1977a7a8e142008-02-13 18:25:27 +00005707case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00005708 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00005709 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00005710 u32 n;
drh70ce3f02003-04-15 19:22:22 +00005711
drhe7b554d2017-01-09 15:44:25 +00005712 pOut = out2Prerelease(p, pOp);
danielk1977a7a8e142008-02-13 18:25:27 +00005713
drh653b82a2009-06-22 11:10:47 +00005714 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5715 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00005716 assert( pC!=0 );
5717 assert( pC->eCurType==CURTYPE_BTREE );
drh14da87f2013-11-20 21:51:33 +00005718 assert( isSorter(pC)==0 );
drhde4fcfd2008-01-19 23:50:26 +00005719 assert( pC->nullRow==0 );
drhc960dcb2015-11-20 19:22:01 +00005720 assert( pC->uc.pCursor!=0 );
5721 pCrsr = pC->uc.pCursor;
drh9a65f2c2009-06-22 19:05:40 +00005722
drh9057fc72016-11-25 19:32:32 +00005723 /* The OP_RowData opcodes always follow OP_NotExists or
drheeb95652016-05-26 20:56:38 +00005724 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
5725 ** that might invalidate the cursor.
5726 ** If this where not the case, on of the following assert()s
drhc22284f2014-10-13 16:02:20 +00005727 ** would fail. Should this ever change (because of changes in the code
5728 ** generator) then the fix would be to insert a call to
5729 ** sqlite3VdbeCursorMoveto().
drh9a65f2c2009-06-22 19:05:40 +00005730 */
5731 assert( pC->deferredMoveto==0 );
drhc22284f2014-10-13 16:02:20 +00005732 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drh9a65f2c2009-06-22 19:05:40 +00005733
drha7c90c42016-06-04 20:37:10 +00005734 n = sqlite3BtreePayloadSize(pCrsr);
drhd66c4f82016-06-04 20:58:35 +00005735 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha7c90c42016-06-04 20:37:10 +00005736 goto too_big;
drhde4fcfd2008-01-19 23:50:26 +00005737 }
drh722246e2014-10-07 23:02:24 +00005738 testcase( n==0 );
drh2a740062020-02-05 18:28:17 +00005739 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut);
drh9467abf2016-02-17 18:44:11 +00005740 if( rc ) goto abort_due_to_error;
drhe7b554d2017-01-09 15:44:25 +00005741 if( !pOp->p3 ) Deephemeralize(pOut);
drhb7654112008-01-12 12:48:07 +00005742 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00005743 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00005744 break;
5745}
5746
drh2133d822008-01-03 18:44:59 +00005747/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005748** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00005749**
drh2133d822008-01-03 18:44:59 +00005750** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00005751** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00005752**
5753** P1 can be either an ordinary table or a virtual table. There used to
5754** be a separate OP_VRowid opcode for use with virtual tables, but this
5755** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00005756*/
drh27a348c2015-04-13 19:14:06 +00005757case OP_Rowid: { /* out2 */
drhdfe88ec2008-11-03 20:55:06 +00005758 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00005759 i64 v;
drh856c1032009-06-02 15:21:42 +00005760 sqlite3_vtab *pVtab;
5761 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00005762
drh27a348c2015-04-13 19:14:06 +00005763 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00005764 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5765 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005766 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005767 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00005768 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00005769 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00005770 break;
5771 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00005772 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00005773#ifndef SQLITE_OMIT_VIRTUALTABLE
drhc960dcb2015-11-20 19:22:01 +00005774 }else if( pC->eCurType==CURTYPE_VTAB ){
5775 assert( pC->uc.pVCur!=0 );
5776 pVtab = pC->uc.pVCur->pVtab;
drh044925b2009-04-22 17:15:02 +00005777 pModule = pVtab->pModule;
5778 assert( pModule->xRowid );
drhc960dcb2015-11-20 19:22:01 +00005779 rc = pModule->xRowid(pC->uc.pVCur, &v);
dan016f7812013-08-21 17:35:48 +00005780 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00005781 if( rc ) goto abort_due_to_error;
drh044925b2009-04-22 17:15:02 +00005782#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00005783 }else{
drhc960dcb2015-11-20 19:22:01 +00005784 assert( pC->eCurType==CURTYPE_BTREE );
5785 assert( pC->uc.pCursor!=0 );
drhc22284f2014-10-13 16:02:20 +00005786 rc = sqlite3VdbeCursorRestore(pC);
drh61495262009-04-22 15:32:59 +00005787 if( rc ) goto abort_due_to_error;
dan2b8669a2014-11-17 19:42:48 +00005788 if( pC->nullRow ){
5789 pOut->flags = MEM_Null;
5790 break;
5791 }
drha7c90c42016-06-04 20:37:10 +00005792 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh5e00f6c2001-09-13 13:46:56 +00005793 }
drh4c583122008-01-04 22:01:03 +00005794 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00005795 break;
5796}
5797
drh9cbf3422008-01-17 16:22:13 +00005798/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00005799**
5800** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00005801** that occur while the cursor is on the null row will always
5802** write a NULL.
drha10be3d2022-02-25 18:51:09 +00005803**
drh3ac62432022-04-13 17:41:03 +00005804** If cursor P1 is not previously opened, open it now to a special
5805** pseudo-cursor that always returns NULL for every column.
drh17f71932002-02-21 12:01:27 +00005806*/
drh9cbf3422008-01-17 16:22:13 +00005807case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00005808 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00005809
drh653b82a2009-06-22 11:10:47 +00005810 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5811 pC = p->apCsr[pOp->p1];
drh3ac62432022-04-13 17:41:03 +00005812 if( pC==0 ){
5813 /* If the cursor is not already open, create a special kind of
5814 ** pseudo-cursor that always gives null rows. */
5815 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO);
5816 if( pC==0 ) goto no_mem;
5817 pC->seekResult = 0;
5818 pC->isTable = 1;
5819 pC->uc.pCursor = sqlite3BtreeFakeValidCursor();
5820 }
drhd7556d22004-05-14 21:59:40 +00005821 pC->nullRow = 1;
drh399af1d2013-11-20 17:25:55 +00005822 pC->cacheStatus = CACHE_STALE;
drhc960dcb2015-11-20 19:22:01 +00005823 if( pC->eCurType==CURTYPE_BTREE ){
5824 assert( pC->uc.pCursor!=0 );
5825 sqlite3BtreeClearCursor(pC->uc.pCursor);
danielk1977be51a652008-10-08 17:58:48 +00005826 }
drhcf025a82018-06-07 18:01:21 +00005827#ifdef SQLITE_DEBUG
5828 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
5829#endif
drh17f71932002-02-21 12:01:27 +00005830 break;
5831}
5832
drh86b40df2017-08-01 19:53:43 +00005833/* Opcode: SeekEnd P1 * * * *
5834**
5835** Position cursor P1 at the end of the btree for the purpose of
5836** appending a new entry onto the btree.
5837**
5838** It is assumed that the cursor is used only for appending and so
5839** if the cursor is valid, then the cursor must already be pointing
5840** at the end of the btree and so no changes are made to
5841** the cursor.
5842*/
5843/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00005844**
drh8af3f772014-07-25 18:01:06 +00005845** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00005846** will refer to the last entry in the database table or index.
5847** If the table or index is empty and P2>0, then jump immediately to P2.
5848** If P2 is 0 or if the table or index is not empty, fall through
5849** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00005850**
5851** This opcode leaves the cursor configured to move in reverse order,
5852** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005853** configured to use Prev, not Next.
drh9562b552002-02-19 15:00:07 +00005854*/
drh86b40df2017-08-01 19:53:43 +00005855case OP_SeekEnd:
drh9cbf3422008-01-17 16:22:13 +00005856case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005857 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00005858 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00005859 int res;
drh9562b552002-02-19 15:00:07 +00005860
drh653b82a2009-06-22 11:10:47 +00005861 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5862 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005863 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005864 assert( pC->eCurType==CURTYPE_BTREE );
5865 pCrsr = pC->uc.pCursor;
drh7abc5402011-10-22 21:00:46 +00005866 res = 0;
drh3da046d2013-11-11 03:24:11 +00005867 assert( pCrsr!=0 );
drh8af3f772014-07-25 18:01:06 +00005868#ifdef SQLITE_DEBUG
drh86b40df2017-08-01 19:53:43 +00005869 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00005870#endif
drh86b40df2017-08-01 19:53:43 +00005871 if( pOp->opcode==OP_SeekEnd ){
drhd6ef5af2016-11-15 04:00:24 +00005872 assert( pOp->p2==0 );
drh86b40df2017-08-01 19:53:43 +00005873 pC->seekResult = -1;
5874 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
5875 break;
5876 }
5877 }
5878 rc = sqlite3BtreeLast(pCrsr, &res);
5879 pC->nullRow = (u8)res;
5880 pC->deferredMoveto = 0;
5881 pC->cacheStatus = CACHE_STALE;
5882 if( rc ) goto abort_due_to_error;
5883 if( pOp->p2>0 ){
5884 VdbeBranchTaken(res!=0,2);
5885 if( res ) goto jump_to_p2;
drh9562b552002-02-19 15:00:07 +00005886 }
5887 break;
5888}
5889
drh5e98e832017-02-17 19:24:06 +00005890/* Opcode: IfSmaller P1 P2 P3 * *
5891**
5892** Estimate the number of rows in the table P1. Jump to P2 if that
5893** estimate is less than approximately 2**(0.1*P3).
5894*/
5895case OP_IfSmaller: { /* jump */
5896 VdbeCursor *pC;
5897 BtCursor *pCrsr;
5898 int res;
5899 i64 sz;
5900
5901 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5902 pC = p->apCsr[pOp->p1];
5903 assert( pC!=0 );
5904 pCrsr = pC->uc.pCursor;
5905 assert( pCrsr );
5906 rc = sqlite3BtreeFirst(pCrsr, &res);
5907 if( rc ) goto abort_due_to_error;
5908 if( res==0 ){
5909 sz = sqlite3BtreeRowCountEst(pCrsr);
5910 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
5911 }
5912 VdbeBranchTaken(res!=0,2);
5913 if( res ) goto jump_to_p2;
5914 break;
5915}
5916
drh0342b1f2005-09-01 03:07:44 +00005917
drh6bd4dc62016-12-23 16:05:22 +00005918/* Opcode: SorterSort P1 P2 * * *
5919**
5920** After all records have been inserted into the Sorter object
5921** identified by P1, invoke this opcode to actually do the sorting.
5922** Jump to P2 if there are no records to be sorted.
5923**
5924** This opcode is an alias for OP_Sort and OP_Rewind that is used
5925** for Sorter objects.
5926*/
drh9cbf3422008-01-17 16:22:13 +00005927/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00005928**
5929** This opcode does exactly the same thing as OP_Rewind except that
5930** it increments an undocumented global variable used for testing.
5931**
5932** Sorting is accomplished by writing records into a sorting index,
5933** then rewinding that index and playing it back from beginning to
5934** end. We use the OP_Sort opcode instead of OP_Rewind to do the
5935** rewinding so that the global variable will be incremented and
5936** regression tests can determine whether or not the optimizer is
5937** correctly optimizing out sorts.
5938*/
drhc6aff302011-09-01 15:32:47 +00005939case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00005940case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00005941#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00005942 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00005943 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00005944#endif
drh9b47ee32013-08-20 03:13:51 +00005945 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00005946 /* Fall through into OP_Rewind */
drh08b92082020-08-10 14:18:00 +00005947 /* no break */ deliberate_fall_through
drh0342b1f2005-09-01 03:07:44 +00005948}
drh038ebf62019-03-29 15:21:22 +00005949/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00005950**
drhf0863fe2005-06-12 21:35:51 +00005951** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00005952** will refer to the first entry in the database table or index.
dan04489b62014-10-31 20:11:32 +00005953** If the table or index is empty, jump immediately to P2.
5954** If the table or index is not empty, fall through to the following
5955** instruction.
drh8af3f772014-07-25 18:01:06 +00005956**
5957** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00005958** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00005959** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00005960*/
drh9cbf3422008-01-17 16:22:13 +00005961case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005962 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005963 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00005964 int res;
drh5e00f6c2001-09-13 13:46:56 +00005965
drh653b82a2009-06-22 11:10:47 +00005966 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh038ebf62019-03-29 15:21:22 +00005967 assert( pOp->p5==0 );
drh653b82a2009-06-22 11:10:47 +00005968 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005969 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005970 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00005971 res = 1;
drh8af3f772014-07-25 18:01:06 +00005972#ifdef SQLITE_DEBUG
5973 pC->seekOp = OP_Rewind;
5974#endif
dan689ab892011-08-12 15:02:00 +00005975 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00005976 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00005977 }else{
drhc960dcb2015-11-20 19:22:01 +00005978 assert( pC->eCurType==CURTYPE_BTREE );
5979 pCrsr = pC->uc.pCursor;
dana205a482011-08-27 18:48:57 +00005980 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00005981 rc = sqlite3BtreeFirst(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00005982 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00005983 pC->cacheStatus = CACHE_STALE;
drhf4dada72004-05-11 09:57:35 +00005984 }
drh9467abf2016-02-17 18:44:11 +00005985 if( rc ) goto abort_due_to_error;
drh9c1905f2008-12-10 22:32:56 +00005986 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00005987 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00005988 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00005989 if( res ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00005990 break;
5991}
5992
drha7c9dd52022-02-24 14:44:23 +00005993/* Opcode: Next P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00005994**
5995** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00005996** table or index. If there are no more key/value pairs then fall through
5997** to the following instruction. But if the cursor advance was successful,
5998** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00005999**
drh5dad9a32014-07-25 18:37:42 +00006000** The Next opcode is only valid following an SeekGT, SeekGE, or
6001** OP_Rewind opcode used to position the cursor. Next is not allowed
6002** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00006003**
drhf93cd942013-11-21 03:12:25 +00006004** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
6005** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00006006**
drhe39a7322014-02-03 14:04:11 +00006007** The P3 value is a hint to the btree implementation. If P3==1, that
6008** means P1 is an SQL index and that this instruction could have been
6009** omitted if that index had been unique. P3 is usually 0. P3 is
6010** always either 0 or 1.
6011**
drhafc266a2010-03-31 17:47:44 +00006012** If P5 is positive and the jump is taken, then event counter
6013** number P5-1 in the prepared statement is incremented.
6014**
drhf1949b62018-06-07 17:32:59 +00006015** See also: Prev
drh8721ce42001-11-07 14:22:00 +00006016*/
drha7c9dd52022-02-24 14:44:23 +00006017/* Opcode: Prev P1 P2 P3 * P5
drhc045ec52002-12-04 20:01:06 +00006018**
6019** Back up cursor P1 so that it points to the previous key/data pair in its
6020** table or index. If there is no previous key/value pairs then fall through
6021** to the following instruction. But if the cursor backup was successful,
6022** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00006023**
drh8af3f772014-07-25 18:01:06 +00006024**
drh5dad9a32014-07-25 18:37:42 +00006025** The Prev opcode is only valid following an SeekLT, SeekLE, or
6026** OP_Last opcode used to position the cursor. Prev is not allowed
6027** to follow SeekGT, SeekGE, or OP_Rewind.
drh8af3f772014-07-25 18:01:06 +00006028**
drhf93cd942013-11-21 03:12:25 +00006029** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
6030** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00006031**
drhe39a7322014-02-03 14:04:11 +00006032** The P3 value is a hint to the btree implementation. If P3==1, that
6033** means P1 is an SQL index and that this instruction could have been
6034** omitted if that index had been unique. P3 is usually 0. P3 is
6035** always either 0 or 1.
6036**
drhafc266a2010-03-31 17:47:44 +00006037** If P5 is positive and the jump is taken, then event counter
6038** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00006039*/
drh6bd4dc62016-12-23 16:05:22 +00006040/* Opcode: SorterNext P1 P2 * * P5
6041**
6042** This opcode works just like OP_Next except that P1 must be a
6043** sorter object for which the OP_SorterSort opcode has been
6044** invoked. This opcode advances the cursor to the next sorted
6045** record, or jumps to P2 if there are no more sorted records.
6046*/
drhf93cd942013-11-21 03:12:25 +00006047case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00006048 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00006049
drhf93cd942013-11-21 03:12:25 +00006050 pC = p->apCsr[pOp->p1];
6051 assert( isSorter(pC) );
drh2ab792e2017-05-30 18:34:07 +00006052 rc = sqlite3VdbeSorterNext(db, pC);
drhf93cd942013-11-21 03:12:25 +00006053 goto next_tail;
drha7c9dd52022-02-24 14:44:23 +00006054
drhf93cd942013-11-21 03:12:25 +00006055case OP_Prev: /* jump */
drha7c9dd52022-02-24 14:44:23 +00006056 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6057 assert( pOp->p5<ArraySize(p->aCounter) );
6058 pC = p->apCsr[pOp->p1];
6059 assert( pC!=0 );
6060 assert( pC->deferredMoveto==0 );
6061 assert( pC->eCurType==CURTYPE_BTREE );
6062 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
6063 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
6064 || pC->seekOp==OP_NullRow);
6065 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
6066 goto next_tail;
6067
drhf93cd942013-11-21 03:12:25 +00006068case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00006069 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00006070 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00006071 pC = p->apCsr[pOp->p1];
drhf93cd942013-11-21 03:12:25 +00006072 assert( pC!=0 );
6073 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00006074 assert( pC->eCurType==CURTYPE_BTREE );
drha7c9dd52022-02-24 14:44:23 +00006075 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drh790b37a2019-08-27 17:01:07 +00006076 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
6077 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
6078 || pC->seekOp==OP_IfNoHope);
drha7c9dd52022-02-24 14:44:23 +00006079 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3);
drh8af3f772014-07-25 18:01:06 +00006080
drhf93cd942013-11-21 03:12:25 +00006081next_tail:
drha3460582008-07-11 21:02:53 +00006082 pC->cacheStatus = CACHE_STALE;
drh2ab792e2017-05-30 18:34:07 +00006083 VdbeBranchTaken(rc==SQLITE_OK,2);
6084 if( rc==SQLITE_OK ){
drhf93cd942013-11-21 03:12:25 +00006085 pC->nullRow = 0;
drh9b47ee32013-08-20 03:13:51 +00006086 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00006087#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00006088 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00006089#endif
drhf56fa462015-04-13 21:39:54 +00006090 goto jump_to_p2_and_check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00006091 }
drh2ab792e2017-05-30 18:34:07 +00006092 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
6093 rc = SQLITE_OK;
6094 pC->nullRow = 1;
drh49afe3a2013-07-10 03:05:14 +00006095 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00006096}
6097
drh9b4eaeb2016-11-09 00:10:33 +00006098/* Opcode: IdxInsert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00006099** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00006100**
drhef8662b2011-06-20 21:47:58 +00006101** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00006102** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00006103** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00006104**
drhfb8c56f2016-11-09 01:19:25 +00006105** If P4 is not zero, then it is the number of values in the unpacked
drh9b4eaeb2016-11-09 00:10:33 +00006106** key of reg(P2). In that case, P3 is the index of the first register
6107** for the unpacked key. The availability of the unpacked key can sometimes
6108** be an optimization.
6109**
6110** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
6111** that this insert is likely to be an append.
drhe4d90812007-03-29 05:51:49 +00006112**
mistachkin21a919f2014-02-07 03:28:02 +00006113** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
6114** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
6115** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00006116**
drheaf6ae22016-11-09 20:14:34 +00006117** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
6118** run faster by avoiding an unnecessary seek on cursor P1. However,
6119** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
6120** seeks on the cursor or if the most recent seek used a key equivalent
6121** to P2.
drh0fd61352014-02-07 02:29:45 +00006122**
drhf0863fe2005-06-12 21:35:51 +00006123** This instruction only works for indices. The equivalent instruction
6124** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00006125*/
drh9cbf3422008-01-17 16:22:13 +00006126case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00006127 VdbeCursor *pC;
drh8eeb4462016-05-21 20:03:42 +00006128 BtreePayload x;
drh856c1032009-06-02 15:21:42 +00006129
drh653b82a2009-06-22 11:10:47 +00006130 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6131 pC = p->apCsr[pOp->p1];
drh4031baf2018-05-28 17:31:20 +00006132 sqlite3VdbeIncrWriteCounter(p, pC);
drh653b82a2009-06-22 11:10:47 +00006133 assert( pC!=0 );
drhc879c4e2020-02-06 13:57:08 +00006134 assert( !isSorter(pC) );
drh3c657212009-11-17 23:59:58 +00006135 pIn2 = &aMem[pOp->p2];
dan7aae7352020-12-10 18:06:24 +00006136 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
drh6546af12013-11-04 15:23:25 +00006137 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhc879c4e2020-02-06 13:57:08 +00006138 assert( pC->eCurType==CURTYPE_BTREE );
drh3da046d2013-11-11 03:24:11 +00006139 assert( pC->isTable==0 );
6140 rc = ExpandBlob(pIn2);
drh9467abf2016-02-17 18:44:11 +00006141 if( rc ) goto abort_due_to_error;
drhc879c4e2020-02-06 13:57:08 +00006142 x.nKey = pIn2->n;
6143 x.pKey = pIn2->z;
6144 x.aMem = aMem + pOp->p3;
6145 x.nMem = (u16)pOp->p4.i;
6146 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
dan7aae7352020-12-10 18:06:24 +00006147 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
drhc879c4e2020-02-06 13:57:08 +00006148 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
6149 );
6150 assert( pC->deferredMoveto==0 );
6151 pC->cacheStatus = CACHE_STALE;
6152 if( rc) goto abort_due_to_error;
6153 break;
6154}
6155
6156/* Opcode: SorterInsert P1 P2 * * *
6157** Synopsis: key=r[P2]
6158**
6159** Register P2 holds an SQL index key made using the
6160** MakeRecord instructions. This opcode writes that key
6161** into the sorter P1. Data for the entry is nil.
6162*/
6163case OP_SorterInsert: { /* in2 */
6164 VdbeCursor *pC;
6165
6166 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6167 pC = p->apCsr[pOp->p1];
6168 sqlite3VdbeIncrWriteCounter(p, pC);
6169 assert( pC!=0 );
6170 assert( isSorter(pC) );
6171 pIn2 = &aMem[pOp->p2];
6172 assert( pIn2->flags & MEM_Blob );
6173 assert( pC->isTable==0 );
6174 rc = ExpandBlob(pIn2);
6175 if( rc ) goto abort_due_to_error;
6176 rc = sqlite3VdbeSorterWrite(pC, pIn2);
drh9467abf2016-02-17 18:44:11 +00006177 if( rc) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00006178 break;
6179}
6180
drh85bd3532020-05-05 18:42:49 +00006181/* Opcode: IdxDelete P1 P2 P3 * P5
drhf63552b2013-10-30 00:25:03 +00006182** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00006183**
drhe14006d2008-03-25 17:23:32 +00006184** The content of P3 registers starting at register P2 form
6185** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00006186** index opened by cursor P1.
drh85bd3532020-05-05 18:42:49 +00006187**
6188** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
6189** if no matching index entry is found. This happens when running
6190** an UPDATE or DELETE statement and the index entry to be updated
6191** or deleted is not found. For some uses of IdxDelete
6192** (example: the EXCEPT operator) it does not matter that no matching
drha76b1512021-08-11 18:43:54 +00006193** entry is found. For those cases, P5 is zero. Also, do not raise
6194** this (self-correcting and non-critical) error if in writable_schema mode.
drh5e00f6c2001-09-13 13:46:56 +00006195*/
drhe14006d2008-03-25 17:23:32 +00006196case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00006197 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00006198 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00006199 int res;
6200 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00006201
drhe14006d2008-03-25 17:23:32 +00006202 assert( pOp->p3>0 );
drh9f6168b2016-03-19 23:32:58 +00006203 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00006204 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6205 pC = p->apCsr[pOp->p1];
6206 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00006207 assert( pC->eCurType==CURTYPE_BTREE );
drh4031baf2018-05-28 17:31:20 +00006208 sqlite3VdbeIncrWriteCounter(p, pC);
drhc960dcb2015-11-20 19:22:01 +00006209 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00006210 assert( pCrsr!=0 );
drh3da046d2013-11-11 03:24:11 +00006211 r.pKeyInfo = pC->pKeyInfo;
6212 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00006213 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00006214 r.aMem = &aMem[pOp->p2];
drh42a410d2021-06-19 18:32:20 +00006215 rc = sqlite3BtreeIndexMoveto(pCrsr, &r, &res);
drh9467abf2016-02-17 18:44:11 +00006216 if( rc ) goto abort_due_to_error;
6217 if( res==0 ){
dane61bbf42016-01-28 17:06:17 +00006218 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
drh9467abf2016-02-17 18:44:11 +00006219 if( rc ) goto abort_due_to_error;
drha76b1512021-08-11 18:43:54 +00006220 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
drhe5ceaac2021-01-25 21:24:14 +00006221 rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX, __LINE__, "index corruption");
drh85bd3532020-05-05 18:42:49 +00006222 goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00006223 }
drh3da046d2013-11-11 03:24:11 +00006224 assert( pC->deferredMoveto==0 );
6225 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00006226 pC->seekResult = 0;
drh5e00f6c2001-09-13 13:46:56 +00006227 break;
6228}
6229
drh170ad682017-06-02 15:44:22 +00006230/* Opcode: DeferredSeek P1 * P3 P4 *
6231** Synopsis: Move P3 to P1.rowid if needed
drh784c1b92016-01-30 16:59:56 +00006232**
6233** P1 is an open index cursor and P3 is a cursor on the corresponding
6234** table. This opcode does a deferred seek of the P3 table cursor
6235** to the row that corresponds to the current row of P1.
6236**
6237** This is a deferred seek. Nothing actually happens until
6238** the cursor is used to read a record. That way, if no reads
6239** occur, no unnecessary I/O happens.
6240**
6241** P4 may be an array of integers (type P4_INTARRAY) containing
drh19d720d2016-02-03 19:52:06 +00006242** one entry for each column in the P3 table. If array entry a(i)
6243** is non-zero, then reading column a(i)-1 from cursor P3 is
drh784c1b92016-01-30 16:59:56 +00006244** equivalent to performing the deferred seek and then reading column i
6245** from P1. This information is stored in P3 and used to redirect
6246** reads against P3 over to P1, thus possibly avoiding the need to
6247** seek and read cursor P3.
6248*/
drh2133d822008-01-03 18:44:59 +00006249/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006250** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00006251**
drh2133d822008-01-03 18:44:59 +00006252** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00006253** the end of the index key pointed to by cursor P1. This integer should be
6254** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00006255**
drh9437bd22009-02-01 00:29:56 +00006256** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00006257*/
drh170ad682017-06-02 15:44:22 +00006258case OP_DeferredSeek:
6259case OP_IdxRowid: { /* out2 */
6260 VdbeCursor *pC; /* The P1 index cursor */
6261 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
6262 i64 rowid; /* Rowid that P1 current points to */
drh8721ce42001-11-07 14:22:00 +00006263
drh653b82a2009-06-22 11:10:47 +00006264 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6265 pC = p->apCsr[pOp->p1];
6266 assert( pC!=0 );
drheab6c122022-04-14 12:59:25 +00006267 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) );
drh784c1b92016-01-30 16:59:56 +00006268 assert( pC->uc.pCursor!=0 );
drhc504f672022-04-14 14:19:23 +00006269 assert( pC->isTable==0 || IsNullCursor(pC) );
drhc22284f2014-10-13 16:02:20 +00006270 assert( pC->deferredMoveto==0 );
drh784c1b92016-01-30 16:59:56 +00006271 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
6272
6273 /* The IdxRowid and Seek opcodes are combined because of the commonality
6274 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
6275 rc = sqlite3VdbeCursorRestore(pC);
drhc22284f2014-10-13 16:02:20 +00006276
6277 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
drh784c1b92016-01-30 16:59:56 +00006278 ** out from under the cursor. That will never happens for an IdxRowid
6279 ** or Seek opcode */
drhc22284f2014-10-13 16:02:20 +00006280 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
6281
drh3da046d2013-11-11 03:24:11 +00006282 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00006283 rowid = 0; /* Not needed. Only used to silence a warning. */
drh784c1b92016-01-30 16:59:56 +00006284 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
drh3da046d2013-11-11 03:24:11 +00006285 if( rc!=SQLITE_OK ){
6286 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00006287 }
drh170ad682017-06-02 15:44:22 +00006288 if( pOp->opcode==OP_DeferredSeek ){
drh784c1b92016-01-30 16:59:56 +00006289 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
6290 pTabCur = p->apCsr[pOp->p3];
6291 assert( pTabCur!=0 );
6292 assert( pTabCur->eCurType==CURTYPE_BTREE );
6293 assert( pTabCur->uc.pCursor!=0 );
6294 assert( pTabCur->isTable );
6295 pTabCur->nullRow = 0;
6296 pTabCur->movetoTarget = rowid;
6297 pTabCur->deferredMoveto = 1;
drhfc569502022-02-25 20:11:59 +00006298 pTabCur->cacheStatus = CACHE_STALE;
drh784c1b92016-01-30 16:59:56 +00006299 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
drhe44ac382021-03-18 13:19:41 +00006300 assert( !pTabCur->isEphemeral );
drhb2486682022-01-03 01:43:28 +00006301 pTabCur->ub.aAltMap = pOp->p4.ai;
6302 assert( !pC->isEphemeral );
drh784c1b92016-01-30 16:59:56 +00006303 pTabCur->pAltCursor = pC;
6304 }else{
6305 pOut = out2Prerelease(p, pOp);
6306 pOut->u.i = rowid;
drh784c1b92016-01-30 16:59:56 +00006307 }
6308 }else{
6309 assert( pOp->opcode==OP_IdxRowid );
6310 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
drh8721ce42001-11-07 14:22:00 +00006311 }
6312 break;
6313}
6314
drhbe3da242019-12-29 00:52:41 +00006315/* Opcode: FinishSeek P1 * * * *
6316**
6317** If cursor P1 was previously moved via OP_DeferredSeek, complete that
6318** seek operation now, without further delay. If the cursor seek has
6319** already occurred, this instruction is a no-op.
6320*/
6321case OP_FinishSeek: {
6322 VdbeCursor *pC; /* The P1 index cursor */
6323
6324 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6325 pC = p->apCsr[pOp->p1];
6326 if( pC->deferredMoveto ){
6327 rc = sqlite3VdbeFinishMoveto(pC);
6328 if( rc ) goto abort_due_to_error;
6329 }
6330 break;
6331}
6332
drhc51ceeb2020-08-31 12:29:03 +00006333/* Opcode: IdxGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00006334** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00006335**
danielk197761dd5832008-04-18 11:31:12 +00006336** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00006337** key that omits the PRIMARY KEY. Compare this key value against the index
6338** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
6339** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00006340**
danielk197761dd5832008-04-18 11:31:12 +00006341** If the P1 index entry is greater than or equal to the key value
6342** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00006343*/
drhc51ceeb2020-08-31 12:29:03 +00006344/* Opcode: IdxGT P1 P2 P3 P4 *
drh4a1d3652014-02-14 15:13:36 +00006345** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00006346**
drh4a1d3652014-02-14 15:13:36 +00006347** The P4 register values beginning with P3 form an unpacked index
6348** key that omits the PRIMARY KEY. Compare this key value against the index
6349** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
6350** fields at the end.
6351**
6352** If the P1 index entry is greater than the key value
6353** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00006354*/
drhc51ceeb2020-08-31 12:29:03 +00006355/* Opcode: IdxLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00006356** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00006357**
danielk197761dd5832008-04-18 11:31:12 +00006358** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00006359** key that omits the PRIMARY KEY or ROWID. Compare this key value against
6360** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
6361** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00006362**
danielk197761dd5832008-04-18 11:31:12 +00006363** If the P1 index entry is less than the key value then jump to P2.
6364** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00006365*/
drhc51ceeb2020-08-31 12:29:03 +00006366/* Opcode: IdxLE P1 P2 P3 P4 *
drh4a1d3652014-02-14 15:13:36 +00006367** Synopsis: key=r[P3@P4]
6368**
6369** The P4 register values beginning with P3 form an unpacked index
6370** key that omits the PRIMARY KEY or ROWID. Compare this key value against
6371** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
6372** ROWID on the P1 index.
6373**
6374** If the P1 index entry is less than or equal to the key value then jump
6375** to P2. Otherwise fall through to the next instruction.
6376*/
6377case OP_IdxLE: /* jump */
6378case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00006379case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00006380case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00006381 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00006382 int res;
6383 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00006384
drh653b82a2009-06-22 11:10:47 +00006385 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6386 pC = p->apCsr[pOp->p1];
6387 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00006388 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00006389 assert( pC->eCurType==CURTYPE_BTREE );
6390 assert( pC->uc.pCursor!=0);
drh3da046d2013-11-11 03:24:11 +00006391 assert( pC->deferredMoveto==0 );
drh3da046d2013-11-11 03:24:11 +00006392 assert( pOp->p4type==P4_INT32 );
6393 r.pKeyInfo = pC->pKeyInfo;
6394 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00006395 if( pOp->opcode<OP_IdxLT ){
6396 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00006397 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00006398 }else{
drh4a1d3652014-02-14 15:13:36 +00006399 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00006400 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00006401 }
6402 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00006403#ifdef SQLITE_DEBUG
drh5eae9742018-08-03 13:56:26 +00006404 {
6405 int i;
6406 for(i=0; i<r.nField; i++){
6407 assert( memIsValid(&r.aMem[i]) );
6408 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
6409 }
6410 }
drh2b4ded92010-09-27 21:09:31 +00006411#endif
drhc40076a2020-09-29 16:05:09 +00006412
6413 /* Inlined version of sqlite3VdbeIdxKeyCompare() */
6414 {
6415 i64 nCellKey = 0;
6416 BtCursor *pCur;
6417 Mem m;
6418
6419 assert( pC->eCurType==CURTYPE_BTREE );
6420 pCur = pC->uc.pCursor;
6421 assert( sqlite3BtreeCursorIsValid(pCur) );
6422 nCellKey = sqlite3BtreePayloadSize(pCur);
6423 /* nCellKey will always be between 0 and 0xffffffff because of the way
6424 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
6425 if( nCellKey<=0 || nCellKey>0x7fffffff ){
6426 rc = SQLITE_CORRUPT_BKPT;
6427 goto abort_due_to_error;
6428 }
6429 sqlite3VdbeMemInit(&m, db, 0);
6430 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
6431 if( rc ) goto abort_due_to_error;
6432 res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, &r, 0);
drhfc854502022-03-02 17:50:59 +00006433 sqlite3VdbeMemReleaseMalloc(&m);
drhc40076a2020-09-29 16:05:09 +00006434 }
6435 /* End of inlined sqlite3VdbeIdxKeyCompare() */
6436
drh4a1d3652014-02-14 15:13:36 +00006437 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
6438 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
6439 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00006440 res = -res;
6441 }else{
drh4a1d3652014-02-14 15:13:36 +00006442 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00006443 res++;
6444 }
drh688852a2014-02-17 22:40:43 +00006445 VdbeBranchTaken(res>0,2);
drhc40076a2020-09-29 16:05:09 +00006446 assert( rc==SQLITE_OK );
drhf56fa462015-04-13 21:39:54 +00006447 if( res>0 ) goto jump_to_p2;
drh8721ce42001-11-07 14:22:00 +00006448 break;
6449}
6450
drh98757152008-01-09 23:04:12 +00006451/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00006452**
6453** Delete an entire database table or index whose root page in the database
6454** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00006455**
drh98757152008-01-09 23:04:12 +00006456** The table being destroyed is in the main database file if P3==0. If
6457** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00006458** that is used to store tables create using CREATE TEMPORARY TABLE.
6459**
drh205f48e2004-11-05 00:43:11 +00006460** If AUTOVACUUM is enabled then it is possible that another root page
6461** might be moved into the newly deleted root page in order to keep all
6462** root pages contiguous at the beginning of the database. The former
6463** value of the root page that moved - its value before the move occurred -
dana34adaf2017-04-08 14:11:47 +00006464** is stored in register P2. If no page movement was required (because the
6465** table being dropped was already the last one in the database) then a
6466** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
6467** is stored in register P2.
6468**
6469** This opcode throws an error if there are any active reader VMs when
6470** it is invoked. This is done to avoid the difficulty associated with
6471** updating existing cursors when a root page is moved in an AUTOVACUUM
6472** database. This error is thrown even if the database is not an AUTOVACUUM
6473** db in order to avoid introducing an incompatibility between autovacuum
6474** and non-autovacuum modes.
drh205f48e2004-11-05 00:43:11 +00006475**
drhb19a2bc2001-09-16 00:13:26 +00006476** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00006477*/
drh27a348c2015-04-13 19:14:06 +00006478case OP_Destroy: { /* out2 */
danielk1977a0bf2652004-11-04 14:30:04 +00006479 int iMoved;
drh856c1032009-06-02 15:21:42 +00006480 int iDb;
drh3a949872012-09-18 13:20:13 +00006481
drh4031baf2018-05-28 17:31:20 +00006482 sqlite3VdbeIncrWriteCounter(p, 0);
drh9e92a472013-06-27 17:40:30 +00006483 assert( p->readOnly==0 );
drh055f2982016-01-15 15:06:41 +00006484 assert( pOp->p1>1 );
drh27a348c2015-04-13 19:14:06 +00006485 pOut = out2Prerelease(p, pOp);
drh3c657212009-11-17 23:59:58 +00006486 pOut->flags = MEM_Null;
drh086723a2015-03-24 12:51:52 +00006487 if( db->nVdbeRead > db->nVDestroy+1 ){
danielk1977e6efa742004-11-10 11:55:10 +00006488 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00006489 p->errorAction = OE_Abort;
drh9467abf2016-02-17 18:44:11 +00006490 goto abort_due_to_error;
danielk1977e6efa742004-11-10 11:55:10 +00006491 }else{
drh856c1032009-06-02 15:21:42 +00006492 iDb = pOp->p3;
drha7ab6d82014-07-21 15:44:39 +00006493 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00006494 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00006495 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00006496 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00006497 pOut->u.i = iMoved;
drh9467abf2016-02-17 18:44:11 +00006498 if( rc ) goto abort_due_to_error;
drh3765df42006-06-28 18:18:09 +00006499#ifndef SQLITE_OMIT_AUTOVACUUM
drh9467abf2016-02-17 18:44:11 +00006500 if( iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00006501 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
6502 /* All OP_Destroy operations occur on the same btree */
6503 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
6504 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00006505 }
drh3765df42006-06-28 18:18:09 +00006506#endif
danielk1977a0bf2652004-11-04 14:30:04 +00006507 }
drh5e00f6c2001-09-13 13:46:56 +00006508 break;
6509}
6510
danielk1977c7af4842008-10-27 13:59:33 +00006511/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00006512**
6513** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00006514** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00006515** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00006516**
drhf57b3392001-10-08 13:22:32 +00006517** The table being clear is in the main database file if P2==0. If
6518** P2==1 then the table to be clear is in the auxiliary database file
6519** that is used to store tables create using CREATE TEMPORARY TABLE.
6520**
drha6df0e62021-06-03 18:51:51 +00006521** If the P3 value is non-zero, then the row change count is incremented
6522** by the number of rows in the table being cleared. If P3 is greater
6523** than zero, then the value stored in register P3 is also incremented
6524** by the number of rows in the table being cleared.
danielk1977c7af4842008-10-27 13:59:33 +00006525**
drhb19a2bc2001-09-16 00:13:26 +00006526** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00006527*/
drh9cbf3422008-01-17 16:22:13 +00006528case OP_Clear: {
dan2c718872021-06-22 18:32:05 +00006529 i64 nChange;
drh856c1032009-06-02 15:21:42 +00006530
drh4031baf2018-05-28 17:31:20 +00006531 sqlite3VdbeIncrWriteCounter(p, 0);
drh856c1032009-06-02 15:21:42 +00006532 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00006533 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00006534 assert( DbMaskTest(p->btreeMask, pOp->p2) );
drha6df0e62021-06-03 18:51:51 +00006535 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange);
danielk1977c7af4842008-10-27 13:59:33 +00006536 if( pOp->p3 ){
6537 p->nChange += nChange;
6538 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00006539 assert( memIsValid(&aMem[pOp->p3]) );
6540 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00006541 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00006542 }
6543 }
drh9467abf2016-02-17 18:44:11 +00006544 if( rc ) goto abort_due_to_error;
drh5edc3122001-09-13 21:53:09 +00006545 break;
6546}
6547
drh65ea12c2014-03-19 17:41:36 +00006548/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00006549**
drh65ea12c2014-03-19 17:41:36 +00006550** Delete all contents from the ephemeral table or sorter
6551** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00006552**
drh65ea12c2014-03-19 17:41:36 +00006553** This opcode only works for cursors used for sorting and
6554** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00006555*/
drh65ea12c2014-03-19 17:41:36 +00006556case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00006557 VdbeCursor *pC;
6558
6559 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6560 pC = p->apCsr[pOp->p1];
6561 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00006562 if( isSorter(pC) ){
6563 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
drh65ea12c2014-03-19 17:41:36 +00006564 }else{
drhc960dcb2015-11-20 19:22:01 +00006565 assert( pC->eCurType==CURTYPE_BTREE );
drh65ea12c2014-03-19 17:41:36 +00006566 assert( pC->isEphemeral );
drhc960dcb2015-11-20 19:22:01 +00006567 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
drh9467abf2016-02-17 18:44:11 +00006568 if( rc ) goto abort_due_to_error;
drh65ea12c2014-03-19 17:41:36 +00006569 }
drh079a3072014-03-19 14:10:55 +00006570 break;
6571}
6572
drh0f3f7662017-08-18 14:34:28 +00006573/* Opcode: CreateBtree P1 P2 P3 * *
6574** Synopsis: r[P2]=root iDb=P1 flags=P3
drh5b2fd562001-09-13 15:21:31 +00006575**
drh0f3f7662017-08-18 14:34:28 +00006576** Allocate a new b-tree in the main database file if P1==0 or in the
6577** TEMP database file if P1==1 or in an attached database if
6578** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
drh416a8012018-05-31 19:14:52 +00006579** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
drh0f3f7662017-08-18 14:34:28 +00006580** The root page number of the new b-tree is stored in register P2.
drh5b2fd562001-09-13 15:21:31 +00006581*/
drh0f3f7662017-08-18 14:34:28 +00006582case OP_CreateBtree: { /* out2 */
drhabc38152020-07-22 13:38:04 +00006583 Pgno pgno;
drh234c39d2004-07-24 03:30:47 +00006584 Db *pDb;
drh856c1032009-06-02 15:21:42 +00006585
drh4031baf2018-05-28 17:31:20 +00006586 sqlite3VdbeIncrWriteCounter(p, 0);
drh27a348c2015-04-13 19:14:06 +00006587 pOut = out2Prerelease(p, pOp);
drh856c1032009-06-02 15:21:42 +00006588 pgno = 0;
drh0f3f7662017-08-18 14:34:28 +00006589 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
drh234c39d2004-07-24 03:30:47 +00006590 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006591 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00006592 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00006593 pDb = &db->aDb[pOp->p1];
6594 assert( pDb->pBt!=0 );
drh0f3f7662017-08-18 14:34:28 +00006595 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
drh9467abf2016-02-17 18:44:11 +00006596 if( rc ) goto abort_due_to_error;
drh88a003e2008-12-11 16:17:03 +00006597 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00006598 break;
6599}
6600
drh4a54bb52017-02-18 15:58:52 +00006601/* Opcode: SqlExec * * * P4 *
6602**
6603** Run the SQL statement or statements specified in the P4 string.
6604*/
6605case OP_SqlExec: {
drh4031baf2018-05-28 17:31:20 +00006606 sqlite3VdbeIncrWriteCounter(p, 0);
drhbce04142017-02-23 00:58:36 +00006607 db->nSqlExec++;
drh4a54bb52017-02-18 15:58:52 +00006608 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
drhbce04142017-02-23 00:58:36 +00006609 db->nSqlExec--;
drh4a54bb52017-02-18 15:58:52 +00006610 if( rc ) goto abort_due_to_error;
6611 break;
6612}
6613
drh22645842011-03-24 01:34:03 +00006614/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00006615**
drh346a70c2020-06-15 20:27:35 +00006616** Read and parse all entries from the schema table of database P1
drh1595abc2018-08-14 19:27:51 +00006617** that match the WHERE clause P4. If P4 is a NULL pointer, then the
6618** entire schema for P1 is reparsed.
drh234c39d2004-07-24 03:30:47 +00006619**
6620** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00006621** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00006622*/
drh9cbf3422008-01-17 16:22:13 +00006623case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00006624 int iDb;
drh067b92b2020-06-19 15:24:12 +00006625 const char *zSchema;
drh856c1032009-06-02 15:21:42 +00006626 char *zSql;
6627 InitData initData;
6628
drhbdaec522011-04-04 00:14:43 +00006629 /* Any prepared statement that invokes this opcode will hold mutexes
6630 ** on every btree. This is a prerequisite for invoking
6631 ** sqlite3InitCallback().
6632 */
6633#ifdef SQLITE_DEBUG
6634 for(iDb=0; iDb<db->nDb; iDb++){
6635 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
6636 }
6637#endif
drhbdaec522011-04-04 00:14:43 +00006638
drh856c1032009-06-02 15:21:42 +00006639 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00006640 assert( iDb>=0 && iDb<db->nDb );
drhfe972342021-06-07 11:50:23 +00006641 assert( DbHasProperty(db, iDb, DB_SchemaLoaded)
6642 || db->mallocFailed
6643 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) );
dane325ffe2018-08-11 13:40:20 +00006644
6645#ifndef SQLITE_OMIT_ALTERTABLE
6646 if( pOp->p4.z==0 ){
6647 sqlite3SchemaClear(db->aDb[iDb].pSchema);
danb0c79202018-08-11 18:34:25 +00006648 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
dan6a5a13d2021-02-17 20:08:22 +00006649 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
dane325ffe2018-08-11 13:40:20 +00006650 db->mDbFlags |= DBFLAG_SchemaChange;
dan0d5fa6b2018-08-24 17:55:49 +00006651 p->expired = 0;
dane325ffe2018-08-11 13:40:20 +00006652 }else
6653#endif
drh1595abc2018-08-14 19:27:51 +00006654 {
drha4a871c2021-11-04 14:04:20 +00006655 zSchema = LEGACY_SCHEMA_TABLE;
danielk1977a8bbef82009-03-23 17:11:26 +00006656 initData.db = db;
mistachkin1c06b472018-09-27 00:04:31 +00006657 initData.iDb = iDb;
danielk1977a8bbef82009-03-23 17:11:26 +00006658 initData.pzErrMsg = &p->zErrMsg;
drh9fd88e82018-09-07 11:08:31 +00006659 initData.mInitFlags = 0;
drh3b3ddba2020-07-22 18:03:56 +00006660 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
danielk1977a8bbef82009-03-23 17:11:26 +00006661 zSql = sqlite3MPrintf(db,
drhc5a93d42019-08-12 00:08:07 +00006662 "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
drh067b92b2020-06-19 15:24:12 +00006663 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
danielk1977a8bbef82009-03-23 17:11:26 +00006664 if( zSql==0 ){
mistachkinfad30392016-02-13 23:43:46 +00006665 rc = SQLITE_NOMEM_BKPT;
danielk1977a8bbef82009-03-23 17:11:26 +00006666 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00006667 assert( db->init.busy==0 );
6668 db->init.busy = 1;
6669 initData.rc = SQLITE_OK;
drh6b86e512019-01-05 21:09:37 +00006670 initData.nInitRow = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00006671 assert( !db->mallocFailed );
6672 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
6673 if( rc==SQLITE_OK ) rc = initData.rc;
drh6b86e512019-01-05 21:09:37 +00006674 if( rc==SQLITE_OK && initData.nInitRow==0 ){
6675 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
6676 ** at least one SQL statement. Any less than that indicates that
drh1e32bed2020-06-19 13:33:53 +00006677 ** the sqlite_schema table is corrupt. */
drh6b86e512019-01-05 21:09:37 +00006678 rc = SQLITE_CORRUPT_BKPT;
6679 }
drhdbd6a7d2017-04-05 12:39:49 +00006680 sqlite3DbFreeNN(db, zSql);
danielk1977a8bbef82009-03-23 17:11:26 +00006681 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00006682 }
drh3c23a882007-01-09 14:01:13 +00006683 }
drh9467abf2016-02-17 18:44:11 +00006684 if( rc ){
6685 sqlite3ResetAllSchemasOfConnection(db);
6686 if( rc==SQLITE_NOMEM ){
6687 goto no_mem;
6688 }
6689 goto abort_due_to_error;
danielk1977261919c2005-12-06 12:52:59 +00006690 }
drh234c39d2004-07-24 03:30:47 +00006691 break;
6692}
6693
drh8bfdf722009-06-19 14:06:03 +00006694#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00006695/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00006696**
6697** Read the sqlite_stat1 table for database P1 and load the content
6698** of that table into the internal index hash table. This will cause
6699** the analysis to be used when preparing all subsequent queries.
6700*/
drh9cbf3422008-01-17 16:22:13 +00006701case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00006702 assert( pOp->p1>=0 && pOp->p1<db->nDb );
6703 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00006704 if( rc ) goto abort_due_to_error;
drh497e4462005-07-23 03:18:40 +00006705 break;
6706}
drh8bfdf722009-06-19 14:06:03 +00006707#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00006708
drh98757152008-01-09 23:04:12 +00006709/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006710**
6711** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006712** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00006713** is dropped from disk (using the Destroy opcode) in order to keep
6714** the internal representation of the
drh956bc922004-07-24 17:38:29 +00006715** schema consistent with what is on disk.
6716*/
drh9cbf3422008-01-17 16:22:13 +00006717case OP_DropTable: {
drh4031baf2018-05-28 17:31:20 +00006718 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006719 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006720 break;
6721}
6722
drh98757152008-01-09 23:04:12 +00006723/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006724**
6725** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006726** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00006727** is dropped from disk (using the Destroy opcode)
6728** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00006729** schema consistent with what is on disk.
6730*/
drh9cbf3422008-01-17 16:22:13 +00006731case OP_DropIndex: {
drh4031baf2018-05-28 17:31:20 +00006732 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006733 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006734 break;
6735}
6736
drh98757152008-01-09 23:04:12 +00006737/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006738**
6739** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006740** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00006741** is dropped from disk (using the Destroy opcode) in order to keep
6742** the internal representation of the
drh956bc922004-07-24 17:38:29 +00006743** schema consistent with what is on disk.
6744*/
drh9cbf3422008-01-17 16:22:13 +00006745case OP_DropTrigger: {
drh4031baf2018-05-28 17:31:20 +00006746 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006747 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006748 break;
6749}
6750
drh234c39d2004-07-24 03:30:47 +00006751
drhb7f91642004-10-31 02:22:47 +00006752#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98968b22016-03-15 22:00:39 +00006753/* Opcode: IntegrityCk P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00006754**
drh98757152008-01-09 23:04:12 +00006755** Do an analysis of the currently open database. Store in
6756** register P1 the text of an error message describing any problems.
6757** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00006758**
drh66accfc2017-02-22 18:04:42 +00006759** The register P3 contains one less than the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00006760** At most reg(P3) errors will be reported.
6761** In other words, the analysis stops as soon as reg(P1) errors are
6762** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00006763**
drh98968b22016-03-15 22:00:39 +00006764** The root page numbers of all tables in the database are integers
6765** stored in P4_INTARRAY argument.
drh21504322002-06-25 13:16:02 +00006766**
drh98757152008-01-09 23:04:12 +00006767** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00006768** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00006769**
drh1dcdbc02007-01-27 02:24:54 +00006770** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00006771*/
drhaaab5722002-02-19 13:39:21 +00006772case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00006773 int nRoot; /* Number of tables to check. (Number of root pages.) */
drhabc38152020-07-22 13:38:04 +00006774 Pgno *aRoot; /* Array of rootpage numbers for tables to be checked */
drh98757152008-01-09 23:04:12 +00006775 int nErr; /* Number of errors reported */
6776 char *z; /* Text of the error report */
6777 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00006778
drh1713afb2013-06-28 01:24:57 +00006779 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00006780 nRoot = pOp->p2;
drh98968b22016-03-15 22:00:39 +00006781 aRoot = pOp->p4.ai;
drh79069752004-05-22 21:30:40 +00006782 assert( nRoot>0 );
mistachkincec5f1d2020-08-04 16:11:37 +00006783 assert( aRoot[0]==(Pgno)nRoot );
drh9f6168b2016-03-19 23:32:58 +00006784 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006785 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00006786 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00006787 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00006788 pIn1 = &aMem[pOp->p1];
drh98757152008-01-09 23:04:12 +00006789 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006790 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drh21f6daa2019-10-11 14:21:48 +00006791 z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
drh66accfc2017-02-22 18:04:42 +00006792 (int)pnErr->u.i+1, &nErr);
drha05a7222008-01-19 03:35:58 +00006793 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00006794 if( nErr==0 ){
6795 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00006796 }else if( z==0 ){
6797 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00006798 }else{
drh66accfc2017-02-22 18:04:42 +00006799 pnErr->u.i -= nErr-1;
danielk1977a7a8e142008-02-13 18:25:27 +00006800 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00006801 }
drhb7654112008-01-12 12:48:07 +00006802 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00006803 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh21f6daa2019-10-11 14:21:48 +00006804 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00006805}
drhb7f91642004-10-31 02:22:47 +00006806#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00006807
drh3d4501e2008-12-04 20:40:10 +00006808/* Opcode: RowSetAdd P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00006809** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00006810**
drhbb6783b2017-04-29 18:02:49 +00006811** Insert the integer value held by register P2 into a RowSet object
drh3d4501e2008-12-04 20:40:10 +00006812** held in register P1.
6813**
6814** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00006815*/
drh93952eb2009-11-13 19:43:43 +00006816case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00006817 pIn1 = &aMem[pOp->p1];
6818 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00006819 assert( (pIn2->flags & MEM_Int)!=0 );
drh9d67afc2018-08-29 20:24:03 +00006820 if( (pIn1->flags & MEM_Blob)==0 ){
6821 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00006822 }
drh9d67afc2018-08-29 20:24:03 +00006823 assert( sqlite3VdbeMemIsRowSet(pIn1) );
6824 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00006825 break;
6826}
6827
6828/* Opcode: RowSetRead P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00006829** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00006830**
drhbb6783b2017-04-29 18:02:49 +00006831** Extract the smallest value from the RowSet object in P1
6832** and put that value into register P3.
6833** Or, if RowSet object P1 is initially empty, leave P3
drh3d4501e2008-12-04 20:40:10 +00006834** unchanged and jump to instruction P2.
6835*/
drh93952eb2009-11-13 19:43:43 +00006836case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00006837 i64 val;
drh49afe3a2013-07-10 03:05:14 +00006838
drh3c657212009-11-17 23:59:58 +00006839 pIn1 = &aMem[pOp->p1];
drh9d67afc2018-08-29 20:24:03 +00006840 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
6841 if( (pIn1->flags & MEM_Blob)==0
6842 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
drh3d4501e2008-12-04 20:40:10 +00006843 ){
6844 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00006845 sqlite3VdbeMemSetNull(pIn1);
drh688852a2014-02-17 22:40:43 +00006846 VdbeBranchTaken(1,2);
drhf56fa462015-04-13 21:39:54 +00006847 goto jump_to_p2_and_check_for_interrupt;
drh3d4501e2008-12-04 20:40:10 +00006848 }else{
6849 /* A value was pulled from the index */
drh688852a2014-02-17 22:40:43 +00006850 VdbeBranchTaken(0,2);
drhf56fa462015-04-13 21:39:54 +00006851 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00006852 }
drh49afe3a2013-07-10 03:05:14 +00006853 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00006854}
6855
drh1b26c7c2009-04-22 02:15:47 +00006856/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00006857** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00006858**
drhade97602009-04-21 15:05:18 +00006859** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00006860** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00006861** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00006862** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00006863** next opcode.
danielk19771d461462009-04-21 09:02:45 +00006864**
drhbb6783b2017-04-29 18:02:49 +00006865** The RowSet object is optimized for the case where sets of integers
6866** are inserted in distinct phases, which each set contains no duplicates.
6867** Each set is identified by a unique P4 value. The first set
6868** must have P4==0, the final set must have P4==-1, and for all other sets
6869** must have P4>0.
danielk19771d461462009-04-21 09:02:45 +00006870**
6871** This allows optimizations: (a) when P4==0 there is no need to test
drhbb6783b2017-04-29 18:02:49 +00006872** the RowSet object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00006873** (b) when P4==-1 there is no need to insert the value, as it will
6874** never be tested for, and (c) when a value that is part of set X is
6875** inserted, there is no need to search to see if the same value was
6876** previously inserted as part of set X (only if it was previously
6877** inserted as part of some other set).
6878*/
drh1b26c7c2009-04-22 02:15:47 +00006879case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00006880 int iSet;
6881 int exists;
6882
drh3c657212009-11-17 23:59:58 +00006883 pIn1 = &aMem[pOp->p1];
6884 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006885 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00006886 assert( pIn3->flags&MEM_Int );
6887
drh1b26c7c2009-04-22 02:15:47 +00006888 /* If there is anything other than a rowset object in memory cell P1,
6889 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00006890 */
drh9d67afc2018-08-29 20:24:03 +00006891 if( (pIn1->flags & MEM_Blob)==0 ){
6892 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00006893 }
drh9d67afc2018-08-29 20:24:03 +00006894 assert( sqlite3VdbeMemIsRowSet(pIn1) );
danielk19771d461462009-04-21 09:02:45 +00006895 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00006896 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00006897 if( iSet ){
drh9d67afc2018-08-29 20:24:03 +00006898 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00006899 VdbeBranchTaken(exists!=0,2);
drhf56fa462015-04-13 21:39:54 +00006900 if( exists ) goto jump_to_p2;
danielk19771d461462009-04-21 09:02:45 +00006901 }
6902 if( iSet>=0 ){
drh9d67afc2018-08-29 20:24:03 +00006903 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00006904 }
6905 break;
6906}
6907
drh5e00f6c2001-09-13 13:46:56 +00006908
danielk197793758c82005-01-21 08:13:14 +00006909#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00006910
drh0fd61352014-02-07 02:29:45 +00006911/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00006912**
dan76d462e2009-08-30 11:42:51 +00006913** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00006914**
dan76d462e2009-08-30 11:42:51 +00006915** P1 contains the address of the memory cell that contains the first memory
6916** cell in an array of values used as arguments to the sub-program. P2
6917** contains the address to jump to if the sub-program throws an IGNORE
6918** exception using the RAISE() function. Register P3 contains the address
6919** of a memory cell in this (the parent) VM that is used to allocate the
6920** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00006921**
6922** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00006923**
6924** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00006925*/
dan76d462e2009-08-30 11:42:51 +00006926case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00006927 int nMem; /* Number of memory registers for sub-program */
6928 int nByte; /* Bytes of runtime space required for sub-program */
6929 Mem *pRt; /* Register to allocate runtime space */
6930 Mem *pMem; /* Used to iterate through memory cells */
6931 Mem *pEnd; /* Last memory cell in new array */
6932 VdbeFrame *pFrame; /* New vdbe frame to execute in */
6933 SubProgram *pProgram; /* Sub-program to execute */
6934 void *t; /* Token identifying trigger */
6935
6936 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00006937 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00006938 assert( pProgram->nOp>0 );
6939
dan1da40a32009-09-19 17:00:31 +00006940 /* If the p5 flag is clear, then recursive invocation of triggers is
6941 ** disabled for backwards compatibility (p5 is set if this sub-program
6942 ** is really a trigger, not a foreign key action, and the flag set
6943 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00006944 **
6945 ** It is recursive invocation of triggers, at the SQL level, that is
6946 ** disabled. In some cases a single trigger may generate more than one
6947 ** SubProgram (if the trigger may be executed with more than one different
6948 ** ON CONFLICT algorithm). SubProgram structures associated with a
6949 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00006950 ** variable. */
6951 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00006952 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00006953 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
6954 if( pFrame ) break;
6955 }
6956
danf5894502009-10-07 18:41:19 +00006957 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00006958 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006959 sqlite3VdbeError(p, "too many levels of trigger recursion");
drh9467abf2016-02-17 18:44:11 +00006960 goto abort_due_to_error;
dan165921a2009-08-28 18:53:45 +00006961 }
6962
6963 /* Register pRt is used to store the memory required to save the state
6964 ** of the current program, and the memory required at runtime to execute
6965 ** the trigger program. If this trigger has been fired before, then pRt
6966 ** is already allocated. Otherwise, it must be initialized. */
drh72f56ef2018-08-29 18:47:22 +00006967 if( (pRt->flags&MEM_Blob)==0 ){
dan165921a2009-08-28 18:53:45 +00006968 /* SubProgram.nMem is set to the number of memory cells used by the
6969 ** program stored in SubProgram.aOp. As well as these, one memory
6970 ** cell is required for each cursor used by the program. Set local
6971 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
6972 */
dan65a7cd12009-09-01 12:16:01 +00006973 nMem = pProgram->nMem + pProgram->nCsr;
drh3cdce922016-03-21 00:30:40 +00006974 assert( nMem>0 );
6975 if( pProgram->nCsr==0 ) nMem++;
dan65a7cd12009-09-01 12:16:01 +00006976 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00006977 + nMem * sizeof(Mem)
drhab087d42017-03-24 17:59:56 +00006978 + pProgram->nCsr * sizeof(VdbeCursor*)
6979 + (pProgram->nOp + 7)/8;
dan165921a2009-08-28 18:53:45 +00006980 pFrame = sqlite3DbMallocZero(db, nByte);
6981 if( !pFrame ){
6982 goto no_mem;
6983 }
6984 sqlite3VdbeMemRelease(pRt);
drh72f56ef2018-08-29 18:47:22 +00006985 pRt->flags = MEM_Blob|MEM_Dyn;
6986 pRt->z = (char*)pFrame;
6987 pRt->n = nByte;
6988 pRt->xDel = sqlite3VdbeFrameMemDel;
dan165921a2009-08-28 18:53:45 +00006989
6990 pFrame->v = p;
6991 pFrame->nChildMem = nMem;
6992 pFrame->nChildCsr = pProgram->nCsr;
drhf56fa462015-04-13 21:39:54 +00006993 pFrame->pc = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +00006994 pFrame->aMem = p->aMem;
6995 pFrame->nMem = p->nMem;
6996 pFrame->apCsr = p->apCsr;
6997 pFrame->nCursor = p->nCursor;
6998 pFrame->aOp = p->aOp;
6999 pFrame->nOp = p->nOp;
7000 pFrame->token = pProgram->token;
dane2f771b2014-11-03 15:33:17 +00007001#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00007002 pFrame->anExec = p->anExec;
dane2f771b2014-11-03 15:33:17 +00007003#endif
drh72f56ef2018-08-29 18:47:22 +00007004#ifdef SQLITE_DEBUG
7005 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
7006#endif
dan165921a2009-08-28 18:53:45 +00007007
7008 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
7009 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00007010 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00007011 pMem->db = db;
7012 }
7013 }else{
drh72f56ef2018-08-29 18:47:22 +00007014 pFrame = (VdbeFrame*)pRt->z;
7015 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
drh9f6168b2016-03-19 23:32:58 +00007016 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
7017 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
dan165921a2009-08-28 18:53:45 +00007018 assert( pProgram->nCsr==pFrame->nChildCsr );
drhf56fa462015-04-13 21:39:54 +00007019 assert( (int)(pOp - aOp)==pFrame->pc );
dan165921a2009-08-28 18:53:45 +00007020 }
7021
7022 p->nFrame++;
7023 pFrame->pParent = p->pFrame;
drhfae58d52017-01-26 17:26:44 +00007024 pFrame->lastRowid = db->lastRowid;
dan76d462e2009-08-30 11:42:51 +00007025 pFrame->nChange = p->nChange;
danc3da6672014-10-28 18:24:16 +00007026 pFrame->nDbChange = p->db->nChange;
dan32001322016-02-19 18:54:29 +00007027 assert( pFrame->pAuxData==0 );
7028 pFrame->pAuxData = p->pAuxData;
7029 p->pAuxData = 0;
dan2832ad42009-08-31 15:27:27 +00007030 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00007031 p->pFrame = pFrame;
drh9f6168b2016-03-19 23:32:58 +00007032 p->aMem = aMem = VdbeFrameMem(pFrame);
dan165921a2009-08-28 18:53:45 +00007033 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00007034 p->nCursor = (u16)pFrame->nChildCsr;
drh9f6168b2016-03-19 23:32:58 +00007035 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
drhab087d42017-03-24 17:59:56 +00007036 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
drh18333ef2017-03-24 18:38:41 +00007037 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
drhbbe879d2009-11-14 18:04:35 +00007038 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00007039 p->nOp = pProgram->nOp;
dane2f771b2014-11-03 15:33:17 +00007040#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00007041 p->anExec = 0;
dane2f771b2014-11-03 15:33:17 +00007042#endif
drhb2e61bc2019-01-25 19:29:01 +00007043#ifdef SQLITE_DEBUG
7044 /* Verify that second and subsequent executions of the same trigger do not
7045 ** try to reuse register values from the first use. */
7046 {
7047 int i;
7048 for(i=0; i<p->nMem; i++){
7049 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
drhf5cfe6f2020-03-03 20:48:12 +00007050 MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */
drhb2e61bc2019-01-25 19:29:01 +00007051 }
7052 }
7053#endif
drhf56fa462015-04-13 21:39:54 +00007054 pOp = &aOp[-1];
drhb1af9c62019-02-20 13:55:45 +00007055 goto check_for_interrupt;
dan165921a2009-08-28 18:53:45 +00007056}
7057
dan76d462e2009-08-30 11:42:51 +00007058/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00007059**
dan76d462e2009-08-30 11:42:51 +00007060** This opcode is only ever present in sub-programs called via the
7061** OP_Program instruction. Copy a value currently stored in a memory
7062** cell of the calling (parent) frame to cell P2 in the current frames
7063** address space. This is used by trigger programs to access the new.*
7064** and old.* values.
dan165921a2009-08-28 18:53:45 +00007065**
dan76d462e2009-08-30 11:42:51 +00007066** The address of the cell in the parent frame is determined by adding
7067** the value of the P1 argument to the value of the P1 argument to the
7068** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00007069*/
drh27a348c2015-04-13 19:14:06 +00007070case OP_Param: { /* out2 */
dan65a7cd12009-09-01 12:16:01 +00007071 VdbeFrame *pFrame;
7072 Mem *pIn;
drh27a348c2015-04-13 19:14:06 +00007073 pOut = out2Prerelease(p, pOp);
dan65a7cd12009-09-01 12:16:01 +00007074 pFrame = p->pFrame;
7075 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00007076 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
7077 break;
7078}
7079
danielk197793758c82005-01-21 08:13:14 +00007080#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00007081
dan1da40a32009-09-19 17:00:31 +00007082#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00007083/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00007084** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00007085**
dan0ff297e2009-09-25 17:03:14 +00007086** Increment a "constraint counter" by P2 (P2 may be negative or positive).
7087** If P1 is non-zero, the database constraint counter is incremented
7088** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00007089** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00007090*/
dan32b09f22009-09-23 17:29:59 +00007091case OP_FkCounter: {
drh963c74d2013-07-11 12:19:12 +00007092 if( db->flags & SQLITE_DeferFKs ){
dancb3e4b72013-07-03 19:53:05 +00007093 db->nDeferredImmCons += pOp->p2;
7094 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00007095 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00007096 }else{
dan0ff297e2009-09-25 17:03:14 +00007097 p->nFkConstraint += pOp->p2;
7098 }
7099 break;
7100}
7101
7102/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00007103** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00007104**
7105** This opcode tests if a foreign key constraint-counter is currently zero.
7106** If so, jump to instruction P2. Otherwise, fall through to the next
7107** instruction.
7108**
7109** If P1 is non-zero, then the jump is taken if the database constraint-counter
7110** is zero (the one that counts deferred constraint violations). If P1 is
7111** zero, the jump is taken if the statement constraint-counter is zero
7112** (immediate foreign key constraint violations).
7113*/
7114case OP_FkIfZero: { /* jump */
7115 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00007116 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00007117 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan0ff297e2009-09-25 17:03:14 +00007118 }else{
drh688852a2014-02-17 22:40:43 +00007119 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00007120 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan32b09f22009-09-23 17:29:59 +00007121 }
dan1da40a32009-09-19 17:00:31 +00007122 break;
7123}
7124#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
7125
drh205f48e2004-11-05 00:43:11 +00007126#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00007127/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00007128** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00007129**
dan76d462e2009-08-30 11:42:51 +00007130** P1 is a register in the root frame of this VM (the root frame is
7131** different from the current frame if this instruction is being executed
7132** within a sub-program). Set the value of register P1 to the maximum of
7133** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00007134**
7135** This instruction throws an error if the memory cell is not initially
7136** an integer.
7137*/
dan76d462e2009-08-30 11:42:51 +00007138case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00007139 VdbeFrame *pFrame;
7140 if( p->pFrame ){
7141 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
7142 pIn1 = &pFrame->aMem[pOp->p1];
7143 }else{
drha6c2ed92009-11-14 23:22:23 +00007144 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00007145 }
drh2b4ded92010-09-27 21:09:31 +00007146 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00007147 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00007148 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00007149 sqlite3VdbeMemIntegerify(pIn2);
7150 if( pIn1->u.i<pIn2->u.i){
7151 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00007152 }
7153 break;
7154}
7155#endif /* SQLITE_OMIT_AUTOINCREMENT */
7156
drh8b0cf382015-10-06 21:07:06 +00007157/* Opcode: IfPos P1 P2 P3 * *
7158** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00007159**
drh16897072015-03-07 00:57:37 +00007160** Register P1 must contain an integer.
mistachkin91a3ecb2015-10-06 21:49:55 +00007161** If the value of register P1 is 1 or greater, subtract P3 from the
drh8b0cf382015-10-06 21:07:06 +00007162** value in P1 and jump to P2.
drh6f58f702006-01-08 05:26:41 +00007163**
drh16897072015-03-07 00:57:37 +00007164** If the initial value of register P1 is less than 1, then the
7165** value is unchanged and control passes through to the next instruction.
danielk1977a2dc3b12005-02-05 12:48:48 +00007166*/
drh9cbf3422008-01-17 16:22:13 +00007167case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00007168 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00007169 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00007170 VdbeBranchTaken( pIn1->u.i>0, 2);
drh8b0cf382015-10-06 21:07:06 +00007171 if( pIn1->u.i>0 ){
7172 pIn1->u.i -= pOp->p3;
7173 goto jump_to_p2;
7174 }
drhec7429a2005-10-06 16:53:14 +00007175 break;
7176}
7177
drhcc2fa4c2016-01-25 15:57:29 +00007178/* Opcode: OffsetLimit P1 P2 P3 * *
7179** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
drh15007a92006-01-08 18:10:17 +00007180**
drhcc2fa4c2016-01-25 15:57:29 +00007181** This opcode performs a commonly used computation associated with
7182** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
7183** holds the offset counter. The opcode computes the combined value
7184** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
7185** value computed is the total number of rows that will need to be
7186** visited in order to complete the query.
7187**
7188** If r[P3] is zero or negative, that means there is no OFFSET
7189** and r[P2] is set to be the value of the LIMIT, r[P1].
7190**
7191** if r[P1] is zero or negative, that means there is no LIMIT
7192** and r[P2] is set to -1.
7193**
7194** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
drh15007a92006-01-08 18:10:17 +00007195*/
drhcc2fa4c2016-01-25 15:57:29 +00007196case OP_OffsetLimit: { /* in1, out2, in3 */
drh719da302016-12-10 04:06:49 +00007197 i64 x;
drh3c657212009-11-17 23:59:58 +00007198 pIn1 = &aMem[pOp->p1];
drhcc2fa4c2016-01-25 15:57:29 +00007199 pIn3 = &aMem[pOp->p3];
7200 pOut = out2Prerelease(p, pOp);
7201 assert( pIn1->flags & MEM_Int );
7202 assert( pIn3->flags & MEM_Int );
drh719da302016-12-10 04:06:49 +00007203 x = pIn1->u.i;
7204 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
7205 /* If the LIMIT is less than or equal to zero, loop forever. This
7206 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
7207 ** also loop forever. This is undocumented. In fact, one could argue
7208 ** that the loop should terminate. But assuming 1 billion iterations
7209 ** per second (far exceeding the capabilities of any current hardware)
7210 ** it would take nearly 300 years to actually reach the limit. So
7211 ** looping forever is a reasonable approximation. */
7212 pOut->u.i = -1;
7213 }else{
7214 pOut->u.i = x;
7215 }
drh15007a92006-01-08 18:10:17 +00007216 break;
7217}
7218
drhf99dd352016-12-18 17:42:00 +00007219/* Opcode: IfNotZero P1 P2 * * *
7220** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
drhec7429a2005-10-06 16:53:14 +00007221**
drh16897072015-03-07 00:57:37 +00007222** Register P1 must contain an integer. If the content of register P1 is
drhf99dd352016-12-18 17:42:00 +00007223** initially greater than zero, then decrement the value in register P1.
7224** If it is non-zero (negative or positive) and then also jump to P2.
7225** If register P1 is initially zero, leave it unchanged and fall through.
drhec7429a2005-10-06 16:53:14 +00007226*/
drh16897072015-03-07 00:57:37 +00007227case OP_IfNotZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00007228 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00007229 assert( pIn1->flags&MEM_Int );
drh16897072015-03-07 00:57:37 +00007230 VdbeBranchTaken(pIn1->u.i<0, 2);
7231 if( pIn1->u.i ){
drhf99dd352016-12-18 17:42:00 +00007232 if( pIn1->u.i>0 ) pIn1->u.i--;
drhf56fa462015-04-13 21:39:54 +00007233 goto jump_to_p2;
drh16897072015-03-07 00:57:37 +00007234 }
7235 break;
7236}
7237
7238/* Opcode: DecrJumpZero P1 P2 * * *
7239** Synopsis: if (--r[P1])==0 goto P2
7240**
drhab5be2e2016-11-30 05:08:59 +00007241** Register P1 must hold an integer. Decrement the value in P1
7242** and jump to P2 if the new value is exactly zero.
drh16897072015-03-07 00:57:37 +00007243*/
7244case OP_DecrJumpZero: { /* jump, in1 */
7245 pIn1 = &aMem[pOp->p1];
7246 assert( pIn1->flags&MEM_Int );
drhab5be2e2016-11-30 05:08:59 +00007247 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
7248 VdbeBranchTaken(pIn1->u.i==0, 2);
7249 if( pIn1->u.i==0 ) goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00007250 break;
7251}
7252
drh16897072015-03-07 00:57:37 +00007253
drh8f26da62018-07-05 21:22:57 +00007254/* Opcode: AggStep * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00007255** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00007256**
drh8f26da62018-07-05 21:22:57 +00007257** Execute the xStep function for an aggregate.
7258** The function has P5 arguments. P4 is a pointer to the
dan9a947222018-06-14 19:06:36 +00007259** FuncDef structure that specifies the function. Register P3 is the
drhe2d9e7c2015-06-26 18:47:53 +00007260** accumulator.
drhe5095352002-02-24 03:25:14 +00007261**
drh98757152008-01-09 23:04:12 +00007262** The P5 arguments are taken from register P2 and its
7263** successors.
drhe5095352002-02-24 03:25:14 +00007264*/
drh8f26da62018-07-05 21:22:57 +00007265/* Opcode: AggInverse * P2 P3 P4 P5
7266** Synopsis: accum=r[P3] inverse(r[P2@P5])
7267**
7268** Execute the xInverse function for an aggregate.
7269** The function has P5 arguments. P4 is a pointer to the
7270** FuncDef structure that specifies the function. Register P3 is the
7271** accumulator.
7272**
7273** The P5 arguments are taken from register P2 and its
7274** successors.
7275*/
7276/* Opcode: AggStep1 P1 P2 P3 P4 P5
drhe2d9e7c2015-06-26 18:47:53 +00007277** Synopsis: accum=r[P3] step(r[P2@P5])
7278**
dan9a947222018-06-14 19:06:36 +00007279** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
7280** aggregate. The function has P5 arguments. P4 is a pointer to the
7281** FuncDef structure that specifies the function. Register P3 is the
7282** accumulator.
drhe2d9e7c2015-06-26 18:47:53 +00007283**
7284** The P5 arguments are taken from register P2 and its
7285** successors.
7286**
7287** This opcode is initially coded as OP_AggStep0. On first evaluation,
7288** the FuncDef stored in P4 is converted into an sqlite3_context and
7289** the opcode is changed. In this way, the initialization of the
7290** sqlite3_context only happens once, instead of on each call to the
7291** step function.
7292*/
drh8f26da62018-07-05 21:22:57 +00007293case OP_AggInverse:
7294case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00007295 int n;
drh9c7c9132015-06-26 18:16:52 +00007296 sqlite3_context *pCtx;
drhe5095352002-02-24 03:25:14 +00007297
drh9c7c9132015-06-26 18:16:52 +00007298 assert( pOp->p4type==P4_FUNCDEF );
drh856c1032009-06-02 15:21:42 +00007299 n = pOp->p5;
drh9f6168b2016-03-19 23:32:58 +00007300 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
7301 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
drh9c7c9132015-06-26 18:16:52 +00007302 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drhf09ac0b2018-01-23 03:44:06 +00007303 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
7304 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
drh9c7c9132015-06-26 18:16:52 +00007305 if( pCtx==0 ) goto no_mem;
7306 pCtx->pMem = 0;
drhf09ac0b2018-01-23 03:44:06 +00007307 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
7308 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00007309 pCtx->pFunc = pOp->p4.pFunc;
7310 pCtx->iOp = (int)(pOp - aOp);
7311 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00007312 pCtx->skipFlag = 0;
7313 pCtx->isError = 0;
drh659fdb42022-04-01 15:31:58 +00007314 pCtx->enc = encoding;
drh9c7c9132015-06-26 18:16:52 +00007315 pCtx->argc = n;
7316 pOp->p4type = P4_FUNCCTX;
7317 pOp->p4.pCtx = pCtx;
drh2c885d02018-07-07 19:36:04 +00007318
7319 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
drh8f26da62018-07-05 21:22:57 +00007320 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
drh2c885d02018-07-07 19:36:04 +00007321
drh8f26da62018-07-05 21:22:57 +00007322 pOp->opcode = OP_AggStep1;
drh9c7c9132015-06-26 18:16:52 +00007323 /* Fall through into OP_AggStep */
drh08b92082020-08-10 14:18:00 +00007324 /* no break */ deliberate_fall_through
drh9c7c9132015-06-26 18:16:52 +00007325}
drh8f26da62018-07-05 21:22:57 +00007326case OP_AggStep1: {
drh9c7c9132015-06-26 18:16:52 +00007327 int i;
7328 sqlite3_context *pCtx;
7329 Mem *pMem;
drh9c7c9132015-06-26 18:16:52 +00007330
7331 assert( pOp->p4type==P4_FUNCCTX );
7332 pCtx = pOp->p4.pCtx;
7333 pMem = &aMem[pOp->p3];
7334
drh2c885d02018-07-07 19:36:04 +00007335#ifdef SQLITE_DEBUG
7336 if( pOp->p1 ){
7337 /* This is an OP_AggInverse call. Verify that xStep has always
7338 ** been called at least once prior to any xInverse call. */
7339 assert( pMem->uTemp==0x1122e0e3 );
7340 }else{
7341 /* This is an OP_AggStep call. Mark it as such. */
7342 pMem->uTemp = 0x1122e0e3;
7343 }
7344#endif
7345
drh9c7c9132015-06-26 18:16:52 +00007346 /* If this function is inside of a trigger, the register array in aMem[]
7347 ** might change from one evaluation to the next. The next block of code
7348 ** checks to see if the register array has changed, and if so it
7349 ** reinitializes the relavant parts of the sqlite3_context object */
7350 if( pCtx->pMem != pMem ){
7351 pCtx->pMem = pMem;
7352 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
7353 }
7354
7355#ifdef SQLITE_DEBUG
7356 for(i=0; i<pCtx->argc; i++){
7357 assert( memIsValid(pCtx->argv[i]) );
7358 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7359 }
7360#endif
7361
drhabfcea22005-09-06 20:36:48 +00007362 pMem->n++;
drhf09ac0b2018-01-23 03:44:06 +00007363 assert( pCtx->pOut->flags==MEM_Null );
7364 assert( pCtx->isError==0 );
7365 assert( pCtx->skipFlag==0 );
dan67a9b8e2018-06-22 20:51:35 +00007366#ifndef SQLITE_OMIT_WINDOWFUNC
7367 if( pOp->p1 ){
7368 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
7369 }else
7370#endif
7371 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
7372
drhf09ac0b2018-01-23 03:44:06 +00007373 if( pCtx->isError ){
7374 if( pCtx->isError>0 ){
7375 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
drh9c7c9132015-06-26 18:16:52 +00007376 rc = pCtx->isError;
7377 }
drhf09ac0b2018-01-23 03:44:06 +00007378 if( pCtx->skipFlag ){
7379 assert( pOp[-1].opcode==OP_CollSeq );
7380 i = pOp[-1].p1;
7381 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
7382 pCtx->skipFlag = 0;
7383 }
7384 sqlite3VdbeMemRelease(pCtx->pOut);
7385 pCtx->pOut->flags = MEM_Null;
7386 pCtx->isError = 0;
drh9467abf2016-02-17 18:44:11 +00007387 if( rc ) goto abort_due_to_error;
drh1350b032002-02-27 19:00:20 +00007388 }
drhf09ac0b2018-01-23 03:44:06 +00007389 assert( pCtx->pOut->flags==MEM_Null );
7390 assert( pCtx->skipFlag==0 );
drh5e00f6c2001-09-13 13:46:56 +00007391 break;
7392}
7393
drh8f26da62018-07-05 21:22:57 +00007394/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00007395** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00007396**
dan9a947222018-06-14 19:06:36 +00007397** P1 is the memory location that is the accumulator for an aggregate
drh8f26da62018-07-05 21:22:57 +00007398** or window function. Execute the finalizer function
7399** for an aggregate and store the result in P1.
drha10a34b2005-09-07 22:09:48 +00007400**
7401** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00007402** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00007403** argument is not used by this opcode. It is only there to disambiguate
7404** functions that can take varying numbers of arguments. The
drh8be47a72018-07-05 20:05:29 +00007405** P4 argument is only needed for the case where
drha10a34b2005-09-07 22:09:48 +00007406** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00007407*/
drh8f26da62018-07-05 21:22:57 +00007408/* Opcode: AggValue * P2 P3 P4 *
7409** Synopsis: r[P3]=value N=P2
7410**
7411** Invoke the xValue() function and store the result in register P3.
7412**
7413** P2 is the number of arguments that the step function takes and
7414** P4 is a pointer to the FuncDef for this function. The P2
7415** argument is not used by this opcode. It is only there to disambiguate
7416** functions that can take varying numbers of arguments. The
7417** P4 argument is only needed for the case where
7418** the step function was not previously called.
7419*/
7420case OP_AggValue:
drh9cbf3422008-01-17 16:22:13 +00007421case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00007422 Mem *pMem;
drh9f6168b2016-03-19 23:32:58 +00007423 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh8f26da62018-07-05 21:22:57 +00007424 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
drha6c2ed92009-11-14 23:22:23 +00007425 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00007426 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
dan67a9b8e2018-06-22 20:51:35 +00007427#ifndef SQLITE_OMIT_WINDOWFUNC
dan86fb6e12018-05-16 20:58:07 +00007428 if( pOp->p3 ){
dan108e6b22019-03-18 18:55:35 +00007429 memAboutToChange(p, &aMem[pOp->p3]);
dan86fb6e12018-05-16 20:58:07 +00007430 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
dan660af932018-06-18 16:55:22 +00007431 pMem = &aMem[pOp->p3];
dan67a9b8e2018-06-22 20:51:35 +00007432 }else
7433#endif
drh8f26da62018-07-05 21:22:57 +00007434 {
7435 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
7436 }
dan67a9b8e2018-06-22 20:51:35 +00007437
drh4c8555f2009-06-25 01:47:11 +00007438 if( rc ){
drh22c17b82015-05-15 04:13:15 +00007439 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
drh9467abf2016-02-17 18:44:11 +00007440 goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00007441 }
drh2dca8682008-03-21 17:13:13 +00007442 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00007443 UPDATE_MAX_BLOBSIZE(pMem);
drh5e00f6c2001-09-13 13:46:56 +00007444 break;
7445}
7446
dan5cf53532010-05-01 16:40:20 +00007447#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00007448/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00007449**
7450** Checkpoint database P1. This is a no-op if P1 is not currently in
drha25165f2014-12-04 04:50:59 +00007451** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
7452** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
drh30aa3b92011-02-07 23:56:01 +00007453** SQLITE_BUSY or not, respectively. Write the number of pages in the
7454** WAL after the checkpoint into mem[P3+1] and the number of pages
7455** in the WAL that have been checkpointed after the checkpoint
7456** completes into mem[P3+2]. However on an error, mem[P3+1] and
7457** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00007458*/
7459case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00007460 int i; /* Loop counter */
7461 int aRes[3]; /* Results */
7462 Mem *pMem; /* Write results here */
7463
drh9e92a472013-06-27 17:40:30 +00007464 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00007465 aRes[0] = 0;
7466 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00007467 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
7468 || pOp->p2==SQLITE_CHECKPOINT_FULL
7469 || pOp->p2==SQLITE_CHECKPOINT_RESTART
danf26a1542014-12-02 19:04:54 +00007470 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
dancdc1f042010-11-18 12:11:05 +00007471 );
drh30aa3b92011-02-07 23:56:01 +00007472 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
drh9467abf2016-02-17 18:44:11 +00007473 if( rc ){
7474 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
dancdc1f042010-11-18 12:11:05 +00007475 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00007476 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00007477 }
drh30aa3b92011-02-07 23:56:01 +00007478 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
7479 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
7480 }
dan7c246102010-04-12 19:00:29 +00007481 break;
7482};
dan5cf53532010-05-01 16:40:20 +00007483#endif
drh5e00f6c2001-09-13 13:46:56 +00007484
drhcac29a62010-07-02 19:36:52 +00007485#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00007486/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00007487**
7488** Change the journal mode of database P1 to P3. P3 must be one of the
7489** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
7490** modes (delete, truncate, persist, off and memory), this is a simple
7491** operation. No IO is required.
7492**
7493** If changing into or out of WAL mode the procedure is more complicated.
7494**
7495** Write a string containing the final journal-mode to register P2.
7496*/
drh27a348c2015-04-13 19:14:06 +00007497case OP_JournalMode: { /* out2 */
dane04dc882010-04-20 18:53:15 +00007498 Btree *pBt; /* Btree to change journal mode of */
7499 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00007500 int eNew; /* New journal mode */
7501 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00007502#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00007503 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00007504#endif
dane04dc882010-04-20 18:53:15 +00007505
drh27a348c2015-04-13 19:14:06 +00007506 pOut = out2Prerelease(p, pOp);
drhd80b2332010-05-01 00:59:37 +00007507 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00007508 assert( eNew==PAGER_JOURNALMODE_DELETE
7509 || eNew==PAGER_JOURNALMODE_TRUNCATE
7510 || eNew==PAGER_JOURNALMODE_PERSIST
7511 || eNew==PAGER_JOURNALMODE_OFF
7512 || eNew==PAGER_JOURNALMODE_MEMORY
7513 || eNew==PAGER_JOURNALMODE_WAL
7514 || eNew==PAGER_JOURNALMODE_QUERY
7515 );
7516 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00007517 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00007518
dane04dc882010-04-20 18:53:15 +00007519 pBt = db->aDb[pOp->p1].pBt;
7520 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00007521 eOld = sqlite3PagerGetJournalMode(pPager);
7522 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
dana8f249f2021-05-20 11:42:51 +00007523 assert( sqlite3BtreeHoldsMutex(pBt) );
drh0b9b4302010-06-11 17:01:24 +00007524 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00007525
7526#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00007527 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00007528
drhd80b2332010-05-01 00:59:37 +00007529 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00007530 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00007531 */
7532 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00007533 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00007534 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00007535 ){
drh0b9b4302010-06-11 17:01:24 +00007536 eNew = eOld;
dane180c292010-04-26 17:42:56 +00007537 }
7538
drh0b9b4302010-06-11 17:01:24 +00007539 if( (eNew!=eOld)
7540 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
7541 ){
danc0537fe2013-06-28 19:41:43 +00007542 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00007543 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00007544 sqlite3VdbeError(p,
drh0b9b4302010-06-11 17:01:24 +00007545 "cannot change %s wal mode from within a transaction",
7546 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
7547 );
drh9467abf2016-02-17 18:44:11 +00007548 goto abort_due_to_error;
drh0b9b4302010-06-11 17:01:24 +00007549 }else{
7550
7551 if( eOld==PAGER_JOURNALMODE_WAL ){
7552 /* If leaving WAL mode, close the log file. If successful, the call
7553 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
7554 ** file. An EXCLUSIVE lock may still be held on the database file
7555 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00007556 */
dan7fb89902016-08-12 16:21:15 +00007557 rc = sqlite3PagerCloseWal(pPager, db);
drhab9b7442010-05-10 11:20:05 +00007558 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00007559 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00007560 }
drh242c4f72010-06-22 14:49:39 +00007561 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
7562 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
7563 ** as an intermediate */
7564 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00007565 }
7566
7567 /* Open a transaction on the database file. Regardless of the journal
7568 ** mode, this transaction always uses a rollback journal.
7569 */
drh99744fa2020-08-25 19:09:07 +00007570 assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE );
drh0b9b4302010-06-11 17:01:24 +00007571 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00007572 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00007573 }
7574 }
7575 }
dan5cf53532010-05-01 16:40:20 +00007576#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00007577
drh9467abf2016-02-17 18:44:11 +00007578 if( rc ) eNew = eOld;
drh0b9b4302010-06-11 17:01:24 +00007579 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00007580
dane04dc882010-04-20 18:53:15 +00007581 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00007582 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00007583 pOut->n = sqlite3Strlen30(pOut->z);
7584 pOut->enc = SQLITE_UTF8;
7585 sqlite3VdbeChangeEncoding(pOut, encoding);
drh9467abf2016-02-17 18:44:11 +00007586 if( rc ) goto abort_due_to_error;
dane04dc882010-04-20 18:53:15 +00007587 break;
drhcac29a62010-07-02 19:36:52 +00007588};
7589#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00007590
drhfdbcdee2007-03-27 14:44:50 +00007591#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh2f6239e2018-12-08 00:43:08 +00007592/* Opcode: Vacuum P1 P2 * * *
drh6f8c91c2003-12-07 00:24:35 +00007593**
drh9ef5e772016-08-19 14:20:56 +00007594** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
7595** for an attached database. The "temp" database may not be vacuumed.
drhb0b7db92018-12-07 17:28:28 +00007596**
drh2f6239e2018-12-08 00:43:08 +00007597** If P2 is not zero, then it is a register holding a string which is
7598** the file into which the result of vacuum should be written. When
7599** P2 is zero, the vacuum overwrites the original database.
drh6f8c91c2003-12-07 00:24:35 +00007600*/
drh9cbf3422008-01-17 16:22:13 +00007601case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00007602 assert( p->readOnly==0 );
drh2f6239e2018-12-08 00:43:08 +00007603 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
7604 pOp->p2 ? &aMem[pOp->p2] : 0);
drh9467abf2016-02-17 18:44:11 +00007605 if( rc ) goto abort_due_to_error;
drh6f8c91c2003-12-07 00:24:35 +00007606 break;
7607}
drh154d4b22006-09-21 11:02:16 +00007608#endif
drh6f8c91c2003-12-07 00:24:35 +00007609
danielk1977dddbcdc2007-04-26 14:42:34 +00007610#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00007611/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00007612**
7613** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00007614** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00007615** P2. Otherwise, fall through to the next instruction.
7616*/
drh9cbf3422008-01-17 16:22:13 +00007617case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00007618 Btree *pBt;
7619
7620 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00007621 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00007622 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00007623 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00007624 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00007625 VdbeBranchTaken(rc==SQLITE_DONE,2);
drh9467abf2016-02-17 18:44:11 +00007626 if( rc ){
7627 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
danielk1977dddbcdc2007-04-26 14:42:34 +00007628 rc = SQLITE_OK;
drhf56fa462015-04-13 21:39:54 +00007629 goto jump_to_p2;
danielk1977dddbcdc2007-04-26 14:42:34 +00007630 }
7631 break;
7632}
7633#endif
7634
drhba968db2018-07-24 22:02:12 +00007635/* Opcode: Expire P1 P2 * * *
danielk1977a21c6b62005-01-24 10:25:59 +00007636**
drh25df48d2014-07-22 14:58:12 +00007637** Cause precompiled statements to expire. When an expired statement
7638** is executed using sqlite3_step() it will either automatically
7639** reprepare itself (if it was originally created using sqlite3_prepare_v2())
7640** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00007641**
7642** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00007643** then only the currently executing statement is expired.
drhba968db2018-07-24 22:02:12 +00007644**
7645** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
7646** then running SQL statements are allowed to continue to run to completion.
7647** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
7648** that might help the statement run faster but which does not affect the
7649** correctness of operation.
danielk1977a21c6b62005-01-24 10:25:59 +00007650*/
drh9cbf3422008-01-17 16:22:13 +00007651case OP_Expire: {
drhba968db2018-07-24 22:02:12 +00007652 assert( pOp->p2==0 || pOp->p2==1 );
danielk1977a21c6b62005-01-24 10:25:59 +00007653 if( !pOp->p1 ){
drhba968db2018-07-24 22:02:12 +00007654 sqlite3ExpirePreparedStatements(db, pOp->p2);
danielk1977a21c6b62005-01-24 10:25:59 +00007655 }else{
drhba968db2018-07-24 22:02:12 +00007656 p->expired = pOp->p2+1;
danielk1977a21c6b62005-01-24 10:25:59 +00007657 }
7658 break;
7659}
7660
drh7b14b652019-12-29 22:08:20 +00007661/* Opcode: CursorLock P1 * * * *
7662**
7663** Lock the btree to which cursor P1 is pointing so that the btree cannot be
7664** written by an other cursor.
7665*/
7666case OP_CursorLock: {
7667 VdbeCursor *pC;
7668 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7669 pC = p->apCsr[pOp->p1];
7670 assert( pC!=0 );
7671 assert( pC->eCurType==CURTYPE_BTREE );
7672 sqlite3BtreeCursorPin(pC->uc.pCursor);
7673 break;
7674}
7675
7676/* Opcode: CursorUnlock P1 * * * *
7677**
7678** Unlock the btree to which cursor P1 is pointing so that it can be
7679** written by other cursors.
7680*/
7681case OP_CursorUnlock: {
7682 VdbeCursor *pC;
7683 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7684 pC = p->apCsr[pOp->p1];
7685 assert( pC!=0 );
7686 assert( pC->eCurType==CURTYPE_BTREE );
7687 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
7688 break;
7689}
7690
danielk1977c00da102006-01-07 13:21:04 +00007691#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00007692/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00007693** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00007694**
7695** Obtain a lock on a particular table. This instruction is only used when
7696** the shared-cache feature is enabled.
7697**
danielk197796d48e92009-06-29 06:00:37 +00007698** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00007699** on which the lock is acquired. A readlock is obtained if P3==0 or
7700** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00007701**
7702** P2 contains the root-page of the table to lock.
7703**
drh66a51672008-01-03 00:01:23 +00007704** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00007705** used to generate an error message if the lock cannot be obtained.
7706*/
drh9cbf3422008-01-17 16:22:13 +00007707case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00007708 u8 isWriteLock = (u8)pOp->p3;
drh169dd922017-06-26 13:57:49 +00007709 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
danielk1977e0d9e6f2009-07-03 16:25:06 +00007710 int p1 = pOp->p1;
7711 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00007712 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00007713 assert( isWriteLock==0 || isWriteLock==1 );
7714 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
drh9467abf2016-02-17 18:44:11 +00007715 if( rc ){
7716 if( (rc&0xFF)==SQLITE_LOCKED ){
7717 const char *z = pOp->p4.z;
7718 sqlite3VdbeError(p, "database table is locked: %s", z);
7719 }
7720 goto abort_due_to_error;
danielk1977e0d9e6f2009-07-03 16:25:06 +00007721 }
danielk1977c00da102006-01-07 13:21:04 +00007722 }
7723 break;
7724}
drhb9bb7c12006-06-11 23:41:55 +00007725#endif /* SQLITE_OMIT_SHARED_CACHE */
7726
7727#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007728/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00007729**
danielk19773e3a84d2008-08-01 17:37:40 +00007730** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
7731** xBegin method for that table.
7732**
7733** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00007734** within a callback to a virtual table xSync() method. If it is, the error
7735** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00007736*/
drh9cbf3422008-01-17 16:22:13 +00007737case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00007738 VTable *pVTab;
7739 pVTab = pOp->p4.pVtab;
7740 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00007741 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
drh9467abf2016-02-17 18:44:11 +00007742 if( rc ) goto abort_due_to_error;
danielk1977f9e7dda2006-06-16 16:08:53 +00007743 break;
7744}
7745#endif /* SQLITE_OMIT_VIRTUALTABLE */
7746
7747#ifndef SQLITE_OMIT_VIRTUALTABLE
dan73779452015-03-19 18:56:17 +00007748/* Opcode: VCreate P1 P2 * * *
danielk1977f9e7dda2006-06-16 16:08:53 +00007749**
dan73779452015-03-19 18:56:17 +00007750** P2 is a register that holds the name of a virtual table in database
7751** P1. Call the xCreate method for that table.
danielk1977f9e7dda2006-06-16 16:08:53 +00007752*/
drh9cbf3422008-01-17 16:22:13 +00007753case OP_VCreate: {
dan73779452015-03-19 18:56:17 +00007754 Mem sMem; /* For storing the record being decoded */
drh47464062015-03-21 12:22:16 +00007755 const char *zTab; /* Name of the virtual table */
7756
dan73779452015-03-19 18:56:17 +00007757 memset(&sMem, 0, sizeof(sMem));
7758 sMem.db = db;
drh47464062015-03-21 12:22:16 +00007759 /* Because P2 is always a static string, it is impossible for the
7760 ** sqlite3VdbeMemCopy() to fail */
7761 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
7762 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
dan73779452015-03-19 18:56:17 +00007763 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
drh47464062015-03-21 12:22:16 +00007764 assert( rc==SQLITE_OK );
7765 zTab = (const char*)sqlite3_value_text(&sMem);
7766 assert( zTab || db->mallocFailed );
7767 if( zTab ){
7768 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
dan73779452015-03-19 18:56:17 +00007769 }
7770 sqlite3VdbeMemRelease(&sMem);
drh9467abf2016-02-17 18:44:11 +00007771 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00007772 break;
7773}
7774#endif /* SQLITE_OMIT_VIRTUALTABLE */
7775
7776#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007777/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00007778**
drh66a51672008-01-03 00:01:23 +00007779** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00007780** of that table.
drhb9bb7c12006-06-11 23:41:55 +00007781*/
drh9cbf3422008-01-17 16:22:13 +00007782case OP_VDestroy: {
drh086723a2015-03-24 12:51:52 +00007783 db->nVDestroy++;
danielk19772dca4ac2008-01-03 11:50:29 +00007784 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
drh086723a2015-03-24 12:51:52 +00007785 db->nVDestroy--;
dan1d4b1642018-12-28 17:45:08 +00007786 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00007787 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00007788 break;
7789}
7790#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00007791
drh9eff6162006-06-12 21:59:13 +00007792#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007793/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00007794**
drh66a51672008-01-03 00:01:23 +00007795** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00007796** P1 is a cursor number. This opcode opens a cursor to the virtual
7797** table and stores that cursor in P1.
7798*/
drh9cbf3422008-01-17 16:22:13 +00007799case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00007800 VdbeCursor *pCur;
drhc960dcb2015-11-20 19:22:01 +00007801 sqlite3_vtab_cursor *pVCur;
drh856c1032009-06-02 15:21:42 +00007802 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00007803 const sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007804
drh1713afb2013-06-28 01:24:57 +00007805 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00007806 pCur = 0;
drhc960dcb2015-11-20 19:22:01 +00007807 pVCur = 0;
danielk1977595a5232009-07-24 17:58:53 +00007808 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00007809 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
7810 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00007811 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00007812 }
7813 pModule = pVtab->pModule;
drhc960dcb2015-11-20 19:22:01 +00007814 rc = pModule->xOpen(pVtab, &pVCur);
dan016f7812013-08-21 17:35:48 +00007815 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007816 if( rc ) goto abort_due_to_error;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007817
drh9467abf2016-02-17 18:44:11 +00007818 /* Initialize sqlite3_vtab_cursor base class */
7819 pVCur->pVtab = pVtab;
7820
7821 /* Initialize vdbe cursor object */
drhb2486682022-01-03 01:43:28 +00007822 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB);
drh9467abf2016-02-17 18:44:11 +00007823 if( pCur ){
7824 pCur->uc.pVCur = pVCur;
7825 pVtab->nRef++;
7826 }else{
7827 assert( db->mallocFailed );
7828 pModule->xClose(pVCur);
7829 goto no_mem;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007830 }
drh9eff6162006-06-12 21:59:13 +00007831 break;
7832}
7833#endif /* SQLITE_OMIT_VIRTUALTABLE */
7834
7835#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fe7e7d2022-02-01 14:58:29 +00007836/* Opcode: VInitIn P1 P2 P3 * *
drh30e314e2022-02-02 14:36:58 +00007837** Synopsis: r[P2]=ValueList(P1,P3)
drh0fe7e7d2022-02-01 14:58:29 +00007838**
drh30e314e2022-02-02 14:36:58 +00007839** Set register P2 to be a pointer to a ValueList object for cursor P1
7840** with cache register P3 and output register P3+1. This ValueList object
7841** can be used as the first argument to sqlite3_vtab_in_first() and
7842** sqlite3_vtab_in_next() to extract all of the values stored in the P1
7843** cursor. Register P3 is used to hold the values returned by
7844** sqlite3_vtab_in_first() and sqlite3_vtab_in_next().
drh0fe7e7d2022-02-01 14:58:29 +00007845*/
7846case OP_VInitIn: { /* out2 */
drh30e314e2022-02-02 14:36:58 +00007847 VdbeCursor *pC; /* The cursor containing the RHS values */
7848 ValueList *pRhs; /* New ValueList object to put in reg[P2] */
7849
drh0fe7e7d2022-02-01 14:58:29 +00007850 pC = p->apCsr[pOp->p1];
drh30e314e2022-02-02 14:36:58 +00007851 pRhs = sqlite3_malloc64( sizeof(*pRhs) );
7852 if( pRhs==0 ) goto no_mem;
7853 pRhs->pCsr = pC->uc.pCursor;
7854 pRhs->pOut = &aMem[pOp->p3];
drh0fe7e7d2022-02-01 14:58:29 +00007855 pOut = out2Prerelease(p, pOp);
drh0fe7e7d2022-02-01 14:58:29 +00007856 pOut->flags = MEM_Null;
drh30e314e2022-02-02 14:36:58 +00007857 sqlite3VdbeMemSetPointer(pOut, pRhs, "ValueList", sqlite3_free);
drh0fe7e7d2022-02-01 14:58:29 +00007858 break;
7859}
7860#endif /* SQLITE_OMIT_VIRTUALTABLE */
7861
7862
7863#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00007864/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00007865** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00007866**
7867** P1 is a cursor opened using VOpen. P2 is an address to jump to if
7868** the filtered result set is empty.
7869**
drh66a51672008-01-03 00:01:23 +00007870** P4 is either NULL or a string that was generated by the xBestIndex
7871** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00007872** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00007873**
drh9eff6162006-06-12 21:59:13 +00007874** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00007875** by P1. The integer query plan parameter to xFilter is stored in register
7876** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00007877** xFilter method. Registers P3+2..P3+1+argc are the argc
7878** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00007879** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00007880**
danielk19776dbee812008-01-03 18:39:41 +00007881** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00007882*/
drh9cbf3422008-01-17 16:22:13 +00007883case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00007884 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00007885 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007886 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00007887 Mem *pQuery;
7888 Mem *pArgc;
drhc960dcb2015-11-20 19:22:01 +00007889 sqlite3_vtab_cursor *pVCur;
drh4dc754d2008-07-23 18:17:32 +00007890 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00007891 VdbeCursor *pCur;
7892 int res;
7893 int i;
7894 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007895
drha6c2ed92009-11-14 23:22:23 +00007896 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00007897 pArgc = &pQuery[1];
7898 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00007899 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00007900 REGISTER_TRACE(pOp->p3, pQuery);
drh3ab4ffc2021-11-11 11:23:08 +00007901 assert( pCur!=0 );
drhc960dcb2015-11-20 19:22:01 +00007902 assert( pCur->eCurType==CURTYPE_VTAB );
7903 pVCur = pCur->uc.pVCur;
7904 pVtab = pVCur->pVtab;
drh4dc754d2008-07-23 18:17:32 +00007905 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007906
drh9cbf3422008-01-17 16:22:13 +00007907 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00007908 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00007909 nArg = (int)pArgc->u.i;
7910 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007911
drh644a5292006-12-20 14:53:38 +00007912 /* Invoke the xFilter method */
drhf56fa462015-04-13 21:39:54 +00007913 apArg = p->apArg;
7914 for(i = 0; i<nArg; i++){
7915 apArg[i] = &pArgc[i+1];
7916 }
drhc960dcb2015-11-20 19:22:01 +00007917 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
drhf56fa462015-04-13 21:39:54 +00007918 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007919 if( rc ) goto abort_due_to_error;
7920 res = pModule->xEof(pVCur);
drh1d454a32008-01-31 19:34:51 +00007921 pCur->nullRow = 0;
drhf56fa462015-04-13 21:39:54 +00007922 VdbeBranchTaken(res!=0,2);
7923 if( res ) goto jump_to_p2;
drh9eff6162006-06-12 21:59:13 +00007924 break;
7925}
7926#endif /* SQLITE_OMIT_VIRTUALTABLE */
7927
7928#ifndef SQLITE_OMIT_VIRTUALTABLE
drhce2fbd12018-01-12 21:00:14 +00007929/* Opcode: VColumn P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00007930** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00007931**
drh6f390be2018-01-11 17:04:26 +00007932** Store in register P3 the value of the P2-th column of
7933** the current row of the virtual-table of cursor P1.
7934**
7935** If the VColumn opcode is being used to fetch the value of
drhce2fbd12018-01-12 21:00:14 +00007936** an unchanging column during an UPDATE operation, then the P5
drh09d00b22018-09-27 20:20:01 +00007937** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
7938** function to return true inside the xColumn method of the virtual
7939** table implementation. The P5 column might also contain other
7940** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
7941** unused by OP_VColumn.
drh9eff6162006-06-12 21:59:13 +00007942*/
7943case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00007944 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007945 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00007946 Mem *pDest;
7947 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007948
drhdfe88ec2008-11-03 20:55:06 +00007949 VdbeCursor *pCur = p->apCsr[pOp->p1];
drh3ab4ffc2021-11-11 11:23:08 +00007950 assert( pCur!=0 );
drhc960dcb2015-11-20 19:22:01 +00007951 assert( pCur->eCurType==CURTYPE_VTAB );
drh9f6168b2016-03-19 23:32:58 +00007952 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00007953 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00007954 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00007955 if( pCur->nullRow ){
7956 sqlite3VdbeMemSetNull(pDest);
7957 break;
7958 }
drhc960dcb2015-11-20 19:22:01 +00007959 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00007960 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00007961 assert( pModule->xColumn );
7962 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00007963 sContext.pOut = pDest;
drh659fdb42022-04-01 15:31:58 +00007964 sContext.enc = encoding;
drh75f10762019-12-14 18:08:22 +00007965 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
drh09d00b22018-09-27 20:20:01 +00007966 if( pOp->p5 & OPFLAG_NOCHNG ){
drhce2fbd12018-01-12 21:00:14 +00007967 sqlite3VdbeMemSetNull(pDest);
7968 pDest->flags = MEM_Null|MEM_Zero;
7969 pDest->u.nZero = 0;
7970 }else{
7971 MemSetTypeFlag(pDest, MEM_Null);
7972 }
drhc960dcb2015-11-20 19:22:01 +00007973 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00007974 sqlite3VtabImportErrmsg(p, pVtab);
drhf09ac0b2018-01-23 03:44:06 +00007975 if( sContext.isError>0 ){
dan099fa842018-01-30 18:33:23 +00007976 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
drh4c8555f2009-06-25 01:47:11 +00007977 rc = sContext.isError;
7978 }
drh9bd038f2014-08-27 14:14:06 +00007979 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00007980 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00007981 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00007982
drh9467abf2016-02-17 18:44:11 +00007983 if( rc ) goto abort_due_to_error;
drh9eff6162006-06-12 21:59:13 +00007984 break;
7985}
7986#endif /* SQLITE_OMIT_VIRTUALTABLE */
7987
7988#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007989/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00007990**
7991** Advance virtual table P1 to the next row in its result set and
7992** jump to instruction P2. Or, if the virtual table has reached
7993** the end of its result set, then fall through to the next instruction.
7994*/
drh9cbf3422008-01-17 16:22:13 +00007995case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00007996 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007997 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00007998 int res;
drh856c1032009-06-02 15:21:42 +00007999 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00008000
drh856c1032009-06-02 15:21:42 +00008001 pCur = p->apCsr[pOp->p1];
drh3ab4ffc2021-11-11 11:23:08 +00008002 assert( pCur!=0 );
drhc960dcb2015-11-20 19:22:01 +00008003 assert( pCur->eCurType==CURTYPE_VTAB );
drh2945b4a2008-01-31 15:53:45 +00008004 if( pCur->nullRow ){
8005 break;
8006 }
drhc960dcb2015-11-20 19:22:01 +00008007 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00008008 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00008009 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00008010
drhde4fcfd2008-01-19 23:50:26 +00008011 /* Invoke the xNext() method of the module. There is no way for the
8012 ** underlying implementation to return an error if one occurs during
8013 ** xNext(). Instead, if an error occurs, true is returned (indicating that
8014 ** data is available) and the error code returned when xColumn or
8015 ** some other method is next invoked on the save virtual table cursor.
8016 */
drhc960dcb2015-11-20 19:22:01 +00008017 rc = pModule->xNext(pCur->uc.pVCur);
dan016f7812013-08-21 17:35:48 +00008018 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00008019 if( rc ) goto abort_due_to_error;
8020 res = pModule->xEof(pCur->uc.pVCur);
drh688852a2014-02-17 22:40:43 +00008021 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00008022 if( !res ){
8023 /* If there is data, jump to P2 */
drhf56fa462015-04-13 21:39:54 +00008024 goto jump_to_p2_and_check_for_interrupt;
drhde4fcfd2008-01-19 23:50:26 +00008025 }
drh49afe3a2013-07-10 03:05:14 +00008026 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00008027}
8028#endif /* SQLITE_OMIT_VIRTUALTABLE */
8029
danielk1977182c4ba2007-06-27 15:53:34 +00008030#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00008031/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00008032**
drh66a51672008-01-03 00:01:23 +00008033** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00008034** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00008035** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00008036*/
drh9cbf3422008-01-17 16:22:13 +00008037case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00008038 sqlite3_vtab *pVtab;
8039 Mem *pName;
dan34566c42018-09-20 17:21:21 +00008040 int isLegacy;
8041
8042 isLegacy = (db->flags & SQLITE_LegacyAlter);
8043 db->flags |= SQLITE_LegacyAlter;
danielk1977595a5232009-07-24 17:58:53 +00008044 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00008045 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00008046 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00008047 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00008048 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00008049 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00008050 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00008051 testcase( pName->enc==SQLITE_UTF8 );
8052 testcase( pName->enc==SQLITE_UTF16BE );
8053 testcase( pName->enc==SQLITE_UTF16LE );
8054 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
drh9467abf2016-02-17 18:44:11 +00008055 if( rc ) goto abort_due_to_error;
8056 rc = pVtab->pModule->xRename(pVtab, pName->z);
drhd5b44d62018-12-06 17:06:02 +00008057 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
drh9467abf2016-02-17 18:44:11 +00008058 sqlite3VtabImportErrmsg(p, pVtab);
8059 p->expired = 0;
8060 if( rc ) goto abort_due_to_error;
danielk1977182c4ba2007-06-27 15:53:34 +00008061 break;
8062}
8063#endif
drh4cbdda92006-06-14 19:00:20 +00008064
8065#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00008066/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00008067** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00008068**
drh66a51672008-01-03 00:01:23 +00008069** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00008070** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00008071** are contiguous memory cells starting at P3 to pass to the xUpdate
8072** invocation. The value in register (P3+P2-1) corresponds to the
8073** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00008074**
8075** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00008076** The argv[0] element (which corresponds to memory cell P3)
8077** is the rowid of a row to delete. If argv[0] is NULL then no
8078** deletion occurs. The argv[1] element is the rowid of the new
8079** row. This can be NULL to have the virtual table select the new
8080** rowid for itself. The subsequent elements in the array are
8081** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00008082**
8083** If P2==1 then no insert is performed. argv[0] is the rowid of
8084** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00008085**
8086** P1 is a boolean flag. If it is set to true and the xUpdate call
8087** is successful, then the value returned by sqlite3_last_insert_rowid()
8088** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00008089**
8090** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
8091** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00008092*/
drh9cbf3422008-01-17 16:22:13 +00008093case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00008094 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00008095 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00008096 int nArg;
8097 int i;
drh9135ebb2021-11-11 23:52:44 +00008098 sqlite_int64 rowid = 0;
drh856c1032009-06-02 15:21:42 +00008099 Mem **apArg;
8100 Mem *pX;
8101
danb061d052011-04-25 18:49:57 +00008102 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
8103 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
8104 );
drh9e92a472013-06-27 17:40:30 +00008105 assert( p->readOnly==0 );
dan466ea9b2018-06-13 11:11:13 +00008106 if( db->mallocFailed ) goto no_mem;
drh4031baf2018-05-28 17:31:20 +00008107 sqlite3VdbeIncrWriteCounter(p, 0);
danielk1977595a5232009-07-24 17:58:53 +00008108 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00008109 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
8110 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00008111 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00008112 }
8113 pModule = pVtab->pModule;
drh856c1032009-06-02 15:21:42 +00008114 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00008115 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00008116 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00008117 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00008118 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00008119 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00008120 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00008121 assert( memIsValid(pX) );
8122 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00008123 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00008124 pX++;
danielk1977399918f2006-06-14 13:03:23 +00008125 }
danb061d052011-04-25 18:49:57 +00008126 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00008127 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00008128 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00008129 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00008130 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00008131 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drhfae58d52017-01-26 17:26:44 +00008132 db->lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00008133 }
drhd91c1a12013-02-09 13:58:25 +00008134 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00008135 if( pOp->p5==OE_Ignore ){
8136 rc = SQLITE_OK;
8137 }else{
8138 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
8139 }
8140 }else{
8141 p->nChange++;
8142 }
drh9467abf2016-02-17 18:44:11 +00008143 if( rc ) goto abort_due_to_error;
danielk1977399918f2006-06-14 13:03:23 +00008144 }
drh4cbdda92006-06-14 19:00:20 +00008145 break;
danielk1977399918f2006-06-14 13:03:23 +00008146}
8147#endif /* SQLITE_OMIT_VIRTUALTABLE */
8148
danielk197759a93792008-05-15 17:48:20 +00008149#ifndef SQLITE_OMIT_PAGER_PRAGMAS
8150/* Opcode: Pagecount P1 P2 * * *
8151**
8152** Write the current number of pages in database P1 to memory cell P2.
8153*/
drh27a348c2015-04-13 19:14:06 +00008154case OP_Pagecount: { /* out2 */
8155 pOut = out2Prerelease(p, pOp);
drhb1299152010-03-30 22:58:33 +00008156 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00008157 break;
8158}
8159#endif
8160
drh60ac3f42010-11-23 18:59:27 +00008161
8162#ifndef SQLITE_OMIT_PAGER_PRAGMAS
8163/* Opcode: MaxPgcnt P1 P2 P3 * *
8164**
8165** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00008166** Do not let the maximum page count fall below the current page count and
8167** do not change the maximum page count value if P3==0.
8168**
drh60ac3f42010-11-23 18:59:27 +00008169** Store the maximum page count after the change in register P2.
8170*/
drh27a348c2015-04-13 19:14:06 +00008171case OP_MaxPgcnt: { /* out2 */
drhc84e0332010-11-23 20:25:08 +00008172 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00008173 Btree *pBt;
8174
drh27a348c2015-04-13 19:14:06 +00008175 pOut = out2Prerelease(p, pOp);
drh60ac3f42010-11-23 18:59:27 +00008176 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00008177 newMax = 0;
8178 if( pOp->p3 ){
8179 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00008180 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00008181 }
8182 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00008183 break;
8184}
8185#endif
8186
drh920cf592019-10-30 16:29:02 +00008187/* Opcode: Function P1 P2 P3 P4 *
drhd7b10d72020-02-01 17:38:24 +00008188** Synopsis: r[P3]=func(r[P2@NP])
drh3e34eab2017-07-19 19:48:40 +00008189**
8190** Invoke a user function (P4 is a pointer to an sqlite3_context object that
drh920cf592019-10-30 16:29:02 +00008191** contains a pointer to the function to be run) with arguments taken
8192** from register P2 and successors. The number of arguments is in
8193** the sqlite3_context object that P4 points to.
8194** The result of the function is stored
drh3e34eab2017-07-19 19:48:40 +00008195** in register P3. Register P3 must not be one of the function inputs.
8196**
8197** P1 is a 32-bit bitmask indicating whether or not each argument to the
8198** function was determined to be constant at compile time. If the first
8199** argument was constant then bit 0 of P1 is set. This is used to determine
8200** whether meta data associated with a user function argument using the
8201** sqlite3_set_auxdata() API may be safely retained until the next
8202** invocation of this opcode.
8203**
drh920cf592019-10-30 16:29:02 +00008204** See also: AggStep, AggFinal, PureFunc
drh3e34eab2017-07-19 19:48:40 +00008205*/
drh920cf592019-10-30 16:29:02 +00008206/* Opcode: PureFunc P1 P2 P3 P4 *
drhd7b10d72020-02-01 17:38:24 +00008207** Synopsis: r[P3]=func(r[P2@NP])
drh920cf592019-10-30 16:29:02 +00008208**
8209** Invoke a user function (P4 is a pointer to an sqlite3_context object that
8210** contains a pointer to the function to be run) with arguments taken
8211** from register P2 and successors. The number of arguments is in
8212** the sqlite3_context object that P4 points to.
8213** The result of the function is stored
8214** in register P3. Register P3 must not be one of the function inputs.
8215**
8216** P1 is a 32-bit bitmask indicating whether or not each argument to the
8217** function was determined to be constant at compile time. If the first
8218** argument was constant then bit 0 of P1 is set. This is used to determine
8219** whether meta data associated with a user function argument using the
8220** sqlite3_set_auxdata() API may be safely retained until the next
8221** invocation of this opcode.
8222**
8223** This opcode works exactly like OP_Function. The only difference is in
8224** its name. This opcode is used in places where the function must be
8225** purely non-deterministic. Some built-in date/time functions can be
8226** either determinitic of non-deterministic, depending on their arguments.
8227** When those function are used in a non-deterministic way, they will check
8228** to see if they were called using OP_PureFunc instead of OP_Function, and
8229** if they were, they throw an error.
8230**
8231** See also: AggStep, AggFinal, Function
8232*/
mistachkin758784d2018-07-25 15:12:29 +00008233case OP_PureFunc: /* group */
8234case OP_Function: { /* group */
drh3e34eab2017-07-19 19:48:40 +00008235 int i;
8236 sqlite3_context *pCtx;
8237
8238 assert( pOp->p4type==P4_FUNCCTX );
8239 pCtx = pOp->p4.pCtx;
8240
8241 /* If this function is inside of a trigger, the register array in aMem[]
8242 ** might change from one evaluation to the next. The next block of code
8243 ** checks to see if the register array has changed, and if so it
8244 ** reinitializes the relavant parts of the sqlite3_context object */
8245 pOut = &aMem[pOp->p3];
8246 if( pCtx->pOut != pOut ){
drh920cf592019-10-30 16:29:02 +00008247 pCtx->pVdbe = p;
drh3e34eab2017-07-19 19:48:40 +00008248 pCtx->pOut = pOut;
drh659fdb42022-04-01 15:31:58 +00008249 pCtx->enc = encoding;
drh3e34eab2017-07-19 19:48:40 +00008250 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
8251 }
drh920cf592019-10-30 16:29:02 +00008252 assert( pCtx->pVdbe==p );
drh3e34eab2017-07-19 19:48:40 +00008253
8254 memAboutToChange(p, pOut);
8255#ifdef SQLITE_DEBUG
8256 for(i=0; i<pCtx->argc; i++){
8257 assert( memIsValid(pCtx->argv[i]) );
8258 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
8259 }
8260#endif
8261 MemSetTypeFlag(pOut, MEM_Null);
drhf09ac0b2018-01-23 03:44:06 +00008262 assert( pCtx->isError==0 );
drh3e34eab2017-07-19 19:48:40 +00008263 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
8264
8265 /* If the function returned an error, throw an exception */
drhf09ac0b2018-01-23 03:44:06 +00008266 if( pCtx->isError ){
8267 if( pCtx->isError>0 ){
drh3e34eab2017-07-19 19:48:40 +00008268 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
8269 rc = pCtx->isError;
8270 }
8271 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
drhf09ac0b2018-01-23 03:44:06 +00008272 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00008273 if( rc ) goto abort_due_to_error;
8274 }
8275
drhfb92e072022-03-29 01:43:09 +00008276 assert( (pOut->flags&MEM_Str)==0
8277 || pOut->enc==encoding
8278 || db->mallocFailed );
8279 assert( !sqlite3VdbeMemTooBig(pOut) );
drh3e34eab2017-07-19 19:48:40 +00008280
8281 REGISTER_TRACE(pOp->p3, pOut);
8282 UPDATE_MAX_BLOBSIZE(pOut);
8283 break;
8284}
8285
drh2db144c2021-12-01 16:31:02 +00008286/* Opcode: FilterAdd P1 * P3 P4 *
8287** Synopsis: filter(P1) += key(P3@P4)
8288**
8289** Compute a hash on the P4 registers starting with r[P3] and
8290** add that hash to the bloom filter contained in r[P1].
8291*/
8292case OP_FilterAdd: {
drh5baaf402021-12-06 13:07:28 +00008293 u64 h;
drh2db144c2021-12-01 16:31:02 +00008294
8295 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
8296 pIn1 = &aMem[pOp->p1];
8297 assert( pIn1->flags & MEM_Blob );
drh5baaf402021-12-06 13:07:28 +00008298 assert( pIn1->n>0 );
drh2db144c2021-12-01 16:31:02 +00008299 h = filterHash(aMem, pOp);
8300#ifdef SQLITE_DEBUG
8301 if( db->flags&SQLITE_VdbeTrace ){
8302 int ii;
8303 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
8304 registerTrace(ii, &aMem[ii]);
8305 }
drh5baaf402021-12-06 13:07:28 +00008306 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
drh2db144c2021-12-01 16:31:02 +00008307 }
8308#endif
drh5baaf402021-12-06 13:07:28 +00008309 h %= pIn1->n;
drh2db144c2021-12-01 16:31:02 +00008310 pIn1->z[h/8] |= 1<<(h&7);
8311 break;
8312}
8313
8314/* Opcode: Filter P1 P2 P3 P4 *
8315** Synopsis: if key(P3@P4) not in filter(P1) goto P2
8316**
8317** Compute a hash on the key contained in the P4 registers starting
8318** with r[P3]. Check to see if that hash is found in the
8319** bloom filter hosted by register P1. If it is not present then
8320** maybe jump to P2. Otherwise fall through.
8321**
8322** False negatives are harmless. It is always safe to fall through,
8323** even if the value is in the bloom filter. A false negative causes
8324** more CPU cycles to be used, but it should still yield the correct
8325** answer. However, an incorrect answer may well arise from a
8326** false positive - if the jump is taken when it should fall through.
8327*/
8328case OP_Filter: { /* jump */
drh5baaf402021-12-06 13:07:28 +00008329 u64 h;
drh2db144c2021-12-01 16:31:02 +00008330
8331 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
8332 pIn1 = &aMem[pOp->p1];
drh7e910f62021-12-09 01:28:15 +00008333 assert( (pIn1->flags & MEM_Blob)!=0 );
8334 assert( pIn1->n >= 1 );
drh2db144c2021-12-01 16:31:02 +00008335 h = filterHash(aMem, pOp);
8336#ifdef SQLITE_DEBUG
8337 if( db->flags&SQLITE_VdbeTrace ){
8338 int ii;
8339 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
8340 registerTrace(ii, &aMem[ii]);
8341 }
drh5baaf402021-12-06 13:07:28 +00008342 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
drh2db144c2021-12-01 16:31:02 +00008343 }
8344#endif
drh5baaf402021-12-06 13:07:28 +00008345 h %= pIn1->n;
drh067c60c2021-12-04 18:45:08 +00008346 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){
8347 VdbeBranchTaken(1, 2);
drh23d41e62021-12-06 21:45:31 +00008348 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++;
drh067c60c2021-12-04 18:45:08 +00008349 goto jump_to_p2;
8350 }else{
drh23d41e62021-12-06 21:45:31 +00008351 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++;
drh067c60c2021-12-04 18:45:08 +00008352 VdbeBranchTaken(0, 2);
8353 }
drh2db144c2021-12-01 16:31:02 +00008354 break;
8355}
8356
drhf259df52017-12-27 20:38:35 +00008357/* Opcode: Trace P1 P2 * P4 *
8358**
8359** Write P4 on the statement trace output if statement tracing is
8360** enabled.
8361**
8362** Operand P1 must be 0x7fffffff and P2 must positive.
8363*/
drh74588ce2017-09-13 00:13:05 +00008364/* Opcode: Init P1 P2 P3 P4 *
drh72e26de2016-08-24 21:24:04 +00008365** Synopsis: Start at P2
drhaceb31b2014-02-08 01:40:27 +00008366**
8367** Programs contain a single instance of this opcode as the very first
8368** opcode.
drh949f9cd2008-01-12 21:35:57 +00008369**
8370** If tracing is enabled (by the sqlite3_trace()) interface, then
8371** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00008372** Or if P4 is blank, use the string returned by sqlite3_sql().
8373**
8374** If P2 is not zero, jump to instruction P2.
drh9e5eb9c2016-09-18 16:08:10 +00008375**
8376** Increment the value of P1 so that OP_Once opcodes will jump the
8377** first time they are evaluated for this run.
drh74588ce2017-09-13 00:13:05 +00008378**
8379** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
8380** error is encountered.
drh949f9cd2008-01-12 21:35:57 +00008381*/
drhf259df52017-12-27 20:38:35 +00008382case OP_Trace:
drhaceb31b2014-02-08 01:40:27 +00008383case OP_Init: { /* jump */
drh9e5eb9c2016-09-18 16:08:10 +00008384 int i;
drhb9f47992018-01-24 12:14:43 +00008385#ifndef SQLITE_OMIT_TRACE
8386 char *zTrace;
8387#endif
drh5fe63bf2016-07-25 02:42:22 +00008388
8389 /* If the P4 argument is not NULL, then it must be an SQL comment string.
8390 ** The "--" string is broken up to prevent false-positives with srcck1.c.
8391 **
8392 ** This assert() provides evidence for:
8393 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
8394 ** would have been returned by the legacy sqlite3_trace() interface by
8395 ** using the X argument when X begins with "--" and invoking
8396 ** sqlite3_expanded_sql(P) otherwise.
8397 */
8398 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
drhf259df52017-12-27 20:38:35 +00008399
8400 /* OP_Init is always instruction 0 */
8401 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
drh856c1032009-06-02 15:21:42 +00008402
drhaceb31b2014-02-08 01:40:27 +00008403#ifndef SQLITE_OMIT_TRACE
drhfca760c2016-07-14 01:09:08 +00008404 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
drha24832b2022-04-01 19:04:13 +00008405 && p->minWriteFileFormat!=254 /* tag-20220401a */
drh37f58e92012-09-04 21:34:26 +00008406 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
8407 ){
drh3d2a5292016-07-13 22:55:01 +00008408#ifndef SQLITE_OMIT_DEPRECATED
drhfca760c2016-07-14 01:09:08 +00008409 if( db->mTrace & SQLITE_TRACE_LEGACY ){
drh5fe63bf2016-07-25 02:42:22 +00008410 char *z = sqlite3VdbeExpandSql(p, zTrace);
drh08b92082020-08-10 14:18:00 +00008411 db->trace.xLegacy(db->pTraceArg, z);
drhbd441f72016-07-25 02:31:48 +00008412 sqlite3_free(z);
drhfca760c2016-07-14 01:09:08 +00008413 }else
drh3d2a5292016-07-13 22:55:01 +00008414#endif
drh7adbcff2017-03-20 15:29:28 +00008415 if( db->nVdbeExec>1 ){
8416 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
drh08b92082020-08-10 14:18:00 +00008417 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
drh7adbcff2017-03-20 15:29:28 +00008418 sqlite3DbFree(db, z);
8419 }else{
drh08b92082020-08-10 14:18:00 +00008420 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
drh3d2a5292016-07-13 22:55:01 +00008421 }
drh949f9cd2008-01-12 21:35:57 +00008422 }
drh8f8b2312013-10-18 20:03:43 +00008423#ifdef SQLITE_USE_FCNTL_TRACE
8424 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
8425 if( zTrace ){
mistachkind8992ce2016-09-20 17:49:01 +00008426 int j;
8427 for(j=0; j<db->nDb; j++){
8428 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
8429 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
drh8f8b2312013-10-18 20:03:43 +00008430 }
8431 }
8432#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00008433#ifdef SQLITE_DEBUG
8434 if( (db->flags & SQLITE_SqlTrace)!=0
8435 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
8436 ){
8437 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
8438 }
8439#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00008440#endif /* SQLITE_OMIT_TRACE */
drh4910a762016-09-03 01:46:15 +00008441 assert( pOp->p2>0 );
drh9e5eb9c2016-09-18 16:08:10 +00008442 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
drhf259df52017-12-27 20:38:35 +00008443 if( pOp->opcode==OP_Trace ) break;
drh9e5eb9c2016-09-18 16:08:10 +00008444 for(i=1; i<p->nOp; i++){
8445 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
8446 }
8447 pOp->p1 = 0;
8448 }
8449 pOp->p1++;
drh00d11d42017-06-29 12:49:18 +00008450 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
drh4910a762016-09-03 01:46:15 +00008451 goto jump_to_p2;
drh949f9cd2008-01-12 21:35:57 +00008452}
drh949f9cd2008-01-12 21:35:57 +00008453
drh28935362013-12-07 20:39:19 +00008454#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0df57012015-08-14 15:05:55 +00008455/* Opcode: CursorHint P1 * * P4 *
drh28935362013-12-07 20:39:19 +00008456**
8457** Provide a hint to cursor P1 that it only needs to return rows that
drh0df57012015-08-14 15:05:55 +00008458** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
8459** to values currently held in registers. TK_COLUMN terms in the P4
8460** expression refer to columns in the b-tree to which cursor P1 is pointing.
drh28935362013-12-07 20:39:19 +00008461*/
8462case OP_CursorHint: {
8463 VdbeCursor *pC;
8464
8465 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
8466 assert( pOp->p4type==P4_EXPR );
8467 pC = p->apCsr[pOp->p1];
dan91d3a612014-07-15 11:59:44 +00008468 if( pC ){
drhc960dcb2015-11-20 19:22:01 +00008469 assert( pC->eCurType==CURTYPE_BTREE );
drh62aaa6c2015-11-21 17:27:42 +00008470 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
8471 pOp->p4.pExpr, aMem);
dan91d3a612014-07-15 11:59:44 +00008472 }
drh28935362013-12-07 20:39:19 +00008473 break;
8474}
8475#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh91fd4d42008-01-19 20:11:25 +00008476
drh4031baf2018-05-28 17:31:20 +00008477#ifdef SQLITE_DEBUG
8478/* Opcode: Abortable * * * * *
8479**
8480** Verify that an Abort can happen. Assert if an Abort at this point
8481** might cause database corruption. This opcode only appears in debugging
8482** builds.
8483**
8484** An Abort is safe if either there have been no writes, or if there is
8485** an active statement journal.
8486*/
8487case OP_Abortable: {
8488 sqlite3VdbeAssertAbortable(p);
8489 break;
8490}
8491#endif
8492
drh13d79502019-12-23 02:18:49 +00008493#ifdef SQLITE_DEBUG
drh3aef2fb2020-01-02 17:46:02 +00008494/* Opcode: ReleaseReg P1 P2 P3 * P5
drh13d79502019-12-23 02:18:49 +00008495** Synopsis: release r[P1@P2] mask P3
8496**
8497** Release registers from service. Any content that was in the
8498** the registers is unreliable after this opcode completes.
8499**
8500** The registers released will be the P2 registers starting at P1,
8501** except if bit ii of P3 set, then do not release register P1+ii.
8502** In other words, P3 is a mask of registers to preserve.
8503**
8504** Releasing a register clears the Mem.pScopyFrom pointer. That means
8505** that if the content of the released register was set using OP_SCopy,
8506** a change to the value of the source register for the OP_SCopy will no longer
8507** generate an assertion fault in sqlite3VdbeMemAboutToChange().
8508**
drh3aef2fb2020-01-02 17:46:02 +00008509** If P5 is set, then all released registers have their type set
8510** to MEM_Undefined so that any subsequent attempt to read the released
drh13d79502019-12-23 02:18:49 +00008511** register (before it is reinitialized) will generate an assertion fault.
drh3aef2fb2020-01-02 17:46:02 +00008512**
8513** P5 ought to be set on every call to this opcode.
8514** However, there are places in the code generator will release registers
drh13d79502019-12-23 02:18:49 +00008515** before their are used, under the (valid) assumption that the registers
8516** will not be reallocated for some other purpose before they are used and
8517** hence are safe to release.
8518**
8519** This opcode is only available in testing and debugging builds. It is
8520** not generated for release builds. The purpose of this opcode is to help
8521** validate the generated bytecode. This opcode does not actually contribute
8522** to computing an answer.
8523*/
8524case OP_ReleaseReg: {
8525 Mem *pMem;
8526 int i;
8527 u32 constMask;
8528 assert( pOp->p1>0 );
8529 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
8530 pMem = &aMem[pOp->p1];
8531 constMask = pOp->p3;
8532 for(i=0; i<pOp->p2; i++, pMem++){
drh7edce5e2019-12-23 13:24:34 +00008533 if( i>=32 || (constMask & MASKBIT32(i))==0 ){
drh13d79502019-12-23 02:18:49 +00008534 pMem->pScopyFrom = 0;
drh3aef2fb2020-01-02 17:46:02 +00008535 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
drh13d79502019-12-23 02:18:49 +00008536 }
8537 }
8538 break;
8539}
8540#endif
8541
drh91fd4d42008-01-19 20:11:25 +00008542/* Opcode: Noop * * * * *
8543**
8544** Do nothing. This instruction is often useful as a jump
8545** destination.
drh5e00f6c2001-09-13 13:46:56 +00008546*/
drh91fd4d42008-01-19 20:11:25 +00008547/*
8548** The magic Explain opcode are only inserted when explain==2 (which
8549** is to say when the EXPLAIN QUERY PLAN syntax is used.)
8550** This opcode records information from the optimizer. It is the
8551** the same as a no-op. This opcodesnever appears in a real VM program.
8552*/
drh4031baf2018-05-28 17:31:20 +00008553default: { /* This is really OP_Noop, OP_Explain */
drh13573c72010-01-12 17:04:07 +00008554 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh4031baf2018-05-28 17:31:20 +00008555
drh5e00f6c2001-09-13 13:46:56 +00008556 break;
8557}
8558
8559/*****************************************************************************
8560** The cases of the switch statement above this line should all be indented
8561** by 6 spaces. But the left-most 6 spaces have been removed to improve the
8562** readability. From this point on down, the normal indentation rules are
8563** restored.
8564*****************************************************************************/
8565 }
drh6e142f52000-06-08 13:36:40 +00008566
drh7b396862003-01-01 23:06:20 +00008567#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00008568 {
drh35043cc2018-02-12 20:27:34 +00008569 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
drh6dc41482015-04-16 17:31:02 +00008570 if( endTime>start ) pOrigOp->cycles += endTime - start;
8571 pOrigOp->cnt++;
drh8178a752003-01-05 21:41:40 +00008572 }
drh7b396862003-01-01 23:06:20 +00008573#endif
8574
drh6e142f52000-06-08 13:36:40 +00008575 /* The following code adds nothing to the actual functionality
8576 ** of the program. It is only here for testing and debugging.
8577 ** On the other hand, it does burn CPU cycles every time through
8578 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
8579 */
8580#ifndef NDEBUG
drh6dc41482015-04-16 17:31:02 +00008581 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
drhae7e1512007-05-02 16:51:59 +00008582
drhcf1023c2007-05-08 20:59:49 +00008583#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00008584 if( db->flags & SQLITE_VdbeTrace ){
drh7cc84c22016-04-11 13:36:42 +00008585 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
drh84e55a82013-11-13 17:58:23 +00008586 if( rc!=0 ) printf("rc=%d\n",rc);
drh7cc84c22016-04-11 13:36:42 +00008587 if( opProperty & (OPFLG_OUT2) ){
drh6dc41482015-04-16 17:31:02 +00008588 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
drh75897232000-05-29 14:26:00 +00008589 }
drh7cc84c22016-04-11 13:36:42 +00008590 if( opProperty & OPFLG_OUT3 ){
drh6dc41482015-04-16 17:31:02 +00008591 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00008592 }
drh17aceeb2020-01-04 19:12:13 +00008593 if( opProperty==0xff ){
8594 /* Never happens. This code exists to avoid a harmless linkage
8595 ** warning aboud sqlite3VdbeRegisterDump() being defined but not
8596 ** used. */
8597 sqlite3VdbeRegisterDump(p);
8598 }
drh75897232000-05-29 14:26:00 +00008599 }
danielk1977b5402fb2005-01-12 07:15:04 +00008600#endif /* SQLITE_DEBUG */
8601#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00008602 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00008603
drha05a7222008-01-19 03:35:58 +00008604 /* If we reach this point, it means that execution is finished with
8605 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00008606 */
drh9467abf2016-02-17 18:44:11 +00008607abort_due_to_error:
drhf56a4bf2020-11-18 21:50:05 +00008608 if( db->mallocFailed ){
8609 rc = SQLITE_NOMEM_BKPT;
8610 }else if( rc==SQLITE_IOERR_CORRUPTFS ){
8611 rc = SQLITE_CORRUPT_BKPT;
8612 }
drha05a7222008-01-19 03:35:58 +00008613 assert( rc );
drh11eb9c62021-09-16 12:33:53 +00008614#ifdef SQLITE_DEBUG
8615 if( db->flags & SQLITE_VdbeTrace ){
drh5b001cc2021-11-15 13:22:42 +00008616 const char *zTrace = p->zSql;
8617 if( zTrace==0 ){
8618 if( aOp[0].opcode==OP_Trace ){
8619 zTrace = aOp[0].p4.z;
8620 }
8621 if( zTrace==0 ) zTrace = "???";
8622 }
8623 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
drh11eb9c62021-09-16 12:33:53 +00008624 }
8625#endif
drh9467abf2016-02-17 18:44:11 +00008626 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
8627 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
8628 }
drha05a7222008-01-19 03:35:58 +00008629 p->rc = rc;
drhf68521c2016-03-21 12:28:02 +00008630 sqlite3SystemError(db, rc);
drha64fa912010-03-04 00:53:32 +00008631 testcase( sqlite3GlobalConfig.xLog!=0 );
8632 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
drhf56fa462015-04-13 21:39:54 +00008633 (int)(pOp - aOp), p->zSql, p->zErrMsg);
drh8703edd2022-04-03 22:35:13 +00008634 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
drh4a642b62016-02-05 01:55:27 +00008635 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
drh46c425b2021-11-10 10:59:10 +00008636 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){
8637 db->flags |= SQLITE_CorruptRdOnly;
8638 }
danielk19777eaabcd2008-07-07 14:56:56 +00008639 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00008640 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00008641 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00008642 }
drh900b31e2007-08-28 02:27:51 +00008643
8644 /* This is the only way out of this procedure. We have to
8645 ** release the mutexes on btrees that were acquired at the
8646 ** top. */
8647vdbe_return:
drhc332e042019-02-12 21:04:33 +00008648#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhb1af9c62019-02-20 13:55:45 +00008649 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
8650 nProgressLimit += db->nProgressOps;
drhc332e042019-02-12 21:04:33 +00008651 if( db->xProgress(db->pProgressArg) ){
drhd1d89142020-07-06 12:13:05 +00008652 nProgressLimit = LARGEST_UINT64;
drhc332e042019-02-12 21:04:33 +00008653 rc = SQLITE_INTERRUPT;
8654 goto abort_due_to_error;
8655 }
8656 }
8657#endif
drh9b47ee32013-08-20 03:13:51 +00008658 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00008659 sqlite3VdbeLeave(p);
dan83f0ab82016-01-29 18:04:31 +00008660 assert( rc!=SQLITE_OK || nExtraDelete==0
8661 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
8662 );
drhb86ccfb2003-01-28 23:13:10 +00008663 return rc;
8664
drh023ae032007-05-08 12:12:16 +00008665 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
8666 ** is encountered.
8667 */
8668too_big:
drh22c17b82015-05-15 04:13:15 +00008669 sqlite3VdbeError(p, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00008670 rc = SQLITE_TOOBIG;
drh9467abf2016-02-17 18:44:11 +00008671 goto abort_due_to_error;
drh023ae032007-05-08 12:12:16 +00008672
drh98640a32007-06-07 19:08:32 +00008673 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00008674 */
8675no_mem:
drh4a642b62016-02-05 01:55:27 +00008676 sqlite3OomFault(db);
drh22c17b82015-05-15 04:13:15 +00008677 sqlite3VdbeError(p, "out of memory");
mistachkinfad30392016-02-13 23:43:46 +00008678 rc = SQLITE_NOMEM_BKPT;
drh9467abf2016-02-17 18:44:11 +00008679 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00008680
danielk19776f8a5032004-05-10 10:34:51 +00008681 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00008682 ** flag.
8683 */
8684abort_due_to_interrupt:
dan892edb62020-03-30 13:35:05 +00008685 assert( AtomicLoad(&db->u1.isInterrupted) );
drh56f18732020-06-03 15:59:22 +00008686 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +00008687 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00008688}