blob: 8226e2cd764f5546ee006c1ac71a57de455ff148 [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;
drh3547e492022-12-23 14:49:24 +0000136 (void)pc;
137 (void)pOp;
138 (void)v;
drh52f11b82020-01-02 13:26:49 +0000139 n++;
140}
141#endif
142
drhb7654112008-01-12 12:48:07 +0000143/*
drh5655c542014-02-19 19:14:34 +0000144** Invoke the VDBE coverage callback, if that callback is defined. This
145** feature is used for test suite validation only and does not appear an
146** production builds.
147**
drhc9065332019-04-01 14:01:21 +0000148** M is the type of branch. I is the direction taken for this instance of
149** the branch.
150**
151** M: 2 - two-way branch (I=0: fall-thru 1: jump )
152** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
153** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
154**
155** In other words, if M is 2, then I is either 0 (for fall-through) or
156** 1 (for when the branch is taken). If M is 3, the I is 0 for an
157** ordinary fall-through, I is 1 if the branch was taken, and I is 2
158** if the result of comparison is NULL. For M=3, I=2 the jump may or
159** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
160** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2
161** depending on if the operands are less than, equal, or greater than.
drh4336b0e2014-08-05 00:53:51 +0000162**
163** iSrcLine is the source code line (from the __LINE__ macro) that
drh7083a482018-07-10 16:04:04 +0000164** generated the VDBE instruction combined with flag bits. The source
165** code line number is in the lower 24 bits of iSrcLine and the upper
166** 8 bytes are flags. The lower three bits of the flags indicate
167** values for I that should never occur. For example, if the branch is
168** always taken, the flags should be 0x05 since the fall-through and
169** alternate branch are never taken. If a branch is never taken then
170** flags should be 0x06 since only the fall-through approach is allowed.
171**
drhc9065332019-04-01 14:01:21 +0000172** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
drh7083a482018-07-10 16:04:04 +0000173** interested in equal or not-equal. In other words, I==0 and I==2
drhc9065332019-04-01 14:01:21 +0000174** should be treated as equivalent
drh7083a482018-07-10 16:04:04 +0000175**
176** Since only a line number is retained, not the filename, this macro
177** only works for amalgamation builds. But that is ok, since these macros
178** should be no-ops except for special builds used to measure test coverage.
drh688852a2014-02-17 22:40:43 +0000179*/
180#if !defined(SQLITE_VDBE_COVERAGE)
181# define VdbeBranchTaken(I,M)
182#else
drh5655c542014-02-19 19:14:34 +0000183# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
drh7083a482018-07-10 16:04:04 +0000184 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
185 u8 mNever;
186 assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */
187 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
188 assert( I<M ); /* I can only be 2 if M is 3 or 4 */
189 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
190 I = 1<<I;
191 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
192 ** the flags indicate directions that the branch can never go. If
193 ** a branch really does go in one of those directions, assert right
194 ** away. */
195 mNever = iSrcLine >> 24;
196 assert( (I & mNever)==0 );
197 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
drhc9065332019-04-01 14:01:21 +0000198 /* Invoke the branch coverage callback with three arguments:
199 ** iSrcLine - the line number of the VdbeCoverage() macro, with
200 ** flags removed.
201 ** I - Mask of bits 0x07 indicating which cases are are
202 ** fulfilled by this instance of the jump. 0x01 means
203 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
204 ** impossible cases (ex: if the comparison is never NULL)
205 ** are filled in automatically so that the coverage
206 ** measurement logic does not flag those impossible cases
207 ** as missed coverage.
208 ** M - Type of jump. Same as M argument above
209 */
drh7083a482018-07-10 16:04:04 +0000210 I |= mNever;
211 if( M==2 ) I |= 0x04;
212 if( M==4 ){
213 I |= 0x08;
drh6ccbd272018-07-10 17:10:44 +0000214 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
drh5655c542014-02-19 19:14:34 +0000215 }
drh7083a482018-07-10 16:04:04 +0000216 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
217 iSrcLine&0xffffff, I, M);
drh5655c542014-02-19 19:14:34 +0000218 }
drh688852a2014-02-17 22:40:43 +0000219#endif
220
221/*
danielk1977bd7e4602004-05-24 07:34:48 +0000222** An ephemeral string value (signified by the MEM_Ephem flag) contains
223** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000224** is responsible for deallocating that string. Because the register
225** does not control the string, it might be deleted without the register
226** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000227**
228** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000229** string that the register itself controls. In other words, it
drhc91b2fd2014-03-01 18:13:23 +0000230** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
danielk1977bd7e4602004-05-24 07:34:48 +0000231*/
drhb21c8cd2007-08-21 19:33:56 +0000232#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000233 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000234 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000235
dan689ab892011-08-12 15:02:00 +0000236/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
drhc960dcb2015-11-20 19:22:01 +0000237#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
danielk19778a6b5412004-05-24 07:04:25 +0000238
239/*
drhdfe88ec2008-11-03 20:55:06 +0000240** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000241** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000242*/
drhdfe88ec2008-11-03 20:55:06 +0000243static VdbeCursor *allocateCursor(
244 Vdbe *p, /* The virtual machine */
245 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000246 int nField, /* Number of fields in the table or index */
drhc960dcb2015-11-20 19:22:01 +0000247 u8 eCurType /* Type of the new cursor */
danielk1977cd3e8f72008-03-25 09:47:35 +0000248){
249 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000250 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000251 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000252 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000253 **
254 ** * Sometimes cursor numbers are used for a couple of different
255 ** purposes in a vdbe program. The different uses might require
256 ** different sized allocations. Memory cells provide growable
257 ** allocations.
258 **
259 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
260 ** be freed lazily via the sqlite3_release_memory() API. This
261 ** minimizes the number of malloc calls made by the system.
262 **
drh3cdce922016-03-21 00:30:40 +0000263 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
drh9f6168b2016-03-19 23:32:58 +0000264 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
265 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
danielk1977cd3e8f72008-03-25 09:47:35 +0000266 */
drh9f6168b2016-03-19 23:32:58 +0000267 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
danielk1977cd3e8f72008-03-25 09:47:35 +0000268
danielk19775f096132008-03-28 15:44:09 +0000269 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000270 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000271 nByte =
drhcf6e3fd2022-04-01 18:45:11 +0000272 ROUND8P(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
drhc960dcb2015-11-20 19:22:01 +0000273 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
danielk1977cd3e8f72008-03-25 09:47:35 +0000274
drh9f6168b2016-03-19 23:32:58 +0000275 assert( iCur>=0 && iCur<p->nCursor );
drha3fa1402016-04-29 02:55:05 +0000276 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
drh473571b2022-04-01 18:19:04 +0000277 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000278 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000279 }
drh2454e4a2021-05-15 19:36:36 +0000280
281 /* There used to be a call to sqlite3VdbeMemClearAndResize() to make sure
282 ** the pMem used to hold space for the cursor has enough storage available
283 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
284 ** to hold cursors, it is faster to in-line the logic. */
285 assert( pMem->flags==MEM_Undefined );
286 assert( (pMem->flags & MEM_Dyn)==0 );
287 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc );
288 if( pMem->szMalloc<nByte ){
289 if( pMem->szMalloc>0 ){
290 sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
danielk1977cd3e8f72008-03-25 09:47:35 +0000291 }
drh2454e4a2021-05-15 19:36:36 +0000292 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte);
293 if( pMem->zMalloc==0 ){
294 pMem->szMalloc = 0;
295 return 0;
296 }
297 pMem->szMalloc = nByte;
298 }
299
300 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
301 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
302 pCx->eCurType = eCurType;
drh2454e4a2021-05-15 19:36:36 +0000303 pCx->nField = nField;
304 pCx->aOffset = &pCx->aType[nField];
305 if( eCurType==CURTYPE_BTREE ){
306 pCx->uc.pCursor = (BtCursor*)
drhcf6e3fd2022-04-01 18:45:11 +0000307 &pMem->z[ROUND8P(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drh2454e4a2021-05-15 19:36:36 +0000308 sqlite3BtreeCursorZero(pCx->uc.pCursor);
danielk197794eb6a12005-12-15 15:22:08 +0000309 }
drh4774b132004-06-12 20:12:51 +0000310 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000311}
312
danielk19773d1bfea2004-05-14 11:00:53 +0000313/*
drh8a3884e2019-05-29 21:18:27 +0000314** The string in pRec is known to look like an integer and to have a
315** floating point value of rValue. Return true and set *piValue to the
316** integer value if the string is in range to be an integer. Otherwise,
317** return false.
318*/
319static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
drha3a4da02022-08-08 12:19:13 +0000320 i64 iValue;
drh26e817f2022-08-08 16:25:13 +0000321 iValue = sqlite3RealToI64(rValue);
drh8a3884e2019-05-29 21:18:27 +0000322 if( sqlite3RealSameAsInt(rValue,iValue) ){
drhc285ded2019-06-10 18:33:16 +0000323 *piValue = iValue;
324 return 1;
drh8a3884e2019-05-29 21:18:27 +0000325 }
326 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
327}
328
329/*
drh29d72102006-02-09 22:13:41 +0000330** Try to convert a value into a numeric representation if we can
331** do so without loss of information. In other words, if the string
332** looks like a number, convert it into a number. If it does not
333** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000334**
335** If the bTryForInt flag is true, then extra effort is made to give
336** an integer representation. Strings that look like floating point
337** values but which have no fractional component (example: '48.00')
338** will have a MEM_Int representation when bTryForInt is true.
339**
340** If bTryForInt is false, then if the input string contains a decimal
341** point or exponential notation, the result is only MEM_Real, even
342** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000343*/
drhbd9507c2014-08-23 17:21:37 +0000344static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000345 double rValue;
drh975b4c62014-07-26 16:47:23 +0000346 u8 enc = pRec->enc;
drh8a3884e2019-05-29 21:18:27 +0000347 int rc;
drh169f0772019-05-02 21:36:26 +0000348 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
drh8a3884e2019-05-29 21:18:27 +0000349 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
drh9a278222019-06-07 22:26:08 +0000350 if( rc<=0 ) return;
drh8a3884e2019-05-29 21:18:27 +0000351 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
drh975b4c62014-07-26 16:47:23 +0000352 pRec->flags |= MEM_Int;
353 }else{
drh74eaba42014-09-18 17:52:15 +0000354 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000355 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000356 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000357 }
drh06b3bd52018-02-01 01:13:33 +0000358 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
359 ** string representation after computing a numeric equivalent, because the
360 ** string representation might not be the canonical representation for the
361 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
362 pRec->flags &= ~MEM_Str;
drh29d72102006-02-09 22:13:41 +0000363}
364
365/*
drh8a512562005-11-14 22:29:05 +0000366** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000367**
drh8a512562005-11-14 22:29:05 +0000368** SQLITE_AFF_INTEGER:
369** SQLITE_AFF_REAL:
370** SQLITE_AFF_NUMERIC:
371** Try to convert pRec to an integer representation or a
372** floating-point representation if an integer representation
373** is not possible. Note that the integer representation is
374** always preferred, even if the affinity is REAL, because
375** an integer representation is more space efficient on disk.
376**
drh00d6b272022-12-15 20:03:08 +0000377** SQLITE_AFF_FLEXNUM:
378** If the value is text, then try to convert it into a number of
379** some kind (integer or real) but do not make any other changes.
380**
drh8a512562005-11-14 22:29:05 +0000381** SQLITE_AFF_TEXT:
382** Convert pRec to a text representation.
383**
drh05883a32015-06-02 15:32:08 +0000384** SQLITE_AFF_BLOB:
drh96fb16e2019-08-06 14:37:24 +0000385** SQLITE_AFF_NONE:
drh8a512562005-11-14 22:29:05 +0000386** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000387*/
drh17435752007-08-16 04:30:38 +0000388static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000389 Mem *pRec, /* The value to apply affinity to */
390 char affinity, /* The affinity to be applied */
391 u8 enc /* Use this text encoding */
392){
drh7ea31cc2014-09-18 14:36:00 +0000393 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000394 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
drh00d6b272022-12-15 20:03:08 +0000395 || affinity==SQLITE_AFF_NUMERIC || affinity==SQLITE_AFF_FLEXNUM );
drha3fa1402016-04-29 02:55:05 +0000396 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
dane3b1c382023-01-05 13:35:23 +0000397 if( (pRec->flags & (MEM_Real|MEM_IntReal))==0 ){
drh11a6eee2014-09-19 22:01:54 +0000398 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drh00d6b272022-12-15 20:03:08 +0000399 }else if( affinity<=SQLITE_AFF_REAL ){
drhbd9507c2014-08-23 17:21:37 +0000400 sqlite3VdbeIntegerAffinity(pRec);
401 }
drh17c40292004-07-21 02:53:29 +0000402 }
drh7ea31cc2014-09-18 14:36:00 +0000403 }else if( affinity==SQLITE_AFF_TEXT ){
danielk19773d1bfea2004-05-14 11:00:53 +0000404 /* Only attempt the conversion to TEXT if there is an integer or real
drhf4479502004-05-27 03:12:53 +0000405 ** representation (blob and NULL do not get converted) but no string
drha3fa1402016-04-29 02:55:05 +0000406 ** representation. It would be harmless to repeat the conversion if
407 ** there is already a string rep, but it is pointless to waste those
408 ** CPU cycles. */
409 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
drh169f0772019-05-02 21:36:26 +0000410 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
drh3242c692019-05-04 01:29:13 +0000411 testcase( pRec->flags & MEM_Int );
412 testcase( pRec->flags & MEM_Real );
413 testcase( pRec->flags & MEM_IntReal );
drha3fa1402016-04-29 02:55:05 +0000414 sqlite3VdbeMemStringify(pRec, enc, 1);
415 }
danielk19773d1bfea2004-05-14 11:00:53 +0000416 }
drh169f0772019-05-02 21:36:26 +0000417 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
danielk19773d1bfea2004-05-14 11:00:53 +0000418 }
419}
420
danielk1977aee18ef2005-03-09 12:26:50 +0000421/*
drh29d72102006-02-09 22:13:41 +0000422** Try to convert the type of a function argument or a result column
423** into a numeric representation. Use either INTEGER or REAL whichever
424** is appropriate. But only do the conversion if it is possible without
425** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000426*/
427int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000428 int eType = sqlite3_value_type(pVal);
429 if( eType==SQLITE_TEXT ){
430 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000431 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000432 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000433 }
drh1b27b8c2014-02-10 03:21:57 +0000434 return eType;
drh29d72102006-02-09 22:13:41 +0000435}
436
437/*
danielk1977aee18ef2005-03-09 12:26:50 +0000438** Exported version of applyAffinity(). This one works on sqlite3_value*,
439** not the internal Mem* type.
440*/
danielk19771e536952007-08-16 10:09:01 +0000441void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000442 sqlite3_value *pVal,
443 u8 affinity,
444 u8 enc
445){
drhb21c8cd2007-08-21 19:33:56 +0000446 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000447}
448
drh3d1d90a2014-03-24 15:00:15 +0000449/*
drhf1a89ed2014-08-23 17:41:15 +0000450** pMem currently only holds a string type (or maybe a BLOB that we can
451** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000452** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000453** accordingly.
454*/
455static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
drh9a278222019-06-07 22:26:08 +0000456 int rc;
457 sqlite3_int64 ix;
drh169f0772019-05-02 21:36:26 +0000458 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
drhf1a89ed2014-08-23 17:41:15 +0000459 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh1fd1cc42021-04-10 15:34:30 +0000460 if( ExpandBlob(pMem) ){
461 pMem->u.i = 0;
462 return MEM_Int;
463 }
drh9a278222019-06-07 22:26:08 +0000464 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
465 if( rc<=0 ){
466 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
467 pMem->u.i = ix;
468 return MEM_Int;
469 }else{
470 return MEM_Real;
471 }
472 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
473 pMem->u.i = ix;
drhf1a89ed2014-08-23 17:41:15 +0000474 return MEM_Int;
475 }
476 return MEM_Real;
477}
478
479/*
drh3d1d90a2014-03-24 15:00:15 +0000480** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
481** none.
482**
483** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000484** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000485*/
486static u16 numericType(Mem *pMem){
drh5e10d892022-08-08 13:04:08 +0000487 assert( (pMem->flags & MEM_Null)==0
488 || pMem->db==0 || pMem->db->mallocFailed );
489 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){
drh3242c692019-05-04 01:29:13 +0000490 testcase( pMem->flags & MEM_Int );
491 testcase( pMem->flags & MEM_Real );
492 testcase( pMem->flags & MEM_IntReal );
drh5e10d892022-08-08 13:04:08 +0000493 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null);
drh3d1d90a2014-03-24 15:00:15 +0000494 }
drh5e10d892022-08-08 13:04:08 +0000495 assert( pMem->flags & (MEM_Str|MEM_Blob) );
496 testcase( pMem->flags & MEM_Str );
497 testcase( pMem->flags & MEM_Blob );
498 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000499 return 0;
500}
501
danielk1977b5402fb2005-01-12 07:15:04 +0000502#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000503/*
danielk1977ca6b2912004-05-21 10:49:47 +0000504** Write a nice string representation of the contents of cell pMem
505** into buffer zBuf, length nBuf.
506*/
drh5ca06322020-01-06 19:23:41 +0000507void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){
danielk1977ca6b2912004-05-21 10:49:47 +0000508 int f = pMem->flags;
drh57196282004-10-06 15:41:16 +0000509 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977ca6b2912004-05-21 10:49:47 +0000510 if( f&MEM_Blob ){
511 int i;
512 char c;
513 if( f & MEM_Dyn ){
514 c = 'z';
515 assert( (f & (MEM_Static|MEM_Ephem))==0 );
516 }else if( f & MEM_Static ){
517 c = 't';
518 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
519 }else if( f & MEM_Ephem ){
520 c = 'e';
521 assert( (f & (MEM_Static|MEM_Dyn))==0 );
522 }else{
523 c = 's';
524 }
drhded33cc2020-01-08 11:36:30 +0000525 sqlite3_str_appendf(pStr, "%cx[", c);
drhefb5f9a2019-08-30 21:52:13 +0000526 for(i=0; i<25 && i<pMem->n; i++){
drh5ca06322020-01-06 19:23:41 +0000527 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
danielk1977ca6b2912004-05-21 10:49:47 +0000528 }
drh5ca06322020-01-06 19:23:41 +0000529 sqlite3_str_appendf(pStr, "|");
drhefb5f9a2019-08-30 21:52:13 +0000530 for(i=0; i<25 && i<pMem->n; i++){
danielk1977ca6b2912004-05-21 10:49:47 +0000531 char z = pMem->z[i];
drh5ca06322020-01-06 19:23:41 +0000532 sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z);
danielk1977ca6b2912004-05-21 10:49:47 +0000533 }
drh5ca06322020-01-06 19:23:41 +0000534 sqlite3_str_appendf(pStr,"]");
drhfdf972a2007-05-02 13:30:27 +0000535 if( f & MEM_Zero ){
drh5ca06322020-01-06 19:23:41 +0000536 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
drhfdf972a2007-05-02 13:30:27 +0000537 }
danielk1977b1bc9532004-05-22 03:05:33 +0000538 }else if( f & MEM_Str ){
drh5ca06322020-01-06 19:23:41 +0000539 int j;
mistachkin59171172020-01-18 19:02:20 +0000540 u8 c;
danielk1977b1bc9532004-05-22 03:05:33 +0000541 if( f & MEM_Dyn ){
drh5ca06322020-01-06 19:23:41 +0000542 c = 'z';
danielk1977b1bc9532004-05-22 03:05:33 +0000543 assert( (f & (MEM_Static|MEM_Ephem))==0 );
544 }else if( f & MEM_Static ){
drh5ca06322020-01-06 19:23:41 +0000545 c = 't';
danielk1977b1bc9532004-05-22 03:05:33 +0000546 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
547 }else if( f & MEM_Ephem ){
drh5ca06322020-01-06 19:23:41 +0000548 c = 'e';
danielk1977b1bc9532004-05-22 03:05:33 +0000549 assert( (f & (MEM_Static|MEM_Dyn))==0 );
550 }else{
drh5ca06322020-01-06 19:23:41 +0000551 c = 's';
danielk1977b1bc9532004-05-22 03:05:33 +0000552 }
drh5ca06322020-01-06 19:23:41 +0000553 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
drhefb5f9a2019-08-30 21:52:13 +0000554 for(j=0; j<25 && j<pMem->n; j++){
mistachkin59171172020-01-18 19:02:20 +0000555 c = pMem->z[j];
drh5ca06322020-01-06 19:23:41 +0000556 sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.');
danielk1977b1bc9532004-05-22 03:05:33 +0000557 }
drh5ca06322020-01-06 19:23:41 +0000558 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
danielk1977ca6b2912004-05-21 10:49:47 +0000559 }
danielk1977ca6b2912004-05-21 10:49:47 +0000560}
561#endif
562
drh5b6afba2008-01-05 16:29:28 +0000563#ifdef SQLITE_DEBUG
564/*
565** Print the value of a register for tracing purposes:
566*/
drh84e55a82013-11-13 17:58:23 +0000567static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000568 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000569 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000570 }else if( p->flags & MEM_Null ){
drhce2fbd12018-01-12 21:00:14 +0000571 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
drh5b6afba2008-01-05 16:29:28 +0000572 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000573 printf(" si:%lld", p->u.i);
drh169f0772019-05-02 21:36:26 +0000574 }else if( (p->flags & (MEM_IntReal))!=0 ){
drh83a1daf2019-05-01 18:59:33 +0000575 printf(" ir:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000576 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000577 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000578#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000579 }else if( p->flags & MEM_Real ){
drhd1c472d2019-10-03 14:51:59 +0000580 printf(" r:%.17g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000581#endif
drh9d67afc2018-08-29 20:24:03 +0000582 }else if( sqlite3VdbeMemIsRowSet(p) ){
drh84e55a82013-11-13 17:58:23 +0000583 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000584 }else{
drh5ca06322020-01-06 19:23:41 +0000585 StrAccum acc;
586 char zBuf[1000];
587 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
588 sqlite3VdbeMemPrettyPrint(p, &acc);
589 printf(" %s", sqlite3StrAccumFinish(&acc));
drh5b6afba2008-01-05 16:29:28 +0000590 }
dan5b6c8e42016-01-30 15:46:03 +0000591 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
drh5b6afba2008-01-05 16:29:28 +0000592}
drh84e55a82013-11-13 17:58:23 +0000593static void registerTrace(int iReg, Mem *p){
drh22e95fb2020-01-02 14:42:42 +0000594 printf("R[%d] = ", iReg);
drh84e55a82013-11-13 17:58:23 +0000595 memTracePrint(p);
drh22e95fb2020-01-02 14:42:42 +0000596 if( p->pScopyFrom ){
597 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
598 }
drh84e55a82013-11-13 17:58:23 +0000599 printf("\n");
drhe2bc6552017-04-17 20:50:34 +0000600 sqlite3VdbeCheckMemInvariants(p);
drh5b6afba2008-01-05 16:29:28 +0000601}
drh93ffb502021-05-18 19:10:10 +0000602/**/ void sqlite3PrintMem(Mem *pMem){
drh59df3e92021-05-05 19:46:50 +0000603 memTracePrint(pMem);
604 printf("\n");
605 fflush(stdout);
606}
drh5b6afba2008-01-05 16:29:28 +0000607#endif
608
609#ifdef SQLITE_DEBUG
drh22e95fb2020-01-02 14:42:42 +0000610/*
611** Show the values of all registers in the virtual machine. Used for
612** interactive debugging.
613*/
614void sqlite3VdbeRegisterDump(Vdbe *v){
615 int i;
616 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
617}
618#endif /* SQLITE_DEBUG */
619
620
621#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000622# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000623#else
624# define REGISTER_TRACE(R,M)
625#endif
626
danielk1977fd7f0452008-12-17 17:30:26 +0000627#ifndef NDEBUG
628/*
629** This function is only called from within an assert() expression. It
630** checks that the sqlite3.nTransaction variable is correctly set to
631** the number of non-transaction savepoints currently in the
632** linked list starting at sqlite3.pSavepoint.
633**
634** Usage:
635**
636** assert( checkSavepointCount(db) );
637*/
638static int checkSavepointCount(sqlite3 *db){
639 int n = 0;
640 Savepoint *p;
641 for(p=db->pSavepoint; p; p=p->pNext) n++;
642 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
643 return 1;
644}
645#endif
646
drh27a348c2015-04-13 19:14:06 +0000647/*
648** Return the register of pOp->p2 after first preparing it to be
649** overwritten with an integer value.
drh9eef8c62015-10-15 17:31:41 +0000650*/
651static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
652 sqlite3VdbeMemSetNull(pOut);
653 pOut->flags = MEM_Int;
654 return pOut;
655}
drh27a348c2015-04-13 19:14:06 +0000656static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
657 Mem *pOut;
658 assert( pOp->p2>0 );
drh9f6168b2016-03-19 23:32:58 +0000659 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
drh27a348c2015-04-13 19:14:06 +0000660 pOut = &p->aMem[pOp->p2];
661 memAboutToChange(p, pOut);
drha3fa1402016-04-29 02:55:05 +0000662 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
drh9eef8c62015-10-15 17:31:41 +0000663 return out2PrereleaseWithClear(pOut);
664 }else{
665 pOut->flags = MEM_Int;
666 return pOut;
667 }
drh27a348c2015-04-13 19:14:06 +0000668}
669
drhfaf9c772021-08-20 08:05:42 +0000670/*
drh2db144c2021-12-01 16:31:02 +0000671** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
672** with pOp->p3. Return the hash.
673*/
drh5baaf402021-12-06 13:07:28 +0000674static u64 filterHash(const Mem *aMem, const Op *pOp){
drh2db144c2021-12-01 16:31:02 +0000675 int i, mx;
drh5baaf402021-12-06 13:07:28 +0000676 u64 h = 0;
drh2db144c2021-12-01 16:31:02 +0000677
drh2db144c2021-12-01 16:31:02 +0000678 assert( pOp->p4type==P4_INT32 );
drh2db144c2021-12-01 16:31:02 +0000679 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
680 const Mem *p = &aMem[i];
681 if( p->flags & (MEM_Int|MEM_IntReal) ){
drh5baaf402021-12-06 13:07:28 +0000682 h += p->u.i;
drh2db144c2021-12-01 16:31:02 +0000683 }else if( p->flags & MEM_Real ){
drh5baaf402021-12-06 13:07:28 +0000684 h += sqlite3VdbeIntValue(p);
drh2db144c2021-12-01 16:31:02 +0000685 }else if( p->flags & (MEM_Str|MEM_Blob) ){
686 h += p->n;
drh067c60c2021-12-04 18:45:08 +0000687 if( p->flags & MEM_Zero ) h += p->u.nZero;
drh2db144c2021-12-01 16:31:02 +0000688 }
689 }
drh5baaf402021-12-06 13:07:28 +0000690 return h;
drh2db144c2021-12-01 16:31:02 +0000691}
692
693/*
drhfaf9c772021-08-20 08:05:42 +0000694** Return the symbolic name for the data type of a pMem
695*/
696static const char *vdbeMemTypeName(Mem *pMem){
697 static const char *azTypes[] = {
698 /* SQLITE_INTEGER */ "INT",
699 /* SQLITE_FLOAT */ "REAL",
700 /* SQLITE_TEXT */ "TEXT",
701 /* SQLITE_BLOB */ "BLOB",
702 /* SQLITE_NULL */ "NULL"
703 };
704 return azTypes[sqlite3_value_type(pMem)-1];
705}
drhb9755982010-07-24 16:34:37 +0000706
707/*
drh0fd61352014-02-07 02:29:45 +0000708** Execute as much of a VDBE program as we can.
709** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000710*/
danielk19774adee202004-05-08 08:23:19 +0000711int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000712 Vdbe *p /* The VDBE */
713){
drhbbe879d2009-11-14 18:04:35 +0000714 Op *aOp = p->aOp; /* Copy of p->aOp */
mistachkin5f7b95f2017-02-01 23:03:54 +0000715 Op *pOp = aOp; /* Current operation */
drhb89aeb62016-01-27 15:49:32 +0000716#ifdef SQLITE_DEBUG
dan231ff4b2022-12-02 20:32:22 +0000717 Op *pOrigOp; /* Value of pOp at the top of the loop */
drhdef19e32016-01-27 16:26:25 +0000718 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
drhf26bad62022-12-22 21:32:58 +0000719 u8 iCompareIsInit = 0; /* iCompare is initialized */
drhb89aeb62016-01-27 15:49:32 +0000720#endif
drhb86ccfb2003-01-28 23:13:10 +0000721 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000722 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000723 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000724 u8 encoding = ENC(db); /* The database encoding */
drh0f825a72016-08-13 14:17:02 +0000725 int iCompare = 0; /* Result of last comparison */
drhd1d89142020-07-06 12:13:05 +0000726 u64 nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000727#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhd1d89142020-07-06 12:13:05 +0000728 u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000729#endif
drha6c2ed92009-11-14 23:22:23 +0000730 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000731 Mem *pIn1 = 0; /* 1st input operand */
732 Mem *pIn2 = 0; /* 2nd input operand */
733 Mem *pIn3 = 0; /* 3rd input operand */
734 Mem *pOut = 0; /* Output operand */
dan231ff4b2022-12-02 20:32:22 +0000735#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
736 u64 *pnCycle = 0;
drhb86ccfb2003-01-28 23:13:10 +0000737#endif
drh856c1032009-06-02 15:21:42 +0000738 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000739
drh66181ce2022-03-31 20:04:49 +0000740 assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */
drhde5f3af2022-12-23 11:46:57 +0000741 if( DbMaskNonZero(p->lockMask) ){
742 sqlite3VdbeEnter(p);
743 }
drh82642f82019-02-12 22:58:32 +0000744#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
745 if( db->xProgress ){
746 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
747 assert( 0 < db->nProgressOps );
748 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
749 }else{
drhd1d89142020-07-06 12:13:05 +0000750 nProgressLimit = LARGEST_UINT64;
drh82642f82019-02-12 22:58:32 +0000751 }
752#endif
danielk19772e588c72005-12-09 14:25:08 +0000753 if( p->rc==SQLITE_NOMEM ){
754 /* This happens if a malloc() inside a call to sqlite3_column_text() or
755 ** sqlite3_column_text16() failed. */
756 goto no_mem;
757 }
drhcbd8db32015-08-20 17:18:32 +0000758 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
drha5f3fb32020-06-03 19:28:10 +0000759 testcase( p->rc!=SQLITE_OK );
760 p->rc = SQLITE_OK;
drh1713afb2013-06-28 01:24:57 +0000761 assert( p->bIsReader || p->readOnly!=0 );
drh95a7b3e2013-09-16 12:57:19 +0000762 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000763 assert( p->explain==0 );
drha4afb652005-07-09 02:16:02 +0000764 db->busyHandler.nBusy = 0;
dan892edb62020-03-30 13:35:05 +0000765 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000766 sqlite3VdbeIOTraceSql(p);
drh3c23a882007-01-09 14:01:13 +0000767#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000768 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000769 if( p->pc==0
770 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
771 ){
drh3c23a882007-01-09 14:01:13 +0000772 int i;
drh84e55a82013-11-13 17:58:23 +0000773 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000774 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000775 if( p->db->flags & SQLITE_VdbeListing ){
776 printf("VDBE Program Listing:\n");
777 for(i=0; i<p->nOp; i++){
778 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
779 }
drh3c23a882007-01-09 14:01:13 +0000780 }
drh84e55a82013-11-13 17:58:23 +0000781 if( p->db->flags & SQLITE_VdbeEQP ){
782 for(i=0; i<p->nOp; i++){
783 if( aOp[i].opcode==OP_Explain ){
784 if( once ) printf("VDBE Query Plan:\n");
785 printf("%s\n", aOp[i].p4.z);
786 once = 0;
787 }
788 }
789 }
790 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000791 }
danielk19772d1d86f2008-06-20 14:59:51 +0000792 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000793#endif
drh9467abf2016-02-17 18:44:11 +0000794 for(pOp=&aOp[p->pc]; 1; pOp++){
795 /* Errors are detected by individual opcodes, with an immediate
796 ** jumps to abort_due_to_error. */
797 assert( rc==SQLITE_OK );
798
drhf56fa462015-04-13 21:39:54 +0000799 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
drhbf159fa2013-06-25 22:01:22 +0000800 nVmStep++;
dan231ff4b2022-12-02 20:32:22 +0000801#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
dan7f4b0662022-12-07 20:09:54 +0000802 pOp->nExec++;
803 pnCycle = &pOp->nCycle;
dan231ff4b2022-12-02 20:32:22 +0000804# ifdef VDBE_PROFILE
dan7f4b0662022-12-07 20:09:54 +0000805 if( sqlite3NProfileCnt==0 )
dan231ff4b2022-12-02 20:32:22 +0000806# endif
dan7f4b0662022-12-07 20:09:54 +0000807 *pnCycle -= sqlite3Hwtime();
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
dan231ff4b2022-12-02 20:32:22 +0000869#ifdef SQLITE_DEBUG
drh6dc41482015-04-16 17:31:02 +0000870 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);
drhd549a702022-04-14 18:19:06 +0000986 goto jump_to_p2_and_check_for_interrupt;
drh8c74a8c2002-08-25 19:20:40 +0000987}
988
drhe603ab02022-04-07 19:06:31 +0000989/* Opcode: Return P1 P2 P3 * *
drh8c74a8c2002-08-25 19:20:40 +0000990**
drh2bd9f442022-04-17 20:30:52 +0000991** Jump to the address stored in register P1. If P1 is a return address
992** register, then this accomplishes a return from a subroutine.
drh6134b2d2022-04-11 17:27:38 +0000993**
drh2bd9f442022-04-17 20:30:52 +0000994** If P3 is 1, then the jump is only taken if register P1 holds an integer
995** values, otherwise execution falls through to the next opcode, and the
996** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
997** integer or else an assert() is raised. P3 should be set to 1 when
998** this opcode is used in combination with OP_BeginSubrtn, and set to 0
999** otherwise.
1000**
1001** The value in register P1 is unchanged by this opcode.
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
drh2591cfb2022-06-22 14:25:12 +00001007** in the subroutine from which this opcode is returning. Thus the P2
drhe603ab02022-04-07 19:06:31 +00001008** value is a byte-code indentation hint. See tag-20220407a in
1009** wherecode.c and shell.c.
drh8c74a8c2002-08-25 19:20:40 +00001010*/
drh2eb95372008-06-06 15:04:36 +00001011case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001012 pIn1 = &aMem[pOp->p1];
drh2bd9f442022-04-17 20:30:52 +00001013 if( pIn1->flags & MEM_Int ){
drh6dab33b2022-04-21 19:25:51 +00001014 if( pOp->p3 ){ VdbeBranchTaken(1, 2); }
drh2bd9f442022-04-17 20:30:52 +00001015 pOp = &aOp[pIn1->u.i];
1016 }else if( ALWAYS(pOp->p3) ){
1017 VdbeBranchTaken(0, 2);
1018 }
drh8c74a8c2002-08-25 19:20:40 +00001019 break;
1020}
1021
drhed71a832014-02-07 19:18:10 +00001022/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +00001023**
drh5dad9a32014-07-25 18:37:42 +00001024** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +00001025** located at address P3.
1026**
drh5dad9a32014-07-25 18:37:42 +00001027** If P2!=0 then the coroutine implementation immediately follows
1028** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +00001029** address P2.
drh5dad9a32014-07-25 18:37:42 +00001030**
1031** See also: EndCoroutine
drh81cf13e2014-02-07 18:27:53 +00001032*/
1033case OP_InitCoroutine: { /* jump */
drh9f6168b2016-03-19 23:32:58 +00001034 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drhed71a832014-02-07 19:18:10 +00001035 assert( pOp->p2>=0 && pOp->p2<p->nOp );
1036 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +00001037 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +00001038 assert( !VdbeMemDynamic(pOut) );
1039 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +00001040 pOut->flags = MEM_Int;
drhd549a702022-04-14 18:19:06 +00001041 if( pOp->p2==0 ) break;
1042
1043 /* Most jump operations do a goto to this spot in order to update
1044 ** the pOp pointer. */
1045jump_to_p2:
drh207f6262022-05-03 12:11:16 +00001046 assert( pOp->p2>0 ); /* There are never any jumps to instruction 0 */
1047 assert( pOp->p2<p->nOp ); /* Jumps must be in range */
drhd549a702022-04-14 18:19:06 +00001048 pOp = &aOp[pOp->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +00001049 break;
1050}
1051
1052/* Opcode: EndCoroutine P1 * * * *
1053**
drhbc5cf382014-08-06 01:08:07 +00001054** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +00001055** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +00001056** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +00001057**
1058** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +00001059*/
1060case OP_EndCoroutine: { /* in1 */
1061 VdbeOp *pCaller;
1062 pIn1 = &aMem[pOp->p1];
1063 assert( pIn1->flags==MEM_Int );
1064 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
1065 pCaller = &aOp[pIn1->u.i];
1066 assert( pCaller->opcode==OP_Yield );
1067 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
drhf56fa462015-04-13 21:39:54 +00001068 pOp = &aOp[pCaller->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +00001069 pIn1->flags = MEM_Undefined;
1070 break;
1071}
1072
1073/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +00001074**
drh5dad9a32014-07-25 18:37:42 +00001075** Swap the program counter with the value in register P1. This
1076** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +00001077**
drh5dad9a32014-07-25 18:37:42 +00001078** If the coroutine that is launched by this instruction ends with
1079** Yield or Return then continue to the next instruction. But if
1080** the coroutine launched by this instruction ends with
1081** EndCoroutine, then jump to P2 rather than continuing with the
1082** next instruction.
1083**
1084** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +00001085*/
drh81cf13e2014-02-07 18:27:53 +00001086case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +00001087 int pcDest;
drh3c657212009-11-17 23:59:58 +00001088 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +00001089 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +00001090 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +00001091 pcDest = (int)pIn1->u.i;
drhf56fa462015-04-13 21:39:54 +00001092 pIn1->u.i = (int)(pOp - aOp);
drhe00ee6e2008-06-20 15:24:01 +00001093 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +00001094 pOp = &aOp[pcDest];
drhe00ee6e2008-06-20 15:24:01 +00001095 break;
1096}
1097
drhf9c8ce32013-11-05 13:33:55 +00001098/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00001099** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +00001100**
drhef8662b2011-06-20 21:47:58 +00001101** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +00001102** parameter P1, P2, and P4 as if this were a Halt instruction. If the
1103** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +00001104** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +00001105*/
1106case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +00001107 pIn3 = &aMem[pOp->p3];
drh4031baf2018-05-28 17:31:20 +00001108#ifdef SQLITE_DEBUG
1109 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
1110#endif
drh5053a792009-02-20 03:02:23 +00001111 if( (pIn3->flags & MEM_Null)==0 ) break;
1112 /* Fall through into OP_Halt */
drh08b92082020-08-10 14:18:00 +00001113 /* no break */ deliberate_fall_through
drh5053a792009-02-20 03:02:23 +00001114}
drhe00ee6e2008-06-20 15:24:01 +00001115
drhf9c8ce32013-11-05 13:33:55 +00001116/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001117**
drh3d4501e2008-12-04 20:40:10 +00001118** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +00001119** automatically.
drhb19a2bc2001-09-16 00:13:26 +00001120**
drh92f02c32004-09-02 14:57:08 +00001121** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
1122** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
1123** For errors, it can be some other value. If P1!=0 then P2 will determine
1124** whether or not to rollback the current transaction. Do not rollback
1125** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
1126** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +00001127** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +00001128**
drh66a51672008-01-03 00:01:23 +00001129** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +00001130**
drhf9c8ce32013-11-05 13:33:55 +00001131** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
1132**
1133** 0: (no change)
1134** 1: NOT NULL contraint failed: P4
1135** 2: UNIQUE constraint failed: P4
1136** 3: CHECK constraint failed: P4
1137** 4: FOREIGN KEY constraint failed: P4
1138**
1139** If P5 is not zero and P4 is NULL, then everything after the ":" is
1140** omitted.
1141**
drh9cfcf5d2002-01-29 18:41:24 +00001142** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +00001143** every program. So a jump past the last instruction of the program
1144** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +00001145*/
drh9cbf3422008-01-17 16:22:13 +00001146case OP_Halt: {
drhf56fa462015-04-13 21:39:54 +00001147 VdbeFrame *pFrame;
1148 int pcx;
drhf9c8ce32013-11-05 13:33:55 +00001149
drh4031baf2018-05-28 17:31:20 +00001150#ifdef SQLITE_DEBUG
1151 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
1152#endif
drh8bb93da2022-04-03 20:39:48 +00001153 if( p->pFrame && pOp->p1==SQLITE_OK ){
dan2832ad42009-08-31 15:27:27 +00001154 /* Halt the sub-program. Return control to the parent frame. */
drhf56fa462015-04-13 21:39:54 +00001155 pFrame = p->pFrame;
dan165921a2009-08-28 18:53:45 +00001156 p->pFrame = pFrame->pParent;
1157 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +00001158 sqlite3VdbeSetChanges(db, p->nChange);
drhf56fa462015-04-13 21:39:54 +00001159 pcx = sqlite3VdbeFrameRestore(pFrame);
dan165921a2009-08-28 18:53:45 +00001160 if( pOp->p2==OE_Ignore ){
drhf56fa462015-04-13 21:39:54 +00001161 /* Instruction pcx is the OP_Program that invoked the sub-program
dan2832ad42009-08-31 15:27:27 +00001162 ** currently being halted. If the p2 instruction of this OP_Halt
1163 ** instruction is set to OE_Ignore, then the sub-program is throwing
1164 ** an IGNORE exception. In this case jump to the address specified
1165 ** as the p2 of the calling OP_Program. */
drhf56fa462015-04-13 21:39:54 +00001166 pcx = p->aOp[pcx].p2-1;
dan165921a2009-08-28 18:53:45 +00001167 }
drhbbe879d2009-11-14 18:04:35 +00001168 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +00001169 aMem = p->aMem;
drhf56fa462015-04-13 21:39:54 +00001170 pOp = &aOp[pcx];
dan165921a2009-08-28 18:53:45 +00001171 break;
1172 }
drh92f02c32004-09-02 14:57:08 +00001173 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +00001174 p->errorAction = (u8)pOp->p2;
drhfb4e3a32016-12-30 00:09:14 +00001175 assert( pOp->p5<=4 );
drhf9c8ce32013-11-05 13:33:55 +00001176 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +00001177 if( pOp->p5 ){
1178 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
1179 "FOREIGN KEY" };
drhd9b7ec92013-11-06 14:05:21 +00001180 testcase( pOp->p5==1 );
1181 testcase( pOp->p5==2 );
1182 testcase( pOp->p5==3 );
1183 testcase( pOp->p5==4 );
drh99f5de72016-04-30 02:59:15 +00001184 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
1185 if( pOp->p4.z ){
1186 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
1187 }
drhd9b7ec92013-11-06 14:05:21 +00001188 }else{
drh22c17b82015-05-15 04:13:15 +00001189 sqlite3VdbeError(p, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +00001190 }
drh8bb93da2022-04-03 20:39:48 +00001191 pcx = (int)(pOp - aOp);
drh99f5de72016-04-30 02:59:15 +00001192 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +00001193 }
drh92f02c32004-09-02 14:57:08 +00001194 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +00001195 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +00001196 if( rc==SQLITE_BUSY ){
drh99f5de72016-04-30 02:59:15 +00001197 p->rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00001198 }else{
drhd91c1a12013-02-09 13:58:25 +00001199 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
dancb3e4b72013-07-03 19:53:05 +00001200 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +00001201 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +00001202 }
drh900b31e2007-08-28 02:27:51 +00001203 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +00001204}
drhc61053b2000-06-04 12:58:36 +00001205
drh4c583122008-01-04 22:01:03 +00001206/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001207** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +00001208**
drh9cbf3422008-01-17 16:22:13 +00001209** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +00001210*/
drh27a348c2015-04-13 19:14:06 +00001211case OP_Integer: { /* out2 */
1212 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001213 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +00001214 break;
1215}
1216
drh4c583122008-01-04 22:01:03 +00001217/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001218** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +00001219**
drh66a51672008-01-03 00:01:23 +00001220** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +00001221** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +00001222*/
drh27a348c2015-04-13 19:14:06 +00001223case OP_Int64: { /* out2 */
1224 pOut = out2Prerelease(p, pOp);
danielk19772dca4ac2008-01-03 11:50:29 +00001225 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +00001226 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +00001227 break;
1228}
drh4f26d6c2004-05-26 23:25:30 +00001229
drh13573c72010-01-12 17:04:07 +00001230#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001231/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001232** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001233**
drh4c583122008-01-04 22:01:03 +00001234** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001235** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001236*/
drh27a348c2015-04-13 19:14:06 +00001237case OP_Real: { /* same as TK_FLOAT, out2 */
1238 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001239 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001240 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001241 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001242 break;
1243}
drh13573c72010-01-12 17:04:07 +00001244#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001245
drh3c84ddf2008-01-09 02:15:38 +00001246/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001247** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001248**
drh66a51672008-01-03 00:01:23 +00001249** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhf07cf6e2015-03-06 16:45:16 +00001250** into a String opcode before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001251** this transformation, the length of string P4 is computed and stored
1252** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001253*/
drh27a348c2015-04-13 19:14:06 +00001254case OP_String8: { /* same as TK_STRING, out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001255 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001256 pOut = out2Prerelease(p, pOp);
drhea678832008-12-10 19:26:22 +00001257 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001258
1259#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001260 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001261 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
drh2f555112016-04-30 18:10:34 +00001262 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
drhdbdddc92019-02-21 16:41:34 +00001263 if( rc ) goto too_big;
drh4c583122008-01-04 22:01:03 +00001264 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001265 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001266 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001267 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001268 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001269 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001270 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001271 }
drh66a51672008-01-03 00:01:23 +00001272 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001273 pOp->p4.z = pOut->z;
1274 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001275 }
danielk197793758c82005-01-21 08:13:14 +00001276#endif
drhbb4957f2008-03-20 14:03:29 +00001277 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001278 goto too_big;
1279 }
drhec722c12019-09-17 21:28:54 +00001280 pOp->opcode = OP_String;
drh2f555112016-04-30 18:10:34 +00001281 assert( rc==SQLITE_OK );
drhcbd2da92007-12-17 16:20:06 +00001282 /* Fall through to the next case, OP_String */
drh08b92082020-08-10 14:18:00 +00001283 /* no break */ deliberate_fall_through
danielk1977cbb18d22004-05-28 11:37:27 +00001284}
drhf4479502004-05-27 03:12:53 +00001285
drhf07cf6e2015-03-06 16:45:16 +00001286/* Opcode: String P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00001287** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001288**
drh9cbf3422008-01-17 16:22:13 +00001289** The string value P4 of length P1 (bytes) is stored in register P2.
drhf07cf6e2015-03-06 16:45:16 +00001290**
drh44aebff2016-05-02 10:25:42 +00001291** If P3 is not zero and the content of register P3 is equal to P5, then
drha9c18a92015-03-06 20:49:52 +00001292** the datatype of the register P2 is converted to BLOB. The content is
1293** the same sequence of bytes, it is merely interpreted as a BLOB instead
drh44aebff2016-05-02 10:25:42 +00001294** of a string, as if it had been CAST. In other words:
1295**
1296** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
drhf4479502004-05-27 03:12:53 +00001297*/
drh27a348c2015-04-13 19:14:06 +00001298case OP_String: { /* out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001299 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001300 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001301 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1302 pOut->z = pOp->p4.z;
1303 pOut->n = pOp->p1;
1304 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001305 UPDATE_MAX_BLOBSIZE(pOut);
drh41d2e662015-12-01 21:23:07 +00001306#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh44aebff2016-05-02 10:25:42 +00001307 if( pOp->p3>0 ){
drh9f6168b2016-03-19 23:32:58 +00001308 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhf07cf6e2015-03-06 16:45:16 +00001309 pIn3 = &aMem[pOp->p3];
1310 assert( pIn3->flags & MEM_Int );
drh44aebff2016-05-02 10:25:42 +00001311 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
drhf07cf6e2015-03-06 16:45:16 +00001312 }
drh41d2e662015-12-01 21:23:07 +00001313#endif
danielk1977c572ef72004-05-27 09:28:41 +00001314 break;
1315}
1316
drh2bd9f442022-04-17 20:30:52 +00001317/* Opcode: BeginSubrtn * P2 * * *
1318** Synopsis: r[P2]=NULL
1319**
1320** Mark the beginning of a subroutine that can be entered in-line
1321** or that can be called using OP_Gosub. The subroutine should
1322** be terminated by an OP_Return instruction that has a P1 operand that
1323** is the same as the P2 operand to this opcode and that has P3 set to 1.
1324** If the subroutine is entered in-line, then the OP_Return will simply
1325** fall through. But if the subroutine is entered using OP_Gosub, then
1326** the OP_Return will jump back to the first instruction after the OP_Gosub.
1327**
1328** This routine works by loading a NULL into the P2 register. When the
1329** return address register contains a NULL, the OP_Return instruction is
1330** a no-op that simply falls through to the next instruction (assuming that
1331** the OP_Return opcode has a P3 value of 1). Thus if the subroutine is
1332** entered in-line, then the OP_Return will cause in-line execution to
1333** continue. But if the subroutine is entered via OP_Gosub, then the
1334** OP_Return will cause a return to the address following the OP_Gosub.
1335**
1336** This opcode is identical to OP_Null. It has a different name
1337** only to make the byte code easier to read and verify.
1338*/
drh053a1282012-09-19 21:15:46 +00001339/* Opcode: Null P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001340** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001341**
drhb8475df2011-12-09 16:21:19 +00001342** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001343** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001344** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001345** set to NULL.
1346**
1347** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1348** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1349** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001350*/
drh2bd9f442022-04-17 20:30:52 +00001351case OP_BeginSubrtn:
drh27a348c2015-04-13 19:14:06 +00001352case OP_Null: { /* out2 */
drhb8475df2011-12-09 16:21:19 +00001353 int cnt;
drh053a1282012-09-19 21:15:46 +00001354 u16 nullFlag;
drh27a348c2015-04-13 19:14:06 +00001355 pOut = out2Prerelease(p, pOp);
drhb8475df2011-12-09 16:21:19 +00001356 cnt = pOp->p3-pOp->p2;
drh9f6168b2016-03-19 23:32:58 +00001357 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001358 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drh2a1df932016-09-30 17:46:44 +00001359 pOut->n = 0;
drh2c885d02018-07-07 19:36:04 +00001360#ifdef SQLITE_DEBUG
1361 pOut->uTemp = 0;
1362#endif
drhb8475df2011-12-09 16:21:19 +00001363 while( cnt>0 ){
1364 pOut++;
1365 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001366 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001367 pOut->flags = nullFlag;
drh2a1df932016-09-30 17:46:44 +00001368 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001369 cnt--;
1370 }
drhf0863fe2005-06-12 21:35:51 +00001371 break;
1372}
1373
drh05a86c52014-02-16 01:55:49 +00001374/* Opcode: SoftNull P1 * * * *
drh72e26de2016-08-24 21:24:04 +00001375** Synopsis: r[P1]=NULL
drh05a86c52014-02-16 01:55:49 +00001376**
1377** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1378** instruction, but do not free any string or blob memory associated with
1379** the register, so that if the value was a string or blob that was
1380** previously copied using OP_SCopy, the copies will continue to be valid.
1381*/
1382case OP_SoftNull: {
drh9f6168b2016-03-19 23:32:58 +00001383 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh05a86c52014-02-16 01:55:49 +00001384 pOut = &aMem[pOp->p1];
drhe2bc6552017-04-17 20:50:34 +00001385 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
drh05a86c52014-02-16 01:55:49 +00001386 break;
1387}
drhf0863fe2005-06-12 21:35:51 +00001388
drha5750cf2014-02-07 13:20:31 +00001389/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001390** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001391**
drh9de221d2008-01-05 06:51:30 +00001392** P4 points to a blob of data P1 bytes long. Store this
drh50fb7e02021-12-06 20:16:53 +00001393** blob in register P2. If P4 is a NULL pointer, then construct
1394** a zero-filled blob that is P1 bytes long in P2.
danielk1977c572ef72004-05-27 09:28:41 +00001395*/
drh27a348c2015-04-13 19:14:06 +00001396case OP_Blob: { /* out2 */
drhcbd2da92007-12-17 16:20:06 +00001397 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh27a348c2015-04-13 19:14:06 +00001398 pOut = out2Prerelease(p, pOp);
drh50fb7e02021-12-06 20:16:53 +00001399 if( pOp->p4.z==0 ){
1400 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1);
1401 if( sqlite3VdbeMemExpandBlob(pOut) ) goto no_mem;
1402 }else{
1403 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
1404 }
drh9de221d2008-01-05 06:51:30 +00001405 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001406 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001407 break;
1408}
1409
drheaf52d82010-05-12 13:50:23 +00001410/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001411** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001412**
drheaf52d82010-05-12 13:50:23 +00001413** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001414**
drh0fd61352014-02-07 02:29:45 +00001415** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001416** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001417*/
drh27a348c2015-04-13 19:14:06 +00001418case OP_Variable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00001419 Mem *pVar; /* Value being transferred */
1420
drheaf52d82010-05-12 13:50:23 +00001421 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh9bf755c2016-12-23 03:59:31 +00001422 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
drheaf52d82010-05-12 13:50:23 +00001423 pVar = &p->aVar[pOp->p1 - 1];
1424 if( sqlite3VdbeMemTooBig(pVar) ){
1425 goto too_big;
drh023ae032007-05-08 12:12:16 +00001426 }
drh7441df72017-01-09 19:27:04 +00001427 pOut = &aMem[pOp->p2];
drhe0f20b42019-04-01 20:57:11 +00001428 if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
1429 memcpy(pOut, pVar, MEMCELLSIZE);
1430 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
1431 pOut->flags |= MEM_Static|MEM_FromBind;
drheaf52d82010-05-12 13:50:23 +00001432 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001433 break;
1434}
danielk1977295ba552004-05-19 10:34:51 +00001435
drhb21e7c72008-06-22 12:37:57 +00001436/* Opcode: Move P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001437** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001438**
drh079a3072014-03-19 14:10:55 +00001439** Move the P3 values in register P1..P1+P3-1 over into
1440** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001441** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001442** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1443** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001444*/
drhe1349cb2008-04-01 00:36:10 +00001445case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001446 int n; /* Number of registers left to copy */
1447 int p1; /* Register to copy from */
1448 int p2; /* Register to copy to */
1449
drhe09f43f2013-11-21 04:18:31 +00001450 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001451 p1 = pOp->p1;
1452 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001453 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001454 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001455
drha6c2ed92009-11-14 23:22:23 +00001456 pIn1 = &aMem[p1];
1457 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001458 do{
drh9f6168b2016-03-19 23:32:58 +00001459 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
1460 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001461 assert( memIsValid(pIn1) );
1462 memAboutToChange(p, pOut);
drh17bcb102014-09-18 21:25:33 +00001463 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001464#ifdef SQLITE_DEBUG
drh4cbd8472020-01-02 15:02:08 +00001465 pIn1->pScopyFrom = 0;
1466 { int i;
1467 for(i=1; i<p->nMem; i++){
1468 if( aMem[i].pScopyFrom==pIn1 ){
1469 aMem[i].pScopyFrom = pOut;
1470 }
1471 }
drh52043d72011-08-03 16:40:15 +00001472 }
1473#endif
drhbd6789e2015-04-28 14:00:02 +00001474 Deephemeralize(pOut);
drhb21e7c72008-06-22 12:37:57 +00001475 REGISTER_TRACE(p2++, pOut);
1476 pIn1++;
1477 pOut++;
drh079a3072014-03-19 14:10:55 +00001478 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001479 break;
1480}
1481
drhe5dea282022-06-09 17:17:14 +00001482/* Opcode: Copy P1 P2 P3 * P5
drh4eded602013-12-20 15:59:20 +00001483** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001484**
drhe8e4af72012-09-21 00:04:28 +00001485** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001486**
drhe5dea282022-06-09 17:17:14 +00001487** If the 0x0002 bit of P5 is set then also clear the MEM_Subtype flag in the
1488** destination. The 0x0001 bit of P5 indicates that this Copy opcode cannot
1489** be merged. The 0x0001 bit is used by the query planner and does not
1490** come into play during query execution.
1491**
drhb1fdb2a2008-01-05 04:06:03 +00001492** This instruction makes a deep copy of the value. A duplicate
1493** is made of any string or blob constant. See also OP_SCopy.
1494*/
drhe8e4af72012-09-21 00:04:28 +00001495case OP_Copy: {
1496 int n;
1497
1498 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001499 pIn1 = &aMem[pOp->p1];
1500 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001501 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001502 while( 1 ){
drh58773a52018-06-12 13:52:23 +00001503 memAboutToChange(p, pOut);
drhe8e4af72012-09-21 00:04:28 +00001504 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1505 Deephemeralize(pOut);
drhe5dea282022-06-09 17:17:14 +00001506 if( (pOut->flags & MEM_Subtype)!=0 && (pOp->p5 & 0x0002)!=0 ){
1507 pOut->flags &= ~MEM_Subtype;
1508 }
drh953f7612012-12-07 22:18:54 +00001509#ifdef SQLITE_DEBUG
1510 pOut->pScopyFrom = 0;
1511#endif
drhe8e4af72012-09-21 00:04:28 +00001512 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1513 if( (n--)==0 ) break;
1514 pOut++;
1515 pIn1++;
1516 }
drhe1349cb2008-04-01 00:36:10 +00001517 break;
1518}
1519
drhb1fdb2a2008-01-05 04:06:03 +00001520/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001521** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001522**
drh9cbf3422008-01-17 16:22:13 +00001523** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001524**
1525** This instruction makes a shallow copy of the value. If the value
1526** is a string or blob, then the copy is only a pointer to the
1527** original and hence if the original changes so will the copy.
1528** Worse, if the original is deallocated, the copy becomes invalid.
1529** Thus the program must guarantee that the original will not change
1530** during the lifetime of the copy. Use OP_Copy to make a complete
1531** copy.
1532*/
drh26198bb2013-10-31 11:15:09 +00001533case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001534 pIn1 = &aMem[pOp->p1];
1535 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001536 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001537 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001538#ifdef SQLITE_DEBUG
drh58773a52018-06-12 13:52:23 +00001539 pOut->pScopyFrom = pIn1;
1540 pOut->mScopyFlags = pIn1->flags;
drh2b4ded92010-09-27 21:09:31 +00001541#endif
drh5e00f6c2001-09-13 13:46:56 +00001542 break;
1543}
drh75897232000-05-29 14:26:00 +00001544
drhfed7ac62015-10-15 18:04:59 +00001545/* Opcode: IntCopy P1 P2 * * *
1546** Synopsis: r[P2]=r[P1]
1547**
1548** Transfer the integer value held in register P1 into register P2.
1549**
1550** This is an optimized version of SCopy that works only for integer
1551** values.
1552*/
1553case OP_IntCopy: { /* out2 */
1554 pIn1 = &aMem[pOp->p1];
1555 assert( (pIn1->flags & MEM_Int)!=0 );
1556 pOut = &aMem[pOp->p2];
1557 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1558 break;
1559}
1560
drh3b26b2b2021-12-01 19:17:14 +00001561/* Opcode: FkCheck * * * * *
drh18e56072021-01-31 15:50:36 +00001562**
drh3b26b2b2021-12-01 19:17:14 +00001563** Halt with an SQLITE_CONSTRAINT error if there are any unresolved
1564** foreign key constraint violations. If there are no foreign key
1565** constraint violations, this is a no-op.
drh18e56072021-01-31 15:50:36 +00001566**
drh3b26b2b2021-12-01 19:17:14 +00001567** FK constraint violations are also checked when the prepared statement
1568** exits. This opcode is used to raise foreign key constraint errors prior
1569** to returning results such as a row change count or the result of a
1570** RETURNING clause.
drh18e56072021-01-31 15:50:36 +00001571*/
drh3b26b2b2021-12-01 19:17:14 +00001572case OP_FkCheck: {
drh18e56072021-01-31 15:50:36 +00001573 if( (rc = sqlite3VdbeCheckFk(p,0))!=SQLITE_OK ){
1574 goto abort_due_to_error;
1575 }
drh3b26b2b2021-12-01 19:17:14 +00001576 break;
drh18e56072021-01-31 15:50:36 +00001577}
1578
drh9cbf3422008-01-17 16:22:13 +00001579/* Opcode: ResultRow P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001580** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001581**
shane21e7feb2008-05-30 15:59:49 +00001582** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001583** results. This opcode causes the sqlite3_step() call to terminate
1584** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001585** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001586** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001587*/
drh9cbf3422008-01-17 16:22:13 +00001588case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001589 assert( p->nResColumn==pOp->p2 );
drh9ce612a2021-04-05 22:30:56 +00001590 assert( pOp->p1>0 || CORRUPT_DB );
drh9f6168b2016-03-19 23:32:58 +00001591 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001592
drhd4e70eb2008-01-02 00:34:36 +00001593 p->cacheCtr = (p->cacheCtr + 2)|1;
drhedc27132022-12-22 18:44:39 +00001594 p->pResultRow = &aMem[pOp->p1];
drh02ff7472019-12-31 12:18:24 +00001595#ifdef SQLITE_DEBUG
drh3b8b5be2022-04-01 20:19:36 +00001596 {
drhedc27132022-12-22 18:44:39 +00001597 Mem *pMem = p->pResultRow;
drh3b8b5be2022-04-01 20:19:36 +00001598 int i;
1599 for(i=0; i<pOp->p2; i++){
1600 assert( memIsValid(&pMem[i]) );
1601 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
1602 /* The registers in the result will not be used again when the
1603 ** prepared statement restarts. This is because sqlite3_column()
1604 ** APIs might have caused type conversions of made other changes to
1605 ** the register values. Therefore, we can go ahead and break any
1606 ** OP_SCopy dependencies. */
1607 pMem[i].pScopyFrom = 0;
1608 }
drhd4e70eb2008-01-02 00:34:36 +00001609 }
drh3b8b5be2022-04-01 20:19:36 +00001610#endif
drh28039692008-03-17 16:54:01 +00001611 if( db->mallocFailed ) goto no_mem;
drh3d2a5292016-07-13 22:55:01 +00001612 if( db->mTrace & SQLITE_TRACE_ROW ){
drh08b92082020-08-10 14:18:00 +00001613 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
drh3d2a5292016-07-13 22:55:01 +00001614 }
drhf56fa462015-04-13 21:39:54 +00001615 p->pc = (int)(pOp - aOp) + 1;
drhd4e70eb2008-01-02 00:34:36 +00001616 rc = SQLITE_ROW;
1617 goto vdbe_return;
1618}
1619
drh5b6afba2008-01-05 16:29:28 +00001620/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001621** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001622**
drh5b6afba2008-01-05 16:29:28 +00001623** Add the text in register P1 onto the end of the text in
1624** register P2 and store the result in register P3.
1625** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001626**
1627** P3 = P2 || P1
1628**
1629** It is illegal for P1 and P3 to be the same register. Sometimes,
1630** if P3 is the same register as P2, the implementation is able
1631** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001632*/
drh5b6afba2008-01-05 16:29:28 +00001633case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh8a7e11f2019-05-01 15:32:40 +00001634 i64 nByte; /* Total size of the output string or blob */
1635 u16 flags1; /* Initial flags for P1 */
1636 u16 flags2; /* Initial flags for P2 */
danielk19778a6b5412004-05-24 07:04:25 +00001637
drh3c657212009-11-17 23:59:58 +00001638 pIn1 = &aMem[pOp->p1];
1639 pIn2 = &aMem[pOp->p2];
1640 pOut = &aMem[pOp->p3];
drh8a7e11f2019-05-01 15:32:40 +00001641 testcase( pOut==pIn2 );
danielk1977a7a8e142008-02-13 18:25:27 +00001642 assert( pIn1!=pOut );
drh8a7e11f2019-05-01 15:32:40 +00001643 flags1 = pIn1->flags;
1644 testcase( flags1 & MEM_Null );
1645 testcase( pIn2->flags & MEM_Null );
1646 if( (flags1 | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001647 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001648 break;
drh5e00f6c2001-09-13 13:46:56 +00001649 }
drh8a7e11f2019-05-01 15:32:40 +00001650 if( (flags1 & (MEM_Str|MEM_Blob))==0 ){
1651 if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001652 flags1 = pIn1->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001653 }else if( (flags1 & MEM_Zero)!=0 ){
1654 if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001655 flags1 = pIn1->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001656 }
1657 flags2 = pIn2->flags;
1658 if( (flags2 & (MEM_Str|MEM_Blob))==0 ){
1659 if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001660 flags2 = pIn2->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001661 }else if( (flags2 & MEM_Zero)!=0 ){
1662 if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem;
drh01325a32019-05-02 00:52:50 +00001663 flags2 = pIn2->flags & ~MEM_Str;
drh8a7e11f2019-05-01 15:32:40 +00001664 }
drh5b6afba2008-01-05 16:29:28 +00001665 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001666 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001667 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001668 }
drh01156ec2022-06-17 21:31:30 +00001669 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001670 goto no_mem;
1671 }
drhc91b2fd2014-03-01 18:13:23 +00001672 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001673 if( pOut!=pIn2 ){
1674 memcpy(pOut->z, pIn2->z, pIn2->n);
drh8a7e11f2019-05-01 15:32:40 +00001675 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
1676 pIn2->flags = flags2;
danielk1977a7a8e142008-02-13 18:25:27 +00001677 }
1678 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh8a7e11f2019-05-01 15:32:40 +00001679 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
1680 pIn1->flags = flags1;
drh01156ec2022-06-17 21:31:30 +00001681 if( encoding>SQLITE_UTF8 ) nByte &= ~1;
drh81316f82013-10-29 20:40:47 +00001682 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001683 pOut->z[nByte+1] = 0;
1684 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001685 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001686 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001687 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001688 break;
1689}
drh75897232000-05-29 14:26:00 +00001690
drh3c84ddf2008-01-09 02:15:38 +00001691/* Opcode: Add P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001692** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001693**
drh60a713c2008-01-21 16:22:45 +00001694** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001695** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001696** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001697*/
drh3c84ddf2008-01-09 02:15:38 +00001698/* Opcode: Multiply P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001699** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001700**
drh3c84ddf2008-01-09 02:15:38 +00001701**
shane21e7feb2008-05-30 15:59:49 +00001702** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001703** and store the result in register P3.
1704** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001705*/
drh3c84ddf2008-01-09 02:15:38 +00001706/* Opcode: Subtract P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001707** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001708**
drh60a713c2008-01-21 16:22:45 +00001709** Subtract the value in register P1 from the value in register P2
1710** and store the result in register P3.
1711** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001712*/
drh9cbf3422008-01-17 16:22:13 +00001713/* Opcode: Divide P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001714** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001715**
drh60a713c2008-01-21 16:22:45 +00001716** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001717** and store the result in register P3 (P3=P2/P1). If the value in
1718** register P1 is zero, then the result is NULL. If either input is
1719** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001720*/
drh9cbf3422008-01-17 16:22:13 +00001721/* Opcode: Remainder P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001722** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001723**
drh40864a12013-11-15 18:58:37 +00001724** Compute the remainder after integer register P2 is divided by
1725** register P1 and store the result in register P3.
1726** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001727** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001728*/
drh5b6afba2008-01-05 16:29:28 +00001729case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1730case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1731case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1732case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1733case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drh3d1d90a2014-03-24 15:00:15 +00001734 u16 type1; /* Numeric type of left operand */
1735 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001736 i64 iA; /* Integer value of left operand */
1737 i64 iB; /* Integer value of right operand */
1738 double rA; /* Real value of left operand */
1739 double rB; /* Real value of right operand */
1740
drh3c657212009-11-17 23:59:58 +00001741 pIn1 = &aMem[pOp->p1];
drh3cf46ee2022-08-03 19:53:54 +00001742 type1 = pIn1->flags;
drh3c657212009-11-17 23:59:58 +00001743 pIn2 = &aMem[pOp->p2];
drh3cf46ee2022-08-03 19:53:54 +00001744 type2 = pIn2->flags;
drh3c657212009-11-17 23:59:58 +00001745 pOut = &aMem[pOp->p3];
drh3d1d90a2014-03-24 15:00:15 +00001746 if( (type1 & type2 & MEM_Int)!=0 ){
drh3cf46ee2022-08-03 19:53:54 +00001747int_math:
drh856c1032009-06-02 15:21:42 +00001748 iA = pIn1->u.i;
1749 iB = pIn2->u.i;
drh5e00f6c2001-09-13 13:46:56 +00001750 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001751 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1752 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1753 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001754 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001755 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001756 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001757 iB /= iA;
drh75897232000-05-29 14:26:00 +00001758 break;
1759 }
drhbf4133c2001-10-13 02:59:08 +00001760 default: {
drh856c1032009-06-02 15:21:42 +00001761 if( iA==0 ) goto arithmetic_result_is_null;
1762 if( iA==-1 ) iA = 1;
1763 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001764 break;
1765 }
drh75897232000-05-29 14:26:00 +00001766 }
drh856c1032009-06-02 15:21:42 +00001767 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001768 MemSetTypeFlag(pOut, MEM_Int);
drh3cf46ee2022-08-03 19:53:54 +00001769 }else if( ((type1 | type2) & MEM_Null)!=0 ){
drhcfcca022017-04-17 23:23:17 +00001770 goto arithmetic_result_is_null;
drh5e00f6c2001-09-13 13:46:56 +00001771 }else{
drh3cf46ee2022-08-03 19:53:54 +00001772 type1 = numericType(pIn1);
1773 type2 = numericType(pIn2);
1774 if( (type1 & type2 & MEM_Int)!=0 ) goto int_math;
drh158b9cb2011-03-05 20:59:46 +00001775fp_math:
drh856c1032009-06-02 15:21:42 +00001776 rA = sqlite3VdbeRealValue(pIn1);
1777 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001778 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001779 case OP_Add: rB += rA; break;
1780 case OP_Subtract: rB -= rA; break;
1781 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001782 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001783 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001784 if( rA==(double)0 ) goto arithmetic_result_is_null;
1785 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001786 break;
1787 }
drhbf4133c2001-10-13 02:59:08 +00001788 default: {
drhe3b89d22019-01-18 17:53:50 +00001789 iA = sqlite3VdbeIntValue(pIn1);
1790 iB = sqlite3VdbeIntValue(pIn2);
drh856c1032009-06-02 15:21:42 +00001791 if( iA==0 ) goto arithmetic_result_is_null;
1792 if( iA==-1 ) iA = 1;
1793 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001794 break;
1795 }
drh5e00f6c2001-09-13 13:46:56 +00001796 }
drhc5a7b512010-01-13 16:25:42 +00001797#ifdef SQLITE_OMIT_FLOATING_POINT
1798 pOut->u.i = rB;
1799 MemSetTypeFlag(pOut, MEM_Int);
1800#else
drh856c1032009-06-02 15:21:42 +00001801 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001802 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001803 }
drh74eaba42014-09-18 17:52:15 +00001804 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001805 MemSetTypeFlag(pOut, MEM_Real);
drhc5a7b512010-01-13 16:25:42 +00001806#endif
drh5e00f6c2001-09-13 13:46:56 +00001807 }
1808 break;
1809
drha05a7222008-01-19 03:35:58 +00001810arithmetic_result_is_null:
1811 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001812 break;
1813}
1814
drh7a957892012-02-02 17:35:43 +00001815/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001816**
drhbb6783b2017-04-29 18:02:49 +00001817** P4 is a pointer to a CollSeq object. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001818** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1819** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001820** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001821**
drh7a957892012-02-02 17:35:43 +00001822** If P1 is not zero, then it is a register that a subsequent min() or
1823** max() aggregate will set to 1 if the current row is not the minimum or
1824** maximum. The P1 register is initialized to 0 by this instruction.
1825**
danielk1977dc1bdc42004-06-11 10:51:27 +00001826** The interface used by the implementation of the aforementioned functions
1827** to retrieve the collation sequence set by this opcode is not available
drh0a0d0562015-03-12 05:08:34 +00001828** publicly. Only built-in functions have access to this feature.
danielk1977dc1bdc42004-06-11 10:51:27 +00001829*/
drh9cbf3422008-01-17 16:22:13 +00001830case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001831 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001832 if( pOp->p1 ){
1833 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1834 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001835 break;
1836}
1837
drh98757152008-01-09 23:04:12 +00001838/* Opcode: BitAnd P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001839** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001840**
drh98757152008-01-09 23:04:12 +00001841** Take the bit-wise AND of the values in register P1 and P2 and
1842** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001843** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001844*/
drh98757152008-01-09 23:04:12 +00001845/* Opcode: BitOr P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001846** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001847**
drh98757152008-01-09 23:04:12 +00001848** Take the bit-wise OR of the values in register P1 and P2 and
1849** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001850** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001851*/
drh98757152008-01-09 23:04:12 +00001852/* Opcode: ShiftLeft P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001853** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001854**
drh98757152008-01-09 23:04:12 +00001855** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001856** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001857** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001858** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001859*/
drh98757152008-01-09 23:04:12 +00001860/* Opcode: ShiftRight P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001861** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001862**
drh98757152008-01-09 23:04:12 +00001863** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001864** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001865** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001866** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001867*/
drh5b6afba2008-01-05 16:29:28 +00001868case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1869case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1870case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1871case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001872 i64 iA;
1873 u64 uA;
1874 i64 iB;
1875 u8 op;
drh6810ce62004-01-31 19:22:56 +00001876
drh3c657212009-11-17 23:59:58 +00001877 pIn1 = &aMem[pOp->p1];
1878 pIn2 = &aMem[pOp->p2];
1879 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001880 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001881 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001882 break;
1883 }
drh158b9cb2011-03-05 20:59:46 +00001884 iA = sqlite3VdbeIntValue(pIn2);
1885 iB = sqlite3VdbeIntValue(pIn1);
1886 op = pOp->opcode;
1887 if( op==OP_BitAnd ){
1888 iA &= iB;
1889 }else if( op==OP_BitOr ){
1890 iA |= iB;
1891 }else if( iB!=0 ){
1892 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1893
1894 /* If shifting by a negative amount, shift in the other direction */
1895 if( iB<0 ){
1896 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1897 op = 2*OP_ShiftLeft + 1 - op;
1898 iB = iB>(-64) ? -iB : 64;
1899 }
1900
1901 if( iB>=64 ){
1902 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1903 }else{
1904 memcpy(&uA, &iA, sizeof(uA));
1905 if( op==OP_ShiftLeft ){
1906 uA <<= iB;
1907 }else{
1908 uA >>= iB;
1909 /* Sign-extend on a right shift of a negative number */
1910 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1911 }
1912 memcpy(&iA, &uA, sizeof(iA));
1913 }
drhbf4133c2001-10-13 02:59:08 +00001914 }
drh158b9cb2011-03-05 20:59:46 +00001915 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001916 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001917 break;
1918}
1919
drh8558cde2008-01-05 05:20:10 +00001920/* Opcode: AddImm P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001921** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001922**
danielk19770cdc0222008-06-26 18:04:03 +00001923** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001924** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001925**
drh8558cde2008-01-05 05:20:10 +00001926** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001927*/
drh9cbf3422008-01-17 16:22:13 +00001928case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001929 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001930 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001931 sqlite3VdbeMemIntegerify(pIn1);
1932 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001933 break;
1934}
1935
dane5166e02019-03-19 11:56:39 +00001936/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001937**
dane5166e02019-03-19 11:56:39 +00001938** Force the value in register P1 to be an integer. If the value
1939** in P1 is not an integer and cannot be converted into an integer
1940** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001941** raise an SQLITE_MISMATCH exception.
1942*/
drh9cbf3422008-01-17 16:22:13 +00001943case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001944 pIn1 = &aMem[pOp->p1];
dane5166e02019-03-19 11:56:39 +00001945 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001946 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
dane5166e02019-03-19 11:56:39 +00001947 if( (pIn1->flags & MEM_Int)==0 ){
drhc9065332019-04-01 14:01:21 +00001948 VdbeBranchTaken(1, 2);
drh83b301b2013-11-20 00:59:02 +00001949 if( pOp->p2==0 ){
1950 rc = SQLITE_MISMATCH;
1951 goto abort_due_to_error;
1952 }else{
drhf56fa462015-04-13 21:39:54 +00001953 goto jump_to_p2;
drh83b301b2013-11-20 00:59:02 +00001954 }
drh8aff1012001-12-22 14:49:24 +00001955 }
drh8aff1012001-12-22 14:49:24 +00001956 }
drhc9065332019-04-01 14:01:21 +00001957 VdbeBranchTaken(0, 2);
dane5166e02019-03-19 11:56:39 +00001958 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001959 break;
1960}
1961
drh13573c72010-01-12 17:04:07 +00001962#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001963/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001964**
drh2133d822008-01-03 18:44:59 +00001965** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001966**
drh8a512562005-11-14 22:29:05 +00001967** This opcode is used when extracting information from a column that
1968** has REAL affinity. Such column values may still be stored as
1969** integers, for space efficiency, but after extraction we want them
1970** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001971*/
drh9cbf3422008-01-17 16:22:13 +00001972case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001973 pIn1 = &aMem[pOp->p1];
drh169f0772019-05-02 21:36:26 +00001974 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
drh3242c692019-05-04 01:29:13 +00001975 testcase( pIn1->flags & MEM_Int );
1976 testcase( pIn1->flags & MEM_IntReal );
drh8558cde2008-01-05 05:20:10 +00001977 sqlite3VdbeMemRealify(pIn1);
drhefb5f9a2019-08-30 21:52:13 +00001978 REGISTER_TRACE(pOp->p1, pIn1);
drh8a512562005-11-14 22:29:05 +00001979 }
drh487e2622005-06-25 18:42:14 +00001980 break;
1981}
drh13573c72010-01-12 17:04:07 +00001982#endif
drh487e2622005-06-25 18:42:14 +00001983
drh8df447f2005-11-01 15:48:24 +00001984#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001985/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001986** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001987**
drh4169e432014-08-25 20:11:52 +00001988** Force the value in register P1 to be the type defined by P2.
1989**
1990** <ul>
drhbb6783b2017-04-29 18:02:49 +00001991** <li> P2=='A' &rarr; BLOB
1992** <li> P2=='B' &rarr; TEXT
1993** <li> P2=='C' &rarr; NUMERIC
1994** <li> P2=='D' &rarr; INTEGER
1995** <li> P2=='E' &rarr; REAL
drh4169e432014-08-25 20:11:52 +00001996** </ul>
drh487e2622005-06-25 18:42:14 +00001997**
1998** A NULL value is not changed by this routine. It remains NULL.
1999*/
drh4169e432014-08-25 20:11:52 +00002000case OP_Cast: { /* in1 */
drh05883a32015-06-02 15:32:08 +00002001 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00002002 testcase( pOp->p2==SQLITE_AFF_TEXT );
drh05883a32015-06-02 15:32:08 +00002003 testcase( pOp->p2==SQLITE_AFF_BLOB );
drh05bbb2e2014-08-25 22:37:19 +00002004 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
2005 testcase( pOp->p2==SQLITE_AFF_INTEGER );
2006 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00002007 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00002008 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00002009 rc = ExpandBlob(pIn1);
drh9467abf2016-02-17 18:44:11 +00002010 if( rc ) goto abort_due_to_error;
drh0af6ddd2019-12-23 03:37:46 +00002011 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
2012 if( rc ) goto abort_due_to_error;
2013 UPDATE_MAX_BLOBSIZE(pIn1);
drh5d732722019-12-20 17:25:10 +00002014 REGISTER_TRACE(pOp->p1, pIn1);
drh487e2622005-06-25 18:42:14 +00002015 break;
2016}
drh8a512562005-11-14 22:29:05 +00002017#endif /* SQLITE_OMIT_CAST */
2018
drh79752b62016-08-13 10:02:17 +00002019/* Opcode: Eq P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002020** Synopsis: IF r[P3]==r[P1]
drh79752b62016-08-13 10:02:17 +00002021**
2022** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
drh4bc20452021-03-29 18:53:47 +00002023** jump to address P2.
drh79752b62016-08-13 10:02:17 +00002024**
2025** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
2026** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
2027** to coerce both inputs according to this affinity before the
2028** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
2029** affinity is used. Note that the affinity conversions are stored
2030** back into the input registers P1 and P3. So this opcode can cause
2031** persistent changes to registers P1 and P3.
2032**
2033** Once any conversions have taken place, and neither value is NULL,
2034** the values are compared. If both values are blobs then memcmp() is
2035** used to determine the results of the comparison. If both values
2036** are text, then the appropriate collating function specified in
2037** P4 is used to do the comparison. If P4 is not specified then
2038** memcmp() is used to compare text string. If both values are
2039** numeric, then a numeric comparison is used. If the two values
2040** are of different types, then numbers are considered less than
2041** strings and strings are considered less than blobs.
2042**
2043** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
2044** true or false and is never NULL. If both operands are NULL then the result
2045** of comparison is true. If either operand is NULL then the result is false.
2046** If neither operand is NULL the result is the same as it would be if
2047** the SQLITE_NULLEQ flag were omitted from P5.
2048**
drh1f97d262021-03-29 13:47:20 +00002049** This opcode saves the result of comparison for use by the new
2050** OP_Jump opcode.
drh79752b62016-08-13 10:02:17 +00002051*/
2052/* Opcode: Ne P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002053** Synopsis: IF r[P3]!=r[P1]
drh79752b62016-08-13 10:02:17 +00002054**
2055** This works just like the Eq opcode except that the jump is taken if
2056** the operands in registers P1 and P3 are not equal. See the Eq opcode for
2057** additional information.
drh79752b62016-08-13 10:02:17 +00002058*/
drh35573352008-01-08 23:54:25 +00002059/* Opcode: Lt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002060** Synopsis: IF r[P3]<r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002061**
drh35573352008-01-08 23:54:25 +00002062** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
drh4bc20452021-03-29 18:53:47 +00002063** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00002064**
drh35573352008-01-08 23:54:25 +00002065** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
drh79752b62016-08-13 10:02:17 +00002066** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00002067** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00002068**
drh35573352008-01-08 23:54:25 +00002069** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00002070** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00002071** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00002072** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00002073** affinity is used. Note that the affinity conversions are stored
2074** back into the input registers P1 and P3. So this opcode can cause
2075** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00002076**
2077** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00002078** the values are compared. If both values are blobs then memcmp() is
2079** used to determine the results of the comparison. If both values
2080** are text, then the appropriate collating function specified in
2081** P4 is used to do the comparison. If P4 is not specified then
2082** memcmp() is used to compare text string. If both values are
2083** numeric, then a numeric comparison is used. If the two values
2084** are of different types, then numbers are considered less than
2085** strings and strings are considered less than blobs.
drh1f97d262021-03-29 13:47:20 +00002086**
2087** This opcode saves the result of comparison for use by the new
2088** OP_Jump opcode.
drh5e00f6c2001-09-13 13:46:56 +00002089*/
drh9cbf3422008-01-17 16:22:13 +00002090/* Opcode: Le P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002091** Synopsis: IF r[P3]<=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002092**
drh35573352008-01-08 23:54:25 +00002093** This works just like the Lt opcode except that the jump is taken if
2094** the content of register P3 is less than or equal to the content of
2095** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00002096*/
drh9cbf3422008-01-17 16:22:13 +00002097/* Opcode: Gt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002098** Synopsis: IF r[P3]>r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002099**
drh35573352008-01-08 23:54:25 +00002100** This works just like the Lt opcode except that the jump is taken if
2101** the content of register P3 is greater than the content of
2102** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00002103*/
drh9cbf3422008-01-17 16:22:13 +00002104/* Opcode: Ge P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00002105** Synopsis: IF r[P3]>=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002106**
drh35573352008-01-08 23:54:25 +00002107** This works just like the Lt opcode except that the jump is taken if
2108** the content of register P3 is greater than or equal to the content of
2109** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00002110*/
drh9cbf3422008-01-17 16:22:13 +00002111case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
2112case OP_Ne: /* same as TK_NE, jump, in1, in3 */
2113case OP_Lt: /* same as TK_LT, jump, in1, in3 */
2114case OP_Le: /* same as TK_LE, jump, in1, in3 */
2115case OP_Gt: /* same as TK_GT, jump, in1, in3 */
2116case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh4910a762016-09-03 01:46:15 +00002117 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
drh6a2fe092009-09-23 02:29:36 +00002118 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00002119 u16 flags1; /* Copy of initial value of pIn1->flags */
2120 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00002121
drh3c657212009-11-17 23:59:58 +00002122 pIn1 = &aMem[pOp->p1];
2123 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00002124 flags1 = pIn1->flags;
2125 flags3 = pIn3->flags;
drh85464972021-05-17 16:54:52 +00002126 if( (flags1 & flags3 & MEM_Int)!=0 ){
drh85464972021-05-17 16:54:52 +00002127 /* Common case of comparison of two integers */
2128 if( pIn3->u.i > pIn1->u.i ){
drh85464972021-05-17 16:54:52 +00002129 if( sqlite3aGTb[pOp->opcode] ){
2130 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
2131 goto jump_to_p2;
2132 }
drha81a9f72022-04-04 11:38:49 +00002133 iCompare = +1;
drhf26bad62022-12-22 21:32:58 +00002134 VVA_ONLY( iCompareIsInit = 1; )
drh85464972021-05-17 16:54:52 +00002135 }else if( pIn3->u.i < pIn1->u.i ){
drh85464972021-05-17 16:54:52 +00002136 if( sqlite3aLTb[pOp->opcode] ){
2137 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
2138 goto jump_to_p2;
2139 }
drha81a9f72022-04-04 11:38:49 +00002140 iCompare = -1;
drhf26bad62022-12-22 21:32:58 +00002141 VVA_ONLY( iCompareIsInit = 1; )
drh85464972021-05-17 16:54:52 +00002142 }else{
drh85464972021-05-17 16:54:52 +00002143 if( sqlite3aEQb[pOp->opcode] ){
2144 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
2145 goto jump_to_p2;
2146 }
drha81a9f72022-04-04 11:38:49 +00002147 iCompare = 0;
drhf26bad62022-12-22 21:32:58 +00002148 VVA_ONLY( iCompareIsInit = 1; )
drh85464972021-05-17 16:54:52 +00002149 }
2150 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
2151 break;
2152 }
drhc3f1d5f2011-05-30 23:42:16 +00002153 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00002154 /* One or both operands are NULL */
2155 if( pOp->p5 & SQLITE_NULLEQ ){
2156 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
2157 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
2158 ** or not both operands are null.
2159 */
drh053a1282012-09-19 21:15:46 +00002160 assert( (flags1 & MEM_Cleared)==0 );
drha42325e2018-12-22 00:34:30 +00002161 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
2162 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
drhc3191d22016-10-18 16:36:15 +00002163 if( (flags1&flags3&MEM_Null)!=0
drh053a1282012-09-19 21:15:46 +00002164 && (flags3&MEM_Cleared)==0
2165 ){
drh4910a762016-09-03 01:46:15 +00002166 res = 0; /* Operands are equal */
drh053a1282012-09-19 21:15:46 +00002167 }else{
danbdabe742019-03-18 16:51:24 +00002168 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
drh053a1282012-09-19 21:15:46 +00002169 }
drh6a2fe092009-09-23 02:29:36 +00002170 }else{
2171 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
2172 ** then the result is always NULL.
2173 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
2174 */
drh4bc20452021-03-29 18:53:47 +00002175 VdbeBranchTaken(2,3);
2176 if( pOp->p5 & SQLITE_JUMPIFNULL ){
2177 goto jump_to_p2;
drh6a2fe092009-09-23 02:29:36 +00002178 }
drha81a9f72022-04-04 11:38:49 +00002179 iCompare = 1; /* Operands are not equal */
drhf26bad62022-12-22 21:32:58 +00002180 VVA_ONLY( iCompareIsInit = 1; )
drh6a2fe092009-09-23 02:29:36 +00002181 break;
danielk1977a37cdde2004-05-16 11:15:36 +00002182 }
drh6a2fe092009-09-23 02:29:36 +00002183 }else{
drh85464972021-05-17 16:54:52 +00002184 /* Neither operand is NULL and we couldn't do the special high-speed
2185 ** integer comparison case. So do a general-case comparison. */
drh6a2fe092009-09-23 02:29:36 +00002186 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00002187 if( affinity>=SQLITE_AFF_NUMERIC ){
drh5fd0c122016-04-04 13:46:24 +00002188 if( (flags1 | flags3)&MEM_Str ){
drh169f0772019-05-02 21:36:26 +00002189 if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
drh5fd0c122016-04-04 13:46:24 +00002190 applyNumericAffinity(pIn1,0);
drh86d2de22020-06-14 13:40:13 +00002191 testcase( flags3==pIn3->flags );
drh4b37cd42016-06-25 11:43:47 +00002192 flags3 = pIn3->flags;
drh5fd0c122016-04-04 13:46:24 +00002193 }
drh169f0772019-05-02 21:36:26 +00002194 if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
drh5fd0c122016-04-04 13:46:24 +00002195 applyNumericAffinity(pIn3,0);
2196 }
drh24a09622014-09-18 16:28:59 +00002197 }
drha8b5c872022-12-14 09:06:45 +00002198 }else if( affinity==SQLITE_AFF_TEXT && ((flags1 | flags3) & MEM_Str)!=0 ){
drh169f0772019-05-02 21:36:26 +00002199 if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002200 testcase( pIn1->flags & MEM_Int );
2201 testcase( pIn1->flags & MEM_Real );
drh169f0772019-05-02 21:36:26 +00002202 testcase( pIn1->flags & MEM_IntReal );
drh24a09622014-09-18 16:28:59 +00002203 sqlite3VdbeMemStringify(pIn1, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002204 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
2205 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
drh1c704292022-12-17 12:49:04 +00002206 if( NEVER(pIn1==pIn3) ) flags3 = flags1 | MEM_Str;
drh24a09622014-09-18 16:28:59 +00002207 }
drhb44fec62019-12-24 21:42:22 +00002208 if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002209 testcase( pIn3->flags & MEM_Int );
2210 testcase( pIn3->flags & MEM_Real );
drh169f0772019-05-02 21:36:26 +00002211 testcase( pIn3->flags & MEM_IntReal );
drh24a09622014-09-18 16:28:59 +00002212 sqlite3VdbeMemStringify(pIn3, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002213 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
2214 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00002215 }
drh6a2fe092009-09-23 02:29:36 +00002216 }
drh6a2fe092009-09-23 02:29:36 +00002217 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh4910a762016-09-03 01:46:15 +00002218 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00002219 }
drh85464972021-05-17 16:54:52 +00002220
drh58596362017-08-03 00:29:23 +00002221 /* At this point, res is negative, zero, or positive if reg[P1] is
2222 ** less than, equal to, or greater than reg[P3], respectively. Compute
2223 ** the answer to this operator in res2, depending on what the comparison
2224 ** operator actually is. The next block of code depends on the fact
2225 ** that the 6 comparison operators are consecutive integers in this
2226 ** order: NE, EQ, GT, LE, LT, GE */
2227 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
2228 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
drh1af3fd52021-03-28 23:37:56 +00002229 if( res<0 ){
2230 res2 = sqlite3aLTb[pOp->opcode];
drh58596362017-08-03 00:29:23 +00002231 }else if( res==0 ){
drh1af3fd52021-03-28 23:37:56 +00002232 res2 = sqlite3aEQb[pOp->opcode];
drh58596362017-08-03 00:29:23 +00002233 }else{
drh1af3fd52021-03-28 23:37:56 +00002234 res2 = sqlite3aGTb[pOp->opcode];
danielk1977a37cdde2004-05-16 11:15:36 +00002235 }
drh1f97d262021-03-29 13:47:20 +00002236 iCompare = res;
drhf26bad62022-12-22 21:32:58 +00002237 VVA_ONLY( iCompareIsInit = 1; )
danielk1977a37cdde2004-05-16 11:15:36 +00002238
drhf56fa462015-04-13 21:39:54 +00002239 /* Undo any changes made by applyAffinity() to the input registers. */
drhf56fa462015-04-13 21:39:54 +00002240 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2241 pIn3->flags = flags3;
drhb44fec62019-12-24 21:42:22 +00002242 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2243 pIn1->flags = flags1;
drhf56fa462015-04-13 21:39:54 +00002244
drh4bc20452021-03-29 18:53:47 +00002245 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
2246 if( res2 ){
2247 goto jump_to_p2;
danielk1977a37cdde2004-05-16 11:15:36 +00002248 }
2249 break;
2250}
drhc9b84a12002-06-20 11:36:48 +00002251
drh4bc20452021-03-29 18:53:47 +00002252/* Opcode: ElseEq * P2 * * *
drh79752b62016-08-13 10:02:17 +00002253**
drh13d79502019-12-23 02:18:49 +00002254** This opcode must follow an OP_Lt or OP_Gt comparison operator. There
2255** can be zero or more OP_ReleaseReg opcodes intervening, but no other
2256** opcodes are allowed to occur between this instruction and the previous
drh4bc20452021-03-29 18:53:47 +00002257** OP_Lt or OP_Gt.
drh13d79502019-12-23 02:18:49 +00002258**
2259** If result of an OP_Eq comparison on the same two operands as the
drh4bc20452021-03-29 18:53:47 +00002260** prior OP_Lt or OP_Gt would have been true, then jump to P2.
2261** If the result of an OP_Eq comparison on the two previous
2262** operands would have been false or NULL, then fall through.
drh79752b62016-08-13 10:02:17 +00002263*/
drh4bc20452021-03-29 18:53:47 +00002264case OP_ElseEq: { /* same as TK_ESCAPE, jump */
drh13d79502019-12-23 02:18:49 +00002265
2266#ifdef SQLITE_DEBUG
2267 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
drh4bc20452021-03-29 18:53:47 +00002268 ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */
drh13d79502019-12-23 02:18:49 +00002269 int iAddr;
2270 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
2271 if( aOp[iAddr].opcode==OP_ReleaseReg ) continue;
2272 assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt );
drh13d79502019-12-23 02:18:49 +00002273 break;
2274 }
2275#endif /* SQLITE_DEBUG */
drhf26bad62022-12-22 21:32:58 +00002276 assert( iCompareIsInit );
drh4bc20452021-03-29 18:53:47 +00002277 VdbeBranchTaken(iCompare==0, 2);
2278 if( iCompare==0 ) goto jump_to_p2;
drh79752b62016-08-13 10:02:17 +00002279 break;
2280}
2281
2282
drh0acb7e42008-06-25 00:12:41 +00002283/* Opcode: Permutation * * * P4 *
2284**
drhb7dab702017-01-26 18:00:00 +00002285** Set the permutation used by the OP_Compare operator in the next
2286** instruction. The permutation is stored in the P4 operand.
drh0acb7e42008-06-25 00:12:41 +00002287**
drha81a9f72022-04-04 11:38:49 +00002288** The permutation is only valid for the next opcode which must be
2289** an OP_Compare that has the OPFLAG_PERMUTE bit set in P5.
drhb1702022016-01-30 00:45:18 +00002290**
2291** The first integer in the P4 integer array is the length of the array
2292** and does not become part of the permutation.
drh0acb7e42008-06-25 00:12:41 +00002293*/
2294case OP_Permutation: {
2295 assert( pOp->p4type==P4_INTARRAY );
2296 assert( pOp->p4.ai );
drhb7dab702017-01-26 18:00:00 +00002297 assert( pOp[1].opcode==OP_Compare );
2298 assert( pOp[1].p5 & OPFLAG_PERMUTE );
drh0acb7e42008-06-25 00:12:41 +00002299 break;
2300}
2301
drh953f7612012-12-07 22:18:54 +00002302/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00002303** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00002304**
drh710c4842010-08-30 01:17:20 +00002305** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2306** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00002307** the comparison for use by the next OP_Jump instruct.
2308**
drh0ca10df2012-12-08 13:26:23 +00002309** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2310** determined by the most recent OP_Permutation operator. If the
2311** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2312** order.
2313**
drh0acb7e42008-06-25 00:12:41 +00002314** P4 is a KeyInfo structure that defines collating sequences and sort
2315** orders for the comparison. The permutation applies to registers
2316** only. The KeyInfo elements are used sequentially.
2317**
2318** The comparison is a sort comparison, so NULLs compare equal,
2319** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00002320** and strings are less than blobs.
drha81a9f72022-04-04 11:38:49 +00002321**
2322** This opcode must be immediately followed by an OP_Jump opcode.
drh16ee60f2008-06-20 18:13:25 +00002323*/
2324case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002325 int n;
2326 int i;
2327 int p1;
2328 int p2;
2329 const KeyInfo *pKeyInfo;
drhabc38152020-07-22 13:38:04 +00002330 u32 idx;
drh856c1032009-06-02 15:21:42 +00002331 CollSeq *pColl; /* Collating sequence to use on this term */
2332 int bRev; /* True for DESCENDING sort order */
drhabc38152020-07-22 13:38:04 +00002333 u32 *aPermute; /* The permutation */
drh856c1032009-06-02 15:21:42 +00002334
drhb7dab702017-01-26 18:00:00 +00002335 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
2336 aPermute = 0;
2337 }else{
2338 assert( pOp>aOp );
2339 assert( pOp[-1].opcode==OP_Permutation );
2340 assert( pOp[-1].p4type==P4_INTARRAY );
2341 aPermute = pOp[-1].p4.ai + 1;
2342 assert( aPermute!=0 );
2343 }
drh856c1032009-06-02 15:21:42 +00002344 n = pOp->p3;
2345 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002346 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002347 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002348 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002349 p2 = pOp->p2;
drhd879e3e2017-02-13 13:35:55 +00002350#ifdef SQLITE_DEBUG
drh6a2fe092009-09-23 02:29:36 +00002351 if( aPermute ){
2352 int k, mx = 0;
mistachkincec5f1d2020-08-04 16:11:37 +00002353 for(k=0; k<n; k++) if( aPermute[k]>(u32)mx ) mx = aPermute[k];
drh9f6168b2016-03-19 23:32:58 +00002354 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
2355 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002356 }else{
drh9f6168b2016-03-19 23:32:58 +00002357 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
2358 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002359 }
2360#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002361 for(i=0; i<n; i++){
drh8deae5a2020-07-29 12:23:20 +00002362 idx = aPermute ? aPermute[i] : (u32)i;
drh2b4ded92010-09-27 21:09:31 +00002363 assert( memIsValid(&aMem[p1+idx]) );
2364 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002365 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2366 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drha485ad12017-08-02 22:43:14 +00002367 assert( i<pKeyInfo->nKeyField );
drh93a960a2008-07-10 00:32:42 +00002368 pColl = pKeyInfo->aColl[i];
dan6e118922019-08-12 16:36:38 +00002369 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
drha6c2ed92009-11-14 23:22:23 +00002370 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drhf26bad62022-12-22 21:32:58 +00002371 VVA_ONLY( iCompareIsInit = 1; )
drh0acb7e42008-06-25 00:12:41 +00002372 if( iCompare ){
dan6e118922019-08-12 16:36:38 +00002373 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
2374 && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null))
2375 ){
2376 iCompare = -iCompare;
2377 }
drh0acb7e42008-06-25 00:12:41 +00002378 if( bRev ) iCompare = -iCompare;
2379 break;
2380 }
drh16ee60f2008-06-20 18:13:25 +00002381 }
drha81a9f72022-04-04 11:38:49 +00002382 assert( pOp[1].opcode==OP_Jump );
drh16ee60f2008-06-20 18:13:25 +00002383 break;
2384}
2385
2386/* Opcode: Jump P1 P2 P3 * *
2387**
2388** Jump to the instruction at address P1, P2, or P3 depending on whether
2389** in the most recent OP_Compare instruction the P1 vector was less than
2390** equal to, or greater than the P2 vector, respectively.
drha81a9f72022-04-04 11:38:49 +00002391**
2392** This opcode must immediately follow an OP_Compare opcode.
drh16ee60f2008-06-20 18:13:25 +00002393*/
drh0acb7e42008-06-25 00:12:41 +00002394case OP_Jump: { /* jump */
drha81a9f72022-04-04 11:38:49 +00002395 assert( pOp>aOp && pOp[-1].opcode==OP_Compare );
drhf26bad62022-12-22 21:32:58 +00002396 assert( iCompareIsInit );
drh0acb7e42008-06-25 00:12:41 +00002397 if( iCompare<0 ){
drh7083a482018-07-10 16:04:04 +00002398 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
drh0acb7e42008-06-25 00:12:41 +00002399 }else if( iCompare==0 ){
drh7083a482018-07-10 16:04:04 +00002400 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
drh16ee60f2008-06-20 18:13:25 +00002401 }else{
drh7083a482018-07-10 16:04:04 +00002402 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
drh16ee60f2008-06-20 18:13:25 +00002403 }
2404 break;
2405}
2406
drh5b6afba2008-01-05 16:29:28 +00002407/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002408** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002409**
drh5b6afba2008-01-05 16:29:28 +00002410** Take the logical AND of the values in registers P1 and P2 and
2411** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002412**
drh5b6afba2008-01-05 16:29:28 +00002413** If either P1 or P2 is 0 (false) then the result is 0 even if
2414** the other input is NULL. A NULL and true or two NULLs give
2415** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002416*/
drh5b6afba2008-01-05 16:29:28 +00002417/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002418** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002419**
2420** Take the logical OR of the values in register P1 and P2 and
2421** store the answer in register P3.
2422**
2423** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2424** even if the other input is NULL. A NULL and false or two NULLs
2425** give a NULL output.
2426*/
2427case OP_And: /* same as TK_AND, in1, in2, out3 */
2428case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002429 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2430 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002431
drh1fcfa722018-02-26 15:27:31 +00002432 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
2433 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
drhbb113512002-05-27 01:04:51 +00002434 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002435 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002436 v1 = and_logic[v1*3+v2];
2437 }else{
drh5b6afba2008-01-05 16:29:28 +00002438 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002439 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002440 }
drh3c657212009-11-17 23:59:58 +00002441 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002442 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002443 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002444 }else{
drh5b6afba2008-01-05 16:29:28 +00002445 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002446 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002447 }
drh5e00f6c2001-09-13 13:46:56 +00002448 break;
2449}
2450
drh8abed7b2018-02-26 18:49:05 +00002451/* Opcode: IsTrue P1 P2 P3 P4 *
2452** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
2453**
2454** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
2455** IS NOT FALSE operators.
2456**
drh96acafb2018-02-27 14:49:25 +00002457** Interpret the value in register P1 as a boolean value. Store that
drh8abed7b2018-02-26 18:49:05 +00002458** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
2459** NULL, then the P3 is stored in register P2. Invert the answer if P4
2460** is 1.
2461**
2462** The logic is summarized like this:
2463**
2464** <ul>
drh96acafb2018-02-27 14:49:25 +00002465** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
2466** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
2467** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
2468** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
drh8abed7b2018-02-26 18:49:05 +00002469** </ul>
2470*/
2471case OP_IsTrue: { /* in1, out2 */
2472 assert( pOp->p4type==P4_INT32 );
2473 assert( pOp->p4.i==0 || pOp->p4.i==1 );
drh96acafb2018-02-27 14:49:25 +00002474 assert( pOp->p3==0 || pOp->p3==1 );
drh8abed7b2018-02-26 18:49:05 +00002475 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
2476 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
2477 break;
2478}
2479
drhe99fa2a2008-12-15 15:27:51 +00002480/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002481** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002482**
drhe99fa2a2008-12-15 15:27:51 +00002483** Interpret the value in register P1 as a boolean value. Store the
2484** boolean complement in register P2. If the value in register P1 is
2485** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002486*/
drh93952eb2009-11-13 19:43:43 +00002487case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002488 pIn1 = &aMem[pOp->p1];
2489 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002490 if( (pIn1->flags & MEM_Null)==0 ){
drhbc8f68a2018-02-26 15:31:39 +00002491 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
drh007c8432018-02-26 03:20:18 +00002492 }else{
2493 sqlite3VdbeMemSetNull(pOut);
drhe99fa2a2008-12-15 15:27:51 +00002494 }
drh5e00f6c2001-09-13 13:46:56 +00002495 break;
2496}
2497
drhe99fa2a2008-12-15 15:27:51 +00002498/* Opcode: BitNot P1 P2 * * *
drhcd9e0142018-06-12 13:16:57 +00002499** Synopsis: r[P2]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002500**
drhe99fa2a2008-12-15 15:27:51 +00002501** Interpret the content of register P1 as an integer. Store the
2502** ones-complement of the P1 value into register P2. If P1 holds
2503** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002504*/
drh93952eb2009-11-13 19:43:43 +00002505case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002506 pIn1 = &aMem[pOp->p1];
2507 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002508 sqlite3VdbeMemSetNull(pOut);
2509 if( (pIn1->flags & MEM_Null)==0 ){
2510 pOut->flags = MEM_Int;
2511 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002512 }
drhbf4133c2001-10-13 02:59:08 +00002513 break;
2514}
2515
drh48f2d3b2011-09-16 01:34:43 +00002516/* Opcode: Once P1 P2 * * *
2517**
drhab087d42017-03-24 17:59:56 +00002518** Fall through to the next instruction the first time this opcode is
2519** encountered on each invocation of the byte-code program. Jump to P2
2520** on the second and all subsequent encounters during the same invocation.
2521**
2522** Top-level programs determine first invocation by comparing the P1
2523** operand against the P1 operand on the OP_Init opcode at the beginning
2524** of the program. If the P1 values differ, then fall through and make
2525** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2526** the same then take the jump.
2527**
2528** For subprograms, there is a bitmask in the VdbeFrame that determines
2529** whether or not the jump should be taken. The bitmask is necessary
2530** because the self-altering code trick does not work for recursive
2531** triggers.
drh48f2d3b2011-09-16 01:34:43 +00002532*/
dan1d8cb212011-12-09 13:24:16 +00002533case OP_Once: { /* jump */
drhab087d42017-03-24 17:59:56 +00002534 u32 iAddr; /* Address of this instruction */
drh9e5eb9c2016-09-18 16:08:10 +00002535 assert( p->aOp[0].opcode==OP_Init );
drhab087d42017-03-24 17:59:56 +00002536 if( p->pFrame ){
2537 iAddr = (int)(pOp - p->aOp);
2538 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
2539 VdbeBranchTaken(1, 2);
drhab087d42017-03-24 17:59:56 +00002540 goto jump_to_p2;
2541 }
drh18333ef2017-03-24 18:38:41 +00002542 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
dan1d8cb212011-12-09 13:24:16 +00002543 }else{
drhab087d42017-03-24 17:59:56 +00002544 if( p->aOp[0].p1==pOp->p1 ){
2545 VdbeBranchTaken(1, 2);
2546 goto jump_to_p2;
2547 }
dan1d8cb212011-12-09 13:24:16 +00002548 }
drhab087d42017-03-24 17:59:56 +00002549 VdbeBranchTaken(0, 2);
2550 pOp->p1 = p->aOp[0].p1;
dan1d8cb212011-12-09 13:24:16 +00002551 break;
2552}
2553
drh3c84ddf2008-01-09 02:15:38 +00002554/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002555**
drhef8662b2011-06-20 21:47:58 +00002556** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002557** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002558** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002559*/
drh1fcfa722018-02-26 15:27:31 +00002560case OP_If: { /* jump, in1 */
2561 int c;
2562 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
2563 VdbeBranchTaken(c!=0, 2);
2564 if( c ) goto jump_to_p2;
2565 break;
2566}
2567
drh3c84ddf2008-01-09 02:15:38 +00002568/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002569**
drhef8662b2011-06-20 21:47:58 +00002570** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002571** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002572** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002573*/
drh9cbf3422008-01-17 16:22:13 +00002574case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002575 int c;
drh1fcfa722018-02-26 15:27:31 +00002576 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
drh688852a2014-02-17 22:40:43 +00002577 VdbeBranchTaken(c!=0, 2);
drh1fcfa722018-02-26 15:27:31 +00002578 if( c ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00002579 break;
2580}
2581
drh830ecf92009-06-18 00:41:55 +00002582/* Opcode: IsNull P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00002583** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002584**
drh830ecf92009-06-18 00:41:55 +00002585** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002586*/
drh9cbf3422008-01-17 16:22:13 +00002587case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002588 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002589 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002590 if( (pIn1->flags & MEM_Null)!=0 ){
drhf56fa462015-04-13 21:39:54 +00002591 goto jump_to_p2;
drh830ecf92009-06-18 00:41:55 +00002592 }
drh477df4b2008-01-05 18:48:24 +00002593 break;
2594}
2595
drh49d77ee2022-10-10 18:25:05 +00002596/* Opcode: IsType P1 P2 P3 P4 P5
2597** Synopsis: if typeof(P1.P3) in P5 goto P2
drh71c770f2021-08-19 16:29:33 +00002598**
drh49d77ee2022-10-10 18:25:05 +00002599** Jump to P2 if the type of a column in a btree is one of the types specified
2600** by the P5 bitmask.
2601**
2602** P1 is normally a cursor on a btree for which the row decode cache is
2603** valid through at least column P3. In other words, there should have been
2604** a prior OP_Column for column P3 or greater. If the cursor is not valid,
2605** then this opcode might give spurious results.
2606** The the btree row has fewer than P3 columns, then use P4 as the
2607** datatype.
2608**
2609** If P1 is -1, then P3 is a register number and the datatype is taken
2610** from the value in that register.
2611**
2612** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant
2613** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04.
2614** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10.
2615**
2616** Take the jump to address P2 if and only if the datatype of the
2617** value determined by P1 and P3 corresponds to one of the bits in the
2618** P5 bitmask.
2619**
drh71c770f2021-08-19 16:29:33 +00002620*/
drhc9ef12f2022-10-10 21:21:04 +00002621case OP_IsType: { /* jump */
drh49d77ee2022-10-10 18:25:05 +00002622 VdbeCursor *pC;
2623 u16 typeMask;
2624 u32 serialType;
2625
2626 assert( pOp->p1>=(-1) && pOp->p1<p->nCursor );
2627 assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) );
2628 if( pOp->p1>=0 ){
2629 pC = p->apCsr[pOp->p1];
2630 assert( pC!=0 );
2631 assert( pOp->p3>=0 );
drhc9ef12f2022-10-10 21:21:04 +00002632 if( pOp->p3<pC->nHdrParsed ){
drh49d77ee2022-10-10 18:25:05 +00002633 serialType = pC->aType[pOp->p3];
drhc2777ab2022-10-11 13:57:55 +00002634 if( serialType>=12 ){
2635 if( serialType&1 ){
2636 typeMask = 0x04; /* SQLITE_TEXT */
2637 }else{
2638 typeMask = 0x08; /* SQLITE_BLOB */
2639 }
drh49d77ee2022-10-10 18:25:05 +00002640 }else{
drhc2777ab2022-10-11 13:57:55 +00002641 static const unsigned char aMask[] = {
2642 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2,
2643 0x01, 0x01, 0x10, 0x10
2644 };
2645 testcase( serialType==0 );
2646 testcase( serialType==1 );
2647 testcase( serialType==2 );
2648 testcase( serialType==3 );
2649 testcase( serialType==4 );
2650 testcase( serialType==5 );
2651 testcase( serialType==6 );
2652 testcase( serialType==7 );
2653 testcase( serialType==8 );
2654 testcase( serialType==9 );
2655 testcase( serialType==10 );
2656 testcase( serialType==11 );
2657 typeMask = aMask[serialType];
drh49d77ee2022-10-10 18:25:05 +00002658 }
2659 }else{
2660 typeMask = 1 << (pOp->p4.i - 1);
drhc2777ab2022-10-11 13:57:55 +00002661 testcase( typeMask==0x01 );
2662 testcase( typeMask==0x02 );
2663 testcase( typeMask==0x04 );
2664 testcase( typeMask==0x08 );
2665 testcase( typeMask==0x10 );
drh49d77ee2022-10-10 18:25:05 +00002666 }
2667 }else{
2668 assert( memIsValid(&aMem[pOp->p3]) );
2669 typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1);
drhc2777ab2022-10-11 13:57:55 +00002670 testcase( typeMask==0x01 );
2671 testcase( typeMask==0x02 );
2672 testcase( typeMask==0x04 );
2673 testcase( typeMask==0x08 );
2674 testcase( typeMask==0x10 );
drh49d77ee2022-10-10 18:25:05 +00002675 }
2676 VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2);
2677 if( typeMask & pOp->p5 ){
2678 goto jump_to_p2;
2679 }
drh71c770f2021-08-19 16:29:33 +00002680 break;
2681}
2682
drh871e7ff2021-03-29 14:40:48 +00002683/* Opcode: ZeroOrNull P1 P2 P3 * *
drh4bc20452021-03-29 18:53:47 +00002684** Synopsis: r[P2] = 0 OR NULL
drh871e7ff2021-03-29 14:40:48 +00002685**
drh4bc20452021-03-29 18:53:47 +00002686** If all both registers P1 and P3 are NOT NULL, then store a zero in
2687** register P2. If either registers P1 or P3 are NULL then put
2688** a NULL in register P2.
drh871e7ff2021-03-29 14:40:48 +00002689*/
drh4bc20452021-03-29 18:53:47 +00002690case OP_ZeroOrNull: { /* in1, in2, out2, in3 */
drh871e7ff2021-03-29 14:40:48 +00002691 if( (aMem[pOp->p1].flags & MEM_Null)!=0
2692 || (aMem[pOp->p3].flags & MEM_Null)!=0
2693 ){
2694 sqlite3VdbeMemSetNull(aMem + pOp->p2);
2695 }else{
2696 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0);
2697 }
2698 break;
2699}
2700
drh98757152008-01-09 23:04:12 +00002701/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002702** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002703**
drh6a288a32008-01-07 19:20:24 +00002704** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002705*/
drh9cbf3422008-01-17 16:22:13 +00002706case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002707 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002708 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002709 if( (pIn1->flags & MEM_Null)==0 ){
drhf56fa462015-04-13 21:39:54 +00002710 goto jump_to_p2;
drh6a288a32008-01-07 19:20:24 +00002711 }
drh5e00f6c2001-09-13 13:46:56 +00002712 break;
2713}
2714
drh31d6fd52017-04-14 19:03:10 +00002715/* Opcode: IfNullRow P1 P2 P3 * *
2716** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
2717**
2718** Check the cursor P1 to see if it is currently pointing at a NULL row.
2719** If it is, then set register P3 to NULL and jump immediately to P2.
2720** If P1 is not on a NULL row, then fall through without making any
2721** changes.
drhd1981832022-06-23 15:15:03 +00002722**
2723** If P1 is not an open cursor, then this opcode is a no-op.
drh31d6fd52017-04-14 19:03:10 +00002724*/
2725case OP_IfNullRow: { /* jump */
drhd1981832022-06-23 15:15:03 +00002726 VdbeCursor *pC;
drh31d6fd52017-04-14 19:03:10 +00002727 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhd1981832022-06-23 15:15:03 +00002728 pC = p->apCsr[pOp->p1];
drh54f1fc42022-06-25 20:32:29 +00002729 if( ALWAYS(pC) && pC->nullRow ){
drh31d6fd52017-04-14 19:03:10 +00002730 sqlite3VdbeMemSetNull(aMem + pOp->p3);
2731 goto jump_to_p2;
2732 }
2733 break;
2734}
2735
drh092457b2017-12-29 15:04:49 +00002736#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
2737/* Opcode: Offset P1 P2 P3 * *
2738** Synopsis: r[P3] = sqlite_offset(P1)
drh2fc865c2017-12-16 20:20:37 +00002739**
drh092457b2017-12-29 15:04:49 +00002740** Store in register r[P3] the byte offset into the database file that is the
drh2fc865c2017-12-16 20:20:37 +00002741** start of the payload for the record at which that cursor P1 is currently
2742** pointing.
drhfe6d20e2017-12-29 14:33:54 +00002743**
drh092457b2017-12-29 15:04:49 +00002744** P2 is the column number for the argument to the sqlite_offset() function.
drhfe6d20e2017-12-29 14:33:54 +00002745** This opcode does not use P2 itself, but the P2 value is used by the
2746** code generator. The P1, P2, and P3 operands to this opcode are the
mistachkin5e9825e2018-03-01 18:09:02 +00002747** same as for OP_Column.
drh092457b2017-12-29 15:04:49 +00002748**
2749** This opcode is only available if SQLite is compiled with the
2750** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
drh2fc865c2017-12-16 20:20:37 +00002751*/
drh092457b2017-12-29 15:04:49 +00002752case OP_Offset: { /* out3 */
drh2fc865c2017-12-16 20:20:37 +00002753 VdbeCursor *pC; /* The VDBE cursor */
2754 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2755 pC = p->apCsr[pOp->p1];
drhfe6d20e2017-12-29 14:33:54 +00002756 pOut = &p->aMem[pOp->p3];
drh8f32d922022-03-05 19:36:29 +00002757 if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){
drhfe6d20e2017-12-29 14:33:54 +00002758 sqlite3VdbeMemSetNull(pOut);
drh2fc865c2017-12-16 20:20:37 +00002759 }else{
drh8f32d922022-03-05 19:36:29 +00002760 if( pC->deferredMoveto ){
2761 rc = sqlite3VdbeFinishMoveto(pC);
2762 if( rc ) goto abort_due_to_error;
2763 }
drhdc951362022-03-05 23:52:05 +00002764 if( sqlite3BtreeEof(pC->uc.pCursor) ){
drh8f32d922022-03-05 19:36:29 +00002765 sqlite3VdbeMemSetNull(pOut);
2766 }else{
2767 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
2768 }
drh2fc865c2017-12-16 20:20:37 +00002769 }
2770 break;
2771}
drh092457b2017-12-29 15:04:49 +00002772#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
drh2fc865c2017-12-16 20:20:37 +00002773
drh3e9ca092009-09-08 01:14:48 +00002774/* Opcode: Column P1 P2 P3 P4 P5
drh088b6152022-04-18 13:57:57 +00002775** Synopsis: r[P3]=PX cursor P1 column P2
danielk1977192ac1d2004-05-10 07:17:30 +00002776**
danielk1977cfcdaef2004-05-12 07:33:33 +00002777** Interpret the data that cursor P1 points to as a structure built using
2778** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002779** information about the format of the data.) Extract the P2-th column
drh7ca4af62022-10-13 14:01:11 +00002780** from this record. If there are less than (P2+1)
drhd4e70eb2008-01-02 00:34:36 +00002781** values in the record, extract a NULL.
2782**
drh9cbf3422008-01-17 16:22:13 +00002783** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002784**
drh1cc3a362017-04-03 13:17:31 +00002785** If the record contains fewer than P2 fields, then extract a NULL. Or,
danielk19771f4aa332008-01-03 09:51:55 +00002786** if the P4 argument is a P4_MEM use the value of the P4 argument as
2787** the result.
drh3e9ca092009-09-08 01:14:48 +00002788**
drheddfa982022-10-13 14:54:32 +00002789** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed
2790** to only be used by the length() function or the equivalent. The content
2791** of large blobs is not loaded, thus saving CPU cycles. If the
2792** OPFLAG_TYPEOFARG bit is set then the result will only be used by the
2793** typeof() function or the IS NULL or IS NOT NULL operators or the
2794** equivalent. In this case, all content loading can be omitted.
danielk1977192ac1d2004-05-10 07:17:30 +00002795*/
dan2adb3092022-12-06 18:48:06 +00002796case OP_Column: { /* ncycle */
drhabc38152020-07-22 13:38:04 +00002797 u32 p2; /* column number to retrieve */
drh856c1032009-06-02 15:21:42 +00002798 VdbeCursor *pC; /* The VDBE cursor */
drhfc569502022-02-25 20:11:59 +00002799 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
drhd3194f52004-05-27 19:59:32 +00002800 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002801 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002802 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002803 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002804 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002805 const u8 *zData; /* Part of the record being decoded */
2806 const u8 *zHdr; /* Next unparsed byte of the header */
2807 const u8 *zEndHdr; /* Pointer to first byte after the header */
drhc6ce38832015-10-15 21:30:24 +00002808 u64 offset64; /* 64-bit offset */
drh5a077b72011-08-29 02:16:18 +00002809 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002810 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002811
drh8c7715d2019-12-20 14:37:56 +00002812 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhfc569502022-02-25 20:11:59 +00002813 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
dande892d92016-01-29 19:29:45 +00002814 pC = p->apCsr[pOp->p1];
drhabc38152020-07-22 13:38:04 +00002815 p2 = (u32)pOp->p2;
dande892d92016-01-29 19:29:45 +00002816
drhfc569502022-02-25 20:11:59 +00002817op_column_restart:
danielk19776c924092007-11-12 08:09:34 +00002818 assert( pC!=0 );
drh7c960392022-04-13 18:20:23 +00002819 assert( p2<(u32)pC->nField
2820 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) );
drhb53a5a92014-10-12 22:37:22 +00002821 aOffset = pC->aOffset;
drhb2486682022-01-03 01:43:28 +00002822 assert( aOffset==pC->aType+pC->nField );
drh62aaa6c2015-11-21 17:27:42 +00002823 assert( pC->eCurType!=CURTYPE_VTAB );
drhc960dcb2015-11-20 19:22:01 +00002824 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2825 assert( pC->eCurType!=CURTYPE_SORTER );
drh399af1d2013-11-20 17:25:55 +00002826
drha43a02e2016-05-19 17:51:19 +00002827 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977192ac1d2004-05-10 07:17:30 +00002828 if( pC->nullRow ){
drh3ac62432022-04-13 17:41:03 +00002829 if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){
drhfe0cf7a2017-08-16 19:20:20 +00002830 /* For the special case of as pseudo-cursor, the seekResult field
2831 ** identifies the register that holds the record */
drhfe0cf7a2017-08-16 19:20:20 +00002832 pReg = &aMem[pC->seekResult];
drhc8606e42013-11-20 19:28:03 +00002833 assert( pReg->flags & MEM_Blob );
2834 assert( memIsValid(pReg) );
drh6cd8c8c2017-08-15 14:14:36 +00002835 pC->payloadSize = pC->szRow = pReg->n;
drhc8606e42013-11-20 19:28:03 +00002836 pC->aRow = (u8*)pReg->z;
2837 }else{
drhfc569502022-02-25 20:11:59 +00002838 pDest = &aMem[pOp->p3];
2839 memAboutToChange(p, pDest);
drh6b5631e2014-11-05 15:57:39 +00002840 sqlite3VdbeMemSetNull(pDest);
drh399af1d2013-11-20 17:25:55 +00002841 goto op_column_out;
2842 }
danielk1977192ac1d2004-05-10 07:17:30 +00002843 }else{
drh06a09a82016-11-25 17:03:03 +00002844 pCrsr = pC->uc.pCursor;
drhfc569502022-02-25 20:11:59 +00002845 if( pC->deferredMoveto ){
2846 u32 iMap;
2847 assert( !pC->isEphemeral );
2848 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){
2849 pC = pC->pAltCursor;
2850 p2 = iMap - 1;
2851 goto op_column_restart;
2852 }
2853 rc = sqlite3VdbeFinishMoveto(pC);
2854 if( rc ) goto abort_due_to_error;
2855 }else if( sqlite3BtreeCursorHasMoved(pCrsr) ){
2856 rc = sqlite3VdbeHandleMovedCursor(pC);
2857 if( rc ) goto abort_due_to_error;
2858 goto op_column_restart;
2859 }
drhc960dcb2015-11-20 19:22:01 +00002860 assert( pC->eCurType==CURTYPE_BTREE );
drhc8606e42013-11-20 19:28:03 +00002861 assert( pCrsr );
drha7c90c42016-06-04 20:37:10 +00002862 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2863 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
drh6cd8c8c2017-08-15 14:14:36 +00002864 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
2865 assert( pC->szRow<=pC->payloadSize );
2866 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
danielk1977192ac1d2004-05-10 07:17:30 +00002867 }
drhb73857f2006-03-17 00:25:59 +00002868 pC->cacheStatus = p->cacheCtr;
drhc2808f32022-04-02 22:47:47 +00002869 if( (aOffset[0] = pC->aRow[0])<0x80 ){
2870 pC->iHdrOffset = 1;
2871 }else{
2872 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset);
2873 }
drh399af1d2013-11-20 17:25:55 +00002874 pC->nHdrParsed = 0;
drh35cd6432009-06-05 14:17:21 +00002875
drh1f613c42017-08-16 14:16:19 +00002876 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
drhc81aa2e2014-10-11 23:31:52 +00002877 /* pC->aRow does not have to hold the entire row, but it does at least
2878 ** need to cover the header of the record. If pC->aRow does not contain
2879 ** the complete header, then set it to zero, forcing the header to be
2880 ** dynamically allocated. */
2881 pC->aRow = 0;
2882 pC->szRow = 0;
drh848a3322015-10-16 12:53:47 +00002883
2884 /* Make sure a corrupt database has not given us an oversize header.
2885 ** Do this now to avoid an oversize memory allocation.
2886 **
2887 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2888 ** types use so much data space that there can only be 4096 and 32 of
2889 ** them, respectively. So the maximum header length results from a
2890 ** 3-byte type for each of the maximum of 32768 columns plus three
2891 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2892 */
drh1f613c42017-08-16 14:16:19 +00002893 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
drh74588ce2017-09-13 00:13:05 +00002894 goto op_column_corrupt;
drh848a3322015-10-16 12:53:47 +00002895 }
drh95b225a2017-08-16 11:04:22 +00002896 }else{
2897 /* This is an optimization. By skipping over the first few tests
2898 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
2899 ** measurable performance gain.
2900 **
drh1f613c42017-08-16 14:16:19 +00002901 ** This branch is taken even if aOffset[0]==0. Such a record is never
drh95b225a2017-08-16 11:04:22 +00002902 ** generated by SQLite, and could be considered corruption, but we
drh1f613c42017-08-16 14:16:19 +00002903 ** accept it for historical reasons. When aOffset[0]==0, the code this
drh95b225a2017-08-16 11:04:22 +00002904 ** branch jumps to reads past the end of the record, but never more
2905 ** than a few bytes. Even if the record occurs at the end of the page
2906 ** content area, the "page header" comes after the page content and so
2907 ** this overread is harmless. Similar overreads can occur for a corrupt
2908 ** database file.
drh0eda6cd2016-05-19 16:58:42 +00002909 */
2910 zData = pC->aRow;
2911 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
drh1f613c42017-08-16 14:16:19 +00002912 testcase( aOffset[0]==0 );
drh0eda6cd2016-05-19 16:58:42 +00002913 goto op_column_read_header;
drhc81aa2e2014-10-11 23:31:52 +00002914 }
drhfc569502022-02-25 20:11:59 +00002915 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){
2916 rc = sqlite3VdbeHandleMovedCursor(pC);
2917 if( rc ) goto abort_due_to_error;
2918 goto op_column_restart;
drh399af1d2013-11-20 17:25:55 +00002919 }
drh35cd6432009-06-05 14:17:21 +00002920
drh399af1d2013-11-20 17:25:55 +00002921 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002922 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002923 */
drhc8606e42013-11-20 19:28:03 +00002924 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002925 /* If there is more header available for parsing in the record, try
2926 ** to extract additional fields up through the p2+1-th field
drh35cd6432009-06-05 14:17:21 +00002927 */
drhc8606e42013-11-20 19:28:03 +00002928 if( pC->iHdrOffset<aOffset[0] ){
2929 /* Make sure zData points to enough of the record to cover the header. */
2930 if( pC->aRow==0 ){
2931 memset(&sMem, 0, sizeof(sMem));
drh2a740062020-02-05 18:28:17 +00002932 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
drh9467abf2016-02-17 18:44:11 +00002933 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhc8606e42013-11-20 19:28:03 +00002934 zData = (u8*)sMem.z;
2935 }else{
2936 zData = pC->aRow;
drh9188b382004-05-14 21:12:22 +00002937 }
drhc8606e42013-11-20 19:28:03 +00002938
drh0c8f7602014-09-19 16:56:45 +00002939 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drh0eda6cd2016-05-19 16:58:42 +00002940 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002941 i = pC->nHdrParsed;
drhc6ce38832015-10-15 21:30:24 +00002942 offset64 = aOffset[i];
drhc8606e42013-11-20 19:28:03 +00002943 zHdr = zData + pC->iHdrOffset;
2944 zEndHdr = zData + aOffset[0];
drh95b225a2017-08-16 11:04:22 +00002945 testcase( zHdr>=zEndHdr );
drhc8606e42013-11-20 19:28:03 +00002946 do{
drhc332e042019-02-12 21:04:33 +00002947 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
drhc8606e42013-11-20 19:28:03 +00002948 zHdr++;
drhfaf37272015-10-16 14:23:42 +00002949 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002950 }else{
drhc8606e42013-11-20 19:28:03 +00002951 zHdr += sqlite3GetVarint32(zHdr, &t);
drhc332e042019-02-12 21:04:33 +00002952 pC->aType[i] = t;
drhfaf37272015-10-16 14:23:42 +00002953 offset64 += sqlite3VdbeSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002954 }
drhc332e042019-02-12 21:04:33 +00002955 aOffset[++i] = (u32)(offset64 & 0xffffffff);
drh8deae5a2020-07-29 12:23:20 +00002956 }while( (u32)i<=p2 && zHdr<zEndHdr );
drh170c2762016-05-20 21:40:11 +00002957
drh8dd83622014-10-13 23:39:02 +00002958 /* The record is corrupt if any of the following are true:
2959 ** (1) the bytes of the header extend past the declared header size
drh8dd83622014-10-13 23:39:02 +00002960 ** (2) the entire header was used but not all data was used
drh8dd83622014-10-13 23:39:02 +00002961 ** (3) the end of the data extends beyond the end of the record.
drhc8606e42013-11-20 19:28:03 +00002962 */
drhc6ce38832015-10-15 21:30:24 +00002963 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2964 || (offset64 > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002965 ){
drh95b225a2017-08-16 11:04:22 +00002966 if( aOffset[0]==0 ){
2967 i = 0;
2968 zHdr = zEndHdr;
2969 }else{
2970 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
drh74588ce2017-09-13 00:13:05 +00002971 goto op_column_corrupt;
drh95b225a2017-08-16 11:04:22 +00002972 }
danielk1977dedf45b2006-01-13 17:12:01 +00002973 }
drhddb2b4a2016-03-25 12:10:32 +00002974
drh170c2762016-05-20 21:40:11 +00002975 pC->nHdrParsed = i;
2976 pC->iHdrOffset = (u32)(zHdr - zData);
2977 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
mistachkin8c7cd6a2015-12-16 21:09:53 +00002978 }else{
drh9fbc8852016-01-04 03:48:46 +00002979 t = 0;
drh9188b382004-05-14 21:12:22 +00002980 }
drhd3194f52004-05-27 19:59:32 +00002981
drhf2db3382015-04-30 20:33:25 +00002982 /* If after trying to extract new entries from the header, nHdrParsed is
drh380d6852013-11-20 20:58:00 +00002983 ** still not up to p2, that means that the record has fewer than p2
2984 ** columns. So the result will be either the default value or a NULL.
drhd3194f52004-05-27 19:59:32 +00002985 */
drhc8606e42013-11-20 19:28:03 +00002986 if( pC->nHdrParsed<=p2 ){
drhfc569502022-02-25 20:11:59 +00002987 pDest = &aMem[pOp->p3];
2988 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002989 if( pOp->p4type==P4_MEM ){
2990 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2991 }else{
drh22e8d832014-10-29 00:58:38 +00002992 sqlite3VdbeMemSetNull(pDest);
drhc8606e42013-11-20 19:28:03 +00002993 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002994 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002995 }
drh95fa6062015-10-16 13:50:08 +00002996 }else{
2997 t = pC->aType[p2];
danielk1977cfcdaef2004-05-12 07:33:33 +00002998 }
danielk1977192ac1d2004-05-10 07:17:30 +00002999
drh380d6852013-11-20 20:58:00 +00003000 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00003001 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00003002 ** all valid.
drh9188b382004-05-14 21:12:22 +00003003 */
drhc8606e42013-11-20 19:28:03 +00003004 assert( p2<pC->nHdrParsed );
3005 assert( rc==SQLITE_OK );
drhfc569502022-02-25 20:11:59 +00003006 pDest = &aMem[pOp->p3];
3007 memAboutToChange(p, pDest);
drh75fd0542014-03-01 16:24:44 +00003008 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drha1851ef2016-05-20 19:51:28 +00003009 if( VdbeMemDynamic(pDest) ){
3010 sqlite3VdbeMemSetNull(pDest);
3011 }
drh95fa6062015-10-16 13:50:08 +00003012 assert( t==pC->aType[p2] );
drhc8606e42013-11-20 19:28:03 +00003013 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00003014 /* This is the common case where the desired content fits on the original
3015 ** page - where the content is not on an overflow page */
drh69f6e252016-01-11 18:05:00 +00003016 zData = pC->aRow + aOffset[p2];
3017 if( t<12 ){
3018 sqlite3VdbeSerialGet(zData, t, pDest);
3019 }else{
3020 /* If the column value is a string, we need a persistent value, not
3021 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
3022 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
3023 */
3024 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
3025 pDest->n = len = (t-12)/2;
drha1851ef2016-05-20 19:51:28 +00003026 pDest->enc = encoding;
drh69f6e252016-01-11 18:05:00 +00003027 if( pDest->szMalloc < len+2 ){
drh9090df62022-02-26 14:39:08 +00003028 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
drh69f6e252016-01-11 18:05:00 +00003029 pDest->flags = MEM_Null;
3030 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
3031 }else{
3032 pDest->z = pDest->zMalloc;
3033 }
3034 memcpy(pDest->z, zData, len);
3035 pDest->z[len] = 0;
3036 pDest->z[len+1] = 0;
3037 pDest->flags = aFlag[t&1];
3038 }
danielk197736963fd2005-02-19 08:18:05 +00003039 }else{
drha1851ef2016-05-20 19:51:28 +00003040 pDest->enc = encoding;
drh58c96082013-12-23 11:33:32 +00003041 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00003042 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
3043 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
3044 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00003045 ){
drh2a2a6962014-09-16 18:22:44 +00003046 /* Content is irrelevant for
3047 ** 1. the typeof() function,
3048 ** 2. the length(X) function if X is a blob, and
3049 ** 3. if the content length is zero.
3050 ** So we might as well use bogus content rather than reading
dan1f9144e2017-03-17 13:59:06 +00003051 ** content from disk.
3052 **
3053 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
3054 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
drhcbae3f82020-01-06 20:48:45 +00003055 ** read more. Use the global constant sqlite3CtypeMap[] as the array,
3056 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
3057 ** and it begins with a bunch of zeros.
dan1f9144e2017-03-17 13:59:06 +00003058 */
drhcbae3f82020-01-06 20:48:45 +00003059 sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00003060 }else{
drh9090df62022-02-26 14:39:08 +00003061 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
drhcb3cabd2016-11-25 19:18:28 +00003062 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
drh9467abf2016-02-17 18:44:11 +00003063 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3064 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
3065 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00003066 }
danielk1977cfcdaef2004-05-12 07:33:33 +00003067 }
drhd3194f52004-05-27 19:59:32 +00003068
danielk19773c9cc8d2005-01-17 03:40:08 +00003069op_column_out:
drhb7654112008-01-12 12:48:07 +00003070 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00003071 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00003072 break;
drh74588ce2017-09-13 00:13:05 +00003073
3074op_column_corrupt:
3075 if( aOp[0].p3>0 ){
3076 pOp = &aOp[aOp[0].p3-1];
3077 break;
3078 }else{
3079 rc = SQLITE_CORRUPT_BKPT;
3080 goto abort_due_to_error;
3081 }
danielk1977192ac1d2004-05-10 07:17:30 +00003082}
3083
drh926aac52021-11-03 14:02:48 +00003084/* Opcode: TypeCheck P1 P2 P3 P4 *
drh72532f52021-08-18 19:22:27 +00003085** Synopsis: typecheck(r[P1@P2])
3086**
3087** Apply affinities to the range of P2 registers beginning with P1.
3088** Take the affinities from the Table object in P4. If any value
3089** cannot be coerced into the correct type, then raise an error.
3090**
3091** This opcode is similar to OP_Affinity except that this opcode
3092** forces the register type to the Table column type. This is used
3093** to implement "strict affinity".
drh71c770f2021-08-19 16:29:33 +00003094**
drh926aac52021-11-03 14:02:48 +00003095** GENERATED ALWAYS AS ... STATIC columns are only checked if P3
3096** is zero. When P3 is non-zero, no type checking occurs for
3097** static generated columns. Virtual columns are computed at query time
3098** and so they are never checked.
3099**
drh71c770f2021-08-19 16:29:33 +00003100** Preconditions:
3101**
3102** <ul>
3103** <li> P2 should be the number of non-virtual columns in the
3104** table of P4.
3105** <li> Table P4 should be a STRICT table.
3106** </ul>
3107**
3108** If any precondition is false, an assertion fault occurs.
drh72532f52021-08-18 19:22:27 +00003109*/
3110case OP_TypeCheck: {
3111 Table *pTab;
3112 Column *aCol;
3113 int i;
3114
3115 assert( pOp->p4type==P4_TABLE );
3116 pTab = pOp->p4.pTab;
3117 assert( pTab->tabFlags & TF_Strict );
drh71c770f2021-08-19 16:29:33 +00003118 assert( pTab->nNVCol==pOp->p2 );
drh72532f52021-08-18 19:22:27 +00003119 aCol = pTab->aCol;
3120 pIn1 = &aMem[pOp->p1];
3121 for(i=0; i<pTab->nCol; i++){
drh926aac52021-11-03 14:02:48 +00003122 if( aCol[i].colFlags & COLFLAG_GENERATED ){
3123 if( aCol[i].colFlags & COLFLAG_VIRTUAL ) continue;
3124 if( pOp->p3 ){ pIn1++; continue; }
3125 }
drh72532f52021-08-18 19:22:27 +00003126 assert( pIn1 < &aMem[pOp->p1+pOp->p2] );
3127 applyAffinity(pIn1, aCol[i].affinity, encoding);
3128 if( (pIn1->flags & MEM_Null)==0 ){
3129 switch( aCol[i].eCType ){
3130 case COLTYPE_BLOB: {
3131 if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error;
3132 break;
3133 }
3134 case COLTYPE_INTEGER:
3135 case COLTYPE_INT: {
3136 if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error;
3137 break;
3138 }
3139 case COLTYPE_TEXT: {
3140 if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error;
3141 break;
3142 }
drh2a0eefd2021-08-21 20:54:19 +00003143 case COLTYPE_REAL: {
drh1f3366c2022-01-17 23:37:25 +00003144 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real );
drh50da6ac2023-01-05 14:41:18 +00003145 assert( (pIn1->flags & MEM_IntReal)==0 );
drh72532f52021-08-18 19:22:27 +00003146 if( pIn1->flags & MEM_Int ){
3147 /* When applying REAL affinity, if the result is still an MEM_Int
3148 ** that will fit in 6 bytes, then change the type to MEM_IntReal
3149 ** so that we keep the high-resolution integer value but know that
3150 ** the type really wants to be REAL. */
3151 testcase( pIn1->u.i==140737488355328LL );
3152 testcase( pIn1->u.i==140737488355327LL );
3153 testcase( pIn1->u.i==-140737488355328LL );
3154 testcase( pIn1->u.i==-140737488355329LL );
3155 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
3156 pIn1->flags |= MEM_IntReal;
3157 pIn1->flags &= ~MEM_Int;
3158 }else{
3159 pIn1->u.r = (double)pIn1->u.i;
3160 pIn1->flags |= MEM_Real;
3161 pIn1->flags &= ~MEM_Int;
3162 }
drh1f3366c2022-01-17 23:37:25 +00003163 }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){
drh72532f52021-08-18 19:22:27 +00003164 goto vdbe_type_error;
3165 }
3166 break;
3167 }
drh2a0eefd2021-08-21 20:54:19 +00003168 default: {
drhb9fd0102021-08-23 10:28:02 +00003169 /* COLTYPE_ANY. Accept anything. */
drh2a0eefd2021-08-21 20:54:19 +00003170 break;
3171 }
drh72532f52021-08-18 19:22:27 +00003172 }
3173 }
3174 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
3175 pIn1++;
3176 }
3177 assert( pIn1 == &aMem[pOp->p1+pOp->p2] );
3178 break;
3179
3180vdbe_type_error:
drhfaf9c772021-08-20 08:05:42 +00003181 sqlite3VdbeError(p, "cannot store %s value in %s column %s.%s",
3182 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1],
3183 pTab->zName, aCol[i].zCnName);
drh72532f52021-08-18 19:22:27 +00003184 rc = SQLITE_CONSTRAINT_DATATYPE;
3185 goto abort_due_to_error;
3186}
3187
danielk1977751de562008-04-18 09:01:15 +00003188/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00003189** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00003190**
3191** Apply affinities to a range of P2 registers starting with P1.
3192**
drhbb6783b2017-04-29 18:02:49 +00003193** P4 is a string that is P2 characters long. The N-th character of the
3194** string indicates the column affinity that should be used for the N-th
danielk1977751de562008-04-18 09:01:15 +00003195** memory cell in the range.
3196*/
3197case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00003198 const char *zAffinity; /* The affinity to be applied */
danielk1977751de562008-04-18 09:01:15 +00003199
drh856c1032009-06-02 15:21:42 +00003200 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00003201 assert( zAffinity!=0 );
drh662c50e2017-04-01 20:14:01 +00003202 assert( pOp->p2>0 );
drh039fc322009-11-17 18:31:47 +00003203 assert( zAffinity[pOp->p2]==0 );
3204 pIn1 = &aMem[pOp->p1];
drh122c5142019-07-29 05:23:01 +00003205 while( 1 /*exit-by-break*/ ){
drh9f6168b2016-03-19 23:32:58 +00003206 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
drhb5f62432019-12-10 02:48:41 +00003207 assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) );
drh83a1daf2019-05-01 18:59:33 +00003208 applyAffinity(pIn1, zAffinity[0], encoding);
3209 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
drh337cc392019-07-29 06:06:53 +00003210 /* When applying REAL affinity, if the result is still an MEM_Int
3211 ** that will fit in 6 bytes, then change the type to MEM_IntReal
3212 ** so that we keep the high-resolution integer value but know that
3213 ** the type really wants to be REAL. */
3214 testcase( pIn1->u.i==140737488355328LL );
3215 testcase( pIn1->u.i==140737488355327LL );
3216 testcase( pIn1->u.i==-140737488355328LL );
3217 testcase( pIn1->u.i==-140737488355329LL );
3218 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
3219 pIn1->flags |= MEM_IntReal;
3220 pIn1->flags &= ~MEM_Int;
3221 }else{
3222 pIn1->u.r = (double)pIn1->u.i;
3223 pIn1->flags |= MEM_Real;
3224 pIn1->flags &= ~MEM_Int;
3225 }
drh83a1daf2019-05-01 18:59:33 +00003226 }
drh6fcc1ec2019-05-01 14:41:47 +00003227 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
drh83a1daf2019-05-01 18:59:33 +00003228 zAffinity++;
3229 if( zAffinity[0]==0 ) break;
drh039fc322009-11-17 18:31:47 +00003230 pIn1++;
drh83a1daf2019-05-01 18:59:33 +00003231 }
danielk1977751de562008-04-18 09:01:15 +00003232 break;
3233}
3234
drh1db639c2008-01-17 02:36:28 +00003235/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003236** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00003237**
drh710c4842010-08-30 01:17:20 +00003238** Convert P2 registers beginning with P1 into the [record format]
3239** use as a data record in a database table or as a key
3240** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00003241**
drhbb6783b2017-04-29 18:02:49 +00003242** P4 may be a string that is P2 characters long. The N-th character of the
3243** string indicates the column affinity that should be used for the N-th
drh9cbf3422008-01-17 16:22:13 +00003244** field of the index key.
drh7a224de2004-06-02 01:22:02 +00003245**
drh8a512562005-11-14 22:29:05 +00003246** The mapping from character to affinity is given by the SQLITE_AFF_
3247** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00003248**
drh05883a32015-06-02 15:32:08 +00003249** If P4 is NULL then all index fields have the affinity BLOB.
drhda369332020-06-29 20:09:04 +00003250**
3251** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM
3252** compile-time option is enabled:
3253**
3254** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index
3255** of the right-most table that can be null-trimmed.
3256**
3257** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value
3258** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to
3259** accept no-change records with serial_type 10. This value is
3260** only used inside an assert() and does not affect the end result.
drh7f057c92005-06-24 03:53:06 +00003261*/
drh1db639c2008-01-17 02:36:28 +00003262case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00003263 Mem *pRec; /* The new record */
3264 u64 nData; /* Number of bytes of data space */
3265 int nHdr; /* Number of bytes of header space */
3266 i64 nByte; /* Data space required for this record */
drh4a335072015-04-11 02:08:48 +00003267 i64 nZero; /* Number of zero bytes at the end of the record */
drh856c1032009-06-02 15:21:42 +00003268 int nVarint; /* Number of bytes in a varint */
3269 u32 serial_type; /* Type field */
3270 Mem *pData0; /* First field to be combined into the record */
3271 Mem *pLast; /* Last field of the record */
3272 int nField; /* Number of fields in the record */
3273 char *zAffinity; /* The affinity string for the record */
drhbe37c122015-10-16 14:54:17 +00003274 u32 len; /* Length of a field */
drhb70b0df2019-04-30 01:08:42 +00003275 u8 *zHdr; /* Where to write next byte of the header */
3276 u8 *zPayload; /* Where to write next byte of the payload */
drh856c1032009-06-02 15:21:42 +00003277
drhf3218fe2004-05-28 08:21:02 +00003278 /* Assuming the record contains N fields, the record format looks
3279 ** like this:
3280 **
drh7a224de2004-06-02 01:22:02 +00003281 ** ------------------------------------------------------------------------
3282 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
3283 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00003284 **
drh9cbf3422008-01-17 16:22:13 +00003285 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00003286 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00003287 **
3288 ** Each type field is a varint representing the serial type of the
3289 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00003290 ** hdr-size field is also a varint which is the offset from the beginning
3291 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00003292 */
drh856c1032009-06-02 15:21:42 +00003293 nData = 0; /* Number of bytes of data space */
3294 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00003295 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00003296 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00003297 zAffinity = pOp->p4.z;
drh9f6168b2016-03-19 23:32:58 +00003298 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00003299 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00003300 nField = pOp->p2;
3301 pLast = &pData0[nField-1];
danielk19778d059842004-05-12 11:24:02 +00003302
drh2b4ded92010-09-27 21:09:31 +00003303 /* Identify the output register */
3304 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
3305 pOut = &aMem[pOp->p3];
3306 memAboutToChange(p, pOut);
3307
drh3e6c0602013-12-10 20:53:01 +00003308 /* Apply the requested affinity to all inputs
3309 */
3310 assert( pData0<=pLast );
3311 if( zAffinity ){
3312 pRec = pData0;
3313 do{
drh5ad12512019-05-09 16:22:51 +00003314 applyAffinity(pRec, zAffinity[0], encoding);
danbe812622019-05-17 15:59:11 +00003315 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
3316 pRec->flags |= MEM_IntReal;
3317 pRec->flags &= ~(MEM_Int);
3318 }
drh5ad12512019-05-09 16:22:51 +00003319 REGISTER_TRACE((int)(pRec-aMem), pRec);
3320 zAffinity++;
3321 pRec++;
drh57bf4a82014-02-17 14:59:22 +00003322 assert( zAffinity[0]==0 || pRec<=pLast );
3323 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00003324 }
3325
drhd447dce2017-01-25 20:55:11 +00003326#ifdef SQLITE_ENABLE_NULL_TRIM
drh585ce192017-01-25 14:58:27 +00003327 /* NULLs can be safely trimmed from the end of the record, as long as
3328 ** as the schema format is 2 or more and none of the omitted columns
3329 ** have a non-NULL default value. Also, the record must be left with
3330 ** at least one field. If P5>0 then it will be one more than the
3331 ** index of the right-most column with a non-NULL default value */
3332 if( pOp->p5 ){
3333 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
3334 pLast--;
3335 nField--;
3336 }
3337 }
drhd447dce2017-01-25 20:55:11 +00003338#endif
drh585ce192017-01-25 14:58:27 +00003339
drhf3218fe2004-05-28 08:21:02 +00003340 /* Loop through the elements that will make up the record to figure
drh76fd7be2019-07-11 19:50:18 +00003341 ** out how much space is required for the new record. After this loop,
3342 ** the Mem.uTemp field of each term should hold the serial-type that will
3343 ** be used for that term in the generated record:
3344 **
3345 ** Mem.uTemp value type
3346 ** --------------- ---------------
3347 ** 0 NULL
3348 ** 1 1-byte signed integer
3349 ** 2 2-byte signed integer
3350 ** 3 3-byte signed integer
3351 ** 4 4-byte signed integer
3352 ** 5 6-byte signed integer
3353 ** 6 8-byte signed integer
3354 ** 7 IEEE float
3355 ** 8 Integer constant 0
3356 ** 9 Integer constant 1
3357 ** 10,11 reserved for expansion
3358 ** N>=12 and even BLOB
3359 ** N>=13 and odd text
3360 **
3361 ** The following additional values are computed:
3362 ** nHdr Number of bytes needed for the record header
3363 ** nData Number of bytes of data space needed for the record
3364 ** nZero Zero bytes at the end of the record
danielk19778d059842004-05-12 11:24:02 +00003365 */
drh038b7bc2013-12-09 23:17:22 +00003366 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00003367 do{
drh2b4ded92010-09-27 21:09:31 +00003368 assert( memIsValid(pRec) );
drhc1da4392019-07-11 19:22:36 +00003369 if( pRec->flags & MEM_Null ){
3370 if( pRec->flags & MEM_Zero ){
drh41fb3672018-01-12 23:18:38 +00003371 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
3372 ** table methods that never invoke sqlite3_result_xxxxx() while
3373 ** computing an unchanging column value in an UPDATE statement.
3374 ** Give such values a special internal-use-only serial-type of 10
3375 ** so that they can be passed through to xUpdate and have
3376 ** a true sqlite3_value_nochange(). */
drhda369332020-06-29 20:09:04 +00003377#ifndef SQLITE_ENABLE_NULL_TRIM
drh41fb3672018-01-12 23:18:38 +00003378 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
drhda369332020-06-29 20:09:04 +00003379#endif
drhc1da4392019-07-11 19:22:36 +00003380 pRec->uTemp = 10;
drh038b7bc2013-12-09 23:17:22 +00003381 }else{
drh76fd7be2019-07-11 19:50:18 +00003382 pRec->uTemp = 0;
drh038b7bc2013-12-09 23:17:22 +00003383 }
drhc1da4392019-07-11 19:22:36 +00003384 nHdr++;
3385 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
3386 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
3387 i64 i = pRec->u.i;
drh9c3bb592019-07-30 21:00:13 +00003388 u64 uu;
drhc1da4392019-07-11 19:22:36 +00003389 testcase( pRec->flags & MEM_Int );
3390 testcase( pRec->flags & MEM_IntReal );
3391 if( i<0 ){
drh9c3bb592019-07-30 21:00:13 +00003392 uu = ~i;
drhc1da4392019-07-11 19:22:36 +00003393 }else{
drh9c3bb592019-07-30 21:00:13 +00003394 uu = i;
drhc1da4392019-07-11 19:22:36 +00003395 }
3396 nHdr++;
drh9c3bb592019-07-30 21:00:13 +00003397 testcase( uu==127 ); testcase( uu==128 );
3398 testcase( uu==32767 ); testcase( uu==32768 );
3399 testcase( uu==8388607 ); testcase( uu==8388608 );
drh16f56e82022-02-21 14:30:59 +00003400 testcase( uu==2147483647 ); testcase( uu==2147483648LL );
drh9c3bb592019-07-30 21:00:13 +00003401 testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL );
3402 if( uu<=127 ){
drha0318fd2022-02-26 23:01:25 +00003403 if( (i&1)==i && p->minWriteFileFormat>=4 ){
drh9c3bb592019-07-30 21:00:13 +00003404 pRec->uTemp = 8+(u32)uu;
drhc1da4392019-07-11 19:22:36 +00003405 }else{
3406 nData++;
3407 pRec->uTemp = 1;
3408 }
drh9c3bb592019-07-30 21:00:13 +00003409 }else if( uu<=32767 ){
drhc1da4392019-07-11 19:22:36 +00003410 nData += 2;
3411 pRec->uTemp = 2;
drh9c3bb592019-07-30 21:00:13 +00003412 }else if( uu<=8388607 ){
drhc1da4392019-07-11 19:22:36 +00003413 nData += 3;
3414 pRec->uTemp = 3;
drh9c3bb592019-07-30 21:00:13 +00003415 }else if( uu<=2147483647 ){
drhc1da4392019-07-11 19:22:36 +00003416 nData += 4;
3417 pRec->uTemp = 4;
drh9c3bb592019-07-30 21:00:13 +00003418 }else if( uu<=140737488355327LL ){
drhc1da4392019-07-11 19:22:36 +00003419 nData += 6;
3420 pRec->uTemp = 5;
3421 }else{
3422 nData += 8;
3423 if( pRec->flags & MEM_IntReal ){
3424 /* If the value is IntReal and is going to take up 8 bytes to store
3425 ** as an integer, then we might as well make it an 8-byte floating
3426 ** point value */
3427 pRec->u.r = (double)pRec->u.i;
3428 pRec->flags &= ~MEM_IntReal;
3429 pRec->flags |= MEM_Real;
3430 pRec->uTemp = 7;
3431 }else{
3432 pRec->uTemp = 6;
3433 }
3434 }
3435 }else if( pRec->flags & MEM_Real ){
3436 nHdr++;
3437 nData += 8;
3438 pRec->uTemp = 7;
3439 }else{
3440 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
3441 assert( pRec->n>=0 );
3442 len = (u32)pRec->n;
3443 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
3444 if( pRec->flags & MEM_Zero ){
3445 serial_type += pRec->u.nZero*2;
3446 if( nData ){
3447 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
3448 len += pRec->u.nZero;
3449 }else{
3450 nZero += pRec->u.nZero;
3451 }
3452 }
3453 nData += len;
3454 nHdr += sqlite3VarintLen(serial_type);
3455 pRec->uTemp = serial_type;
drhfdf972a2007-05-02 13:30:27 +00003456 }
drh45c3c662016-04-07 14:16:16 +00003457 if( pRec==pData0 ) break;
3458 pRec--;
3459 }while(1);
danielk19773d1bfea2004-05-14 11:00:53 +00003460
drh654858d2014-11-20 02:18:14 +00003461 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
3462 ** which determines the total number of bytes in the header. The varint
3463 ** value is the size of the header in bytes including the size varint
3464 ** itself. */
drh59bf00c2013-12-08 23:33:28 +00003465 testcase( nHdr==126 );
3466 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00003467 if( nHdr<=126 ){
3468 /* The common case */
3469 nHdr += 1;
3470 }else{
3471 /* Rare case of a really large header */
3472 nVarint = sqlite3VarintLen(nHdr);
3473 nHdr += nVarint;
3474 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00003475 }
drh038b7bc2013-12-09 23:17:22 +00003476 nByte = nHdr+nData;
drhf3218fe2004-05-28 08:21:02 +00003477
danielk1977a7a8e142008-02-13 18:25:27 +00003478 /* Make sure the output register has a buffer large enough to store
3479 ** the new record. The output register (pOp->p3) is not allowed to
3480 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00003481 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00003482 */
drh0d7f0cc2018-09-21 13:07:14 +00003483 if( nByte+nZero<=pOut->szMalloc ){
3484 /* The output register is already large enough to hold the record.
3485 ** No error checks or buffer enlargement is required */
3486 pOut->z = pOut->zMalloc;
3487 }else{
3488 /* Need to make sure that the output is not too big and then enlarge
3489 ** the output register to hold the full result */
3490 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
3491 goto too_big;
3492 }
3493 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
3494 goto no_mem;
3495 }
danielk19778d059842004-05-12 11:24:02 +00003496 }
drh9c1905f2008-12-10 22:32:56 +00003497 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00003498 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00003499 if( nZero ){
drh8df32842008-12-09 02:51:23 +00003500 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00003501 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00003502 }
drhb7654112008-01-12 12:48:07 +00003503 UPDATE_MAX_BLOBSIZE(pOut);
drhb70b0df2019-04-30 01:08:42 +00003504 zHdr = (u8 *)pOut->z;
3505 zPayload = zHdr + nHdr;
3506
3507 /* Write the record */
drhb47b1f62022-04-01 21:01:37 +00003508 if( nHdr<0x80 ){
3509 *(zHdr++) = nHdr;
3510 }else{
3511 zHdr += sqlite3PutVarint(zHdr,nHdr);
3512 }
drhb70b0df2019-04-30 01:08:42 +00003513 assert( pData0<=pLast );
3514 pRec = pData0;
drh759e5072022-04-01 20:39:40 +00003515 while( 1 /*exit-by-break*/ ){
drhb70b0df2019-04-30 01:08:42 +00003516 serial_type = pRec->uTemp;
3517 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
drhd859dc22022-04-02 14:30:58 +00003518 ** additional varints, one per column.
3519 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
3520 ** immediately follow the header. */
3521 if( serial_type<=7 ){
drhb47b1f62022-04-01 21:01:37 +00003522 *(zHdr++) = serial_type;
drhd859dc22022-04-02 14:30:58 +00003523 if( serial_type==0 ){
3524 /* NULL value. No change in zPayload */
3525 }else{
3526 u64 v;
drh1f416db2022-04-02 20:08:48 +00003527 u32 i;
drhd859dc22022-04-02 14:30:58 +00003528 if( serial_type==7 ){
3529 assert( sizeof(v)==sizeof(pRec->u.r) );
3530 memcpy(&v, &pRec->u.r, sizeof(v));
3531 swapMixedEndianFloat(v);
3532 }else{
3533 v = pRec->u.i;
3534 }
3535 len = i = sqlite3SmallTypeSizes[serial_type];
3536 assert( i>0 );
drh2c144b02022-04-02 15:19:02 +00003537 while( 1 /*exit-by-break*/ ){
drhd859dc22022-04-02 14:30:58 +00003538 zPayload[--i] = (u8)(v&0xFF);
drh2c144b02022-04-02 15:19:02 +00003539 if( i==0 ) break;
drhd859dc22022-04-02 14:30:58 +00003540 v >>= 8;
drh2c144b02022-04-02 15:19:02 +00003541 }
drhd859dc22022-04-02 14:30:58 +00003542 zPayload += len;
3543 }
3544 }else if( serial_type<0x80 ){
3545 *(zHdr++) = serial_type;
drhd13527d2022-04-02 19:21:58 +00003546 if( serial_type>=14 && pRec->n>0 ){
3547 assert( pRec->z!=0 );
drhd859dc22022-04-02 14:30:58 +00003548 memcpy(zPayload, pRec->z, pRec->n);
3549 zPayload += pRec->n;
3550 }
drhb47b1f62022-04-01 21:01:37 +00003551 }else{
3552 zHdr += sqlite3PutVarint(zHdr, serial_type);
drhd13527d2022-04-02 19:21:58 +00003553 if( pRec->n ){
3554 assert( pRec->z!=0 );
3555 memcpy(zPayload, pRec->z, pRec->n);
3556 zPayload += pRec->n;
3557 }
drhb47b1f62022-04-01 21:01:37 +00003558 }
drh759e5072022-04-01 20:39:40 +00003559 if( pRec==pLast ) break;
3560 pRec++;
3561 }
drhb70b0df2019-04-30 01:08:42 +00003562 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
3563 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
3564
3565 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
3566 REGISTER_TRACE(pOp->p3, pOut);
danielk19778d059842004-05-12 11:24:02 +00003567 break;
3568}
3569
drh50fb7e02021-12-06 20:16:53 +00003570/* Opcode: Count P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00003571** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00003572**
3573** Store the number of entries (an integer value) in the table or index
drh9f274632020-03-17 17:11:23 +00003574** opened by cursor P1 in register P2.
3575**
3576** If P3==0, then an exact count is obtained, which involves visiting
3577** every btree page of the table. But if P3 is non-zero, an estimate
3578** is returned based on the current cursor position.
danielk1977a5533162009-02-24 10:01:51 +00003579*/
drh27a348c2015-04-13 19:14:06 +00003580case OP_Count: { /* out2 */
danielk1977a5533162009-02-24 10:01:51 +00003581 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00003582 BtCursor *pCrsr;
3583
drhc960dcb2015-11-20 19:22:01 +00003584 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
3585 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00003586 assert( pCrsr );
drh9f274632020-03-17 17:11:23 +00003587 if( pOp->p3 ){
3588 nEntry = sqlite3BtreeRowCountEst(pCrsr);
3589 }else{
3590 nEntry = 0; /* Not needed. Only used to silence a warning. */
3591 rc = sqlite3BtreeCount(db, pCrsr, &nEntry);
3592 if( rc ) goto abort_due_to_error;
3593 }
drh27a348c2015-04-13 19:14:06 +00003594 pOut = out2Prerelease(p, pOp);
danielk1977a5533162009-02-24 10:01:51 +00003595 pOut->u.i = nEntry;
drh21f6daa2019-10-11 14:21:48 +00003596 goto check_for_interrupt;
danielk1977a5533162009-02-24 10:01:51 +00003597}
danielk1977a5533162009-02-24 10:01:51 +00003598
danielk1977fd7f0452008-12-17 17:30:26 +00003599/* Opcode: Savepoint P1 * * P4 *
3600**
3601** Open, release or rollback the savepoint named by parameter P4, depending
drh2ce9b6b2019-05-10 14:03:07 +00003602** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
3603** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
3604** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
danielk1977fd7f0452008-12-17 17:30:26 +00003605*/
3606case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00003607 int p1; /* Value of P1 operand */
3608 char *zName; /* Name of savepoint */
3609 int nName;
3610 Savepoint *pNew;
3611 Savepoint *pSavepoint;
3612 Savepoint *pTmp;
3613 int iSavepoint;
3614 int ii;
3615
3616 p1 = pOp->p1;
3617 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00003618
3619 /* Assert that the p1 parameter is valid. Also that if there is no open
3620 ** transaction, then there cannot be any savepoints.
3621 */
3622 assert( db->pSavepoint==0 || db->autoCommit==0 );
3623 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
3624 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
3625 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00003626 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00003627
3628 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00003629 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00003630 /* A new savepoint cannot be created if there are active write
3631 ** statements (i.e. open read/write incremental blob handles).
3632 */
drh22c17b82015-05-15 04:13:15 +00003633 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003634 rc = SQLITE_BUSY;
3635 }else{
drh856c1032009-06-02 15:21:42 +00003636 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003637
drhbe07ec52011-06-03 12:15:26 +00003638#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00003639 /* This call is Ok even if this savepoint is actually a transaction
3640 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
3641 ** If this is a transaction savepoint being opened, it is guaranteed
3642 ** that the db->aVTrans[] array is empty. */
3643 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00003644 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
3645 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00003646 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00003647#endif
dand9495cd2011-04-27 12:08:04 +00003648
danielk1977fd7f0452008-12-17 17:30:26 +00003649 /* Create a new savepoint structure. */
drh575fad62016-02-05 13:38:36 +00003650 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
danielk1977fd7f0452008-12-17 17:30:26 +00003651 if( pNew ){
3652 pNew->zName = (char *)&pNew[1];
3653 memcpy(pNew->zName, zName, nName+1);
3654
3655 /* If there is no open transaction, then mark this as a special
3656 ** "transaction savepoint". */
3657 if( db->autoCommit ){
3658 db->autoCommit = 0;
3659 db->isTransactionSavepoint = 1;
3660 }else{
3661 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00003662 }
dan21e8d012011-03-03 20:05:59 +00003663
danielk1977fd7f0452008-12-17 17:30:26 +00003664 /* Link the new savepoint into the database handle's list. */
3665 pNew->pNext = db->pSavepoint;
3666 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00003667 pNew->nDeferredCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003668 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003669 }
3670 }
3671 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003672 assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK );
drh856c1032009-06-02 15:21:42 +00003673 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00003674
3675 /* Find the named savepoint. If there is no such savepoint, then an
3676 ** an error is returned to the user. */
3677 for(
drh856c1032009-06-02 15:21:42 +00003678 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003679 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00003680 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00003681 ){
3682 iSavepoint++;
3683 }
3684 if( !pSavepoint ){
drh22c17b82015-05-15 04:13:15 +00003685 sqlite3VdbeError(p, "no such savepoint: %s", zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003686 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00003687 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00003688 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00003689 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00003690 */
drh22c17b82015-05-15 04:13:15 +00003691 sqlite3VdbeError(p, "cannot release savepoint - "
3692 "SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003693 rc = SQLITE_BUSY;
3694 }else{
3695
3696 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00003697 ** and this is a RELEASE command, then the current transaction
3698 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00003699 */
3700 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
3701 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00003702 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003703 goto vdbe_return;
3704 }
danielk1977fd7f0452008-12-17 17:30:26 +00003705 db->autoCommit = 1;
3706 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00003707 p->pc = (int)(pOp - aOp);
danielk1977fd7f0452008-12-17 17:30:26 +00003708 db->autoCommit = 0;
3709 p->rc = rc = SQLITE_BUSY;
3710 goto vdbe_return;
3711 }
danielk197734cf35d2008-12-18 18:31:38 +00003712 rc = p->rc;
drh94649b62019-12-18 02:12:04 +00003713 if( rc ){
3714 db->autoCommit = 0;
3715 }else{
3716 db->isTransactionSavepoint = 0;
3717 }
danielk1977fd7f0452008-12-17 17:30:26 +00003718 }else{
drh47b7fc72014-11-11 01:33:57 +00003719 int isSchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003720 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00003721 if( p1==SAVEPOINT_ROLLBACK ){
drh8257aa82017-07-26 19:59:13 +00003722 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
drh31f10052012-03-31 17:17:26 +00003723 for(ii=0; ii<db->nDb; ii++){
drh77b1dee2014-11-17 17:13:06 +00003724 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
3725 SQLITE_ABORT_ROLLBACK,
drh47b7fc72014-11-11 01:33:57 +00003726 isSchemaChange==0);
dan80231042014-11-12 14:56:02 +00003727 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh31f10052012-03-31 17:17:26 +00003728 }
drh47b7fc72014-11-11 01:33:57 +00003729 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003730 assert( p1==SAVEPOINT_RELEASE );
drh47b7fc72014-11-11 01:33:57 +00003731 isSchemaChange = 0;
drh0f198a72012-02-13 16:43:16 +00003732 }
3733 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00003734 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
3735 if( rc!=SQLITE_OK ){
3736 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00003737 }
danielk1977fd7f0452008-12-17 17:30:26 +00003738 }
drh47b7fc72014-11-11 01:33:57 +00003739 if( isSchemaChange ){
drhba968db2018-07-24 22:02:12 +00003740 sqlite3ExpirePreparedStatements(db, 0);
drh81028a42012-05-15 18:28:27 +00003741 sqlite3ResetAllSchemasOfConnection(db);
drh8257aa82017-07-26 19:59:13 +00003742 db->mDbFlags |= DBFLAG_SchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003743 }
3744 }
drh95866af2019-12-15 00:36:33 +00003745 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003746
3747 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3748 ** savepoints nested inside of the savepoint being operated on. */
3749 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00003750 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003751 db->pSavepoint = pTmp->pNext;
3752 sqlite3DbFree(db, pTmp);
3753 db->nSavepoint--;
3754 }
3755
dan1da40a32009-09-19 17:00:31 +00003756 /* If it is a RELEASE, then destroy the savepoint being operated on
3757 ** too. If it is a ROLLBACK TO, then set the number of deferred
3758 ** constraint violations present in the database to the value stored
3759 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00003760 if( p1==SAVEPOINT_RELEASE ){
3761 assert( pSavepoint==db->pSavepoint );
3762 db->pSavepoint = pSavepoint->pNext;
3763 sqlite3DbFree(db, pSavepoint);
3764 if( !isTransaction ){
3765 db->nSavepoint--;
3766 }
dan1da40a32009-09-19 17:00:31 +00003767 }else{
drh2ce9b6b2019-05-10 14:03:07 +00003768 assert( p1==SAVEPOINT_ROLLBACK );
dan1da40a32009-09-19 17:00:31 +00003769 db->nDeferredCons = pSavepoint->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003770 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003771 }
dand9495cd2011-04-27 12:08:04 +00003772
danea8562e2015-04-18 16:25:54 +00003773 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
dand9495cd2011-04-27 12:08:04 +00003774 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
3775 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3776 }
danielk1977fd7f0452008-12-17 17:30:26 +00003777 }
3778 }
drh9467abf2016-02-17 18:44:11 +00003779 if( rc ) goto abort_due_to_error;
drh8703edd2022-04-03 22:35:13 +00003780 if( p->eVdbeState==VDBE_HALT_STATE ){
3781 rc = SQLITE_DONE;
3782 goto vdbe_return;
3783 }
danielk1977fd7f0452008-12-17 17:30:26 +00003784 break;
3785}
3786
drh98757152008-01-09 23:04:12 +00003787/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00003788**
3789** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00003790** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00003791** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3792** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00003793**
3794** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00003795*/
drh9cbf3422008-01-17 16:22:13 +00003796case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00003797 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00003798 int iRollback;
danielk19771d850a72004-05-31 08:26:49 +00003799
drh856c1032009-06-02 15:21:42 +00003800 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00003801 iRollback = pOp->p2;
drhad4a4b82008-11-05 16:37:34 +00003802 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00003803 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00003804 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00003805 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00003806
drhb0c88652016-02-01 13:21:13 +00003807 if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00003808 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00003809 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00003810 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00003811 db->autoCommit = 1;
drhb0c88652016-02-01 13:21:13 +00003812 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3813 /* If this instruction implements a COMMIT and other VMs are writing
3814 ** return an error indicating that the other VMs must complete first.
3815 */
3816 sqlite3VdbeError(p, "cannot commit transaction - "
3817 "SQL statements in progress");
3818 rc = SQLITE_BUSY;
drh9467abf2016-02-17 18:44:11 +00003819 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00003820 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003821 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00003822 }else{
shane7d3846a2008-12-11 02:58:26 +00003823 db->autoCommit = (u8)desiredAutoCommit;
drh8ff25872015-07-31 18:59:56 +00003824 }
3825 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3826 p->pc = (int)(pOp - aOp);
3827 db->autoCommit = (u8)(1-desiredAutoCommit);
3828 p->rc = rc = SQLITE_BUSY;
3829 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003830 }
danielk1977fd7f0452008-12-17 17:30:26 +00003831 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00003832 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00003833 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00003834 }else{
drh900b31e2007-08-28 02:27:51 +00003835 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00003836 }
drh900b31e2007-08-28 02:27:51 +00003837 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003838 }else{
drh22c17b82015-05-15 04:13:15 +00003839 sqlite3VdbeError(p,
drhad4a4b82008-11-05 16:37:34 +00003840 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00003841 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00003842 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00003843
3844 rc = SQLITE_ERROR;
drh9467abf2016-02-17 18:44:11 +00003845 goto abort_due_to_error;
drh663fc632002-02-02 18:49:19 +00003846 }
drh8616cff2019-07-13 16:15:23 +00003847 /*NOTREACHED*/ assert(0);
drh663fc632002-02-02 18:49:19 +00003848}
3849
drhb22f7c82014-02-06 23:56:27 +00003850/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003851**
drh05a86c52014-02-16 01:55:49 +00003852** Begin a transaction on database P1 if a transaction is not already
3853** active.
3854** If P2 is non-zero, then a write-transaction is started, or if a
3855** read-transaction is already active, it is upgraded to a write-transaction.
drh1ca037f2020-10-12 13:24:00 +00003856** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
3857** then an exclusive transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00003858**
drh001bbcb2003-03-19 03:14:00 +00003859** P1 is the index of the database file on which the transaction is
3860** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00003861** file used for temporary tables. Indices of 2 or more are used for
3862** attached databases.
drhcabb0812002-09-14 13:47:32 +00003863**
dane0af83a2009-09-08 19:15:01 +00003864** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3865** true (this flag is set if the Vdbe may modify more than one row and may
3866** throw an ABORT exception), a statement transaction may also be opened.
3867** More specifically, a statement transaction is opened iff the database
3868** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00003869** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00003870** VDBE to be rolled back after an error without having to roll back the
3871** entire transaction. If no error is encountered, the statement transaction
3872** will automatically commit when the VDBE halts.
3873**
drhb22f7c82014-02-06 23:56:27 +00003874** If P5!=0 then this opcode also checks the schema cookie against P3
3875** and the schema generation counter against P4.
3876** The cookie changes its value whenever the database schema changes.
3877** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00003878** and that the current process needs to reread the schema. If the schema
3879** cookie in P3 differs from the schema cookie in the database header or
3880** if the schema generation counter in P4 differs from the current
3881** generation counter, then an SQLITE_SCHEMA error is raised and execution
3882** halts. The sqlite3_step() wrapper function might then reprepare the
3883** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003884*/
drh9cbf3422008-01-17 16:22:13 +00003885case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003886 Btree *pBt;
drh8ee75f72022-04-03 10:42:06 +00003887 Db *pDb;
drhbb2d9b12018-06-06 16:28:40 +00003888 int iMeta = 0;
danielk19771d850a72004-05-31 08:26:49 +00003889
drh1713afb2013-06-28 01:24:57 +00003890 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003891 assert( p->readOnly==0 || pOp->p2==0 );
drh1ca037f2020-10-12 13:24:00 +00003892 assert( pOp->p2>=0 && pOp->p2<=2 );
drh653b82a2009-06-22 11:10:47 +00003893 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003894 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh8cb63f52021-10-29 09:59:06 +00003895 assert( rc==SQLITE_OK );
drh46c425b2021-11-10 10:59:10 +00003896 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){
3897 if( db->flags & SQLITE_QueryOnly ){
3898 /* Writes prohibited by the "PRAGMA query_only=TRUE" statement */
3899 rc = SQLITE_READONLY;
3900 }else{
3901 /* Writes prohibited due to a prior SQLITE_CORRUPT in the current
3902 ** transaction */
3903 rc = SQLITE_CORRUPT;
3904 }
drh13447bf2013-07-10 13:33:49 +00003905 goto abort_due_to_error;
3906 }
drh8ee75f72022-04-03 10:42:06 +00003907 pDb = &db->aDb[pOp->p1];
3908 pBt = pDb->pBt;
danielk19771d850a72004-05-31 08:26:49 +00003909
danielk197724162fe2004-06-04 06:22:00 +00003910 if( pBt ){
drhbb2d9b12018-06-06 16:28:40 +00003911 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
drhcbd8db32015-08-20 17:18:32 +00003912 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3913 testcase( rc==SQLITE_BUSY_RECOVERY );
drh9e9f1bd2009-10-13 15:36:51 +00003914 if( rc!=SQLITE_OK ){
drhfadd2b12016-09-19 23:39:34 +00003915 if( (rc&0xff)==SQLITE_BUSY ){
3916 p->pc = (int)(pOp - aOp);
3917 p->rc = rc;
3918 goto vdbe_return;
3919 }
danielk197724162fe2004-06-04 06:22:00 +00003920 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003921 }
dane0af83a2009-09-08 19:15:01 +00003922
drh4d294482019-10-05 15:28:24 +00003923 if( p->usesStmtJournal
3924 && pOp->p2
danc0537fe2013-06-28 19:41:43 +00003925 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003926 ){
drh99744fa2020-08-25 19:09:07 +00003927 assert( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE );
dane0af83a2009-09-08 19:15:01 +00003928 if( p->iStatement==0 ){
3929 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3930 db->nStatement++;
3931 p->iStatement = db->nSavepoint + db->nStatement;
3932 }
dana311b802011-04-26 19:21:34 +00003933
drh346506f2011-05-25 01:16:42 +00003934 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003935 if( rc==SQLITE_OK ){
3936 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3937 }
dan1da40a32009-09-19 17:00:31 +00003938
3939 /* Store the current value of the database handles deferred constraint
3940 ** counter. If the statement transaction needs to be rolled back,
3941 ** the value of this counter needs to be restored too. */
3942 p->nStmtDefCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003943 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003944 }
drh397776a2018-06-06 17:45:51 +00003945 }
3946 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
drhe4e1af52021-10-29 16:19:03 +00003947 if( rc==SQLITE_OK
3948 && pOp->p5
drh8ee75f72022-04-03 10:42:06 +00003949 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i)
drh397776a2018-06-06 17:45:51 +00003950 ){
dand2ffc972020-12-10 19:20:15 +00003951 /*
3952 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3953 ** version is checked to ensure that the schema has not changed since the
3954 ** SQL statement was prepared.
3955 */
3956 sqlite3DbFree(db, p->zErrMsg);
3957 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
drhb22f7c82014-02-06 23:56:27 +00003958 /* If the schema-cookie from the database file matches the cookie
3959 ** stored with the in-memory representation of the schema, do
3960 ** not reload the schema from the database file.
3961 **
3962 ** If virtual-tables are in use, this is not just an optimization.
3963 ** Often, v-tables store their data in other SQLite tables, which
3964 ** are queried from within xNext() and other v-table methods using
3965 ** prepared queries. If such a query is out-of-date, we do not want to
3966 ** discard the database schema, as the user code implementing the
3967 ** v-table would have to be ready for the sqlite3_vtab structure itself
3968 ** to be invalidated whenever sqlite3_step() is called from within
3969 ** a v-table method.
3970 */
3971 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3972 sqlite3ResetOneSchema(db, pOp->p1);
3973 }
3974 p->expired = 1;
3975 rc = SQLITE_SCHEMA;
dan0a841a22022-06-08 18:20:36 +00003976
3977 /* Set changeCntOn to 0 to prevent the value returned by sqlite3_changes()
3978 ** from being modified in sqlite3VdbeHalt(). If this statement is
3979 ** reprepared, changeCntOn will be set again. */
3980 p->changeCntOn = 0;
drhb86ccfb2003-01-28 23:13:10 +00003981 }
drh9467abf2016-02-17 18:44:11 +00003982 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003983 break;
3984}
3985
drhb1fdb2a2008-01-05 04:06:03 +00003986/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003987**
drh9cbf3422008-01-17 16:22:13 +00003988** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003989** P3==1 is the schema version. P3==2 is the database format.
3990** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003991** the main database file and P1==1 is the database file used to store
3992** temporary tables.
drh4a324312001-12-21 14:30:42 +00003993**
drh50e5dad2001-09-15 00:57:28 +00003994** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003995** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003996** executing this instruction.
3997*/
drh27a348c2015-04-13 19:14:06 +00003998case OP_ReadCookie: { /* out2 */
drhf328bc82004-05-10 23:29:49 +00003999 int iMeta;
drh856c1032009-06-02 15:21:42 +00004000 int iDb;
4001 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00004002
drh1713afb2013-06-28 01:24:57 +00004003 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00004004 iDb = pOp->p1;
4005 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00004006 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00004007 assert( iDb>=0 && iDb<db->nDb );
4008 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00004009 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00004010
danielk1977602b4662009-07-02 07:47:33 +00004011 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh27a348c2015-04-13 19:14:06 +00004012 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00004013 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00004014 break;
4015}
4016
drhe3863b52020-07-01 16:19:14 +00004017/* Opcode: SetCookie P1 P2 P3 * P5
drh50e5dad2001-09-15 00:57:28 +00004018**
drh1861afc2016-02-01 21:48:34 +00004019** Write the integer value P3 into cookie number P2 of database P1.
4020** P2==1 is the schema version. P2==2 is the database format.
4021** P2==3 is the recommended pager cache
danielk19770d19f7a2009-06-03 11:25:07 +00004022** size, and so forth. P1==0 is the main database file and P1==1 is the
4023** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00004024**
4025** A transaction must be started before executing this opcode.
drhe3863b52020-07-01 16:19:14 +00004026**
4027** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal
4028** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
4029** has P5 set to 1, so that the internal schema version will be different
4030** from the database schema version, resulting in a schema reset.
drh50e5dad2001-09-15 00:57:28 +00004031*/
drh1861afc2016-02-01 21:48:34 +00004032case OP_SetCookie: {
drh3f7d4e42004-07-24 14:35:58 +00004033 Db *pDb;
drh4031baf2018-05-28 17:31:20 +00004034
4035 sqlite3VdbeIncrWriteCounter(p, 0);
drh4a324312001-12-21 14:30:42 +00004036 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00004037 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00004038 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00004039 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00004040 pDb = &db->aDb[pOp->p1];
4041 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00004042 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drha3b321d2004-05-11 09:31:31 +00004043 /* See note about index shifting on OP_ReadCookie */
drh1861afc2016-02-01 21:48:34 +00004044 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
danielk19770d19f7a2009-06-03 11:25:07 +00004045 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00004046 /* When the schema cookie changes, record the new cookie internally */
drh86bc5e42022-06-24 12:56:48 +00004047 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5;
drh8257aa82017-07-26 19:59:13 +00004048 db->mDbFlags |= DBFLAG_SchemaChange;
drh44a5c022022-01-02 12:01:03 +00004049 sqlite3FkClearTriggerCache(db, pOp->p1);
danielk19770d19f7a2009-06-03 11:25:07 +00004050 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00004051 /* Record changes in the file format */
drh1861afc2016-02-01 21:48:34 +00004052 pDb->pSchema->file_format = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00004053 }
drhfd426c62006-01-30 15:34:22 +00004054 if( pOp->p1==1 ){
4055 /* Invalidate all prepared statements whenever the TEMP database
4056 ** schema is changed. Ticket #1644 */
drhba968db2018-07-24 22:02:12 +00004057 sqlite3ExpirePreparedStatements(db, 0);
danfa401de2009-10-16 14:55:03 +00004058 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00004059 }
drh9467abf2016-02-17 18:44:11 +00004060 if( rc ) goto abort_due_to_error;
drh50e5dad2001-09-15 00:57:28 +00004061 break;
4062}
4063
drh98757152008-01-09 23:04:12 +00004064/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004065** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00004066**
drhecdc7532001-09-23 02:35:53 +00004067** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00004068** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00004069** P3==0 means the main database, P3==1 means the database used for
4070** temporary tables, and P3>1 means used the corresponding attached
4071** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00004072** values need not be contiguous but all P1 values should be small integers.
4073** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00004074**
drh8e9deb62018-06-05 13:43:02 +00004075** Allowed P5 bits:
4076** <ul>
4077** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
4078** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
drh576d0a92020-03-12 17:28:27 +00004079** of OP_SeekLE/OP_IdxLT)
drh8e9deb62018-06-05 13:43:02 +00004080** </ul>
drhb19a2bc2001-09-16 00:13:26 +00004081**
danielk1977d336e222009-02-20 10:58:41 +00004082** The P4 value may be either an integer (P4_INT32) or a pointer to
4083** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00004084** object, then table being opened must be an [index b-tree] where the
4085** KeyInfo object defines the content and collating
4086** sequence of that index b-tree. Otherwise, if P4 is an integer
4087** value, then the table being opened must be a [table b-tree] with a
4088** number of columns no less than the value of P4.
drhf57b3392001-10-08 13:22:32 +00004089**
drh35263192014-07-22 20:02:19 +00004090** See also: OpenWrite, ReopenIdx
4091*/
4092/* Opcode: ReopenIdx P1 P2 P3 P4 P5
4093** Synopsis: root=P2 iDb=P3
4094**
drh8e9deb62018-06-05 13:43:02 +00004095** The ReopenIdx opcode works like OP_OpenRead except that it first
4096** checks to see if the cursor on P1 is already open on the same
4097** b-tree and if it is this opcode becomes a no-op. In other words,
drh35263192014-07-22 20:02:19 +00004098** if the cursor is already open, do not reopen it.
4099**
drh8e9deb62018-06-05 13:43:02 +00004100** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
4101** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
4102** be the same as every other ReopenIdx or OpenRead for the same cursor
4103** number.
drh35263192014-07-22 20:02:19 +00004104**
drh8e9deb62018-06-05 13:43:02 +00004105** Allowed P5 bits:
4106** <ul>
4107** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
4108** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
drh576d0a92020-03-12 17:28:27 +00004109** of OP_SeekLE/OP_IdxLT)
drh8e9deb62018-06-05 13:43:02 +00004110** </ul>
4111**
4112** See also: OP_OpenRead, OP_OpenWrite
drh5e00f6c2001-09-13 13:46:56 +00004113*/
drh98757152008-01-09 23:04:12 +00004114/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004115** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00004116**
4117** Open a read/write cursor named P1 on the table or index whose root
drh8e9deb62018-06-05 13:43:02 +00004118** page is P2 (or whose root page is held in register P2 if the
4119** OPFLAG_P2ISREG bit is set in P5 - see below).
drhecdc7532001-09-23 02:35:53 +00004120**
danielk1977d336e222009-02-20 10:58:41 +00004121** The P4 value may be either an integer (P4_INT32) or a pointer to
4122** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
drh8e9deb62018-06-05 13:43:02 +00004123** object, then table being opened must be an [index b-tree] where the
4124** KeyInfo object defines the content and collating
4125** sequence of that index b-tree. Otherwise, if P4 is an integer
4126** value, then the table being opened must be a [table b-tree] with a
4127** number of columns no less than the value of P4.
jplyon5a564222003-06-02 06:15:58 +00004128**
drh8e9deb62018-06-05 13:43:02 +00004129** Allowed P5 bits:
4130** <ul>
4131** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
4132** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
drh576d0a92020-03-12 17:28:27 +00004133** of OP_SeekLE/OP_IdxLT)
drh8e9deb62018-06-05 13:43:02 +00004134** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
4135** and subsequently delete entries in an index btree. This is a
4136** hint to the storage engine that the storage engine is allowed to
4137** ignore. The hint is not used by the official SQLite b*tree storage
4138** engine, but is used by COMDB2.
4139** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
4140** as the root page, not the value of P2 itself.
4141** </ul>
drhf57b3392001-10-08 13:22:32 +00004142**
drh8e9deb62018-06-05 13:43:02 +00004143** This instruction works like OpenRead except that it opens the cursor
4144** in read/write mode.
4145**
4146** See also: OP_OpenRead, OP_ReopenIdx
drhecdc7532001-09-23 02:35:53 +00004147*/
dan2adb3092022-12-06 18:48:06 +00004148case OP_ReopenIdx: { /* ncycle */
drh856c1032009-06-02 15:21:42 +00004149 int nField;
4150 KeyInfo *pKeyInfo;
drhabc38152020-07-22 13:38:04 +00004151 u32 p2;
drh856c1032009-06-02 15:21:42 +00004152 int iDb;
drhf57b3392001-10-08 13:22:32 +00004153 int wrFlag;
4154 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00004155 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00004156 Db *pDb;
drh856c1032009-06-02 15:21:42 +00004157
drhe0997b32015-03-20 14:57:50 +00004158 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh35263192014-07-22 20:02:19 +00004159 assert( pOp->p4type==P4_KEYINFO );
4160 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00004161 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00004162 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
drh4422b3a2021-06-25 14:48:24 +00004163 assert( pCur->eCurType==CURTYPE_BTREE );
4164 sqlite3BtreeClearCursor(pCur->uc.pCursor);
drhe0997b32015-03-20 14:57:50 +00004165 goto open_cursor_set_hints;
drh35263192014-07-22 20:02:19 +00004166 }
4167 /* If the cursor is not currently open or is open on a different
4168 ** index, then fall through into OP_OpenRead to force a reopen */
dan2adb3092022-12-06 18:48:06 +00004169case OP_OpenRead: /* ncycle */
drh1fa509a2015-03-20 16:34:49 +00004170case OP_OpenWrite:
drh856c1032009-06-02 15:21:42 +00004171
drhe0997b32015-03-20 14:57:50 +00004172 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh1713afb2013-06-28 01:24:57 +00004173 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00004174 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
4175 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00004176
drhba968db2018-07-24 22:02:12 +00004177 if( p->expired==1 ){
drh47b7fc72014-11-11 01:33:57 +00004178 rc = SQLITE_ABORT_ROLLBACK;
drh9467abf2016-02-17 18:44:11 +00004179 goto abort_due_to_error;
danfa401de2009-10-16 14:55:03 +00004180 }
4181
drh856c1032009-06-02 15:21:42 +00004182 nField = 0;
4183 pKeyInfo = 0;
drhabc38152020-07-22 13:38:04 +00004184 p2 = (u32)pOp->p2;
drh856c1032009-06-02 15:21:42 +00004185 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00004186 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00004187 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00004188 pDb = &db->aDb[iDb];
4189 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00004190 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00004191 if( pOp->opcode==OP_OpenWrite ){
danfd261ec2015-10-22 20:54:33 +00004192 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
4193 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
drh21206082011-04-04 18:22:02 +00004194 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00004195 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
4196 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00004197 }
4198 }else{
4199 wrFlag = 0;
4200 }
dan428c2182012-08-06 18:50:11 +00004201 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00004202 assert( p2>0 );
mistachkincec5f1d2020-08-04 16:11:37 +00004203 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
drh8e9deb62018-06-05 13:43:02 +00004204 assert( pOp->opcode==OP_OpenWrite );
drha6c2ed92009-11-14 23:22:23 +00004205 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00004206 assert( memIsValid(pIn2) );
4207 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00004208 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00004209 p2 = (int)pIn2->u.i;
drh0f3f7662017-08-18 14:34:28 +00004210 /* The p2 value always comes from a prior OP_CreateBtree opcode and
drh9a65f2c2009-06-22 19:05:40 +00004211 ** that opcode will always set the p2 value to 2 or more or else fail.
4212 ** If there were a failure, the prepared statement would have halted
4213 ** before reaching this instruction. */
drh9467abf2016-02-17 18:44:11 +00004214 assert( p2>=2 );
drh5edc3122001-09-13 21:53:09 +00004215 }
danielk1977d336e222009-02-20 10:58:41 +00004216 if( pOp->p4type==P4_KEYINFO ){
4217 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00004218 assert( pKeyInfo->enc==ENC(db) );
4219 assert( pKeyInfo->db==db );
drha485ad12017-08-02 22:43:14 +00004220 nField = pKeyInfo->nAllField;
danielk1977d336e222009-02-20 10:58:41 +00004221 }else if( pOp->p4type==P4_INT32 ){
4222 nField = pOp->p4.i;
4223 }
drh653b82a2009-06-22 11:10:47 +00004224 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00004225 assert( nField>=0 );
4226 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drhb2486682022-01-03 01:43:28 +00004227 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00004228 if( pCur==0 ) goto no_mem;
drhb2486682022-01-03 01:43:28 +00004229 pCur->iDb = iDb;
drhf328bc82004-05-10 23:29:49 +00004230 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00004231 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00004232 pCur->pgnoRoot = p2;
drhb89aeb62016-01-27 15:49:32 +00004233#ifdef SQLITE_DEBUG
4234 pCur->wrFlag = wrFlag;
4235#endif
drhc960dcb2015-11-20 19:22:01 +00004236 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
danielk1977d336e222009-02-20 10:58:41 +00004237 pCur->pKeyInfo = pKeyInfo;
drh14da87f2013-11-20 21:51:33 +00004238 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00004239 ** SQLite used to check if the root-page flags were sane at this point
4240 ** and report database corruption if they were not, but this check has
4241 ** since moved into the btree layer. */
4242 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhe0997b32015-03-20 14:57:50 +00004243
4244open_cursor_set_hints:
4245 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
4246 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
drh0403cb32015-08-14 23:57:04 +00004247 testcase( pOp->p5 & OPFLAG_BULKCSR );
drh0403cb32015-08-14 23:57:04 +00004248 testcase( pOp->p2 & OPFLAG_SEEKEQ );
drhc960dcb2015-11-20 19:22:01 +00004249 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
drhf7854c72015-10-27 13:24:37 +00004250 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
drh9467abf2016-02-17 18:44:11 +00004251 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00004252 break;
4253}
4254
drhe08e8d62017-05-01 15:15:41 +00004255/* Opcode: OpenDup P1 P2 * * *
4256**
4257** Open a new cursor P1 that points to the same ephemeral table as
4258** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
4259** opcode. Only ephemeral cursors may be duplicated.
4260**
4261** Duplicate ephemeral cursors are used for self-joins of materialized views.
4262*/
dan2adb3092022-12-06 18:48:06 +00004263case OP_OpenDup: { /* ncycle */
drhe08e8d62017-05-01 15:15:41 +00004264 VdbeCursor *pOrig; /* The original cursor to be duplicated */
4265 VdbeCursor *pCx; /* The new cursor */
4266
4267 pOrig = p->apCsr[pOp->p2];
dan2811ea62019-12-23 14:20:46 +00004268 assert( pOrig );
drh5a4a15f2021-03-18 15:42:59 +00004269 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */
drhe08e8d62017-05-01 15:15:41 +00004270
drhb2486682022-01-03 01:43:28 +00004271 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE);
drhe08e8d62017-05-01 15:15:41 +00004272 if( pCx==0 ) goto no_mem;
4273 pCx->nullRow = 1;
4274 pCx->isEphemeral = 1;
4275 pCx->pKeyInfo = pOrig->pKeyInfo;
4276 pCx->isTable = pOrig->isTable;
drh2c041312018-12-24 02:34:49 +00004277 pCx->pgnoRoot = pOrig->pgnoRoot;
dana0f6b832019-03-14 16:36:20 +00004278 pCx->isOrdered = pOrig->isOrdered;
drhb2486682022-01-03 01:43:28 +00004279 pCx->ub.pBtx = pOrig->ub.pBtx;
drh27a242c2022-06-14 22:21:23 +00004280 pCx->noReuse = 1;
4281 pOrig->noReuse = 1;
drhb2486682022-01-03 01:43:28 +00004282 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
drhe08e8d62017-05-01 15:15:41 +00004283 pCx->pKeyInfo, pCx->uc.pCursor);
drh3f4df4c2017-05-02 17:54:19 +00004284 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
4285 ** opened for a database. Since there is already an open cursor when this
4286 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
4287 assert( rc==SQLITE_OK );
drhe08e8d62017-05-01 15:15:41 +00004288 break;
4289}
4290
4291
drh32881be2020-11-17 21:26:13 +00004292/* Opcode: OpenEphemeral P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004293** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00004294**
drhb9bb7c12006-06-11 23:41:55 +00004295** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00004296** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00004297** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00004298** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00004299**
drhdfe3b582019-01-04 12:35:50 +00004300** If the cursor P1 is already opened on an ephemeral table, the table
drh4afdfa12018-12-31 16:36:42 +00004301** is cleared (all content is erased).
4302**
drh25d3adb2010-04-05 15:11:08 +00004303** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00004304** The cursor points to a BTree table if P4==0 and to a BTree index
4305** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00004306** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00004307**
drh2a5d9902011-08-26 00:34:45 +00004308** The P5 parameter can be a mask of the BTREE_* flags defined
4309** in btree.h. These flags control aspects of the operation of
4310** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
4311** added automatically.
drh32881be2020-11-17 21:26:13 +00004312**
4313** If P3 is positive, then reg[P3] is modified slightly so that it
4314** can be used as zero-length data for OP_Insert. This is an optimization
4315** that avoids an extra OP_Blob opcode to initialize that register.
drh5e00f6c2001-09-13 13:46:56 +00004316*/
drha21a64d2010-04-06 22:33:55 +00004317/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00004318** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00004319**
4320** This opcode works the same as OP_OpenEphemeral. It has a
4321** different name to distinguish its use. Tables created using
4322** by this opcode will be used for automatically created transient
4323** indices in joins.
4324*/
dan2adb3092022-12-06 18:48:06 +00004325case OP_OpenAutoindex: /* ncycle */
4326case OP_OpenEphemeral: { /* ncycle */
drhdfe88ec2008-11-03 20:55:06 +00004327 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00004328 KeyInfo *pKeyInfo;
4329
drhd4187c72010-08-30 22:15:45 +00004330 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00004331 SQLITE_OPEN_READWRITE |
4332 SQLITE_OPEN_CREATE |
4333 SQLITE_OPEN_EXCLUSIVE |
4334 SQLITE_OPEN_DELETEONCLOSE |
4335 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00004336 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00004337 assert( pOp->p2>=0 );
drh32881be2020-11-17 21:26:13 +00004338 if( pOp->p3>0 ){
4339 /* Make register reg[P3] into a value that can be used as the data
4340 ** form sqlite3BtreeInsert() where the length of the data is zero. */
4341 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
4342 assert( pOp->opcode==OP_OpenEphemeral );
4343 assert( aMem[pOp->p3].flags & MEM_Null );
4344 aMem[pOp->p3].n = 0;
4345 aMem[pOp->p3].z = "";
4346 }
drh4afdfa12018-12-31 16:36:42 +00004347 pCx = p->apCsr[pOp->p1];
drh27a242c2022-06-14 22:21:23 +00004348 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField) ){
drh5a4a15f2021-03-18 15:42:59 +00004349 /* If the ephermeral table is already open and has no duplicates from
4350 ** OP_OpenDup, then erase all existing content so that the table is
4351 ** empty again, rather than creating a new table. */
dana5129722019-05-03 18:50:24 +00004352 assert( pCx->isEphemeral );
dan855b5d12019-06-26 21:04:30 +00004353 pCx->seqCount = 0;
4354 pCx->cacheStatus = CACHE_STALE;
drhb2486682022-01-03 01:43:28 +00004355 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0);
drhd0fb7962018-12-31 17:58:05 +00004356 }else{
drhb2486682022-01-03 01:43:28 +00004357 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE);
drhd0fb7962018-12-31 17:58:05 +00004358 if( pCx==0 ) goto no_mem;
drhd0fb7962018-12-31 17:58:05 +00004359 pCx->isEphemeral = 1;
drhb2486682022-01-03 01:43:28 +00004360 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx,
drhd0fb7962018-12-31 17:58:05 +00004361 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
4362 vfsFlags);
4363 if( rc==SQLITE_OK ){
drhb2486682022-01-03 01:43:28 +00004364 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0);
daneeee8a52021-03-18 14:31:37 +00004365 if( rc==SQLITE_OK ){
4366 /* If a transient index is required, create it by calling
4367 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
4368 ** opening it. If a transient table is required, just use the
4369 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
4370 */
4371 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
4372 assert( pOp->p4type==P4_KEYINFO );
drhb2486682022-01-03 01:43:28 +00004373 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot,
daneeee8a52021-03-18 14:31:37 +00004374 BTREE_BLOBKEY | pOp->p5);
4375 if( rc==SQLITE_OK ){
4376 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
4377 assert( pKeyInfo->db==db );
4378 assert( pKeyInfo->enc==ENC(db) );
drhb2486682022-01-03 01:43:28 +00004379 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
daneeee8a52021-03-18 14:31:37 +00004380 pKeyInfo, pCx->uc.pCursor);
4381 }
4382 pCx->isTable = 0;
4383 }else{
4384 pCx->pgnoRoot = SCHEMA_ROOT;
drhb2486682022-01-03 01:43:28 +00004385 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR,
daneeee8a52021-03-18 14:31:37 +00004386 0, pCx->uc.pCursor);
4387 pCx->isTable = 1;
drhd0fb7962018-12-31 17:58:05 +00004388 }
daneeee8a52021-03-18 14:31:37 +00004389 }
4390 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
4391 if( rc ){
drhb2486682022-01-03 01:43:28 +00004392 sqlite3BtreeClose(pCx->ub.pBtx);
drhd0fb7962018-12-31 17:58:05 +00004393 }
4394 }
drh5e00f6c2001-09-13 13:46:56 +00004395 }
drh9467abf2016-02-17 18:44:11 +00004396 if( rc ) goto abort_due_to_error;
dan855b5d12019-06-26 21:04:30 +00004397 pCx->nullRow = 1;
dan5134d132011-09-02 10:31:11 +00004398 break;
4399}
4400
danfad9f9a2014-04-01 18:41:51 +00004401/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00004402**
4403** This opcode works like OP_OpenEphemeral except that it opens
4404** a transient index that is specifically designed to sort large
4405** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00004406**
4407** If argument P3 is non-zero, then it indicates that the sorter may
4408** assume that a stable sort considering the first P3 fields of each
4409** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00004410*/
drhca892a72011-09-03 00:17:51 +00004411case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00004412 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00004413
drh399af1d2013-11-20 17:25:55 +00004414 assert( pOp->p1>=0 );
4415 assert( pOp->p2>=0 );
drhb2486682022-01-03 01:43:28 +00004416 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER);
dan5134d132011-09-02 10:31:11 +00004417 if( pCx==0 ) goto no_mem;
4418 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00004419 assert( pCx->pKeyInfo->db==db );
4420 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00004421 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh9467abf2016-02-17 18:44:11 +00004422 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00004423 break;
4424}
4425
dan78d58432014-03-25 15:04:07 +00004426/* Opcode: SequenceTest P1 P2 * * *
4427** Synopsis: if( cursor[P1].ctr++ ) pc = P2
4428**
4429** P1 is a sorter cursor. If the sequence counter is currently zero, jump
4430** to P2. Regardless of whether or not the jump is taken, increment the
4431** the sequence value.
4432*/
4433case OP_SequenceTest: {
4434 VdbeCursor *pC;
4435 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4436 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004437 assert( isSorter(pC) );
dan78d58432014-03-25 15:04:07 +00004438 if( (pC->seqCount++)==0 ){
drhf56fa462015-04-13 21:39:54 +00004439 goto jump_to_p2;
dan78d58432014-03-25 15:04:07 +00004440 }
drh5e00f6c2001-09-13 13:46:56 +00004441 break;
4442}
4443
drh5f612292014-02-08 23:20:32 +00004444/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00004445** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00004446**
4447** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00004448** row of data. The content of that one row is the content of memory
4449** register P2. In other words, cursor P1 becomes an alias for the
4450** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00004451**
drh2d8d7ce2010-02-15 15:17:05 +00004452** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00004453** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00004454** individual columns using the OP_Column opcode. The OP_Column opcode
4455** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00004456**
4457** P3 is the number of fields in the records that will be stored by
4458** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004459*/
drh9cbf3422008-01-17 16:22:13 +00004460case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00004461 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00004462
drh653b82a2009-06-22 11:10:47 +00004463 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00004464 assert( pOp->p3>=0 );
drhb2486682022-01-03 01:43:28 +00004465 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO);
drh4774b132004-06-12 20:12:51 +00004466 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00004467 pCx->nullRow = 1;
drhfe0cf7a2017-08-16 19:20:20 +00004468 pCx->seekResult = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00004469 pCx->isTable = 1;
drhfe0cf7a2017-08-16 19:20:20 +00004470 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
4471 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
4472 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
4473 ** which is a performance optimization */
4474 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
drh5f612292014-02-08 23:20:32 +00004475 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00004476 break;
4477}
4478
drh98757152008-01-09 23:04:12 +00004479/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00004480**
4481** Close a cursor previously opened as P1. If P1 is not
4482** currently open, this instruction is a no-op.
4483*/
dan2adb3092022-12-06 18:48:06 +00004484case OP_Close: { /* ncycle */
drh653b82a2009-06-22 11:10:47 +00004485 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4486 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
4487 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00004488 break;
4489}
4490
drh97bae792015-06-05 15:59:57 +00004491#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
4492/* Opcode: ColumnsUsed P1 * * P4 *
4493**
4494** This opcode (which only exists if SQLite was compiled with
4495** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
4496** table or index for cursor P1 are used. P4 is a 64-bit integer
4497** (P4_INT64) in which the first 63 bits are one for each of the
4498** first 63 columns of the table or index that are actually used
4499** by the cursor. The high-order bit is set if any column after
4500** the 64th is used.
4501*/
4502case OP_ColumnsUsed: {
4503 VdbeCursor *pC;
4504 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004505 assert( pC->eCurType==CURTYPE_BTREE );
drh97bae792015-06-05 15:59:57 +00004506 pC->maskUsed = *(u64*)pOp->p4.pI64;
4507 break;
4508}
4509#endif
4510
drh8af3f772014-07-25 18:01:06 +00004511/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004512** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00004513**
danielk1977b790c6c2008-04-18 10:25:24 +00004514** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004515** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004516** to an SQL index, then P3 is the first in an array of P4 registers
4517** that are used as an unpacked index key.
4518**
4519** Reposition cursor P1 so that it points to the smallest entry that
4520** is greater than or equal to the key value. If there are no records
4521** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00004522**
drhb1d607d2015-11-05 22:30:54 +00004523** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
drh576d0a92020-03-12 17:28:27 +00004524** opcode will either land on a record that exactly matches the key, or
4525** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4526** this opcode must be followed by an IdxLE opcode with the same arguments.
4527** The IdxGT opcode will be skipped if this opcode succeeds, but the
4528** IdxGT opcode will be used on subsequent loop iterations. The
4529** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4530** is an equality search.
drhb1d607d2015-11-05 22:30:54 +00004531**
drh8af3f772014-07-25 18:01:06 +00004532** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00004533** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004534** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00004535**
drh935850e2014-05-24 17:15:15 +00004536** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00004537*/
drh8af3f772014-07-25 18:01:06 +00004538/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004539** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00004540**
danielk1977b790c6c2008-04-18 10:25:24 +00004541** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004542** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004543** to an SQL index, then P3 is the first in an array of P4 registers
4544** that are used as an unpacked index key.
4545**
drh576d0a92020-03-12 17:28:27 +00004546** Reposition cursor P1 so that it points to the smallest entry that
danielk1977b790c6c2008-04-18 10:25:24 +00004547** is greater than the key value. If there are no records greater than
4548** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00004549**
drh8af3f772014-07-25 18:01:06 +00004550** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00004551** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004552** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00004553**
drh935850e2014-05-24 17:15:15 +00004554** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00004555*/
drh8af3f772014-07-25 18:01:06 +00004556/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004557** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00004558**
danielk1977b790c6c2008-04-18 10:25:24 +00004559** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004560** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004561** to an SQL index, then P3 is the first in an array of P4 registers
4562** that are used as an unpacked index key.
4563**
4564** Reposition cursor P1 so that it points to the largest entry that
4565** is less than the key value. If there are no records less than
4566** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00004567**
drh8af3f772014-07-25 18:01:06 +00004568** This opcode leaves the cursor configured to move in reverse order,
4569** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004570** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00004571**
drh935850e2014-05-24 17:15:15 +00004572** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00004573*/
drh8af3f772014-07-25 18:01:06 +00004574/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00004575** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00004576**
danielk1977b790c6c2008-04-18 10:25:24 +00004577** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00004578** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00004579** to an SQL index, then P3 is the first in an array of P4 registers
4580** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00004581**
danielk1977b790c6c2008-04-18 10:25:24 +00004582** Reposition cursor P1 so that it points to the largest entry that
4583** is less than or equal to the key value. If there are no records
4584** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00004585**
drh8af3f772014-07-25 18:01:06 +00004586** This opcode leaves the cursor configured to move in reverse order,
4587** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004588** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00004589**
drhb1d607d2015-11-05 22:30:54 +00004590** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
drh576d0a92020-03-12 17:28:27 +00004591** opcode will either land on a record that exactly matches the key, or
4592** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ,
4593** this opcode must be followed by an IdxLE opcode with the same arguments.
drhb1d607d2015-11-05 22:30:54 +00004594** The IdxGE opcode will be skipped if this opcode succeeds, but the
drh576d0a92020-03-12 17:28:27 +00004595** IdxGE opcode will be used on subsequent loop iterations. The
4596** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
4597** is an equality search.
drhb1d607d2015-11-05 22:30:54 +00004598**
drh935850e2014-05-24 17:15:15 +00004599** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00004600*/
dan2adb3092022-12-06 18:48:06 +00004601case OP_SeekLT: /* jump, in3, group, ncycle */
4602case OP_SeekLE: /* jump, in3, group, ncycle */
4603case OP_SeekGE: /* jump, in3, group, ncycle */
4604case OP_SeekGT: { /* jump, in3, group, ncycle */
drhb1d607d2015-11-05 22:30:54 +00004605 int res; /* Comparison result */
4606 int oc; /* Opcode */
4607 VdbeCursor *pC; /* The cursor to seek */
4608 UnpackedRecord r; /* The key to seek for */
4609 int nField; /* Number of columns or fields in the key */
4610 i64 iKey; /* The rowid we are to seek to */
drhd6b79462015-11-07 01:19:00 +00004611 int eqOnly; /* Only interested in == results */
drh80ff32f2001-11-04 18:32:46 +00004612
drh653b82a2009-06-22 11:10:47 +00004613 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00004614 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00004615 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004616 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004617 assert( pC->eCurType==CURTYPE_BTREE );
drh4a1d3652014-02-14 15:13:36 +00004618 assert( OP_SeekLE == OP_SeekLT+1 );
4619 assert( OP_SeekGE == OP_SeekLT+2 );
4620 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00004621 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00004622 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004623 oc = pOp->opcode;
drhd6b79462015-11-07 01:19:00 +00004624 eqOnly = 0;
drh3da046d2013-11-11 03:24:11 +00004625 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00004626#ifdef SQLITE_DEBUG
4627 pC->seekOp = pOp->opcode;
4628#endif
drhe0997b32015-03-20 14:57:50 +00004629
dana40cb962019-05-14 20:25:22 +00004630 pC->deferredMoveto = 0;
4631 pC->cacheStatus = CACHE_STALE;
drh3da046d2013-11-11 03:24:11 +00004632 if( pC->isTable ){
drh3e364802019-08-22 00:53:16 +00004633 u16 flags3, newType;
drh576d0a92020-03-12 17:28:27 +00004634 /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
drh218c66e2016-12-27 12:35:36 +00004635 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
4636 || CORRUPT_DB );
drhd6b79462015-11-07 01:19:00 +00004637
drh3da046d2013-11-11 03:24:11 +00004638 /* The input value in P3 might be of any type: integer, real, string,
4639 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00004640 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00004641 pIn3 = &aMem[pOp->p3];
drh3e364802019-08-22 00:53:16 +00004642 flags3 = pIn3->flags;
4643 if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00004644 applyNumericAffinity(pIn3, 0);
4645 }
drh3e364802019-08-22 00:53:16 +00004646 iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */
4647 newType = pIn3->flags; /* Record the type after applying numeric affinity */
4648 pIn3->flags = flags3; /* But convert the type back to its original */
drh959403f2008-12-12 17:56:16 +00004649
drh3da046d2013-11-11 03:24:11 +00004650 /* If the P3 value could not be converted into an integer without
4651 ** loss of information, then special processing is required... */
drh3e364802019-08-22 00:53:16 +00004652 if( (newType & (MEM_Int|MEM_IntReal))==0 ){
drhde324612021-07-19 20:52:31 +00004653 int c;
drh3e364802019-08-22 00:53:16 +00004654 if( (newType & MEM_Real)==0 ){
4655 if( (newType & MEM_Null) || oc>=OP_SeekGE ){
drh8616cff2019-07-13 16:15:23 +00004656 VdbeBranchTaken(1,2);
4657 goto jump_to_p2;
dan9edd8c12019-05-08 11:42:49 +00004658 }else{
dan873b0192019-05-09 11:19:27 +00004659 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
4660 if( rc!=SQLITE_OK ) goto abort_due_to_error;
dan9edd8c12019-05-08 11:42:49 +00004661 goto seek_not_found;
4662 }
drhde324612021-07-19 20:52:31 +00004663 }
4664 c = sqlite3IntFloatCompare(iKey, pIn3->u.r);
drh959403f2008-12-12 17:56:16 +00004665
danaa1776f2013-11-26 18:22:59 +00004666 /* If the approximation iKey is larger than the actual real search
4667 ** term, substitute >= for > and < for <=. e.g. if the search term
4668 ** is 4.9 and the integer approximation 5:
4669 **
4670 ** (x > 4.9) -> (x >= 5)
4671 ** (x <= 4.9) -> (x < 5)
4672 */
drhde324612021-07-19 20:52:31 +00004673 if( c>0 ){
drh4a1d3652014-02-14 15:13:36 +00004674 assert( OP_SeekGE==(OP_SeekGT-1) );
4675 assert( OP_SeekLT==(OP_SeekLE-1) );
4676 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
4677 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00004678 }
4679
4680 /* If the approximation iKey is smaller than the actual real search
4681 ** term, substitute <= for < and > for >=. */
drhde324612021-07-19 20:52:31 +00004682 else if( c<0 ){
drh4a1d3652014-02-14 15:13:36 +00004683 assert( OP_SeekLE==(OP_SeekLT+1) );
4684 assert( OP_SeekGT==(OP_SeekGE+1) );
4685 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
4686 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00004687 }
dan9edd8c12019-05-08 11:42:49 +00004688 }
drh42a410d2021-06-19 18:32:20 +00004689 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00004690 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004691 if( rc!=SQLITE_OK ){
4692 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00004693 }
drhaa736092009-06-22 00:55:30 +00004694 }else{
drh576d0a92020-03-12 17:28:27 +00004695 /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
4696 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
4697 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
4698 ** with the same key.
drhd6b79462015-11-07 01:19:00 +00004699 */
drhc960dcb2015-11-20 19:22:01 +00004700 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
drhd6b79462015-11-07 01:19:00 +00004701 eqOnly = 1;
4702 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
4703 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
drh576d0a92020-03-12 17:28:27 +00004704 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
4705 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
drhd6b79462015-11-07 01:19:00 +00004706 assert( pOp[1].p1==pOp[0].p1 );
4707 assert( pOp[1].p2==pOp[0].p2 );
4708 assert( pOp[1].p3==pOp[0].p3 );
4709 assert( pOp[1].p4.i==pOp[0].p4.i );
4710 }
4711
drh3da046d2013-11-11 03:24:11 +00004712 nField = pOp->p4.i;
4713 assert( pOp->p4type==P4_INT32 );
4714 assert( nField>0 );
4715 r.pKeyInfo = pC->pKeyInfo;
4716 r.nField = (u16)nField;
4717
4718 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00004719 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00004720 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00004721 ** }else{
dan1fed5da2014-02-25 21:01:25 +00004722 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00004723 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00004724 */
dan1fed5da2014-02-25 21:01:25 +00004725 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
4726 assert( oc!=OP_SeekGT || r.default_rc==-1 );
4727 assert( oc!=OP_SeekLE || r.default_rc==-1 );
4728 assert( oc!=OP_SeekGE || r.default_rc==+1 );
4729 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00004730
4731 r.aMem = &aMem[pOp->p3];
4732#ifdef SQLITE_DEBUG
drh8a2254f2022-10-07 15:55:35 +00004733 {
4734 int i;
4735 for(i=0; i<r.nField; i++){
4736 assert( memIsValid(&r.aMem[i]) );
4737 if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
4738 }
4739 }
drh3da046d2013-11-11 03:24:11 +00004740#endif
drh70528d72015-11-05 20:25:09 +00004741 r.eqSeen = 0;
drh42a410d2021-06-19 18:32:20 +00004742 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
drh3da046d2013-11-11 03:24:11 +00004743 if( rc!=SQLITE_OK ){
4744 goto abort_due_to_error;
4745 }
drhb1d607d2015-11-05 22:30:54 +00004746 if( eqOnly && r.eqSeen==0 ){
4747 assert( res!=0 );
4748 goto seek_not_found;
drh70528d72015-11-05 20:25:09 +00004749 }
drh3da046d2013-11-11 03:24:11 +00004750 }
drh3da046d2013-11-11 03:24:11 +00004751#ifdef SQLITE_TEST
4752 sqlite3_search_count++;
4753#endif
drh4a1d3652014-02-14 15:13:36 +00004754 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
4755 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00004756 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004757 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
4758 if( rc!=SQLITE_OK ){
4759 if( rc==SQLITE_DONE ){
4760 rc = SQLITE_OK;
4761 res = 1;
4762 }else{
4763 goto abort_due_to_error;
4764 }
4765 }
drh3da046d2013-11-11 03:24:11 +00004766 }else{
4767 res = 0;
4768 }
4769 }else{
drh4a1d3652014-02-14 15:13:36 +00004770 assert( oc==OP_SeekLT || oc==OP_SeekLE );
4771 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00004772 res = 0;
drh2ab792e2017-05-30 18:34:07 +00004773 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
4774 if( rc!=SQLITE_OK ){
4775 if( rc==SQLITE_DONE ){
4776 rc = SQLITE_OK;
4777 res = 1;
4778 }else{
4779 goto abort_due_to_error;
4780 }
4781 }
drh3da046d2013-11-11 03:24:11 +00004782 }else{
4783 /* res might be negative because the table is empty. Check to
4784 ** see if this is the case.
4785 */
drhc960dcb2015-11-20 19:22:01 +00004786 res = sqlite3BtreeEof(pC->uc.pCursor);
drh3da046d2013-11-11 03:24:11 +00004787 }
4788 }
drhb1d607d2015-11-05 22:30:54 +00004789seek_not_found:
drh3da046d2013-11-11 03:24:11 +00004790 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00004791 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004792 if( res ){
drhf56fa462015-04-13 21:39:54 +00004793 goto jump_to_p2;
drhb1d607d2015-11-05 22:30:54 +00004794 }else if( eqOnly ){
4795 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
4796 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
drh5e00f6c2001-09-13 13:46:56 +00004797 }
drh5e00f6c2001-09-13 13:46:56 +00004798 break;
4799}
dan71c57db2016-07-09 20:23:55 +00004800
drh68cf0ac2020-09-28 19:51:54 +00004801
dan73c586b2022-10-07 18:57:15 +00004802/* Opcode: SeekScan P1 P2 * * P5
drh68cf0ac2020-09-28 19:51:54 +00004803** Synopsis: Scan-ahead up to P1 rows
4804**
drhdfbaae72020-09-29 17:29:11 +00004805** This opcode is a prefix opcode to OP_SeekGE. In other words, this
drh04e70ce2020-10-02 11:55:07 +00004806** opcode must be immediately followed by OP_SeekGE. This constraint is
drhdfbaae72020-09-29 17:29:11 +00004807** checked by assert() statements.
4808**
4809** This opcode uses the P1 through P4 operands of the subsequent
4810** OP_SeekGE. In the text that follows, the operands of the subsequent
4811** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
dan73c586b2022-10-07 18:57:15 +00004812** the P1, P2 and P5 operands of this opcode are also used, and are called
4813** This.P1, This.P2 and This.P5.
drh68cf0ac2020-09-28 19:51:54 +00004814**
4815** This opcode helps to optimize IN operators on a multi-column index
drhdfbaae72020-09-29 17:29:11 +00004816** where the IN operator is on the later terms of the index by avoiding
4817** unnecessary seeks on the btree, substituting steps to the next row
4818** of the b-tree instead. A correct answer is obtained if this opcode
4819** is omitted or is a no-op.
drh68cf0ac2020-09-28 19:51:54 +00004820**
drhdfbaae72020-09-29 17:29:11 +00004821** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
4822** is the desired entry that we want the cursor SeekGE.P1 to be pointing
dan73c586b2022-10-07 18:57:15 +00004823** to. Call this SeekGE.P3/P4 row the "target".
drh68cf0ac2020-09-28 19:51:54 +00004824**
drha54e1b12020-09-29 23:52:25 +00004825** If the SeekGE.P1 cursor is not currently pointing to a valid row,
4826** then this opcode is a no-op and control passes through into the OP_SeekGE.
drh68cf0ac2020-09-28 19:51:54 +00004827**
drhdfbaae72020-09-29 17:29:11 +00004828** If the SeekGE.P1 cursor is pointing to a valid row, then that row
4829** might be the target row, or it might be near and slightly before the
dan73c586b2022-10-07 18:57:15 +00004830** target row, or it might be after the target row. If the cursor is
4831** currently before the target row, then this opcode attempts to position
4832** the cursor on or after the target row by invoking sqlite3BtreeStep()
4833** on the cursor between 1 and This.P1 times.
drh68cf0ac2020-09-28 19:51:54 +00004834**
dan73c586b2022-10-07 18:57:15 +00004835** The This.P5 parameter is a flag that indicates what to do if the
4836** cursor ends up pointing at a valid row that is past the target
4837** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If
4838** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
4839** case occurs when there are no inequality constraints to the right of
4840** the IN constraing. The jump to SeekGE.P2 ends the loop. The P5!=0 case
4841** occurs when there are inequality constraints to the right of the IN
4842** operator. In that case, the This.P2 will point either directly to or
4843** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for
4844** loop terminate.
drh68cf0ac2020-09-28 19:51:54 +00004845**
dan73c586b2022-10-07 18:57:15 +00004846** Possible outcomes from this opcode:<ol>
drh68cf0ac2020-09-28 19:51:54 +00004847**
dan73c586b2022-10-07 18:57:15 +00004848** <li> If the cursor is initally not pointed to any valid row, then
4849** fall through into the subsequent OP_SeekGE opcode.
drhdfbaae72020-09-29 17:29:11 +00004850**
dan73c586b2022-10-07 18:57:15 +00004851** <li> If the cursor is left pointing to a row that is before the target
4852** row, even after making as many as This.P1 calls to
4853** sqlite3BtreeNext(), then also fall through into OP_SeekGE.
4854**
4855** <li> If the cursor is left pointing at the target row, either because it
4856** was at the target row to begin with or because one or more
4857** sqlite3BtreeNext() calls moved the cursor to the target row,
4858** then jump to This.P2..,
4859**
4860** <li> If the cursor started out before the target row and a call to
4861** to sqlite3BtreeNext() moved the cursor off the end of the index
4862** (indicating that the target row definitely does not exist in the
4863** btree) then jump to SeekGE.P2, ending the loop.
4864**
4865** <li> If the cursor ends up on a valid row that is past the target row
4866** (indicating that the target row does not exist in the btree) then
4867** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0.
drhdfbaae72020-09-29 17:29:11 +00004868** </ol>
drh68cf0ac2020-09-28 19:51:54 +00004869*/
dan2adb3092022-12-06 18:48:06 +00004870case OP_SeekScan: { /* ncycle */
drhf761d932020-09-29 01:48:46 +00004871 VdbeCursor *pC;
4872 int res;
drhdeaa6102020-10-01 15:46:21 +00004873 int nStep;
drhf761d932020-09-29 01:48:46 +00004874 UnpackedRecord r;
4875
drh68cf0ac2020-09-28 19:51:54 +00004876 assert( pOp[1].opcode==OP_SeekGE );
drh04e70ce2020-10-02 11:55:07 +00004877
dan73c586b2022-10-07 18:57:15 +00004878 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
4879 ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first
4880 ** opcode past the OP_SeekGE itself. */
drh04e70ce2020-10-02 11:55:07 +00004881 assert( pOp->p2>=(int)(pOp-aOp)+2 );
dan73c586b2022-10-07 18:57:15 +00004882#ifdef SQLITE_DEBUG
4883 if( pOp->p5==0 ){
4884 /* There are no inequality constraints following the IN constraint. */
4885 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
4886 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
4887 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
4888 assert( aOp[pOp->p2-1].opcode==OP_IdxGT
4889 || aOp[pOp->p2-1].opcode==OP_IdxGE );
4890 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
4891 }else{
4892 /* There are inequality constraints. */
4893 assert( pOp->p2==(int)(pOp-aOp)+2 );
4894 assert( aOp[pOp->p2-1].opcode==OP_SeekGE );
4895 }
4896#endif
drh04e70ce2020-10-02 11:55:07 +00004897
drh68cf0ac2020-09-28 19:51:54 +00004898 assert( pOp->p1>0 );
drhf761d932020-09-29 01:48:46 +00004899 pC = p->apCsr[pOp[1].p1];
4900 assert( pC!=0 );
4901 assert( pC->eCurType==CURTYPE_BTREE );
4902 assert( !pC->isTable );
drha54e1b12020-09-29 23:52:25 +00004903 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
drhf761d932020-09-29 01:48:46 +00004904#ifdef SQLITE_DEBUG
4905 if( db->flags&SQLITE_VdbeTrace ){
drha54e1b12020-09-29 23:52:25 +00004906 printf("... cursor not valid - fall through\n");
drhf761d932020-09-29 01:48:46 +00004907 }
4908#endif
4909 break;
4910 }
drhdeaa6102020-10-01 15:46:21 +00004911 nStep = pOp->p1;
4912 assert( nStep>=1 );
drhf761d932020-09-29 01:48:46 +00004913 r.pKeyInfo = pC->pKeyInfo;
4914 r.nField = (u16)pOp[1].p4.i;
4915 r.default_rc = 0;
4916 r.aMem = &aMem[pOp[1].p3];
4917#ifdef SQLITE_DEBUG
4918 {
4919 int i;
4920 for(i=0; i<r.nField; i++){
4921 assert( memIsValid(&r.aMem[i]) );
4922 REGISTER_TRACE(pOp[1].p3+i, &aMem[pOp[1].p3+i]);
4923 }
4924 }
4925#endif
4926 res = 0; /* Not needed. Only used to silence a warning. */
4927 while(1){
4928 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
4929 if( rc ) goto abort_due_to_error;
dan73c586b2022-10-07 18:57:15 +00004930 if( res>0 && pOp->p5==0 ){
drh0b2949c2020-09-29 20:22:19 +00004931 seekscan_search_fail:
dan73c586b2022-10-07 18:57:15 +00004932 /* Jump to SeekGE.P2, ending the loop */
drhf761d932020-09-29 01:48:46 +00004933#ifdef SQLITE_DEBUG
drh0b2949c2020-09-29 20:22:19 +00004934 if( db->flags&SQLITE_VdbeTrace ){
drhdeaa6102020-10-01 15:46:21 +00004935 printf("... %d steps and then skip\n", pOp->p1 - nStep);
drh0b2949c2020-09-29 20:22:19 +00004936 }
drhf761d932020-09-29 01:48:46 +00004937#endif
drh0b2949c2020-09-29 20:22:19 +00004938 VdbeBranchTaken(1,3);
drhf287d002020-09-30 00:10:22 +00004939 pOp++;
drhf761d932020-09-29 01:48:46 +00004940 goto jump_to_p2;
4941 }
dan73c586b2022-10-07 18:57:15 +00004942 if( res>=0 ){
4943 /* Jump to This.P2, bypassing the OP_SeekGE opcode */
drhf761d932020-09-29 01:48:46 +00004944#ifdef SQLITE_DEBUG
drh0b2949c2020-09-29 20:22:19 +00004945 if( db->flags&SQLITE_VdbeTrace ){
drhdeaa6102020-10-01 15:46:21 +00004946 printf("... %d steps and then success\n", pOp->p1 - nStep);
drh0b2949c2020-09-29 20:22:19 +00004947 }
drhf761d932020-09-29 01:48:46 +00004948#endif
drh0b2949c2020-09-29 20:22:19 +00004949 VdbeBranchTaken(2,3);
drh04e70ce2020-10-02 11:55:07 +00004950 goto jump_to_p2;
drhf761d932020-09-29 01:48:46 +00004951 break;
4952 }
drhdeaa6102020-10-01 15:46:21 +00004953 if( nStep<=0 ){
drh0b2949c2020-09-29 20:22:19 +00004954#ifdef SQLITE_DEBUG
4955 if( db->flags&SQLITE_VdbeTrace ){
4956 printf("... fall through after %d steps\n", pOp->p1);
4957 }
4958#endif
4959 VdbeBranchTaken(0,3);
4960 break;
4961 }
drhdeaa6102020-10-01 15:46:21 +00004962 nStep--;
drhf761d932020-09-29 01:48:46 +00004963 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
drh0b2949c2020-09-29 20:22:19 +00004964 if( rc ){
4965 if( rc==SQLITE_DONE ){
4966 rc = SQLITE_OK;
4967 goto seekscan_search_fail;
4968 }else{
4969 goto abort_due_to_error;
4970 }
4971 }
drhf761d932020-09-29 01:48:46 +00004972 }
drh0b2949c2020-09-29 20:22:19 +00004973
drhf761d932020-09-29 01:48:46 +00004974 break;
drh68cf0ac2020-09-28 19:51:54 +00004975}
4976
4977
drhfa17e132020-09-01 01:52:03 +00004978/* Opcode: SeekHit P1 P2 P3 * *
4979** Synopsis: set P2<=seekHit<=P3
drh8c2b6d72018-06-05 20:45:20 +00004980**
drhfa17e132020-09-01 01:52:03 +00004981** Increase or decrease the seekHit value for cursor P1, if necessary,
4982** so that it is no less than P2 and no greater than P3.
drh8c2b6d72018-06-05 20:45:20 +00004983**
drhfa17e132020-09-01 01:52:03 +00004984** The seekHit integer represents the maximum of terms in an index for which
4985** there is known to be at least one match. If the seekHit value is smaller
4986** than the total number of equality terms in an index lookup, then the
4987** OP_IfNoHope opcode might run to see if the IN loop can be abandoned
4988** early, thus saving work. This is part of the IN-early-out optimization.
4989**
4990** P1 must be a valid b-tree cursor.
drh8c2b6d72018-06-05 20:45:20 +00004991*/
dan2adb3092022-12-06 18:48:06 +00004992case OP_SeekHit: { /* ncycle */
drh8c2b6d72018-06-05 20:45:20 +00004993 VdbeCursor *pC;
4994 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4995 pC = p->apCsr[pOp->p1];
4996 assert( pC!=0 );
drhfa17e132020-09-01 01:52:03 +00004997 assert( pOp->p3>=pOp->p2 );
4998 if( pC->seekHit<pOp->p2 ){
drh7bfccfe2021-04-29 13:58:28 +00004999#ifdef SQLITE_DEBUG
5000 if( db->flags&SQLITE_VdbeTrace ){
5001 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
5002 }
5003#endif
drhfa17e132020-09-01 01:52:03 +00005004 pC->seekHit = pOp->p2;
5005 }else if( pC->seekHit>pOp->p3 ){
drh7bfccfe2021-04-29 13:58:28 +00005006#ifdef SQLITE_DEBUG
5007 if( db->flags&SQLITE_VdbeTrace ){
5008 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
5009 }
5010#endif
drhfa17e132020-09-01 01:52:03 +00005011 pC->seekHit = pOp->p3;
5012 }
drh8c2b6d72018-06-05 20:45:20 +00005013 break;
5014}
5015
dan74ebaad2020-01-04 16:55:57 +00005016/* Opcode: IfNotOpen P1 P2 * * *
5017** Synopsis: if( !csr[P1] ) goto P2
5018**
drh8b9a3d12022-09-20 19:22:17 +00005019** If cursor P1 is not open or if P1 is set to a NULL row using the
5020** OP_NullRow opcode, then jump to instruction P2. Otherwise, fall through.
dan74ebaad2020-01-04 16:55:57 +00005021*/
5022case OP_IfNotOpen: { /* jump */
drh8b9a3d12022-09-20 19:22:17 +00005023 VdbeCursor *pCur;
5024
dan74ebaad2020-01-04 16:55:57 +00005025 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8b9a3d12022-09-20 19:22:17 +00005026 pCur = p->apCsr[pOp->p1];
5027 VdbeBranchTaken(pCur==0 || pCur->nullRow, 2);
5028 if( pCur==0 || pCur->nullRow ){
dan74ebaad2020-01-04 16:55:57 +00005029 goto jump_to_p2_and_check_for_interrupt;
5030 }
5031 break;
5032}
5033
drh8cff69d2009-11-12 19:59:44 +00005034/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00005035** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00005036**
drh8cff69d2009-11-12 19:59:44 +00005037** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
5038** P4>0 then register P3 is the first of P4 registers that form an unpacked
5039** record.
5040**
5041** Cursor P1 is on an index btree. If the record identified by P3 and P4
5042** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00005043** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00005044**
drhcefc87f2014-08-01 01:40:33 +00005045** This operation leaves the cursor in a state where it can be
5046** advanced in the forward direction. The Next instruction will work,
5047** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00005048**
drh6f225d02013-10-26 13:36:51 +00005049** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00005050*/
drh8cff69d2009-11-12 19:59:44 +00005051/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00005052** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00005053**
drh8cff69d2009-11-12 19:59:44 +00005054** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
5055** P4>0 then register P3 is the first of P4 registers that form an unpacked
5056** record.
5057**
5058** Cursor P1 is on an index btree. If the record identified by P3 and P4
5059** is not the prefix of any entry in P1 then a jump is made to P2. If P1
5060** does contain an entry whose prefix matches the P3/P4 record then control
5061** falls through to the next instruction and P1 is left pointing at the
5062** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00005063**
drh8af3f772014-07-25 18:01:06 +00005064** This operation leaves the cursor in a state where it cannot be
5065** advanced in either direction. In other words, the Next and Prev
5066** opcodes do not work after this operation.
5067**
drh8c2b6d72018-06-05 20:45:20 +00005068** See also: Found, NotExists, NoConflict, IfNoHope
5069*/
5070/* Opcode: IfNoHope P1 P2 P3 P4 *
5071** Synopsis: key=r[P3@P4]
5072**
5073** Register P3 is the first of P4 registers that form an unpacked
drhfa17e132020-09-01 01:52:03 +00005074** record. Cursor P1 is an index btree. P2 is a jump destination.
5075** In other words, the operands to this opcode are the same as the
5076** operands to OP_NotFound and OP_IdxGT.
drh8c2b6d72018-06-05 20:45:20 +00005077**
drhfa17e132020-09-01 01:52:03 +00005078** This opcode is an optimization attempt only. If this opcode always
5079** falls through, the correct answer is still obtained, but extra works
5080** is performed.
drh8c2b6d72018-06-05 20:45:20 +00005081**
drhfa17e132020-09-01 01:52:03 +00005082** A value of N in the seekHit flag of cursor P1 means that there exists
5083** a key P3:N that will match some record in the index. We want to know
5084** if it is possible for a record P3:P4 to match some record in the
5085** index. If it is not possible, we can skips some work. So if seekHit
5086** is less than P4, attempt to find out if a match is possible by running
5087** OP_NotFound.
drh8c2b6d72018-06-05 20:45:20 +00005088**
5089** This opcode is used in IN clause processing for a multi-column key.
5090** If an IN clause is attached to an element of the key other than the
5091** left-most element, and if there are no matches on the most recent
5092** seek over the whole key, then it might be that one of the key element
5093** to the left is prohibiting a match, and hence there is "no hope" of
5094** any match regardless of how many IN clause elements are checked.
5095** In such a case, we abandon the IN clause search early, using this
5096** opcode. The opcode name comes from the fact that the
5097** jump is taken if there is "no hope" of achieving a match.
5098**
5099** See also: NotFound, SeekHit
drh5e00f6c2001-09-13 13:46:56 +00005100*/
drh6f225d02013-10-26 13:36:51 +00005101/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00005102** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00005103**
5104** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
5105** P4>0 then register P3 is the first of P4 registers that form an unpacked
5106** record.
5107**
5108** Cursor P1 is on an index btree. If the record identified by P3 and P4
5109** contains any NULL value, jump immediately to P2. If all terms of the
5110** record are not-NULL then a check is done to determine if any row in the
5111** P1 index btree has a matching key prefix. If there are no matches, jump
5112** immediately to P2. If there is a match, fall through and leave the P1
5113** cursor pointing to the matching row.
5114**
5115** This opcode is similar to OP_NotFound with the exceptions that the
5116** branch is always taken if any part of the search key input is NULL.
5117**
drh8af3f772014-07-25 18:01:06 +00005118** This operation leaves the cursor in a state where it cannot be
5119** advanced in either direction. In other words, the Next and Prev
5120** opcodes do not work after this operation.
5121**
drh6f225d02013-10-26 13:36:51 +00005122** See also: NotFound, Found, NotExists
5123*/
dan2adb3092022-12-06 18:48:06 +00005124case OP_IfNoHope: { /* jump, in3, ncycle */
drh8c2b6d72018-06-05 20:45:20 +00005125 VdbeCursor *pC;
5126 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5127 pC = p->apCsr[pOp->p1];
5128 assert( pC!=0 );
drh7bfccfe2021-04-29 13:58:28 +00005129#ifdef SQLITE_DEBUG
5130 if( db->flags&SQLITE_VdbeTrace ){
5131 printf("seekHit is %d\n", pC->seekHit);
5132 }
5133#endif
drhfa17e132020-09-01 01:52:03 +00005134 if( pC->seekHit>=pOp->p4.i ) break;
drh8c2b6d72018-06-05 20:45:20 +00005135 /* Fall through into OP_NotFound */
drh08b92082020-08-10 14:18:00 +00005136 /* no break */ deliberate_fall_through
drh8c2b6d72018-06-05 20:45:20 +00005137}
dan2adb3092022-12-06 18:48:06 +00005138case OP_NoConflict: /* jump, in3, ncycle */
5139case OP_NotFound: /* jump, in3, ncycle */
5140case OP_Found: { /* jump, in3, ncycle */
drh856c1032009-06-02 15:21:42 +00005141 int alreadyExists;
drh6f225d02013-10-26 13:36:51 +00005142 int ii;
drhdfe88ec2008-11-03 20:55:06 +00005143 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00005144 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00005145 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00005146
dan0ff297e2009-09-25 17:03:14 +00005147#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00005148 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00005149#endif
5150
drhaa736092009-06-22 00:55:30 +00005151 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00005152 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00005153 pC = p->apCsr[pOp->p1];
5154 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00005155#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00005156 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00005157#endif
drh95b10362022-04-13 19:00:57 +00005158 r.aMem = &aMem[pOp->p3];
drhc960dcb2015-11-20 19:22:01 +00005159 assert( pC->eCurType==CURTYPE_BTREE );
5160 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00005161 assert( pC->isTable==0 );
drh95b10362022-04-13 19:00:57 +00005162 r.nField = (u16)pOp->p4.i;
5163 if( r.nField>0 ){
drhb834e0d2022-04-04 19:43:57 +00005164 /* Key values in an array of registers */
drhb834e0d2022-04-04 19:43:57 +00005165 r.pKeyInfo = pC->pKeyInfo;
drhb834e0d2022-04-04 19:43:57 +00005166 r.default_rc = 0;
drh8aaf7bc2016-09-20 01:19:18 +00005167#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00005168 for(ii=0; ii<r.nField; ii++){
5169 assert( memIsValid(&r.aMem[ii]) );
drh8aaf7bc2016-09-20 01:19:18 +00005170 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
drh826af372014-02-08 19:12:21 +00005171 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh826af372014-02-08 19:12:21 +00005172 }
drh8aaf7bc2016-09-20 01:19:18 +00005173#endif
drhec534e62022-04-04 20:20:22 +00005174 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult);
drh3da046d2013-11-11 03:24:11 +00005175 }else{
drhb834e0d2022-04-04 19:43:57 +00005176 /* Composite key generated by OP_MakeRecord */
drh95b10362022-04-13 19:00:57 +00005177 assert( r.aMem->flags & MEM_Blob );
drhb834e0d2022-04-04 19:43:57 +00005178 assert( pOp->opcode!=OP_NoConflict );
drh95b10362022-04-13 19:00:57 +00005179 rc = ExpandBlob(r.aMem);
drhe46515b2017-05-19 22:51:00 +00005180 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
5181 if( rc ) goto no_mem;
drhb834e0d2022-04-04 19:43:57 +00005182 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
drh3da046d2013-11-11 03:24:11 +00005183 if( pIdxKey==0 ) goto no_mem;
drh95b10362022-04-13 19:00:57 +00005184 sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey);
drhb834e0d2022-04-04 19:43:57 +00005185 pIdxKey->default_rc = 0;
drhec534e62022-04-04 20:20:22 +00005186 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult);
drhb834e0d2022-04-04 19:43:57 +00005187 sqlite3DbFreeNN(db, pIdxKey);
drh5e00f6c2001-09-13 13:46:56 +00005188 }
drh3da046d2013-11-11 03:24:11 +00005189 if( rc!=SQLITE_OK ){
drh9467abf2016-02-17 18:44:11 +00005190 goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00005191 }
drhec534e62022-04-04 20:20:22 +00005192 alreadyExists = (pC->seekResult==0);
drh3da046d2013-11-11 03:24:11 +00005193 pC->nullRow = 1-alreadyExists;
5194 pC->deferredMoveto = 0;
5195 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00005196 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00005197 VdbeBranchTaken(alreadyExists!=0,2);
drhf56fa462015-04-13 21:39:54 +00005198 if( alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00005199 }else{
drhb834e0d2022-04-04 19:43:57 +00005200 if( !alreadyExists ){
5201 VdbeBranchTaken(1,2);
5202 goto jump_to_p2;
5203 }
5204 if( pOp->opcode==OP_NoConflict ){
5205 /* For the OP_NoConflict opcode, take the jump if any of the
5206 ** input fields are NULL, since any key with a NULL will not
5207 ** conflict */
5208 for(ii=0; ii<r.nField; ii++){
5209 if( r.aMem[ii].flags & MEM_Null ){
5210 VdbeBranchTaken(1,2);
5211 goto jump_to_p2;
5212 }
5213 }
5214 }
5215 VdbeBranchTaken(0,2);
5216 if( pOp->opcode==OP_IfNoHope ){
5217 pC->seekHit = pOp->p4.i;
5218 }
drh5e00f6c2001-09-13 13:46:56 +00005219 }
drh5e00f6c2001-09-13 13:46:56 +00005220 break;
5221}
5222
drheeb95652016-05-26 20:56:38 +00005223/* Opcode: SeekRowid P1 P2 P3 * *
5224** Synopsis: intkey=r[P3]
5225**
5226** P1 is the index of a cursor open on an SQL table btree (with integer
5227** keys). If register P3 does not contain an integer or if P1 does not
5228** contain a record with rowid P3 then jump immediately to P2.
5229** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
5230** a record with rowid P3 then
5231** leave the cursor pointing at that record and fall through to the next
5232** instruction.
5233**
5234** The OP_NotExists opcode performs the same operation, but with OP_NotExists
5235** the P3 register must be guaranteed to contain an integer value. With this
5236** opcode, register P3 might not contain an integer.
5237**
5238** The OP_NotFound opcode performs the same operation on index btrees
5239** (with arbitrary multi-value keys).
5240**
5241** This opcode leaves the cursor in a state where it cannot be advanced
5242** in either direction. In other words, the Next and Prev opcodes will
5243** not work following this opcode.
5244**
5245** See also: Found, NotFound, NoConflict, SeekRowid
5246*/
drh9cbf3422008-01-17 16:22:13 +00005247/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005248** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00005249**
drh261c02d2013-10-25 14:46:15 +00005250** P1 is the index of a cursor open on an SQL table btree (with integer
5251** keys). P3 is an integer rowid. If P1 does not contain a record with
danc6157e12015-09-14 09:23:47 +00005252** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
5253** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
5254** leave the cursor pointing at that record and fall through to the next
5255** instruction.
drh6b125452002-01-28 15:53:03 +00005256**
drheeb95652016-05-26 20:56:38 +00005257** The OP_SeekRowid opcode performs the same operation but also allows the
5258** P3 register to contain a non-integer value, in which case the jump is
5259** always taken. This opcode requires that P3 always contain an integer.
5260**
drh261c02d2013-10-25 14:46:15 +00005261** The OP_NotFound opcode performs the same operation on index btrees
5262** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00005263**
drh8af3f772014-07-25 18:01:06 +00005264** This opcode leaves the cursor in a state where it cannot be advanced
5265** in either direction. In other words, the Next and Prev opcodes will
5266** not work following this opcode.
5267**
drheeb95652016-05-26 20:56:38 +00005268** See also: Found, NotFound, NoConflict, SeekRowid
drh6b125452002-01-28 15:53:03 +00005269*/
dan2adb3092022-12-06 18:48:06 +00005270case OP_SeekRowid: { /* jump, in3, ncycle */
drhdfe88ec2008-11-03 20:55:06 +00005271 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00005272 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00005273 int res;
5274 u64 iKey;
5275
drh3c657212009-11-17 23:59:58 +00005276 pIn3 = &aMem[pOp->p3];
drh3242c692019-05-04 01:29:13 +00005277 testcase( pIn3->flags & MEM_Int );
5278 testcase( pIn3->flags & MEM_IntReal );
drhb29ef5e2019-10-07 01:05:57 +00005279 testcase( pIn3->flags & MEM_Real );
5280 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
drh169f0772019-05-02 21:36:26 +00005281 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
drhb29ef5e2019-10-07 01:05:57 +00005282 /* If pIn3->u.i does not contain an integer, compute iKey as the
5283 ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted
5284 ** into an integer without loss of information. Take care to avoid
5285 ** changing the datatype of pIn3, however, as it is used by other
5286 ** parts of the prepared statement. */
5287 Mem x = pIn3[0];
5288 applyAffinity(&x, SQLITE_AFF_NUMERIC, encoding);
5289 if( (x.flags & MEM_Int)==0 ) goto jump_to_p2;
5290 iKey = x.u.i;
5291 goto notExistsWithKey;
drheeb95652016-05-26 20:56:38 +00005292 }
5293 /* Fall through into OP_NotExists */
drh08b92082020-08-10 14:18:00 +00005294 /* no break */ deliberate_fall_through
dan2adb3092022-12-06 18:48:06 +00005295case OP_NotExists: /* jump, in3, ncycle */
drheeb95652016-05-26 20:56:38 +00005296 pIn3 = &aMem[pOp->p3];
drhe4fe6d42018-08-03 15:58:07 +00005297 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
drhaa736092009-06-22 00:55:30 +00005298 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhb29ef5e2019-10-07 01:05:57 +00005299 iKey = pIn3->u.i;
5300notExistsWithKey:
drhaa736092009-06-22 00:55:30 +00005301 pC = p->apCsr[pOp->p1];
5302 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00005303#ifdef SQLITE_DEBUG
drh94f4f872018-12-20 22:08:32 +00005304 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
drh8af3f772014-07-25 18:01:06 +00005305#endif
drhaa736092009-06-22 00:55:30 +00005306 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00005307 assert( pC->eCurType==CURTYPE_BTREE );
5308 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00005309 assert( pCrsr!=0 );
5310 res = 0;
drh42a410d2021-06-19 18:32:20 +00005311 rc = sqlite3BtreeTableMoveto(pCrsr, iKey, 0, &res);
drhb79d5522015-09-14 19:26:37 +00005312 assert( rc==SQLITE_OK || res==0 );
drhb53a5a92014-10-12 22:37:22 +00005313 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00005314 pC->nullRow = 0;
5315 pC->cacheStatus = CACHE_STALE;
5316 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00005317 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00005318 pC->seekResult = res;
danc6157e12015-09-14 09:23:47 +00005319 if( res!=0 ){
drhb79d5522015-09-14 19:26:37 +00005320 assert( rc==SQLITE_OK );
5321 if( pOp->p2==0 ){
5322 rc = SQLITE_CORRUPT_BKPT;
5323 }else{
5324 goto jump_to_p2;
5325 }
danc6157e12015-09-14 09:23:47 +00005326 }
drh9467abf2016-02-17 18:44:11 +00005327 if( rc ) goto abort_due_to_error;
drh6b125452002-01-28 15:53:03 +00005328 break;
5329}
5330
drh4c583122008-01-04 22:01:03 +00005331/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00005332** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00005333**
drh4c583122008-01-04 22:01:03 +00005334** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00005335** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00005336** The sequence number on the cursor is incremented after this
5337** instruction.
drh4db38a72005-09-01 12:16:28 +00005338*/
drh27a348c2015-04-13 19:14:06 +00005339case OP_Sequence: { /* out2 */
drh653b82a2009-06-22 11:10:47 +00005340 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5341 assert( p->apCsr[pOp->p1]!=0 );
drhc960dcb2015-11-20 19:22:01 +00005342 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
drh27a348c2015-04-13 19:14:06 +00005343 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00005344 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00005345 break;
5346}
5347
5348
drh98757152008-01-09 23:04:12 +00005349/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005350** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00005351**
drhf0863fe2005-06-12 21:35:51 +00005352** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00005353** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00005354** table that cursor P1 points to. The new record number is written
5355** written to register P2.
drh205f48e2004-11-05 00:43:11 +00005356**
dan76d462e2009-08-30 11:42:51 +00005357** If P3>0 then P3 is a register in the root frame of this VDBE that holds
5358** the largest previously generated record number. No new record numbers are
5359** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00005360** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00005361** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00005362** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00005363*/
drh27a348c2015-04-13 19:14:06 +00005364case OP_NewRowid: { /* out2 */
drhaa736092009-06-22 00:55:30 +00005365 i64 v; /* The new rowid */
5366 VdbeCursor *pC; /* Cursor of table to get the new rowid */
5367 int res; /* Result of an sqlite3BtreeLast() */
5368 int cnt; /* Counter to limit the number of searches */
mistachkind6665c52021-01-18 19:28:56 +00005369#ifndef SQLITE_OMIT_AUTOINCREMENT
drhaa736092009-06-22 00:55:30 +00005370 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00005371 VdbeFrame *pFrame; /* Root frame of VDBE */
mistachkind6665c52021-01-18 19:28:56 +00005372#endif
drh856c1032009-06-02 15:21:42 +00005373
drh856c1032009-06-02 15:21:42 +00005374 v = 0;
5375 res = 0;
drh27a348c2015-04-13 19:14:06 +00005376 pOut = out2Prerelease(p, pOp);
drhaa736092009-06-22 00:55:30 +00005377 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5378 pC = p->apCsr[pOp->p1];
5379 assert( pC!=0 );
drh4c57e322018-05-23 17:53:07 +00005380 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00005381 assert( pC->eCurType==CURTYPE_BTREE );
5382 assert( pC->uc.pCursor!=0 );
drh98ef0f62015-06-30 01:25:52 +00005383 {
drh5cf8e8c2002-02-19 22:42:05 +00005384 /* The next rowid or record number (different terms for the same
5385 ** thing) is obtained in a two-step algorithm.
5386 **
5387 ** First we attempt to find the largest existing rowid and add one
5388 ** to that. But if the largest existing rowid is already the maximum
5389 ** positive integer, we have to fall through to the second
5390 ** probabilistic algorithm
5391 **
5392 ** The second algorithm is to select a rowid at random and see if
5393 ** it already exists in the table. If it does not exist, we have
5394 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00005395 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00005396 */
drhaa736092009-06-22 00:55:30 +00005397 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00005398
drh75f86a42005-02-17 00:03:06 +00005399#ifdef SQLITE_32BIT_ROWID
5400# define MAX_ROWID 0x7fffffff
5401#else
drhfe2093d2005-01-20 22:48:47 +00005402 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
5403 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
5404 ** to provide the constant while making all compilers happy.
5405 */
danielk197764202cf2008-11-17 15:31:47 +00005406# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00005407#endif
drhfe2093d2005-01-20 22:48:47 +00005408
drh5cf8e8c2002-02-19 22:42:05 +00005409 if( !pC->useRandomRowid ){
drhc960dcb2015-11-20 19:22:01 +00005410 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
drhe0670b62014-02-12 21:31:12 +00005411 if( rc!=SQLITE_OK ){
5412 goto abort_due_to_error;
5413 }
5414 if( res ){
5415 v = 1; /* IMP: R-61914-48074 */
5416 }else{
drhc960dcb2015-11-20 19:22:01 +00005417 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
drha7c90c42016-06-04 20:37:10 +00005418 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drhe0670b62014-02-12 21:31:12 +00005419 if( v>=MAX_ROWID ){
5420 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00005421 }else{
drhe0670b62014-02-12 21:31:12 +00005422 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00005423 }
drh3fc190c2001-09-14 03:24:23 +00005424 }
drhe0670b62014-02-12 21:31:12 +00005425 }
drh205f48e2004-11-05 00:43:11 +00005426
5427#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00005428 if( pOp->p3 ){
5429 /* Assert that P3 is a valid memory cell. */
5430 assert( pOp->p3>0 );
5431 if( p->pFrame ){
5432 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00005433 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00005434 assert( pOp->p3<=pFrame->nMem );
5435 pMem = &pFrame->aMem[pOp->p3];
5436 }else{
5437 /* Assert that P3 is a valid memory cell. */
drh9f6168b2016-03-19 23:32:58 +00005438 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhe0670b62014-02-12 21:31:12 +00005439 pMem = &aMem[pOp->p3];
5440 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00005441 }
drhe0670b62014-02-12 21:31:12 +00005442 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00005443
drhe0670b62014-02-12 21:31:12 +00005444 REGISTER_TRACE(pOp->p3, pMem);
5445 sqlite3VdbeMemIntegerify(pMem);
5446 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
5447 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhe77caa12016-11-02 13:18:46 +00005448 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
drhe0670b62014-02-12 21:31:12 +00005449 goto abort_due_to_error;
5450 }
5451 if( v<pMem->u.i+1 ){
5452 v = pMem->u.i + 1;
5453 }
5454 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00005455 }
drhe0670b62014-02-12 21:31:12 +00005456#endif
drh5cf8e8c2002-02-19 22:42:05 +00005457 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00005458 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00005459 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00005460 ** engine starts picking positive candidate ROWIDs at random until
5461 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00005462 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
5463 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00005464 cnt = 0;
drh2c4dc632014-09-25 12:31:28 +00005465 do{
5466 sqlite3_randomness(sizeof(v), &v);
drhd8633462014-09-25 17:42:41 +00005467 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
drh42a410d2021-06-19 18:32:20 +00005468 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v,
drh748a52c2010-09-01 11:50:08 +00005469 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00005470 && (res==0)
drh2c4dc632014-09-25 12:31:28 +00005471 && (++cnt<100));
drh9467abf2016-02-17 18:44:11 +00005472 if( rc ) goto abort_due_to_error;
5473 if( res==0 ){
drhc79c7612010-01-01 18:57:48 +00005474 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00005475 goto abort_due_to_error;
5476 }
drh748a52c2010-09-01 11:50:08 +00005477 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00005478 }
drha11846b2004-01-07 18:52:56 +00005479 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00005480 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00005481 }
drh4c583122008-01-04 22:01:03 +00005482 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00005483 break;
5484}
5485
danielk19771f4aa332008-01-03 09:51:55 +00005486/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00005487** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005488**
jplyon5a564222003-06-02 06:15:58 +00005489** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00005490** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00005491** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00005492** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00005493** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00005494**
danielk19771f4aa332008-01-03 09:51:55 +00005495** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
5496** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00005497** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00005498** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00005499**
drheaf6ae22016-11-09 20:14:34 +00005500** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5501** run faster by avoiding an unnecessary seek on cursor P1. However,
5502** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5503** seeks on the cursor or if the most recent seek used a key equal to P3.
drh3e9ca092009-09-08 01:14:48 +00005504**
5505** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
5506** UPDATE operation. Otherwise (if the flag is clear) then this opcode
5507** is part of an INSERT operation. The difference is only important to
5508** the update hook.
5509**
dan319eeb72011-03-19 08:38:50 +00005510** Parameter P4 may point to a Table structure, or may be NULL. If it is
5511** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
5512** following a successful insert.
danielk19771f6eec52006-06-16 06:17:47 +00005513**
drh93aed5a2008-01-16 17:46:38 +00005514** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
5515** allocated, then ownership of P2 is transferred to the pseudo-cursor
5516** and register P2 becomes ephemeral. If the cursor is changed, the
5517** value of register P2 will then change. Make sure this does not
5518** cause any problems.)
5519**
drhf0863fe2005-06-12 21:35:51 +00005520** This instruction only works on tables. The equivalent instruction
5521** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00005522*/
drh50ef6712019-02-22 23:29:56 +00005523case OP_Insert: {
drh3e9ca092009-09-08 01:14:48 +00005524 Mem *pData; /* MEM cell holding data for the record to be inserted */
5525 Mem *pKey; /* MEM cell holding key for the record */
drh3e9ca092009-09-08 01:14:48 +00005526 VdbeCursor *pC; /* Cursor to table into which insert is written */
drh3e9ca092009-09-08 01:14:48 +00005527 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
5528 const char *zDb; /* database name - used by the update hook */
dan319eeb72011-03-19 08:38:50 +00005529 Table *pTab; /* Table structure - used by update and pre-update hooks */
drh8eeb4462016-05-21 20:03:42 +00005530 BtreePayload x; /* Payload to be inserted */
drh856c1032009-06-02 15:21:42 +00005531
drha6c2ed92009-11-14 23:22:23 +00005532 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00005533 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00005534 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00005535 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00005536 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005537 assert( pC->eCurType==CURTYPE_BTREE );
drhbe3da242019-12-29 00:52:41 +00005538 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00005539 assert( pC->uc.pCursor!=0 );
dancb9a3642017-01-30 19:44:53 +00005540 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
drhcbf1b8e2013-11-11 22:55:26 +00005541 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
drh5b6afba2008-01-05 16:29:28 +00005542 REGISTER_TRACE(pOp->p2, pData);
drh4031baf2018-05-28 17:31:20 +00005543 sqlite3VdbeIncrWriteCounter(p, pC);
danielk19775f8d8a82004-05-11 00:28:42 +00005544
drh50ef6712019-02-22 23:29:56 +00005545 pKey = &aMem[pOp->p3];
5546 assert( pKey->flags & MEM_Int );
5547 assert( memIsValid(pKey) );
5548 REGISTER_TRACE(pOp->p3, pKey);
5549 x.nKey = pKey->u.i;
drhe05c9292009-10-29 13:48:10 +00005550
drh9b1c62d2011-03-30 21:04:43 +00005551 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00005552 assert( pC->iDb>=0 );
drh69c33822016-08-18 14:33:11 +00005553 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00005554 pTab = pOp->p4.pTab;
dancb9a3642017-01-30 19:44:53 +00005555 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
drh74c33022016-03-30 12:56:55 +00005556 }else{
drh4ec6f3a2018-01-12 19:33:18 +00005557 pTab = 0;
drhe1e1a432021-10-05 11:11:43 +00005558 zDb = 0;
dan46c47d42011-03-01 18:42:07 +00005559 }
5560
drh9b1c62d2011-03-30 21:04:43 +00005561#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00005562 /* Invoke the pre-update hook, if any */
drh4ec6f3a2018-01-12 19:33:18 +00005563 if( pTab ){
drh84ebe2b2018-01-12 18:46:52 +00005564 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
dana23a8732021-04-21 20:52:17 +00005565 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1);
drh84ebe2b2018-01-12 18:46:52 +00005566 }
drh4ec6f3a2018-01-12 19:33:18 +00005567 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
5568 /* Prevent post-update hook from running in cases when it should not */
5569 pTab = 0;
drh84ebe2b2018-01-12 18:46:52 +00005570 }
dan46c47d42011-03-01 18:42:07 +00005571 }
dancb9a3642017-01-30 19:44:53 +00005572 if( pOp->p5 & OPFLAG_ISNOOP ) break;
drh9b1c62d2011-03-30 21:04:43 +00005573#endif
dan46c47d42011-03-01 18:42:07 +00005574
drh57366d82022-12-22 17:36:02 +00005575 assert( (pOp->p5 & OPFLAG_LASTROWID)==0 || (pOp->p5 & OPFLAG_NCHANGE)!=0 );
5576 if( pOp->p5 & OPFLAG_NCHANGE ){
5577 p->nChange++;
5578 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
5579 }
drh32881be2020-11-17 21:26:13 +00005580 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
dan21cd29a2017-10-23 16:03:54 +00005581 x.pData = pData->z;
5582 x.nData = pData->n;
drh3e9ca092009-09-08 01:14:48 +00005583 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
5584 if( pData->flags & MEM_Zero ){
drh8eeb4462016-05-21 20:03:42 +00005585 x.nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00005586 }else{
drh8eeb4462016-05-21 20:03:42 +00005587 x.nZero = 0;
drha05a7222008-01-19 03:35:58 +00005588 }
drh8eeb4462016-05-21 20:03:42 +00005589 x.pKey = 0;
drhecba1072022-11-19 20:10:55 +00005590 assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT );
drh8eeb4462016-05-21 20:03:42 +00005591 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
dan7aae7352020-12-10 18:06:24 +00005592 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
5593 seekResult
drh3e9ca092009-09-08 01:14:48 +00005594 );
drha05a7222008-01-19 03:35:58 +00005595 pC->deferredMoveto = 0;
5596 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00005597
drha05a7222008-01-19 03:35:58 +00005598 /* Invoke the update-hook if required. */
drh9467abf2016-02-17 18:44:11 +00005599 if( rc ) goto abort_due_to_error;
drh4ec6f3a2018-01-12 19:33:18 +00005600 if( pTab ){
5601 assert( db->xUpdateCallback!=0 );
5602 assert( pTab->aCol!=0 );
5603 db->xUpdateCallback(db->pUpdateArg,
5604 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
5605 zDb, pTab->zName, x.nKey);
drha05a7222008-01-19 03:35:58 +00005606 }
drh5e00f6c2001-09-13 13:46:56 +00005607 break;
5608}
5609
dan7aae7352020-12-10 18:06:24 +00005610/* Opcode: RowCell P1 P2 P3 * *
dand2ffc972020-12-10 19:20:15 +00005611**
5612** P1 and P2 are both open cursors. Both must be opened on the same type
5613** of table - intkey or index. This opcode is used as part of copying
5614** the current row from P2 into P1. If the cursors are opened on intkey
5615** tables, register P3 contains the rowid to use with the new record in
5616** P1. If they are opened on index tables, P3 is not used.
5617**
5618** This opcode must be followed by either an Insert or InsertIdx opcode
5619** with the OPFLAG_PREFORMAT flag set to complete the insert operation.
dan036e0672020-12-08 20:19:07 +00005620*/
dan7aae7352020-12-10 18:06:24 +00005621case OP_RowCell: {
dan036e0672020-12-08 20:19:07 +00005622 VdbeCursor *pDest; /* Cursor to write to */
5623 VdbeCursor *pSrc; /* Cursor to read from */
5624 i64 iKey; /* Rowid value to insert with */
dan7aae7352020-12-10 18:06:24 +00005625 assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert );
drha06eafc2020-12-29 15:06:26 +00005626 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
5627 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
dand2ffc972020-12-10 19:20:15 +00005628 assert( pOp[1].p5 & OPFLAG_PREFORMAT );
dan036e0672020-12-08 20:19:07 +00005629 pDest = p->apCsr[pOp->p1];
5630 pSrc = p->apCsr[pOp->p2];
dancd1b2d02020-12-09 20:33:51 +00005631 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
dan7aae7352020-12-10 18:06:24 +00005632 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
dan036e0672020-12-08 20:19:07 +00005633 if( rc!=SQLITE_OK ) goto abort_due_to_error;
5634 break;
dan7aae7352020-12-10 18:06:24 +00005635};
dan036e0672020-12-08 20:19:07 +00005636
dan438b8812015-09-15 15:55:15 +00005637/* Opcode: Delete P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005638**
drh5edc3122001-09-13 21:53:09 +00005639** Delete the record at which the P1 cursor is currently pointing.
5640**
drhe807bdb2016-01-21 17:06:33 +00005641** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
5642** the cursor will be left pointing at either the next or the previous
5643** record in the table. If it is left pointing at the next record, then
5644** the next Next instruction will be a no-op. As a result, in this case
5645** it is ok to delete a record from within a Next loop. If
5646** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
5647** left in an undefined state.
drhc8d30ac2002-04-12 10:08:59 +00005648**
drhdef19e32016-01-27 16:26:25 +00005649** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
5650** delete one of several associated with deleting a table row and all its
5651** associated index entries. Exactly one of those deletes is the "primary"
5652** delete. The others are all on OPFLAG_FORDELETE cursors or else are
5653** marked with the AUXDELETE flag.
drhe807bdb2016-01-21 17:06:33 +00005654**
5655** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
5656** change count is incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00005657**
drh91fd4d42008-01-19 20:11:25 +00005658** P1 must not be pseudo-table. It has to be a real table with
5659** multiple rows.
5660**
drh5e769a52016-09-28 16:05:53 +00005661** If P4 is not NULL then it points to a Table object. In this case either
dan319eeb72011-03-19 08:38:50 +00005662** the update or pre-update hook, or both, may be invoked. The P1 cursor must
5663** have been positioned using OP_NotFound prior to invoking this opcode in
5664** this case. Specifically, if one is configured, the pre-update hook is
5665** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
5666** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
dan46c47d42011-03-01 18:42:07 +00005667**
5668** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
5669** of the memory cell that contains the value that the rowid of the row will
5670** be set to by the update.
drh5e00f6c2001-09-13 13:46:56 +00005671*/
drh9cbf3422008-01-17 16:22:13 +00005672case OP_Delete: {
drhdfe88ec2008-11-03 20:55:06 +00005673 VdbeCursor *pC;
dan46c47d42011-03-01 18:42:07 +00005674 const char *zDb;
dan319eeb72011-03-19 08:38:50 +00005675 Table *pTab;
dan46c47d42011-03-01 18:42:07 +00005676 int opflags;
drh91fd4d42008-01-19 20:11:25 +00005677
dan46c47d42011-03-01 18:42:07 +00005678 opflags = pOp->p2;
drh653b82a2009-06-22 11:10:47 +00005679 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5680 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005681 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005682 assert( pC->eCurType==CURTYPE_BTREE );
5683 assert( pC->uc.pCursor!=0 );
drh9a65f2c2009-06-22 19:05:40 +00005684 assert( pC->deferredMoveto==0 );
drh4031baf2018-05-28 17:31:20 +00005685 sqlite3VdbeIncrWriteCounter(p, pC);
drh9a65f2c2009-06-22 19:05:40 +00005686
drhb53a5a92014-10-12 22:37:22 +00005687#ifdef SQLITE_DEBUG
drh6b559f32020-01-02 19:50:50 +00005688 if( pOp->p4type==P4_TABLE
5689 && HasRowid(pOp->p4.pTab)
5690 && pOp->p5==0
5691 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
5692 ){
dan438b8812015-09-15 15:55:15 +00005693 /* If p5 is zero, the seek operation that positioned the cursor prior to
5694 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
5695 ** the row that is being deleted */
drha7c90c42016-06-04 20:37:10 +00005696 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan0971ef42019-05-16 20:13:32 +00005697 assert( CORRUPT_DB || pC->movetoTarget==iKey );
drhb53a5a92014-10-12 22:37:22 +00005698 }
5699#endif
drh91fd4d42008-01-19 20:11:25 +00005700
dan438b8812015-09-15 15:55:15 +00005701 /* If the update-hook or pre-update-hook will be invoked, set zDb to
5702 ** the name of the db to pass as to it. Also set local pTab to a copy
5703 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
5704 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
5705 ** VdbeCursor.movetoTarget to the current rowid. */
drhc556f3c2016-03-30 15:30:07 +00005706 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00005707 assert( pC->iDb>=0 );
drhc556f3c2016-03-30 15:30:07 +00005708 assert( pOp->p4.pTab!=0 );
drh69c33822016-08-18 14:33:11 +00005709 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00005710 pTab = pOp->p4.pTab;
drhc556f3c2016-03-30 15:30:07 +00005711 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
drha7c90c42016-06-04 20:37:10 +00005712 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan438b8812015-09-15 15:55:15 +00005713 }
drh74c33022016-03-30 12:56:55 +00005714 }else{
drhe1e1a432021-10-05 11:11:43 +00005715 zDb = 0;
5716 pTab = 0;
drh92fe38e2014-10-14 13:41:32 +00005717 }
dan46c47d42011-03-01 18:42:07 +00005718
drh9b1c62d2011-03-30 21:04:43 +00005719#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00005720 /* Invoke the pre-update-hook if required. */
drhe1e1a432021-10-05 11:11:43 +00005721 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab );
5722 if( db->xPreUpdateCallback && pTab ){
dancb9a3642017-01-30 19:44:53 +00005723 assert( !(opflags & OPFLAG_ISUPDATE)
5724 || HasRowid(pTab)==0
5725 || (aMem[pOp->p3].flags & MEM_Int)
5726 );
dan46c47d42011-03-01 18:42:07 +00005727 sqlite3VdbePreUpdateHook(p, pC,
5728 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
drh92fe38e2014-10-14 13:41:32 +00005729 zDb, pTab, pC->movetoTarget,
dana23a8732021-04-21 20:52:17 +00005730 pOp->p3, -1
dan46c47d42011-03-01 18:42:07 +00005731 );
5732 }
dan46c47d42011-03-01 18:42:07 +00005733 if( opflags & OPFLAG_ISNOOP ) break;
drhc556f3c2016-03-30 15:30:07 +00005734#endif
drhb53a5a92014-10-12 22:37:22 +00005735
drhdef19e32016-01-27 16:26:25 +00005736 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
5737 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
drhe807bdb2016-01-21 17:06:33 +00005738 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
drhdef19e32016-01-27 16:26:25 +00005739 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
drhb89aeb62016-01-27 15:49:32 +00005740
5741#ifdef SQLITE_DEBUG
dane61bbf42016-01-28 17:06:17 +00005742 if( p->pFrame==0 ){
5743 if( pC->isEphemeral==0
5744 && (pOp->p5 & OPFLAG_AUXDELETE)==0
5745 && (pC->wrFlag & OPFLAG_FORDELETE)==0
5746 ){
5747 nExtraDelete++;
5748 }
5749 if( pOp->p2 & OPFLAG_NCHANGE ){
5750 nExtraDelete--;
5751 }
drhb89aeb62016-01-27 15:49:32 +00005752 }
5753#endif
5754
drhc960dcb2015-11-20 19:22:01 +00005755 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
drh91fd4d42008-01-19 20:11:25 +00005756 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00005757 pC->seekResult = 0;
drhd3e1af42016-02-25 18:54:30 +00005758 if( rc ) goto abort_due_to_error;
danielk197794eb6a12005-12-15 15:22:08 +00005759
drh91fd4d42008-01-19 20:11:25 +00005760 /* Invoke the update-hook if required. */
dan46c47d42011-03-01 18:42:07 +00005761 if( opflags & OPFLAG_NCHANGE ){
5762 p->nChange++;
drh7d4c94b2021-10-04 22:34:38 +00005763 if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){
drh92fe38e2014-10-14 13:41:32 +00005764 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
dan438b8812015-09-15 15:55:15 +00005765 pC->movetoTarget);
5766 assert( pC->iDb>=0 );
dan46c47d42011-03-01 18:42:07 +00005767 }
drh5e00f6c2001-09-13 13:46:56 +00005768 }
dan438b8812015-09-15 15:55:15 +00005769
rdcb0c374f2004-02-20 22:53:38 +00005770 break;
5771}
drhb7f1d9a2009-09-08 02:27:58 +00005772/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00005773**
drhb7f1d9a2009-09-08 02:27:58 +00005774** The value of the change counter is copied to the database handle
5775** change counter (returned by subsequent calls to sqlite3_changes()).
5776** Then the VMs internal change counter resets to 0.
5777** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00005778*/
drh9cbf3422008-01-17 16:22:13 +00005779case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00005780 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00005781 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00005782 break;
5783}
5784
drh1153c7b2013-11-01 22:02:56 +00005785/* Opcode: SorterCompare P1 P2 P3 P4
drh72e26de2016-08-24 21:24:04 +00005786** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00005787**
drh1153c7b2013-11-01 22:02:56 +00005788** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00005789** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00005790** the sorter cursor currently points to. Only the first P4 fields
5791** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00005792**
5793** If either P3 or the sorter contains a NULL in one of their significant
5794** fields (not counting the P4 fields at the end which are ignored) then
5795** the comparison is assumed to be equal.
5796**
5797** Fall through to next instruction if the two records compare equal to
5798** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00005799*/
5800case OP_SorterCompare: {
5801 VdbeCursor *pC;
5802 int res;
drhac502322014-07-30 13:56:48 +00005803 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00005804
5805 pC = p->apCsr[pOp->p1];
5806 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00005807 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00005808 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00005809 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00005810 res = 0;
drhac502322014-07-30 13:56:48 +00005811 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00005812 VdbeBranchTaken(res!=0,2);
drh9467abf2016-02-17 18:44:11 +00005813 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005814 if( res ) goto jump_to_p2;
dan5134d132011-09-02 10:31:11 +00005815 break;
5816};
5817
drh6cf4a7d2014-10-13 13:00:58 +00005818/* Opcode: SorterData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005819** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00005820**
5821** Write into register P2 the current sorter data for sorter cursor P1.
drh6cf4a7d2014-10-13 13:00:58 +00005822** Then clear the column header cache on cursor P3.
5823**
5824** This opcode is normally use to move a record out of the sorter and into
5825** a register that is the source for a pseudo-table cursor created using
5826** OpenPseudo. That pseudo-table cursor is the one that is identified by
5827** parameter P3. Clearing the P3 column cache as part of this opcode saves
5828** us from having to issue a separate NullRow instruction to clear that cache.
dan5134d132011-09-02 10:31:11 +00005829*/
5830case OP_SorterData: {
5831 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00005832
dan5134d132011-09-02 10:31:11 +00005833 pOut = &aMem[pOp->p2];
5834 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00005835 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00005836 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00005837 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
drh6cf4a7d2014-10-13 13:00:58 +00005838 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9467abf2016-02-17 18:44:11 +00005839 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00005840 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
dan5134d132011-09-02 10:31:11 +00005841 break;
5842}
5843
drhe7b554d2017-01-09 15:44:25 +00005844/* Opcode: RowData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005845** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00005846**
drh9057fc72016-11-25 19:32:32 +00005847** Write into register P2 the complete row content for the row at
5848** which cursor P1 is currently pointing.
drh98757152008-01-09 23:04:12 +00005849** There is no interpretation of the data.
5850** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00005851** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00005852**
drh9057fc72016-11-25 19:32:32 +00005853** If cursor P1 is an index, then the content is the key of the row.
5854** If cursor P2 is a table, then the content extracted is the data.
drh143f3c42004-01-07 20:37:52 +00005855**
drhde4fcfd2008-01-19 23:50:26 +00005856** If the P1 cursor must be pointing to a valid row (not a NULL row)
5857** of a real table, not a pseudo-table.
drhe7b554d2017-01-09 15:44:25 +00005858**
drh8cdafc32018-05-31 19:00:20 +00005859** If P3!=0 then this opcode is allowed to make an ephemeral pointer
drhe7b554d2017-01-09 15:44:25 +00005860** into the database page. That means that the content of the output
5861** register will be invalidated as soon as the cursor moves - including
drh416a8012018-05-31 19:14:52 +00005862** moves caused by other cursors that "save" the current cursors
drhe7b554d2017-01-09 15:44:25 +00005863** position in order that they can write to the same table. If P3==0
5864** then a copy of the data is made into memory. P3!=0 is faster, but
5865** P3==0 is safer.
5866**
5867** If P3!=0 then the content of the P2 register is unsuitable for use
5868** in OP_Result and any OP_Result will invalidate the P2 register content.
mistachkinab61cf72017-01-09 18:22:54 +00005869** The P2 register content is invalidated by opcodes like OP_Function or
drhe7b554d2017-01-09 15:44:25 +00005870** by any use of another cursor pointing to the same table.
drh143f3c42004-01-07 20:37:52 +00005871*/
danielk1977a7a8e142008-02-13 18:25:27 +00005872case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00005873 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00005874 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00005875 u32 n;
drh70ce3f02003-04-15 19:22:22 +00005876
drhe7b554d2017-01-09 15:44:25 +00005877 pOut = out2Prerelease(p, pOp);
danielk1977a7a8e142008-02-13 18:25:27 +00005878
drh653b82a2009-06-22 11:10:47 +00005879 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5880 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00005881 assert( pC!=0 );
5882 assert( pC->eCurType==CURTYPE_BTREE );
drh14da87f2013-11-20 21:51:33 +00005883 assert( isSorter(pC)==0 );
drhde4fcfd2008-01-19 23:50:26 +00005884 assert( pC->nullRow==0 );
drhc960dcb2015-11-20 19:22:01 +00005885 assert( pC->uc.pCursor!=0 );
5886 pCrsr = pC->uc.pCursor;
drh9a65f2c2009-06-22 19:05:40 +00005887
drh9057fc72016-11-25 19:32:32 +00005888 /* The OP_RowData opcodes always follow OP_NotExists or
drheeb95652016-05-26 20:56:38 +00005889 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
5890 ** that might invalidate the cursor.
5891 ** If this where not the case, on of the following assert()s
drhc22284f2014-10-13 16:02:20 +00005892 ** would fail. Should this ever change (because of changes in the code
5893 ** generator) then the fix would be to insert a call to
5894 ** sqlite3VdbeCursorMoveto().
drh9a65f2c2009-06-22 19:05:40 +00005895 */
5896 assert( pC->deferredMoveto==0 );
drhc22284f2014-10-13 16:02:20 +00005897 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drh9a65f2c2009-06-22 19:05:40 +00005898
drha7c90c42016-06-04 20:37:10 +00005899 n = sqlite3BtreePayloadSize(pCrsr);
drhd66c4f82016-06-04 20:58:35 +00005900 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha7c90c42016-06-04 20:37:10 +00005901 goto too_big;
drhde4fcfd2008-01-19 23:50:26 +00005902 }
drh722246e2014-10-07 23:02:24 +00005903 testcase( n==0 );
drh2a740062020-02-05 18:28:17 +00005904 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut);
drh9467abf2016-02-17 18:44:11 +00005905 if( rc ) goto abort_due_to_error;
drhe7b554d2017-01-09 15:44:25 +00005906 if( !pOp->p3 ) Deephemeralize(pOut);
drhb7654112008-01-12 12:48:07 +00005907 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00005908 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00005909 break;
5910}
5911
drh2133d822008-01-03 18:44:59 +00005912/* Opcode: Rowid P1 P2 * * *
drh088b6152022-04-18 13:57:57 +00005913** Synopsis: r[P2]=PX rowid of P1
drh5e00f6c2001-09-13 13:46:56 +00005914**
drh2133d822008-01-03 18:44:59 +00005915** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00005916** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00005917**
5918** P1 can be either an ordinary table or a virtual table. There used to
5919** be a separate OP_VRowid opcode for use with virtual tables, but this
5920** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00005921*/
dan2adb3092022-12-06 18:48:06 +00005922case OP_Rowid: { /* out2, ncycle */
drhdfe88ec2008-11-03 20:55:06 +00005923 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00005924 i64 v;
drh856c1032009-06-02 15:21:42 +00005925 sqlite3_vtab *pVtab;
5926 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00005927
drh27a348c2015-04-13 19:14:06 +00005928 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00005929 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5930 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00005931 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005932 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00005933 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00005934 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00005935 break;
5936 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00005937 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00005938#ifndef SQLITE_OMIT_VIRTUALTABLE
drhc960dcb2015-11-20 19:22:01 +00005939 }else if( pC->eCurType==CURTYPE_VTAB ){
5940 assert( pC->uc.pVCur!=0 );
5941 pVtab = pC->uc.pVCur->pVtab;
drh044925b2009-04-22 17:15:02 +00005942 pModule = pVtab->pModule;
5943 assert( pModule->xRowid );
drhc960dcb2015-11-20 19:22:01 +00005944 rc = pModule->xRowid(pC->uc.pVCur, &v);
dan016f7812013-08-21 17:35:48 +00005945 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00005946 if( rc ) goto abort_due_to_error;
drh044925b2009-04-22 17:15:02 +00005947#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00005948 }else{
drhc960dcb2015-11-20 19:22:01 +00005949 assert( pC->eCurType==CURTYPE_BTREE );
5950 assert( pC->uc.pCursor!=0 );
drhc22284f2014-10-13 16:02:20 +00005951 rc = sqlite3VdbeCursorRestore(pC);
drh61495262009-04-22 15:32:59 +00005952 if( rc ) goto abort_due_to_error;
dan2b8669a2014-11-17 19:42:48 +00005953 if( pC->nullRow ){
5954 pOut->flags = MEM_Null;
5955 break;
5956 }
drha7c90c42016-06-04 20:37:10 +00005957 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh5e00f6c2001-09-13 13:46:56 +00005958 }
drh4c583122008-01-04 22:01:03 +00005959 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00005960 break;
5961}
5962
drh9cbf3422008-01-17 16:22:13 +00005963/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00005964**
5965** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00005966** that occur while the cursor is on the null row will always
5967** write a NULL.
drha10be3d2022-02-25 18:51:09 +00005968**
drh3ac62432022-04-13 17:41:03 +00005969** If cursor P1 is not previously opened, open it now to a special
5970** pseudo-cursor that always returns NULL for every column.
drh17f71932002-02-21 12:01:27 +00005971*/
drh9cbf3422008-01-17 16:22:13 +00005972case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00005973 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00005974
drh653b82a2009-06-22 11:10:47 +00005975 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5976 pC = p->apCsr[pOp->p1];
drh3ac62432022-04-13 17:41:03 +00005977 if( pC==0 ){
5978 /* If the cursor is not already open, create a special kind of
5979 ** pseudo-cursor that always gives null rows. */
5980 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO);
5981 if( pC==0 ) goto no_mem;
5982 pC->seekResult = 0;
5983 pC->isTable = 1;
drh27a242c2022-06-14 22:21:23 +00005984 pC->noReuse = 1;
drh3ac62432022-04-13 17:41:03 +00005985 pC->uc.pCursor = sqlite3BtreeFakeValidCursor();
5986 }
drhd7556d22004-05-14 21:59:40 +00005987 pC->nullRow = 1;
drh399af1d2013-11-20 17:25:55 +00005988 pC->cacheStatus = CACHE_STALE;
drhc960dcb2015-11-20 19:22:01 +00005989 if( pC->eCurType==CURTYPE_BTREE ){
5990 assert( pC->uc.pCursor!=0 );
5991 sqlite3BtreeClearCursor(pC->uc.pCursor);
danielk1977be51a652008-10-08 17:58:48 +00005992 }
drhcf025a82018-06-07 18:01:21 +00005993#ifdef SQLITE_DEBUG
5994 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
5995#endif
drh17f71932002-02-21 12:01:27 +00005996 break;
5997}
5998
drh86b40df2017-08-01 19:53:43 +00005999/* Opcode: SeekEnd P1 * * * *
6000**
6001** Position cursor P1 at the end of the btree for the purpose of
6002** appending a new entry onto the btree.
6003**
6004** It is assumed that the cursor is used only for appending and so
6005** if the cursor is valid, then the cursor must already be pointing
6006** at the end of the btree and so no changes are made to
6007** the cursor.
6008*/
6009/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00006010**
drh8af3f772014-07-25 18:01:06 +00006011** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00006012** will refer to the last entry in the database table or index.
6013** If the table or index is empty and P2>0, then jump immediately to P2.
6014** If P2 is 0 or if the table or index is not empty, fall through
6015** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00006016**
6017** This opcode leaves the cursor configured to move in reverse order,
6018** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00006019** configured to use Prev, not Next.
drh9562b552002-02-19 15:00:07 +00006020*/
dan2adb3092022-12-06 18:48:06 +00006021case OP_SeekEnd: /* ncycle */
6022case OP_Last: { /* jump, ncycle */
drhdfe88ec2008-11-03 20:55:06 +00006023 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00006024 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00006025 int res;
drh9562b552002-02-19 15:00:07 +00006026
drh653b82a2009-06-22 11:10:47 +00006027 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6028 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00006029 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00006030 assert( pC->eCurType==CURTYPE_BTREE );
6031 pCrsr = pC->uc.pCursor;
drh7abc5402011-10-22 21:00:46 +00006032 res = 0;
drh3da046d2013-11-11 03:24:11 +00006033 assert( pCrsr!=0 );
drh8af3f772014-07-25 18:01:06 +00006034#ifdef SQLITE_DEBUG
drh86b40df2017-08-01 19:53:43 +00006035 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00006036#endif
drh86b40df2017-08-01 19:53:43 +00006037 if( pOp->opcode==OP_SeekEnd ){
drhd6ef5af2016-11-15 04:00:24 +00006038 assert( pOp->p2==0 );
drh86b40df2017-08-01 19:53:43 +00006039 pC->seekResult = -1;
6040 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
6041 break;
6042 }
6043 }
6044 rc = sqlite3BtreeLast(pCrsr, &res);
6045 pC->nullRow = (u8)res;
6046 pC->deferredMoveto = 0;
6047 pC->cacheStatus = CACHE_STALE;
6048 if( rc ) goto abort_due_to_error;
6049 if( pOp->p2>0 ){
6050 VdbeBranchTaken(res!=0,2);
6051 if( res ) goto jump_to_p2;
drh9562b552002-02-19 15:00:07 +00006052 }
6053 break;
6054}
6055
drh5e98e832017-02-17 19:24:06 +00006056/* Opcode: IfSmaller P1 P2 P3 * *
6057**
6058** Estimate the number of rows in the table P1. Jump to P2 if that
6059** estimate is less than approximately 2**(0.1*P3).
6060*/
6061case OP_IfSmaller: { /* jump */
6062 VdbeCursor *pC;
6063 BtCursor *pCrsr;
6064 int res;
6065 i64 sz;
6066
6067 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6068 pC = p->apCsr[pOp->p1];
6069 assert( pC!=0 );
6070 pCrsr = pC->uc.pCursor;
6071 assert( pCrsr );
6072 rc = sqlite3BtreeFirst(pCrsr, &res);
6073 if( rc ) goto abort_due_to_error;
6074 if( res==0 ){
6075 sz = sqlite3BtreeRowCountEst(pCrsr);
6076 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
6077 }
6078 VdbeBranchTaken(res!=0,2);
6079 if( res ) goto jump_to_p2;
6080 break;
6081}
6082
drh0342b1f2005-09-01 03:07:44 +00006083
drh6bd4dc62016-12-23 16:05:22 +00006084/* Opcode: SorterSort P1 P2 * * *
6085**
6086** After all records have been inserted into the Sorter object
6087** identified by P1, invoke this opcode to actually do the sorting.
6088** Jump to P2 if there are no records to be sorted.
6089**
6090** This opcode is an alias for OP_Sort and OP_Rewind that is used
6091** for Sorter objects.
6092*/
drh9cbf3422008-01-17 16:22:13 +00006093/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00006094**
6095** This opcode does exactly the same thing as OP_Rewind except that
6096** it increments an undocumented global variable used for testing.
6097**
6098** Sorting is accomplished by writing records into a sorting index,
6099** then rewinding that index and playing it back from beginning to
6100** end. We use the OP_Sort opcode instead of OP_Rewind to do the
6101** rewinding so that the global variable will be incremented and
6102** regression tests can determine whether or not the optimizer is
6103** correctly optimizing out sorts.
6104*/
drhc6aff302011-09-01 15:32:47 +00006105case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00006106case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00006107#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00006108 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00006109 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00006110#endif
drh9b47ee32013-08-20 03:13:51 +00006111 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00006112 /* Fall through into OP_Rewind */
drh08b92082020-08-10 14:18:00 +00006113 /* no break */ deliberate_fall_through
drh0342b1f2005-09-01 03:07:44 +00006114}
drh038ebf62019-03-29 15:21:22 +00006115/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00006116**
drhf0863fe2005-06-12 21:35:51 +00006117** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00006118** will refer to the first entry in the database table or index.
dan04489b62014-10-31 20:11:32 +00006119** If the table or index is empty, jump immediately to P2.
6120** If the table or index is not empty, fall through to the following
6121** instruction.
drh8af3f772014-07-25 18:01:06 +00006122**
6123** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00006124** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00006125** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00006126*/
dan2adb3092022-12-06 18:48:06 +00006127case OP_Rewind: { /* jump, ncycle */
drhdfe88ec2008-11-03 20:55:06 +00006128 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00006129 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00006130 int res;
drh5e00f6c2001-09-13 13:46:56 +00006131
drh653b82a2009-06-22 11:10:47 +00006132 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh038ebf62019-03-29 15:21:22 +00006133 assert( pOp->p5==0 );
drh653b82a2009-06-22 11:10:47 +00006134 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00006135 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00006136 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00006137 res = 1;
drh8af3f772014-07-25 18:01:06 +00006138#ifdef SQLITE_DEBUG
6139 pC->seekOp = OP_Rewind;
6140#endif
dan689ab892011-08-12 15:02:00 +00006141 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00006142 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00006143 }else{
drhc960dcb2015-11-20 19:22:01 +00006144 assert( pC->eCurType==CURTYPE_BTREE );
6145 pCrsr = pC->uc.pCursor;
dana205a482011-08-27 18:48:57 +00006146 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00006147 rc = sqlite3BtreeFirst(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00006148 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00006149 pC->cacheStatus = CACHE_STALE;
drhf4dada72004-05-11 09:57:35 +00006150 }
drh9467abf2016-02-17 18:44:11 +00006151 if( rc ) goto abort_due_to_error;
drh9c1905f2008-12-10 22:32:56 +00006152 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00006153 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00006154 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00006155 if( res ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00006156 break;
6157}
6158
drha7c9dd52022-02-24 14:44:23 +00006159/* Opcode: Next P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00006160**
6161** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00006162** table or index. If there are no more key/value pairs then fall through
6163** to the following instruction. But if the cursor advance was successful,
6164** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00006165**
drh5dad9a32014-07-25 18:37:42 +00006166** The Next opcode is only valid following an SeekGT, SeekGE, or
6167** OP_Rewind opcode used to position the cursor. Next is not allowed
6168** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00006169**
drhf93cd942013-11-21 03:12:25 +00006170** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
6171** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00006172**
drhe39a7322014-02-03 14:04:11 +00006173** The P3 value is a hint to the btree implementation. If P3==1, that
6174** means P1 is an SQL index and that this instruction could have been
6175** omitted if that index had been unique. P3 is usually 0. P3 is
6176** always either 0 or 1.
6177**
drhafc266a2010-03-31 17:47:44 +00006178** If P5 is positive and the jump is taken, then event counter
6179** number P5-1 in the prepared statement is incremented.
6180**
drhf1949b62018-06-07 17:32:59 +00006181** See also: Prev
drh8721ce42001-11-07 14:22:00 +00006182*/
drha7c9dd52022-02-24 14:44:23 +00006183/* Opcode: Prev P1 P2 P3 * P5
drhc045ec52002-12-04 20:01:06 +00006184**
6185** Back up cursor P1 so that it points to the previous key/data pair in its
6186** table or index. If there is no previous key/value pairs then fall through
6187** to the following instruction. But if the cursor backup was successful,
6188** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00006189**
drh8af3f772014-07-25 18:01:06 +00006190**
drh5dad9a32014-07-25 18:37:42 +00006191** The Prev opcode is only valid following an SeekLT, SeekLE, or
6192** OP_Last opcode used to position the cursor. Prev is not allowed
6193** to follow SeekGT, SeekGE, or OP_Rewind.
drh8af3f772014-07-25 18:01:06 +00006194**
drhf93cd942013-11-21 03:12:25 +00006195** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
6196** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00006197**
drhe39a7322014-02-03 14:04:11 +00006198** The P3 value is a hint to the btree implementation. If P3==1, that
6199** means P1 is an SQL index and that this instruction could have been
6200** omitted if that index had been unique. P3 is usually 0. P3 is
6201** always either 0 or 1.
6202**
drhafc266a2010-03-31 17:47:44 +00006203** If P5 is positive and the jump is taken, then event counter
6204** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00006205*/
drh6bd4dc62016-12-23 16:05:22 +00006206/* Opcode: SorterNext P1 P2 * * P5
6207**
6208** This opcode works just like OP_Next except that P1 must be a
6209** sorter object for which the OP_SorterSort opcode has been
6210** invoked. This opcode advances the cursor to the next sorted
6211** record, or jumps to P2 if there are no more sorted records.
6212*/
drhf93cd942013-11-21 03:12:25 +00006213case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00006214 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00006215
drhf93cd942013-11-21 03:12:25 +00006216 pC = p->apCsr[pOp->p1];
6217 assert( isSorter(pC) );
drh2ab792e2017-05-30 18:34:07 +00006218 rc = sqlite3VdbeSorterNext(db, pC);
drhf93cd942013-11-21 03:12:25 +00006219 goto next_tail;
drha7c9dd52022-02-24 14:44:23 +00006220
dan2adb3092022-12-06 18:48:06 +00006221case OP_Prev: /* jump, ncycle */
drha7c9dd52022-02-24 14:44:23 +00006222 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh35908b12022-08-23 17:51:39 +00006223 assert( pOp->p5==0
6224 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
6225 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
drha7c9dd52022-02-24 14:44:23 +00006226 pC = p->apCsr[pOp->p1];
6227 assert( pC!=0 );
6228 assert( pC->deferredMoveto==0 );
6229 assert( pC->eCurType==CURTYPE_BTREE );
6230 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
6231 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
6232 || pC->seekOp==OP_NullRow);
6233 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
6234 goto next_tail;
6235
dan2adb3092022-12-06 18:48:06 +00006236case OP_Next: /* jump, ncycle */
drh70ce3f02003-04-15 19:22:22 +00006237 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh35908b12022-08-23 17:51:39 +00006238 assert( pOp->p5==0
6239 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
6240 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
drhd7556d22004-05-14 21:59:40 +00006241 pC = p->apCsr[pOp->p1];
drhf93cd942013-11-21 03:12:25 +00006242 assert( pC!=0 );
6243 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00006244 assert( pC->eCurType==CURTYPE_BTREE );
drha7c9dd52022-02-24 14:44:23 +00006245 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drh790b37a2019-08-27 17:01:07 +00006246 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
6247 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
6248 || pC->seekOp==OP_IfNoHope);
drha7c9dd52022-02-24 14:44:23 +00006249 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3);
drh8af3f772014-07-25 18:01:06 +00006250
drhf93cd942013-11-21 03:12:25 +00006251next_tail:
drha3460582008-07-11 21:02:53 +00006252 pC->cacheStatus = CACHE_STALE;
drh2ab792e2017-05-30 18:34:07 +00006253 VdbeBranchTaken(rc==SQLITE_OK,2);
6254 if( rc==SQLITE_OK ){
drhf93cd942013-11-21 03:12:25 +00006255 pC->nullRow = 0;
drh9b47ee32013-08-20 03:13:51 +00006256 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00006257#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00006258 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00006259#endif
drhf56fa462015-04-13 21:39:54 +00006260 goto jump_to_p2_and_check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00006261 }
drh2ab792e2017-05-30 18:34:07 +00006262 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
6263 rc = SQLITE_OK;
6264 pC->nullRow = 1;
drh49afe3a2013-07-10 03:05:14 +00006265 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00006266}
6267
drh9b4eaeb2016-11-09 00:10:33 +00006268/* Opcode: IdxInsert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00006269** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00006270**
drhef8662b2011-06-20 21:47:58 +00006271** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00006272** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00006273** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00006274**
drhfb8c56f2016-11-09 01:19:25 +00006275** If P4 is not zero, then it is the number of values in the unpacked
drh9b4eaeb2016-11-09 00:10:33 +00006276** key of reg(P2). In that case, P3 is the index of the first register
6277** for the unpacked key. The availability of the unpacked key can sometimes
6278** be an optimization.
6279**
6280** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
6281** that this insert is likely to be an append.
drhe4d90812007-03-29 05:51:49 +00006282**
mistachkin21a919f2014-02-07 03:28:02 +00006283** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
6284** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
6285** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00006286**
drheaf6ae22016-11-09 20:14:34 +00006287** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
6288** run faster by avoiding an unnecessary seek on cursor P1. However,
6289** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
6290** seeks on the cursor or if the most recent seek used a key equivalent
6291** to P2.
drh0fd61352014-02-07 02:29:45 +00006292**
drhf0863fe2005-06-12 21:35:51 +00006293** This instruction only works for indices. The equivalent instruction
6294** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00006295*/
drh9cbf3422008-01-17 16:22:13 +00006296case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00006297 VdbeCursor *pC;
drh8eeb4462016-05-21 20:03:42 +00006298 BtreePayload x;
drh856c1032009-06-02 15:21:42 +00006299
drh653b82a2009-06-22 11:10:47 +00006300 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6301 pC = p->apCsr[pOp->p1];
drh4031baf2018-05-28 17:31:20 +00006302 sqlite3VdbeIncrWriteCounter(p, pC);
drh653b82a2009-06-22 11:10:47 +00006303 assert( pC!=0 );
drhc879c4e2020-02-06 13:57:08 +00006304 assert( !isSorter(pC) );
drh3c657212009-11-17 23:59:58 +00006305 pIn2 = &aMem[pOp->p2];
dan7aae7352020-12-10 18:06:24 +00006306 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
drh6546af12013-11-04 15:23:25 +00006307 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhc879c4e2020-02-06 13:57:08 +00006308 assert( pC->eCurType==CURTYPE_BTREE );
drh3da046d2013-11-11 03:24:11 +00006309 assert( pC->isTable==0 );
6310 rc = ExpandBlob(pIn2);
drh9467abf2016-02-17 18:44:11 +00006311 if( rc ) goto abort_due_to_error;
drhc879c4e2020-02-06 13:57:08 +00006312 x.nKey = pIn2->n;
6313 x.pKey = pIn2->z;
6314 x.aMem = aMem + pOp->p3;
6315 x.nMem = (u16)pOp->p4.i;
6316 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
dan7aae7352020-12-10 18:06:24 +00006317 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
drhc879c4e2020-02-06 13:57:08 +00006318 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
6319 );
6320 assert( pC->deferredMoveto==0 );
6321 pC->cacheStatus = CACHE_STALE;
6322 if( rc) goto abort_due_to_error;
6323 break;
6324}
6325
6326/* Opcode: SorterInsert P1 P2 * * *
6327** Synopsis: key=r[P2]
6328**
6329** Register P2 holds an SQL index key made using the
6330** MakeRecord instructions. This opcode writes that key
6331** into the sorter P1. Data for the entry is nil.
6332*/
6333case OP_SorterInsert: { /* in2 */
6334 VdbeCursor *pC;
6335
6336 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6337 pC = p->apCsr[pOp->p1];
6338 sqlite3VdbeIncrWriteCounter(p, pC);
6339 assert( pC!=0 );
6340 assert( isSorter(pC) );
6341 pIn2 = &aMem[pOp->p2];
6342 assert( pIn2->flags & MEM_Blob );
6343 assert( pC->isTable==0 );
6344 rc = ExpandBlob(pIn2);
6345 if( rc ) goto abort_due_to_error;
6346 rc = sqlite3VdbeSorterWrite(pC, pIn2);
drh9467abf2016-02-17 18:44:11 +00006347 if( rc) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00006348 break;
6349}
6350
drh85bd3532020-05-05 18:42:49 +00006351/* Opcode: IdxDelete P1 P2 P3 * P5
drhf63552b2013-10-30 00:25:03 +00006352** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00006353**
drhe14006d2008-03-25 17:23:32 +00006354** The content of P3 registers starting at register P2 form
6355** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00006356** index opened by cursor P1.
drh85bd3532020-05-05 18:42:49 +00006357**
6358** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error
6359** if no matching index entry is found. This happens when running
6360** an UPDATE or DELETE statement and the index entry to be updated
6361** or deleted is not found. For some uses of IdxDelete
6362** (example: the EXCEPT operator) it does not matter that no matching
drha76b1512021-08-11 18:43:54 +00006363** entry is found. For those cases, P5 is zero. Also, do not raise
6364** this (self-correcting and non-critical) error if in writable_schema mode.
drh5e00f6c2001-09-13 13:46:56 +00006365*/
drhe14006d2008-03-25 17:23:32 +00006366case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00006367 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00006368 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00006369 int res;
6370 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00006371
drhe14006d2008-03-25 17:23:32 +00006372 assert( pOp->p3>0 );
drh9f6168b2016-03-19 23:32:58 +00006373 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00006374 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6375 pC = p->apCsr[pOp->p1];
6376 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00006377 assert( pC->eCurType==CURTYPE_BTREE );
drh4031baf2018-05-28 17:31:20 +00006378 sqlite3VdbeIncrWriteCounter(p, pC);
drhc960dcb2015-11-20 19:22:01 +00006379 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00006380 assert( pCrsr!=0 );
drh3da046d2013-11-11 03:24:11 +00006381 r.pKeyInfo = pC->pKeyInfo;
6382 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00006383 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00006384 r.aMem = &aMem[pOp->p2];
drh42a410d2021-06-19 18:32:20 +00006385 rc = sqlite3BtreeIndexMoveto(pCrsr, &r, &res);
drh9467abf2016-02-17 18:44:11 +00006386 if( rc ) goto abort_due_to_error;
6387 if( res==0 ){
dane61bbf42016-01-28 17:06:17 +00006388 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
drh9467abf2016-02-17 18:44:11 +00006389 if( rc ) goto abort_due_to_error;
drha76b1512021-08-11 18:43:54 +00006390 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
drhe5ceaac2021-01-25 21:24:14 +00006391 rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX, __LINE__, "index corruption");
drh85bd3532020-05-05 18:42:49 +00006392 goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00006393 }
drh3da046d2013-11-11 03:24:11 +00006394 assert( pC->deferredMoveto==0 );
6395 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00006396 pC->seekResult = 0;
drh5e00f6c2001-09-13 13:46:56 +00006397 break;
6398}
6399
drh170ad682017-06-02 15:44:22 +00006400/* Opcode: DeferredSeek P1 * P3 P4 *
6401** Synopsis: Move P3 to P1.rowid if needed
drh784c1b92016-01-30 16:59:56 +00006402**
6403** P1 is an open index cursor and P3 is a cursor on the corresponding
6404** table. This opcode does a deferred seek of the P3 table cursor
6405** to the row that corresponds to the current row of P1.
6406**
6407** This is a deferred seek. Nothing actually happens until
6408** the cursor is used to read a record. That way, if no reads
6409** occur, no unnecessary I/O happens.
6410**
6411** P4 may be an array of integers (type P4_INTARRAY) containing
drh19d720d2016-02-03 19:52:06 +00006412** one entry for each column in the P3 table. If array entry a(i)
6413** is non-zero, then reading column a(i)-1 from cursor P3 is
drh784c1b92016-01-30 16:59:56 +00006414** equivalent to performing the deferred seek and then reading column i
6415** from P1. This information is stored in P3 and used to redirect
6416** reads against P3 over to P1, thus possibly avoiding the need to
6417** seek and read cursor P3.
6418*/
drh2133d822008-01-03 18:44:59 +00006419/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006420** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00006421**
drh2133d822008-01-03 18:44:59 +00006422** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00006423** the end of the index key pointed to by cursor P1. This integer should be
6424** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00006425**
drh9437bd22009-02-01 00:29:56 +00006426** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00006427*/
dan2adb3092022-12-06 18:48:06 +00006428case OP_DeferredSeek: /* ncycle */
6429case OP_IdxRowid: { /* out2, ncycle */
drh170ad682017-06-02 15:44:22 +00006430 VdbeCursor *pC; /* The P1 index cursor */
6431 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
6432 i64 rowid; /* Rowid that P1 current points to */
drh8721ce42001-11-07 14:22:00 +00006433
drh653b82a2009-06-22 11:10:47 +00006434 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6435 pC = p->apCsr[pOp->p1];
6436 assert( pC!=0 );
drheab6c122022-04-14 12:59:25 +00006437 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) );
drh784c1b92016-01-30 16:59:56 +00006438 assert( pC->uc.pCursor!=0 );
drhc504f672022-04-14 14:19:23 +00006439 assert( pC->isTable==0 || IsNullCursor(pC) );
drhc22284f2014-10-13 16:02:20 +00006440 assert( pC->deferredMoveto==0 );
drh784c1b92016-01-30 16:59:56 +00006441 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
6442
6443 /* The IdxRowid and Seek opcodes are combined because of the commonality
6444 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
6445 rc = sqlite3VdbeCursorRestore(pC);
drhc22284f2014-10-13 16:02:20 +00006446
danaa07b362022-08-25 13:32:55 +00006447 /* sqlite3VdbeCursorRestore() may fail if the cursor has been disturbed
6448 ** since it was last positioned and an error (e.g. OOM or an IO error)
6449 ** occurs while trying to reposition it. */
6450 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhc22284f2014-10-13 16:02:20 +00006451
drh3da046d2013-11-11 03:24:11 +00006452 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00006453 rowid = 0; /* Not needed. Only used to silence a warning. */
drh784c1b92016-01-30 16:59:56 +00006454 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
drh3da046d2013-11-11 03:24:11 +00006455 if( rc!=SQLITE_OK ){
6456 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00006457 }
drh170ad682017-06-02 15:44:22 +00006458 if( pOp->opcode==OP_DeferredSeek ){
drh784c1b92016-01-30 16:59:56 +00006459 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
6460 pTabCur = p->apCsr[pOp->p3];
6461 assert( pTabCur!=0 );
6462 assert( pTabCur->eCurType==CURTYPE_BTREE );
6463 assert( pTabCur->uc.pCursor!=0 );
6464 assert( pTabCur->isTable );
6465 pTabCur->nullRow = 0;
6466 pTabCur->movetoTarget = rowid;
6467 pTabCur->deferredMoveto = 1;
drhfc569502022-02-25 20:11:59 +00006468 pTabCur->cacheStatus = CACHE_STALE;
drh784c1b92016-01-30 16:59:56 +00006469 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
drhe44ac382021-03-18 13:19:41 +00006470 assert( !pTabCur->isEphemeral );
drhb2486682022-01-03 01:43:28 +00006471 pTabCur->ub.aAltMap = pOp->p4.ai;
6472 assert( !pC->isEphemeral );
drh784c1b92016-01-30 16:59:56 +00006473 pTabCur->pAltCursor = pC;
6474 }else{
6475 pOut = out2Prerelease(p, pOp);
6476 pOut->u.i = rowid;
drh784c1b92016-01-30 16:59:56 +00006477 }
6478 }else{
6479 assert( pOp->opcode==OP_IdxRowid );
6480 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
drh8721ce42001-11-07 14:22:00 +00006481 }
6482 break;
6483}
6484
drhbe3da242019-12-29 00:52:41 +00006485/* Opcode: FinishSeek P1 * * * *
6486**
6487** If cursor P1 was previously moved via OP_DeferredSeek, complete that
6488** seek operation now, without further delay. If the cursor seek has
6489** already occurred, this instruction is a no-op.
6490*/
dan2adb3092022-12-06 18:48:06 +00006491case OP_FinishSeek: { /* ncycle */
6492 VdbeCursor *pC; /* The P1 index cursor */
drhbe3da242019-12-29 00:52:41 +00006493
6494 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6495 pC = p->apCsr[pOp->p1];
6496 if( pC->deferredMoveto ){
6497 rc = sqlite3VdbeFinishMoveto(pC);
6498 if( rc ) goto abort_due_to_error;
6499 }
6500 break;
6501}
6502
drhc51ceeb2020-08-31 12:29:03 +00006503/* Opcode: IdxGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00006504** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00006505**
danielk197761dd5832008-04-18 11:31:12 +00006506** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00006507** key that omits the PRIMARY KEY. Compare this key value against the index
6508** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
6509** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00006510**
danielk197761dd5832008-04-18 11:31:12 +00006511** If the P1 index entry is greater than or equal to the key value
6512** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00006513*/
drhc51ceeb2020-08-31 12:29:03 +00006514/* Opcode: IdxGT P1 P2 P3 P4 *
drh4a1d3652014-02-14 15:13:36 +00006515** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00006516**
drh4a1d3652014-02-14 15:13:36 +00006517** The P4 register values beginning with P3 form an unpacked index
6518** key that omits the PRIMARY KEY. Compare this key value against the index
6519** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
6520** fields at the end.
6521**
6522** If the P1 index entry is greater than the key value
6523** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00006524*/
drhc51ceeb2020-08-31 12:29:03 +00006525/* Opcode: IdxLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00006526** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00006527**
danielk197761dd5832008-04-18 11:31:12 +00006528** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00006529** key that omits the PRIMARY KEY or ROWID. Compare this key value against
6530** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
6531** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00006532**
danielk197761dd5832008-04-18 11:31:12 +00006533** If the P1 index entry is less than the key value then jump to P2.
6534** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00006535*/
drhc51ceeb2020-08-31 12:29:03 +00006536/* Opcode: IdxLE P1 P2 P3 P4 *
drh4a1d3652014-02-14 15:13:36 +00006537** Synopsis: key=r[P3@P4]
6538**
6539** The P4 register values beginning with P3 form an unpacked index
6540** key that omits the PRIMARY KEY or ROWID. Compare this key value against
6541** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
6542** ROWID on the P1 index.
6543**
6544** If the P1 index entry is less than or equal to the key value then jump
6545** to P2. Otherwise fall through to the next instruction.
6546*/
dan2adb3092022-12-06 18:48:06 +00006547case OP_IdxLE: /* jump, ncycle */
6548case OP_IdxGT: /* jump, ncycle */
6549case OP_IdxLT: /* jump, ncycle */
6550case OP_IdxGE: { /* jump, ncycle */
drhdfe88ec2008-11-03 20:55:06 +00006551 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00006552 int res;
6553 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00006554
drh653b82a2009-06-22 11:10:47 +00006555 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6556 pC = p->apCsr[pOp->p1];
6557 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00006558 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00006559 assert( pC->eCurType==CURTYPE_BTREE );
6560 assert( pC->uc.pCursor!=0);
drh3da046d2013-11-11 03:24:11 +00006561 assert( pC->deferredMoveto==0 );
drh3da046d2013-11-11 03:24:11 +00006562 assert( pOp->p4type==P4_INT32 );
6563 r.pKeyInfo = pC->pKeyInfo;
6564 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00006565 if( pOp->opcode<OP_IdxLT ){
6566 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00006567 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00006568 }else{
drh4a1d3652014-02-14 15:13:36 +00006569 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00006570 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00006571 }
6572 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00006573#ifdef SQLITE_DEBUG
drh5eae9742018-08-03 13:56:26 +00006574 {
6575 int i;
6576 for(i=0; i<r.nField; i++){
6577 assert( memIsValid(&r.aMem[i]) );
6578 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
6579 }
6580 }
drh2b4ded92010-09-27 21:09:31 +00006581#endif
drhc40076a2020-09-29 16:05:09 +00006582
6583 /* Inlined version of sqlite3VdbeIdxKeyCompare() */
6584 {
6585 i64 nCellKey = 0;
6586 BtCursor *pCur;
6587 Mem m;
6588
6589 assert( pC->eCurType==CURTYPE_BTREE );
6590 pCur = pC->uc.pCursor;
6591 assert( sqlite3BtreeCursorIsValid(pCur) );
6592 nCellKey = sqlite3BtreePayloadSize(pCur);
6593 /* nCellKey will always be between 0 and 0xffffffff because of the way
6594 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
6595 if( nCellKey<=0 || nCellKey>0x7fffffff ){
6596 rc = SQLITE_CORRUPT_BKPT;
6597 goto abort_due_to_error;
6598 }
6599 sqlite3VdbeMemInit(&m, db, 0);
6600 rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m);
6601 if( rc ) goto abort_due_to_error;
6602 res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, &r, 0);
drhfc854502022-03-02 17:50:59 +00006603 sqlite3VdbeMemReleaseMalloc(&m);
drhc40076a2020-09-29 16:05:09 +00006604 }
6605 /* End of inlined sqlite3VdbeIdxKeyCompare() */
6606
drh4a1d3652014-02-14 15:13:36 +00006607 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
6608 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
6609 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00006610 res = -res;
6611 }else{
drh4a1d3652014-02-14 15:13:36 +00006612 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00006613 res++;
6614 }
drh688852a2014-02-17 22:40:43 +00006615 VdbeBranchTaken(res>0,2);
drhc40076a2020-09-29 16:05:09 +00006616 assert( rc==SQLITE_OK );
drhf56fa462015-04-13 21:39:54 +00006617 if( res>0 ) goto jump_to_p2;
drh8721ce42001-11-07 14:22:00 +00006618 break;
6619}
6620
drh98757152008-01-09 23:04:12 +00006621/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00006622**
6623** Delete an entire database table or index whose root page in the database
6624** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00006625**
drh98757152008-01-09 23:04:12 +00006626** The table being destroyed is in the main database file if P3==0. If
6627** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00006628** that is used to store tables create using CREATE TEMPORARY TABLE.
6629**
drh205f48e2004-11-05 00:43:11 +00006630** If AUTOVACUUM is enabled then it is possible that another root page
6631** might be moved into the newly deleted root page in order to keep all
6632** root pages contiguous at the beginning of the database. The former
6633** value of the root page that moved - its value before the move occurred -
dana34adaf2017-04-08 14:11:47 +00006634** is stored in register P2. If no page movement was required (because the
6635** table being dropped was already the last one in the database) then a
6636** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
6637** is stored in register P2.
6638**
6639** This opcode throws an error if there are any active reader VMs when
6640** it is invoked. This is done to avoid the difficulty associated with
6641** updating existing cursors when a root page is moved in an AUTOVACUUM
6642** database. This error is thrown even if the database is not an AUTOVACUUM
6643** db in order to avoid introducing an incompatibility between autovacuum
6644** and non-autovacuum modes.
drh205f48e2004-11-05 00:43:11 +00006645**
drhb19a2bc2001-09-16 00:13:26 +00006646** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00006647*/
drh27a348c2015-04-13 19:14:06 +00006648case OP_Destroy: { /* out2 */
danielk1977a0bf2652004-11-04 14:30:04 +00006649 int iMoved;
drh856c1032009-06-02 15:21:42 +00006650 int iDb;
drh3a949872012-09-18 13:20:13 +00006651
drh4031baf2018-05-28 17:31:20 +00006652 sqlite3VdbeIncrWriteCounter(p, 0);
drh9e92a472013-06-27 17:40:30 +00006653 assert( p->readOnly==0 );
drh055f2982016-01-15 15:06:41 +00006654 assert( pOp->p1>1 );
drh27a348c2015-04-13 19:14:06 +00006655 pOut = out2Prerelease(p, pOp);
drh3c657212009-11-17 23:59:58 +00006656 pOut->flags = MEM_Null;
drh086723a2015-03-24 12:51:52 +00006657 if( db->nVdbeRead > db->nVDestroy+1 ){
danielk1977e6efa742004-11-10 11:55:10 +00006658 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00006659 p->errorAction = OE_Abort;
drh9467abf2016-02-17 18:44:11 +00006660 goto abort_due_to_error;
danielk1977e6efa742004-11-10 11:55:10 +00006661 }else{
drh856c1032009-06-02 15:21:42 +00006662 iDb = pOp->p3;
drha7ab6d82014-07-21 15:44:39 +00006663 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00006664 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00006665 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00006666 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00006667 pOut->u.i = iMoved;
drh9467abf2016-02-17 18:44:11 +00006668 if( rc ) goto abort_due_to_error;
drh3765df42006-06-28 18:18:09 +00006669#ifndef SQLITE_OMIT_AUTOVACUUM
drh9467abf2016-02-17 18:44:11 +00006670 if( iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00006671 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
6672 /* All OP_Destroy operations occur on the same btree */
6673 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
6674 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00006675 }
drh3765df42006-06-28 18:18:09 +00006676#endif
danielk1977a0bf2652004-11-04 14:30:04 +00006677 }
drh5e00f6c2001-09-13 13:46:56 +00006678 break;
6679}
6680
danielk1977c7af4842008-10-27 13:59:33 +00006681/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00006682**
6683** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00006684** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00006685** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00006686**
drhf57b3392001-10-08 13:22:32 +00006687** The table being clear is in the main database file if P2==0. If
6688** P2==1 then the table to be clear is in the auxiliary database file
6689** that is used to store tables create using CREATE TEMPORARY TABLE.
6690**
drha6df0e62021-06-03 18:51:51 +00006691** If the P3 value is non-zero, then the row change count is incremented
6692** by the number of rows in the table being cleared. If P3 is greater
6693** than zero, then the value stored in register P3 is also incremented
6694** by the number of rows in the table being cleared.
danielk1977c7af4842008-10-27 13:59:33 +00006695**
drhb19a2bc2001-09-16 00:13:26 +00006696** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00006697*/
drh9cbf3422008-01-17 16:22:13 +00006698case OP_Clear: {
dan2c718872021-06-22 18:32:05 +00006699 i64 nChange;
drh856c1032009-06-02 15:21:42 +00006700
drh4031baf2018-05-28 17:31:20 +00006701 sqlite3VdbeIncrWriteCounter(p, 0);
drh856c1032009-06-02 15:21:42 +00006702 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00006703 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00006704 assert( DbMaskTest(p->btreeMask, pOp->p2) );
drha6df0e62021-06-03 18:51:51 +00006705 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange);
danielk1977c7af4842008-10-27 13:59:33 +00006706 if( pOp->p3 ){
6707 p->nChange += nChange;
6708 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00006709 assert( memIsValid(&aMem[pOp->p3]) );
6710 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00006711 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00006712 }
6713 }
drh9467abf2016-02-17 18:44:11 +00006714 if( rc ) goto abort_due_to_error;
drh5edc3122001-09-13 21:53:09 +00006715 break;
6716}
6717
drh65ea12c2014-03-19 17:41:36 +00006718/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00006719**
drh65ea12c2014-03-19 17:41:36 +00006720** Delete all contents from the ephemeral table or sorter
6721** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00006722**
drh65ea12c2014-03-19 17:41:36 +00006723** This opcode only works for cursors used for sorting and
6724** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00006725*/
drh65ea12c2014-03-19 17:41:36 +00006726case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00006727 VdbeCursor *pC;
6728
6729 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6730 pC = p->apCsr[pOp->p1];
6731 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00006732 if( isSorter(pC) ){
6733 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
drh65ea12c2014-03-19 17:41:36 +00006734 }else{
drhc960dcb2015-11-20 19:22:01 +00006735 assert( pC->eCurType==CURTYPE_BTREE );
drh65ea12c2014-03-19 17:41:36 +00006736 assert( pC->isEphemeral );
drhc960dcb2015-11-20 19:22:01 +00006737 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
drh9467abf2016-02-17 18:44:11 +00006738 if( rc ) goto abort_due_to_error;
drh65ea12c2014-03-19 17:41:36 +00006739 }
drh079a3072014-03-19 14:10:55 +00006740 break;
6741}
6742
drh0f3f7662017-08-18 14:34:28 +00006743/* Opcode: CreateBtree P1 P2 P3 * *
6744** Synopsis: r[P2]=root iDb=P1 flags=P3
drh5b2fd562001-09-13 15:21:31 +00006745**
drh0f3f7662017-08-18 14:34:28 +00006746** Allocate a new b-tree in the main database file if P1==0 or in the
6747** TEMP database file if P1==1 or in an attached database if
6748** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
drh416a8012018-05-31 19:14:52 +00006749** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
drh0f3f7662017-08-18 14:34:28 +00006750** The root page number of the new b-tree is stored in register P2.
drh5b2fd562001-09-13 15:21:31 +00006751*/
drh0f3f7662017-08-18 14:34:28 +00006752case OP_CreateBtree: { /* out2 */
drhabc38152020-07-22 13:38:04 +00006753 Pgno pgno;
drh234c39d2004-07-24 03:30:47 +00006754 Db *pDb;
drh856c1032009-06-02 15:21:42 +00006755
drh4031baf2018-05-28 17:31:20 +00006756 sqlite3VdbeIncrWriteCounter(p, 0);
drh27a348c2015-04-13 19:14:06 +00006757 pOut = out2Prerelease(p, pOp);
drh856c1032009-06-02 15:21:42 +00006758 pgno = 0;
drh0f3f7662017-08-18 14:34:28 +00006759 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
drh234c39d2004-07-24 03:30:47 +00006760 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006761 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00006762 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00006763 pDb = &db->aDb[pOp->p1];
6764 assert( pDb->pBt!=0 );
drh0f3f7662017-08-18 14:34:28 +00006765 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
drh9467abf2016-02-17 18:44:11 +00006766 if( rc ) goto abort_due_to_error;
drh88a003e2008-12-11 16:17:03 +00006767 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00006768 break;
6769}
6770
drh4a54bb52017-02-18 15:58:52 +00006771/* Opcode: SqlExec * * * P4 *
6772**
6773** Run the SQL statement or statements specified in the P4 string.
6774*/
6775case OP_SqlExec: {
drh4031baf2018-05-28 17:31:20 +00006776 sqlite3VdbeIncrWriteCounter(p, 0);
drhbce04142017-02-23 00:58:36 +00006777 db->nSqlExec++;
drh4a54bb52017-02-18 15:58:52 +00006778 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
drhbce04142017-02-23 00:58:36 +00006779 db->nSqlExec--;
drh4a54bb52017-02-18 15:58:52 +00006780 if( rc ) goto abort_due_to_error;
6781 break;
6782}
6783
drh22645842011-03-24 01:34:03 +00006784/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00006785**
drh346a70c2020-06-15 20:27:35 +00006786** Read and parse all entries from the schema table of database P1
drh1595abc2018-08-14 19:27:51 +00006787** that match the WHERE clause P4. If P4 is a NULL pointer, then the
6788** entire schema for P1 is reparsed.
drh234c39d2004-07-24 03:30:47 +00006789**
6790** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00006791** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00006792*/
drh9cbf3422008-01-17 16:22:13 +00006793case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00006794 int iDb;
drh067b92b2020-06-19 15:24:12 +00006795 const char *zSchema;
drh856c1032009-06-02 15:21:42 +00006796 char *zSql;
6797 InitData initData;
6798
drhbdaec522011-04-04 00:14:43 +00006799 /* Any prepared statement that invokes this opcode will hold mutexes
6800 ** on every btree. This is a prerequisite for invoking
6801 ** sqlite3InitCallback().
6802 */
6803#ifdef SQLITE_DEBUG
6804 for(iDb=0; iDb<db->nDb; iDb++){
6805 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
6806 }
6807#endif
drhbdaec522011-04-04 00:14:43 +00006808
drh856c1032009-06-02 15:21:42 +00006809 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00006810 assert( iDb>=0 && iDb<db->nDb );
drhfe972342021-06-07 11:50:23 +00006811 assert( DbHasProperty(db, iDb, DB_SchemaLoaded)
6812 || db->mallocFailed
6813 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) );
dane325ffe2018-08-11 13:40:20 +00006814
6815#ifndef SQLITE_OMIT_ALTERTABLE
6816 if( pOp->p4.z==0 ){
6817 sqlite3SchemaClear(db->aDb[iDb].pSchema);
danb0c79202018-08-11 18:34:25 +00006818 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
dan6a5a13d2021-02-17 20:08:22 +00006819 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
dane325ffe2018-08-11 13:40:20 +00006820 db->mDbFlags |= DBFLAG_SchemaChange;
dan0d5fa6b2018-08-24 17:55:49 +00006821 p->expired = 0;
dane325ffe2018-08-11 13:40:20 +00006822 }else
6823#endif
drh1595abc2018-08-14 19:27:51 +00006824 {
drha4a871c2021-11-04 14:04:20 +00006825 zSchema = LEGACY_SCHEMA_TABLE;
danielk1977a8bbef82009-03-23 17:11:26 +00006826 initData.db = db;
mistachkin1c06b472018-09-27 00:04:31 +00006827 initData.iDb = iDb;
danielk1977a8bbef82009-03-23 17:11:26 +00006828 initData.pzErrMsg = &p->zErrMsg;
drh9fd88e82018-09-07 11:08:31 +00006829 initData.mInitFlags = 0;
drh3b3ddba2020-07-22 18:03:56 +00006830 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
danielk1977a8bbef82009-03-23 17:11:26 +00006831 zSql = sqlite3MPrintf(db,
drhc5a93d42019-08-12 00:08:07 +00006832 "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
drh067b92b2020-06-19 15:24:12 +00006833 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
danielk1977a8bbef82009-03-23 17:11:26 +00006834 if( zSql==0 ){
mistachkinfad30392016-02-13 23:43:46 +00006835 rc = SQLITE_NOMEM_BKPT;
danielk1977a8bbef82009-03-23 17:11:26 +00006836 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00006837 assert( db->init.busy==0 );
6838 db->init.busy = 1;
6839 initData.rc = SQLITE_OK;
drh6b86e512019-01-05 21:09:37 +00006840 initData.nInitRow = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00006841 assert( !db->mallocFailed );
6842 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
6843 if( rc==SQLITE_OK ) rc = initData.rc;
drh6b86e512019-01-05 21:09:37 +00006844 if( rc==SQLITE_OK && initData.nInitRow==0 ){
6845 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
6846 ** at least one SQL statement. Any less than that indicates that
drh1e32bed2020-06-19 13:33:53 +00006847 ** the sqlite_schema table is corrupt. */
drh6b86e512019-01-05 21:09:37 +00006848 rc = SQLITE_CORRUPT_BKPT;
6849 }
drhdbd6a7d2017-04-05 12:39:49 +00006850 sqlite3DbFreeNN(db, zSql);
danielk1977a8bbef82009-03-23 17:11:26 +00006851 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00006852 }
drh3c23a882007-01-09 14:01:13 +00006853 }
drh9467abf2016-02-17 18:44:11 +00006854 if( rc ){
6855 sqlite3ResetAllSchemasOfConnection(db);
6856 if( rc==SQLITE_NOMEM ){
6857 goto no_mem;
6858 }
6859 goto abort_due_to_error;
danielk1977261919c2005-12-06 12:52:59 +00006860 }
drh234c39d2004-07-24 03:30:47 +00006861 break;
6862}
6863
drh8bfdf722009-06-19 14:06:03 +00006864#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00006865/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00006866**
6867** Read the sqlite_stat1 table for database P1 and load the content
6868** of that table into the internal index hash table. This will cause
6869** the analysis to be used when preparing all subsequent queries.
6870*/
drh9cbf3422008-01-17 16:22:13 +00006871case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00006872 assert( pOp->p1>=0 && pOp->p1<db->nDb );
6873 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00006874 if( rc ) goto abort_due_to_error;
drh497e4462005-07-23 03:18:40 +00006875 break;
6876}
drh8bfdf722009-06-19 14:06:03 +00006877#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00006878
drh98757152008-01-09 23:04:12 +00006879/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006880**
6881** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006882** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00006883** is dropped from disk (using the Destroy opcode) in order to keep
6884** the internal representation of the
drh956bc922004-07-24 17:38:29 +00006885** schema consistent with what is on disk.
6886*/
drh9cbf3422008-01-17 16:22:13 +00006887case OP_DropTable: {
drh4031baf2018-05-28 17:31:20 +00006888 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006889 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006890 break;
6891}
6892
drh98757152008-01-09 23:04:12 +00006893/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006894**
6895** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006896** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00006897** is dropped from disk (using the Destroy opcode)
6898** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00006899** schema consistent with what is on disk.
6900*/
drh9cbf3422008-01-17 16:22:13 +00006901case OP_DropIndex: {
drh4031baf2018-05-28 17:31:20 +00006902 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006903 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006904 break;
6905}
6906
drh98757152008-01-09 23:04:12 +00006907/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00006908**
6909** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00006910** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00006911** is dropped from disk (using the Destroy opcode) in order to keep
6912** the internal representation of the
drh956bc922004-07-24 17:38:29 +00006913** schema consistent with what is on disk.
6914*/
drh9cbf3422008-01-17 16:22:13 +00006915case OP_DropTrigger: {
drh4031baf2018-05-28 17:31:20 +00006916 sqlite3VdbeIncrWriteCounter(p, 0);
danielk19772dca4ac2008-01-03 11:50:29 +00006917 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00006918 break;
6919}
6920
drh234c39d2004-07-24 03:30:47 +00006921
drhb7f91642004-10-31 02:22:47 +00006922#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98968b22016-03-15 22:00:39 +00006923/* Opcode: IntegrityCk P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00006924**
drh98757152008-01-09 23:04:12 +00006925** Do an analysis of the currently open database. Store in
6926** register P1 the text of an error message describing any problems.
6927** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00006928**
drh66accfc2017-02-22 18:04:42 +00006929** The register P3 contains one less than the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00006930** At most reg(P3) errors will be reported.
6931** In other words, the analysis stops as soon as reg(P1) errors are
6932** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00006933**
drh98968b22016-03-15 22:00:39 +00006934** The root page numbers of all tables in the database are integers
6935** stored in P4_INTARRAY argument.
drh21504322002-06-25 13:16:02 +00006936**
drh98757152008-01-09 23:04:12 +00006937** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00006938** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00006939**
drh1dcdbc02007-01-27 02:24:54 +00006940** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00006941*/
drhaaab5722002-02-19 13:39:21 +00006942case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00006943 int nRoot; /* Number of tables to check. (Number of root pages.) */
drhabc38152020-07-22 13:38:04 +00006944 Pgno *aRoot; /* Array of rootpage numbers for tables to be checked */
drh98757152008-01-09 23:04:12 +00006945 int nErr; /* Number of errors reported */
6946 char *z; /* Text of the error report */
6947 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00006948
drh1713afb2013-06-28 01:24:57 +00006949 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00006950 nRoot = pOp->p2;
drh98968b22016-03-15 22:00:39 +00006951 aRoot = pOp->p4.ai;
drh79069752004-05-22 21:30:40 +00006952 assert( nRoot>0 );
mistachkincec5f1d2020-08-04 16:11:37 +00006953 assert( aRoot[0]==(Pgno)nRoot );
drh9f6168b2016-03-19 23:32:58 +00006954 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006955 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00006956 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00006957 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00006958 pIn1 = &aMem[pOp->p1];
drh98757152008-01-09 23:04:12 +00006959 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006960 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drh21f6daa2019-10-11 14:21:48 +00006961 z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
drh66accfc2017-02-22 18:04:42 +00006962 (int)pnErr->u.i+1, &nErr);
drha05a7222008-01-19 03:35:58 +00006963 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00006964 if( nErr==0 ){
6965 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00006966 }else if( z==0 ){
6967 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00006968 }else{
drh66accfc2017-02-22 18:04:42 +00006969 pnErr->u.i -= nErr-1;
danielk1977a7a8e142008-02-13 18:25:27 +00006970 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00006971 }
drhb7654112008-01-12 12:48:07 +00006972 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00006973 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh21f6daa2019-10-11 14:21:48 +00006974 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00006975}
drhb7f91642004-10-31 02:22:47 +00006976#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00006977
drh3d4501e2008-12-04 20:40:10 +00006978/* Opcode: RowSetAdd P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00006979** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00006980**
drhbb6783b2017-04-29 18:02:49 +00006981** Insert the integer value held by register P2 into a RowSet object
drh3d4501e2008-12-04 20:40:10 +00006982** held in register P1.
6983**
6984** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00006985*/
drh93952eb2009-11-13 19:43:43 +00006986case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00006987 pIn1 = &aMem[pOp->p1];
6988 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00006989 assert( (pIn2->flags & MEM_Int)!=0 );
drh9d67afc2018-08-29 20:24:03 +00006990 if( (pIn1->flags & MEM_Blob)==0 ){
6991 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00006992 }
drh9d67afc2018-08-29 20:24:03 +00006993 assert( sqlite3VdbeMemIsRowSet(pIn1) );
6994 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00006995 break;
6996}
6997
6998/* Opcode: RowSetRead P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00006999** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00007000**
drhbb6783b2017-04-29 18:02:49 +00007001** Extract the smallest value from the RowSet object in P1
7002** and put that value into register P3.
7003** Or, if RowSet object P1 is initially empty, leave P3
drh3d4501e2008-12-04 20:40:10 +00007004** unchanged and jump to instruction P2.
7005*/
drh93952eb2009-11-13 19:43:43 +00007006case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00007007 i64 val;
drh49afe3a2013-07-10 03:05:14 +00007008
drh3c657212009-11-17 23:59:58 +00007009 pIn1 = &aMem[pOp->p1];
drh9d67afc2018-08-29 20:24:03 +00007010 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
7011 if( (pIn1->flags & MEM_Blob)==0
7012 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
drh3d4501e2008-12-04 20:40:10 +00007013 ){
7014 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00007015 sqlite3VdbeMemSetNull(pIn1);
drh688852a2014-02-17 22:40:43 +00007016 VdbeBranchTaken(1,2);
drhf56fa462015-04-13 21:39:54 +00007017 goto jump_to_p2_and_check_for_interrupt;
drh3d4501e2008-12-04 20:40:10 +00007018 }else{
7019 /* A value was pulled from the index */
drh688852a2014-02-17 22:40:43 +00007020 VdbeBranchTaken(0,2);
drhf56fa462015-04-13 21:39:54 +00007021 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00007022 }
drh49afe3a2013-07-10 03:05:14 +00007023 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00007024}
7025
drh1b26c7c2009-04-22 02:15:47 +00007026/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00007027** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00007028**
drhade97602009-04-21 15:05:18 +00007029** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00007030** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00007031** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00007032** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00007033** next opcode.
danielk19771d461462009-04-21 09:02:45 +00007034**
drhbb6783b2017-04-29 18:02:49 +00007035** The RowSet object is optimized for the case where sets of integers
7036** are inserted in distinct phases, which each set contains no duplicates.
7037** Each set is identified by a unique P4 value. The first set
7038** must have P4==0, the final set must have P4==-1, and for all other sets
7039** must have P4>0.
danielk19771d461462009-04-21 09:02:45 +00007040**
7041** This allows optimizations: (a) when P4==0 there is no need to test
drhbb6783b2017-04-29 18:02:49 +00007042** the RowSet object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00007043** (b) when P4==-1 there is no need to insert the value, as it will
7044** never be tested for, and (c) when a value that is part of set X is
7045** inserted, there is no need to search to see if the same value was
7046** previously inserted as part of set X (only if it was previously
7047** inserted as part of some other set).
7048*/
drh1b26c7c2009-04-22 02:15:47 +00007049case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00007050 int iSet;
7051 int exists;
7052
drh3c657212009-11-17 23:59:58 +00007053 pIn1 = &aMem[pOp->p1];
7054 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00007055 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00007056 assert( pIn3->flags&MEM_Int );
7057
drh1b26c7c2009-04-22 02:15:47 +00007058 /* If there is anything other than a rowset object in memory cell P1,
7059 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00007060 */
drh9d67afc2018-08-29 20:24:03 +00007061 if( (pIn1->flags & MEM_Blob)==0 ){
7062 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00007063 }
drh9d67afc2018-08-29 20:24:03 +00007064 assert( sqlite3VdbeMemIsRowSet(pIn1) );
danielk19771d461462009-04-21 09:02:45 +00007065 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00007066 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00007067 if( iSet ){
drh9d67afc2018-08-29 20:24:03 +00007068 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00007069 VdbeBranchTaken(exists!=0,2);
drhf56fa462015-04-13 21:39:54 +00007070 if( exists ) goto jump_to_p2;
danielk19771d461462009-04-21 09:02:45 +00007071 }
7072 if( iSet>=0 ){
drh9d67afc2018-08-29 20:24:03 +00007073 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00007074 }
7075 break;
7076}
7077
drh5e00f6c2001-09-13 13:46:56 +00007078
danielk197793758c82005-01-21 08:13:14 +00007079#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00007080
drh0fd61352014-02-07 02:29:45 +00007081/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00007082**
dan76d462e2009-08-30 11:42:51 +00007083** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00007084**
dan76d462e2009-08-30 11:42:51 +00007085** P1 contains the address of the memory cell that contains the first memory
7086** cell in an array of values used as arguments to the sub-program. P2
7087** contains the address to jump to if the sub-program throws an IGNORE
7088** exception using the RAISE() function. Register P3 contains the address
7089** of a memory cell in this (the parent) VM that is used to allocate the
7090** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00007091**
7092** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00007093**
7094** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00007095*/
dan76d462e2009-08-30 11:42:51 +00007096case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00007097 int nMem; /* Number of memory registers for sub-program */
7098 int nByte; /* Bytes of runtime space required for sub-program */
7099 Mem *pRt; /* Register to allocate runtime space */
7100 Mem *pMem; /* Used to iterate through memory cells */
7101 Mem *pEnd; /* Last memory cell in new array */
7102 VdbeFrame *pFrame; /* New vdbe frame to execute in */
7103 SubProgram *pProgram; /* Sub-program to execute */
7104 void *t; /* Token identifying trigger */
7105
7106 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00007107 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00007108 assert( pProgram->nOp>0 );
7109
dan1da40a32009-09-19 17:00:31 +00007110 /* If the p5 flag is clear, then recursive invocation of triggers is
7111 ** disabled for backwards compatibility (p5 is set if this sub-program
7112 ** is really a trigger, not a foreign key action, and the flag set
7113 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00007114 **
7115 ** It is recursive invocation of triggers, at the SQL level, that is
7116 ** disabled. In some cases a single trigger may generate more than one
7117 ** SubProgram (if the trigger may be executed with more than one different
7118 ** ON CONFLICT algorithm). SubProgram structures associated with a
7119 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00007120 ** variable. */
7121 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00007122 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00007123 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
7124 if( pFrame ) break;
7125 }
7126
danf5894502009-10-07 18:41:19 +00007127 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00007128 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00007129 sqlite3VdbeError(p, "too many levels of trigger recursion");
drh9467abf2016-02-17 18:44:11 +00007130 goto abort_due_to_error;
dan165921a2009-08-28 18:53:45 +00007131 }
7132
7133 /* Register pRt is used to store the memory required to save the state
7134 ** of the current program, and the memory required at runtime to execute
7135 ** the trigger program. If this trigger has been fired before, then pRt
7136 ** is already allocated. Otherwise, it must be initialized. */
drh72f56ef2018-08-29 18:47:22 +00007137 if( (pRt->flags&MEM_Blob)==0 ){
dan165921a2009-08-28 18:53:45 +00007138 /* SubProgram.nMem is set to the number of memory cells used by the
7139 ** program stored in SubProgram.aOp. As well as these, one memory
7140 ** cell is required for each cursor used by the program. Set local
7141 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
7142 */
dan65a7cd12009-09-01 12:16:01 +00007143 nMem = pProgram->nMem + pProgram->nCsr;
drh3cdce922016-03-21 00:30:40 +00007144 assert( nMem>0 );
7145 if( pProgram->nCsr==0 ) nMem++;
dan65a7cd12009-09-01 12:16:01 +00007146 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00007147 + nMem * sizeof(Mem)
drhab087d42017-03-24 17:59:56 +00007148 + pProgram->nCsr * sizeof(VdbeCursor*)
7149 + (pProgram->nOp + 7)/8;
dan165921a2009-08-28 18:53:45 +00007150 pFrame = sqlite3DbMallocZero(db, nByte);
7151 if( !pFrame ){
7152 goto no_mem;
7153 }
7154 sqlite3VdbeMemRelease(pRt);
drh72f56ef2018-08-29 18:47:22 +00007155 pRt->flags = MEM_Blob|MEM_Dyn;
7156 pRt->z = (char*)pFrame;
7157 pRt->n = nByte;
7158 pRt->xDel = sqlite3VdbeFrameMemDel;
dan165921a2009-08-28 18:53:45 +00007159
7160 pFrame->v = p;
7161 pFrame->nChildMem = nMem;
7162 pFrame->nChildCsr = pProgram->nCsr;
drhf56fa462015-04-13 21:39:54 +00007163 pFrame->pc = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +00007164 pFrame->aMem = p->aMem;
7165 pFrame->nMem = p->nMem;
7166 pFrame->apCsr = p->apCsr;
7167 pFrame->nCursor = p->nCursor;
7168 pFrame->aOp = p->aOp;
7169 pFrame->nOp = p->nOp;
7170 pFrame->token = pProgram->token;
drh72f56ef2018-08-29 18:47:22 +00007171#ifdef SQLITE_DEBUG
7172 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
7173#endif
dan165921a2009-08-28 18:53:45 +00007174
7175 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
7176 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00007177 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00007178 pMem->db = db;
7179 }
7180 }else{
drh72f56ef2018-08-29 18:47:22 +00007181 pFrame = (VdbeFrame*)pRt->z;
7182 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
drh9f6168b2016-03-19 23:32:58 +00007183 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
7184 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
dan165921a2009-08-28 18:53:45 +00007185 assert( pProgram->nCsr==pFrame->nChildCsr );
drhf56fa462015-04-13 21:39:54 +00007186 assert( (int)(pOp - aOp)==pFrame->pc );
dan165921a2009-08-28 18:53:45 +00007187 }
7188
7189 p->nFrame++;
7190 pFrame->pParent = p->pFrame;
drhfae58d52017-01-26 17:26:44 +00007191 pFrame->lastRowid = db->lastRowid;
dan76d462e2009-08-30 11:42:51 +00007192 pFrame->nChange = p->nChange;
danc3da6672014-10-28 18:24:16 +00007193 pFrame->nDbChange = p->db->nChange;
dan32001322016-02-19 18:54:29 +00007194 assert( pFrame->pAuxData==0 );
7195 pFrame->pAuxData = p->pAuxData;
7196 p->pAuxData = 0;
dan2832ad42009-08-31 15:27:27 +00007197 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00007198 p->pFrame = pFrame;
drh9f6168b2016-03-19 23:32:58 +00007199 p->aMem = aMem = VdbeFrameMem(pFrame);
dan165921a2009-08-28 18:53:45 +00007200 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00007201 p->nCursor = (u16)pFrame->nChildCsr;
drh9f6168b2016-03-19 23:32:58 +00007202 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
drhab087d42017-03-24 17:59:56 +00007203 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
drh18333ef2017-03-24 18:38:41 +00007204 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
drhbbe879d2009-11-14 18:04:35 +00007205 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00007206 p->nOp = pProgram->nOp;
drhb2e61bc2019-01-25 19:29:01 +00007207#ifdef SQLITE_DEBUG
7208 /* Verify that second and subsequent executions of the same trigger do not
7209 ** try to reuse register values from the first use. */
7210 {
7211 int i;
7212 for(i=0; i<p->nMem; i++){
7213 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
drhf5cfe6f2020-03-03 20:48:12 +00007214 MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */
drhb2e61bc2019-01-25 19:29:01 +00007215 }
7216 }
7217#endif
drhf56fa462015-04-13 21:39:54 +00007218 pOp = &aOp[-1];
drhb1af9c62019-02-20 13:55:45 +00007219 goto check_for_interrupt;
dan165921a2009-08-28 18:53:45 +00007220}
7221
dan76d462e2009-08-30 11:42:51 +00007222/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00007223**
dan76d462e2009-08-30 11:42:51 +00007224** This opcode is only ever present in sub-programs called via the
7225** OP_Program instruction. Copy a value currently stored in a memory
7226** cell of the calling (parent) frame to cell P2 in the current frames
7227** address space. This is used by trigger programs to access the new.*
7228** and old.* values.
dan165921a2009-08-28 18:53:45 +00007229**
dan76d462e2009-08-30 11:42:51 +00007230** The address of the cell in the parent frame is determined by adding
7231** the value of the P1 argument to the value of the P1 argument to the
7232** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00007233*/
drh27a348c2015-04-13 19:14:06 +00007234case OP_Param: { /* out2 */
dan65a7cd12009-09-01 12:16:01 +00007235 VdbeFrame *pFrame;
7236 Mem *pIn;
drh27a348c2015-04-13 19:14:06 +00007237 pOut = out2Prerelease(p, pOp);
dan65a7cd12009-09-01 12:16:01 +00007238 pFrame = p->pFrame;
7239 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00007240 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
7241 break;
7242}
7243
danielk197793758c82005-01-21 08:13:14 +00007244#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00007245
dan1da40a32009-09-19 17:00:31 +00007246#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00007247/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00007248** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00007249**
dan0ff297e2009-09-25 17:03:14 +00007250** Increment a "constraint counter" by P2 (P2 may be negative or positive).
7251** If P1 is non-zero, the database constraint counter is incremented
7252** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00007253** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00007254*/
dan32b09f22009-09-23 17:29:59 +00007255case OP_FkCounter: {
drh963c74d2013-07-11 12:19:12 +00007256 if( db->flags & SQLITE_DeferFKs ){
dancb3e4b72013-07-03 19:53:05 +00007257 db->nDeferredImmCons += pOp->p2;
7258 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00007259 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00007260 }else{
dan0ff297e2009-09-25 17:03:14 +00007261 p->nFkConstraint += pOp->p2;
7262 }
7263 break;
7264}
7265
7266/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00007267** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00007268**
7269** This opcode tests if a foreign key constraint-counter is currently zero.
7270** If so, jump to instruction P2. Otherwise, fall through to the next
7271** instruction.
7272**
7273** If P1 is non-zero, then the jump is taken if the database constraint-counter
7274** is zero (the one that counts deferred constraint violations). If P1 is
7275** zero, the jump is taken if the statement constraint-counter is zero
7276** (immediate foreign key constraint violations).
7277*/
7278case OP_FkIfZero: { /* jump */
7279 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00007280 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00007281 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan0ff297e2009-09-25 17:03:14 +00007282 }else{
drh688852a2014-02-17 22:40:43 +00007283 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00007284 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan32b09f22009-09-23 17:29:59 +00007285 }
dan1da40a32009-09-19 17:00:31 +00007286 break;
7287}
7288#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
7289
drh205f48e2004-11-05 00:43:11 +00007290#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00007291/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00007292** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00007293**
dan76d462e2009-08-30 11:42:51 +00007294** P1 is a register in the root frame of this VM (the root frame is
7295** different from the current frame if this instruction is being executed
7296** within a sub-program). Set the value of register P1 to the maximum of
7297** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00007298**
7299** This instruction throws an error if the memory cell is not initially
7300** an integer.
7301*/
dan76d462e2009-08-30 11:42:51 +00007302case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00007303 VdbeFrame *pFrame;
7304 if( p->pFrame ){
7305 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
7306 pIn1 = &pFrame->aMem[pOp->p1];
7307 }else{
drha6c2ed92009-11-14 23:22:23 +00007308 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00007309 }
drh2b4ded92010-09-27 21:09:31 +00007310 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00007311 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00007312 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00007313 sqlite3VdbeMemIntegerify(pIn2);
7314 if( pIn1->u.i<pIn2->u.i){
7315 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00007316 }
7317 break;
7318}
7319#endif /* SQLITE_OMIT_AUTOINCREMENT */
7320
drh8b0cf382015-10-06 21:07:06 +00007321/* Opcode: IfPos P1 P2 P3 * *
7322** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00007323**
drh16897072015-03-07 00:57:37 +00007324** Register P1 must contain an integer.
mistachkin91a3ecb2015-10-06 21:49:55 +00007325** If the value of register P1 is 1 or greater, subtract P3 from the
drh8b0cf382015-10-06 21:07:06 +00007326** value in P1 and jump to P2.
drh6f58f702006-01-08 05:26:41 +00007327**
drh16897072015-03-07 00:57:37 +00007328** If the initial value of register P1 is less than 1, then the
7329** value is unchanged and control passes through to the next instruction.
danielk1977a2dc3b12005-02-05 12:48:48 +00007330*/
drh9cbf3422008-01-17 16:22:13 +00007331case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00007332 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00007333 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00007334 VdbeBranchTaken( pIn1->u.i>0, 2);
drh8b0cf382015-10-06 21:07:06 +00007335 if( pIn1->u.i>0 ){
7336 pIn1->u.i -= pOp->p3;
7337 goto jump_to_p2;
7338 }
drhec7429a2005-10-06 16:53:14 +00007339 break;
7340}
7341
drhcc2fa4c2016-01-25 15:57:29 +00007342/* Opcode: OffsetLimit P1 P2 P3 * *
7343** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
drh15007a92006-01-08 18:10:17 +00007344**
drhcc2fa4c2016-01-25 15:57:29 +00007345** This opcode performs a commonly used computation associated with
drhe24a6f52022-08-04 14:02:35 +00007346** LIMIT and OFFSET processing. r[P1] holds the limit counter. r[P3]
drhcc2fa4c2016-01-25 15:57:29 +00007347** holds the offset counter. The opcode computes the combined value
7348** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
7349** value computed is the total number of rows that will need to be
7350** visited in order to complete the query.
7351**
7352** If r[P3] is zero or negative, that means there is no OFFSET
7353** and r[P2] is set to be the value of the LIMIT, r[P1].
7354**
7355** if r[P1] is zero or negative, that means there is no LIMIT
7356** and r[P2] is set to -1.
7357**
7358** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
drh15007a92006-01-08 18:10:17 +00007359*/
drhcc2fa4c2016-01-25 15:57:29 +00007360case OP_OffsetLimit: { /* in1, out2, in3 */
drh719da302016-12-10 04:06:49 +00007361 i64 x;
drh3c657212009-11-17 23:59:58 +00007362 pIn1 = &aMem[pOp->p1];
drhcc2fa4c2016-01-25 15:57:29 +00007363 pIn3 = &aMem[pOp->p3];
7364 pOut = out2Prerelease(p, pOp);
7365 assert( pIn1->flags & MEM_Int );
7366 assert( pIn3->flags & MEM_Int );
drh719da302016-12-10 04:06:49 +00007367 x = pIn1->u.i;
7368 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
7369 /* If the LIMIT is less than or equal to zero, loop forever. This
7370 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
7371 ** also loop forever. This is undocumented. In fact, one could argue
7372 ** that the loop should terminate. But assuming 1 billion iterations
7373 ** per second (far exceeding the capabilities of any current hardware)
7374 ** it would take nearly 300 years to actually reach the limit. So
7375 ** looping forever is a reasonable approximation. */
7376 pOut->u.i = -1;
7377 }else{
7378 pOut->u.i = x;
7379 }
drh15007a92006-01-08 18:10:17 +00007380 break;
7381}
7382
drhf99dd352016-12-18 17:42:00 +00007383/* Opcode: IfNotZero P1 P2 * * *
7384** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
drhec7429a2005-10-06 16:53:14 +00007385**
drh16897072015-03-07 00:57:37 +00007386** Register P1 must contain an integer. If the content of register P1 is
drhf99dd352016-12-18 17:42:00 +00007387** initially greater than zero, then decrement the value in register P1.
7388** If it is non-zero (negative or positive) and then also jump to P2.
7389** If register P1 is initially zero, leave it unchanged and fall through.
drhec7429a2005-10-06 16:53:14 +00007390*/
drh16897072015-03-07 00:57:37 +00007391case OP_IfNotZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00007392 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00007393 assert( pIn1->flags&MEM_Int );
drh16897072015-03-07 00:57:37 +00007394 VdbeBranchTaken(pIn1->u.i<0, 2);
7395 if( pIn1->u.i ){
drhf99dd352016-12-18 17:42:00 +00007396 if( pIn1->u.i>0 ) pIn1->u.i--;
drhf56fa462015-04-13 21:39:54 +00007397 goto jump_to_p2;
drh16897072015-03-07 00:57:37 +00007398 }
7399 break;
7400}
7401
7402/* Opcode: DecrJumpZero P1 P2 * * *
7403** Synopsis: if (--r[P1])==0 goto P2
7404**
drhab5be2e2016-11-30 05:08:59 +00007405** Register P1 must hold an integer. Decrement the value in P1
7406** and jump to P2 if the new value is exactly zero.
drh16897072015-03-07 00:57:37 +00007407*/
7408case OP_DecrJumpZero: { /* jump, in1 */
7409 pIn1 = &aMem[pOp->p1];
7410 assert( pIn1->flags&MEM_Int );
drhab5be2e2016-11-30 05:08:59 +00007411 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
7412 VdbeBranchTaken(pIn1->u.i==0, 2);
7413 if( pIn1->u.i==0 ) goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00007414 break;
7415}
7416
drh16897072015-03-07 00:57:37 +00007417
drh8f26da62018-07-05 21:22:57 +00007418/* Opcode: AggStep * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00007419** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00007420**
drh8f26da62018-07-05 21:22:57 +00007421** Execute the xStep function for an aggregate.
7422** The function has P5 arguments. P4 is a pointer to the
dan9a947222018-06-14 19:06:36 +00007423** FuncDef structure that specifies the function. Register P3 is the
drhe2d9e7c2015-06-26 18:47:53 +00007424** accumulator.
drhe5095352002-02-24 03:25:14 +00007425**
drh98757152008-01-09 23:04:12 +00007426** The P5 arguments are taken from register P2 and its
7427** successors.
drhe5095352002-02-24 03:25:14 +00007428*/
drh8f26da62018-07-05 21:22:57 +00007429/* Opcode: AggInverse * P2 P3 P4 P5
7430** Synopsis: accum=r[P3] inverse(r[P2@P5])
7431**
7432** Execute the xInverse function for an aggregate.
7433** The function has P5 arguments. P4 is a pointer to the
7434** FuncDef structure that specifies the function. Register P3 is the
7435** accumulator.
7436**
7437** The P5 arguments are taken from register P2 and its
7438** successors.
7439*/
7440/* Opcode: AggStep1 P1 P2 P3 P4 P5
drhe2d9e7c2015-06-26 18:47:53 +00007441** Synopsis: accum=r[P3] step(r[P2@P5])
7442**
dan9a947222018-06-14 19:06:36 +00007443** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
7444** aggregate. The function has P5 arguments. P4 is a pointer to the
7445** FuncDef structure that specifies the function. Register P3 is the
7446** accumulator.
drhe2d9e7c2015-06-26 18:47:53 +00007447**
7448** The P5 arguments are taken from register P2 and its
7449** successors.
7450**
7451** This opcode is initially coded as OP_AggStep0. On first evaluation,
7452** the FuncDef stored in P4 is converted into an sqlite3_context and
7453** the opcode is changed. In this way, the initialization of the
7454** sqlite3_context only happens once, instead of on each call to the
7455** step function.
7456*/
drh8f26da62018-07-05 21:22:57 +00007457case OP_AggInverse:
7458case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00007459 int n;
drh9c7c9132015-06-26 18:16:52 +00007460 sqlite3_context *pCtx;
drhe5095352002-02-24 03:25:14 +00007461
drh9c7c9132015-06-26 18:16:52 +00007462 assert( pOp->p4type==P4_FUNCDEF );
drh856c1032009-06-02 15:21:42 +00007463 n = pOp->p5;
drh9f6168b2016-03-19 23:32:58 +00007464 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
7465 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
drh9c7c9132015-06-26 18:16:52 +00007466 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drhf09ac0b2018-01-23 03:44:06 +00007467 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
7468 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
drh9c7c9132015-06-26 18:16:52 +00007469 if( pCtx==0 ) goto no_mem;
7470 pCtx->pMem = 0;
drhf09ac0b2018-01-23 03:44:06 +00007471 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
7472 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00007473 pCtx->pFunc = pOp->p4.pFunc;
7474 pCtx->iOp = (int)(pOp - aOp);
7475 pCtx->pVdbe = p;
drhf09ac0b2018-01-23 03:44:06 +00007476 pCtx->skipFlag = 0;
7477 pCtx->isError = 0;
drh659fdb42022-04-01 15:31:58 +00007478 pCtx->enc = encoding;
drh9c7c9132015-06-26 18:16:52 +00007479 pCtx->argc = n;
7480 pOp->p4type = P4_FUNCCTX;
7481 pOp->p4.pCtx = pCtx;
drh2c885d02018-07-07 19:36:04 +00007482
7483 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
drh8f26da62018-07-05 21:22:57 +00007484 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
drh2c885d02018-07-07 19:36:04 +00007485
drh8f26da62018-07-05 21:22:57 +00007486 pOp->opcode = OP_AggStep1;
drh9c7c9132015-06-26 18:16:52 +00007487 /* Fall through into OP_AggStep */
drh08b92082020-08-10 14:18:00 +00007488 /* no break */ deliberate_fall_through
drh9c7c9132015-06-26 18:16:52 +00007489}
drh8f26da62018-07-05 21:22:57 +00007490case OP_AggStep1: {
drh9c7c9132015-06-26 18:16:52 +00007491 int i;
7492 sqlite3_context *pCtx;
7493 Mem *pMem;
drh9c7c9132015-06-26 18:16:52 +00007494
7495 assert( pOp->p4type==P4_FUNCCTX );
7496 pCtx = pOp->p4.pCtx;
7497 pMem = &aMem[pOp->p3];
7498
drh2c885d02018-07-07 19:36:04 +00007499#ifdef SQLITE_DEBUG
7500 if( pOp->p1 ){
7501 /* This is an OP_AggInverse call. Verify that xStep has always
7502 ** been called at least once prior to any xInverse call. */
7503 assert( pMem->uTemp==0x1122e0e3 );
7504 }else{
7505 /* This is an OP_AggStep call. Mark it as such. */
7506 pMem->uTemp = 0x1122e0e3;
7507 }
7508#endif
7509
drh9c7c9132015-06-26 18:16:52 +00007510 /* If this function is inside of a trigger, the register array in aMem[]
7511 ** might change from one evaluation to the next. The next block of code
7512 ** checks to see if the register array has changed, and if so it
7513 ** reinitializes the relavant parts of the sqlite3_context object */
7514 if( pCtx->pMem != pMem ){
7515 pCtx->pMem = pMem;
7516 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
7517 }
7518
7519#ifdef SQLITE_DEBUG
7520 for(i=0; i<pCtx->argc; i++){
7521 assert( memIsValid(pCtx->argv[i]) );
7522 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
7523 }
7524#endif
7525
drhabfcea22005-09-06 20:36:48 +00007526 pMem->n++;
drhf09ac0b2018-01-23 03:44:06 +00007527 assert( pCtx->pOut->flags==MEM_Null );
7528 assert( pCtx->isError==0 );
7529 assert( pCtx->skipFlag==0 );
dan67a9b8e2018-06-22 20:51:35 +00007530#ifndef SQLITE_OMIT_WINDOWFUNC
7531 if( pOp->p1 ){
7532 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
7533 }else
7534#endif
7535 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
7536
drhf09ac0b2018-01-23 03:44:06 +00007537 if( pCtx->isError ){
7538 if( pCtx->isError>0 ){
7539 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
drh9c7c9132015-06-26 18:16:52 +00007540 rc = pCtx->isError;
7541 }
drhf09ac0b2018-01-23 03:44:06 +00007542 if( pCtx->skipFlag ){
7543 assert( pOp[-1].opcode==OP_CollSeq );
7544 i = pOp[-1].p1;
7545 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
7546 pCtx->skipFlag = 0;
7547 }
7548 sqlite3VdbeMemRelease(pCtx->pOut);
7549 pCtx->pOut->flags = MEM_Null;
7550 pCtx->isError = 0;
drh9467abf2016-02-17 18:44:11 +00007551 if( rc ) goto abort_due_to_error;
drh1350b032002-02-27 19:00:20 +00007552 }
drhf09ac0b2018-01-23 03:44:06 +00007553 assert( pCtx->pOut->flags==MEM_Null );
7554 assert( pCtx->skipFlag==0 );
drh5e00f6c2001-09-13 13:46:56 +00007555 break;
7556}
7557
drh8f26da62018-07-05 21:22:57 +00007558/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00007559** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00007560**
dan9a947222018-06-14 19:06:36 +00007561** P1 is the memory location that is the accumulator for an aggregate
drh8f26da62018-07-05 21:22:57 +00007562** or window function. Execute the finalizer function
7563** for an aggregate and store the result in P1.
drha10a34b2005-09-07 22:09:48 +00007564**
7565** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00007566** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00007567** argument is not used by this opcode. It is only there to disambiguate
7568** functions that can take varying numbers of arguments. The
drh8be47a72018-07-05 20:05:29 +00007569** P4 argument is only needed for the case where
drha10a34b2005-09-07 22:09:48 +00007570** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00007571*/
drh8f26da62018-07-05 21:22:57 +00007572/* Opcode: AggValue * P2 P3 P4 *
7573** Synopsis: r[P3]=value N=P2
7574**
7575** Invoke the xValue() function and store the result in register P3.
7576**
7577** P2 is the number of arguments that the step function takes and
7578** P4 is a pointer to the FuncDef for this function. The P2
7579** argument is not used by this opcode. It is only there to disambiguate
7580** functions that can take varying numbers of arguments. The
7581** P4 argument is only needed for the case where
7582** the step function was not previously called.
7583*/
7584case OP_AggValue:
drh9cbf3422008-01-17 16:22:13 +00007585case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00007586 Mem *pMem;
drh9f6168b2016-03-19 23:32:58 +00007587 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh8f26da62018-07-05 21:22:57 +00007588 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
drha6c2ed92009-11-14 23:22:23 +00007589 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00007590 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
dan67a9b8e2018-06-22 20:51:35 +00007591#ifndef SQLITE_OMIT_WINDOWFUNC
dan86fb6e12018-05-16 20:58:07 +00007592 if( pOp->p3 ){
dan108e6b22019-03-18 18:55:35 +00007593 memAboutToChange(p, &aMem[pOp->p3]);
dan86fb6e12018-05-16 20:58:07 +00007594 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
dan660af932018-06-18 16:55:22 +00007595 pMem = &aMem[pOp->p3];
dan67a9b8e2018-06-22 20:51:35 +00007596 }else
7597#endif
drh8f26da62018-07-05 21:22:57 +00007598 {
7599 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
7600 }
dan67a9b8e2018-06-22 20:51:35 +00007601
drh4c8555f2009-06-25 01:47:11 +00007602 if( rc ){
drh22c17b82015-05-15 04:13:15 +00007603 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
drh9467abf2016-02-17 18:44:11 +00007604 goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00007605 }
drh2dca8682008-03-21 17:13:13 +00007606 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00007607 UPDATE_MAX_BLOBSIZE(pMem);
drh5e00f6c2001-09-13 13:46:56 +00007608 break;
7609}
7610
dan5cf53532010-05-01 16:40:20 +00007611#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00007612/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00007613**
7614** Checkpoint database P1. This is a no-op if P1 is not currently in
drha25165f2014-12-04 04:50:59 +00007615** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
7616** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
drh30aa3b92011-02-07 23:56:01 +00007617** SQLITE_BUSY or not, respectively. Write the number of pages in the
7618** WAL after the checkpoint into mem[P3+1] and the number of pages
7619** in the WAL that have been checkpointed after the checkpoint
7620** completes into mem[P3+2]. However on an error, mem[P3+1] and
7621** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00007622*/
7623case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00007624 int i; /* Loop counter */
7625 int aRes[3]; /* Results */
7626 Mem *pMem; /* Write results here */
7627
drh9e92a472013-06-27 17:40:30 +00007628 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00007629 aRes[0] = 0;
7630 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00007631 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
7632 || pOp->p2==SQLITE_CHECKPOINT_FULL
7633 || pOp->p2==SQLITE_CHECKPOINT_RESTART
danf26a1542014-12-02 19:04:54 +00007634 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
dancdc1f042010-11-18 12:11:05 +00007635 );
drh30aa3b92011-02-07 23:56:01 +00007636 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
drh9467abf2016-02-17 18:44:11 +00007637 if( rc ){
7638 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
dancdc1f042010-11-18 12:11:05 +00007639 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00007640 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00007641 }
drh30aa3b92011-02-07 23:56:01 +00007642 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
7643 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
7644 }
dan7c246102010-04-12 19:00:29 +00007645 break;
7646};
dan5cf53532010-05-01 16:40:20 +00007647#endif
drh5e00f6c2001-09-13 13:46:56 +00007648
drhcac29a62010-07-02 19:36:52 +00007649#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00007650/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00007651**
7652** Change the journal mode of database P1 to P3. P3 must be one of the
7653** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
7654** modes (delete, truncate, persist, off and memory), this is a simple
7655** operation. No IO is required.
7656**
7657** If changing into or out of WAL mode the procedure is more complicated.
7658**
7659** Write a string containing the final journal-mode to register P2.
7660*/
drh27a348c2015-04-13 19:14:06 +00007661case OP_JournalMode: { /* out2 */
dane04dc882010-04-20 18:53:15 +00007662 Btree *pBt; /* Btree to change journal mode of */
7663 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00007664 int eNew; /* New journal mode */
7665 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00007666#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00007667 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00007668#endif
dane04dc882010-04-20 18:53:15 +00007669
drh27a348c2015-04-13 19:14:06 +00007670 pOut = out2Prerelease(p, pOp);
drhd80b2332010-05-01 00:59:37 +00007671 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00007672 assert( eNew==PAGER_JOURNALMODE_DELETE
7673 || eNew==PAGER_JOURNALMODE_TRUNCATE
7674 || eNew==PAGER_JOURNALMODE_PERSIST
7675 || eNew==PAGER_JOURNALMODE_OFF
7676 || eNew==PAGER_JOURNALMODE_MEMORY
7677 || eNew==PAGER_JOURNALMODE_WAL
7678 || eNew==PAGER_JOURNALMODE_QUERY
7679 );
7680 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00007681 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00007682
dane04dc882010-04-20 18:53:15 +00007683 pBt = db->aDb[pOp->p1].pBt;
7684 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00007685 eOld = sqlite3PagerGetJournalMode(pPager);
7686 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
dana8f249f2021-05-20 11:42:51 +00007687 assert( sqlite3BtreeHoldsMutex(pBt) );
drh0b9b4302010-06-11 17:01:24 +00007688 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00007689
7690#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00007691 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00007692
drhd80b2332010-05-01 00:59:37 +00007693 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00007694 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00007695 */
7696 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00007697 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00007698 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00007699 ){
drh0b9b4302010-06-11 17:01:24 +00007700 eNew = eOld;
dane180c292010-04-26 17:42:56 +00007701 }
7702
drh0b9b4302010-06-11 17:01:24 +00007703 if( (eNew!=eOld)
7704 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
7705 ){
danc0537fe2013-06-28 19:41:43 +00007706 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00007707 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00007708 sqlite3VdbeError(p,
drh0b9b4302010-06-11 17:01:24 +00007709 "cannot change %s wal mode from within a transaction",
7710 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
7711 );
drh9467abf2016-02-17 18:44:11 +00007712 goto abort_due_to_error;
drh0b9b4302010-06-11 17:01:24 +00007713 }else{
7714
7715 if( eOld==PAGER_JOURNALMODE_WAL ){
7716 /* If leaving WAL mode, close the log file. If successful, the call
7717 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
7718 ** file. An EXCLUSIVE lock may still be held on the database file
7719 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00007720 */
dan7fb89902016-08-12 16:21:15 +00007721 rc = sqlite3PagerCloseWal(pPager, db);
drhab9b7442010-05-10 11:20:05 +00007722 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00007723 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00007724 }
drh242c4f72010-06-22 14:49:39 +00007725 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
7726 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
7727 ** as an intermediate */
7728 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00007729 }
7730
7731 /* Open a transaction on the database file. Regardless of the journal
7732 ** mode, this transaction always uses a rollback journal.
7733 */
drh99744fa2020-08-25 19:09:07 +00007734 assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE );
drh0b9b4302010-06-11 17:01:24 +00007735 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00007736 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00007737 }
7738 }
7739 }
dan5cf53532010-05-01 16:40:20 +00007740#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00007741
drh9467abf2016-02-17 18:44:11 +00007742 if( rc ) eNew = eOld;
drh0b9b4302010-06-11 17:01:24 +00007743 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00007744
dane04dc882010-04-20 18:53:15 +00007745 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00007746 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00007747 pOut->n = sqlite3Strlen30(pOut->z);
7748 pOut->enc = SQLITE_UTF8;
7749 sqlite3VdbeChangeEncoding(pOut, encoding);
drh9467abf2016-02-17 18:44:11 +00007750 if( rc ) goto abort_due_to_error;
dane04dc882010-04-20 18:53:15 +00007751 break;
drhcac29a62010-07-02 19:36:52 +00007752};
7753#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00007754
drhfdbcdee2007-03-27 14:44:50 +00007755#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh2f6239e2018-12-08 00:43:08 +00007756/* Opcode: Vacuum P1 P2 * * *
drh6f8c91c2003-12-07 00:24:35 +00007757**
drh9ef5e772016-08-19 14:20:56 +00007758** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
7759** for an attached database. The "temp" database may not be vacuumed.
drhb0b7db92018-12-07 17:28:28 +00007760**
drh2f6239e2018-12-08 00:43:08 +00007761** If P2 is not zero, then it is a register holding a string which is
7762** the file into which the result of vacuum should be written. When
7763** P2 is zero, the vacuum overwrites the original database.
drh6f8c91c2003-12-07 00:24:35 +00007764*/
drh9cbf3422008-01-17 16:22:13 +00007765case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00007766 assert( p->readOnly==0 );
drh2f6239e2018-12-08 00:43:08 +00007767 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
7768 pOp->p2 ? &aMem[pOp->p2] : 0);
drh9467abf2016-02-17 18:44:11 +00007769 if( rc ) goto abort_due_to_error;
drh6f8c91c2003-12-07 00:24:35 +00007770 break;
7771}
drh154d4b22006-09-21 11:02:16 +00007772#endif
drh6f8c91c2003-12-07 00:24:35 +00007773
danielk1977dddbcdc2007-04-26 14:42:34 +00007774#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00007775/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00007776**
7777** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00007778** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00007779** P2. Otherwise, fall through to the next instruction.
7780*/
drh9cbf3422008-01-17 16:22:13 +00007781case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00007782 Btree *pBt;
7783
7784 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00007785 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00007786 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00007787 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00007788 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00007789 VdbeBranchTaken(rc==SQLITE_DONE,2);
drh9467abf2016-02-17 18:44:11 +00007790 if( rc ){
7791 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
danielk1977dddbcdc2007-04-26 14:42:34 +00007792 rc = SQLITE_OK;
drhf56fa462015-04-13 21:39:54 +00007793 goto jump_to_p2;
danielk1977dddbcdc2007-04-26 14:42:34 +00007794 }
7795 break;
7796}
7797#endif
7798
drhba968db2018-07-24 22:02:12 +00007799/* Opcode: Expire P1 P2 * * *
danielk1977a21c6b62005-01-24 10:25:59 +00007800**
drh25df48d2014-07-22 14:58:12 +00007801** Cause precompiled statements to expire. When an expired statement
7802** is executed using sqlite3_step() it will either automatically
7803** reprepare itself (if it was originally created using sqlite3_prepare_v2())
7804** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00007805**
7806** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00007807** then only the currently executing statement is expired.
drhba968db2018-07-24 22:02:12 +00007808**
7809** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
7810** then running SQL statements are allowed to continue to run to completion.
7811** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
7812** that might help the statement run faster but which does not affect the
7813** correctness of operation.
danielk1977a21c6b62005-01-24 10:25:59 +00007814*/
drh9cbf3422008-01-17 16:22:13 +00007815case OP_Expire: {
drhba968db2018-07-24 22:02:12 +00007816 assert( pOp->p2==0 || pOp->p2==1 );
danielk1977a21c6b62005-01-24 10:25:59 +00007817 if( !pOp->p1 ){
drhba968db2018-07-24 22:02:12 +00007818 sqlite3ExpirePreparedStatements(db, pOp->p2);
danielk1977a21c6b62005-01-24 10:25:59 +00007819 }else{
drhba968db2018-07-24 22:02:12 +00007820 p->expired = pOp->p2+1;
danielk1977a21c6b62005-01-24 10:25:59 +00007821 }
7822 break;
7823}
7824
drh7b14b652019-12-29 22:08:20 +00007825/* Opcode: CursorLock P1 * * * *
7826**
7827** Lock the btree to which cursor P1 is pointing so that the btree cannot be
7828** written by an other cursor.
7829*/
7830case OP_CursorLock: {
7831 VdbeCursor *pC;
7832 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7833 pC = p->apCsr[pOp->p1];
7834 assert( pC!=0 );
7835 assert( pC->eCurType==CURTYPE_BTREE );
7836 sqlite3BtreeCursorPin(pC->uc.pCursor);
7837 break;
7838}
7839
7840/* Opcode: CursorUnlock P1 * * * *
7841**
7842** Unlock the btree to which cursor P1 is pointing so that it can be
7843** written by other cursors.
7844*/
7845case OP_CursorUnlock: {
7846 VdbeCursor *pC;
7847 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7848 pC = p->apCsr[pOp->p1];
7849 assert( pC!=0 );
7850 assert( pC->eCurType==CURTYPE_BTREE );
7851 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
7852 break;
7853}
7854
danielk1977c00da102006-01-07 13:21:04 +00007855#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00007856/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00007857** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00007858**
7859** Obtain a lock on a particular table. This instruction is only used when
7860** the shared-cache feature is enabled.
7861**
danielk197796d48e92009-06-29 06:00:37 +00007862** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00007863** on which the lock is acquired. A readlock is obtained if P3==0 or
7864** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00007865**
7866** P2 contains the root-page of the table to lock.
7867**
drh66a51672008-01-03 00:01:23 +00007868** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00007869** used to generate an error message if the lock cannot be obtained.
7870*/
drh9cbf3422008-01-17 16:22:13 +00007871case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00007872 u8 isWriteLock = (u8)pOp->p3;
drh169dd922017-06-26 13:57:49 +00007873 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
danielk1977e0d9e6f2009-07-03 16:25:06 +00007874 int p1 = pOp->p1;
7875 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00007876 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00007877 assert( isWriteLock==0 || isWriteLock==1 );
7878 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
drh9467abf2016-02-17 18:44:11 +00007879 if( rc ){
7880 if( (rc&0xFF)==SQLITE_LOCKED ){
7881 const char *z = pOp->p4.z;
7882 sqlite3VdbeError(p, "database table is locked: %s", z);
7883 }
7884 goto abort_due_to_error;
danielk1977e0d9e6f2009-07-03 16:25:06 +00007885 }
danielk1977c00da102006-01-07 13:21:04 +00007886 }
7887 break;
7888}
drhb9bb7c12006-06-11 23:41:55 +00007889#endif /* SQLITE_OMIT_SHARED_CACHE */
7890
7891#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007892/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00007893**
danielk19773e3a84d2008-08-01 17:37:40 +00007894** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
7895** xBegin method for that table.
7896**
7897** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00007898** within a callback to a virtual table xSync() method. If it is, the error
7899** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00007900*/
drh9cbf3422008-01-17 16:22:13 +00007901case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00007902 VTable *pVTab;
7903 pVTab = pOp->p4.pVtab;
7904 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00007905 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
drh9467abf2016-02-17 18:44:11 +00007906 if( rc ) goto abort_due_to_error;
danielk1977f9e7dda2006-06-16 16:08:53 +00007907 break;
7908}
7909#endif /* SQLITE_OMIT_VIRTUALTABLE */
7910
7911#ifndef SQLITE_OMIT_VIRTUALTABLE
dan73779452015-03-19 18:56:17 +00007912/* Opcode: VCreate P1 P2 * * *
danielk1977f9e7dda2006-06-16 16:08:53 +00007913**
dan73779452015-03-19 18:56:17 +00007914** P2 is a register that holds the name of a virtual table in database
7915** P1. Call the xCreate method for that table.
danielk1977f9e7dda2006-06-16 16:08:53 +00007916*/
drh9cbf3422008-01-17 16:22:13 +00007917case OP_VCreate: {
dan73779452015-03-19 18:56:17 +00007918 Mem sMem; /* For storing the record being decoded */
drh47464062015-03-21 12:22:16 +00007919 const char *zTab; /* Name of the virtual table */
7920
dan73779452015-03-19 18:56:17 +00007921 memset(&sMem, 0, sizeof(sMem));
7922 sMem.db = db;
drh47464062015-03-21 12:22:16 +00007923 /* Because P2 is always a static string, it is impossible for the
7924 ** sqlite3VdbeMemCopy() to fail */
7925 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
7926 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
dan73779452015-03-19 18:56:17 +00007927 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
drh47464062015-03-21 12:22:16 +00007928 assert( rc==SQLITE_OK );
7929 zTab = (const char*)sqlite3_value_text(&sMem);
7930 assert( zTab || db->mallocFailed );
7931 if( zTab ){
7932 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
dan73779452015-03-19 18:56:17 +00007933 }
7934 sqlite3VdbeMemRelease(&sMem);
drh9467abf2016-02-17 18:44:11 +00007935 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00007936 break;
7937}
7938#endif /* SQLITE_OMIT_VIRTUALTABLE */
7939
7940#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007941/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00007942**
drh66a51672008-01-03 00:01:23 +00007943** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00007944** of that table.
drhb9bb7c12006-06-11 23:41:55 +00007945*/
drh9cbf3422008-01-17 16:22:13 +00007946case OP_VDestroy: {
drh086723a2015-03-24 12:51:52 +00007947 db->nVDestroy++;
danielk19772dca4ac2008-01-03 11:50:29 +00007948 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
drh086723a2015-03-24 12:51:52 +00007949 db->nVDestroy--;
dan1d4b1642018-12-28 17:45:08 +00007950 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00007951 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00007952 break;
7953}
7954#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00007955
drh9eff6162006-06-12 21:59:13 +00007956#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00007957/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00007958**
drh66a51672008-01-03 00:01:23 +00007959** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00007960** P1 is a cursor number. This opcode opens a cursor to the virtual
7961** table and stores that cursor in P1.
7962*/
dan2adb3092022-12-06 18:48:06 +00007963case OP_VOpen: { /* ncycle */
drh856c1032009-06-02 15:21:42 +00007964 VdbeCursor *pCur;
drhc960dcb2015-11-20 19:22:01 +00007965 sqlite3_vtab_cursor *pVCur;
drh856c1032009-06-02 15:21:42 +00007966 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00007967 const sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007968
drh1713afb2013-06-28 01:24:57 +00007969 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00007970 pCur = 0;
drhc960dcb2015-11-20 19:22:01 +00007971 pVCur = 0;
danielk1977595a5232009-07-24 17:58:53 +00007972 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00007973 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
7974 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00007975 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00007976 }
7977 pModule = pVtab->pModule;
drhc960dcb2015-11-20 19:22:01 +00007978 rc = pModule->xOpen(pVtab, &pVCur);
dan016f7812013-08-21 17:35:48 +00007979 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00007980 if( rc ) goto abort_due_to_error;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007981
drh9467abf2016-02-17 18:44:11 +00007982 /* Initialize sqlite3_vtab_cursor base class */
7983 pVCur->pVtab = pVtab;
7984
7985 /* Initialize vdbe cursor object */
drhb2486682022-01-03 01:43:28 +00007986 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB);
drh9467abf2016-02-17 18:44:11 +00007987 if( pCur ){
7988 pCur->uc.pVCur = pVCur;
7989 pVtab->nRef++;
7990 }else{
7991 assert( db->mallocFailed );
7992 pModule->xClose(pVCur);
7993 goto no_mem;
danielk1977b7a7b9a2006-06-13 10:24:42 +00007994 }
drh9eff6162006-06-12 21:59:13 +00007995 break;
7996}
7997#endif /* SQLITE_OMIT_VIRTUALTABLE */
7998
7999#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fe7e7d2022-02-01 14:58:29 +00008000/* Opcode: VInitIn P1 P2 P3 * *
drh30e314e2022-02-02 14:36:58 +00008001** Synopsis: r[P2]=ValueList(P1,P3)
drh0fe7e7d2022-02-01 14:58:29 +00008002**
drh30e314e2022-02-02 14:36:58 +00008003** Set register P2 to be a pointer to a ValueList object for cursor P1
8004** with cache register P3 and output register P3+1. This ValueList object
8005** can be used as the first argument to sqlite3_vtab_in_first() and
8006** sqlite3_vtab_in_next() to extract all of the values stored in the P1
8007** cursor. Register P3 is used to hold the values returned by
8008** sqlite3_vtab_in_first() and sqlite3_vtab_in_next().
drh0fe7e7d2022-02-01 14:58:29 +00008009*/
dan2adb3092022-12-06 18:48:06 +00008010case OP_VInitIn: { /* out2, ncycle */
drh30e314e2022-02-02 14:36:58 +00008011 VdbeCursor *pC; /* The cursor containing the RHS values */
8012 ValueList *pRhs; /* New ValueList object to put in reg[P2] */
8013
drh0fe7e7d2022-02-01 14:58:29 +00008014 pC = p->apCsr[pOp->p1];
drh30e314e2022-02-02 14:36:58 +00008015 pRhs = sqlite3_malloc64( sizeof(*pRhs) );
8016 if( pRhs==0 ) goto no_mem;
8017 pRhs->pCsr = pC->uc.pCursor;
8018 pRhs->pOut = &aMem[pOp->p3];
drh0fe7e7d2022-02-01 14:58:29 +00008019 pOut = out2Prerelease(p, pOp);
drh0fe7e7d2022-02-01 14:58:29 +00008020 pOut->flags = MEM_Null;
drh30e314e2022-02-02 14:36:58 +00008021 sqlite3VdbeMemSetPointer(pOut, pRhs, "ValueList", sqlite3_free);
drh0fe7e7d2022-02-01 14:58:29 +00008022 break;
8023}
8024#endif /* SQLITE_OMIT_VIRTUALTABLE */
8025
8026
8027#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00008028/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00008029** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00008030**
8031** P1 is a cursor opened using VOpen. P2 is an address to jump to if
8032** the filtered result set is empty.
8033**
drh66a51672008-01-03 00:01:23 +00008034** P4 is either NULL or a string that was generated by the xBestIndex
8035** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00008036** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00008037**
drh9eff6162006-06-12 21:59:13 +00008038** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00008039** by P1. The integer query plan parameter to xFilter is stored in register
8040** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00008041** xFilter method. Registers P3+2..P3+1+argc are the argc
8042** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00008043** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00008044**
danielk19776dbee812008-01-03 18:39:41 +00008045** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00008046*/
dan2adb3092022-12-06 18:48:06 +00008047case OP_VFilter: { /* jump, ncycle */
danielk1977b7a7b9a2006-06-13 10:24:42 +00008048 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00008049 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00008050 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00008051 Mem *pQuery;
8052 Mem *pArgc;
drhc960dcb2015-11-20 19:22:01 +00008053 sqlite3_vtab_cursor *pVCur;
drh4dc754d2008-07-23 18:17:32 +00008054 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00008055 VdbeCursor *pCur;
8056 int res;
8057 int i;
8058 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00008059
drha6c2ed92009-11-14 23:22:23 +00008060 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00008061 pArgc = &pQuery[1];
8062 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00008063 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00008064 REGISTER_TRACE(pOp->p3, pQuery);
drh3ab4ffc2021-11-11 11:23:08 +00008065 assert( pCur!=0 );
drhc960dcb2015-11-20 19:22:01 +00008066 assert( pCur->eCurType==CURTYPE_VTAB );
8067 pVCur = pCur->uc.pVCur;
8068 pVtab = pVCur->pVtab;
drh4dc754d2008-07-23 18:17:32 +00008069 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00008070
drh9cbf3422008-01-17 16:22:13 +00008071 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00008072 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00008073 nArg = (int)pArgc->u.i;
8074 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00008075
drh644a5292006-12-20 14:53:38 +00008076 /* Invoke the xFilter method */
drhf56fa462015-04-13 21:39:54 +00008077 apArg = p->apArg;
8078 for(i = 0; i<nArg; i++){
8079 apArg[i] = &pArgc[i+1];
8080 }
drhc960dcb2015-11-20 19:22:01 +00008081 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
drhf56fa462015-04-13 21:39:54 +00008082 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00008083 if( rc ) goto abort_due_to_error;
8084 res = pModule->xEof(pVCur);
drh1d454a32008-01-31 19:34:51 +00008085 pCur->nullRow = 0;
drhf56fa462015-04-13 21:39:54 +00008086 VdbeBranchTaken(res!=0,2);
8087 if( res ) goto jump_to_p2;
drh9eff6162006-06-12 21:59:13 +00008088 break;
8089}
8090#endif /* SQLITE_OMIT_VIRTUALTABLE */
8091
8092#ifndef SQLITE_OMIT_VIRTUALTABLE
drhce2fbd12018-01-12 21:00:14 +00008093/* Opcode: VColumn P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00008094** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00008095**
drh6f390be2018-01-11 17:04:26 +00008096** Store in register P3 the value of the P2-th column of
8097** the current row of the virtual-table of cursor P1.
8098**
8099** If the VColumn opcode is being used to fetch the value of
drhce2fbd12018-01-12 21:00:14 +00008100** an unchanging column during an UPDATE operation, then the P5
drh09d00b22018-09-27 20:20:01 +00008101** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
8102** function to return true inside the xColumn method of the virtual
8103** table implementation. The P5 column might also contain other
8104** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
8105** unused by OP_VColumn.
drh9eff6162006-06-12 21:59:13 +00008106*/
dan2adb3092022-12-06 18:48:06 +00008107case OP_VColumn: { /* ncycle */
danielk19773e3a84d2008-08-01 17:37:40 +00008108 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00008109 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00008110 Mem *pDest;
8111 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00008112
drhdfe88ec2008-11-03 20:55:06 +00008113 VdbeCursor *pCur = p->apCsr[pOp->p1];
drh3ab4ffc2021-11-11 11:23:08 +00008114 assert( pCur!=0 );
drh9f6168b2016-03-19 23:32:58 +00008115 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00008116 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00008117 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00008118 if( pCur->nullRow ){
8119 sqlite3VdbeMemSetNull(pDest);
8120 break;
8121 }
drha27e3502022-06-10 10:10:31 +00008122 assert( pCur->eCurType==CURTYPE_VTAB );
drhc960dcb2015-11-20 19:22:01 +00008123 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00008124 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00008125 assert( pModule->xColumn );
8126 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00008127 sContext.pOut = pDest;
drh659fdb42022-04-01 15:31:58 +00008128 sContext.enc = encoding;
drh75f10762019-12-14 18:08:22 +00008129 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
drh09d00b22018-09-27 20:20:01 +00008130 if( pOp->p5 & OPFLAG_NOCHNG ){
drhce2fbd12018-01-12 21:00:14 +00008131 sqlite3VdbeMemSetNull(pDest);
8132 pDest->flags = MEM_Null|MEM_Zero;
8133 pDest->u.nZero = 0;
8134 }else{
8135 MemSetTypeFlag(pDest, MEM_Null);
8136 }
drhc960dcb2015-11-20 19:22:01 +00008137 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00008138 sqlite3VtabImportErrmsg(p, pVtab);
drhf09ac0b2018-01-23 03:44:06 +00008139 if( sContext.isError>0 ){
dan099fa842018-01-30 18:33:23 +00008140 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
drh4c8555f2009-06-25 01:47:11 +00008141 rc = sContext.isError;
8142 }
drh9bd038f2014-08-27 14:14:06 +00008143 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00008144 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00008145 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00008146
drh9467abf2016-02-17 18:44:11 +00008147 if( rc ) goto abort_due_to_error;
drh9eff6162006-06-12 21:59:13 +00008148 break;
8149}
8150#endif /* SQLITE_OMIT_VIRTUALTABLE */
8151
8152#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00008153/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00008154**
8155** Advance virtual table P1 to the next row in its result set and
8156** jump to instruction P2. Or, if the virtual table has reached
8157** the end of its result set, then fall through to the next instruction.
8158*/
dan2adb3092022-12-06 18:48:06 +00008159case OP_VNext: { /* jump, ncycle */
danielk19773e3a84d2008-08-01 17:37:40 +00008160 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00008161 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00008162 int res;
drh856c1032009-06-02 15:21:42 +00008163 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00008164
drh856c1032009-06-02 15:21:42 +00008165 pCur = p->apCsr[pOp->p1];
drh3ab4ffc2021-11-11 11:23:08 +00008166 assert( pCur!=0 );
drhc960dcb2015-11-20 19:22:01 +00008167 assert( pCur->eCurType==CURTYPE_VTAB );
drh2945b4a2008-01-31 15:53:45 +00008168 if( pCur->nullRow ){
8169 break;
8170 }
drhc960dcb2015-11-20 19:22:01 +00008171 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00008172 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00008173 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00008174
drhde4fcfd2008-01-19 23:50:26 +00008175 /* Invoke the xNext() method of the module. There is no way for the
8176 ** underlying implementation to return an error if one occurs during
8177 ** xNext(). Instead, if an error occurs, true is returned (indicating that
8178 ** data is available) and the error code returned when xColumn or
8179 ** some other method is next invoked on the save virtual table cursor.
8180 */
drhc960dcb2015-11-20 19:22:01 +00008181 rc = pModule->xNext(pCur->uc.pVCur);
dan016f7812013-08-21 17:35:48 +00008182 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00008183 if( rc ) goto abort_due_to_error;
8184 res = pModule->xEof(pCur->uc.pVCur);
drh688852a2014-02-17 22:40:43 +00008185 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00008186 if( !res ){
8187 /* If there is data, jump to P2 */
drhf56fa462015-04-13 21:39:54 +00008188 goto jump_to_p2_and_check_for_interrupt;
drhde4fcfd2008-01-19 23:50:26 +00008189 }
drh49afe3a2013-07-10 03:05:14 +00008190 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00008191}
8192#endif /* SQLITE_OMIT_VIRTUALTABLE */
8193
danielk1977182c4ba2007-06-27 15:53:34 +00008194#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00008195/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00008196**
drh66a51672008-01-03 00:01:23 +00008197** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00008198** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00008199** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00008200*/
drh9cbf3422008-01-17 16:22:13 +00008201case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00008202 sqlite3_vtab *pVtab;
8203 Mem *pName;
dan34566c42018-09-20 17:21:21 +00008204 int isLegacy;
8205
8206 isLegacy = (db->flags & SQLITE_LegacyAlter);
8207 db->flags |= SQLITE_LegacyAlter;
danielk1977595a5232009-07-24 17:58:53 +00008208 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00008209 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00008210 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00008211 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00008212 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00008213 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00008214 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00008215 testcase( pName->enc==SQLITE_UTF8 );
8216 testcase( pName->enc==SQLITE_UTF16BE );
8217 testcase( pName->enc==SQLITE_UTF16LE );
8218 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
drh9467abf2016-02-17 18:44:11 +00008219 if( rc ) goto abort_due_to_error;
8220 rc = pVtab->pModule->xRename(pVtab, pName->z);
drhd5b44d62018-12-06 17:06:02 +00008221 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
drh9467abf2016-02-17 18:44:11 +00008222 sqlite3VtabImportErrmsg(p, pVtab);
8223 p->expired = 0;
8224 if( rc ) goto abort_due_to_error;
danielk1977182c4ba2007-06-27 15:53:34 +00008225 break;
8226}
8227#endif
drh4cbdda92006-06-14 19:00:20 +00008228
8229#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00008230/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00008231** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00008232**
drh66a51672008-01-03 00:01:23 +00008233** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00008234** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00008235** are contiguous memory cells starting at P3 to pass to the xUpdate
8236** invocation. The value in register (P3+P2-1) corresponds to the
8237** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00008238**
8239** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00008240** The argv[0] element (which corresponds to memory cell P3)
8241** is the rowid of a row to delete. If argv[0] is NULL then no
8242** deletion occurs. The argv[1] element is the rowid of the new
8243** row. This can be NULL to have the virtual table select the new
8244** rowid for itself. The subsequent elements in the array are
8245** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00008246**
8247** If P2==1 then no insert is performed. argv[0] is the rowid of
8248** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00008249**
8250** P1 is a boolean flag. If it is set to true and the xUpdate call
8251** is successful, then the value returned by sqlite3_last_insert_rowid()
8252** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00008253**
8254** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
8255** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00008256*/
drh9cbf3422008-01-17 16:22:13 +00008257case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00008258 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00008259 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00008260 int nArg;
8261 int i;
drh9135ebb2021-11-11 23:52:44 +00008262 sqlite_int64 rowid = 0;
drh856c1032009-06-02 15:21:42 +00008263 Mem **apArg;
8264 Mem *pX;
8265
danb061d052011-04-25 18:49:57 +00008266 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
8267 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
8268 );
drh9e92a472013-06-27 17:40:30 +00008269 assert( p->readOnly==0 );
dan466ea9b2018-06-13 11:11:13 +00008270 if( db->mallocFailed ) goto no_mem;
drh4031baf2018-05-28 17:31:20 +00008271 sqlite3VdbeIncrWriteCounter(p, 0);
danielk1977595a5232009-07-24 17:58:53 +00008272 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00008273 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
8274 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00008275 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00008276 }
8277 pModule = pVtab->pModule;
drh856c1032009-06-02 15:21:42 +00008278 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00008279 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00008280 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00008281 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00008282 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00008283 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00008284 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00008285 assert( memIsValid(pX) );
8286 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00008287 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00008288 pX++;
danielk1977399918f2006-06-14 13:03:23 +00008289 }
danb061d052011-04-25 18:49:57 +00008290 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00008291 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00008292 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00008293 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00008294 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00008295 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drhfae58d52017-01-26 17:26:44 +00008296 db->lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00008297 }
drhd91c1a12013-02-09 13:58:25 +00008298 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00008299 if( pOp->p5==OE_Ignore ){
8300 rc = SQLITE_OK;
8301 }else{
8302 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
8303 }
8304 }else{
8305 p->nChange++;
8306 }
drh9467abf2016-02-17 18:44:11 +00008307 if( rc ) goto abort_due_to_error;
danielk1977399918f2006-06-14 13:03:23 +00008308 }
drh4cbdda92006-06-14 19:00:20 +00008309 break;
danielk1977399918f2006-06-14 13:03:23 +00008310}
8311#endif /* SQLITE_OMIT_VIRTUALTABLE */
8312
danielk197759a93792008-05-15 17:48:20 +00008313#ifndef SQLITE_OMIT_PAGER_PRAGMAS
8314/* Opcode: Pagecount P1 P2 * * *
8315**
8316** Write the current number of pages in database P1 to memory cell P2.
8317*/
drh27a348c2015-04-13 19:14:06 +00008318case OP_Pagecount: { /* out2 */
8319 pOut = out2Prerelease(p, pOp);
drhb1299152010-03-30 22:58:33 +00008320 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00008321 break;
8322}
8323#endif
8324
drh60ac3f42010-11-23 18:59:27 +00008325
8326#ifndef SQLITE_OMIT_PAGER_PRAGMAS
8327/* Opcode: MaxPgcnt P1 P2 P3 * *
8328**
8329** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00008330** Do not let the maximum page count fall below the current page count and
8331** do not change the maximum page count value if P3==0.
8332**
drh60ac3f42010-11-23 18:59:27 +00008333** Store the maximum page count after the change in register P2.
8334*/
drh27a348c2015-04-13 19:14:06 +00008335case OP_MaxPgcnt: { /* out2 */
drhc84e0332010-11-23 20:25:08 +00008336 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00008337 Btree *pBt;
8338
drh27a348c2015-04-13 19:14:06 +00008339 pOut = out2Prerelease(p, pOp);
drh60ac3f42010-11-23 18:59:27 +00008340 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00008341 newMax = 0;
8342 if( pOp->p3 ){
8343 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00008344 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00008345 }
8346 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00008347 break;
8348}
8349#endif
8350
drh920cf592019-10-30 16:29:02 +00008351/* Opcode: Function P1 P2 P3 P4 *
drhd7b10d72020-02-01 17:38:24 +00008352** Synopsis: r[P3]=func(r[P2@NP])
drh3e34eab2017-07-19 19:48:40 +00008353**
8354** Invoke a user function (P4 is a pointer to an sqlite3_context object that
drh920cf592019-10-30 16:29:02 +00008355** contains a pointer to the function to be run) with arguments taken
8356** from register P2 and successors. The number of arguments is in
8357** the sqlite3_context object that P4 points to.
8358** The result of the function is stored
drh3e34eab2017-07-19 19:48:40 +00008359** in register P3. Register P3 must not be one of the function inputs.
8360**
8361** P1 is a 32-bit bitmask indicating whether or not each argument to the
8362** function was determined to be constant at compile time. If the first
8363** argument was constant then bit 0 of P1 is set. This is used to determine
8364** whether meta data associated with a user function argument using the
8365** sqlite3_set_auxdata() API may be safely retained until the next
8366** invocation of this opcode.
8367**
drh920cf592019-10-30 16:29:02 +00008368** See also: AggStep, AggFinal, PureFunc
drh3e34eab2017-07-19 19:48:40 +00008369*/
drh920cf592019-10-30 16:29:02 +00008370/* Opcode: PureFunc P1 P2 P3 P4 *
drhd7b10d72020-02-01 17:38:24 +00008371** Synopsis: r[P3]=func(r[P2@NP])
drh920cf592019-10-30 16:29:02 +00008372**
8373** Invoke a user function (P4 is a pointer to an sqlite3_context object that
8374** contains a pointer to the function to be run) with arguments taken
8375** from register P2 and successors. The number of arguments is in
8376** the sqlite3_context object that P4 points to.
8377** The result of the function is stored
8378** in register P3. Register P3 must not be one of the function inputs.
8379**
8380** P1 is a 32-bit bitmask indicating whether or not each argument to the
8381** function was determined to be constant at compile time. If the first
8382** argument was constant then bit 0 of P1 is set. This is used to determine
8383** whether meta data associated with a user function argument using the
8384** sqlite3_set_auxdata() API may be safely retained until the next
8385** invocation of this opcode.
8386**
8387** This opcode works exactly like OP_Function. The only difference is in
8388** its name. This opcode is used in places where the function must be
8389** purely non-deterministic. Some built-in date/time functions can be
8390** either determinitic of non-deterministic, depending on their arguments.
8391** When those function are used in a non-deterministic way, they will check
8392** to see if they were called using OP_PureFunc instead of OP_Function, and
8393** if they were, they throw an error.
8394**
8395** See also: AggStep, AggFinal, Function
8396*/
mistachkin758784d2018-07-25 15:12:29 +00008397case OP_PureFunc: /* group */
8398case OP_Function: { /* group */
drh3e34eab2017-07-19 19:48:40 +00008399 int i;
8400 sqlite3_context *pCtx;
8401
8402 assert( pOp->p4type==P4_FUNCCTX );
8403 pCtx = pOp->p4.pCtx;
8404
8405 /* If this function is inside of a trigger, the register array in aMem[]
8406 ** might change from one evaluation to the next. The next block of code
8407 ** checks to see if the register array has changed, and if so it
8408 ** reinitializes the relavant parts of the sqlite3_context object */
8409 pOut = &aMem[pOp->p3];
8410 if( pCtx->pOut != pOut ){
drh920cf592019-10-30 16:29:02 +00008411 pCtx->pVdbe = p;
drh3e34eab2017-07-19 19:48:40 +00008412 pCtx->pOut = pOut;
drh659fdb42022-04-01 15:31:58 +00008413 pCtx->enc = encoding;
drh3e34eab2017-07-19 19:48:40 +00008414 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
8415 }
drh920cf592019-10-30 16:29:02 +00008416 assert( pCtx->pVdbe==p );
drh3e34eab2017-07-19 19:48:40 +00008417
8418 memAboutToChange(p, pOut);
8419#ifdef SQLITE_DEBUG
8420 for(i=0; i<pCtx->argc; i++){
8421 assert( memIsValid(pCtx->argv[i]) );
8422 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
8423 }
8424#endif
8425 MemSetTypeFlag(pOut, MEM_Null);
drhf09ac0b2018-01-23 03:44:06 +00008426 assert( pCtx->isError==0 );
drh3e34eab2017-07-19 19:48:40 +00008427 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
8428
8429 /* If the function returned an error, throw an exception */
drhf09ac0b2018-01-23 03:44:06 +00008430 if( pCtx->isError ){
8431 if( pCtx->isError>0 ){
drh3e34eab2017-07-19 19:48:40 +00008432 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
8433 rc = pCtx->isError;
8434 }
8435 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
drhf09ac0b2018-01-23 03:44:06 +00008436 pCtx->isError = 0;
drh3e34eab2017-07-19 19:48:40 +00008437 if( rc ) goto abort_due_to_error;
8438 }
8439
drhfb92e072022-03-29 01:43:09 +00008440 assert( (pOut->flags&MEM_Str)==0
8441 || pOut->enc==encoding
8442 || db->mallocFailed );
8443 assert( !sqlite3VdbeMemTooBig(pOut) );
drh3e34eab2017-07-19 19:48:40 +00008444
8445 REGISTER_TRACE(pOp->p3, pOut);
8446 UPDATE_MAX_BLOBSIZE(pOut);
8447 break;
8448}
8449
drh8878f8a2022-06-09 16:19:01 +00008450/* Opcode: ClrSubtype P1 * * * *
8451** Synopsis: r[P1].subtype = 0
8452**
8453** Clear the subtype from register P1.
8454*/
8455case OP_ClrSubtype: { /* in1 */
8456 pIn1 = &aMem[pOp->p1];
8457 pIn1->flags &= ~MEM_Subtype;
8458 break;
8459}
8460
drh2db144c2021-12-01 16:31:02 +00008461/* Opcode: FilterAdd P1 * P3 P4 *
8462** Synopsis: filter(P1) += key(P3@P4)
8463**
8464** Compute a hash on the P4 registers starting with r[P3] and
8465** add that hash to the bloom filter contained in r[P1].
8466*/
8467case OP_FilterAdd: {
drh5baaf402021-12-06 13:07:28 +00008468 u64 h;
drh2db144c2021-12-01 16:31:02 +00008469
8470 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
8471 pIn1 = &aMem[pOp->p1];
8472 assert( pIn1->flags & MEM_Blob );
drh5baaf402021-12-06 13:07:28 +00008473 assert( pIn1->n>0 );
drh2db144c2021-12-01 16:31:02 +00008474 h = filterHash(aMem, pOp);
8475#ifdef SQLITE_DEBUG
8476 if( db->flags&SQLITE_VdbeTrace ){
8477 int ii;
8478 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
8479 registerTrace(ii, &aMem[ii]);
8480 }
drh5baaf402021-12-06 13:07:28 +00008481 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
drh2db144c2021-12-01 16:31:02 +00008482 }
8483#endif
drh5baaf402021-12-06 13:07:28 +00008484 h %= pIn1->n;
drh2db144c2021-12-01 16:31:02 +00008485 pIn1->z[h/8] |= 1<<(h&7);
8486 break;
8487}
8488
8489/* Opcode: Filter P1 P2 P3 P4 *
8490** Synopsis: if key(P3@P4) not in filter(P1) goto P2
8491**
8492** Compute a hash on the key contained in the P4 registers starting
8493** with r[P3]. Check to see if that hash is found in the
8494** bloom filter hosted by register P1. If it is not present then
8495** maybe jump to P2. Otherwise fall through.
8496**
8497** False negatives are harmless. It is always safe to fall through,
8498** even if the value is in the bloom filter. A false negative causes
8499** more CPU cycles to be used, but it should still yield the correct
8500** answer. However, an incorrect answer may well arise from a
8501** false positive - if the jump is taken when it should fall through.
8502*/
8503case OP_Filter: { /* jump */
drh5baaf402021-12-06 13:07:28 +00008504 u64 h;
drh2db144c2021-12-01 16:31:02 +00008505
8506 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
8507 pIn1 = &aMem[pOp->p1];
drh7e910f62021-12-09 01:28:15 +00008508 assert( (pIn1->flags & MEM_Blob)!=0 );
8509 assert( pIn1->n >= 1 );
drh2db144c2021-12-01 16:31:02 +00008510 h = filterHash(aMem, pOp);
8511#ifdef SQLITE_DEBUG
8512 if( db->flags&SQLITE_VdbeTrace ){
8513 int ii;
8514 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
8515 registerTrace(ii, &aMem[ii]);
8516 }
drh5baaf402021-12-06 13:07:28 +00008517 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
drh2db144c2021-12-01 16:31:02 +00008518 }
8519#endif
drh5baaf402021-12-06 13:07:28 +00008520 h %= pIn1->n;
drh067c60c2021-12-04 18:45:08 +00008521 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){
8522 VdbeBranchTaken(1, 2);
drh23d41e62021-12-06 21:45:31 +00008523 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++;
drh067c60c2021-12-04 18:45:08 +00008524 goto jump_to_p2;
8525 }else{
drh23d41e62021-12-06 21:45:31 +00008526 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++;
drh067c60c2021-12-04 18:45:08 +00008527 VdbeBranchTaken(0, 2);
8528 }
drh2db144c2021-12-01 16:31:02 +00008529 break;
8530}
8531
drhf259df52017-12-27 20:38:35 +00008532/* Opcode: Trace P1 P2 * P4 *
8533**
8534** Write P4 on the statement trace output if statement tracing is
8535** enabled.
8536**
8537** Operand P1 must be 0x7fffffff and P2 must positive.
8538*/
drh74588ce2017-09-13 00:13:05 +00008539/* Opcode: Init P1 P2 P3 P4 *
drh72e26de2016-08-24 21:24:04 +00008540** Synopsis: Start at P2
drhaceb31b2014-02-08 01:40:27 +00008541**
8542** Programs contain a single instance of this opcode as the very first
8543** opcode.
drh949f9cd2008-01-12 21:35:57 +00008544**
8545** If tracing is enabled (by the sqlite3_trace()) interface, then
8546** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00008547** Or if P4 is blank, use the string returned by sqlite3_sql().
8548**
8549** If P2 is not zero, jump to instruction P2.
drh9e5eb9c2016-09-18 16:08:10 +00008550**
8551** Increment the value of P1 so that OP_Once opcodes will jump the
8552** first time they are evaluated for this run.
drh74588ce2017-09-13 00:13:05 +00008553**
8554** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
8555** error is encountered.
drh949f9cd2008-01-12 21:35:57 +00008556*/
drhf259df52017-12-27 20:38:35 +00008557case OP_Trace:
drhaceb31b2014-02-08 01:40:27 +00008558case OP_Init: { /* jump */
drh9e5eb9c2016-09-18 16:08:10 +00008559 int i;
drhb9f47992018-01-24 12:14:43 +00008560#ifndef SQLITE_OMIT_TRACE
8561 char *zTrace;
8562#endif
drh5fe63bf2016-07-25 02:42:22 +00008563
8564 /* If the P4 argument is not NULL, then it must be an SQL comment string.
8565 ** The "--" string is broken up to prevent false-positives with srcck1.c.
8566 **
8567 ** This assert() provides evidence for:
8568 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
8569 ** would have been returned by the legacy sqlite3_trace() interface by
8570 ** using the X argument when X begins with "--" and invoking
8571 ** sqlite3_expanded_sql(P) otherwise.
8572 */
8573 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
drhf259df52017-12-27 20:38:35 +00008574
8575 /* OP_Init is always instruction 0 */
8576 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
drh856c1032009-06-02 15:21:42 +00008577
drhaceb31b2014-02-08 01:40:27 +00008578#ifndef SQLITE_OMIT_TRACE
drhfca760c2016-07-14 01:09:08 +00008579 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
drha24832b2022-04-01 19:04:13 +00008580 && p->minWriteFileFormat!=254 /* tag-20220401a */
drh37f58e92012-09-04 21:34:26 +00008581 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
8582 ){
drh3d2a5292016-07-13 22:55:01 +00008583#ifndef SQLITE_OMIT_DEPRECATED
drhfca760c2016-07-14 01:09:08 +00008584 if( db->mTrace & SQLITE_TRACE_LEGACY ){
drh5fe63bf2016-07-25 02:42:22 +00008585 char *z = sqlite3VdbeExpandSql(p, zTrace);
drh08b92082020-08-10 14:18:00 +00008586 db->trace.xLegacy(db->pTraceArg, z);
drhbd441f72016-07-25 02:31:48 +00008587 sqlite3_free(z);
drhfca760c2016-07-14 01:09:08 +00008588 }else
drh3d2a5292016-07-13 22:55:01 +00008589#endif
drh7adbcff2017-03-20 15:29:28 +00008590 if( db->nVdbeExec>1 ){
8591 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
drh08b92082020-08-10 14:18:00 +00008592 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
drh7adbcff2017-03-20 15:29:28 +00008593 sqlite3DbFree(db, z);
8594 }else{
drh08b92082020-08-10 14:18:00 +00008595 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
drh3d2a5292016-07-13 22:55:01 +00008596 }
drh949f9cd2008-01-12 21:35:57 +00008597 }
drh8f8b2312013-10-18 20:03:43 +00008598#ifdef SQLITE_USE_FCNTL_TRACE
8599 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
8600 if( zTrace ){
mistachkind8992ce2016-09-20 17:49:01 +00008601 int j;
8602 for(j=0; j<db->nDb; j++){
8603 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
8604 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
drh8f8b2312013-10-18 20:03:43 +00008605 }
8606 }
8607#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00008608#ifdef SQLITE_DEBUG
8609 if( (db->flags & SQLITE_SqlTrace)!=0
8610 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
8611 ){
8612 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
8613 }
8614#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00008615#endif /* SQLITE_OMIT_TRACE */
drh4910a762016-09-03 01:46:15 +00008616 assert( pOp->p2>0 );
drh9e5eb9c2016-09-18 16:08:10 +00008617 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
drhf259df52017-12-27 20:38:35 +00008618 if( pOp->opcode==OP_Trace ) break;
drh9e5eb9c2016-09-18 16:08:10 +00008619 for(i=1; i<p->nOp; i++){
8620 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
8621 }
8622 pOp->p1 = 0;
8623 }
8624 pOp->p1++;
drh00d11d42017-06-29 12:49:18 +00008625 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
drh4910a762016-09-03 01:46:15 +00008626 goto jump_to_p2;
drh949f9cd2008-01-12 21:35:57 +00008627}
drh949f9cd2008-01-12 21:35:57 +00008628
drh28935362013-12-07 20:39:19 +00008629#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0df57012015-08-14 15:05:55 +00008630/* Opcode: CursorHint P1 * * P4 *
drh28935362013-12-07 20:39:19 +00008631**
8632** Provide a hint to cursor P1 that it only needs to return rows that
drh0df57012015-08-14 15:05:55 +00008633** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
8634** to values currently held in registers. TK_COLUMN terms in the P4
8635** expression refer to columns in the b-tree to which cursor P1 is pointing.
drh28935362013-12-07 20:39:19 +00008636*/
8637case OP_CursorHint: {
8638 VdbeCursor *pC;
8639
8640 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
8641 assert( pOp->p4type==P4_EXPR );
8642 pC = p->apCsr[pOp->p1];
dan91d3a612014-07-15 11:59:44 +00008643 if( pC ){
drhc960dcb2015-11-20 19:22:01 +00008644 assert( pC->eCurType==CURTYPE_BTREE );
drh62aaa6c2015-11-21 17:27:42 +00008645 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
8646 pOp->p4.pExpr, aMem);
dan91d3a612014-07-15 11:59:44 +00008647 }
drh28935362013-12-07 20:39:19 +00008648 break;
8649}
8650#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh91fd4d42008-01-19 20:11:25 +00008651
drh4031baf2018-05-28 17:31:20 +00008652#ifdef SQLITE_DEBUG
8653/* Opcode: Abortable * * * * *
8654**
8655** Verify that an Abort can happen. Assert if an Abort at this point
8656** might cause database corruption. This opcode only appears in debugging
8657** builds.
8658**
8659** An Abort is safe if either there have been no writes, or if there is
8660** an active statement journal.
8661*/
8662case OP_Abortable: {
8663 sqlite3VdbeAssertAbortable(p);
8664 break;
8665}
8666#endif
8667
drh13d79502019-12-23 02:18:49 +00008668#ifdef SQLITE_DEBUG
drh3aef2fb2020-01-02 17:46:02 +00008669/* Opcode: ReleaseReg P1 P2 P3 * P5
drh13d79502019-12-23 02:18:49 +00008670** Synopsis: release r[P1@P2] mask P3
8671**
8672** Release registers from service. Any content that was in the
8673** the registers is unreliable after this opcode completes.
8674**
8675** The registers released will be the P2 registers starting at P1,
8676** except if bit ii of P3 set, then do not release register P1+ii.
8677** In other words, P3 is a mask of registers to preserve.
8678**
8679** Releasing a register clears the Mem.pScopyFrom pointer. That means
8680** that if the content of the released register was set using OP_SCopy,
8681** a change to the value of the source register for the OP_SCopy will no longer
8682** generate an assertion fault in sqlite3VdbeMemAboutToChange().
8683**
drh3aef2fb2020-01-02 17:46:02 +00008684** If P5 is set, then all released registers have their type set
8685** to MEM_Undefined so that any subsequent attempt to read the released
drh13d79502019-12-23 02:18:49 +00008686** register (before it is reinitialized) will generate an assertion fault.
drh3aef2fb2020-01-02 17:46:02 +00008687**
8688** P5 ought to be set on every call to this opcode.
8689** However, there are places in the code generator will release registers
drh13d79502019-12-23 02:18:49 +00008690** before their are used, under the (valid) assumption that the registers
8691** will not be reallocated for some other purpose before they are used and
8692** hence are safe to release.
8693**
8694** This opcode is only available in testing and debugging builds. It is
8695** not generated for release builds. The purpose of this opcode is to help
8696** validate the generated bytecode. This opcode does not actually contribute
8697** to computing an answer.
8698*/
8699case OP_ReleaseReg: {
8700 Mem *pMem;
8701 int i;
8702 u32 constMask;
8703 assert( pOp->p1>0 );
8704 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
8705 pMem = &aMem[pOp->p1];
8706 constMask = pOp->p3;
8707 for(i=0; i<pOp->p2; i++, pMem++){
drh7edce5e2019-12-23 13:24:34 +00008708 if( i>=32 || (constMask & MASKBIT32(i))==0 ){
drh13d79502019-12-23 02:18:49 +00008709 pMem->pScopyFrom = 0;
drh3aef2fb2020-01-02 17:46:02 +00008710 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
drh13d79502019-12-23 02:18:49 +00008711 }
8712 }
8713 break;
8714}
8715#endif
8716
drh91fd4d42008-01-19 20:11:25 +00008717/* Opcode: Noop * * * * *
8718**
8719** Do nothing. This instruction is often useful as a jump
8720** destination.
drh5e00f6c2001-09-13 13:46:56 +00008721*/
drh91fd4d42008-01-19 20:11:25 +00008722/*
8723** The magic Explain opcode are only inserted when explain==2 (which
8724** is to say when the EXPLAIN QUERY PLAN syntax is used.)
8725** This opcode records information from the optimizer. It is the
8726** the same as a no-op. This opcodesnever appears in a real VM program.
8727*/
drh4031baf2018-05-28 17:31:20 +00008728default: { /* This is really OP_Noop, OP_Explain */
drh13573c72010-01-12 17:04:07 +00008729 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh4031baf2018-05-28 17:31:20 +00008730
drh5e00f6c2001-09-13 13:46:56 +00008731 break;
8732}
8733
8734/*****************************************************************************
8735** The cases of the switch statement above this line should all be indented
8736** by 6 spaces. But the left-most 6 spaces have been removed to improve the
8737** readability. From this point on down, the normal indentation rules are
8738** restored.
8739*****************************************************************************/
8740 }
drh6e142f52000-06-08 13:36:40 +00008741
dan231ff4b2022-12-02 20:32:22 +00008742#if defined(VDBE_PROFILE)
dan7f4b0662022-12-07 20:09:54 +00008743 *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
8744 pnCycle = 0;
dan231ff4b2022-12-02 20:32:22 +00008745#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS)
dan7f4b0662022-12-07 20:09:54 +00008746 *pnCycle += sqlite3Hwtime();
8747 pnCycle = 0;
drh7b396862003-01-01 23:06:20 +00008748#endif
8749
drh6e142f52000-06-08 13:36:40 +00008750 /* The following code adds nothing to the actual functionality
8751 ** of the program. It is only here for testing and debugging.
8752 ** On the other hand, it does burn CPU cycles every time through
8753 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
8754 */
8755#ifndef NDEBUG
drh6dc41482015-04-16 17:31:02 +00008756 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
drhae7e1512007-05-02 16:51:59 +00008757
drhcf1023c2007-05-08 20:59:49 +00008758#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00008759 if( db->flags & SQLITE_VdbeTrace ){
drh7cc84c22016-04-11 13:36:42 +00008760 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
drh84e55a82013-11-13 17:58:23 +00008761 if( rc!=0 ) printf("rc=%d\n",rc);
drh7cc84c22016-04-11 13:36:42 +00008762 if( opProperty & (OPFLG_OUT2) ){
drh6dc41482015-04-16 17:31:02 +00008763 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
drh75897232000-05-29 14:26:00 +00008764 }
drh7cc84c22016-04-11 13:36:42 +00008765 if( opProperty & OPFLG_OUT3 ){
drh6dc41482015-04-16 17:31:02 +00008766 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00008767 }
drh17aceeb2020-01-04 19:12:13 +00008768 if( opProperty==0xff ){
8769 /* Never happens. This code exists to avoid a harmless linkage
8770 ** warning aboud sqlite3VdbeRegisterDump() being defined but not
8771 ** used. */
8772 sqlite3VdbeRegisterDump(p);
8773 }
drh75897232000-05-29 14:26:00 +00008774 }
danielk1977b5402fb2005-01-12 07:15:04 +00008775#endif /* SQLITE_DEBUG */
8776#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00008777 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00008778
drha05a7222008-01-19 03:35:58 +00008779 /* If we reach this point, it means that execution is finished with
8780 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00008781 */
drh9467abf2016-02-17 18:44:11 +00008782abort_due_to_error:
drhf56a4bf2020-11-18 21:50:05 +00008783 if( db->mallocFailed ){
8784 rc = SQLITE_NOMEM_BKPT;
8785 }else if( rc==SQLITE_IOERR_CORRUPTFS ){
8786 rc = SQLITE_CORRUPT_BKPT;
8787 }
drha05a7222008-01-19 03:35:58 +00008788 assert( rc );
drh11eb9c62021-09-16 12:33:53 +00008789#ifdef SQLITE_DEBUG
8790 if( db->flags & SQLITE_VdbeTrace ){
drh5b001cc2021-11-15 13:22:42 +00008791 const char *zTrace = p->zSql;
8792 if( zTrace==0 ){
8793 if( aOp[0].opcode==OP_Trace ){
8794 zTrace = aOp[0].p4.z;
8795 }
8796 if( zTrace==0 ) zTrace = "???";
8797 }
8798 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
drh11eb9c62021-09-16 12:33:53 +00008799 }
8800#endif
drh9467abf2016-02-17 18:44:11 +00008801 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
8802 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
8803 }
drha05a7222008-01-19 03:35:58 +00008804 p->rc = rc;
drhf68521c2016-03-21 12:28:02 +00008805 sqlite3SystemError(db, rc);
drha64fa912010-03-04 00:53:32 +00008806 testcase( sqlite3GlobalConfig.xLog!=0 );
8807 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
drhf56fa462015-04-13 21:39:54 +00008808 (int)(pOp - aOp), p->zSql, p->zErrMsg);
drh8703edd2022-04-03 22:35:13 +00008809 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
drh4a642b62016-02-05 01:55:27 +00008810 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
drh46c425b2021-11-10 10:59:10 +00008811 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){
8812 db->flags |= SQLITE_CorruptRdOnly;
8813 }
danielk19777eaabcd2008-07-07 14:56:56 +00008814 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00008815 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00008816 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00008817 }
drh900b31e2007-08-28 02:27:51 +00008818
8819 /* This is the only way out of this procedure. We have to
8820 ** release the mutexes on btrees that were acquired at the
8821 ** top. */
8822vdbe_return:
dan231ff4b2022-12-02 20:32:22 +00008823#if defined(VDBE_PROFILE)
dan7f4b0662022-12-07 20:09:54 +00008824 if( pnCycle ){
8825 *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
8826 pnCycle = 0;
8827 }
dan231ff4b2022-12-02 20:32:22 +00008828#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS)
dana3d0c152022-12-05 18:19:56 +00008829 if( pnCycle ){
8830 *pnCycle += sqlite3Hwtime();
8831 pnCycle = 0;
8832 }
dan231ff4b2022-12-02 20:32:22 +00008833#endif
8834
drhc332e042019-02-12 21:04:33 +00008835#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drhb1af9c62019-02-20 13:55:45 +00008836 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
8837 nProgressLimit += db->nProgressOps;
drhc332e042019-02-12 21:04:33 +00008838 if( db->xProgress(db->pProgressArg) ){
drhd1d89142020-07-06 12:13:05 +00008839 nProgressLimit = LARGEST_UINT64;
drhc332e042019-02-12 21:04:33 +00008840 rc = SQLITE_INTERRUPT;
8841 goto abort_due_to_error;
8842 }
8843 }
8844#endif
drh9b47ee32013-08-20 03:13:51 +00008845 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhde5f3af2022-12-23 11:46:57 +00008846 if( DbMaskNonZero(p->lockMask) ){
8847 sqlite3VdbeLeave(p);
8848 }
dan83f0ab82016-01-29 18:04:31 +00008849 assert( rc!=SQLITE_OK || nExtraDelete==0
8850 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
8851 );
drhb86ccfb2003-01-28 23:13:10 +00008852 return rc;
8853
drh023ae032007-05-08 12:12:16 +00008854 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
8855 ** is encountered.
8856 */
8857too_big:
drh22c17b82015-05-15 04:13:15 +00008858 sqlite3VdbeError(p, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00008859 rc = SQLITE_TOOBIG;
drh9467abf2016-02-17 18:44:11 +00008860 goto abort_due_to_error;
drh023ae032007-05-08 12:12:16 +00008861
drh98640a32007-06-07 19:08:32 +00008862 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00008863 */
8864no_mem:
drh4a642b62016-02-05 01:55:27 +00008865 sqlite3OomFault(db);
drh22c17b82015-05-15 04:13:15 +00008866 sqlite3VdbeError(p, "out of memory");
mistachkinfad30392016-02-13 23:43:46 +00008867 rc = SQLITE_NOMEM_BKPT;
drh9467abf2016-02-17 18:44:11 +00008868 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00008869
danielk19776f8a5032004-05-10 10:34:51 +00008870 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00008871 ** flag.
8872 */
8873abort_due_to_interrupt:
dan892edb62020-03-30 13:35:05 +00008874 assert( AtomicLoad(&db->u1.isInterrupted) );
drh56f18732020-06-03 15:59:22 +00008875 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +00008876 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00008877}