Lines Matching +full:button +full:- +full:392
17 ** language. The code for the "sqlite3" command-line shell is also in a
20 ** The content in this amalgamation comes from Fossil check-in
51 ** NO_TEST - The branches on this line are not
56 ** OPTIMIZATION-IF-TRUE - This branch is allowed to always be false
60 ** OPTIMIZATION-IF-FALSE - This branch is allowed to always be true
64 ** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
69 ** slash-asterisk...asterisk-slash comment marks, with no spaces between the
144 ** 2015-03-02
182 ** large file support, or if the OS is windows, these should be no-ops.
188 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
197 ** on 2008-11-28.) These days, all Linux kernels support large files, so
214 ** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
221 ** so the GCC_VERSION macro will be set to a correct non-zero value even
274 ** some MinGW-specific macros). When compiling for MinGW, either the
297 /* Optionally #include a user-defined header, whereby compilation options
315 ** 2001-09-15
326 ** presents to client programs. If a C-function, structure, datatype,
337 ** The official C-language API documentation for SQLite is derived
382 ** that require non-default calling conventions.
407 ** These no-op macros are used in front of interfaces to mark those
409 ** should not use deprecated interfaces - they are supported for backwards
433 ** CAPI3REF: Compile-Time Library Version Numbers
449 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
451 ** a string which identifies a particular check-in of SQLite
453 ** string contains the date and time of the check-in (UTC) and a SHA1
454 ** or SHA3-256 hash of the entire source tree. If the source code has
464 #define SQLITE_SOURCE_ID "2024-08-13 09:16:08 c9c2ab54ba1f5f46360f1b4f35d849cd3f080e6fc2b6c60e…
467 ** CAPI3REF: Run-Time Library Version Numbers
504 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
513 ** returning the N-th compile time option string. ^If N is out of range,
538 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
556 ** This interface only reports on the compile-time mutex setting
562 ** sqlite3_threadsafe() function shows only the compile-time setting of
563 ** thread safety, not any run-time changes to that setting made by
588 ** CAPI3REF: 64-Bit Integer Types
591 ** Because there is no cross-platform way to specify 64-bit integer types
592 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
599 ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
622 ** substitute integer for floating-point.
664 ** argument is a harmless no-op.
677 ** CAPI3REF: One-Step Query Execution Interface
685 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
686 ** semicolon-separate SQL statements passed into its 2nd argument,
708 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
760 /* beginning-of-error-codes */
791 /* end-of-error-codes */
799 ** these result codes are too coarse-grained. They do not provide as
905 ** [sqlite3_open_v2()] has historically be a no-op and might become an
961 ** read-only media and cannot be changed even by processes with
1023 ** (Third-party VFS implementations might also make the distinction
1058 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1082 ** requested lock, then the call to xLock() is a no-op.
1085 ** to xUnlock() is a no-op.
1098 ** locking strategy (for example to use dot-file locks), to inquire
1145 ** fails to zero-fill short reads might seem to work. However,
1146 ** failure to zero-fill short reads will eventually lead to
1202 ** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1204 ** of the in-memory database. The argument is a pointer to a [sqlite3_int64].
1214 ** point to an integer (type int) containing the new chunk-size to use
1216 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
1227 ** the [rollback journal] or the [write-ahead log]) for a particular database
1240 ** this file-control is NULL. However, if the database file is being synced
1241 ** as part of a multi-database commit, the argument points to a nul-terminated
1242 ** string containing the transactions super-journal file name. VFSes that
1259 ** anti-virus programs. By default, the windows VFS will retry file read,
1285 ** WAL mode. If the integer is -1, then it is overwritten with the current
1290 ** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1294 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1295 ** mode. If the integer is -1, then it is overwritten with the current
1296 ** zero-damage mode setting.
1307 ** final bottom-level VFS are written into memory obtained from
1311 ** all file-control actions, there is no guarantee that this will actually
1313 ** pointer in case this file-control is not implemented. This file-control
1317 ** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1321 ** to a pointer to the top-level VFS.)^
1323 ** upper-most shim only.
1340 ** VFS has handled the PRAGMA itself and the parser generates a no-op
1342 ** of the result string if the string is non-NULL.
1348 ** it is able to override built-in [PRAGMA] statements.
1352 ** file-control may be invoked by SQLite on the database file handle
1354 ** to the connection's busy-handler callback. The argument is of type (void**)
1355 ** - an array of two (void *) values. The first (void *) actually points
1357 ** busy-handler, this function should be invoked with the second (void *) in
1358 ** the array as the only argument. If it returns non-zero, then the operation
1363 ** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1373 ** maximum number of bytes that will be used for memory-mapped I/O.
1379 ** file-control is used internally to implement [PRAGMA mmap_size].
1385 ** The argument is a zero-terminated string. Higher layers in the
1387 ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
1412 ** Applications should <em>not</em> use this file-control.
1461 ** The parameter is a pointer to a 32-bit signed integer that contains
1462 ** the value that M is to be set to. Before returning, the 32-bit signed
1467 ** a database file. The argument is a pointer to a 32-bit unsigned integer.
1493 ** file to the database file, but before the *-shm file is updated to
1498 ** whether or not there is a database client in another process with a wal-mode
1500 ** (void*) argument passed with this file-control should be a pointer to a
1504 ** the database is not a wal-mode db, or if there is no such connection in any
1514 ** database is not a temp db, then the [SQLITE_FCNTL_RESET_CACHE] file-control
1515 ** purges the contents of the in-memory page cache. If there is an open
1516 ** transaction, or if the db is a temp-db, this opcode is a no-op, not an error.
1594 ** as a normal, nul-terminated, UTF-8 buffer containing the filename, but
1637 ** in a thread-safe way. The [sqlite3_vfs_find()] interface
1655 ** consist of a single "-" character followed by no more than
1656 ** 11 alphanumeric and/or "-" characters.
1671 ** If xOpen() opens a file read-only then it sets *pOutFlags to
1691 ** the open of a journal file a no-op. Writes to this journal would
1692 ** also be no-ops, and any attempt to read the journal would return
1694 ** file will be doing page-aligned sector reads and writes in a random
1733 ** flag is never actually used and is not implemented in the built-in
1736 ** non-zero error code if there is an I/O error or if the name of
1738 ** is returned, then non-zero or zero is written into *pResOut to indicate
1752 ** of good-quality randomness into zOut. The return value is
1760 ** a 24-hour day).
1786 void *pAppData; /* Pointer to application-specific data */
1896 ** are harmless no-ops.)^
1901 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1924 ** compile-time option, then the automatic calls to sqlite3_initialize()
1933 ** The sqlite3_os_init() routine does operating-system specific
1949 ** (using the [SQLITE_OS_OTHER=1] compile-time
1951 ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
1989 ** implementation of an application-defined [sqlite3_os_init()].
1993 ** then this routine returns a non-zero [error code].
2007 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2020 ** and low-level memory allocation routines.
2032 ** Note that SQLite comes with several [built-in memory allocators]
2037 ** memory allocator that simulates memory out-of-memory conditions in
2104 ** [sqlite3_shutdown()] is a no-op that returns SQLITE_MISUSE.
2119 ** non-zero [error code] if a discontinued or unsupported configuration option
2125 ** [threading mode] to Single-thread. In other words, it disables
2128 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2130 ** value of Single-thread and so [sqlite3_config()] will return
2136 ** [threading mode] to Multi-thread. In other words, it disables
2140 ** are enabled so that SQLite will be safe to use in a multi-threaded
2143 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2144 ** it is not possible to set the Multi-thread [threading mode] and
2159 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2168 ** alternative low-level memory allocation routines to be used in place of
2196 ** disabled, the following SQLite interfaces become non-operational:
2217 ** This configuration option is a no-op if an application-defined page
2220 ** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2228 ** argument must be either a NULL pointer or a pointer to an 8-byte
2235 ** ^If pMem is NULL and N is non-zero, then each database connection
2238 ** of -1024*N bytes if N is negative, . ^If additional
2251 ** An 8-byte aligned pointer to the memory,
2258 ** The first pointer (the memory pointer) must be aligned to an 8-byte
2266 ** The argument specifies alternative low-level mutex routines to be used
2270 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2283 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2316 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2318 ** passed through as the first parameter to the application-defined logger
2326 ** In a multi-threaded application, the application-defined logger
2331 ** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2348 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2349 ** if that compile-time option is omitted.
2359 ** They are retained for backwards compatibility but are now no-ops.
2365 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2381 ** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2388 ** compile-time maximum mmap size set by the
2389 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2391 ** changed to its compile-time default.
2396 ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2397 ** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2413 ** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2422 ** becomes the [statement journal] spill-to-disk threshold.
2425 ** Or if the threshold is -1, statement journals are always held
2431 ** [SQLITE_STMTJRNL_SPILL] compile-time option.
2436 ** of type (int) - the new value of the sorter-reference size threshold.
2440 ** of a table column that its values are likely to be very large - larger
2441 ** than the configured sorter-reference size threshold - then a reference
2447 ** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2452 ** [sqlite3_int64] parameter which is the default maximum size for an in-memory
2455 ** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
2457 ** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
2458 ** compile-time option is not set, then the default maximum is 1073741824.
2464 ** compiled with -DSQLITE_ALLOW_ROWID_IN_VIEW, in which case the capability
2472 ** is compiled without -DSQLITE_ALLOW_ROWID_IN_VIEW (which is the usual and
2490 #define SQLITE_CONFIG_PCACHE 14 /* no-op */
2491 #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
2518 ** non-zero [error code] if a discontinued or unsupported configuration option
2534 ** must be aligned to an 8-byte boundary. ^If the second argument to
2570 ** triggers in the main database schema or in the schemas of ATTACH-ed
2587 ** views in the main database schema or in the schemas of ATTACH-ed
2594 ** [FTS3] full-text search engine extension.
2609 ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
2611 ** When the first argument to this interface is 1, then only the C-API is
2613 ** this interface is 0, then both the C-API and the SQL function are disabled.
2614 ** If the first argument is -1, then no changes are made to state of either the
2615 ** C-API or the SQL function.
2638 ** is an integer - positive to disable checkpoints-on-close, or zero (the
2641 ** into which is written 0 or 1 to indicate whether checkpoints-on-close
2642 ** have been disabled - 0 if they are not disabled, 1 if they are.
2665 ** behavior. The first parameter passed to this operation is an integer -
2669 ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2727 ** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
2736 ** the legacy [double-quoted string literal] misfeature for DML statements
2738 ** default value of this setting is determined by the [-DSQLITE_DQS]
2739 ** compile-time option.
2745 ** the legacy [double-quoted string literal] misfeature for DDL statements,
2747 ** default value of this setting is determined by the [-DSQLITE_DQS]
2748 ** compile-time option.
2775 ** created database file to have a schema format version number (the 4-byte
2802 ** an integer.. The first argument is 1, 0, or -1 to enable, disable, or
2817 ** argument is 1, 0, or -1 to enable, disable, or leave unchanged the
2863 ** has a unique 64-bit signed
2944 ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2966 ** any changes performed by sub-triggers, as the sqlite3_changes()
2967 ** value will be saved and restored after each sub-trigger has run.)^
3035 ** CAPI3REF: Interrupt A Long-Running Query
3041 ** or Ctrl-C where the user wants a long query operation to halt
3066 ** SQL statements is a no-op and has no effect on SQL statements
3079 ** These routines are useful during command-line input to determine if the
3085 ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3100 ** then the return value from sqlite3_complete16() will be non-zero
3103 ** The input to [sqlite3_complete()] must be a zero-terminated
3104 ** UTF-8 string.
3106 ** The input to [sqlite3_complete16()] must be a zero-terminated
3107 ** UTF-16 string in native byte order.
3114 ** KEYWORDS: {busy-handler callback} {busy handler}
3136 ** ^If the callback returns non-zero, then another attempt
3212 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
3214 ** to zero-terminated strings that contain the names of the columns.
3216 ** in NULL pointers. All other values are in their UTF-8 zero-terminated
3228 ** -----------------------
3250 ** semicolon-separated SQL statements in the zero-terminated UTF-8
3282 ** These routines are work-alikes of the "printf()" family of functions
3286 ** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3287 ** See the [built-in printf()] documentation for details.
3310 ** guarantees that the buffer is always zero-terminated. ^The first
3313 ** written will be n-1 characters.
3317 ** See also: [built-in printf()], [printf() SQL function]
3329 ** does not include operating-system specific [VFS] implementation. The
3340 ** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3341 ** of a signed 32-bit integer.
3346 ** a no-op if is called with a NULL pointer. Passing a NULL pointer
3351 ** might result if sqlite3_free() is called with a non-NULL pointer that
3371 ** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3372 ** of a 32-bit signed integer.
3387 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3411 ** routines, which form the built-in memory allocation subsystem.
3416 ** value of [sqlite3_memory_used()] since the high-water mark
3423 ** ^The memory high-water mark is reset to the current value of
3426 ** by [sqlite3_memory_highwater(1)] is the high-water mark
3433 ** CAPI3REF: Pseudo-Random Number Generator
3435 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3438 ** the built-in random() and randomblob() SQL functions. This interface allows
3449 ** non-NULL P then the pseudo-randomness is generated
3456 ** CAPI3REF: Compile-Time Authorization Callbacks
3487 ** to the callback are either NULL pointers or zero-terminated strings
3515 ** user-entered SQL is being [sqlite3_prepare | prepared] that
3534 ** statement might be re-prepared during [sqlite3_step()] due to a
3580 ** is the name of the inner-most trigger or view that is responsible for
3582 ** top-level SQL code.
3632 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3636 ** contain a UTF-8 SQL comment that identifies the trigger.)^
3638 ** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3643 ** the original statement text and an estimate of wall-clock time
3663 ** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
3685 ** interface by using the X argument when X begins with "--" and invoking
3692 ** X argument points to a 64-bit integer which is approximately
3722 ** M argument should be the bitwise OR-ed combination of
3774 ** ^If the progress callback returns non-zero, the operation is
3776 ** "Cancel" button on a GUI progress dialog box.
3798 ** filename argument. ^The filename argument is interpreted as UTF-8 for
3799 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
3810 ** ^The default encoding will be UTF-8 for databases created using
3812 ** created using sqlite3_open16() will be UTF-16 in the native byte order.
3826 ** <dd>The database is opened in read-only mode. If the database does
3834 ** read-write mode fails due to OS-level permissions, an attempt is
3835 ** made to open it in read-only mode. [sqlite3_db_readonly()] can be
3837 ** read-write.</dd>)^
3853 ** <dd>The database will be opened as an in-memory database. The database
3854 ** is named by the "filename" argument for the purposes of cache-sharing,
3859 ** <dd>The new database connection will use the "multi-thread"
3877 ** this option is a no-op.
3903 ** upon it. Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op
3914 ** ^If the filename is ":memory:", then a private, temporary in-memory database
3915 ** is created for the connection. ^This in-memory database will vanish when
3923 ** on-disk database will be created. ^This private database will be
3933 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
3956 ** SQLite and its built-in [VFSes] interpret the
3971 ** ^If "ro" is specified, then the database is opened for read-only
3974 ** "rw", then the database is opened for read-write (but not create)
3978 ** set to "memory" then a pure [in-memory database] that never reads
4005 ** read-only media. ^When immutable is set, SQLite assumes that the
4007 ** privilege, and so the database is opened read-only and all locking
4032 ** <tr><td style="white-space:nowrap">
4036 ** necessary - space characters can be used literally
4039 ** Open file "data.db" in the current directory for read-only access.
4040 ** Regardless of whether or not shared-cache mode is enabled by
4042 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4043 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4044 ** that uses dot-files in place of posix advisory locking.
4052 ** percent sign - "%" - followed by exactly two hexadecimal digits
4054 ** URI filename are interpreted, they are encoded using UTF-8 and all
4056 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
4060 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4062 ** characters must be converted to UTF-8 prior to passing them into
4072 const char *filename, /* Database filename (UTF-8) */
4076 const void *filename, /* Database filename (UTF-16) */
4080 const char *filename, /* Database filename (UTF-8) */
4117 ** case or if the value begins with a non-zero number. The
4125 ** 64-bit signed integer and returns that integer, or D if P does not
4130 ** the value) of the N-th query parameter for filename F, or a NULL
4132 ** parameters minus 1. The N value is zero-based so N should be 0 to obtain
4191 ** ^If X is the name of a rollback or WAL-mode journal file that is
4197 ** only. It is not a general-purpose interface.
4241 ** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4277 ** change the value of the error code. The error-code preserving
4288 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4289 ** text that describes the error, as either UTF-8 or UTF-16 respectively,
4297 ** ^The sqlite3_errstr(E) interface returns the English-language text
4298 ** that describes the [result code] E, as UTF-8, or NULL if E is not an
4308 ** SQL, then the sqlite3_error_offset() function returns -1.
4343 ** The life-cycle of a prepared statement object usually goes like this:
4358 ** CAPI3REF: Run-time Limits
4371 ** set at compile-time by a C preprocessor macro called
4380 ** simply invoke this interface with the third parameter set to -1.
4382 ** Run-time limits are intended for use in applications that manage
4395 ** New run-time limit categories may be added in future releases.
4400 ** CAPI3REF: Run-Time Limit Categories
4404 ** that can be lowered at run-time using [sqlite3_limit()].
4490 ** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4513 ** To execute an SQL statement, it must first be compiled into a byte-code
4522 ** The use of the UTF-8 interfaces is preferred, as SQLite currently
4523 ** does all parsing using UTF-8. The UTF-16 interfaces are provided
4524 ** as a convenience. The UTF-16 interfaces work by converting the
4525 ** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4532 ** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4534 ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4535 ** and sqlite3_prepare16_v3() use UTF-16.
4541 ** If the caller knows that the supplied string is nul-terminated, then
4544 ** the nul-terminator.
4594 ** ^The specific value of a WHERE-clause [parameter] might influence the
4595 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
4597 ** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
4609 const char *zSql, /* SQL statement, UTF-8 encoded */
4616 const char *zSql, /* SQL statement, UTF-8 encoded */
4623 const char *zSql, /* SQL statement, UTF-8 encoded */
4631 const void *zSql, /* SQL statement, UTF-16 encoded */
4638 const void *zSql, /* SQL statement, UTF-16 encoded */
4645 const void *zSql, /* SQL statement, UTF-16 encoded */
4656 ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4660 ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4663 ** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4679 ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4680 ** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4691 ** the [SQLITE_ENABLE_NORMALIZE] compile-time option is defined.
4703 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4707 ** Note that [application-defined SQL functions] or
4711 ** change the database file through side-effects:
4737 ** makes it a no-op, but the sqlite3_stmt_readonly() result would still
4739 ** read-only no-op if the table already exists, but
4799 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4836 ** sqlite3_value object. If SQLite is compiled to be single-threaded
4847 ** implementation of [application-defined SQL functions] are protected.
4865 ** is always first parameter to [application-defined SQL functions].
4866 ** The application-defined SQL function implementation will pass this
4916 ** it should be a pointer to well-formed UTF8 text.
4918 ** it should be a pointer to well-formed UTF16 text.
4920 ** it should be a pointer to a well-formed unicode string that is
4924 ** [[byte-order determination rules]] ^The byte-order of
4925 ** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
4942 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4982 ** ^A negative value for the zeroblob results in a zero-length BLOB.
4984 ** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
5056 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
5067 ** ^If the value N is out of range or if the N-th parameter is
5069 ** always in UTF-8 encoding even if the named parameter was
5070 ** originally specified as UTF-16 in [sqlite3_prepare16()],
5087 ** name must be given in UTF-8 even if the original statement
5088 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5129 ** interface returns a pointer to a zero-terminated UTF-8 string
5130 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
5131 ** UTF-16 string. ^The first parameter is the [prepared statement]
5142 ** (for example during a conversion from UTF-8 to UTF-16) then a
5161 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5170 ** ^The names returned are the original un-aliased names of the
5176 ** ^The left-most column is column 0 for these routines.
5185 ** UTF-16 encoded strings and the other functions return UTF-8.
5188 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5212 ** ^The returned string is always UTF-8 encoded.
5225 ** ^SQLite uses dynamic run-time typing. ^So just because a column
5275 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5301 ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5331 ** will return non-zero if previous call to [sqlite3_step](P) returned
5333 ** where it always returns zero since each step of that multi-step
5347 ** <li> 64-bit signed integer
5348 ** <li> 64-bit IEEE floating point number
5381 ** <tr><td><b>sqlite3_column_int</b><td>→<td>32-bit INTEGER result
5382 ** <tr><td><b>sqlite3_column_int64</b><td>→<td>64-bit INTEGER result
5383 ** <tr><td><b>sqlite3_column_text</b><td>→<td>UTF-8 TEXT result
5384 ** <tr><td><b>sqlite3_column_text16</b><td>→<td>UTF-16 TEXT result
5389 ** or a UTF-8 TEXT result in bytes
5391 ** <td>→ <td>Size of UTF-16
5443 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5445 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5446 ** the string to UTF-8 and then returns the number of bytes.
5448 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5452 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5454 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5455 ** the string to UTF-16 and then returns the number of bytes.
5457 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5468 ** even empty strings, are always zero-terminated. ^The return
5469 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5485 ** [application-defined SQL functions] or [virtual tables], not within
5486 ** top-level application code.
5504 ** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5525 ** sqlite3_column_text16() is called. A zero-terminator might
5527 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5529 ** to UTF-16.</li>
5530 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5532 ** to UTF-8.</li>
5535 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5566 ** fail if an out-of-memory error occurs during a format conversion.
5567 ** Only the following subset of interfaces are subject to out-of-memory
5578 ** If an out-of-memory error occurs, then the return value from these
5580 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
5614 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5629 ** object back to its initial state, ready to be re-executed.
5681 ** connection then application-defined SQL functions must be added
5685 ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5686 ** representation, exclusive of the zero-terminator. ^Note that the name
5687 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5693 ** aggregate takes. ^If this parameter is -1, then the SQL function or
5696 ** parameter is less than -1 or greater than 127 then the behavior is
5715 ** deterministic. The built-in [random()] SQL function is an example of a
5726 ** all application-defined SQL functions that do not need to be
5740 ** pointers to C-language functions that implement the SQL function or
5750 ** C-language callbacks that implement the new function. xStep and xFinal
5751 ** must both be non-NULL. xValue and xInverse may either both be NULL, in
5753 ** non-NULL, in which case the new function may be used as either an aggregate
5756 ** [user-defined window functions|available here].
5771 ** SQL function is used. ^A function implementation with a non-negative
5780 ** ^Built-in functions may be overloaded by new application-defined functions.
5782 ** ^An application-defined function is permitted to call other
5837 #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
5838 #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
5839 #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
5866 ** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
5871 ** [application-defined SQL function]
5872 ** that has side-effects or that could potentially leak sensitive information.
5875 ** modified to invoke the application-defined function in ways that are
5879 ** [application-defined SQL functions], regardless of whether or not they
5883 ** that do not have access to the application-defined functions.
5902 ** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions
5904 ** SQLITE_INNOCUOUS flag for application-defined functions unless the
5906 ** security-adverse side-effects and information-leaks.
5911 ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5912 ** This flag instructs SQLite to omit some corner-case optimizations that
5918 ** a non-zero subtype was specified by the function argument expression.
5922 ** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
5926 ** might become a no-op if the function is used as term in an
5968 ** <tr><td><b>sqlite3_value_int</b><td>→<td>32-bit INTEGER value
5969 ** <tr><td><b>sqlite3_value_int64</b><td>→<td>64-bit INTEGER value
5971 ** <tr><td><b>sqlite3_value_text</b><td>→<td>UTF-8 TEXT value
5972 ** <tr><td><b>sqlite3_value_text16</b><td>→<td>UTF-16 TEXT value in
5974 ** <tr><td><b>sqlite3_value_text16be</b><td>→<td>UTF-16be TEXT value
5975 ** <tr><td><b>sqlite3_value_text16le</b><td>→<td>UTF-16le TEXT value
5978 ** or a UTF-8 TEXT in bytes
5980 ** <td>→ <td>Size of UTF-16
5998 ** implement [application-defined SQL functions] and [virtual tables].
6008 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
6009 ** in the native byte-order of the host machine. ^The
6011 ** extract UTF-16 strings as big-endian and little-endian respectively.
6052 ** ^The sqlite3_value_frombind(X) interface returns non-zero if the
6067 ** fail if an out-of-memory error occurs during a format conversion.
6068 ** Only the following subset of interfaces are subject to out-of-memory
6081 ** If an out-of-memory error occurs, then the return value from these
6083 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
6131 ** an [application-defined SQL function] argument V. The subtype
6136 ** Every [application-defined SQL function] that invoke this interface
6157 ** then sqlite3_value_free(V) is a harmless no-op.
6218 ** the application-defined function is running.
6238 ** These functions may be used by (non-aggregate) SQL functions to
6243 ** regular-expression matching function. The compiled version of the regular
6251 ** value to the application-defined function. ^N is zero for the left-most
6257 ** N-th argument of the application-defined function. ^Subsequent
6283 ** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
6289 ** function parameters that are compile-time constants, including literal
6292 ** The value of the N parameter to these interfaces should be non-negative.
6317 ** If P and X are both non-NULL, then the destructor X is invoked with
6320 ** <li> An out-of-memory error occurs during the call to
6376 #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6392 ** an application-defined function to be the BLOB whose content is pointed
6397 ** interfaces set the result of the application-defined function to be
6401 ** an application-defined function to be a floating point value specified
6409 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
6410 ** interprets the string from sqlite3_result_error16() as UTF-16 using
6411 ** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6416 ** sqlite3_result_error16() is non-negative then SQLite takes that many
6434 ** of the application-defined function to be the 32-bit signed integer
6437 ** of the application-defined function to be the 64-bit signed integer
6441 ** of the application-defined function to be NULL.
6445 ** set the return value of the application-defined function to be
6446 ** a text string which is represented as UTF-8, UTF-16 native byte order,
6447 ** UTF-16 little endian, or UTF-16 big endian, respectively.
6449 ** application-defined function to be a text string in an encoding
6459 ** is non-negative, then as many bytes (not characters) of the text
6460 ** pointed to by the 2nd parameter are taken as the application-defined
6461 ** function result. If the 3rd parameter is non-negative, then it
6468 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6484 ** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6486 ** byte-order specified by the BOM. ^The byte-order specified by
6487 ** the BOM at the beginning of the text overrides the byte-order
6490 ** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6501 ** the application-defined function to be a copy of the
6512 ** also associates the host-language pointer P or type T with that
6514 ** [application-defined SQL function] using [sqlite3_value_pointer()].
6522 ** than the one containing the application-defined function that received
6554 ** the result from the [application-defined SQL function] with
6561 ** Every [application-defined SQL function] that invokes this interface
6569 ** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
6572 ** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
6584 ** ^The name of the collation is a UTF-8 string
6586 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6649 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6692 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6693 ** the names are passed as UTF-16 in machine native byte order.
6694 ** ^A call to either function replaces the existing collation-needed callback.
6760 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6802 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6803 ** TemporaryFolder->Path->Data();
6806 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6819 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6858 ** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6863 ** the current directory on the sub-platforms of Win32 where that concept is
6867 ** UTF-8 or UTF-16, respectively.
6886 ** CAPI3REF: Test For Auto-Commit Mode
6890 ** ^The sqlite3_get_autocommit() interface returns non-zero or
6894 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6896 ** If certain kinds of errors occur on a statement within a multi-statement
6927 ** for the N-th database on database connection D, or a NULL pointer of N is
6929 ** the "temp" schema. Larger values of N correspond to various ATTACH-ed
6937 ** remember the string long-term should make their own copy. Applications that
6939 ** threads should mutex-protect calls to this API and should make their own
6951 ** connection D, or if database N is a temporary or in-memory database, then
6956 ** is [DETACH]-ed or until the database connection closes.
6977 ** CAPI3REF: Determine if a database is read-only
6981 ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
7000 ** a valid schema, then -1 is returned.
7066 ** ^If the callback on a commit hook function returns non-zero,
7088 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
7090 ** hook returning non-zero, just as it would be with any other rollback.
7110 ** the schema-name of the attached database that is being autovacuumed,
7118 ** <p>^If there are multiple ATTACH-ed database files that are being
7239 ** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE]
7240 ** compile-time option is recommended because the
7266 ** shared cache mode should be enabled per-database connection via
7270 ** 32-bit integer is atomic.
7272 ** See Also: [SQLite Shared-Cache Mode]
7280 ** of heap memory by deallocating non-essential memory allocations
7282 ** pages to improve performance is an example of non-essential memory.
7285 ** ^The sqlite3_release_memory() routine is a no-op returning zero
7299 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
7334 ** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
7358 ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7359 ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7392 ** interface returns SQLITE_OK and fills in the non-NULL pointers in
7396 ** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7463 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7475 ** with various operating-system specific extensions added.
7484 ** X is consists of the lower-case equivalent of all ASCII alphabetic
7523 ** [extension loading] while evaluating user-entered SQL, the following API
7531 ** ^This interface enables or disables both the C-API
7534 ** to enable or disable only the C-API.)^
7574 ** on the list of automatic extensions is a harmless no-op. ^No entry point
7676 ** inputs to xBestIndex and are read-only. xBestIndex inserts its
7688 ** expr on the right-hand side can be evaluated (and thus the constraint
7703 ** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7709 ** non-zero.
7713 ** the right-hand side of the corresponding aConstraint[] is evaluated
7714 ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7743 ** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7774 int iColumn; /* Column constrained. -1 for ROWID */
7777 int iTermOffset; /* Used internally - xBestIndex should ignore */
7819 ** ^The left-hand operand of the operator is given by the corresponding
7820 ** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
7823 ** operators have no left-hand operand, and so for those operators the
7832 ** The right-hand operands for each constraint might be accessible using
7833 ** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
7835 ** in the input SQL. If the right-hand operand is another column or an
7839 ** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
7844 ** the [sqlite3_vtab_collation()] interface. For most real-world virtual
8039 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
8041 ** read-only access.
8077 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
8093 ** and the built-in [zeroblob] SQL function may be used to create a
8094 ** zero-filled blob to read or write using the incremental-blob interface.
8124 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
8146 ** ^If the blob handle being closed was opened for read-write access, and if
8147 ** the database is in auto-commit mode and there are no other open read-write
8155 ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
8182 ** caller-supplied buffer. N bytes of data are copied into buffer Z
8211 ** caller-supplied buffer. N bytes of data are copied from the buffer Z
8260 ** ^Names are zero-terminated UTF-8 strings.
8291 ** is selected automatically at compile-time. The following
8302 ** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
8307 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
8344 ** cases where it really needs one. If a faster non-recursive mutex
8393 ** then any of the four routines behaves as a no-op.
8406 ** An instance of this structure defines the low-level routines
8456 ** xMutexInit() must be no-ops.
8502 ** the routine should return 1. This seems counter-intuitive since
8506 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
8522 ** next. Applications that override the built-in mutex logic must be
8538 #define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
8553 ** ^If the [threading mode] is Single-thread or Multi-thread then this
8559 ** CAPI3REF: Low-Level Control Of Database Files
8674 ** by enclosing in double-quotes) so as not to confuse the parser.
8679 ** The sqlite3_keyword_name(N,Z,L) interface finds the 0-based N-th keyword and
8682 ** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8688 ** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8700 ** <li> Put all identifier names inside double-quotes. This is the official
8711 ** compile-time options. For example, "VACUUM" is not a keyword if
8712 ** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8723 ** An instance of the sqlite3_str object contains a dynamically-sized
8747 ** valid [sqlite3_str] object, though in the event of an out-of-memory
8787 ** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8792 ** onto the end of the [sqlite3_str] object X. N must be non-negative.
8793 ** S must contain at least N non-zero bytes of content. To append a
8794 ** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8798 ** zero-terminated string S onto the end of [sqlite3_str] object X.
8801 ** single-byte character C onto the end of [sqlite3_str] object X.
8827 ** [SQLITE_NOMEM] following any out-of-memory error, or
8834 ** zero-termination byte.
8869 ** SQLITE_OK on success and a non-zero [error code] on failure.
8872 ** be represented by a 32-bit integer, then the values returned by
8890 ** These integer constants designate various run-time status parameters
8898 ** and internal memory usage by the SQLite library. Auxiliary page-cache
8983 ** non-zero [error code] on failure.
8999 ** The [sqlite3_db_status()] interface will return a non-zero error code
9009 ** satisfied using lookaside memory. Only the high-water value is meaningful;
9017 ** Only the high-water value is meaningful;
9025 ** Only the high-water value is meaningful;
9048 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
9088 ** to disk all at once. When pages spill mid-transaction, that introduces
9159 ** A non-zero value in this counter may indicate an opportunity to
9165 ** A non-zero value in this counter may indicate an opportunity to
9194 ** step was bypassed because a Bloom filter returned not-found. The
9264 ** The built-in page cache is recommended for most uses.
9279 ** built-in default page cache is used instead of the application defined
9311 ** false if it is used for an in-memory database. The cache implementation
9322 ** suggested maximum cache-size (number of pages stored by) the cache
9368 ** as its second argument. If the third parameter, discard, is non-zero,
9402 ** is not obligated to free any memory, but well-behaved implementations should
9461 ** for copying in-memory databases to or from persistent files.
9467 ** ^The source database is read-locked only while it is being read;
9501 ** there is already a read or read-write transaction open on the
9533 ** <li> the destination database was opened read-only, or
9534 ** <li> the destination database is using write-ahead-log journaling
9536 ** <li> the destination database is an in-memory database and the
9540 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9541 ** the [sqlite3_busy_handler | busy-handler function]
9543 ** busy-handler returns non-zero before the lock is available, then
9564 ** sqlite3_backup_step(), the source database may be modified mid-way
9581 ** active write-transaction on the destination database is rolled back.
9588 ** ^If an out-of-memory condition or IO error occurred during any prior
9657 ** ^When running in shared-cache mode, a database operation may fail with
9658 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9659 ** individual tables within the shared-cache cannot be obtained. See
9660 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9664 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9668 ** ^Shared-cache locks are released when a database connection concludes
9672 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9682 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9688 ** ^If the blocked connection is attempting to obtain a write-lock on a
9689 ** shared-cache table, and more than one other connection currently holds
9690 ** a read-lock on the same table, then SQLite arbitrarily selects one of
9693 ** ^(There may be at most one unlock-notify callback registered by a
9695 ** blocked connection already has a registered unlock-notify callback,
9698 ** unlock-notify callback is canceled. ^The blocked connections
9699 ** unlock-notify callback may also be canceled by closing the blocked
9702 ** The unlock-notify callback is not reentrant. If an application invokes
9703 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
9711 ** When an unlock-notify callback is registered, the application provides a
9715 ** an unlock-notify callback is a pointer to an array of void* pointers,
9719 ** more than one blocked connection that has registered for an unlock-notify
9729 ** Assuming that after registering for an unlock-notify callback a
9740 ** unlock-notify callback is registered. The system is said to be in
9741 ** a deadlocked state if connection A has registered for an unlock-notify
9743 ** B has itself registered for an unlock-notify callback when connection
9746 ** registered for an unlock-notify callback on the conclusion of connection
9758 ** sqlite3_unlock_notify() results in the unlock-notify callback being
9759 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
9779 ** and extensions to compare the contents of two buffers containing UTF-8
9780 ** strings in a case-independent fashion, using the same definition of "case
9796 ** Note that this routine returns zero on a match and non-zero if the strings
9813 ** insensitive - equivalent upper and lower case ASCII characters match
9819 ** Note that this routine returns zero on a match and non-zero if the strings
9843 ** a fixed-length buffer on the stack. If the log message is longer than
9850 ** CAPI3REF: Write-Ahead Log Commit Hook
9857 ** the associated write-lock on the database released)^, so the implementation
9863 ** ^The third parameter is the name of the database that was written to -
9864 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9865 ** is the number of pages currently in the write-ahead log file,
9876 ** A single database handle may have at most a single write-ahead log callback
9878 ** previously registered write-ahead log callback. ^The return value is
9891 ** CAPI3REF: Configure an auto-checkpoint
9898 ** more frames in the [write-ahead log] file. ^Passing zero or
9913 ** ^Every new [database connection] defaults to having the auto-checkpoint
9929 ** [write-ahead log] for database X on [database connection] D to be
9930 ** transferred into the database file and for the write-ahead log to
9956 ** in the log were checkpointed. ^The [busy-handler callback]
9963 ** [sqlite3_busy_handler|busy-handler callback]) until there is no
9972 ** [busy-handler callback])
9985 ** the log file or to -1 if the checkpoint could not run because
9989 ** was called) or to -1 if the checkpoint could not run due to an error or
9997 ** busy-handler configured, it will not be invoked in this case.
10001 ** obtained immediately, and a busy-handler is configured, it is invoked and
10002 ** the writer lock retried until either the busy-handler returns 0 or the lock
10003 ** is successfully obtained. ^The busy-handler is also invoked while waiting for
10004 ** database readers as described above. ^If the busy-handler returns 0 before
10007 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
10023 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
10099 ** If X is non-zero, then the virtual table implementation guarantees
10143 ** all schemas ("main", "temp", and any ATTACH-ed databases) whenever the
10174 ** [xUpdate] method understands as a "no-change" value.
10336 ** on the right-hand side of the IN operator.)^ Thus the virtual table
10337 ** only sees a single value from the right-hand side of the IN operator
10341 ** table to see all values on the right-hand of the IN operator all at
10346 ** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero)
10347 ** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint
10349 ** sqlite3_vtab_in() with -1 in the third argument is a mechanism
10350 ** by which the virtual table can ask SQLite if all-at-once processing
10356 ** the IN operator all-at-once, respectively. ^Thus when the third
10357 ** parameter (F) is non-negative, this interface is the mechanism by
10366 ** (non-zero), that means that the constraint is an IN operator
10367 ** that can be processed all-at-once. ^If the constraint is not an IN
10368 ** operator or cannot be processed all-at-once, then the interface returns
10371 ** ^(All-at-once processing of the IN operator is selected if both of the
10375 ** <li><p> The P->aConstraintUsage[N].argvIndex value is set to a positive
10377 ** use the N-th constraint.
10380 ** non-negative had F>=1.
10384 ** the traditional one-at-a-time processing strategy for the IN constraint.
10385 ** ^If both conditions are true, then the argvIndex-th parameter to the
10388 ** [sqlite3_vtab_in_next()] to find all values on the right-hand side
10394 ** CAPI3REF: Find all elements on the right-hand side of an IN constraint.
10404 ** a parameter that was previously selected for all-at-once IN constraint
10407 ** an xFilter argument that was selected for all-at-once IN constraint
10410 ** ^(Use these routines to access all values on the right-hand side
10452 ** J being a 0-based index into P->aConstraint[], then this routine
10453 ** attempts to set *V to the value of the right-hand operand of
10454 ** that constraint if the right-hand operand is known. ^If the
10455 ** right-hand operand is not known, then *V is set to a NULL pointer.
10458 ** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
10464 ** the right-hand operand of a constraint is a literal value in the original
10465 ** SQL statement. If the right-hand operand is an expression or a reference
10470 ** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such
10479 ** "Right-Hand Side".
10514 ** not available, the output variable is set to -1 if the value is numeric,
10520 ** set to the total number of times that the X-th loop has run.</dd>
10524 ** to the total number of rows examined by all iterations of the X-th loop.</dd>
10529 ** iteration of the X-th loop. If the query planner's estimates was accurate,
10536 ** to a zero-terminated UTF-8 string containing the name of the index or table
10537 ** used for the X-th loop.
10541 ** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
10542 ** description for the X-th loop.
10546 ** id for the X-th query plan element. The id value is unique within the
10547 ** statement. The select-id is the same value as is output in the first
10558 ** according to the processor time-stamp counter, that elapsed while the
10560 ** all query elements - if it is unavailable the output variable is
10561 ** set to -1.
10584 ** compile-time option.
10592 ** one flag is defined - SQLITE_SCANSTAT_COMPLEX. If SQLITE_SCANSTAT_COMPLEX
10602 ** for. Query elements are numbered starting from zero. A value of -1 may be
10604 ** - less than -1 or greater than or equal to the total number of query
10605 ** elements used to implement the statement - a non-zero value is returned and
10631 ** CAPI3REF: Zero Scan-Status Counters
10636 ** This API is only available if the library is built with pre-processor
10642 ** CAPI3REF: Flush caches to disk mid-transaction
10645 ** ^If a write-transaction is open on [database connection] D when the
10647 ** pages in the pager-cache that are not currently in use are written out
10651 ** interface flushes caches for all schemas - "main", "temp", and
10656 ** immediately and there is a busy-handler callback configured, it is invoked
10664 ** example an IO error or out-of-memory condition), then processing is
10675 ** CAPI3REF: The pre-update hook.
10679 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10755 ** operation; or 1 for inserts, updates, or deletes invoked by top-level
10756 ** triggers; or 2 for changes resulting from triggers called by top-level
10760 ** the pre-update hook is invoked with SQLITE_DELETE. This is because the
10765 ** pre-update hook is being invoked for some other reason, including a
10766 ** regular DELETE, sqlite3_preupdate_blobwrite() returns -1.
10792 ** CAPI3REF: Low-level system error code
10797 ** The return value is OS-dependent. For example, on unix systems, after
10837 ** If there is not already a read-transaction open on schema S when
10869 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10903 ** is returned. If another error code - for example SQLITE_PROTOCOL or an
10904 ** SQLITE_IOERR error code - is returned, then the final state of the
10918 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10935 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
11002 ** For an ordinary on-disk database file, the serialization is just a
11003 ** copy of the disk file. For an in-memory database or a "TEMP" database,
11047 ** Zero or more of the following constants can be OR-ed together for
11051 ** a pointer to contiguous in-memory database that it is currently using,
11053 ** a contiguous in-memory database, then this option causes
11055 ** using a contiguous in-memory database if it has been initialized by a
11065 ** reopen S as an in-memory database based on the serialization contained
11069 ** permitted to add content to the in-memory database as long as the total
11131 ** should be treated as read-only.
11135 #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
11188 /* The double-precision datatype used by RTree depends on the
11189 ** SQLITE_RTREE_INT_ONLY compile-time option.
11199 ** R-Tree geometry query as follows:
11224 ** Register a 2nd-generation geometry callback named zScore that can be
11225 ** used as part of an R-Tree geometry query as follows:
11331 ** is not possible for an application to register a pre-update hook on a
11334 ** which a pre-update hook is already defined. The results of attempting
11415 ** disabled - it does not. A newly created session object is enabled.
11422 ** no-op, and may be used to query the current state of the session.
11499 ** rows a zero-length blob (SQL value X'') is stored in the changeset or
11505 ** zero-length blob back to a NULL value when updating the sqlite_stat1
11509 ** conflict-handler callback) then the X'' value is returned. The application
11557 ** database row along with the updated values for each updated non-primary-key
11566 ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
11568 ** with non-NULL PRIMARY KEY values is updated so that one or more of its
11581 ** are sorted in the same order in which they were attached (or auto-attached)
11595 ** recorded once - the first time a row with said primary key is inserted,
11602 ** The session object therefore accumulates two types of records - those
11620 ** found and one or more of the non-primary key fields have been
11637 ** This may appear to have some counter-intuitive effects if a single row
11653 ** CAPI3REF: Return An Upper-limit For The Size Of The Changeset
11675 ** does not have a primary key, this function is a no-op (but does not return
11695 ** used to update the table in database zFrom (call this the "from-table")
11697 ** object (call this the "to-table"). Specifically:
11700 ** <li> For each row (primary key) that exists in the to-table but not in
11701 ** the from-table, an INSERT record is added to the session object.
11703 ** <li> For each row (primary key) that exists in the to-table but not in
11704 ** the from-table, a DELETE record is added to the session object.
11707 ** different non-PK values in each, an UPDATE record is added to the
11752 ** Because the non-primary key "old.*" fields are omitted, no
11771 ** Return non-zero if no changes to attached tables have been recorded by
11779 ** are restored. However, if this function returns non-zero, then it is
11866 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11890 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11903 ** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
11906 ** or until the conflict-handler function returns.
11953 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
11962 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11993 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
12027 ** conflict-handler callback by [sqlite3changeset_apply()] with either
12038 ** "conflicting row" associated with the current conflict-handler callback
12076 ** function with an iterator passed to a conflict-handler by
12084 ** returned. This is to allow the following pattern (pseudo-code):
12281 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
12282 ** <tr><th style="white-space:pre">Existing Change </th>
12283 ** <th style="white-space:pre">New Change </th>
12333 ** detected, SQLITE_CORRUPT is returned. Or, if an out-of-memory condition
12415 ** Otherwise, if the return value is non-zero or the xFilter argument to
12451 ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
12455 ** returned by each invocation of the conflict-handler function. Refer to
12464 ** stored in all non-primary key columns also match the values stored in
12468 ** the non-primary key fields contains a value different from the original
12469 ** row value stored in the changeset, the conflict-handler function is
12472 ** only the values of those non-primary key fields are compared against
12473 ** the current database contents - any trailing database table columns
12477 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12482 ** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
12501 ** This includes the case where the INSERT operation is re-attempted because
12509 ** stored in all modified non-primary key columns also match the values
12513 ** the modified non-primary key fields contains a value different from an
12514 ** original row value stored in the changeset, the conflict-handler function
12516 ** UPDATE changes only contain values for non-primary key fields that are
12518 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
12521 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12525 ** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
12543 ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
12641 ** Values that may be passed as the second argument to a conflict-handler.
12648 ** (non primary-key) fields modified by the update do not contain the
12809 ** combined on a per-field basis, not per-row. This means that in the
12893 ** corresponding non-streaming API functions:
12895 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
12896 ** <tr><th>Streaming function<th>Non-streaming equivalent</th>
12906 ** Non-streaming functions that accept changesets (or patchsets) as input
12911 ** low-memory environment is required to handle very large changesets, the
13175 ** non-negative but less than the number of columns in the table, return
13190 ** non-negative but less than the number of columns in the table, set
13207 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
13312 ** If the bClear argument is non-zero, then the auxiliary data is cleared
13334 ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
13336 ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
13342 ** modify this structure directly - it should only be used as shown above
13350 ** through an empty set (all calls to xPhraseFirst() set iCol to -1).
13364 ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
13366 ** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
13375 ** xPhraseFirstColumn() set iCol to -1).
13484 ** The second and third arguments are an array of nul-terminated strings
13502 ** by argument pText. pText may or may not be nul-terminated. The first
13510 ** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
13515 ** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
13519 ** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
13524 ** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
13557 ** built-in tokenizers, the FTS5 query 'first + place' will match instances
13591 ** still appears to contain just two phrases - "(first OR 1st)"
13602 ** when tokenizing query text (it should not - to do so would be
13649 ** provide synonyms for prefixes). However, a non-prefix query like '1st'
13750 ** autoconf-based build
13778 ** The hard limit is the ability of a 32-bit signed integer
13779 ** to count the size: 2^31-1 or 2147483647.
13797 ** tell you that in a well-normalized database, you usually should
13855 ** The suggested maximum number of in-memory pages to use for
13858 ** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
13860 ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
13861 ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
13864 # define SQLITE_DEFAULT_CACHE_SIZE -2000
13878 ** counted using a signed 8-bit integer which has a maximum value of 127
13890 ** as a signed 32-bit integer can hold.
13897 ** imposed by the use of 16-bit offsets within each page.
13903 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
13904 ** compiled with the default page-size limit will not be able to rollback
13927 ** device characteristics (sector-size and atomic write() support),
13944 ** This value can be lowered (or raised) at run-time using that the
13975 #pragma warn -rch /* unreachable code */
13976 #pragma warn -ccc /* Condition is always true or false */
13977 #pragma warn -aus /* Assigned value is never used */
13978 #pragma warn -csu /* Comparing signed and unsigned */
13979 #pragma warn -spa /* Suspicious pointer arithmetic */
13987 # define __has_extension(x) 0 /* compatibility with non-clang compilers */
14020 ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
14033 # define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
14034 #else /* Generates a warning - but it always works */
14079 ** SEH support if the -DSQLITE_OMIT_SEH option is given.
14089 ** disables it using -DSQLITE_DIRECT_OVERFLOW_READ=0
14092 /* Disable if -DSQLITE_DIRECT_OVERFLOW_READ=0 */
14104 ** level of threadsafety. 2 means the library is multithreaded - multiple
14112 ** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
14120 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
14126 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
14133 ** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
14162 # error "Two or more of the following compile-time configuration options\
14200 ** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
14265 ** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage
14277 ** of SQLite to unexpected behavior - to make the code "self-healing"
14283 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
14371 ** Return true (non-zero) if the input is an integer that is too large
14372 ** to fit in 32-bits. This macro is used inside of various testcase()
14373 ** macros to verify that we have tested SQLite for large-file support.
14400 ** This is the header file for the generic hash-table implementation
14411 ** The internals of this structure are intended to be opaque -- client
14418 ** All elements of the hash table are on a single doubly-linked list.
14422 ** the global doubly-linked list. The contents of the bucket are the
14423 ** element pointed to plus the next _ht.count-1 elements in the list.
14442 ** structure. All elements are stored on a single doubly-linked list.
14473 #define sqliteHashFirst(H) ((H)->first)
14474 #define sqliteHashNext(E) ((E)->next)
14475 #define sqliteHashData(E) ((E)->data)
14476 /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
14477 /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
14482 #define sqliteHashCount(H) ((H)->count)
14693 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
14698 ** substitute integer for floating-point
14729 ** the VDBE-level file format changes. The following macros define the
14740 ** changed at run-time using a pragma.
14748 ** on the command-line
14778 ** of -1024*N bytes is allocated and used for as many pages as it will hold.
14780 ** The default value of "20" was chosen to minimize the run-time of the
14781 ** speedtest1 test program with options: --shrink-memory --reprepare
14795 ** The compile-time options SQLITE_MMAP_READWRITE and
14808 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
14839 ** types can be conveniently redefined at compile-type. Like this:
14841 ** cc '-DUINTPTR_TYPE=long long int' ...
14881 typedef sqlite_int64 i64; /* 8-byte signed integer */
14882 typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
14883 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
14884 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
14885 typedef INT16_TYPE i16; /* 2-byte signed integer */
14886 typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
14887 typedef INT8_TYPE i8; /* 1-byte signed integer */
14895 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
14904 ** Estimated quantities used for query planning are stored as 16-bit
14906 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
14915 ** 1 -> 0 20 -> 43 10000 -> 132
14916 ** 2 -> 10 25 -> 46 25000 -> 146
14917 ** 3 -> 16 100 -> 66 1000000 -> 199
14918 ** 4 -> 20 1000 -> 99 1048576 -> 200
14919 ** 10 -> 33 1024 -> 100 4294967296 -> 320
14924 ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
14967 ** if the sub-buffer S..E-1 overflows the buffer whose last byte is P-1.
14972 ** |-----------------| FALSE
14973 ** |-------|
14977 ** |-----------------|
14978 ** |-------| TRUE
14982 ** |-----------------|
14983 ** |-------| FALSE
14990 ** and whether or not that determination is run-time or compile-time.
14992 ** For best performance, an attempt is made to guess at the byte-order
14993 ** using C-preprocessor macros. If that is unsuccessful, or if
14994 ** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
14995 ** at run-time.
15000 ** -DSQLITE_BYTEORDER=1234
15004 ** -DSQLITE_BYTEORDER=4321
15006 ** to cause the build to work for little-endian or big-endian processors,
15009 #ifndef SQLITE_BYTEORDER /* Replicate changes at tag-20230904a */
15047 ** Constants for the largest and smallest possible 64-bit signed integers.
15048 ** These macros are designed to work correctly on both 32-bit and 64-bit
15053 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
15057 ** to force 8-byte alignment on 64-bit architectures.
15062 ** pointers in size, and so it is a no-op on systems where the pointer
15078 ** Assert that the pointer X is aligned to an 8-byte boundary. This
15083 ** underlying malloc() implementation might return us 4-byte aligned
15084 ** pointers. In that case, only verify 4-byte alignment.
15087 # define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&3)==0)
15089 # define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&7)==0)
15101 ** Default maximum size of memory used by memory-mapped I/O in the VFS
15121 ** default MMAP_SIZE is specified at compile-time, make sure that it does
15145 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
15157 ** 0x00000008 Result-set wildcard expansion
15163 ** 0x00000200 Count-of-view optimization
15168 ** 0x00004000 Push-down optimization
15169 ** 0x00008000 After all FROM-clause analysis
15191 ** (---any--) Top-level block structure
15192 ** 0x-------F High-level debug messages
15193 ** 0x----FFF- More detail
15194 ** 0xFFFF---- Low-level debug messages
15220 ** An instance of the following structure is used to store the busy-handler
15225 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
15259 ** The root-page of the schema table.
15278 #define IsPowerOfTwo(X) (((X)&((X)-1))==0)
15297 ** directly, we use its constant as a key to lookup the run-time allocated
15299 ** for the run-time allocated buffer.
15302 ** macros become no-ops and have zero performance impact.
15403 ** Changing this from a 64-bit to a 32-bit type limits the number of
15425 #define ALLBITS ((Bitmask)-1)
15426 #define TOPBIT (((Bitmask)1)<<(BMS-1))
15455 ** This header file (together with is companion C source-code file
15459 ** This header file is #include-ed by sqliteInt.h and thus ends up
15467 ** necessary pre-processor macros for it.
15483 ** This file contains pre-processor directives related to operating system
15504 ** If SQLITE_OS_OTHER=1 is specified at compile-time, then the application
15567 ** a no-op
15600 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
15602 ** 2006-10-31: The default prefix used to be "sqlite_". But then
15603 ** Mcafee started using SQLite in their anti-virus product and it
15649 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
15757 ** sqlite3_malloc() to obtain space for the file-handle structure.
15793 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
15816 ** is devoted to storing a super-journal name - there are no more pages to
15820 #define PAGER_SJ_PGNO_COMPUTED(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
15821 #define PAGER_SJ_PGNO(x) ((x)->lckPgno)
15829 #define PAGER_MEMORY 0x0002 /* In-memory database */
15834 #define PAGER_LOCKINGMODE_QUERY -1
15845 #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
15850 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
15851 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
15857 #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
15879 ** that make up the Pager sub-system API. See source code comments for
16031 ** This header file defines the interface that the sqlite B-Tree file
16044 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
16051 #define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
16052 #define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
16065 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
16080 #define BTREE_MEMORY 2 /* This is an in-memory DB */
16081 #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
16135 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
16138 ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
16141 #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
16142 #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
16162 ** For example, the free-page-count field is located at byte offset 36 of
16163 ** the database file header. The incr-vacuum-flag field is located at
16167 ** It is a read-only number computed by the pager. But we merge it with
16180 #define BTREE_DATA_VERSION 15 /* A virtual meta-value */
16194 ** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
16195 ** column of the b-tree of the cursor. The Expr tree will not contain
16196 ** any function calls nor subqueries nor references to b-trees other than
16199 ** The design of the _RANGE hint is aid b-tree implementations that try
16200 ** to prefetch content from remote machines - to provide those
16225 #define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
16230 ** For read-only cursors the wrFlag argument is always zero. For read-write
16241 ** by this, the native b-tree engine of SQLite, but it is available to
16243 ** b-tree system. For alternative storage engines in which a delete of
16247 ** and DELETE operations as no-ops, and any READ operation against a
16250 #define BTREE_WRCSR 0x00000004 /* read-write cursor */
16256 int wrFlag, /* 1 for writing. 0 for read-only */
16402 ** Enter and Leave procedures no-ops.
16489 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
16516 u32 iSrcLine; /* Source-code line that generated this opcode
16528 ** A sub-routine used to implement a trigger program.
16531 VdbeOp *aOp; /* Array of opcodes for sub-program */
16537 SubProgram *pNext; /* Next sub-program already visited */
16557 #define P4_STATIC (-1) /* Pointer to a static string */
16558 #define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
16559 #define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
16560 #define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
16561 #define P4_TABLE (-5) /* P4 is a pointer to a Table structure */
16563 #define P4_FREE_IF_LE (-6)
16564 #define P4_DYNAMIC (-6) /* Pointer to memory from sqliteMalloc() */
16565 #define P4_FUNCDEF (-7) /* P4 is a pointer to a FuncDef structure */
16566 #define P4_KEYINFO (-8) /* P4 is a pointer to a KeyInfo structure */
16567 #define P4_EXPR (-9) /* P4 is a pointer to an Expr tree */
16568 #define P4_MEM (-10) /* P4 is a pointer to a Mem* structure */
16569 #define P4_VTAB (-11) /* P4 is a pointer to an sqlite3_vtab structure */
16570 #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
16571 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
16572 #define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */
16573 #define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */
16574 #define P4_TABLEREF (-16) /* Like P4_TABLE, but reference counted */
16675 #define OP_IfPos 59 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
16676 #define OP_IfNotZero 60 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
16677 #define OP_DecrJumpZero 61 /* jump, synopsis: if (--r[P1])==0 goto P2 */
16706 #define OP_Compare 90 /* synopsis: r[P1@P3] <-> r[P2@P3] */
16723 #define OP_Subtract 107 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
16740 #define OP_SeekScan 124 /* synopsis: Scan-ahead up to P1 rows */
16776 #define OP_OffsetLimit 160 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) …
16857 ** Additional non-public SQLITE_PREPARE_* flags
16912 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
16917 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
16996 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
17033 ** VdbeCoverageNeverNull(v) // Previous three-way branch is only
17038 ** // in distinguishing equal and not-equal.
17041 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
17042 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
17050 ** is responsible for keeping track of this and reporting byte-code branches
17188 /* Modify the page-size after the cache has been created. */
17210 /* Change a page number. Used by incr-vacuum. */
17252 /* Set and get the suggested cache-size for the specified pager-cache.
17255 ** the total number of pages cached by purgeable pager-caches to the sum
17256 ** of the suggested cache-sizes.
17263 /* Set or get the suggested spill-size for the specified pager-cache.
17265 ** The spill-size is the minimum number of pages in cache before the cache
17329 ** at start-time.
17331 ** SQLITE_MUTEX_NOOP For single-threaded applications. No
17334 ** start-time.
17336 ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
17338 ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
17355 ** If this is a no-op implementation, implement everything as macros.
17376 /* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
17396 ** The "PRAGMA synchronous" statement also uses the zero-based numbers.
17397 ** In other words, the zero-based numbers are used for all external interfaces
17398 ** and the one-based values are used internally.
17426 ** the Schema for the TEMP database (sqlite3.aDb[1]) which is free-standing.
17455 ** Db.pSchema->flags field.
17457 #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
17458 #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
17459 #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
17460 #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
17463 ** Allowed values for the DB.pSchema->flags field.
17483 ** Lookaside malloc is a set of fixed-size buffers that can be used
17505 ** in a performance-critical path. sz should be set by to szTrue whenever
17510 ** come off of pFree first, then pInit as a fallback. This dual-list
17511 ** allows use to compute a high-water mark - the maximum number of allocations
17512 ** outstanding at any point in the past - by subtracting the number of
17515 ** Enhancement on 2019-12-12: Two-size-lookaside
17519 ** The two-size-lookaside enhancement breaks up the lookaside allocation
17520 ** into two pools: One of 128-byte slots and the other of the default size
17521 ** (1200-byte) slots. Allocations are filled from the small-pool first,
17522 ** failing over to the full-size pool if that does not work. Thus more
17539 void *pMiddle; /* First byte past end of full-size buffers and
17544 void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */
17550 #define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0
17551 #define EnableLookaside db->lookaside.bDisable--;\
17552 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
17554 /* Size of the smaller allocations in two-size lookaside */
17562 ** A hash table for built-in function definitions. (Application-defined
17577 See ext/userauth/user-auth.txt for details."
17649 u32 nSchemaLock; /* Do not reset the schema when non-zero */
17657 u8 autoCommit; /* The auto-commit flag. */
17661 u8 dfltLockMode; /* Default locking-mode for attached dbs */
17667 u8 noSharedCache; /* True if no shared-cache backends */
17715 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
17753 int nSavepoint; /* Number of non-transaction savepoints */
17754 int nStatement; /* Number of nested statement-transactions */
17784 #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
17785 #define ENC(db) ((db)->enc)
17790 ** C-compilers still do not accept LL integer literals.
17833 #define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/
17834 #define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/
17840 #define SQLITE_ReadUncommit HI(0x00004) /* READ UNCOMMITTED in shared-cache */
17857 #define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
17878 #define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */
17882 #define SQLITE_PushDown 0x00001000 /* WHERE-clause push-down opt */
17884 #define SQLITE_SkipScan 0x00004000 /* Skip-scans */
17892 #define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */
17897 #define SQLITE_Coroutines 0x02000000 /* Co-routines for subqueries */
17899 #define SQLITE_OnePass 0x08000000 /* Single-pass DELETE and UPDATE */
17905 #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
17906 #define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
17912 #define ConstFactorOk(P) ((P)->okConstFactor)
17927 ** structure. For global built-in functions (ex: substr(), max(), count())
17929 ** For per-connection application-defined functions, a pointer to this
17930 ** structure is held in the db->aHash hash table.
17932 ** The u.pHash field is used by the global built-ins. The u.pDestructor
17933 ** field is used by per-connection app-def functions.
17936 i8 nArg; /* Number of arguments. -1 means unlimited */
17940 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
17943 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
17952 ** This structure encapsulates a user-function destructor callback (as
17985 ** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS -- opposite meanings!!!
17992 ** See multiple instances of tag-20230109-1.
17996 #define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
17999 #define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
18000 #define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
18001 #define SQLITE_FUNC_BYTELEN 0x00c0 /* Built-in octet_length() function */
18002 #define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
18003 /* 0x0200 -- available for reuse */
18004 #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
18008 ** single query - might change over time */
18009 #define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
18011 #define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
18016 #define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
18017 #define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
18021 /* Identifier numbers for each in-line function */
18039 ** as the user-data (sqlite3_user_data()) for the function. If
18050 ** zName is the name of a function that is implemented by in-line
18056 ** zName is the name of a test-only function implemented by in-line
18065 ** a single query. The iArg is ignored. The user-data is always set
18069 ** For math-library functions. xPtr is an arbitrary pointer.
18074 ** ignored and the user-data for these functions is set to an
18075 ** arbitrary non-NULL pointer. The bNC parameter is not used.
18093 ** available as the function user-data (sqlite3_user_data()). The
18158 char *zName; /* Savepoint name (nul-terminated) */
18219 u16 iDflt; /* 1-based index of DEFAULT. 0 means "none" */
18250 #define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
18270 char *zName; /* Name of the collating sequence, UTF-8 encoded */
18282 #define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
18336 ** database connections, even when the rest of the in-memory database
18346 ** database schema are initially stored in a linked-list pointed to by
18353 ** When an in-memory Table object is deleted (for example when the
18404 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
18435 ** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
18436 ** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
18446 #define TF_Readonly 0x00000001 /* Read-only system table */
18456 #define TF_NoVisibleRowid 0x00000200 /* No user-visible "rowid" column */
18457 #define TF_OOOHidden 0x00000400 /* Out-of-Order hidden columns */
18472 #define IsView(X) ((X)->eTabType==TABTYP_VIEW)
18473 #define IsOrdinaryTable(X) ((X)->eTabType==TABTYP_NORM)
18481 # define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB)
18483 ((X)->op==TK_COLUMN && (X)->y.pTab->eTabType==TABTYP_VTAB)
18491 ** only works for non-virtual tables (ordinary tables and views) and is
18496 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18497 # define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18499 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18508 #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
18509 #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
18511 /* Macro is true if the SQLITE_ALLOW_ROWID_IN_VIEW (mis-)feature is
18533 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
18536 ** from-table == child-table
18537 ** to-table == parent-table
18540 ** which is attached to the from-table. The to-table need not exist when
18541 ** the from-table is created. The existence of the to-table is not checked.
18555 /* EV: R-30323-21917 */
18621 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
18649 ** pKeyInfo->nField.
18653 ** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
18657 ** an equals comparison. default_rc can be -1, 0, or +1. If there are
18658 ** multiple entries in the b-tree with the same key (when only looking
18659 ** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
18664 ** get and equal results when comparing this structure to a b-tree record.
18668 ** b-tree.
18671 KeyInfo *pKeyInfo; /* Collation and sort-order information */
18710 ** algorithm to employ when an attempt is made to insert a non-unique
18717 ** "colUsed & colNotIdxed" will be non-zero if the index is not a
18719 ** be true (note-20221022-a). If a column beyond the 63rd column of the
18720 ** table is used, the "colUsed & colNotIdxed" test will always be non-zero
18730 ** number (it cannot - the database page is not allocated until the VDBE
18755 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
18757 unsigned bLowQual:1; /* sqlite_stat1 says this is a low-quality index */
18768 IndexSample *aSample; /* Samples of the left-most key */
18769 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
18770 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
18784 #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
18787 #define IsUniqueIndex(X) ((X)->onError!=OE_None)
18792 #define XN_ROWID (-1) /* Indexed column is the rowid */
18793 #define XN_EXPR (-2) /* Indexed column is an expression */
18825 const char *z; /* Text of the token. Not NULL-terminated! */
18849 int sortingIdxPTab; /* Cursor number of pseudo-table */
18884 ** assignAggregateRegisters() that computes the value of pAggInfo->iFirstReg.
18887 #define AggInfoColumnReg(A,I) (assert((A)->iFirstReg),(A)->iFirstReg+(I))
18889 (assert((A)->iFirstReg),(A)->iFirstReg+(A)->nColumn+(I))
18892 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
18893 ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
18894 ** than 32767 we have to make it 32-bit. 16-bit is preferred because
18899 ** the option is available (at compile-time).
18914 ** to represent the greater-than-or-equal-to operator in the expression
18928 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
18947 ** subquery gives a constant result, then iTable is -1. If the subquery
18952 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
18983 int iValue; /* Non-negative integer value if EP_IntValue */
19008 ** TK_TRIGGER: 1 -> new, 0 -> old
19013 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
19016 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
19080 #define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
19081 #define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
19082 #define ExprSetProperty(E,P) (E)->flags|=(P)
19083 #define ExprClearProperty(E,P) (E)->flags&=~(P)
19084 #define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue)
19085 #define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse)
19086 #define ExprIsFullSize(E) (((E)->flags&(EP_Reduced|EP_TokenOnly))==0)
19091 #define ExprUseUToken(E) (((E)->flags&EP_IntValue)==0)
19092 #define ExprUseUValue(E) (((E)->flags&EP_IntValue)!=0)
19093 #define ExprUseWOfst(E) (((E)->flags&(EP_InnerON|EP_OuterON))==0)
19094 #define ExprUseWJoin(E) (((E)->flags&(EP_InnerON|EP_OuterON))!=0)
19095 #define ExprUseXList(E) (((E)->flags&EP_xIsSelect)==0)
19096 #define ExprUseXSelect(E) (((E)->flags&EP_xIsSelect)!=0)
19097 #define ExprUseYTab(E) (((E)->flags&(EP_WinFunc|EP_Subrtn))==0)
19098 #define ExprUseYWin(E) (((E)->flags&EP_WinFunc)!=0)
19099 #define ExprUseYSub(E) (((E)->flags&EP_Subrtn)!=0)
19108 ** processes but is a no-op for delivery.
19111 # define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
19112 # define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
19113 # define ExprClearVVAProperties(E) (E)->vvaFlags = 0
19133 #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
19143 ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
19159 ** ---------- -------------------------
19219 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
19228 Expr *pExpr; /* Expr to implement a USING variable -- NOT USED */
19239 #define EU4_EXPR 2 /* Uses IdList.a.u4.pExpr -- NOT CURRENTLY USED */
19250 ** In the colUsed field, the high-order bit (bit 63) is set if the table
19251 ** contains more than 63 columns and the 64-th or later column is used.
19271 int regResult; /* Registers holding results of a co-routine */
19276 unsigned isTabFunc :1; /* True if table-valued-function syntax */
19277 unsigned isCorrelated :1; /* True if sub-query is correlated */
19279 unsigned viaCoroutine :1; /* Implemented as a co-routine */
19285 unsigned isOn :1; /* u3.pOn was once valid and non-NULL */
19298 ExprList *pFuncArg; /* Arguments to table-valued-function */
19349 #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
19352 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
19355 #define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
19364 #define WHERE_KEEP_ALL_JOINS 0x2000 /* Do not do the omit-noop-join opt */
19384 ** NameContexts can be nested. When resolving names, the inner-most
19400 ExprList *pEList; /* Optional list of result-set columns */
19442 #define NC_NoSelect 0x080000 /* Do not descend into sub-selects */
19451 ** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
19452 ** conflict-target clause.) The pUpsertTargetWhere is the optional
19557 #define SF_PushDown 0x1000000 /* Modified by WHERE-clause push-down opt */
19565 #define IsNestedFrom(S) ((S)!=0 && ((S)->selFlags&SF_NestedFrom)!=0)
19573 ** identified by pDest->iSDParm.
19575 ** SRT_Except Remove results from the temporary index pDest->iSDParm.
19577 ** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
19582 ** the side-effects of functions.
19589 ** in register pDest->iSDParm then abandon the rest
19593 ** row of result as the key in table pDest->iSDParm.
19594 ** Apply the affinity pDest->affSdst before storing
19597 ** SRT_EphemTab Create an temporary table pDest->iSDParm and store
19603 ** SRT_Coroutine Generate a co-routine that returns a new row of
19605 ** of the co-routine is stored in register pDest->iSDParm
19606 ** and the result row is stored in pDest->nDest registers
19607 ** starting with pDest->iSdst.
19609 ** SRT_Table Store results in temporary table pDest->iSDParm.
19615 ** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
19616 ** But also use temporary table pDest->iSDParm+1 as
19620 ** SRT_Queue Store results in priority queue pDest->iSDParm (really
19624 ** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
19626 ** index at pDest->iSDParm+1 hold all prior stores.
19629 ** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
19630 ** table is an intkey table - in this case the first
19632 ** key. If (pDest->iSDParm>0), then the table is an index
19633 ** table. (pDest->iSDParm) is the number of key columns in
19645 #define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue)
19651 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo)
19679 ** the code generator needs. We have to keep per-table autoincrement
19698 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
19780 ** The nTableLock and aTableLock variables are only used if the shared-cache
19781 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
19782 ** used to store the set of table-locks required by the statement being
19816 ** of the base register during check-constraint eval */
19828 int nMaxArg; /* Max args passed to user function by sub-program */
19835 TableLock *aTableLock; /* Required table locks for shared-cache mode */
19880 int nHeight; /* Expression tree height of current sub-select */
19892 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
19914 #define PARSE_HDR_SZ (offsetof(Parse,aTempReg)-offsetof(Parse,zErrMsg)) /* Recursive part w/o aColC…
19916 #define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
19925 #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
19931 #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
19937 #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
19961 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
19965 #define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
19969 #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
20005 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
20006 the <column-list> is stored here */
20025 ** that is a part of a trigger-program.
20030 ** the first step of the trigger-program.
20037 ** orconf -> stores the ON CONFLICT algorithm
20038 ** pSelect -> The content to be inserted - either a SELECT statement or
20040 ** zTarget -> Dequoted name of the table to insert into.
20041 ** pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
20042 ** statement, then this stores the column-names to be
20044 ** pUpsert -> The ON CONFLICT clauses for an Upsert
20047 ** zTarget -> Dequoted name of the table to delete from.
20048 ** pWhere -> The WHERE clause of the DELETE statement if one is specified.
20052 ** zTarget -> Dequoted name of the table to update.
20053 ** pWhere -> The WHERE clause of the UPDATE statement if one is specified.
20055 ** pExprList -> A list of the columns to update and the expressions to update
20060 ** pSelect -> The SELECT statement
20063 ** pExprList -> The list of expressions that follow the RETURNING keyword.
20079 TriggerStep *pNext; /* Next in the link-list */
20080 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
20110 #define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
20114 #define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
20117 ** The following object is the header for an "RCStr" or "reference-counted
20128 ** 3. Make a (read-only) copy of a read-only RCStr string using
20162 ** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning
20165 ** be used on trunk check-ins. They are a temporary mechanism available
20170 #define SQLITE_NTUNE 6 /* Should be zero for all trunk check-ins */
20172 # define Tuning(X) (sqlite3Config.aTune[(X)-1])
20187 u8 bUseCis; /* Use covering indices for full-scans */
20192 u8 bJsonSelfcheck; /* Double-check JSON parsing */
20195 int neverCorrupt; /* Database is always well-formed */
20198 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
20199 sqlite3_mem_methods m; /* Low-level memory allocation interface */
20200 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
20201 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
20211 int sharedCacheEnabled; /* true if shared-cache mode enabled */
20213 /* The above might be initialized to non-zero. The following need to always
20249 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
20251 /* vvvv--- must be last ---vvv */
20259 ** the assert is only valid on a well-formed database. Instead of:
20271 ** things that are always true for well-formed databases.
20276 ** Context pointer passed down through the tree-walk.
20285 u16 mWFlags; /* Use-dependent flags */
20319 const char *zType; /* Type of the container - used for error messages */
20320 const Token *pName; /* Name of the container - used for error messages */
20348 ** Return code from the parse-tree walking primitives and their
20419 ** data structures on sqlite3DebugPrintf() using a tree-like view.
20436 ** (2) All window functions in a single SELECT form a linked-list
20509 ** Assuming zIn points to the first byte of a UTF-8 character,
20510 ** advance zIn to point to the first byte of the next UTF-8 character.
20521 ** routines that report the line-number on which the error originated
20566 ** The ctype.h header is needed for non-ASCII systems. It is also
20702 # define MAN754 ((((u64)1)<<52)-1)
20728 char sign; /* '+' or '-' */
20869 # define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
20870 # define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
20879 # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
21140 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
21141 # define sqlite3IsToplevel(p) ((p)->pToplevel==0)
21205 ** Routines to read and write variable-length integers. These used to
21433 ** The interface to the LEMON-generated parser
21494 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
21571 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
21573 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
21575 ** provided (enforcement of FK constraints requires the triggers sub-system).
21626 #define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
21680 ** sqlite3IoTrace is a pointer to a printf-like routine used to
21713 ** passed back to non-lookaside free() routines. Asserts such as the
21714 ** example above are placed on the non-lookaside free() routines to verify
21717 ** All of this is no-op for a production build. It only comes into
21718 ** play when the SQLITE_MEMDEBUG compile-time option is used.
21725 # define sqlite3MemdebugSetType(X,Y) /* no-op */
21769 # define IS_STMT_SCANSTATUS(db) (db->flags & SQLITE_StmtScanStatus)
21791 ** all of the platform-specific files (os_*.c) and is #included into those
21803 ** switch. The following code should catch this problem at compile-time.
21818 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
21842 || sqlite3_io_error_pending-- == 1 ) \
21857 sqlite3_diskfull_pending--; \
21900 ** This file implements routines used to report what compile-time options
21903 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */
21907 ** autoconf-based build
21921 ** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
21927 ** An array of names of all compile-time options. This array should
21928 ** be sorted A-Z.
21931 ** only a handful of compile-time options, so most times this array is usually
21969 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
21973 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
21975 "COMPILER=gcc-" __VERSION__,
22695 /* An array to map all upper-case characters into their corresponding
22696 ** lower-case character.
22698 ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
22738 /* All of the upper-to-lower conversion data is above. The following
22755 ** index (here shown as [256-OP_Ne]) would be out-of-bounds and thus
22756 ** be undefined behavior. That's goofy, but the C-standards people thought
22764 SQLITE_PRIVATE const unsigned char *sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne];
22765 SQLITE_PRIVATE const unsigned char *sqlite3aEQb = &sqlite3UpperToLower[256+6-OP_Ne];
22766 SQLITE_PRIVATE const unsigned char *sqlite3aGTb = &sqlite3UpperToLower[256+12-OP_Ne];
22769 ** The following 256 byte lookup table is used to support SQLites built-in
22778 ** SQLite identifier character 0x40 $, _, or non-ascii
22782 ** case. i.e. if the character is a lower-case ASCII character.
22783 ** If x is a lower-case ASCII character, then its upper-case equivalent
22784 ** is (x - 0x20). Therefore toupper() can be implemented as:
22791 ** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
22793 ** non-ASCII UTF character. Hence the test for whether or not a character is
22802 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
22834 /* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
22838 ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
22839 ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
22841 ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
22849 /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
22850 ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
22851 ** that compile-time option is omitted.
22857 # error "Compile-time disabling of covering index scan using the\
22858 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
22874 ** before 3.12.0). -1 means always keep the entire statement journal in
22884 ** The default lookaside-configuration, the format "SZ,N". SZ is the
22886 ** and N is the number of slots. The lookaside-configuration can be
22887 ** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
22888 ** or at run-time for an individual database connection using
22891 ** With the two-size-lookaside enhancement, less lookaside is required.
22892 ** The default configuration of 1200,40 actually provides 30 1200-byte slots
22893 ** and 93 128-byte slots, which is more lookaside than is available
22894 ** using the older 1200,100 configuration without two-size-lookaside.
22905 /* The default maximum size of an in-memory database created using
22971 0, /* mNoVisibleRowid. 0 == allow rowid-in-view */
22984 ** Hash table for global functions - functions common to all
22986 ** read-only.
23005 ** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
23012 ** 1-gibabyte boundary) in a compatible database. SQLite never uses
23172 ** * A b-tree cursor
23173 ** - In the main database or in an ephemeral database
23174 ** - On either an index or a table
23177 ** * A one-row "pseudotable" stored in a single register
23182 i8 iDb; /* Index of cursor database in db->aDb[] */
23191 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
23194 Bool colCache:1; /* pCache pointer is initialized and non-NULL */
23243 /* Return true if P is a null-only cursor
23246 ((P)->eCurType==CURTYPE_PSEUDO && (P)->nullRow && (P)->seekResult==0)
23268 ** When a sub-program is executed (OP_Program), a structure of this type
23271 ** values stored in the Vdbe struct. When the sub-program is finished,
23273 ** restoring the state of the VM to as it was before the sub-program
23309 i64 nDbChange; /* Value of db->nChange */
23344 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
23363 ** UPDATE no-change flag set
23369 ** * MEM_Null|MEM_Cleared Special SQL NULL that compares non-equal
23373 ** length Mem.n. Zero-terminated if
23429 /* Return TRUE if Mem X contains dynamically allocated content - anything
23433 (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
23439 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
23442 ** True if Mem X is a NULL-nochng type.
23445 (((X)->flags&MEM_TypeMask)==(MEM_Null|MEM_Zero) \
23446 && (X)->n==0 && (X)->u.nZero==0)
23457 #define memIsValid(M) ((M)->flags & MEM_AffMask)!=0
23523 int iSelectID; /* The "Select-ID" for this loop */
23528 /* The DblquoteStr object holds the text of a double-quoted
23532 ** list is consulted for each double-quoted identifier to see if the
23580 i64 startTime; /* Time when query started - used for profiling */
23594 bft changeCntOn:1; /* True to update the change-counter */
23599 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
23605 DblquoteStr *pDblStr; /* List of double-quoted string literals */
23612 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
23652 ** set of values on the right-hand side of an IN constraint.
23668 ** single-byte varint.
23826 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
23871 ** we have to locate the state vector at run-time. In the more common
23901 ** The value of N is added to the current status value and the high-water
23905 ** mark is unchanged. N must be non-negative for StatusDown().
23925 wsdStat.nowValue[op] -= N; in sqlite3StatusDown()
23997 p = p->pNext; in countLookasideSlots()
24007 u32 nInit = countLookasideSlots(db->lookaside.pInit); in sqlite3LookasideUsed()
24008 u32 nFree = countLookasideSlots(db->lookaside.pFree); in sqlite3LookasideUsed()
24010 nInit += countLookasideSlots(db->lookaside.pSmallInit); in sqlite3LookasideUsed()
24011 nFree += countLookasideSlots(db->lookaside.pSmallFree); in sqlite3LookasideUsed()
24013 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit; in sqlite3LookasideUsed()
24014 return db->lookaside.nSlot - (nInit+nFree); in sqlite3LookasideUsed()
24024 int *pHighwater, /* Write high-water mark here */ in sqlite3_db_status()
24025 int resetFlag /* Reset high-water mark if true */ in sqlite3_db_status()
24033 sqlite3_mutex_enter(db->mutex); in sqlite3_db_status()
24038 LookasideSlot *p = db->lookaside.pFree; in sqlite3_db_status()
24040 while( p->pNext ) p = p->pNext; in sqlite3_db_status()
24041 p->pNext = db->lookaside.pInit; in sqlite3_db_status()
24042 db->lookaside.pInit = db->lookaside.pFree; in sqlite3_db_status()
24043 db->lookaside.pFree = 0; in sqlite3_db_status()
24046 p = db->lookaside.pSmallFree; in sqlite3_db_status()
24048 while( p->pNext ) p = p->pNext; in sqlite3_db_status()
24049 p->pNext = db->lookaside.pSmallInit; in sqlite3_db_status()
24050 db->lookaside.pSmallInit = db->lookaside.pSmallFree; in sqlite3_db_status()
24051 db->lookaside.pSmallFree = 0; in sqlite3_db_status()
24064 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 ); in sqlite3_db_status()
24065 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 ); in sqlite3_db_status()
24067 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT]; in sqlite3_db_status()
24069 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0; in sqlite3_db_status()
24084 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
24085 Btree *pBt = db->aDb[i].pBt; in sqlite3_db_status()
24111 db->pnBytesFreed = &nByte; in sqlite3_db_status()
24112 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); in sqlite3_db_status()
24113 db->lookaside.pEnd = db->lookaside.pStart; in sqlite3_db_status()
24114 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
24115 Schema *pSchema = db->aDb[i].pSchema; in sqlite3_db_status()
24120 pSchema->tblHash.count in sqlite3_db_status()
24121 + pSchema->trigHash.count in sqlite3_db_status()
24122 + pSchema->idxHash.count in sqlite3_db_status()
24123 + pSchema->fkeyHash.count in sqlite3_db_status()
24125 nByte += sqlite3_msize(pSchema->tblHash.ht); in sqlite3_db_status()
24126 nByte += sqlite3_msize(pSchema->trigHash.ht); in sqlite3_db_status()
24127 nByte += sqlite3_msize(pSchema->idxHash.ht); in sqlite3_db_status()
24128 nByte += sqlite3_msize(pSchema->fkeyHash.ht); in sqlite3_db_status()
24130 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
24133 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
24138 db->pnBytesFreed = 0; in sqlite3_db_status()
24139 db->lookaside.pEnd = db->lookaside.pTrueEnd; in sqlite3_db_status()
24156 db->pnBytesFreed = &nByte; in sqlite3_db_status()
24157 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); in sqlite3_db_status()
24158 db->lookaside.pEnd = db->lookaside.pStart; in sqlite3_db_status()
24159 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){ in sqlite3_db_status()
24162 db->lookaside.pEnd = db->lookaside.pTrueEnd; in sqlite3_db_status()
24163 db->pnBytesFreed = 0; in sqlite3_db_status()
24165 *pHighwater = 0; /* IMP: R-64479-57858 */ in sqlite3_db_status()
24187 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
24188 if( db->aDb[i].pBt ){ in sqlite3_db_status()
24189 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); in sqlite3_db_status()
24193 *pHighwater = 0; /* IMP: R-42420-56072 */ in sqlite3_db_status()
24194 /* IMP: R-54100-20147 */ in sqlite3_db_status()
24195 /* IMP: R-29431-39229 */ in sqlite3_db_status()
24200 /* Set *pCurrent to non-zero if there are unresolved deferred foreign in sqlite3_db_status()
24205 *pHighwater = 0; /* IMP: R-11967-56545 */ in sqlite3_db_status()
24206 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; in sqlite3_db_status()
24214 sqlite3_mutex_leave(db->mutex); in sqlite3_db_status()
24234 ** There is only one exported symbol in this file - the function
24243 ** 1970-01-01 00:00:00 is JD 2440587.5
24244 ** 2000-01-01 00:00:00 is JD 2451544.5
24246 ** This implementation requires years to be expressed as a 4-digit number
24247 ** which means that only dates between 0000-01-01 and 9999-12-31 can
24253 ** use the julian calendar for dates prior to 1582-10-15 and for some
24261 ** ISBN 0-943396-61-1
24262 ** Willmann-Bell, Inc
24310 ** of a four-character format specifiers ABCD is:
24324 ** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
24325 ** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
24326 ** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
24327 ** the 2-digit day which is the last integer in the set.
24340 char N = zFormat[0] - '0'; in getDigits()
24341 char min = zFormat[1] - '0'; in getDigits()
24346 max = aMx[zFormat[2] - 'a']; in getDigits()
24349 while( N-- ){ in getDigits()
24353 val = val*10 + *zDate - '0'; in getDigits()
24370 ** Parse a timezone extension on the end of a date-time.
24373 ** (+/-)HH:MM
24380 ** of change in p->tz and return 0. If a parser error occurs,
24381 ** return non-zero.
24390 p->tz = 0; in parseTimezone()
24392 if( c=='-' ){ in parseTimezone()
24393 sgn = -1; in parseTimezone()
24398 p->isLocal = 0; in parseTimezone()
24399 p->isUtc = 1; in parseTimezone()
24409 p->tz = sgn*(nMn + nHr*60); in parseTimezone()
24439 ms = ms*10.0 + *zDate - '0'; in parseHhMmSs()
24448 p->validJD = 0; in parseHhMmSs()
24449 p->rawS = 0; in parseHhMmSs()
24450 p->validHMS = 1; in parseHhMmSs()
24451 p->h = h; in parseHhMmSs()
24452 p->m = m; in parseHhMmSs()
24453 p->s = s + ms; in parseHhMmSs()
24463 p->isError = 1; in datetimeError()
24467 ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
24468 ** that the YYYY-MM-DD is according to the Gregorian calendar.
24475 if( p->validJD ) return; in computeJD()
24476 if( p->validYMD ){ in computeJD()
24477 Y = p->Y; in computeJD()
24478 M = p->M; in computeJD()
24479 D = p->D; in computeJD()
24481 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */ in computeJD()
24485 if( Y<-4713 || Y>9999 || p->rawS ){ in computeJD()
24490 Y--; in computeJD()
24494 B = 2 - A + (A/4); in computeJD()
24497 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000); in computeJD()
24498 p->validJD = 1; in computeJD()
24499 if( p->validHMS ){ in computeJD()
24500 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000 + 0.5); in computeJD()
24501 if( p->tz ){ in computeJD()
24502 p->iJD -= p->tz*60000; in computeJD()
24503 p->validYMD = 0; in computeJD()
24504 p->validHMS = 0; in computeJD()
24505 p->tz = 0; in computeJD()
24506 p->isUtc = 1; in computeJD()
24507 p->isLocal = 0; in computeJD()
24513 ** Given the YYYY-MM-DD information current in p, determine if there
24514 ** is day-of-month overflow and set nFloor to the number of days that
24519 assert( p->validYMD || p->isError ); in computeFloor()
24520 assert( p->D>=0 && p->D<=31 ); in computeFloor()
24521 assert( p->M>=0 && p->M<=12 ); in computeFloor()
24522 if( p->D<=28 ){ in computeFloor()
24523 p->nFloor = 0; in computeFloor()
24524 }else if( (1<<p->M) & 0x15aa ){ in computeFloor()
24525 p->nFloor = 0; in computeFloor()
24526 }else if( p->M!=2 ){ in computeFloor()
24527 p->nFloor = (p->D==31); in computeFloor()
24528 }else if( p->Y%4!=0 || (p->Y%100==0 && p->Y%400!=0) ){ in computeFloor()
24529 p->nFloor = p->D - 28; in computeFloor()
24531 p->nFloor = p->D - 29; in computeFloor()
24538 ** YYYY-MM-DD HH:MM:SS.FFF
24539 ** YYYY-MM-DD HH:MM:SS
24540 ** YYYY-MM-DD HH:MM
24541 ** YYYY-MM-DD
24544 ** on success and 1 if the input string is not a well-formed
24550 if( zDate[0]=='-' ){ in parseYyyyMmDd()
24556 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){ in parseYyyyMmDd()
24564 p->validHMS = 0; in parseYyyyMmDd()
24568 p->validJD = 0; in parseYyyyMmDd()
24569 p->validYMD = 1; in parseYyyyMmDd()
24570 p->Y = neg ? -Y : Y; in parseYyyyMmDd()
24571 p->M = M; in parseYyyyMmDd()
24572 p->D = D; in parseYyyyMmDd()
24574 if( p->tz ){ in parseYyyyMmDd()
24589 p->iJD = sqlite3StmtCurrentTime(context); in setDateTimeToCurrent()
24590 if( p->iJD>0 ){ in setDateTimeToCurrent()
24591 p->validJD = 1; in setDateTimeToCurrent()
24592 p->isUtc = 1; in setDateTimeToCurrent()
24593 p->isLocal = 0; in setDateTimeToCurrent()
24605 ** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
24608 p->s = r; in setRawDateNumber()
24609 p->rawS = 1; in setRawDateNumber()
24611 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); in setRawDateNumber()
24612 p->validJD = 1; in setRawDateNumber()
24622 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
24626 ** In the first form, the +/-HH:MM is always optional. The fractional
24650 p->useSubsec = 1; in parseDateOrTime()
24656 /* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
24679 if( p->validYMD ) return; in computeYMD()
24680 if( !p->validJD ){ in computeYMD()
24681 p->Y = 2000; in computeYMD()
24682 p->M = 1; in computeYMD()
24683 p->D = 1; in computeYMD()
24684 }else if( !validJulianDay(p->iJD) ){ in computeYMD()
24688 Z = (int)((p->iJD + 43200000)/86400000); in computeYMD()
24689 A = (int)((Z - 1867216.25)/36524.25); in computeYMD()
24690 A = Z + 1 + A - (A/4); in computeYMD()
24692 C = (int)((B - 122.1)/365.25); in computeYMD()
24694 E = (int)((B-D)/30.6001); in computeYMD()
24696 p->D = B - D - X1; in computeYMD()
24697 p->M = E<14 ? E-1 : E-13; in computeYMD()
24698 p->Y = p->M>2 ? C - 4716 : C - 4715; in computeYMD()
24700 p->validYMD = 1; in computeYMD()
24708 if( p->validHMS ) return; in computeHMS()
24710 day_ms = (int)((p->iJD + 43200000) % 86400000); in computeHMS()
24711 p->s = (day_ms % 60000)/1000.0; in computeHMS()
24713 p->m = day_min % 60; in computeHMS()
24714 p->h = day_min / 60; in computeHMS()
24715 p->rawS = 0; in computeHMS()
24716 p->validHMS = 1; in computeHMS()
24731 p->validYMD = 0; in clearYMD_HMS_TZ()
24732 p->validHMS = 0; in clearYMD_HMS_TZ()
24733 p->tz = 0; in clearYMD_HMS_TZ()
24743 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
24757 ** using whatever operating-system specific localtime facility that
24759 ** non-zero on any kind of error.
24761 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this
24764 ** invoked in place of the OS-defined localtime() function.
24766 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
24832 if( p->iJD<2108667600*(i64)100000 /* 1970-01-01 */ in toLocaltime()
24833 || p->iJD>2130141456*(i64)100000 /* 2038-01-18 */ in toLocaltime()
24835 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only in toLocaltime()
24842 iYearDiff = (2000 + x.Y%4) - x.Y; in toLocaltime()
24846 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000); in toLocaltime()
24849 t = (time_t)(p->iJD/1000 - 21086676*(i64)10000); in toLocaltime()
24852 sqlite3_result_error(pCtx, "local time unavailable", -1); in toLocaltime()
24855 p->Y = sLocal.tm_year + 1900 - iYearDiff; in toLocaltime()
24856 p->M = sLocal.tm_mon + 1; in toLocaltime()
24857 p->D = sLocal.tm_mday; in toLocaltime()
24858 p->h = sLocal.tm_hour; in toLocaltime()
24859 p->m = sLocal.tm_min; in toLocaltime()
24860 p->s = sLocal.tm_sec + (p->iJD%1000)*0.001; in toLocaltime()
24861 p->validYMD = 1; in toLocaltime()
24862 p->validHMS = 1; in toLocaltime()
24863 p->validJD = 0; in toLocaltime()
24864 p->rawS = 0; in toLocaltime()
24865 p->tz = 0; in toLocaltime()
24866 p->isError = 0; in toLocaltime()
24876 ** Where NNN is an arbitrary floating-point number and "days" can be one
24899 if( !p->rawS || p->validJD ){ in autoAdjustDate()
24900 p->rawS = 0; in autoAdjustDate()
24901 }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */ in autoAdjustDate()
24902 && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */ in autoAdjustDate()
24904 double r = p->s*1000.0 + 210866760000000.0; in autoAdjustDate()
24906 p->iJD = (sqlite3_int64)(r + 0.5); in autoAdjustDate()
24907 p->validJD = 1; in autoAdjustDate()
24908 p->rawS = 0; in autoAdjustDate()
24913 ** Process a modifier to a date-time stamp. The modifiers are
24922 ** +/-YYYY-MM-DD HH:MM:SS.SSS
24960 if( idx>1 ) return 1; /* IMP: R-33611-57934 */ in parseModifier()
24970 ** Resolve day-of-month overflow by rolling forward into the next in parseModifier()
24972 ** a no-op that is only included for symmetry. See "floor". in parseModifier()
24978 p->nFloor = 0; in parseModifier()
24986 ** Resolve day-of-month overflow by rolling back to the end of the in parseModifier()
24991 p->iJD -= p->nFloor*86400000; in parseModifier()
25001 ** Always interpret the prior number as a julian-day value. If this in parseModifier()
25007 if( idx>1 ) return 1; /* IMP: R-31176-64601 */ in parseModifier()
25008 if( p->validJD && p->rawS ){ in parseModifier()
25010 p->rawS = 0; in parseModifier()
25023 rc = p->isLocal ? SQLITE_OK : toLocaltime(p, pCtx); in parseModifier()
25024 p->isUtc = 0; in parseModifier()
25025 p->isLocal = 1; in parseModifier()
25034 ** Treat the current value of p->s as the number of in parseModifier()
25037 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){ in parseModifier()
25038 if( idx>1 ) return 1; /* IMP: R-49255-55373 */ in parseModifier()
25039 r = p->s*1000.0 + 210866760000000.0; in parseModifier()
25042 p->iJD = (sqlite3_int64)(r + 0.5); in parseModifier()
25043 p->validJD = 1; in parseModifier()
25044 p->rawS = 0; in parseModifier()
25050 if( p->isUtc==0 ){ in parseModifier()
25057 iGuess = iOrigJD = p->iJD; in parseModifier()
25062 iGuess -= iErr; in parseModifier()
25068 iErr = new.iJD - iOrigJD; in parseModifier()
25071 p->iJD = iGuess; in parseModifier()
25072 p->validJD = 1; in parseModifier()
25073 p->isUtc = 1; in parseModifier()
25074 p->isLocal = 0; in parseModifier()
25087 ** date is already on the appropriate weekday, this is a no-op. in parseModifier()
25094 p->tz = 0; in parseModifier()
25095 p->validJD = 0; in parseModifier()
25097 Z = ((p->iJD + 129600000)/86400000) % 7; in parseModifier()
25098 if( Z>n ) Z -= 7; in parseModifier()
25099 p->iJD += (n - Z)*86400000; in parseModifier()
25122 p->useSubsec = 1; in parseModifier()
25127 if( !p->validJD && !p->validYMD && !p->validHMS ) break; in parseModifier()
25130 p->validHMS = 1; in parseModifier()
25131 p->h = p->m = 0; in parseModifier()
25132 p->s = 0.0; in parseModifier()
25133 p->rawS = 0; in parseModifier()
25134 p->tz = 0; in parseModifier()
25135 p->validJD = 0; in parseModifier()
25137 p->D = 1; in parseModifier()
25140 p->M = 1; in parseModifier()
25141 p->D = 1; in parseModifier()
25149 case '-': in parseModifier()
25168 if( z[n]=='-' ){ in parseModifier()
25177 if( z[n]=='-' ){ in parseModifier()
25178 /* A modifier of the form (+|-)YYYY-MM-DD adds or subtracts the in parseModifier()
25180 ** the range 0-11 and DD is limited to 0-30. in parseModifier()
25182 if( z0!='+' && z0!='-' ) break; /* Must start with +/- */ in parseModifier()
25184 if( getDigits(&z[1], "40f-20a-20d", &Y, &M, &D)!=3 ) break; in parseModifier()
25187 if( getDigits(&z[1], "50f-20a-20d", &Y, &M, &D)!=3 ) break; in parseModifier()
25193 p->validJD = 0; in parseModifier()
25194 if( z0=='-' ){ in parseModifier()
25195 p->Y -= Y; in parseModifier()
25196 p->M -= M; in parseModifier()
25197 D = -D; in parseModifier()
25199 p->Y += Y; in parseModifier()
25200 p->M += M; in parseModifier()
25202 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; in parseModifier()
25203 p->Y += x; in parseModifier()
25204 p->M -= x*12; in parseModifier()
25207 p->validHMS = 0; in parseModifier()
25208 p->validYMD = 0; in parseModifier()
25209 p->iJD += (i64)D*86400000; in parseModifier()
25224 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the in parseModifier()
25236 tx.iJD -= 43200000; in parseModifier()
25238 tx.iJD -= day*86400000; in parseModifier()
25239 if( z0=='-' ) tx.iJD = -tx.iJD; in parseModifier()
25242 p->iJD += tx.iJD; in parseModifier()
25253 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--; in parseModifier()
25256 rRounder = r<0 ? -0.5 : +0.5; in parseModifier()
25257 p->nFloor = 0; in parseModifier()
25261 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit in parseModifier()
25267 p->M += (int)r; in parseModifier()
25268 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; in parseModifier()
25269 p->Y += x; in parseModifier()
25270 p->M -= x*12; in parseModifier()
25272 p->validJD = 0; in parseModifier()
25273 r -= (int)r; in parseModifier()
25280 assert( p->M>=0 && p->M<=12 ); in parseModifier()
25281 p->Y += y; in parseModifier()
25283 p->validJD = 0; in parseModifier()
25284 r -= (int)r; in parseModifier()
25289 p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder); in parseModifier()
25305 ** Process time function arguments. argv[0] is a date-time stamp.
25342 if( p->isError || !validJulianDay(p->iJD) ) return 1; in isDate()
25343 if( argc==1 && p->validYMD && p->D>28 ){ in isDate()
25344 /* Make sure a YYYY-MM-DD is normalized. in isDate()
25345 ** Example: 2023-02-31 -> 2023-03-03 */ in isDate()
25346 assert( p->validJD ); in isDate()
25347 p->validYMD = 0; in isDate()
25379 ** the unix epoch of 1970-01-01 00:00:00 GMT.
25390 sqlite3_result_double(context, (x.iJD - 21086676*(i64)10000000)/1000.0); in unixepochFunc()
25392 sqlite3_result_int64(context, x.iJD/1000 - 21086676*(i64)10000); in unixepochFunc()
25400 ** Return YYYY-MM-DD HH:MM:SS
25413 if( Y<0 ) Y = -Y; in datetimeFunc()
25418 zBuf[5] = '-'; in datetimeFunc()
25421 zBuf[8] = '-'; in datetimeFunc()
25449 zBuf[0] = '-'; in datetimeFunc()
25452 sqlite3_result_text(context, &zBuf[1], n-1, SQLITE_TRANSIENT); in datetimeFunc()
25502 ** Return YYYY-MM-DD
25515 if( Y<0 ) Y = -Y; in dateFunc()
25520 zBuf[5] = '-'; in dateFunc()
25523 zBuf[8] = '-'; in dateFunc()
25528 zBuf[0] = '-'; in dateFunc()
25539 ** In other words, compute the zero-based day number for the
25549 assert( pDate->validJD ); in daysAfterJan01()
25554 return (int)((pDate->iJD-jan01.iJD+43200000)/86400000); in daysAfterJan01()
25566 assert( pDate->validJD ); in daysAfterMonday()
25567 return (int)((pDate->iJD+43200000)/86400000) % 7; in daysAfterMonday()
25579 assert( pDate->validJD ); in daysAfterSunday()
25580 return (int)((pDate->iJD+129600000)/86400000) % 7; in daysAfterSunday()
25588 ** %d day of month 01-31
25589 ** %e day of month 1-31
25591 ** %F ISO date. YYYY-MM-DD
25592 ** %G ISO year corresponding to %V 0000-9999.
25593 ** %g 2-digit ISO year corresponding to %V 00-99
25594 ** %H hour 00-24
25595 ** %k hour 0-24 (leading zero converted to space)
25596 ** %I hour 01-12
25597 ** %j day of year 001-366
25599 ** %l hour 1-12 (leading zero converted to space)
25600 ** %m month 01-12
25601 ** %M minute 00-59
25605 ** %s seconds since 1970-01-01
25606 ** %S seconds 00-59
25608 ** %u day of week 1-7 Monday==1, Sunday==7
25609 ** %w day of week 0-6 Sunday==0, Monday==1
25610 ** %U week of year 00-53 (First Sunday is start of week 01)
25611 ** %V week of year 01-53 (First week containing Thursday is week 01)
25612 ** %W week of year 00-53 (First Monday is start of week 01)
25613 ** %Y year 0000-9999
25630 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return; in strftimeFunc()
25632 sqlite3StrAccumInit(&sRes, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); in strftimeFunc()
25639 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j)); in strftimeFunc()
25649 case 'f': { /* Fractional seconds. (Non-standard) */ in strftimeFunc()
25656 sqlite3_str_appendf(&sRes, "%04d-%02d-%02d", x.Y, x.M, x.D); in strftimeFunc()
25664 y.iJD += (3 - daysAfterMonday(&x))*86400000; in strftimeFunc()
25682 if( h>12 ) h -= 12; in strftimeFunc()
25691 case 'J': { /* Julian day number. (Non-standard) */ in strftimeFunc()
25719 (x.iJD - 21086676*(i64)10000000)/1000.0); in strftimeFunc()
25721 i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000); in strftimeFunc()
25741 case 'U': { /* Week num. 00-53. First Sun of the year is week 01 */ in strftimeFunc()
25743 (daysAfterJan01(&x)-daysAfterSunday(&x)+7)/7); in strftimeFunc()
25746 case 'V': { /* Week num. 01-53. First week with a Thur is week 01 */ in strftimeFunc()
25750 y.iJD += (3 - daysAfterMonday(&x))*86400000; in strftimeFunc()
25756 case 'W': { /* Week num. 00-53. First Mon of the year is week 01 */ in strftimeFunc()
25758 (daysAfterJan01(&x)-daysAfterMonday(&x)+7)/7); in strftimeFunc()
25775 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j)); in strftimeFunc()
25813 ** +YYYY-MM-DD HH:MM:SS.SSS
25815 ** The initial "+" becomes "-" if DATE1 occurs before DATE2. For
25821 ** ISO-8601 string. The unix timestamps are not supported by this
25840 Y = d1.Y - d2.Y; in timediffFunc()
25846 M = d1.M - d2.M; in timediffFunc()
25848 Y--; in timediffFunc()
25857 M--; in timediffFunc()
25860 Y--; in timediffFunc()
25862 d2.M--; in timediffFunc()
25865 d2.Y--; in timediffFunc()
25870 d1.iJD -= d2.iJD; in timediffFunc()
25873 sign = '-'; in timediffFunc()
25874 Y = d2.Y - d1.Y; in timediffFunc()
25880 M = d2.M - d1.M; in timediffFunc()
25882 Y--; in timediffFunc()
25891 M--; in timediffFunc()
25894 Y--; in timediffFunc()
25904 d1.iJD = d2.iJD - d1.iJD; in timediffFunc()
25910 sqlite3_str_appendf(&sRes, "%c%04d-%02d-%02d %02d:%02d:%06.3f", in timediffFunc()
25911 sign, Y, M, d1.D-1, d1.h, d1.m, d1.s); in timediffFunc()
25933 ** If the library is compiled to omit the full-scale date and time
25939 ** This function uses the C-library functions time(), gmtime()
25941 ** as the user-data for the function.
25960 t = iT/1000 - 10000*(sqlite3_int64)21086676; in currentTimeFunc()
25971 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in currentTimeFunc()
26000 sqlite3_result_text(context, zJson, -1, sqlite3_free); in datedebugFunc()
26014 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ), in sqlite3RegisterDateTimeFunctions()
26015 PURE_DATE(unixepoch, -1, 0, 0, unixepochFunc ), in sqlite3RegisterDateTimeFunctions()
26016 PURE_DATE(date, -1, 0, 0, dateFunc ), in sqlite3RegisterDateTimeFunctions()
26017 PURE_DATE(time, -1, 0, 0, timeFunc ), in sqlite3RegisterDateTimeFunctions()
26018 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ), in sqlite3RegisterDateTimeFunctions()
26019 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ), in sqlite3RegisterDateTimeFunctions()
26022 PURE_DATE(datedebug, -1, 0, 0, datedebugFunc ), in sqlite3RegisterDateTimeFunctions()
26029 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
26030 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
26062 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
26080 ** from within OsOpen()), but some third-party implementations may.
26120 if( pId->pMethods ){ in sqlite3OsClose()
26121 pId->pMethods->xClose(pId); in sqlite3OsClose()
26122 pId->pMethods = 0; in sqlite3OsClose()
26127 return id->pMethods->xRead(id, pBuf, amt, offset); in sqlite3OsRead()
26131 return id->pMethods->xWrite(id, pBuf, amt, offset); in sqlite3OsWrite()
26134 return id->pMethods->xTruncate(id, size); in sqlite3OsTruncate()
26138 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK; in sqlite3OsSync()
26142 return id->pMethods->xFileSize(id, pSize); in sqlite3OsFileSize()
26147 return id->pMethods->xLock(id, lockType); in sqlite3OsLock()
26151 return id->pMethods->xUnlock(id, lockType); in sqlite3OsUnlock()
26155 return id->pMethods->xCheckReservedLock(id, pResOut); in sqlite3OsCheckReservedLock()
26167 if( id->pMethods==0 ) return SQLITE_NOTFOUND; in sqlite3OsFileControl()
26177 ** confuses the test scripts - the COMMIT command returns SQLITE_NOMEM in sqlite3OsFileControl()
26185 ** The CKPT_DONE and CKPT_START file-controls are write-only signals in sqlite3OsFileControl()
26192 return id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControl()
26195 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControlHint()
26199 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; in sqlite3OsSectorSize()
26203 if( NEVER(id->pMethods==0) ) return 0; in sqlite3OsDeviceCharacteristics()
26204 return id->pMethods->xDeviceCharacteristics(id); in sqlite3OsDeviceCharacteristics()
26208 return id->pMethods->xShmLock(id, offset, n, flags); in sqlite3OsShmLock()
26211 id->pMethods->xShmBarrier(id); in sqlite3OsShmBarrier()
26214 return id->pMethods->xShmUnmap(id, deleteFlag); in sqlite3OsShmUnmap()
26224 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp); in sqlite3OsShmMap()
26232 return id->pMethods->xFetch(id, iOff, iAmt, pp); in sqlite3OsFetch()
26235 return id->pMethods->xUnfetch(id, iOff, p); in sqlite3OsUnfetch()
26238 /* No-op stubs to use when memory-mapped I/O is disabled */
26266 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut); in sqlite3OsOpen()
26267 assert( rc==SQLITE_OK || pFile->pMethods==0 ); in sqlite3OsOpen()
26273 return pVfs->xDelete!=0 ? pVfs->xDelete(pVfs, zPath, dirSync) : SQLITE_OK; in sqlite3OsDelete()
26282 return pVfs->xAccess(pVfs, zPath, flags, pResOut); in sqlite3OsAccess()
26292 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut); in sqlite3OsFullPathname()
26297 assert( strlen(zPath)<=SQLITE_MAX_PATHLEN ); /* tag-20210611-1 */ in sqlite3OsDlOpen()
26298 return pVfs->xDlOpen(pVfs, zPath); in sqlite3OsDlOpen()
26301 pVfs->xDlError(pVfs, nByte, zBufOut); in sqlite3OsDlError()
26304 return pVfs->xDlSym(pVfs, pHdle, zSym); in sqlite3OsDlSym()
26307 pVfs->xDlClose(pVfs, pHandle); in sqlite3OsDlClose()
26317 return pVfs->xRandomness(pVfs, nByte, zBufOut); in sqlite3OsRandomness()
26322 return pVfs->xSleep(pVfs, nMicro); in sqlite3OsSleep()
26325 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0; in sqlite3OsGetLastError()
26329 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64() in sqlite3OsCurrentTimeInt64()
26335 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ in sqlite3OsCurrentTimeInt64()
26336 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut); in sqlite3OsCurrentTimeInt64()
26339 rc = pVfs->xCurrentTime(pVfs, &r); in sqlite3OsCurrentTimeInt64()
26354 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile); in sqlite3OsOpenMalloc()
26412 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){ in sqlite3_vfs_find()
26414 if( strcmp(zVfs, pVfs->zName)==0 ) break; in sqlite3_vfs_find()
26426 /* No-op */ in vfsUnlink()
26428 vfsList = pVfs->pNext; in vfsUnlink()
26431 while( p->pNext && p->pNext!=pVfs ){ in vfsUnlink()
26432 p = p->pNext; in vfsUnlink()
26434 if( p->pNext==pVfs ){ in vfsUnlink()
26435 p->pNext = pVfs->pNext; in vfsUnlink()
26459 pVfs->pNext = vfsList; in sqlite3_vfs_register()
26462 pVfs->pNext = vfsList->pNext; in sqlite3_vfs_register()
26463 vfsList->pNext = pVfs; in sqlite3_vfs_register()
26505 ** Most malloc failures are non-benign. After they occur, SQLite
26529 ** we have to locate the state vector at run-time. In the more common
26559 ** indicates that subsequent malloc failures are non-benign.
26590 ** This file contains a no-op memory allocation drivers for use when
26600 ** used when no other memory allocator is specified using compile-time
26606 ** No-op versions of all memory allocation routines
26619 ** Populate the low-level memory allocation function pointers in
26652 ** This file contains low-level memory allocation drivers for when
26653 ** SQLite will use the standard C-library malloc/realloc/free interface
26656 ** This file contains implementations of the low-level memory allocation
26664 ** C-preprocessor macro summary:
26671 ** a different name, using a separate -D
26687 ** used when no other memory allocator is specified using compile-time
26707 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
26712 ** Use standard C library malloc and free on non-Apple systems.
26729 ** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
26763 ** For this low-level routine, we are guaranteed that nByte>0 because
26797 ** For this low-level routine, we already know that pPrior!=0 since
26799 ** by higher-level routines.
26807 p--; in sqlite3MemFree()
26824 p--; in sqlite3MemSize()
26833 ** For this low-level interface, we know that pPrior!=0. Cases where
26834 ** pPrior==0 while have been intercepted by higher-level routine and
26836 ** cases where nByte<=0 will have been intercepted by higher-level
26852 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
26853 p--; in sqlite3MemRealloc()
26913 ** Populate the low-level memory allocation function pointers in
26946 ** This file contains low-level memory allocation drivers for when
26947 ** SQLite will use the standard C-library malloc/realloc/free interface
26952 ** This file contains implementations of the low-level memory allocation
26978 ** ------------------------------------------------------------------------
26980 ** ------------------------------------------------------------------------
27064 if( i>NCSIZE-1 ){ in adjustStats()
27065 i = NCSIZE - 1; in adjustStats()
27074 mem.nCurrent[i]--; in adjustStats()
27092 p--; in sqlite3MemsysGetHeader()
27093 assert( p->iForeGuard==(int)FOREGUARD ); in sqlite3MemsysGetHeader()
27094 nReserve = ROUND8(p->iSize); in sqlite3MemsysGetHeader()
27102 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 ); in sqlite3MemsysGetHeader()
27115 return (int)pHdr->iSize; in sqlite3MemSize()
27148 ** Fill a buffer with pseudo-random bytes. This is used to preset
27157 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
27162 nByte -= 4; in randomFill()
27164 while( nByte-- > 0 ){ in randomFill()
27165 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
27193 pHdr->pNext = 0; in sqlite3MemMalloc()
27194 pHdr->pPrev = mem.pLast; in sqlite3MemMalloc()
27196 mem.pLast->pNext = pHdr; in sqlite3MemMalloc()
27201 pHdr->iForeGuard = FOREGUARD; in sqlite3MemMalloc()
27202 pHdr->eType = MEMTYPE_HEAP; in sqlite3MemMalloc()
27203 pHdr->nBacktraceSlots = mem.nBacktrace; in sqlite3MemMalloc()
27204 pHdr->nTitle = mem.nTitle; in sqlite3MemMalloc()
27207 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1; in sqlite3MemMalloc()
27208 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*)); in sqlite3MemMalloc()
27211 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]); in sqlite3MemMalloc()
27214 pHdr->nBacktrace = 0; in sqlite3MemMalloc()
27219 pHdr->iSize = nByte; in sqlite3MemMalloc()
27224 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte); in sqlite3MemMalloc()
27242 pBt -= pHdr->nBacktraceSlots; in sqlite3MemFree()
27244 if( pHdr->pPrev ){ in sqlite3MemFree()
27245 assert( pHdr->pPrev->pNext==pHdr ); in sqlite3MemFree()
27246 pHdr->pPrev->pNext = pHdr->pNext; in sqlite3MemFree()
27249 mem.pFirst = pHdr->pNext; in sqlite3MemFree()
27251 if( pHdr->pNext ){ in sqlite3MemFree()
27252 assert( pHdr->pNext->pPrev==pHdr ); in sqlite3MemFree()
27253 pHdr->pNext->pPrev = pHdr->pPrev; in sqlite3MemFree()
27256 mem.pLast = pHdr->pPrev; in sqlite3MemFree()
27259 z -= pHdr->nTitle; in sqlite3MemFree()
27260 adjustStats((int)pHdr->iSize, -1); in sqlite3MemFree()
27261 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) + in sqlite3MemFree()
27262 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle); in sqlite3MemFree()
27280 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
27284 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize)); in sqlite3MemRealloc()
27285 if( nByte>pOldHdr->iSize ){ in sqlite3MemRealloc()
27286 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize); in sqlite3MemRealloc()
27294 ** Populate the low-level memory allocation function pointers in
27318 assert( pHdr->iForeGuard==FOREGUARD ); in sqlite3MemdebugSetType()
27319 pHdr->eType = eType; in sqlite3MemdebugSetType()
27337 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugHasType()
27338 if( (pHdr->eType&eType)==0 ){ in sqlite3MemdebugHasType()
27359 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugNoType()
27360 if( (pHdr->eType&eType)!=0 ){ in sqlite3MemdebugNoType()
27389 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1; in sqlite3MemdebugSettitle()
27398 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugSync()
27400 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugSync()
27401 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]); in sqlite3MemdebugSync()
27420 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugDump()
27422 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle; in sqlite3MemdebugDump()
27424 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???"); in sqlite3MemdebugDump()
27425 if( pHdr->nBacktrace ){ in sqlite3MemdebugDump()
27428 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugDump()
27429 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out)); in sqlite3MemdebugDump()
27434 for(i=0; i<NCSIZE-1; i++){ in sqlite3MemdebugDump()
27440 if( mem.nAlloc[NCSIZE-1] ){ in sqlite3MemdebugDump()
27442 NCSIZE*8-8, mem.nAlloc[NCSIZE-1], in sqlite3MemdebugDump()
27443 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]); in sqlite3MemdebugDump()
27495 ** mean that the library will use a memory-pool by default, just that
27536 ** two fields form a double-linked list of chunks of related sizes.
27573 ** True if we are evaluating an out-of-memory callback.
27601 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
27634 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Unlink()
27636 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Unlink()
27637 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Unlink()
27640 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]); in memsys3Unlink()
27669 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Link()
27670 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Link()
27671 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Link()
27674 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]); in memsys3Link()
27713 ** size parameters for check-out and return a pointer to the
27720 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ); in memsys3Checkout()
27721 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock ); in memsys3Checkout()
27722 x = mem3.aPool[i-1].u.hdr.size4x; in memsys3Checkout()
27723 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2); in memsys3Checkout()
27724 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock; in memsys3Checkout()
27725 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2; in memsys3Checkout()
27737 if( nBlock>=mem3.szKeyBlk-1 ){ in memsys3FromKeyBlk()
27747 newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock; in memsys3FromKeyBlk()
27749 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock; in memsys3FromKeyBlk()
27750 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2; in memsys3FromKeyBlk()
27751 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1; in memsys3FromKeyBlk()
27752 mem3.szKeyBlk -= nBlock; in memsys3FromKeyBlk()
27753 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FromKeyBlk()
27754 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FromKeyBlk()
27755 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FromKeyBlk()
27785 size = mem3.aPool[i-1].u.hdr.size4x; in memsys3Merge()
27789 assert( i > mem3.aPool[i-1].u.hdr.prevSize ); in memsys3Merge()
27790 prev = i - mem3.aPool[i-1].u.hdr.prevSize; in memsys3Merge()
27795 size = i + size/4 - prev; in memsys3Merge()
27796 x = mem3.aPool[prev-1].u.hdr.size4x & 2; in memsys3Merge()
27797 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x; in memsys3Merge()
27798 mem3.aPool[prev+size-1].u.hdr.prevSize = size; in memsys3Merge()
27838 i = mem3.aiSmall[nBlock-2]; in memsys3MallocUnsafe()
27840 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]); in memsys3MallocUnsafe()
27846 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){ in memsys3MallocUnsafe()
27879 for(i=0; i<MX_SMALL-1; i++){ in memsys3MallocUnsafe()
27906 i = p - mem3.aPool; in memsys3FreeUnsafe()
27907 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 ); in memsys3FreeUnsafe()
27908 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
27910 mem3.aPool[i-1].u.hdr.size4x &= ~1; in memsys3FreeUnsafe()
27911 mem3.aPool[i+size-1].u.hdr.prevSize = size; in memsys3FreeUnsafe()
27912 mem3.aPool[i+size-1].u.hdr.size4x &= ~2; in memsys3FreeUnsafe()
27917 while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){ in memsys3FreeUnsafe()
27918 size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize; in memsys3FreeUnsafe()
27919 mem3.iKeyBlk -= size; in memsys3FreeUnsafe()
27922 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
27923 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FreeUnsafe()
27924 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FreeUnsafe()
27926 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
27927 while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){ in memsys3FreeUnsafe()
27929 mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
27930 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FreeUnsafe()
27931 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FreeUnsafe()
27938 ** size returned omits the 8-byte header overhead. This only
27945 assert( (pBlock[-1].u.hdr.size4x&1)!=0 ); in memsys3Size()
27946 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4; in memsys3Size()
27956 return ((n+11)&~7) - 4; in memsys3Roundup()
27996 if( nBytes<=nOld && nBytes>=nOld-128 ){ in memsys3Realloc()
28025 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2; in memsys3Init()
28071 size = mem3.aPool[i-1].u.hdr.size4x; in sqlite3Memsys3Dump()
28077 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){ in sqlite3Memsys3Dump()
28082 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){ in sqlite3Memsys3Dump()
28088 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8); in sqlite3Memsys3Dump()
28090 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8, in sqlite3Memsys3Dump()
28094 for(i=0; i<MX_SMALL-1; i++){ in sqlite3Memsys3Dump()
28099 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
28108 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
28113 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szKeyBlk*8); in sqlite3Memsys3Dump()
28114 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnKeyBlk*8); in sqlite3Memsys3Dump()
28130 ** Populate the low-level memory allocation function pointers in
28191 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
28200 ** N >= M*(1 + log2(n)/2) - n + 1
28229 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
28264 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
28295 ** structures, return a pointer to the idx-th such link.
28309 next = MEM5LINK(i)->next; in memsys5Unlink()
28310 prev = MEM5LINK(i)->prev; in memsys5Unlink()
28314 MEM5LINK(prev)->next = next; in memsys5Unlink()
28317 MEM5LINK(next)->prev = prev; in memsys5Unlink()
28332 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize]; in memsys5Link()
28333 MEM5LINK(i)->prev = -1; in memsys5Link()
28336 MEM5LINK(x)->prev = i; in memsys5Link()
28358 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom); in memsys5Size()
28413 iBin--; in memsys5MallocUnsafe()
28424 mem5.totalExcess += iFullSz - nByte; in memsys5MallocUnsafe()
28451 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom); in memsys5FreeUnsafe()
28453 /* Check that the pointer pOld points to a valid, non-free block. */ in memsys5FreeUnsafe()
28455 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 ); in memsys5FreeUnsafe()
28460 assert( iBlock+size-1<(u32)mem5.nBlock ); in memsys5FreeUnsafe()
28463 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE; in memsys5FreeUnsafe()
28468 mem5.currentCount--; in memsys5FreeUnsafe()
28469 mem5.currentOut -= size*mem5.szAtom; in memsys5FreeUnsafe()
28478 iBuddy = iBlock - size; in memsys5FreeUnsafe()
28540 ** memsys5Round(). Hence nBytes is always a non-negative power
28549 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */ in memsys5Realloc()
28572 ** 32-bit signed integer. Hence the largest allocation is 0x40000000
28594 ** Examples: memsys5Log(1) -> 0
28595 ** memsys5Log(2) -> 1
28596 ** memsys5Log(4) -> 2
28597 ** memsys5Log(5) -> 3
28598 ** memsys5Log(8) -> 3
28599 ** memsys5Log(9) -> 4
28603 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++); in memsys5Log()
28628 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 ); in memsys5Init()
28646 mem5.aiFreelist[ii] = -1; in memsys5Init()
28650 for(ii=LOGMAX; ii>=0; ii--){ in memsys5Init()
28700 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){} in sqlite3Memsys5Dump()
28791 ** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
28802 #define SQLITE_MUTEX_WARNONCONTENTION (-1)
28812 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex); in checkMutexHeld()
28815 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex); in checkMutexNotheld()
28846 p->iType = iType; in checkMutexAlloc()
28849 if( iType-2>=ArraySize(staticMutexes) ){ in checkMutexAlloc()
28854 p = &staticMutexes[iType-2]; in checkMutexAlloc()
28857 if( p->mutex==0 ){ in checkMutexAlloc()
28858 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType); in checkMutexAlloc()
28859 if( p->mutex==0 ){ in checkMutexAlloc()
28879 if( ((CheckMutex*)p)->iType<2 ) in checkMutexFree()
28883 pGlobalMutexMethods->xMutexFree(pCheck->mutex); in checkMutexFree()
28898 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){ in checkMutexEnter()
28899 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){ in checkMutexEnter()
28903 "illegal multi-threaded access to database connection" in checkMutexEnter()
28906 pGlobalMutexMethods->xMutexEnter(pCheck->mutex); in checkMutexEnter()
28914 return pGlobalMutexMethods->xMutexTry(pCheck->mutex); in checkMutexTry()
28922 pGlobalMutexMethods->xMutexLeave(pCheck->mutex); in checkMutexLeave()
28952 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE ); in sqlite3MutexWarnOnContention()
28953 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION; in sqlite3MutexWarnOnContention()
28981 pTo->xMutexInit = pFrom->xMutexInit; in sqlite3MutexInit()
28982 pTo->xMutexEnd = pFrom->xMutexEnd; in sqlite3MutexInit()
28983 pTo->xMutexFree = pFrom->xMutexFree; in sqlite3MutexInit()
28984 pTo->xMutexEnter = pFrom->xMutexEnter; in sqlite3MutexInit()
28985 pTo->xMutexTry = pFrom->xMutexTry; in sqlite3MutexInit()
28986 pTo->xMutexLeave = pFrom->xMutexLeave; in sqlite3MutexInit()
28987 pTo->xMutexHeld = pFrom->xMutexHeld; in sqlite3MutexInit()
28988 pTo->xMutexNotheld = pFrom->xMutexNotheld; in sqlite3MutexInit()
28990 pTo->xMutexAlloc = pFrom->xMutexAlloc; in sqlite3MutexInit()
29079 ** this function is a no-op.
29123 ** here are place-holders. Applications can substitute working
29124 ** mutex routines at start-time using the
29197 return p==0 || p->cnt>0; in debugMutexHeld()
29201 return p==0 || p->cnt==0; in debugMutexNotheld()
29216 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1]; in debugMutexAlloc()
29223 pNew->id = id; in debugMutexAlloc()
29224 pNew->cnt = 0; in debugMutexAlloc()
29230 if( id-2<0 || id-2>=ArraySize(aStatic) ){ in debugMutexAlloc()
29235 pNew = &aStatic[id-2]; in debugMutexAlloc()
29236 pNew->id = id; in debugMutexAlloc()
29248 assert( p->cnt==0 ); in debugMutexFree()
29249 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){ in debugMutexFree()
29271 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexEnter()
29272 p->cnt++; in debugMutexEnter()
29276 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexTry()
29277 p->cnt++; in debugMutexTry()
29290 p->cnt--; in debugMutexLeave()
29291 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexLeave()
29313 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
29314 ** is used regardless of the run-time threadsafety setting.
29354 ** home-grown mutexes. Encapsulate these conditions into a single #define.
29397 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
29403 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self())); in pthreadMutexHeld()
29406 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0; in pthreadMutexNotheld()
29459 ** cases where it really needs one. If a faster non-recursive mutex
29500 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
29506 pthread_mutex_init(&p->mutex, &recursiveAttr); in pthreadMutexAlloc()
29510 p->id = SQLITE_MUTEX_RECURSIVE; in pthreadMutexAlloc()
29518 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
29520 p->id = SQLITE_MUTEX_FAST; in pthreadMutexAlloc()
29527 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){ in pthreadMutexAlloc()
29532 p = &staticMutexes[iType-2]; in pthreadMutexAlloc()
29537 assert( p==0 || p->id==iType ); in pthreadMutexAlloc()
29549 assert( p->nRef==0 ); in pthreadMutexFree()
29551 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ) in pthreadMutexFree()
29554 pthread_mutex_destroy(&p->mutex); in pthreadMutexFree()
29576 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexEnter()
29581 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexEnter()
29582 ** and p->owner are equal if p->owner changes between two values in pthreadMutexEnter()
29584 ** This implementation also assumes a coherent cache - that in pthreadMutexEnter()
29591 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexEnter()
29592 p->nRef++; in pthreadMutexEnter()
29594 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
29595 assert( p->nRef==0 ); in pthreadMutexEnter()
29596 p->owner = self; in pthreadMutexEnter()
29597 p->nRef = 1; in pthreadMutexEnter()
29601 /* Use the built-in recursive mutexes if they are available. in pthreadMutexEnter()
29603 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
29605 assert( p->nRef>0 || p->owner==0 ); in pthreadMutexEnter()
29606 p->owner = pthread_self(); in pthreadMutexEnter()
29607 p->nRef++; in pthreadMutexEnter()
29612 if( p->trace ){ in pthreadMutexEnter()
29613 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexEnter()
29619 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexTry()
29624 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexTry()
29625 ** and p->owner are equal if p->owner changes between two values in pthreadMutexTry()
29627 ** This implementation also assumes a coherent cache - that in pthreadMutexTry()
29634 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexTry()
29635 p->nRef++; in pthreadMutexTry()
29637 }else if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
29638 assert( p->nRef==0 ); in pthreadMutexTry()
29639 p->owner = self; in pthreadMutexTry()
29640 p->nRef = 1; in pthreadMutexTry()
29647 /* Use the built-in recursive mutexes if they are available. in pthreadMutexTry()
29649 if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
29651 p->owner = pthread_self(); in pthreadMutexTry()
29652 p->nRef++; in pthreadMutexTry()
29661 if( rc==SQLITE_OK && p->trace ){ in pthreadMutexTry()
29662 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexTry()
29677 p->nRef--; in pthreadMutexLeave()
29678 if( p->nRef==0 ) p->owner = 0; in pthreadMutexLeave()
29680 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); in pthreadMutexLeave()
29683 if( p->nRef==0 ){ in pthreadMutexLeave()
29684 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
29687 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
29691 if( p->trace ){ in pthreadMutexLeave()
29692 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexLeave()
29797 ** Determine if we are dealing with Windows CE - which has a much reduced
29825 ** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
29880 return p->nRef!=0 && p->owner==GetCurrentThreadId(); in winMutexHeld()
29884 return p->nRef==0 || p->owner!=tid; in winMutexNotheld2()
29929 static int winMutex_isNt = -1; /* <0 means "need to query" */
30007 ** cases where it really needs one. If a faster non-recursive mutex
30033 p->id = iType; in winMutexAlloc()
30036 p->trace = 1; in winMutexAlloc()
30040 InitializeCriticalSectionEx(&p->mutex, 0, 0); in winMutexAlloc()
30042 InitializeCriticalSection(&p->mutex); in winMutexAlloc()
30049 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){ in winMutexAlloc()
30054 p = &winMutex_staticMutexes[iType-2]; in winMutexAlloc()
30057 InterlockedCompareExchange(&p->trace, 1, 0); in winMutexAlloc()
30063 assert( p==0 || p->id==iType ); in winMutexAlloc()
30075 assert( p->nRef==0 && p->owner==0 ); in winMutexFree()
30076 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){ in winMutexFree()
30077 DeleteCriticalSection(&p->mutex); in winMutexFree()
30103 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); in winMutexEnter()
30108 EnterCriticalSection(&p->mutex); in winMutexEnter()
30110 assert( p->nRef>0 || p->owner==0 ); in winMutexEnter()
30111 p->owner = tid; in winMutexEnter()
30112 p->nRef++; in winMutexEnter()
30113 if( p->trace ){ in winMutexEnter()
30114 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", in winMutexEnter()
30115 tid, p->id, p, p->trace, p->nRef)); in winMutexEnter()
30126 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); in winMutexTry()
30140 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 ); in winMutexTry()
30145 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){ in winMutexTry()
30147 p->owner = tid; in winMutexTry()
30148 p->nRef++; in winMutexTry()
30156 if( p->trace ){ in winMutexTry()
30157 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n", in winMutexTry()
30158 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc))); in winMutexTry()
30176 assert( p->nRef>0 ); in winMutexLeave()
30177 assert( p->owner==tid ); in winMutexLeave()
30178 p->nRef--; in winMutexLeave()
30179 if( p->nRef==0 ) p->owner = 0; in winMutexLeave()
30180 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); in winMutexLeave()
30183 LeaveCriticalSection(&p->mutex); in winMutexLeave()
30185 if( p->trace ){ in winMutexLeave()
30186 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", in winMutexLeave()
30187 tid, p->id, p, p->trace, p->nRef)); in winMutexLeave()
30234 ** Attempt to release up to n bytes of non-essential memory currently
30235 ** held by SQLite. An example of non-essential memory is memory used to
30242 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine in sqlite3_release_memory()
30243 ** is a no-op returning zero if SQLite is not compiled with in sqlite3_release_memory()
30285 ** no-op.
30300 ** Set the soft heap-size limit for the library. An argument of
30301 ** zero disables the limit. A negative argument is a no-op used to
30316 if( rc ) return -1; in sqlite3_soft_heap_limit64()
30331 excess = sqlite3_memory_used() - n; in sqlite3_soft_heap_limit64()
30341 ** Set the hard heap-size limit for the library. An argument of zero
30342 ** disables the hard heap limit. A negative argument is a no-op used
30356 if( rc ) return -1; in sqlite3_hard_heap_limit64()
30391 ** Return true if the heap is currently under memory pressure - in other
30441 ** This routine is called whenever an out-of-memory condition is seen,
30443 ** code debuggers when working on out-of-memory conditions, for example
30454 # define test_oom_breakpoint(X) /* No-op for production builds */
30467 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal in mallocWithAlarm()
30477 if( nUsed >= mem0.alarmThreshold - nFull ){ in mallocWithAlarm()
30482 if( nUsed >= mem0.hardLimit - nFull ){ in mallocWithAlarm()
30514 ** This provides a 256-byte safety margin for defense against 32-bit
30543 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */ in sqlite3Malloc()
30570 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd); in isLookaside()
30586 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL; in lookasideMallocSize()
30588 return db->lookaside.szTrue; in lookasideMallocSize()
30603 if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){ in sqlite3DbMallocSize()
30605 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ in sqlite3DbMallocSize()
30606 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocSize()
30610 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ in sqlite3DbMallocSize()
30611 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocSize()
30612 return db->lookaside.szTrue; in sqlite3DbMallocSize()
30628 if( p==0 ) return; /* IMP: R-49053-54554 */ in sqlite3_free()
30644 ** *db->pnBytesFreed.
30647 *db->pnBytesFreed += sqlite3DbMallocSize(db,p); in measureAllocationSize()
30652 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
30653 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
30656 assert( db==0 || sqlite3_mutex_held(db->mutex) ); in sqlite3DbFreeNN()
30659 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ in sqlite3DbFreeNN()
30661 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ in sqlite3DbFreeNN()
30663 assert( db->pnBytesFreed==0 ); in sqlite3DbFreeNN()
30667 pBuf->pNext = db->lookaside.pSmallFree; in sqlite3DbFreeNN()
30668 db->lookaside.pSmallFree = pBuf; in sqlite3DbFreeNN()
30672 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ in sqlite3DbFreeNN()
30674 assert( db->pnBytesFreed==0 ); in sqlite3DbFreeNN()
30676 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ in sqlite3DbFreeNN()
30678 pBuf->pNext = db->lookaside.pFree; in sqlite3DbFreeNN()
30679 db->lookaside.pFree = pBuf; in sqlite3DbFreeNN()
30683 if( db->pnBytesFreed ){ in sqlite3DbFreeNN()
30696 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbNNFreeNN()
30698 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ in sqlite3DbNNFreeNN()
30700 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ in sqlite3DbNNFreeNN()
30702 assert( db->pnBytesFreed==0 ); in sqlite3DbNNFreeNN()
30706 pBuf->pNext = db->lookaside.pSmallFree; in sqlite3DbNNFreeNN()
30707 db->lookaside.pSmallFree = pBuf; in sqlite3DbNNFreeNN()
30711 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ in sqlite3DbNNFreeNN()
30713 assert( db->pnBytesFreed==0 ); in sqlite3DbNNFreeNN()
30715 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ in sqlite3DbNNFreeNN()
30717 pBuf->pNext = db->lookaside.pFree; in sqlite3DbNNFreeNN()
30718 db->lookaside.pFree = pBuf; in sqlite3DbNNFreeNN()
30722 if( db->pnBytesFreed ){ in sqlite3DbNNFreeNN()
30732 assert( db==0 || sqlite3_mutex_held(db->mutex) ); in sqlite3DbFree()
30745 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ in sqlite3Realloc()
30748 sqlite3_free(pOld); /* IMP: R-26507-47431 */ in sqlite3Realloc()
30756 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second in sqlite3Realloc()
30766 nDiff = nNew - nOld; in sqlite3Realloc()
30768 mem0.alarmThreshold-nDiff ){ in sqlite3Realloc()
30770 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){ in sqlite3Realloc()
30785 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); in sqlite3Realloc()
30791 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */ in sqlite3Realloc()
30803 if( n<0 ) n = 0; /* IMP: R-26507-47431 */ in sqlite3_realloc()
30847 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); in dbMallocRawFinish()
30856 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
30884 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocRawNN()
30885 assert( db->pnBytesFreed==0 ); in sqlite3DbMallocRawNN()
30886 if( n>db->lookaside.sz ){ in sqlite3DbMallocRawNN()
30887 if( !db->lookaside.bDisable ){ in sqlite3DbMallocRawNN()
30888 db->lookaside.anStat[1]++; in sqlite3DbMallocRawNN()
30889 }else if( db->mallocFailed ){ in sqlite3DbMallocRawNN()
30896 if( (pBuf = db->lookaside.pSmallFree)!=0 ){ in sqlite3DbMallocRawNN()
30897 db->lookaside.pSmallFree = pBuf->pNext; in sqlite3DbMallocRawNN()
30898 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
30900 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){ in sqlite3DbMallocRawNN()
30901 db->lookaside.pSmallInit = pBuf->pNext; in sqlite3DbMallocRawNN()
30902 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
30907 if( (pBuf = db->lookaside.pFree)!=0 ){ in sqlite3DbMallocRawNN()
30908 db->lookaside.pFree = pBuf->pNext; in sqlite3DbMallocRawNN()
30909 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
30911 }else if( (pBuf = db->lookaside.pInit)!=0 ){ in sqlite3DbMallocRawNN()
30912 db->lookaside.pInit = pBuf->pNext; in sqlite3DbMallocRawNN()
30913 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
30916 db->lookaside.anStat[2]++; in sqlite3DbMallocRawNN()
30920 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocRawNN()
30921 assert( db->pnBytesFreed==0 ); in sqlite3DbMallocRawNN()
30922 if( db->mallocFailed ){ in sqlite3DbMallocRawNN()
30939 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbRealloc()
30940 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){ in sqlite3DbRealloc()
30942 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){ in sqlite3DbRealloc()
30946 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){ in sqlite3DbRealloc()
30947 if( n<=db->lookaside.szTrue ) return p; in sqlite3DbRealloc()
30956 if( db->mallocFailed==0 ){ in dbReallocFinish()
30972 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); in dbReallocFinish()
31033 ** at least one non-space character */ in sqlite3DbSpanDup()
31037 n = (int)(zEnd - zStart); in sqlite3DbSpanDup()
31038 while( sqlite3Isspace(zStart[n-1]) ) n--; in sqlite3DbSpanDup()
31052 ** Call this routine to record the fact that an OOM (out-of-memory) error
31053 ** has happened. This routine will set db->mallocFailed, and also
31065 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){ in sqlite3OomFault()
31066 db->mallocFailed = 1; in sqlite3OomFault()
31067 if( db->nVdbeExec>0 ){ in sqlite3OomFault()
31068 AtomicStore(&db->u1.isInterrupted, 1); in sqlite3OomFault()
31071 if( db->pParse ){ in sqlite3OomFault()
31073 sqlite3ErrorMsg(db->pParse, "out of memory"); in sqlite3OomFault()
31074 db->pParse->rc = SQLITE_NOMEM_BKPT; in sqlite3OomFault()
31075 for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){ in sqlite3OomFault()
31076 pParse->nErr++; in sqlite3OomFault()
31077 pParse->rc = SQLITE_NOMEM; in sqlite3OomFault()
31086 ** db->mallocFailed flag as necessary.
31092 if( db->mallocFailed && db->nVdbeExec==0 ){ in sqlite3OomClear()
31093 db->mallocFailed = 0; in sqlite3OomClear()
31094 AtomicStore(&db->u1.isInterrupted, 0); in sqlite3OomClear()
31095 assert( db->lookaside.bDisable>0 ); in sqlite3OomClear()
31104 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ in apiHandleError()
31109 return rc & db->errMask; in apiHandleError()
31121 ** If an OOM as occurred, then the connection error-code (the value
31126 ** Otherwise the read (and possible write) of db->mallocFailed in sqlite3ApiExit()
31130 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3ApiExit()
31131 if( db->mallocFailed || rc ){ in sqlite3ApiExit()
31145 ** This file contains code for a set of "printf"-like routines. These
31156 #define etRADIX 0 /* non-decimal integer types. %x %o */
31172 #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
31173 #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
31180 ** An "etByte" is an 8-bit unsigned value.
31209 static const char aPrefix[] = "-x0\000X0";
31251 p->accError = eError; in sqlite3StrAccumSetError()
31252 if( p->mxAlloc ) sqlite3_str_reset(p); in sqlite3StrAccumSetError()
31253 if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError); in sqlite3StrAccumSetError()
31260 if( p->nArg<=p->nUsed ) return 0; in getIntArg()
31261 return sqlite3_value_int64(p->apArg[p->nUsed++]); in getIntArg()
31264 if( p->nArg<=p->nUsed ) return 0.0; in getDoubleArg()
31265 return sqlite3_value_double(p->apArg[p->nUsed++]); in getDoubleArg()
31268 if( p->nArg<=p->nUsed ) return 0; in getTextArg()
31269 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); in getTextArg()
31283 if( pAccum->accError ) return 0; in printfTempBuf()
31284 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){ in printfTempBuf()
31288 z = sqlite3DbMallocRaw(pAccum->db, n); in printfTempBuf()
31305 ** Hard limit on the precision of floating-point conversions.
31325 etByte flag_leftjustify; /* True if "-" flag is present */ in sqlite3_str_vappendf()
31335 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ in sqlite3_str_vappendf()
31352 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); in sqlite3_str_vappendf()
31355 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){ in sqlite3_str_vappendf()
31369 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt)); in sqlite3_str_vappendf()
31382 precision = -1; in sqlite3_str_vappendf()
31385 case '-': flag_leftjustify = 1; break; in sqlite3_str_vappendf()
31405 unsigned wx = c - '0'; in sqlite3_str_vappendf()
31407 wx = wx*10 + c - '0'; in sqlite3_str_vappendf()
31419 fmt--; in sqlite3_str_vappendf()
31431 width = width >= -2147483647 ? -width : 0; in sqlite3_str_vappendf()
31453 precision = precision >= -2147483647 ? -precision : -1; in sqlite3_str_vappendf()
31459 px = px*10 + c - '0'; in sqlite3_str_vappendf()
31471 --fmt; in sqlite3_str_vappendf()
31486 xtype = infop->type; in sqlite3_str_vappendf()
31497 ** flag_leftjustify TRUE if a '-' is present or if the in sqlite3_str_vappendf()
31502 ** always non-negative. Zero is the default. in sqlite3_str_vappendf()
31504 ** is -1. in sqlite3_str_vappendf()
31509 assert( precision>=(-1) ); in sqlite3_str_vappendf()
31520 if( infop->flags & FLAG_SIGNED ){ in sqlite3_str_vappendf()
31535 testcase( v==(-1) ); in sqlite3_str_vappendf()
31538 prefix = '-'; in sqlite3_str_vappendf()
31558 if( flag_zeropad && precision<width-(prefix!=0) ){ in sqlite3_str_vappendf()
31559 precision = width-(prefix!=0); in sqlite3_str_vappendf()
31561 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){ in sqlite3_str_vappendf()
31572 bufpt = &zOut[nOut-1]; in sqlite3_str_vappendf()
31579 *(--bufpt) = zOrd[x*2+1]; in sqlite3_str_vappendf()
31580 *(--bufpt) = zOrd[x*2]; in sqlite3_str_vappendf()
31583 const char *cset = &aDigits[infop->charset]; in sqlite3_str_vappendf()
31584 u8 base = infop->base; in sqlite3_str_vappendf()
31586 *(--bufpt) = cset[longvalue%base]; in sqlite3_str_vappendf()
31590 length = (int)(&zOut[nOut-1]-bufpt); in sqlite3_str_vappendf()
31592 *(--bufpt) = '0'; /* Zero pad */ in sqlite3_str_vappendf()
31596 int nn = (length - 1)/3; /* Number of "," to insert */ in sqlite3_str_vappendf()
31597 int ix = (length - 1)%3 + 1; in sqlite3_str_vappendf()
31598 bufpt -= nn; in sqlite3_str_vappendf()
31601 ix--; in sqlite3_str_vappendf()
31604 nn--; in sqlite3_str_vappendf()
31609 if( prefix ) *(--bufpt) = prefix; /* Add sign */ in sqlite3_str_vappendf()
31610 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ in sqlite3_str_vappendf()
31613 pre = &aPrefix[infop->prefix]; in sqlite3_str_vappendf()
31614 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; in sqlite3_str_vappendf()
31616 length = (int)(&zOut[nOut-1]-bufpt); in sqlite3_str_vappendf()
31637 iRound = -precision; in sqlite3_str_vappendf()
31655 memcpy(buf, "-Inf", 5); in sqlite3_str_vappendf()
31657 if( s.sign=='-' ){ in sqlite3_str_vappendf()
31658 /* no-op */ in sqlite3_str_vappendf()
31668 if( s.sign=='-' ){ in sqlite3_str_vappendf()
31669 prefix = '-'; in sqlite3_str_vappendf()
31674 exp = s.iDP-1; in sqlite3_str_vappendf()
31682 precision--; in sqlite3_str_vappendf()
31684 if( exp<-4 || exp>precision ){ in sqlite3_str_vappendf()
31687 precision = precision - exp; in sqlite3_str_vappendf()
31696 e2 = s.iDP - 1; in sqlite3_str_vappendf()
31719 for(; e2>=0; e2--){ in sqlite3_str_vappendf()
31730 for(e2++; e2<0 && precision>0; precision--, e2++){ in sqlite3_str_vappendf()
31734 while( (precision--)>0 ){ in sqlite3_str_vappendf()
31739 while( bufpt[-1]=='0' ) *(--bufpt) = 0; in sqlite3_str_vappendf()
31741 if( bufpt[-1]=='.' ){ in sqlite3_str_vappendf()
31745 *(--bufpt) = 0; in sqlite3_str_vappendf()
31751 exp = s.iDP - 1; in sqlite3_str_vappendf()
31752 *(bufpt++) = aDigits[infop->charset]; in sqlite3_str_vappendf()
31754 *(bufpt++) = '-'; exp = -exp; in sqlite3_str_vappendf()
31770 length = (int)(bufpt-zOut); in sqlite3_str_vappendf()
31777 int nPad = width - length; in sqlite3_str_vappendf()
31778 for(i=width; i>=nPad; i--){ in sqlite3_str_vappendf()
31779 bufpt[i] = bufpt[i-nPad]; in sqlite3_str_vappendf()
31782 while( nPad-- ) bufpt[i++] = '0'; in sqlite3_str_vappendf()
31789 *(va_arg(ap,int*)) = pAccum->nChar; in sqlite3_str_vappendf()
31836 width -= precision-1; in sqlite3_str_vappendf()
31838 sqlite3_str_appendchar(pAccum, width-1, ' '); in sqlite3_str_vappendf()
31842 precision--; in sqlite3_str_vappendf()
31845 if( nPrior > precision-1 ) nPrior = precision - 1; in sqlite3_str_vappendf()
31847 if( nCopyBytes + pAccum->nChar >= pAccum->nAlloc ){ in sqlite3_str_vappendf()
31850 if( pAccum->accError ) break; in sqlite3_str_vappendf()
31852 &pAccum->zText[pAccum->nChar-nCopyBytes], nCopyBytes); in sqlite3_str_vappendf()
31853 precision -= nPrior; in sqlite3_str_vappendf()
31871 if( pAccum->nChar==0 in sqlite3_str_vappendf()
31872 && pAccum->mxAlloc in sqlite3_str_vappendf()
31875 && pAccum->accError==0 in sqlite3_str_vappendf()
31880 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); in sqlite3_str_vappendf()
31881 pAccum->zText = bufpt; in sqlite3_str_vappendf()
31882 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt); in sqlite3_str_vappendf()
31883 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt); in sqlite3_str_vappendf()
31884 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED; in sqlite3_str_vappendf()
31895 while( precision-- > 0 && z[0] ){ in sqlite3_str_vappendf()
31898 length = (int)(z - (unsigned char*)bufpt); in sqlite3_str_vappendf()
31907 /* Adjust width to account for extra bytes in UTF-8 characters */ in sqlite3_str_vappendf()
31908 int ii = length - 1; in sqlite3_str_vappendf()
31909 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++; in sqlite3_str_vappendf()
31934 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ in sqlite3_str_vappendf()
31961 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; in sqlite3_str_vappendf()
31966 sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken); in sqlite3_str_vappendf()
31967 sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr); in sqlite3_str_vappendf()
31973 if( pToken && pToken->n ){ in sqlite3_str_vappendf()
31974 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n); in sqlite3_str_vappendf()
31975 sqlite3RecordErrorByteOffset(pAccum->db, pToken->z); in sqlite3_str_vappendf()
31983 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; in sqlite3_str_vappendf()
31986 if( pItem->zAlias && !flag_altform2 ){ in sqlite3_str_vappendf()
31987 sqlite3_str_appendall(pAccum, pItem->zAlias); in sqlite3_str_vappendf()
31988 }else if( pItem->zName ){ in sqlite3_str_vappendf()
31989 if( pItem->zDatabase ){ in sqlite3_str_vappendf()
31990 sqlite3_str_appendall(pAccum, pItem->zDatabase); in sqlite3_str_vappendf()
31993 sqlite3_str_appendall(pAccum, pItem->zName); in sqlite3_str_vappendf()
31994 }else if( pItem->zAlias ){ in sqlite3_str_vappendf()
31995 sqlite3_str_appendall(pAccum, pItem->zAlias); in sqlite3_str_vappendf()
31997 Select *pSel = pItem->pSelect; in sqlite3_str_vappendf()
31998 assert( pSel!=0 ); /* Because of tag-20240424-1 */ in sqlite3_str_vappendf()
31999 if( pSel->selFlags & SF_NestedFrom ){ in sqlite3_str_vappendf()
32000 sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId); in sqlite3_str_vappendf()
32001 }else if( pSel->selFlags & SF_MultiValue ){ in sqlite3_str_vappendf()
32002 assert( !pItem->fg.isTabFunc && !pItem->fg.isIndexedBy ); in sqlite3_str_vappendf()
32003 sqlite3_str_appendf(pAccum, "%u-ROW VALUES CLAUSE", in sqlite3_str_vappendf()
32004 pItem->u1.nRow); in sqlite3_str_vappendf()
32006 sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId); in sqlite3_str_vappendf()
32025 width -= length; in sqlite3_str_vappendf()
32035 sqlite3DbFree(pAccum->db, zExtra); in sqlite3_str_vappendf()
32054 if( db->errByteOffset!=(-2) ) return; in sqlite3RecordErrorByteOffset()
32055 pParse = db->pParse; in sqlite3RecordErrorByteOffset()
32057 zText =pParse->zTail; in sqlite3RecordErrorByteOffset()
32061 db->errByteOffset = (int)(z-zText); in sqlite3RecordErrorByteOffset()
32071 && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0) in sqlite3RecordErrorOffsetOfExpr()
32073 pExpr = pExpr->pLeft; in sqlite3RecordErrorOffsetOfExpr()
32076 db->errByteOffset = pExpr->w.iOfst; in sqlite3RecordErrorOffsetOfExpr()
32088 assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */ in sqlite3StrAccumEnlarge()
32089 if( p->accError ){ in sqlite3StrAccumEnlarge()
32090 testcase(p->accError==SQLITE_TOOBIG); in sqlite3StrAccumEnlarge()
32091 testcase(p->accError==SQLITE_NOMEM); in sqlite3StrAccumEnlarge()
32094 if( p->mxAlloc==0 ){ in sqlite3StrAccumEnlarge()
32096 return p->nAlloc - p->nChar - 1; in sqlite3StrAccumEnlarge()
32098 char *zOld = isMalloced(p) ? p->zText : 0; in sqlite3StrAccumEnlarge()
32099 i64 szNew = p->nChar + N + 1; in sqlite3StrAccumEnlarge()
32100 if( szNew+p->nChar<=p->mxAlloc ){ in sqlite3StrAccumEnlarge()
32103 szNew += p->nChar; in sqlite3StrAccumEnlarge()
32105 if( szNew > p->mxAlloc ){ in sqlite3StrAccumEnlarge()
32110 p->nAlloc = (int)szNew; in sqlite3StrAccumEnlarge()
32112 if( p->db ){ in sqlite3StrAccumEnlarge()
32113 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); in sqlite3StrAccumEnlarge()
32115 zNew = sqlite3Realloc(zOld, p->nAlloc); in sqlite3StrAccumEnlarge()
32118 assert( p->zText!=0 || p->nChar==0 ); in sqlite3StrAccumEnlarge()
32119 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); in sqlite3StrAccumEnlarge()
32120 p->zText = zNew; in sqlite3StrAccumEnlarge()
32121 p->nAlloc = sqlite3DbMallocSize(p->db, zNew); in sqlite3StrAccumEnlarge()
32122 p->printfFlags |= SQLITE_PRINTF_MALLOCED; in sqlite3StrAccumEnlarge()
32137 testcase( p->nChar + (i64)N > 0x7fffffff ); in sqlite3_str_appendchar()
32138 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ in sqlite3_str_appendchar()
32141 while( (N--)>0 ) p->zText[p->nChar++] = c; in sqlite3_str_appendchar()
32148 ** This is a helper routine to sqlite3_str_append() that does special-case
32155 memcpy(&p->zText[p->nChar], z, N); in enlargeAndAppend()
32156 p->nChar += N; in enlargeAndAppend()
32166 assert( p->zText!=0 || p->nChar==0 || p->accError ); in sqlite3_str_append()
32168 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 ); in sqlite3_str_append()
32169 if( p->nChar+N >= p->nAlloc ){ in sqlite3_str_append()
32172 assert( p->zText ); in sqlite3_str_append()
32173 p->nChar += N; in sqlite3_str_append()
32174 memcpy(&p->zText[p->nChar-N], z, N); in sqlite3_str_append()
32179 ** Append the complete text of zero-terminated string z[] to the p string.
32187 ** Finish off a string by making sure it is zero-terminated.
32193 assert( p->mxAlloc>0 && !isMalloced(p) ); in strAccumFinishRealloc()
32194 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); in strAccumFinishRealloc()
32196 memcpy(zText, p->zText, p->nChar+1); in strAccumFinishRealloc()
32197 p->printfFlags |= SQLITE_PRINTF_MALLOCED; in strAccumFinishRealloc()
32201 p->zText = zText; in strAccumFinishRealloc()
32205 if( p->zText ){ in sqlite3StrAccumFinish()
32206 p->zText[p->nChar] = 0; in sqlite3StrAccumFinish()
32207 if( p->mxAlloc>0 && !isMalloced(p) ){ in sqlite3StrAccumFinish()
32211 return p->zText; in sqlite3StrAccumFinish()
32219 if( p->accError ){ in sqlite3ResultStrAccum()
32220 sqlite3_result_error_code(pCtx, p->accError); in sqlite3ResultStrAccum()
32223 sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC); in sqlite3ResultStrAccum()
32255 return p ? p->accError : SQLITE_NOMEM; in sqlite3_str_errcode()
32260 return p ? p->nChar : 0; in sqlite3_str_length()
32265 if( p==0 || p->nChar==0 ) return 0; in sqlite3_str_value()
32266 p->zText[p->nChar] = 0; in sqlite3_str_value()
32267 return p->zText; in sqlite3_str_value()
32275 sqlite3DbFree(p->db, p->zText); in sqlite3_str_reset()
32276 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; in sqlite3_str_reset()
32278 p->nAlloc = 0; in sqlite3_str_reset()
32279 p->nChar = 0; in sqlite3_str_reset()
32280 p->zText = 0; in sqlite3_str_reset()
32288 ** memory is used if not NULL. db->mallocFailed is set appropriately
32298 p->zText = zBase; in sqlite3StrAccumInit()
32299 p->db = db; in sqlite3StrAccumInit()
32300 p->nAlloc = n; in sqlite3StrAccumInit()
32301 p->mxAlloc = mx; in sqlite3StrAccumInit()
32302 p->nChar = 0; in sqlite3StrAccumInit()
32303 p->accError = 0; in sqlite3StrAccumInit()
32304 p->printfFlags = 0; in sqlite3StrAccumInit()
32312 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); in sqlite3_str_new()
32321 ** %-conversion extensions.
32329 db->aLimit[SQLITE_LIMIT_LENGTH]); in sqlite3VMPrintf()
32341 ** %-conversion extensions.
32354 ** %-conversion extensions.
32378 ** %-conversion extensions.
32442 ** stack space on small-stack systems when logging is disabled.
32504 ** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
32527 p--; in sqlite3RCStrRef()
32528 p->nRCRef++; in sqlite3RCStrRef()
32539 p--; in sqlite3RCStrUnref()
32540 assert( p->nRCRef>0 ); in sqlite3RCStrUnref()
32541 if( p->nRCRef>=2 ){ in sqlite3RCStrUnref()
32542 p->nRCRef--; in sqlite3RCStrUnref()
32560 p->nRCRef = 1; in sqlite3RCStrNew()
32572 p--; in sqlite3RCStrResize()
32573 assert( p->nRCRef==1 ); in sqlite3RCStrResize()
32586 ** 2015-06-08
32618 p->iLevel++; in sqlite3TreeViewPush()
32621 if( p->iLevel<(int)sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow; in sqlite3TreeViewPush()
32630 p->iLevel--; in sqlite3TreeViewPop()
32631 if( p->iLevel<0 ){ in sqlite3TreeViewPop()
32648 for(i=0; i<p->iLevel && i<(int)sizeof(p->bLine)-1; i++){ in sqlite3TreeViewLine()
32649 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4); in sqlite3TreeViewLine()
32651 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); in sqlite3TreeViewLine()
32687 int colMoreToFollow = i<(nCol - 1); in sqlite3TreeViewColumnList()
32702 printf(" X-%s", z); in sqlite3TreeViewColumnList()
32710 if( flg & COLFLAG_NOEXPAND ) printf(" NO-EXPAND"); in sqlite3TreeViewColumnList()
32721 ** Generate a human-readable description of a WITH clause.
32726 if( pWith->nCte==0 ) return; in sqlite3TreeViewWith()
32727 if( pWith->pOuter ){ in sqlite3TreeViewWith()
32728 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter); in sqlite3TreeViewWith()
32732 if( pWith->nCte>0 ){ in sqlite3TreeViewWith()
32734 for(i=0; i<pWith->nCte; i++){ in sqlite3TreeViewWith()
32737 const struct Cte *pCte = &pWith->a[i]; in sqlite3TreeViewWith()
32739 sqlite3_str_appendf(&x, "%s", pCte->zName); in sqlite3TreeViewWith()
32740 if( pCte->pCols && pCte->pCols->nExpr>0 ){ in sqlite3TreeViewWith()
32743 for(j=0; j<pCte->pCols->nExpr; j++){ in sqlite3TreeViewWith()
32744 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName); in sqlite3TreeViewWith()
32749 if( pCte->eM10d!=M10d_Any ){ in sqlite3TreeViewWith()
32751 pCte->eM10d==M10d_No ? "NOT " : ""); in sqlite3TreeViewWith()
32753 if( pCte->pUse ){ in sqlite3TreeViewWith()
32754 sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse, in sqlite3TreeViewWith()
32755 pCte->pUse->nUse); in sqlite3TreeViewWith()
32758 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1); in sqlite3TreeViewWith()
32759 sqlite3TreeViewSelect(pView, pCte->pSelect, 0); in sqlite3TreeViewWith()
32767 ** Generate a human-readable description of a SrcList object.
32772 for(i=0; i<pSrc->nSrc; i++){ in sqlite3TreeViewSrcList()
32773 const SrcItem *pItem = &pSrc->a[i]; in sqlite3TreeViewSrcList()
32779 sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem); in sqlite3TreeViewSrcList()
32780 if( pItem->pTab ){ in sqlite3TreeViewSrcList()
32782 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, in sqlite3TreeViewSrcList()
32783 pItem->colUsed, in sqlite3TreeViewSrcList()
32784 pItem->fg.rowidUsed ? "+rowid" : ""); in sqlite3TreeViewSrcList()
32786 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==(JT_LEFT|JT_RIGHT) ){ in sqlite3TreeViewSrcList()
32787 sqlite3_str_appendf(&x, " FULL-OUTER-JOIN"); in sqlite3TreeViewSrcList()
32788 }else if( pItem->fg.jointype & JT_LEFT ){ in sqlite3TreeViewSrcList()
32789 sqlite3_str_appendf(&x, " LEFT-JOIN"); in sqlite3TreeViewSrcList()
32790 }else if( pItem->fg.jointype & JT_RIGHT ){ in sqlite3TreeViewSrcList()
32791 sqlite3_str_appendf(&x, " RIGHT-JOIN"); in sqlite3TreeViewSrcList()
32792 }else if( pItem->fg.jointype & JT_CROSS ){ in sqlite3TreeViewSrcList()
32793 sqlite3_str_appendf(&x, " CROSS-JOIN"); in sqlite3TreeViewSrcList()
32795 if( pItem->fg.jointype & JT_LTORJ ){ in sqlite3TreeViewSrcList()
32798 if( pItem->fg.fromDDL ){ in sqlite3TreeViewSrcList()
32801 if( pItem->fg.isCte ){ in sqlite3TreeViewSrcList()
32802 sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse); in sqlite3TreeViewSrcList()
32804 if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){ in sqlite3TreeViewSrcList()
32807 if( pItem->fg.isTabFunc ) sqlite3_str_appendf(&x, " isTabFunc"); in sqlite3TreeViewSrcList()
32808 if( pItem->fg.isCorrelated ) sqlite3_str_appendf(&x, " isCorrelated"); in sqlite3TreeViewSrcList()
32809 if( pItem->fg.isMaterialized ) sqlite3_str_appendf(&x, " isMaterialized"); in sqlite3TreeViewSrcList()
32810 if( pItem->fg.viaCoroutine ) sqlite3_str_appendf(&x, " viaCoroutine"); in sqlite3TreeViewSrcList()
32811 if( pItem->fg.notCte ) sqlite3_str_appendf(&x, " notCte"); in sqlite3TreeViewSrcList()
32812 if( pItem->fg.isNestedFrom ) sqlite3_str_appendf(&x, " isNestedFrom"); in sqlite3TreeViewSrcList()
32815 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1); in sqlite3TreeViewSrcList()
32817 if( pItem->pSelect ) n++; in sqlite3TreeViewSrcList()
32818 if( pItem->fg.isTabFunc ) n++; in sqlite3TreeViewSrcList()
32819 if( pItem->fg.isUsing ) n++; in sqlite3TreeViewSrcList()
32820 if( pItem->fg.isUsing ){ in sqlite3TreeViewSrcList()
32821 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING"); in sqlite3TreeViewSrcList()
32823 if( pItem->pSelect ){ in sqlite3TreeViewSrcList()
32824 sqlite3TreeViewPush(&pView, i+1<pSrc->nSrc); in sqlite3TreeViewSrcList()
32825 if( pItem->pTab ){ in sqlite3TreeViewSrcList()
32826 Table *pTab = pItem->pTab; in sqlite3TreeViewSrcList()
32827 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1); in sqlite3TreeViewSrcList()
32829 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) ); in sqlite3TreeViewSrcList()
32830 sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0); in sqlite3TreeViewSrcList()
32833 if( pItem->fg.isTabFunc ){ in sqlite3TreeViewSrcList()
32834 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); in sqlite3TreeViewSrcList()
32841 ** Generate a human-readable description of a Select object.
32847 sqlite3TreeViewLine(pView, "nil-SELECT"); in sqlite3TreeViewSelect()
32851 if( p->pWith ){ in sqlite3TreeViewSelect()
32852 sqlite3TreeViewWith(pView, p->pWith, 1); in sqlite3TreeViewSelect()
32857 if( p->selFlags & SF_WhereBegin ){ in sqlite3TreeViewSelect()
32862 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), in sqlite3TreeViewSelect()
32863 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), in sqlite3TreeViewSelect()
32864 p->selId, p, p->selFlags, in sqlite3TreeViewSelect()
32865 (int)p->nSelectRow in sqlite3TreeViewSelect()
32869 if( p->pPrior ){ in sqlite3TreeViewSelect()
32873 if( p->pSrc && p->pSrc->nSrc ) n++; in sqlite3TreeViewSelect()
32874 if( p->pWhere ) n++; in sqlite3TreeViewSelect()
32875 if( p->pGroupBy ) n++; in sqlite3TreeViewSelect()
32876 if( p->pHaving ) n++; in sqlite3TreeViewSelect()
32877 if( p->pOrderBy ) n++; in sqlite3TreeViewSelect()
32878 if( p->pLimit ) n++; in sqlite3TreeViewSelect()
32880 if( p->pWin ) n++; in sqlite3TreeViewSelect()
32881 if( p->pWinDefn ) n++; in sqlite3TreeViewSelect()
32884 if( p->pEList ){ in sqlite3TreeViewSelect()
32885 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set"); in sqlite3TreeViewSelect()
32887 n--; in sqlite3TreeViewSelect()
32889 if( p->pWin ){ in sqlite3TreeViewSelect()
32891 sqlite3TreeViewPush(&pView, (n--)>0); in sqlite3TreeViewSelect()
32892 sqlite3TreeViewLine(pView, "window-functions"); in sqlite3TreeViewSelect()
32893 for(pX=p->pWin; pX; pX=pX->pNextWin){ in sqlite3TreeViewSelect()
32894 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0); in sqlite3TreeViewSelect()
32899 if( p->pSrc && p->pSrc->nSrc ){ in sqlite3TreeViewSelect()
32900 sqlite3TreeViewPush(&pView, (n--)>0); in sqlite3TreeViewSelect()
32902 sqlite3TreeViewSrcList(pView, p->pSrc); in sqlite3TreeViewSelect()
32905 if( p->pWhere ){ in sqlite3TreeViewSelect()
32906 sqlite3TreeViewItem(pView, "WHERE", (n--)>0); in sqlite3TreeViewSelect()
32907 sqlite3TreeViewExpr(pView, p->pWhere, 0); in sqlite3TreeViewSelect()
32910 if( p->pGroupBy ){ in sqlite3TreeViewSelect()
32911 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); in sqlite3TreeViewSelect()
32913 if( p->pHaving ){ in sqlite3TreeViewSelect()
32914 sqlite3TreeViewItem(pView, "HAVING", (n--)>0); in sqlite3TreeViewSelect()
32915 sqlite3TreeViewExpr(pView, p->pHaving, 0); in sqlite3TreeViewSelect()
32919 if( p->pWinDefn ){ in sqlite3TreeViewSelect()
32921 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0); in sqlite3TreeViewSelect()
32922 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){ in sqlite3TreeViewSelect()
32923 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0); in sqlite3TreeViewSelect()
32928 if( p->pOrderBy ){ in sqlite3TreeViewSelect()
32929 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); in sqlite3TreeViewSelect()
32931 if( p->pLimit ){ in sqlite3TreeViewSelect()
32932 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); in sqlite3TreeViewSelect()
32933 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0); in sqlite3TreeViewSelect()
32934 if( p->pLimit->pRight ){ in sqlite3TreeViewSelect()
32936 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0); in sqlite3TreeViewSelect()
32941 if( p->pPrior ){ in sqlite3TreeViewSelect()
32943 switch( p->op ){ in sqlite3TreeViewSelect()
32950 p = p->pPrior; in sqlite3TreeViewSelect()
32994 ** Generate a human-readable explanation for a Window object
32999 if( pWin->pFilter ){ in sqlite3TreeViewWindow()
33001 sqlite3TreeViewExpr(pView, pWin->pFilter, 0); in sqlite3TreeViewWindow()
33003 if( pWin->eFrmType==TK_FILTER ) return; in sqlite3TreeViewWindow()
33006 if( pWin->zName ){ in sqlite3TreeViewWindow()
33007 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin); in sqlite3TreeViewWindow()
33011 if( pWin->zBase ) nElement++; in sqlite3TreeViewWindow()
33012 if( pWin->pOrderBy ) nElement++; in sqlite3TreeViewWindow()
33013 if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER ) nElement++; in sqlite3TreeViewWindow()
33014 if( pWin->eExclude ) nElement++; in sqlite3TreeViewWindow()
33015 if( pWin->zBase ){ in sqlite3TreeViewWindow()
33016 sqlite3TreeViewPush(&pView, (--nElement)>0); in sqlite3TreeViewWindow()
33017 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase); in sqlite3TreeViewWindow()
33020 if( pWin->pPartition ){ in sqlite3TreeViewWindow()
33021 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY"); in sqlite3TreeViewWindow()
33023 if( pWin->pOrderBy ){ in sqlite3TreeViewWindow()
33024 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY"); in sqlite3TreeViewWindow()
33026 if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER ){ in sqlite3TreeViewWindow()
33029 if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE"; in sqlite3TreeViewWindow()
33030 if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS"; in sqlite3TreeViewWindow()
33032 pWin->bImplicitFrame ? " (implied)" : ""); in sqlite3TreeViewWindow()
33033 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0); in sqlite3TreeViewWindow()
33034 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1); in sqlite3TreeViewWindow()
33035 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0); in sqlite3TreeViewWindow()
33038 if( pWin->eExclude ){ in sqlite3TreeViewWindow()
33041 switch( pWin->eExclude ){ in sqlite3TreeViewWindow()
33047 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude); in sqlite3TreeViewWindow()
33061 ** Generate a human-readable explanation for a Window Function object
33067 pWin->pWFunc->zName, pWin->pWFunc->nArg); in sqlite3TreeViewWinFunc()
33074 ** Generate a human-readable explanation of an expression tree.
33086 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags || pExpr->pAggInfo ){ in sqlite3TreeViewExpr()
33090 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n'); in sqlite3TreeViewExpr()
33092 sqlite3_str_appendf(&x, " outer.iJoin=%d", pExpr->w.iJoin); in sqlite3TreeViewExpr()
33095 sqlite3_str_appendf(&x, " inner.iJoin=%d", pExpr->w.iJoin); in sqlite3TreeViewExpr()
33103 if( pExpr->pAggInfo!=0 ){ in sqlite3TreeViewExpr()
33104 sqlite3_str_appendf(&x, " agg-column[%d]", pExpr->iAgg); in sqlite3TreeViewExpr()
33110 switch( pExpr->op ){ in sqlite3TreeViewExpr()
33113 pExpr->iTable, pExpr->iColumn, zFlgs); in sqlite3TreeViewExpr()
33117 if( pExpr->iTable<0 ){ in sqlite3TreeViewExpr()
33120 if( pExpr->op2 ){ in sqlite3TreeViewExpr()
33121 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2); in sqlite3TreeViewExpr()
33126 pExpr->iColumn, zFlgs, zOp2); in sqlite3TreeViewExpr()
33130 pExpr->iTable, pExpr->iColumn, in sqlite3TreeViewExpr()
33131 pExpr->y.pTab, zFlgs); in sqlite3TreeViewExpr()
33134 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33139 if( pExpr->flags & EP_IntValue ){ in sqlite3TreeViewExpr()
33140 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); in sqlite3TreeViewExpr()
33142 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); in sqlite3TreeViewExpr()
33149 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); in sqlite3TreeViewExpr()
33155 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); in sqlite3TreeViewExpr()
33170 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); in sqlite3TreeViewExpr()
33177 pExpr->u.zToken, pExpr->iColumn); in sqlite3TreeViewExpr()
33181 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); in sqlite3TreeViewExpr()
33186 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken); in sqlite3TreeViewExpr()
33193 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); in sqlite3TreeViewExpr()
33194 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33231 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE" in sqlite3TreeViewExpr()
33233 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT ); in sqlite3TreeViewExpr()
33234 assert( pExpr->pRight ); in sqlite3TreeViewExpr()
33235 assert( sqlite3ExprSkipCollateAndLikely(pExpr->pRight)->op in sqlite3TreeViewExpr()
33237 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight); in sqlite3TreeViewExpr()
33244 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken); in sqlite3TreeViewExpr()
33245 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33252 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE in sqlite3TreeViewExpr()
33257 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "", in sqlite3TreeViewExpr()
33258 pExpr->u.zToken, zFlgs); in sqlite3TreeViewExpr()
33259 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33272 pFarg = pExpr->x.pList; in sqlite3TreeViewExpr()
33274 pWin = IsWindowFunc(pExpr) ? pExpr->y.pWin : 0; in sqlite3TreeViewExpr()
33280 if( pExpr->op==TK_AGG_FUNCTION ){ in sqlite3TreeViewExpr()
33282 pExpr->op2, pExpr->u.zToken, zFlgs, in sqlite3TreeViewExpr()
33283 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0, in sqlite3TreeViewExpr()
33284 pExpr->iAgg, pExpr->pAggInfo); in sqlite3TreeViewExpr()
33285 }else if( pExpr->op2!=0 ){ in sqlite3TreeViewExpr()
33288 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2); in sqlite3TreeViewExpr()
33290 if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck"; in sqlite3TreeViewExpr()
33291 if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr"; in sqlite3TreeViewExpr()
33292 if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx"; in sqlite3TreeViewExpr()
33293 if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol"; in sqlite3TreeViewExpr()
33295 pExpr->u.zToken, zFlgs, zOp2); in sqlite3TreeViewExpr()
33297 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs); in sqlite3TreeViewExpr()
33300 sqlite3TreeViewExprList(pView, pFarg, pWin!=0 || pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33301 if( pExpr->pLeft ){ in sqlite3TreeViewExpr()
33302 Expr *pOB = pExpr->pLeft; in sqlite3TreeViewExpr()
33303 assert( pOB->op==TK_ORDER ); in sqlite3TreeViewExpr()
33305 sqlite3TreeViewExprList(pView, pOB->x.pList, pWin!=0, "ORDERBY"); in sqlite3TreeViewExpr()
33316 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, "ORDERBY"); in sqlite3TreeViewExpr()
33322 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags); in sqlite3TreeViewExpr()
33323 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); in sqlite3TreeViewExpr()
33328 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags); in sqlite3TreeViewExpr()
33329 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); in sqlite3TreeViewExpr()
33335 sqlite3_str_appendf(pStr, "IN flags=0x%x", pExpr->flags); in sqlite3TreeViewExpr()
33336 if( pExpr->iTable ) sqlite3_str_appendf(pStr, " iTable=%d",pExpr->iTable); in sqlite3TreeViewExpr()
33339 pExpr->y.sub.regReturn, pExpr->y.sub.iAddr); in sqlite3TreeViewExpr()
33344 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); in sqlite3TreeViewExpr()
33346 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); in sqlite3TreeViewExpr()
33348 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); in sqlite3TreeViewExpr()
33361 ** X is stored in pExpr->pLeft. in sqlite3TreeViewExpr()
33362 ** Y is stored in pExpr->pList->a[0].pExpr. in sqlite3TreeViewExpr()
33363 ** Z is stored in pExpr->pList->a[1].pExpr. in sqlite3TreeViewExpr()
33367 pX = pExpr->pLeft; in sqlite3TreeViewExpr()
33369 assert( pExpr->x.pList->nExpr==2 ); in sqlite3TreeViewExpr()
33370 pY = pExpr->x.pList->a[0].pExpr; in sqlite3TreeViewExpr()
33371 pZ = pExpr->x.pList->a[1].pExpr; in sqlite3TreeViewExpr()
33380 ** to a column in the new.* or old.* pseudo-tables available to in sqlite3TreeViewExpr()
33382 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn in sqlite3TreeViewExpr()
33383 ** is set to the column of the pseudo-table to read, or to -1 to in sqlite3TreeViewExpr()
33387 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); in sqlite3TreeViewExpr()
33392 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); in sqlite3TreeViewExpr()
33394 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); in sqlite3TreeViewExpr()
33400 switch( pExpr->affExpr ){ in sqlite3TreeViewExpr()
33407 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); in sqlite3TreeViewExpr()
33413 pExpr->iTable, pExpr->iColumn, zFlgs); in sqlite3TreeViewExpr()
33414 sqlite3TreeViewExpr(pView, pExpr->pRight, 0); in sqlite3TreeViewExpr()
33420 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z); in sqlite3TreeViewExpr()
33425 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s", in sqlite3TreeViewExpr()
33426 pExpr->iColumn, pExpr->iTable-1, in sqlite3TreeViewExpr()
33427 pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : ""); in sqlite3TreeViewExpr()
33428 assert( ExprUseXSelect(pExpr->pLeft) ); in sqlite3TreeViewExpr()
33429 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0); in sqlite3TreeViewExpr()
33433 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable); in sqlite3TreeViewExpr()
33434 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33441 tmp.op = pExpr->op2; in sqlite3TreeViewExpr()
33446 if( pExpr->iColumn<=0 ){ in sqlite3TreeViewExpr()
33450 pExpr->iColumn-1); in sqlite3TreeViewExpr()
33455 sqlite3TreeViewLine(pView, "op=%d", pExpr->op); in sqlite3TreeViewExpr()
33461 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); in sqlite3TreeViewExpr()
33462 sqlite3TreeViewExpr(pView, pExpr->pRight, 0); in sqlite3TreeViewExpr()
33465 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33472 ** Generate a human-readable explanation of an expression list.
33485 for(i=0; i<pList->nExpr; i++){ in sqlite3TreeViewBareExprList()
33486 int j = pList->a[i].u.x.iOrderByCol; in sqlite3TreeViewBareExprList()
33487 char *zName = pList->a[i].zEName; in sqlite3TreeViewBareExprList()
33488 int moreToFollow = i<pList->nExpr - 1; in sqlite3TreeViewBareExprList()
33494 switch( pList->a[i].fg.eEName ){ in sqlite3TreeViewBareExprList()
33499 fprintf(stdout, "TABLE-ALIAS-NAME(\"%s\") ", zName); in sqlite3TreeViewBareExprList()
33500 if( pList->a[i].fg.bUsed ) fprintf(stdout, "(used) "); in sqlite3TreeViewBareExprList()
33501 if( pList->a[i].fg.bUsingTerm ) fprintf(stdout, "(USING-term) "); in sqlite3TreeViewBareExprList()
33502 if( pList->a[i].fg.bNoExpand ) fprintf(stdout, "(NoExpand) "); in sqlite3TreeViewBareExprList()
33515 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow); in sqlite3TreeViewBareExprList()
33534 ** Generate a human-readable explanation of an id-list.
33547 for(i=0; i<pList->nId; i++){ in sqlite3TreeViewBareIdList()
33548 char *zName = pList->a[i].zName; in sqlite3TreeViewBareIdList()
33549 int moreToFollow = i<pList->nId - 1; in sqlite3TreeViewBareIdList()
33553 if( pList->eU4==EU4_NONE ){ in sqlite3TreeViewBareIdList()
33555 }else if( pList->eU4==EU4_IDX ){ in sqlite3TreeViewBareIdList()
33556 fprintf(stdout, "%s (%d)\n", zName, pList->a[i].u4.idx); in sqlite3TreeViewBareIdList()
33558 assert( pList->eU4==EU4_EXPR ); in sqlite3TreeViewBareIdList()
33559 if( pList->a[i].u4.pExpr==0 ){ in sqlite3TreeViewBareIdList()
33563 sqlite3TreeViewPush(&pView, i<pList->nId-1); in sqlite3TreeViewBareIdList()
33564 sqlite3TreeViewExpr(pView, pList->a[i].u4.pExpr, 0); in sqlite3TreeViewBareIdList()
33584 ** Generate a human-readable explanation of a list of Upsert objects
33595 sqlite3TreeViewPush(&pView, pUpsert->pNextUpsert!=0 || moreToFollow); in sqlite3TreeViewUpsert()
33597 pUpsert->isDoUpdate ? "UPDATE" : "NOTHING"); in sqlite3TreeViewUpsert()
33598 n = (pUpsert->pUpsertSet!=0) + (pUpsert->pUpsertWhere!=0); in sqlite3TreeViewUpsert()
33599 sqlite3TreeViewExprList(pView, pUpsert->pUpsertTarget, (n--)>0, "TARGET"); in sqlite3TreeViewUpsert()
33600 sqlite3TreeViewExprList(pView, pUpsert->pUpsertSet, (n--)>0, "SET"); in sqlite3TreeViewUpsert()
33601 if( pUpsert->pUpsertWhere ){ in sqlite3TreeViewUpsert()
33602 sqlite3TreeViewItem(pView, "WHERE", (n--)>0); in sqlite3TreeViewUpsert()
33603 sqlite3TreeViewExpr(pView, pUpsert->pUpsertWhere, 0); in sqlite3TreeViewUpsert()
33607 pUpsert = pUpsert->pNextUpsert; in sqlite3TreeViewUpsert()
33614 ** Generate a human-readable diagram of the data structure that go
33636 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
33641 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
33647 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
33653 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY"); in sqlite3TreeViewDelete()
33656 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
33662 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); in sqlite3TreeViewDelete()
33670 ** Generate a human-readable diagram of the data structure that go
33703 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
33708 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
33714 sqlite3TreeViewIdList(pView, pColumnList, (--n)>0, "COLUMNS"); in sqlite3TreeViewInsert()
33717 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
33718 sqlite3TreeViewLine(pView, "DATA-SOURCE"); in sqlite3TreeViewInsert()
33723 sqlite3TreeViewExprList(pView, pExprList, (--n)>0, "VALUES"); in sqlite3TreeViewInsert()
33726 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
33732 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); in sqlite3TreeViewInsert()
33740 ** Generate a human-readable diagram of the data structure that go
33775 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
33780 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
33786 sqlite3TreeViewExprList(pView, pChanges, (--n)>0, "SET"); in sqlite3TreeViewUpdate()
33789 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
33795 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY"); in sqlite3TreeViewUpdate()
33798 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
33804 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
33810 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); in sqlite3TreeViewUpdate()
33818 ** Show a human-readable graph of a TriggerStep
33829 moreToFollow || (showFullList && pStep->pNext!=0)); in sqlite3TreeViewTriggerStep()
33831 if( cnt++ && pStep->pNext==0 ){ in sqlite3TreeViewTriggerStep()
33835 sqlite3TreeViewLine(pView, "%s", pStep->zSpan ? pStep->zSpan : "RETURNING"); in sqlite3TreeViewTriggerStep()
33836 }while( showFullList && (pStep = pStep->pNext)!=0 ); in sqlite3TreeViewTriggerStep()
33841 ** Show a human-readable graph of a Trigger
33852 moreToFollow || (showFullList && pTrigger->pNext!=0)); in sqlite3TreeViewTrigger()
33854 if( cnt++ && pTrigger->pNext==0 ){ in sqlite3TreeViewTrigger()
33858 sqlite3TreeViewLine(pView, "TRIGGER %s", pTrigger->zName); in sqlite3TreeViewTrigger()
33860 sqlite3TreeViewTriggerStep(pView, pTrigger->step_list, 0, 1); in sqlite3TreeViewTrigger()
33862 }while( showFullList && (pTrigger = pTrigger->pNext)!=0 ); in sqlite3TreeViewTrigger()
33869 ** These simplified versions of the tree-view routines omit unnecessary
33915 ** This file contains code to implement a pseudo-random number
33934 /* The RFC-7539 ChaCha20 block function
33936 #define ROTL(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
33965 /* The "wsdPrng" macro will resolve to the pseudo-random number generator in sqlite3_randomness()
33967 ** we have to locate the state vector at run-time. In the more common in sqlite3_randomness()
34019 memcpy(zBuf, &wsdPrng.out[wsdPrng.n-N], N); in sqlite3_randomness()
34020 wsdPrng.n -= N; in sqlite3_randomness()
34025 N -= wsdPrng.n; in sqlite3_randomness()
34076 ** This file presents a simple cross-platform threading interface for
34089 ** single-threaded if desired.
34101 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
34124 /* This routine is never used in single-threaded mode */ in sqlite3ThreadCreate()
34131 p->xTask = xTask; in sqlite3ThreadCreate()
34132 p->pIn = pIn; in sqlite3ThreadCreate()
34140 rc = pthread_create(&p->tid, 0, xTask, pIn); in sqlite3ThreadCreate()
34143 p->done = 1; in sqlite3ThreadCreate()
34144 p->pOut = xTask(pIn); in sqlite3ThreadCreate()
34156 if( p->done ){ in sqlite3ThreadJoin()
34157 *ppOut = p->pOut; in sqlite3ThreadJoin()
34160 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK; in sqlite3ThreadJoin()
34173 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
34199 assert( p->id==GetCurrentThreadId() ); in sqlite3ThreadProc()
34201 assert( p->xTask!=0 ); in sqlite3ThreadProc()
34202 p->pResult = p->xTask(p->pIn); in sqlite3ThreadProc()
34229 p->xTask = xTask; in sqlite3ThreadCreate()
34230 p->pIn = pIn; in sqlite3ThreadCreate()
34231 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id); in sqlite3ThreadCreate()
34232 if( p->tid==0 ){ in sqlite3ThreadCreate()
34236 if( p->xTask==0 ){ in sqlite3ThreadCreate()
34237 p->id = GetCurrentThreadId(); in sqlite3ThreadCreate()
34238 p->pResult = xTask(pIn); in sqlite3ThreadCreate()
34253 if( p->xTask==0 ){ in sqlite3ThreadJoin()
34254 /* assert( p->id==GetCurrentThreadId() ); */ in sqlite3ThreadJoin()
34256 assert( p->tid==0 ); in sqlite3ThreadJoin()
34258 assert( p->id!=0 && p->id!=GetCurrentThreadId() ); in sqlite3ThreadJoin()
34259 rc = sqlite3Win32Wait((HANDLE)p->tid); in sqlite3ThreadJoin()
34261 bRc = CloseHandle((HANDLE)p->tid); in sqlite3ThreadJoin()
34264 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult; in sqlite3ThreadJoin()
34273 /********************************* Single-Threaded **************************/
34302 p->xTask = xTask; in sqlite3ThreadCreate()
34303 p->pIn = pIn; in sqlite3ThreadCreate()
34305 p->xTask = 0; in sqlite3ThreadCreate()
34306 p->pResult = xTask(pIn); in sqlite3ThreadCreate()
34317 if( p->xTask ){ in sqlite3ThreadJoin()
34318 *ppOut = p->xTask(p->pIn); in sqlite3ThreadJoin()
34320 *ppOut = p->pResult; in sqlite3ThreadJoin()
34336 /****************************** End Single-Threaded *************************/
34352 ** This file contains routines used to translate between UTF-8,
34353 ** UTF-16, UTF-16BE, and UTF-16LE.
34355 ** Notes on UTF-8:
34357 ** Byte-0 Byte-1 Byte-2 Byte-3 Value
34364 ** Notes on UTF-16: (with wwww+1==uuuuu)
34366 ** Word-0 Word-1 Value
34372 ** 0xff 0xfe little-endian utf-16 follows
34373 ** 0xfe 0xff big-endian utf-16 follows
34390 ** a multi-byte UTF8 character.
34429 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
34430 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
34441 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
34442 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
34449 ** Translate a single UTF-8 character. Return the unicode value.
34456 ** Notes On Invalid UTF-8:
34458 ** * This routine never allows a 7-bit character (0x00 through 0x7f) to
34459 ** be encoded as a multi-byte character. Any multi-byte character that
34463 ** If a multi-byte character attempts to encode a value between
34467 ** byte of a character are interpreted as single-byte characters
34471 ** * This routine accepts over-length UTF8 encodings
34472 ** for unicode values 0x80 and greater. It does not change over-length
34478 c = sqlite3Utf8Trans1[c-0xc0]; \
34492 ** For this routine, we assume the UTF8 string is always zero-terminated. in sqlite3Utf8Read()
34496 c = sqlite3Utf8Trans1[c-0xc0]; in sqlite3Utf8Read()
34509 ** more than n characters from the buffer. z[] is not zero-terminated.
34529 c = sqlite3Utf8Trans1[c-0xc0]; in sqlite3Utf8ReadLimited()
34561 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); in sqlite3VdbeMemTranslate()
34562 assert( pMem->flags&MEM_Str ); in sqlite3VdbeMemTranslate()
34563 assert( pMem->enc!=desiredEnc ); in sqlite3VdbeMemTranslate()
34564 assert( pMem->enc!=0 ); in sqlite3VdbeMemTranslate()
34565 assert( pMem->n>=0 ); in sqlite3VdbeMemTranslate()
34577 /* If the translation is between UTF-16 little and big endian, then in sqlite3VdbeMemTranslate()
34581 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){ in sqlite3VdbeMemTranslate()
34589 zIn = (u8*)pMem->z; in sqlite3VdbeMemTranslate()
34590 zTerm = &zIn[pMem->n&~1]; in sqlite3VdbeMemTranslate()
34597 pMem->enc = desiredEnc; in sqlite3VdbeMemTranslate()
34603 /* When converting from UTF-16, the maximum growth results from in sqlite3VdbeMemTranslate()
34604 ** translating a 2-byte character to a 4-byte UTF-8 character. in sqlite3VdbeMemTranslate()
34606 ** nul-terminator. in sqlite3VdbeMemTranslate()
34608 pMem->n &= ~1; in sqlite3VdbeMemTranslate()
34609 len = 2 * (sqlite3_int64)pMem->n + 1; in sqlite3VdbeMemTranslate()
34611 /* When converting from UTF-8 to UTF-16 the maximum growth is caused in sqlite3VdbeMemTranslate()
34612 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16 in sqlite3VdbeMemTranslate()
34614 ** nul-terminator. in sqlite3VdbeMemTranslate()
34616 len = 2 * (sqlite3_int64)pMem->n + 2; in sqlite3VdbeMemTranslate()
34625 zIn = (u8*)pMem->z; in sqlite3VdbeMemTranslate()
34626 zTerm = &zIn[pMem->n]; in sqlite3VdbeMemTranslate()
34627 zOut = sqlite3DbMallocRaw(pMem->db, len); in sqlite3VdbeMemTranslate()
34633 if( pMem->enc==SQLITE_UTF8 ){ in sqlite3VdbeMemTranslate()
34635 /* UTF-8 -> UTF-16 Little-endian */ in sqlite3VdbeMemTranslate()
34642 /* UTF-8 -> UTF-16 Big-endian */ in sqlite3VdbeMemTranslate()
34648 pMem->n = (int)(z - zOut); in sqlite3VdbeMemTranslate()
34652 if( pMem->enc==SQLITE_UTF16LE ){ in sqlite3VdbeMemTranslate()
34653 /* UTF-16 Little-endian -> UTF-8 */ in sqlite3VdbeMemTranslate()
34665 zIn -= 2; in sqlite3VdbeMemTranslate()
34682 /* UTF-16 Big-endian -> UTF-8 */ in sqlite3VdbeMemTranslate()
34694 zIn -= 2; in sqlite3VdbeMemTranslate()
34711 pMem->n = (int)(z - zOut); in sqlite3VdbeMemTranslate()
34714 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len ); in sqlite3VdbeMemTranslate()
34716 c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype)); in sqlite3VdbeMemTranslate()
34718 pMem->flags = c; in sqlite3VdbeMemTranslate()
34719 pMem->enc = desiredEnc; in sqlite3VdbeMemTranslate()
34720 pMem->z = (char*)zOut; in sqlite3VdbeMemTranslate()
34721 pMem->zMalloc = pMem->z; in sqlite3VdbeMemTranslate()
34722 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z); in sqlite3VdbeMemTranslate()
34740 ** This routine checks for a byte-order mark at the beginning of the
34741 ** UTF-16 string stored in *pMem. If one is present, it is removed and
34743 ** byte-swapping, it just sets Mem.enc appropriately.
34752 assert( pMem->n>=0 ); in sqlite3VdbeMemHandleBom()
34753 if( pMem->n>1 ){ in sqlite3VdbeMemHandleBom()
34754 u8 b1 = *(u8 *)pMem->z; in sqlite3VdbeMemHandleBom()
34755 u8 b2 = *(((u8 *)pMem->z) + 1); in sqlite3VdbeMemHandleBom()
34767 pMem->n -= 2; in sqlite3VdbeMemHandleBom()
34768 memmove(pMem->z, &pMem->z[2], pMem->n); in sqlite3VdbeMemHandleBom()
34769 pMem->z[pMem->n] = '\0'; in sqlite3VdbeMemHandleBom()
34770 pMem->z[pMem->n+1] = '\0'; in sqlite3VdbeMemHandleBom()
34771 pMem->flags |= MEM_Term; in sqlite3VdbeMemHandleBom()
34772 pMem->enc = bom; in sqlite3VdbeMemHandleBom()
34780 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
34793 zTerm = (const u8*)(-1); in sqlite3Utf8CharLen()
34803 /* This test function is not currently used by the automated test-suite.
34808 ** Translate UTF-8 to UTF-8.
34810 ** This has the effect of making sure that the string is well-formed
34811 ** UTF-8. Miscoded characters are removed.
34813 ** The translation is done in-place and aborted if the output
34828 return (int)(zOut - zStart); in sqlite3Utf8To8()
34834 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
34835 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
34846 if( db->mallocFailed ){ in sqlite3Utf16to8()
34850 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed ); in sqlite3Utf16to8()
34851 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed ); in sqlite3Utf16to8()
34852 assert( m.z || db->mallocFailed ); in sqlite3Utf16to8()
34857 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
34859 ** in pZ. nChar must be non-negative.
34873 return (int)(z-(unsigned char const *)zIn) in sqlite3Utf16ByteLen()
34874 - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE); in sqlite3Utf16ByteLen()
34893 n = (int)(z-zBuf); in sqlite3UtfSelfTest()
34902 assert( (z-zBuf)==n ); in sqlite3UtfSelfTest()
34939 ** sqlite3FaultSim() function only returns non-zero during testing.
34941 ** During testing, if the test harness has set a fault-sim callback using
34943 ** each call to sqlite3FaultSim() is relayed to that application-supplied
34944 ** callback and the integer return value form the application-supplied
34982 ** Return true if the floating point value is NaN or +Inf or -Inf.
34995 ** lower 30 bits of a 32-bit signed integer.
35010 ** The column type is an extra string stored after the zero-terminator on
35014 if( pCol->colFlags & COLFLAG_HASTYPE ){ in sqlite3ColumnType()
35015 return pCol->zCnName + strlen(pCol->zCnName) + 1; in sqlite3ColumnType()
35016 }else if( pCol->eCType ){ in sqlite3ColumnType()
35017 assert( pCol->eCType<=SQLITE_N_STDTYPE ); in sqlite3ColumnType()
35018 return (char*)sqlite3StdType[pCol->eCType-1]; in sqlite3ColumnType()
35025 ** Helper function for sqlite3Error() - called rarely. Broken out into
35030 if( db->pErr ) sqlite3ValueSetNull(db->pErr); in sqlite3ErrorFinish()
35041 db->errCode = err_code; in sqlite3Error()
35042 if( err_code || db->pErr ){ in sqlite3Error()
35045 db->errByteOffset = -1; in sqlite3Error()
35055 db->errCode = SQLITE_OK; in sqlite3ErrorClear()
35056 db->errByteOffset = -1; in sqlite3ErrorClear()
35057 if( db->pErr ) sqlite3ValueSetNull(db->pErr); in sqlite3ErrorClear()
35071 for(ii=0; ii<db->nDb; ii++){ in sqlite3SystemError()
35072 if( db->aDb[ii].pBt ){ in sqlite3SystemError()
35073 iErr = sqlite3PagerWalSystemErrno(sqlite3BtreePager(db->aDb[ii].pBt)); in sqlite3SystemError()
35075 db->iSysErrno = iErr; in sqlite3SystemError()
35085 db->iSysErrno = sqlite3OsGetLastError(db->pVfs); in sqlite3SystemError()
35095 ** assumed to be encoded in UTF-8.
35103 db->errCode = err_code; in sqlite3ErrorWithMsg()
35107 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ in sqlite3ErrorWithMsg()
35113 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); in sqlite3ErrorWithMsg()
35121 sqlite3 *db = p->db; in sqlite3ProgressCheck()
35122 if( AtomicLoad(&db->u1.isInterrupted) ){ in sqlite3ProgressCheck()
35123 p->nErr++; in sqlite3ProgressCheck()
35124 p->rc = SQLITE_INTERRUPT; in sqlite3ProgressCheck()
35127 if( db->xProgress ){ in sqlite3ProgressCheck()
35128 if( p->rc==SQLITE_INTERRUPT ){ in sqlite3ProgressCheck()
35129 p->nProgressSteps = 0; in sqlite3ProgressCheck()
35130 }else if( (++p->nProgressSteps)>=db->nProgressOps ){ in sqlite3ProgressCheck()
35131 if( db->xProgress(db->pProgressArg) ){ in sqlite3ProgressCheck()
35132 p->nErr++; in sqlite3ProgressCheck()
35133 p->rc = SQLITE_INTERRUPT; in sqlite3ProgressCheck()
35135 p->nProgressSteps = 0; in sqlite3ProgressCheck()
35142 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
35154 sqlite3 *db = pParse->db; in sqlite3ErrorMsg()
35156 assert( db->pParse==pParse || db->pParse->pToplevel==pParse ); in sqlite3ErrorMsg()
35157 db->errByteOffset = -2; in sqlite3ErrorMsg()
35161 if( db->errByteOffset<-1 ) db->errByteOffset = -1; in sqlite3ErrorMsg()
35162 if( db->suppressErr ){ in sqlite3ErrorMsg()
35164 if( db->mallocFailed ){ in sqlite3ErrorMsg()
35165 pParse->nErr++; in sqlite3ErrorMsg()
35166 pParse->rc = SQLITE_NOMEM; in sqlite3ErrorMsg()
35169 pParse->nErr++; in sqlite3ErrorMsg()
35170 sqlite3DbFree(db, pParse->zErrMsg); in sqlite3ErrorMsg()
35171 pParse->zErrMsg = zMsg; in sqlite3ErrorMsg()
35172 pParse->rc = SQLITE_ERROR; in sqlite3ErrorMsg()
35173 pParse->pWith = 0; in sqlite3ErrorMsg()
35184 if( db==0 || (pParse = db->pParse)==0 ) return errCode; in sqlite3ErrorToParser()
35185 pParse->rc = errCode; in sqlite3ErrorToParser()
35186 pParse->nErr++; in sqlite3ErrorToParser()
35191 ** Convert an SQL-style quoted string into a normal string by removing
35192 ** the quote characters. The conversion is done in-place. If the
35194 ** is a no-op.
35196 ** The input string must be zero-terminated. A new zero-terminator
35199 ** The return value is -1 if no dequoting occurs or the length of the
35203 ** 2002-02-14: This routine is extended to remove MS-Access style
35204 ** brackets from around identifiers. For example: "[a-b-c]" becomes
35205 ** "a-b-c".
35231 assert( sqlite3Isquote(p->u.zToken[0]) ); in sqlite3DequoteExpr()
35232 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted; in sqlite3DequoteExpr()
35233 sqlite3Dequote(p->u.zToken); in sqlite3DequoteExpr()
35237 ** Expression p is a QNUMBER (quoted number). Dequote the value in p->u.zToken
35242 assert( p!=0 || pParse->db->mallocFailed ); in sqlite3DequoteNumber()
35244 const char *pIn = p->u.zToken; in sqlite3DequoteNumber()
35245 char *pOut = p->u.zToken; in sqlite3DequoteNumber()
35248 assert( p->op==TK_QNUMBER ); in sqlite3DequoteNumber()
35249 p->op = TK_INTEGER; in sqlite3DequoteNumber()
35253 if( *pIn=='e' || *pIn=='E' || *pIn=='.' ) p->op = TK_FLOAT; in sqlite3DequoteNumber()
35255 if( (bHex==0 && (!sqlite3Isdigit(pIn[-1]) || !sqlite3Isdigit(pIn[1]))) in sqlite3DequoteNumber()
35256 || (bHex==1 && (!sqlite3Isxdigit(pIn[-1]) || !sqlite3Isxdigit(pIn[1]))) in sqlite3DequoteNumber()
35258 sqlite3ErrorMsg(pParse, "unrecognized token: \"%s\"", p->u.zToken); in sqlite3DequoteNumber()
35262 if( bHex ) p->op = TK_INTEGER; in sqlite3DequoteNumber()
35264 /* tag-20240227-a: If after dequoting, the number is an integer that in sqlite3DequoteNumber()
35266 ** parts of the code expect this. See also tag-20240227-b. */ in sqlite3DequoteNumber()
35267 if( p->op==TK_INTEGER && sqlite3GetInt32(p->u.zToken, &iValue) ){ in sqlite3DequoteNumber()
35268 p->u.iValue = iValue; in sqlite3DequoteNumber()
35269 p->flags |= EP_IntValue; in sqlite3DequoteNumber()
35278 ** "abc" -> abc
35279 ** "ab""cd" -> (not possible because of the interior "")
35283 ** is always a no-op.
35287 if( p->n<2 ) return; in sqlite3DequoteToken()
35288 if( !sqlite3Isquote(p->z[0]) ) return; in sqlite3DequoteToken()
35289 for(i=1; i<p->n-1; i++){ in sqlite3DequoteToken()
35290 if( sqlite3Isquote(p->z[i]) ) return; in sqlite3DequoteToken()
35292 p->n -= 2; in sqlite3DequoteToken()
35293 p->z++; in sqlite3DequoteToken()
35300 p->z = z; in sqlite3TokenInit()
35301 p->n = sqlite3Strlen30(z); in sqlite3TokenInit()
35304 /* Convenient short-hand */
35311 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
35313 ** the contents of two buffers containing UTF-8 strings in a
35314 ** case-independent fashion, using the same definition of "case
35319 return zRight ? -1 : 0; in sqlite3_stricmp()
35336 c = (int)UpperToLower[c] - (int)UpperToLower[x]; in sqlite3StrICmp()
35347 return zRight ? -1 : 0; in sqlite3_strnicmp()
35353 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } in sqlite3_strnicmp()
35354 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; in sqlite3_strnicmp()
35358 ** Compute an 8-bit hash on a string that is insensitive to case differences
35370 /* Double-Double multiplication. (x[0],x[1]) *= (y,yy)
35373 ** T. J. Dekker, "A Floating-Point Technique for Extending the
35374 ** Available Precision". 1971-07-26.
35380 ** binary64 rather than be carried around in an extended-precision in dekkerMul2()
35391 tx = x[0] - hx; in dekkerMul2()
35395 ty = y - hy; in dekkerMul2()
35399 cc = p - c + q + tx*ty; in dekkerMul2()
35402 x[1] = c - x[0]; in dekkerMul2()
35411 ** uses the encoding enc. The string is not necessarily zero-terminated.
35419 ** -1 => Not a valid number, but has a valid prefix which
35424 ** [+-]digits[E[+-]digits]
35425 ** [+-]digits.[digits][E[+-]digits]
35426 ** [+-].digits[E[+-]digits]
35448 int eValid = 1; /* True exponent is either not used or is well-formed */ in sqlite3AtoF()
35450 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */ in sqlite3AtoF()
35466 for(i=3-enc; i<length && z[i]==0; i+=2){} in sqlite3AtoF()
35467 if( i<length ) eType = -100; in sqlite3AtoF()
35477 if( *z=='-' ){ in sqlite3AtoF()
35478 sign = -1; in sqlite3AtoF()
35486 s = s*10 + (*z - '0'); in sqlite3AtoF()
35488 if( s>=((LARGEST_UINT64-9)/10) ){ in sqlite3AtoF()
35489 /* skip non-significant significand digits in sqlite3AtoF()
35503 if( s<((LARGEST_UINT64-9)/10) ){ in sqlite3AtoF()
35504 s = s*10 + (*z - '0'); in sqlite3AtoF()
35505 d--; in sqlite3AtoF()
35522 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/ in sqlite3AtoF()
35525 if( *z=='-' ){ in sqlite3AtoF()
35526 esign = -1; in sqlite3AtoF()
35533 e = e<10000 ? (e*10 + (*z - '0')) : 10000; in sqlite3AtoF()
35545 *pResult = sign<0 ? -0.0 : +0.0; in sqlite3AtoF()
35555 e--; in sqlite3AtoF()
35567 while( e>=100 ){ e-=100; r *= 1.0e+100L; } in sqlite3AtoF()
35568 while( e>=10 ){ e-=10; r *= 1.0e+10L; } in sqlite3AtoF()
35569 while( e>=1 ){ e-=1; r *= 1.0e+01L; } in sqlite3AtoF()
35571 while( e<=-100 ){ e+=100; r *= 1.0e-100L; } in sqlite3AtoF()
35572 while( e<=-10 ){ e+=10; r *= 1.0e-10L; } in sqlite3AtoF()
35573 while( e<=-1 ){ e+=1; r *= 1.0e-01L; } in sqlite3AtoF()
35593 rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s); in sqlite3AtoF()
35596 e -= 100; in sqlite3AtoF()
35597 dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); in sqlite3AtoF()
35600 e -= 10; in sqlite3AtoF()
35604 e -= 1; in sqlite3AtoF()
35608 while( e<=-100 ){ in sqlite3AtoF()
35610 dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); in sqlite3AtoF()
35612 while( e<=-10 ){ in sqlite3AtoF()
35614 dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); in sqlite3AtoF()
35616 while( e<=-1 ){ in sqlite3AtoF()
35618 dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); in sqlite3AtoF()
35624 if( sign<0 ) *pResult = -*pResult; in sqlite3AtoF()
35628 /* return true if number and no extra non-whitespace characters after */ in sqlite3AtoF()
35632 return -1; in sqlite3AtoF()
35645 ** Render an signed 64-bit integer as text. Store the result in zOut[] and
35657 x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v; in sqlite3Int64ToText()
35661 i = sizeof(zTemp)-2; in sqlite3Int64ToText()
35662 zTemp[sizeof(zTemp)-1] = 0; in sqlite3Int64ToText()
35663 while( 1 /*exit-by-break*/ ){ in sqlite3Int64ToText()
35667 i--; in sqlite3Int64ToText()
35669 if( v<0 ) zTemp[--i] = '-'; in sqlite3Int64ToText()
35670 memcpy(zOut, &zTemp[i], sizeof(zTemp)-i); in sqlite3Int64ToText()
35671 return sizeof(zTemp)-1-i; in sqlite3Int64ToText()
35675 ** Compare the 19-character string zNum against the text representation
35686 ** will return -8.
35694 c = (zNum[i*incr]-pow63[i])*10; in compare2pow63()
35697 c = zNum[18*incr] - '8'; in compare2pow63()
35698 testcase( c==(-1) ); in compare2pow63()
35706 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
35711 ** -1 Not even a prefix of the input text looks like an integer
35712 ** 0 Successful transformation. Fits in a 64-bit signed integer.
35713 ** 1 Excess non-space text after the integer value
35714 ** 2 Integer too large for a 64-bit signed integer or is malformed
35718 ** The string is not necessarily zero-terminated. The encoding is
35727 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ in sqlite3Atoi64()
35738 for(i=3-enc; i<length && zNum[i]==0; i+=2){} in sqlite3Atoi64()
35745 if( *zNum=='-' ){ in sqlite3Atoi64()
35755 u = u*10 + c - '0'; in sqlite3Atoi64()
35762 ** from clang and -fsanitize=undefined. This test and assignment make in sqlite3Atoi64()
35764 ** them, but we must appease the undefined-behavior pharisees. */ in sqlite3Atoi64()
35767 *pNum = -(i64)u; in sqlite3Atoi64()
35773 rc = -1; in sqlite3Atoi64()
35774 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */ in sqlite3Atoi64()
35780 rc = 1; /* Extra non-space text after the integer */ in sqlite3Atoi64()
35791 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ in sqlite3Atoi64()
35805 assert( u-1==LARGEST_INT64 ); in sqlite3Atoi64()
35813 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
35814 ** into a 64-bit signed integer. This routine accepts hexadecimal literals,
35819 ** 0 Successful transformation. Fits in a 64-bit signed integer.
35821 ** 2 Integer too large for a 64-bit signed integer or is malformed
35836 if( k-i>16 ) return 2; in sqlite3DecOrHexToI64()
35842 int n = (int)(0x3fffffff&strspn(z,"+- \n\t0123456789")); in sqlite3DecOrHexToI64()
35849 ** If zNum represents an integer that will fit in 32-bits, then set
35854 ** Any non-numeric characters that following zNum are ignored.
35856 ** input number to be zero-terminated.
35862 if( zNum[0]=='-' ){ in sqlite3GetInt32()
35889 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ in sqlite3GetInt32()
35896 ** 2^31 -> 2147483648 in sqlite3GetInt32()
35902 testcase( v-neg==2147483647 ); in sqlite3GetInt32()
35903 if( v-neg>2147483647 ){ in sqlite3GetInt32()
35907 v = -v; in sqlite3GetInt32()
35914 ** Return a 32-bit integer value extracted from a string. If the
35924 ** Decode a floating-point value into an approximate decimal
35928 ** n is positive. Or round to -n signficant digits after the
35933 ** stored in p->z[] which is a often (but not always) a pointer
35934 ** into the middle of p->zBuf[]. There are p->n significant digits.
35935 ** The p->z[] array is *not* zero-terminated.
35941 p->isSpecial = 0; in sqlite3FpDecode()
35942 p->z = p->zBuf; in sqlite3FpDecode()
35947 p->sign = '-'; in sqlite3FpDecode()
35948 r = -r; in sqlite3FpDecode()
35950 p->sign = '+'; in sqlite3FpDecode()
35951 p->n = 1; in sqlite3FpDecode()
35952 p->iDP = 1; in sqlite3FpDecode()
35953 p->z = "0"; in sqlite3FpDecode()
35956 p->sign = '+'; in sqlite3FpDecode()
35961 p->isSpecial = 1 + (v!=0x7ff0000000000000LL); in sqlite3FpDecode()
35962 p->n = 0; in sqlite3FpDecode()
35963 p->iDP = 0; in sqlite3FpDecode()
35973 while( rr>=1.0e+119L ){ exp+=100; rr *= 1.0e-100L; } in sqlite3FpDecode()
35974 while( rr>=1.0e+29L ){ exp+=10; rr *= 1.0e-10L; } in sqlite3FpDecode()
35975 while( rr>=1.0e+19L ){ exp++; rr *= 1.0e-1L; } in sqlite3FpDecode()
35977 while( rr<1.0e-97L ){ exp-=100; rr *= 1.0e+100L; } in sqlite3FpDecode()
35978 while( rr<1.0e+07L ){ exp-=10; rr *= 1.0e+10L; } in sqlite3FpDecode()
35979 while( rr<1.0e+17L ){ exp--; rr *= 1.0e+1L; } in sqlite3FpDecode()
35983 /* If high-precision floating point is not available using "long double", in sqlite3FpDecode()
35984 ** then use Dekker-style double-double computation to increase the in sqlite3FpDecode()
35998 dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); in sqlite3FpDecode()
36002 dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); in sqlite3FpDecode()
36006 dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); in sqlite3FpDecode()
36009 while( rr[0]<9.223372036854774784e-83 ){ in sqlite3FpDecode()
36010 exp -= 100; in sqlite3FpDecode()
36011 dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); in sqlite3FpDecode()
36014 exp -= 10; in sqlite3FpDecode()
36018 exp -= 1; in sqlite3FpDecode()
36022 v = rr[1]<0.0 ? (u64)rr[0]-(u64)(-rr[1]) : (u64)rr[0]+(u64)rr[1]; in sqlite3FpDecode()
36027 i = sizeof(p->zBuf)-1; in sqlite3FpDecode()
36029 while( v ){ p->zBuf[i--] = (v%10) + '0'; v /= 10; } in sqlite3FpDecode()
36030 assert( i>=0 && i<sizeof(p->zBuf)-1 ); in sqlite3FpDecode()
36031 p->n = sizeof(p->zBuf) - 1 - i; in sqlite3FpDecode()
36032 assert( p->n>0 ); in sqlite3FpDecode()
36033 assert( p->n<sizeof(p->zBuf) ); in sqlite3FpDecode()
36034 p->iDP = p->n + exp; in sqlite3FpDecode()
36036 iRound = p->iDP - iRound; in sqlite3FpDecode()
36037 if( iRound==0 && p->zBuf[i+1]>='5' ){ in sqlite3FpDecode()
36039 p->zBuf[i--] = '0'; in sqlite3FpDecode()
36040 p->n++; in sqlite3FpDecode()
36041 p->iDP++; in sqlite3FpDecode()
36044 if( iRound>0 && (iRound<p->n || p->n>mxRound) ){ in sqlite3FpDecode()
36045 char *z = &p->zBuf[i+1]; in sqlite3FpDecode()
36047 p->n = iRound; in sqlite3FpDecode()
36049 int j = iRound-1; in sqlite3FpDecode()
36050 while( 1 /*exit-by-break*/ ){ in sqlite3FpDecode()
36055 p->z[i--] = '1'; in sqlite3FpDecode()
36056 p->n++; in sqlite3FpDecode()
36057 p->iDP++; in sqlite3FpDecode()
36060 j--; in sqlite3FpDecode()
36065 p->z = &p->zBuf[i+1]; in sqlite3FpDecode()
36066 assert( i+p->n < sizeof(p->zBuf) ); in sqlite3FpDecode()
36067 while( ALWAYS(p->n>0) && p->z[p->n-1]=='0' ){ p->n--; } in sqlite3FpDecode()
36071 ** Try to convert z into an unsigned 32-bit integer. Return true on
36080 v = v*10 + z[i] - '0'; in sqlite3GetUInt32()
36089 ** The variable-length integer encoding is as follows:
36096 ** 7 bits - A
36097 ** 14 bits - BA
36098 ** 21 bits - BBA
36099 ** 28 bits - BBBA
36100 ** 35 bits - BBBBA
36101 ** 42 bits - BBBBBA
36102 ** 49 bits - BBBBBBA
36103 ** 56 bits - BBBBBBBA
36104 ** 64 bits - BBBBBBBBC
36108 ** Write a 64-bit variable-length integer to memory starting at p[0].
36112 ** A variable-length integer consists of the lower 7 bits of each byte
36123 for(i=7; i>=0; i--){ in putVarint64()
36136 for(i=0, j=n-1; j>=0; j--, i++){ in putVarint64()
36168 ** Read a 64-bit variable-length integer from memory starting at p[0].
36311 b = p[-4]; in sqlite3GetVarint()
36322 ** Read a 32-bit variable-length integer from memory starting at p[0].
36325 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
36329 ** single-byte case. All code should use the MACRO version as
36330 ** this function assumes the single-byte case has already been handled.
36336 /* Assume that the single-byte case has already been handled by in sqlite3GetVarint32()
36341 /* This is the two-byte case */ in sqlite3GetVarint32()
36346 /* This is the three-byte case */ in sqlite3GetVarint32()
36363 ** 64-bit integer.
36373 ** Read or write a four-byte big-endian integer value.
36440 n--; in sqlite3HexToBlob()
36483 eOpenState = db->eOpenState; in sqlite3SafetyCheckOk()
36496 eOpenState = db->eOpenState; in sqlite3SafetyCheckSickOrOk()
36509 ** Attempt to add, subtract, or multiply the 64-bit signed value iB against
36510 ** the other 64-bit signed integer at *pA and store the result in *pA.
36520 testcase( iB==-1 ); testcase( iB==0 ); in sqlite3AddInt64()
36522 testcase( iA>0 && LARGEST_INT64 - iA == iB ); in sqlite3AddInt64()
36523 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); in sqlite3AddInt64()
36524 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; in sqlite3AddInt64()
36526 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); in sqlite3AddInt64()
36527 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); in sqlite3AddInt64()
36528 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; in sqlite3AddInt64()
36540 testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); in sqlite3SubInt64()
36542 *pA -= iB; in sqlite3SubInt64()
36545 return sqlite3AddInt64(pA, -iB); in sqlite3SubInt64()
36563 if( -iA>LARGEST_INT64/-iB ) return 1; in sqlite3MulInt64()
36572 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
36573 ** if the integer has a value of -2147483648, return +2147483647
36578 return -x; in sqlite3AbsInt32()
36583 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
36589 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
36594 ** test.db-journal => test.nal
36595 ** test.db-wal => test.wal
36596 ** test.db-shm => test.shm
36597 ** test.db-mj7f3319fa => test.9fa
36606 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} in sqlite3FileSuffix3()
36607 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); in sqlite3FileSuffix3()
36625 5, 5, 5, /* 12-14 */ in sqlite3LogEstAdd()
36626 4, 4, 4, 4, /* 15-18 */ in sqlite3LogEstAdd()
36627 3, 3, 3, 3, 3, 3, /* 19-24 */ in sqlite3LogEstAdd()
36628 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ in sqlite3LogEstAdd()
36633 return a+x[a-b]; in sqlite3LogEstAdd()
36637 return b+x[b-a]; in sqlite3LogEstAdd()
36650 while( x<8 ){ y -= 10; x <<= 1; } in sqlite3LogEst()
36653 int i = 60 - __builtin_clzll(x); in sqlite3LogEst()
36657 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/ in sqlite3LogEst()
36661 return a[x&7] + y - 10; in sqlite3LogEst()
36675 e = (a>>52) - 1022; in sqlite3LogEstFromDouble()
36686 if( n>=5 ) n -= 2; in sqlite3LogEstToInt()
36687 else if( n>=1 ) n -= 1; in sqlite3LogEstToInt()
36689 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x); in sqlite3LogEstToInt()
36696 ** db->mallocFailed flag is set.
36709 ** is always zero-terminated.
36796 ** High-resolution hardware timer used for debugging and testing only.
36815 ** This file contains inline asm code for retrieving "high-performance"
36822 ** The following routine only works on Pentium-class (or newer) processors.
36824 ** processor and returns that value. This can be used for high-res
36906 ** This is the implementation of generic hash-tables
36919 pNew->first = 0;
36920 pNew->count = 0;
36921 pNew->htsize = 0;
36922 pNew->ht = 0;
36933 elem = pH->first;
36934 pH->first = 0;
36935 sqlite3_free(pH->ht);
36936 pH->ht = 0;
36937 pH->htsize = 0;
36939 HashElem *next_elem = elem->next;
36943 pH->count = 0;
36952 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
36955 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
36973 pHead = pEntry->count ? pEntry->chain : 0;
36974 pEntry->count++;
36975 pEntry->chain = pNew;
36980 pNew->next = pHead;
36981 pNew->prev = pHead->prev;
36982 if( pHead->prev ){ pHead->prev->next = pNew; }
36983 else { pH->first = pNew; }
36984 pHead->prev = pNew;
36986 pNew->next = pH->first;
36987 if( pH->first ){ pH->first->prev = pNew; }
36988 pNew->prev = 0;
36989 pH->first = pNew;
37008 if( new_size==pH->htsize ) return 0;
37024 sqlite3_free(pH->ht);
37025 pH->ht = new_ht;
37026 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
37028 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
37029 unsigned int h = strHash(elem->pKey) % new_size;
37030 next_elem = elem->next;
37051 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
37053 h = strHash(pKey) % pH->htsize;
37054 pEntry = &pH->ht[h];
37055 elem = pEntry->chain;
37056 count = pEntry->count;
37059 elem = pH->first;
37060 count = pH->count;
37065 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
37068 elem = elem->next;
37069 count--;
37083 if( elem->prev ){
37084 elem->prev->next = elem->next;
37086 pH->first = elem->next;
37088 if( elem->next ){
37089 elem->next->prev = elem->prev;
37091 if( pH->ht ){
37092 pEntry = &pH->ht[h];
37093 if( pEntry->chain==elem ){
37094 pEntry->chain = elem->next;
37096 assert( pEntry->count>0 );
37097 pEntry->count--;
37100 pH->count--;
37101 if( pH->count==0 ){
37102 assert( pH->first==0 );
37103 assert( pH->count==0 );
37115 return findElementWithHash(pH, pKey, 0)->data;
37140 if( elem->data ){
37141 void *old_data = elem->data;
37145 elem->data = data;
37146 elem->pKey = pKey;
37153 new_elem->pKey = pKey;
37154 new_elem->data = data;
37155 pH->count++;
37156 if( pH->count>=10 && pH->count > 2*pH->htsize ){
37157 if( rehash(pH, pH->count*2) ){
37158 assert( pH->htsize>0 );
37159 h = strHash(pKey) % pH->htsize;
37162 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
37239 /* 59 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
37240 /* 60 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
37241 /* 61 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
37270 /* 90 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
37287 /* 107 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
37304 /* 124 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"),
37340 /* 160 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
37378 ** 2022-09-06
37421 ** VFS - the database and the rollback journal.
37430 sqlite3_int64 szDb; /* Database file size. -1 means unknown */
37545 /* Forward declarations for the low-level storage engine
37561 sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn);
37565 ** underlying key/value store to use - either "local" or "session".
37567 ** Both zKey and zData are zero-terminated pure text strings.
37581 SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey,
37594 ** this routine is a no-op.
37600 SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey));
37611 ** not counting the final zero terminator. Return -1 if the key does
37631 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
37632 return -1;
37638 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey,
37647 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
37648 return -1;
37650 sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd);
37653 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey,
37680 ** for JavaScript-side implementations in WASM builds. In such builds
37709 ** ---------------
37711 ** * Non-zero bytes are encoded as upper-case hexadecimal
37713 ** * A sequence of one or more zero-bytes that are not at the
37714 ** beginning of the buffer are encoded as a little-endian
37715 ** base-26 number using a..z. "a" means 0. "b" means 1,
37719 ** of hexadecimal and base-26 numbers, it is always clear where
37731 /* A sequence of 1 or more zeros is stored as a little-endian
37732 ** base-26 number using a..z as the digits. So one zero is "b".
37738 i += k-1;
37750 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37751 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37752 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37753 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
37754 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37755 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37756 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37757 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37759 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37760 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37761 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37762 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37763 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37764 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37765 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37766 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
37789 n += (c - 'a')*mult;
37793 if( j+n>nOut ) return -1;
37809 ** Decode a complete journal file. Allocate space in pFile->aJrnl
37810 ** and store the decoding there. Or leave pFile->aJrnl set to NULL
37813 ** The first few characters of the text encoding will be a little-endian
37814 ** base-26 number (digits a..z) that is the total number of bytes
37815 ** in the decoded journal file image. This base-26 number is followed
37817 ** separator is required to act as a terminator for the base-26 number.
37820 KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */
37821 const char *zTxt, /* Text encoding. Zero-terminated */
37829 n += (zTxt[i] - 'a')*mult;
37832 sqlite3_free(pFile->aJrnl);
37833 pFile->aJrnl = sqlite3_malloc64( n );
37834 if( pFile->aJrnl==0 ){
37835 pFile->nJrnl = 0;
37838 pFile->nJrnl = n;
37839 n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl);
37840 if( n<pFile->nJrnl ){
37841 sqlite3_free(pFile->aJrnl);
37842 pFile->aJrnl = 0;
37843 pFile->nJrnl = 0;
37853 sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1);
37859 return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData);
37865 ** Close an kvvfs-file.
37870 SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
37871 pFile->isJournal ? "journal" : "db"));
37872 sqlite3_free(pFile->aJrnl);
37873 sqlite3_free(pFile->aData);
37878 ** Read from the -journal file.
37887 assert( pFile->isJournal );
37888 SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
37889 if( pFile->aJrnl==0 ){
37890 int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0);
37897 kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1);
37900 if( pFile->aJrnl==0 ) return SQLITE_IOERR;
37902 if( iOfst+iAmt>pFile->nJrnl ){
37905 memcpy(zBuf, pFile->aJrnl+iOfst, iAmt);
37922 char *aData = pFile->aData;
37925 SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
37930 if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){
37933 pFile->szPage = iAmt;
37939 got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey,
37940 aData, SQLITE_KVOS_SZ-1);
37948 n = kvvfsDecode(aData, &aData[2000], SQLITE_KVOS_SZ-2000);
37960 memset(zBuf+n, 0, iAmt-n);
37968 ** Write into the -journal file.
37978 SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
37980 if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){
37981 char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd);
37985 pFile->aJrnl = aNew;
37986 if( pFile->nJrnl<iOfst ){
37987 memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl);
37989 pFile->nJrnl = iEnd;
37991 memcpy(pFile->aJrnl+iOfst, zBuf, iAmt);
38007 char *aData = pFile->aData;
38008 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
38010 assert( (iAmt & (iAmt-1))==0 );
38011 assert( pFile->szPage<0 || pFile->szPage==iAmt );
38012 pFile->szPage = iAmt;
38016 if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
38019 if( iOfst+iAmt > pFile->szDb ){
38020 pFile->szDb = iOfst + iAmt;
38026 ** Truncate an kvvfs-file.
38030 SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size));
38032 sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl");
38033 sqlite3_free(pFile->aJrnl);
38034 pFile->aJrnl = 0;
38035 pFile->nJrnl = 0;
38040 if( pFile->szDb>size
38041 && pFile->szPage>0
38042 && (size % pFile->szPage)==0
38046 SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size));
38047 pgno = 1 + size/pFile->szPage;
38048 pgnoMax = 2 + pFile->szDb/pFile->szPage;
38051 sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey);
38054 pFile->szDb = size;
38061 ** Sync an kvvfs-file.
38067 SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass));
38068 if( pFile->nJrnl<=0 ){
38071 zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 );
38075 n = pFile->nJrnl;
38082 kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]);
38083 i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut);
38092 ** Return the current file-size of an kvvfs-file.
38096 SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass));
38097 *pSize = pFile->nJrnl;
38102 SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass));
38103 if( pFile->szDb>=0 ){
38104 *pSize = pFile->szDb;
38112 ** Lock an kvvfs-file.
38116 assert( !pFile->isJournal );
38117 SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock));
38120 pFile->szDb = kvvfsReadFileSize(pFile);
38126 ** Unlock an kvvfs-file.
38130 assert( !pFile->isJournal );
38131 SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock));
38133 pFile->szDb = -1;
38139 ** Check if another file-handle holds a RESERVED lock on an kvvfs-file.
38148 ** File control method. For custom operations on an kvvfs-file.
38159 SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass));
38160 if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){
38169 ** Return the sector-size in bytes for an kvvfs-file.
38176 ** Return the device characteristic flags supported by an kvvfs-file.
38200 pFile->isJournal = 0;
38201 pFile->base.pMethods = &kvvfs_db_io_methods;
38203 if( strcmp(zName, "local-journal")==0
38204 || strcmp(zName, "session-journal")==0
38206 pFile->isJournal = 1;
38207 pFile->base.pMethods = &kvvfs_jrnl_io_methods;
38212 pFile->zClass = "session";
38214 pFile->zClass = "local";
38216 pFile->aData = sqlite3_malloc64(SQLITE_KVOS_SZ);
38217 if( pFile->aData==0 ){
38220 pFile->aJrnl = 0;
38221 pFile->nJrnl = 0;
38222 pFile->szPage = -1;
38223 pFile->szDb = -1;
38229 ** ensure the file-system modifications are synced to disk before
38233 if( strcmp(zPath, "local-journal")==0 ){
38236 if( strcmp(zPath, "session-journal")==0 ){
38253 if( strcmp(zPath, "local-journal")==0 ){
38256 if( strcmp(zPath, "session-journal")==0 ){
38289 if( nOut<nPath+1 ) nPath = nOut - 1;
38341 ** This routine is called initialize the KV-vfs as the default VFS.
38371 ** This file contains the VFS implementation for unix-like operating systems
38377 ** use flock(), dot-files, various proprietary locking schemas, or simply
38387 ** * General-purpose declarations and utility functions.
38391 ** + for no-op locks
38392 ** + for dot-file locks
38400 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
38413 ** 3. Dot-file locking,
38473 ** -DHAVE_GETHOSTUUID=0
38474 ** -DHAVE_GETHOSTUUID=1
38477 ** -DSQLITE_ENABLE_LOCKING_STYLE.
38515 ** If we are to be thread-safe, include the pthreads header.
38536 ** Maximum supported path-length.
38555 # define SQLITE_DEFAULT_UNIX_VFS "unix-dotfile"
38556 /* ^^^ should SQLITE_DEFAULT_UNIX_VFS be "unix-none"? */
38595 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
38624 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
38651 ** transaction counter in bytes 24-27 of database files are updated
38716 ** The threadid macro resolves to the thread-id or to 0. Used for
38737 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
38738 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
38746 ** Linux-specific IOCTL magic numbers used for controlling F2FS
38764 ** which always has the same well-defined interface.
38775 ** Many system calls are accessed through pointer-to-functions so that
38804 ** DJGPP. But it is DOS - what did you expect?
38957 ** log if they come from non-root processes. So avoid calling fchown() if
39037 int i = -1;
39041 for(i=0; i<ArraySize(aSyscall)-1; i++){
39069 ** The m parameter will be non-zero only when creating -wal, -journal,
39070 ** and -shm files. We want those files to have *exactly* the same
39072 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
39097 fd = -1;
39162 ** integer lock-type.
39181 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
39182 ** command-line option on the compiler. This code is normally
39198 if( p->l_type==F_RDLCK ){
39200 }else if( p->l_type==F_WRLCK ){
39202 }else if( p->l_type==F_UNLCK ){
39207 assert( p->l_whence==SEEK_SET );
39211 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
39212 (int)p->l_pid, s);
39213 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
39226 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
39246 /* On Android, ftruncate() always uses 32-bit offsets, even if
39307 ** a performance-critical path, so it is sufficient to put these
39332 ** Changes are made in-place. Return the new name length.
39334 ** The original filename is in z[0..n-1]. Return the number of
39339 while( n>1 && z[n-1]=='/' ){ n--; }
39348 while( j>0 && z[j-1]!='/' ){ j--; }
39349 if( j>0 ){ j--; }
39380 pNew->zCanonicalName = (char*)&pNew[1];
39381 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
39382 n = vxworksSimplifyName(pNew->zCanonicalName, n);
39389 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
39390 if( pCandidate->nName==n
39391 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
39394 pCandidate->nRef++;
39401 pNew->nRef = 1;
39402 pNew->nName = n;
39403 pNew->pNext = vxworksFileList;
39415 assert( pId->nRef>0 );
39416 pId->nRef--;
39417 if( pId->nRef==0 ){
39419 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
39421 *pp = pId->pNext;
39479 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
39507 ** on linux - with NPTL a lock created by thread A can override locks
39508 ** in thread B. But there is no way to know at compile-time which
39510 ** compile-time whether or not thread A can override locks on thread B.
39511 ** One has to do a run-time check to discover the behavior of the
39531 ** sizes ino_t at only 32-bits instead of 64-bits. (See
39532 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
39533 ** To work around this, always allocate 64-bits for the inode number.
39534 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
39601 assert( pFile->pInode );
39602 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
39605 assert( pFile->pInode );
39606 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
39612 ** This function - unixLogErrorAtLine(), is only ever called via the macro
39617 ** errno and, if possible, the human-readable equivalent from strerror() or
39623 ** failed (e.g. "unlink", "open") and the associated file-system path,
39637 ** the strerror() function to obtain the human-readable error message
39658 ** int-type return, depending on its version.
39664 strerror_r(iErrno, aErr, sizeof(aErr)-1);
39670 /* Non-threadsafe build, use strerror(). */
39676 "os_unix.c:%d: (%d) %s(%s) - %s",
39699 pFile ? pFile->zPath : 0, lineno);
39704 ** Set the pFile->lastErrno. Do this in a subroutine as that provides
39708 pFile->lastErrno = error;
39712 ** Close all file descriptors accumulated in the unixInodeInfo->pUnused list.
39715 unixInodeInfo *pInode = pFile->pInode;
39719 for(p=pInode->pUnused; p; p=pNext){
39720 pNext = p->pNext;
39721 robust_close(pFile, p->fd, __LINE__);
39724 pInode->pUnused = 0;
39734 unixInodeInfo *pInode = pFile->pInode;
39738 pInode->nRef--;
39739 if( pInode->nRef==0 ){
39740 assert( pInode->pShmNode==0 );
39741 sqlite3_mutex_enter(pInode->pLockMutex);
39743 sqlite3_mutex_leave(pInode->pLockMutex);
39744 if( pInode->pPrev ){
39745 assert( pInode->pPrev->pNext==pInode );
39746 pInode->pPrev->pNext = pInode->pNext;
39749 inodeList = pInode->pNext;
39751 if( pInode->pNext ){
39752 assert( pInode->pNext->pPrev==pInode );
39753 pInode->pNext->pPrev = pInode->pPrev;
39755 sqlite3_mutex_free(pInode->pLockMutex);
39777 struct stat statbuf; /* Low-level file information */
39782 /* Get low-level information about the file that we can used to
39785 fd = pFile->h;
39790 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
39797 ** incorrectly for zero-size files. See ticket #3260. To work
39806 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
39823 fileId.pId = pFile->pId;
39829 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
39830 pInode = pInode->pNext;
39838 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
39840 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
39841 if( pInode->pLockMutex==0 ){
39846 pInode->nRef = 1;
39848 pInode->pNext = inodeList;
39849 pInode->pPrev = 0;
39850 if( inodeList ) inodeList->pPrev = pInode;
39853 pInode->nRef++;
39864 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
39867 return pFile->pInode!=0 &&
39868 (osStat(pFile->zPath, &buf)!=0
39869 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
39888 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
39890 rc = osFstat(pFile->h, &buf);
39892 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
39896 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
39900 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
39904 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
39913 ** to a non-zero value otherwise *pResOut is set to zero. The return value
39924 assert( pFile->eFileLock<=SHARED_LOCK );
39925 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
39928 if( pFile->pInode->eFileLock>SHARED_LOCK ){
39935 if( !reserved && !pFile->pInode->bProcessLock ){
39941 if( osFcntl(pFile->h, F_GETLK, &lock) ){
39950 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
39951 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
39961 ** Set a posix-advisory-lock.
39965 ** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
39970 ** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
39981 int tm = pFile->iBusyTimeout;
39991 tm--;
39999 ** Attempt to set a system-lock on the file pFile. The lock is
40002 ** If the pFile was opened read/write from unix-excl, then the only lock
40005 ** operations become no-ops. Locking operations still happen internally,
40010 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
40011 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
40012 ** and is read-only.
40014 ** Zero is returned if the call completes successfully, or -1 if a call
40019 unixInodeInfo *pInode = pFile->pInode;
40021 assert( sqlite3_mutex_held(pInode->pLockMutex) );
40022 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
40023 if( pInode->bProcessLock==0 ){
40025 assert( pInode->nLock==0 );
40030 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
40032 pInode->bProcessLock = 1;
40033 pInode->nLock++;
40038 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
40044 ** Lock the file with the lock specified by parameter eFileLock - one
40058 ** UNLOCKED -> SHARED
40059 ** SHARED -> RESERVED
40060 ** SHARED -> EXCLUSIVE
40061 ** RESERVED -> (PENDING) -> EXCLUSIVE
40062 ** PENDING -> EXCLUSIVE
40070 ** lock primitives (called read-locks and write-locks below, to avoid
40079 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
40080 ** byte'. If this is successful, 'shared byte range' is read-locked
40083 ** and Windows95 lacks a shared-lock capability. So on Windows95, a
40085 ** Windows95 is now pretty much extinct, but this work-around for the
40086 ** lack of shared-locks on Windows95 lives on, for backwards
40090 ** A RESERVED lock is implemented by grabbing a write-lock on the
40095 ** a write-lock on the entire 'shared byte range'. Since all other locks
40096 ** require a read-lock on one of the bytes within this range, this ensures
40101 ** obtaining a write-lock on the 'pending byte'. This ensures that no new
40105 ** then re-attempt the EXCLUSIVE lock later on, after existing SHARED
40115 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
40116 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
40117 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
40124 if( pFile->eFileLock>=eFileLock ){
40125 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
40135 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
40137 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
40139 /* This mutex is needed because pFile->pInode is shared across threads
40141 pInode = pFile->pInode;
40142 sqlite3_mutex_enter(pInode->pLockMutex);
40147 if( (pFile->eFileLock!=pInode->eFileLock &&
40148 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
40159 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
40161 assert( pFile->eFileLock==0 );
40162 assert( pInode->nShared>0 );
40163 pFile->eFileLock = SHARED_LOCK;
40164 pInode->nShared++;
40165 pInode->nLock++;
40177 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock==RESERVED_LOCK)
40189 pFile->eFileLock = PENDING_LOCK;
40190 pInode->eFileLock = PENDING_LOCK;
40199 assert( pInode->nShared==0 );
40200 assert( pInode->eFileLock==0 );
40203 /* Now get the read-lock */
40227 pFile->eFileLock = SHARED_LOCK;
40228 pInode->nLock++;
40229 pInode->nShared = 1;
40231 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
40240 assert( 0!=pFile->eFileLock );
40263 /* Set up the transaction-counter change checking flags when
40269 && pFile->eFileLock<=SHARED_LOCK
40272 pFile->transCntrChng = 0;
40273 pFile->dbUpdate = 0;
40274 pFile->inNormalWrite = 1;
40279 pFile->eFileLock = eFileLock;
40280 pInode->eFileLock = eFileLock;
40284 sqlite3_mutex_leave(pInode->pLockMutex);
40285 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
40295 unixInodeInfo *pInode = pFile->pInode;
40296 UnixUnusedFd *p = pFile->pPreallocatedUnused;
40298 p->pNext = pInode->pUnused;
40299 pInode->pUnused = p;
40300 pFile->h = -1;
40301 pFile->pPreallocatedUnused = 0;
40309 ** the requested locking level, this routine is a no-op.
40324 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
40325 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
40329 if( pFile->eFileLock<=eFileLock ){
40332 pInode = pFile->pInode;
40333 sqlite3_mutex_enter(pInode->pLockMutex);
40334 assert( pInode->nShared!=0 );
40335 if( pFile->eFileLock>SHARED_LOCK ){
40336 assert( pInode->eFileLock==pFile->eFileLock );
40347 pFile->inNormalWrite = 0;
40351 ** before establishing the readlock - to avoid a race condition we downgrade
40367 off_t divSize = SHARED_SIZE - 1;
40373 if( unixFileLock(pFile, &lock)==(-1) ){
40383 if( unixFileLock(pFile, &lock)==(-1) ){
40394 lock.l_len = SHARED_SIZE-divSize;
40395 if( unixFileLock(pFile, &lock)==(-1) ){
40426 pInode->eFileLock = SHARED_LOCK;
40438 pInode->nShared--;
40439 if( pInode->nShared==0 ){
40444 pInode->eFileLock = NO_LOCK;
40448 pInode->eFileLock = NO_LOCK;
40449 pFile->eFileLock = NO_LOCK;
40457 pInode->nLock--;
40458 assert( pInode->nLock>=0 );
40459 if( pInode->nLock==0 ) closePendingFds(pFile);
40463 sqlite3_mutex_leave(pInode->pLockMutex);
40465 pFile->eFileLock = eFileLock;
40475 ** the requested locking level, this routine is a no-op.
40479 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
40504 if( pFile->h>=0 ){
40505 robust_close(pFile, pFile->h, __LINE__);
40506 pFile->h = -1;
40509 if( pFile->pId ){
40510 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
40511 osUnlink(pFile->pId->zCanonicalName);
40513 vxworksReleaseFileId(pFile->pId);
40514 pFile->pId = 0;
40518 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
40519 osUnlink(pFile->zPath);
40520 sqlite3_free(*(char**)&pFile->zPath);
40521 pFile->zPath = 0;
40524 OSTRACE(("CLOSE %-3d\n", pFile->h));
40525 OpenCounter(-1);
40526 sqlite3_free(pFile->pPreallocatedUnused);
40537 unixInodeInfo *pInode = pFile->pInode;
40548 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
40549 sqlite3_mutex_enter(pInode->pLockMutex);
40550 if( pInode->nLock ){
40553 ** descriptor to pInode->pUnused list. It will be automatically closed
40558 sqlite3_mutex_leave(pInode->pLockMutex);
40560 assert( pFile->pShm==0 );
40570 ****************************** No-op Locking **********************************
40576 ** This locking mode is appropriate for use on read-only databases
40577 ** (ex: databases that are burned into CD-ROM, for example.) It can
40607 /******************* End of the no-op lock implementation *********************
40611 ************************* Begin dot-file Locking ******************************
40641 ** to a non-zero value otherwise *pResOut is set to zero. The return value
40656 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
40657 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
40663 ** Lock the file with the lock specified by parameter eFileLock - one
40677 ** UNLOCKED -> SHARED
40678 ** SHARED -> RESERVED
40679 ** SHARED -> (PENDING) -> EXCLUSIVE
40680 ** RESERVED -> (PENDING) -> EXCLUSIVE
40681 ** PENDING -> EXCLUSIVE
40691 char *zLockFile = (char *)pFile->lockingContext;
40698 if( pFile->eFileLock > NO_LOCK ){
40699 pFile->eFileLock = eFileLock;
40726 pFile->eFileLock = eFileLock;
40735 ** the requested locking level, this routine is a no-op.
40741 char *zLockFile = (char *)pFile->lockingContext;
40745 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
40746 pFile->eFileLock, osGetpid(0)));
40749 /* no-op if possible */
40750 if( pFile->eFileLock==eFileLock ){
40758 pFile->eFileLock = SHARED_LOCK;
40775 pFile->eFileLock = NO_LOCK;
40786 sqlite3_free(pFile->lockingContext);
40789 /****************** End of the dot-file lock implementation *******************
40797 ** flock() locking is like dot-file locking in that the various
40798 ** fine-grain locking levels supported by SQLite are collapsed into
40825 ** to a non-zero value otherwise *pResOut is set to zero. The return value
40838 if( pFile->eFileLock>SHARED_LOCK ){
40845 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
40848 lrc = robust_flock(pFile->h, LOCK_UN);
40867 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
40880 ** Lock the file with the lock specified by parameter eFileLock - one
40894 ** UNLOCKED -> SHARED
40895 ** SHARED -> RESERVED
40896 ** SHARED -> (PENDING) -> EXCLUSIVE
40897 ** RESERVED -> (PENDING) -> EXCLUSIVE
40898 ** PENDING -> EXCLUSIVE
40916 if (pFile->eFileLock > NO_LOCK) {
40917 pFile->eFileLock = eFileLock;
40923 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
40932 pFile->eFileLock = eFileLock;
40934 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
40950 ** the requested locking level, this routine is a no-op.
40956 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
40957 pFile->eFileLock, osGetpid(0)));
40960 /* no-op if possible */
40961 if( pFile->eFileLock==eFileLock ){
40967 pFile->eFileLock = eFileLock;
40972 if( robust_flock(pFile->h, LOCK_UN) ){
40978 pFile->eFileLock = NO_LOCK;
41002 ** Semaphore locking is like dot-lock and flock in that it really only
41012 ** to a non-zero value otherwise *pResOut is set to zero. The return value
41025 if( pFile->eFileLock>SHARED_LOCK ){
41031 sem_t *pSem = pFile->pInode->pSem;
41033 if( sem_trywait(pSem)==-1 ){
41040 reserved = (pFile->eFileLock < SHARED_LOCK);
41047 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
41054 ** Lock the file with the lock specified by parameter eFileLock - one
41068 ** UNLOCKED -> SHARED
41069 ** SHARED -> RESERVED
41070 ** SHARED -> (PENDING) -> EXCLUSIVE
41071 ** RESERVED -> (PENDING) -> EXCLUSIVE
41072 ** PENDING -> EXCLUSIVE
41084 sem_t *pSem = pFile->pInode->pSem;
41089 if (pFile->eFileLock > NO_LOCK) {
41090 pFile->eFileLock = eFileLock;
41096 if( sem_trywait(pSem)==-1 ){
41102 pFile->eFileLock = eFileLock;
41113 ** the requested locking level, this routine is a no-op.
41117 sem_t *pSem = pFile->pInode->pSem;
41121 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
41122 pFile->eFileLock, osGetpid(0)));
41125 /* no-op if possible */
41126 if( pFile->eFileLock==eFileLock ){
41132 pFile->eFileLock = eFileLock;
41137 if ( sem_post(pSem)==-1 ) {
41145 pFile->eFileLock = NO_LOCK;
41178 ** on Apple Macintosh computers - both OS9 and OSX.
41180 ** Third-party implementations of AFP are available. But this code here
41207 ** This is a utility for setting or clearing a bit-range lock on an
41226 pb.fd = pFile->h;
41229 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
41232 if ( err==-1 ) {
41255 ** to a non-zero value otherwise *pResOut is set to zero. The return value
41267 context = (afpLockingContext *) pFile->lockingContext;
41268 if( context->reserved ){
41272 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
41274 if( pFile->pInode->eFileLock>SHARED_LOCK ){
41282 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
41286 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
41296 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
41297 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
41304 ** Lock the file with the lock specified by parameter eFileLock - one
41318 ** UNLOCKED -> SHARED
41319 ** SHARED -> RESERVED
41320 ** SHARED -> (PENDING) -> EXCLUSIVE
41321 ** RESERVED -> (PENDING) -> EXCLUSIVE
41322 ** PENDING -> EXCLUSIVE
41330 unixInodeInfo *pInode = pFile->pInode;
41331 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
41334 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
41335 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
41336 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
41342 if( pFile->eFileLock>=eFileLock ){
41343 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
41353 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
41355 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
41357 /* This mutex is needed because pFile->pInode is shared across threads
41359 pInode = pFile->pInode;
41360 sqlite3_mutex_enter(pInode->pLockMutex);
41365 if( (pFile->eFileLock!=pInode->eFileLock &&
41366 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
41377 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
41379 assert( pFile->eFileLock==0 );
41380 assert( pInode->nShared>0 );
41381 pFile->eFileLock = SHARED_LOCK;
41382 pInode->nShared++;
41383 pInode->nLock++;
41392 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
41395 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
41409 assert( pInode->nShared==0 );
41410 assert( pInode->eFileLock==0 );
41413 /* Now get the read-lock SHARED_LOCK */
41416 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
41417 lrc1 = afpSetLock(context->dbPath, pFile,
41418 SHARED_FIRST+pInode->sharedByte, 1, 1);
41420 lrc1Errno = pFile->lastErrno;
41423 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
41435 pFile->eFileLock = SHARED_LOCK;
41436 pInode->nLock++;
41437 pInode->nShared = 1;
41439 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
41449 assert( 0!=pFile->eFileLock );
41450 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
41452 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
41454 context->reserved = 1;
41463 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
41464 pInode->sharedByte, 1, 0)) ){
41467 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
41469 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
41470 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
41488 pFile->eFileLock = eFileLock;
41489 pInode->eFileLock = eFileLock;
41491 pFile->eFileLock = PENDING_LOCK;
41492 pInode->eFileLock = PENDING_LOCK;
41496 sqlite3_mutex_leave(pInode->pLockMutex);
41497 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
41507 ** the requested locking level, this routine is a no-op.
41513 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
41517 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
41518 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
41522 if( pFile->eFileLock<=eFileLock ){
41525 pInode = pFile->pInode;
41526 sqlite3_mutex_enter(pInode->pLockMutex);
41527 assert( pInode->nShared!=0 );
41528 if( pFile->eFileLock>SHARED_LOCK ){
41529 assert( pInode->eFileLock==pFile->eFileLock );
41540 assert( pFile->inNormalWrite==0
41541 || pFile->dbUpdate==0
41542 || pFile->transCntrChng==1 );
41543 pFile->inNormalWrite = 0;
41546 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
41547 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
41548 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
41549 /* only re-establish the shared lock if necessary */
41550 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
41551 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
41556 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
41557 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
41559 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
41560 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
41562 context->reserved = 0;
41565 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
41566 pInode->eFileLock = SHARED_LOCK;
41575 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
41576 pInode->nShared--;
41577 if( pInode->nShared==0 ){
41579 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
41582 pInode->eFileLock = NO_LOCK;
41583 pFile->eFileLock = NO_LOCK;
41587 pInode->nLock--;
41588 assert( pInode->nLock>=0 );
41589 if( pInode->nLock==0 ) closePendingFds(pFile);
41593 sqlite3_mutex_leave(pInode->pLockMutex);
41595 pFile->eFileLock = eFileLock;
41610 if( pFile->pInode ){
41611 unixInodeInfo *pInode = pFile->pInode;
41612 sqlite3_mutex_enter(pInode->pLockMutex);
41613 if( pInode->nLock ){
41616 ** descriptor to pInode->aPending. It will be automatically closed when
41621 sqlite3_mutex_leave(pInode->pLockMutex);
41624 sqlite3_free(pFile->lockingContext);
41634 ** is available. If you don't compile for a mac, then the "unix-afp"
41649 ** the requested locking level, this routine is a no-op.
41665 **************** Non-locking sqlite3_file methods *****************************
41689 assert( id->h>2 );
41692 got = osPread(id->h, pBuf, cnt, offset);
41693 SimulateIOError( got = -1 );
41695 got = osPread64(id->h, pBuf, cnt, offset);
41696 SimulateIOError( got = -1 );
41698 newOffset = lseek(id->h, offset, SEEK_SET);
41699 SimulateIOError( newOffset = -1 );
41702 return -1;
41704 got = osRead(id->h, pBuf, cnt);
41713 cnt -= got;
41720 OSTRACE(("READ %-3d %5d %7lld %llu\n",
41721 id->h, got+prior, offset-prior, TIMER_ELAPSED));
41742 /* If this is a database file (not a journal, super-journal or temp
41745 assert( pFile->pPreallocatedUnused==0
41754 if( offset<pFile->mmapSize ){
41755 if( offset+amt <= pFile->mmapSize ){
41756 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
41759 int nCopy = pFile->mmapSize - offset;
41760 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
41762 amt -= nCopy;
41772 /* pFile->lastErrno has been set by seekAndRead().
41779 switch( pFile->lastErrno ){
41793 /* Unread parts of the buffer must be zero-filled */
41794 memset(&((char*)pBuf)[got], 0, amt-got);
41800 ** Attempt to seek the file-descriptor passed as the first argument to
41802 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
41828 SimulateIOError( iSeek = -1 );
41830 rc = -1;
41838 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
41846 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
41853 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
41872 /* If this is a database file (not a journal, super-journal or temp
41875 assert( pFile->pPreallocatedUnused==0
41883 ** doing a hot-journal rollback or a write to some file other than a
41888 if( pFile->inNormalWrite ){
41889 pFile->dbUpdate = 1; /* The database has been modified */
41896 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
41897 pFile->transCntrChng = 1; /* The transaction counter has changed */
41906 if( offset<pFile->mmapSize ){
41907 if( offset+amt <= pFile->mmapSize ){
41908 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
41911 int nCopy = pFile->mmapSize - offset;
41912 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
41914 amt -= nCopy;
41921 amt -= wrote;
41925 SimulateIOError(( wrote=(-1), amt=1 ));
41929 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
41954 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
42024 ** no-op. But go ahead and call fstat() to validate the file
42057 if( rc==-1 && errno==ENOTSUP ){
42063 if( OS_VXWORKS && rc!= -1 ){
42076 ** The directory file descriptor is used for only one thing - to
42085 ** replace this routine with a harmless no-op. To make this routine
42086 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
42094 int fd = -1;
42098 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
42107 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
42122 ** has been created by fsync-ing the directory that contains the file.
42127 ** will not roll back - possibly leading to database corruption.
42147 OSTRACE(("SYNC %-3d\n", pFile->h));
42148 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
42152 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
42156 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
42159 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
42161 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
42163 rc = osOpenDirectory(pFile->zPath, &dirfd);
42171 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
42185 /* If the user has configured a chunk-size for this file, truncate the
42190 if( pFile->szChunk>0 ){
42191 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
42194 rc = robust_ftruncate(pFile->h, nByte);
42197 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
42201 ** doing a hot-journal rollback or a write to some file other than a
42204 ** when restoring a database using the backup API from a zero-length
42207 if( pFile->inNormalWrite && nByte==0 ){
42208 pFile->transCntrChng = 1;
42217 if( nByte<pFile->mmapSize ){
42218 pFile->mmapSize = nByte;
42233 rc = osFstat(((unixFile*)id)->h, &buf);
42241 /* When opening a zero-size database, the findInodeInfo() procedure
42243 ** in the OS-X msdos filesystem. In order to avoid problems with upper
42255 ** Handler for proxy-locking file-control verbs. Defined below in the
42263 ** file-control operation. Enlarge the database to nBytes in size
42264 ** (rounded up to the next chunk-size). If the database is already
42265 ** nBytes or larger, this routine is a no-op.
42268 if( pFile->szChunk>0 ){
42272 if( osFstat(pFile->h, &buf) ){
42276 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
42285 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
42292 ** at offset (nSize-1), to set the size of the file correctly.
42296 int nBlk = buf.st_blksize; /* File-system block size */
42300 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
42303 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
42304 if( iWrite>=nSize ) iWrite = nSize - 1;
42313 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
42315 if( pFile->szChunk<=0 ){
42316 if( robust_ftruncate(pFile->h, nByte) ){
42318 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
42332 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
42334 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
42338 *pArg = (pFile->ctrlFlags & mask)!=0;
42340 pFile->ctrlFlags &= ~mask;
42342 pFile->ctrlFlags |= mask;
42360 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
42364 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
42368 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
42374 *(int*)pArg = pFile->eFileLock;
42378 *(int*)pArg = pFile->lastErrno;
42382 pFile->szChunk = *(int *)pArg;
42401 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
42405 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
42407 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
42418 int iOld = pFile->iBusyTimeout;
42420 pFile->iBusyTimeout = *(int*)pArg;
42422 pFile->iBusyTimeout = !!(*(int*)pArg);
42440 ** 64-bit type. */
42445 *(i64*)pArg = pFile->mmapSizeMax;
42446 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
42447 pFile->mmapSizeMax = newLimit;
42448 if( pFile->mmapSize>0 ){
42450 rc = unixMapfile(pFile, -1);
42463 ((unixFile*)id)->dbUpdate = 0;
42487 ** If pFd->sectorSize is non-zero when this function is called, it is a
42488 ** no-op. Otherwise, the values of pFd->sectorSize and
42489 ** pFd->deviceCharacteristics are set according to the file-system
42497 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
42498 if( pFd->sectorSize==0 ){
42504 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
42506 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
42511 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
42512 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
42515 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
42522 if( pFile->sectorSize == 0 ){
42525 /* Set defaults for non-supported filesystems */
42526 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
42527 pFile->deviceCharacteristics = 0;
42528 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
42533 pFile->sectorSize = fsInfo.f_bsize;
42534 pFile->deviceCharacteristics =
42542 pFile->sectorSize = fsInfo.f_bsize;
42543 pFile->deviceCharacteristics =
42545 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
42552 pFile->sectorSize = fsInfo.f_bsize;
42553 pFile->deviceCharacteristics =
42561 pFile->sectorSize = fsInfo.f_bsize;
42562 pFile->deviceCharacteristics =
42564 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
42569 pFile->sectorSize = fsInfo.f_bsize;
42570 pFile->deviceCharacteristics =
42572 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
42577 pFile->deviceCharacteristics =
42586 if( pFile->sectorSize % 512 != 0 ){
42587 pFile->deviceCharacteristics = 0;
42588 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
42599 ** if two files are created in the same file-system directory (i.e.
42606 return pFd->sectorSize;
42615 ** words, after a power-loss event, parts of the file that were never
42616 ** written might end up being altered.) However, non-PSOW behavior is very,
42619 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
42625 return pFd->deviceCharacteristics;
42653 ** When multiple threads all reference the same wal-index, each thread
42655 ** of this unixShmNode object. In other words, each wal-index is opened
42670 ** The following fields are read-only after the object is created:
42682 ** locks are held by the process on this slot. If it is set to -1, then
42693 ** of mutexes - one for each locking slot. To read or write locking
42703 int szRegion; /* Size of shared-memory regions */
42705 u8 isReadonly; /* True if read-only */
42707 char **apRegion; /* Array of mapped shared-memory regions */
42713 int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */
42724 ** are read-only thereafter:
42729 ** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
42735 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
42749 ** wal-mode transactions in other processes on database file pFile. If
42758 if( pFile->pShm){
42759 unixShmNode *pShmNode = pFile->pShm->pShmNode;
42766 f.l_len = SQLITE_SHM_NLOCK - 3;
42768 sqlite3_mutex_enter(pShmNode->pShmMutex);
42769 if( osFcntl(pShmNode->hShm, F_GETLK, &f)<0 ){
42774 sqlite3_mutex_leave(pShmNode->pShmMutex);
42782 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
42784 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
42793 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
42797 pShmNode = pFile->pInode->pShmNode;
42801 assert( pShmNode->nRef>=0 );
42806 assert( pShmNode->nRef>0 || unixMutexHeld() );
42807 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
42811 for(ii=ofst-UNIX_SHM_BASE; ii<ofst-UNIX_SHM_BASE+n; ii++){
42812 assert( sqlite3_mutex_held(pShmNode->aMutex[ii]) );
42815 assert( sqlite3_mutex_held(pShmNode->pShmMutex) );
42816 assert( pShmNode->nRef>0 );
42827 if( pShmNode->hShm>=0 ){
42834 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
42835 if( res==-1 ){
42837 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY);
42846 OSTRACE(("SHM-LOCK "));
42849 OSTRACE(("unlock %d..%d ok\n", ofst, ofst+n-1));
42851 OSTRACE(("read-lock %d..%d ok\n", ofst, ofst+n-1));
42854 OSTRACE(("write-lock %d..%d ok\n", ofst, ofst+n-1));
42858 OSTRACE(("unlock %d..%d failed\n", ofst, ofst+n-1));
42860 OSTRACE(("read-lock %d..%d failed\n", ofst, ofst+n-1));
42863 OSTRACE(("write-lock %d..%d failed\n", ofst, ofst+n-1));
42874 ** current system page-size.
42877 ** to use 64KB pages - in this case each mapping must cover at least two
42883 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
42891 ** This is not a VFS shared-memory method; it is a utility function called
42892 ** by VFS shared-memory methods.
42895 unixShmNode *p = pFd->pInode->pShmNode;
42897 if( p && ALWAYS(p->nRef==0) ){
42900 assert( p->pInode==pFd->pInode );
42901 sqlite3_mutex_free(p->pShmMutex);
42904 sqlite3_mutex_free(p->aMutex[i]);
42907 for(i=0; i<p->nRegion; i+=nShmPerMap){
42908 if( p->hShm>=0 ){
42909 osMunmap(p->apRegion[i], p->szRegion);
42911 sqlite3_free(p->apRegion[i]);
42914 sqlite3_free(p->apRegion);
42915 if( p->hShm>=0 ){
42916 robust_close(pFd, p->hShm, __LINE__);
42917 p->hShm = -1;
42919 p->pInode->pShmNode = 0;
42931 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
42940 ** and proceed with opening the *-shm file.
42944 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
42951 ** EXCLUSIVE failed just before truncating the *-shm file, then this
42952 ** process might open and use the *-shm file without truncating it.
42953 ** And if the *-shm file has been corrupted by a power failure or
42959 if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
42962 if( pShmNode->isReadonly ){
42963 pShmNode->isUnlocked = 1;
42969 ** *-shm file. And after it has done so, it will not release its
42973 int iSaveTimeout = pDbFd->iBusyTimeout;
42974 pDbFd->iBusyTimeout = 0;
42978 pDbFd->iBusyTimeout = iSaveTimeout;
42980 /* The first connection to attach must truncate the -shm file. We
42982 ** -shm header size) rather than 0 as a system debugging aid, to
42983 ** help detect if a -shm file truncation is legitimate or is the work
42985 if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){
42986 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
43001 ** Open a shared-memory area associated with open database file pDbFd.
43004 ** The file used to implement shared-memory is in the same directory
43006 ** file with the "-shm" suffix added. For example, if the database file
43008 ** for shared memory will be called "/home/user1/config.db-shm".
43014 ** database to end up using different files for shared memory -
43015 ** meaning that their memory would not really be shared - resulting
43017 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
43018 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
43022 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
43025 ** When opening a new shared-memory file, if no other instances of that
43029 ** If the original database file (pDbFd) is using the "unix-excl" VFS
43047 assert( pDbFd->pShm==0 );
43054 pInode = pDbFd->pInode;
43055 pShmNode = pInode->pShmNode;
43059 const char *zBasePath = pDbFd->zPath;
43063 ** a new *-shm file is created, an attempt will be made to create it
43066 if( osFstat(pDbFd->h, &sStat) ){
43082 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
43085 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
43088 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
43089 sqlite3FileSuffix3(pDbFd->zPath, zShm);
43091 pShmNode->hShm = -1;
43092 pDbFd->pInode->pShmNode = pShmNode;
43093 pShmNode->pInode = pDbFd->pInode;
43095 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
43096 if( pShmNode->pShmMutex==0 ){
43104 pShmNode->aMutex[ii] = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
43105 if( pShmNode->aMutex[ii]==0 ){
43114 if( pInode->bProcessLock==0 ){
43115 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
43116 pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW,
43119 if( pShmNode->hShm<0 ){
43120 pShmNode->hShm = robust_open(zShm, O_RDONLY|O_NOFOLLOW,
43122 if( pShmNode->hShm<0 ){
43126 pShmNode->isReadonly = 1;
43133 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
43141 p->pShmNode = pShmNode;
43143 p->id = pShmNode->nextShmId++;
43145 pShmNode->nRef++;
43146 pDbFd->pShm = p;
43153 ** at pShmNode->pFirst. This must be done while holding the
43154 ** pShmNode->pShmMutex.
43156 sqlite3_mutex_enter(pShmNode->pShmMutex);
43157 p->pNext = pShmNode->pFirst;
43158 pShmNode->pFirst = p;
43159 sqlite3_mutex_leave(pShmNode->pShmMutex);
43172 ** shared-memory associated with the database file fd. Shared-memory regions
43173 ** are numbered starting from zero. Each shared-memory region is szRegion
43178 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
43181 ** bExtend is non-zero and the requested shared-memory region has not yet
43184 ** If the shared-memory region has already been allocated or is allocated by
43203 /* If the shared-memory file has not yet been opened, open it now. */
43204 if( pDbFd->pShm==0 ){
43209 p = pDbFd->pShm;
43210 pShmNode = p->pShmNode;
43211 sqlite3_mutex_enter(pShmNode->pShmMutex);
43212 if( pShmNode->isUnlocked ){
43215 pShmNode->isUnlocked = 0;
43217 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
43218 assert( pShmNode->pInode==pDbFd->pInode );
43219 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
43220 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
43225 if( pShmNode->nRegion<nReqRegion ){
43230 pShmNode->szRegion = szRegion;
43232 if( pShmNode->hShm>=0 ){
43234 ** Check to see if it has been allocated (i.e. if the wal-index file is
43237 if( osFstat(pShmNode->hShm, &sStat) ){
43265 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
43266 const char *zFile = pShmNode->zFilename;
43277 pShmNode->apRegion, nReqRegion*sizeof(char *)
43283 pShmNode->apRegion = apNew;
43284 while( pShmNode->nRegion<nReqRegion ){
43288 if( pShmNode->hShm>=0 ){
43290 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
43291 MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
43294 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
43307 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
43309 pShmNode->nRegion += nShmPerMap;
43314 if( pShmNode->nRegion>iRegion ){
43315 *pp = pShmNode->apRegion[iRegion];
43319 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
43320 sqlite3_mutex_leave(pShmNode->pShmMutex);
43325 ** Check that the pShmNode->aLock[] array comports with the locking bitmasks
43340 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
43343 if( pX->exclMask & (1<<i) ){
43345 aLock[i] = -1;
43346 }else if( pX->sharedMask & (1<<i) ){
43353 assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) );
43354 return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0);
43360 ** Change the lock state for a shared-memory segment.
43377 u16 mask = (1<<(ofst+n)) - (1<<ofst); /* Mask of locks to take or release */
43380 p = pDbFd->pShm;
43382 pShmNode = p->pShmNode;
43384 aLock = pShmNode->aLock;
43386 assert( pShmNode==pDbFd->pInode->pShmNode );
43387 assert( pShmNode->pInode==pDbFd->pInode );
43395 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
43396 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
43413 u16 lockMask = (p->exclMask|p->sharedMask);
43414 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
43433 || 0==(p->exclMask & mask)
43435 if( ((flags & SQLITE_SHM_UNLOCK) && ((p->exclMask|p->sharedMask) & mask))
43436 || (flags==(SQLITE_SHM_SHARED|SQLITE_SHM_LOCK) && 0==(p->sharedMask & mask))
43449 ** case thread 2 does a non-blocking request for the WRITER lock. But -
43453 ** a lock on a read-locking slot at this point, this breaks the
43454 ** anti-deadlock rules (see above). */
43459 rc = sqlite3_mutex_try(pShmNode->aMutex[iMutex]);
43462 sqlite3_mutex_enter(pShmNode->aMutex[iMutex]);
43466 sqlite3_mutex_enter(pShmNode->pShmMutex);
43471 /* Case (a) - unlock. */
43473 assert( (p->exclMask & p->sharedMask)==0 );
43474 assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask );
43475 assert( !(flags & SQLITE_SHM_SHARED) || (p->sharedMask & mask)==mask );
43480 ** from the file-descriptor below. */
43486 aLock[ofst]--;
43487 p->sharedMask &= ~mask;
43495 p->sharedMask &= ~mask;
43496 p->exclMask &= ~mask;
43500 /* Case (b) - a shared lock. */
43511 p->sharedMask |= mask;
43515 /* Case (c) - an exclusive lock. */
43519 assert( (p->sharedMask & mask)==0 );
43520 assert( (p->exclMask & mask)==0 );
43532 ** also update the in-memory values. */
43536 p->exclMask |= mask;
43538 aLock[ii] = -1;
43549 for(iMutex--; iMutex>=ofst; iMutex--){
43550 sqlite3_mutex_leave(pShmNode->aMutex[iMutex]);
43553 sqlite3_mutex_leave(pShmNode->pShmMutex);
43557 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
43558 p->id, osGetpid(0), p->sharedMask, p->exclMask));
43572 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
43573 assert( fd->pMethods->xLock==nolockLock
43581 ** Close a connection to shared-memory. Delete the underlying
43585 ** routine is a harmless no-op.
43589 int deleteFlag /* Delete shared-memory if true */
43592 unixShmNode *pShmNode; /* The underlying shared-memory file */
43597 p = pDbFd->pShm;
43599 pShmNode = p->pShmNode;
43601 assert( pShmNode==pDbFd->pInode->pShmNode );
43602 assert( pShmNode->pInode==pDbFd->pInode );
43606 sqlite3_mutex_enter(pShmNode->pShmMutex);
43607 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
43608 *pp = p->pNext;
43612 pDbFd->pShm = 0;
43613 sqlite3_mutex_leave(pShmNode->pShmMutex);
43615 /* If pShmNode->nRef has reached 0, then close the underlying
43616 ** shared-memory file, too */
43619 assert( pShmNode->nRef>0 );
43620 pShmNode->nRef--;
43621 if( pShmNode->nRef==0 ){
43622 if( deleteFlag && pShmNode->hShm>=0 ){
43623 osUnlink(pShmNode->zFilename);
43645 assert( pFd->nFetchOut==0 );
43646 if( pFd->pMapRegion ){
43647 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
43648 pFd->pMapRegion = 0;
43649 pFd->mmapSize = 0;
43650 pFd->mmapSizeActual = 0;
43674 int h = pFd->h; /* File descriptor open on db file */
43675 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
43676 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
43680 assert( pFd->nFetchOut==0 );
43681 assert( nNew>pFd->mmapSize );
43682 assert( nNew<=pFd->mmapSizeMax );
43684 assert( pFd->mmapSizeActual>=pFd->mmapSize );
43688 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
43693 i64 nReuse = pFd->mmapSize;
43696 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
43702 osMunmap(pReq, nOrig-nReuse);
43709 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
43712 osMunmap(pNew, nNew - nReuse);
43734 unixLogError(SQLITE_OK, zErr, pFd->zPath);
43739 pFd->mmapSizeMax = 0;
43741 pFd->pMapRegion = (void *)pNew;
43742 pFd->mmapSize = pFd->mmapSizeActual = nNew;
43746 ** Memory map or remap the file opened by file-descriptor pFd (if the file
43749 ** outstanding xFetch() references to it, this function is a no-op.
43751 ** If parameter nByte is non-negative, then it is the requested size of
43762 assert( nMap>=0 || pFd->nFetchOut==0 );
43763 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
43764 if( pFd->nFetchOut>0 ) return SQLITE_OK;
43767 struct stat statbuf; /* Low-level file information */
43768 if( osFstat(pFd->h, &statbuf) ){
43773 if( nMap>pFd->mmapSizeMax ){
43774 nMap = pFd->mmapSizeMax;
43777 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
43778 if( nMap!=pFd->mmapSize ){
43805 if( pFd->mmapSizeMax>0 ){
43811 if( pFd->pMapRegion==0 ){
43812 int rc = unixMapfile(pFd, -1);
43815 if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){
43816 *pp = &((u8 *)pFd->pMapRegion)[iOff];
43817 pFd->nFetchOut++;
43825 ** If the third argument is non-NULL, then this function releases a
43842 assert( (p==0)==(pFd->nFetchOut==0) );
43845 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
43848 pFd->nFetchOut--;
43853 assert( pFd->nFetchOut>=0 );
43871 ** of "finder" functions. A finder-function is used to locate the appropriate
43874 ** the correct finder-function for that VFS.
43877 ** object. The only interesting finder-function is autolockIoFinder, which
43881 ** For finder-function F, two objects are created:
43883 ** (1) The real finder-function named "FImpt()".
43890 ** directly at the finder-function since C90 rules prevent a void*
44007 ** The proxy locking method is a "super-method" in the sense that it
44009 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
44078 if( statfs(filePath, &fsInfo) != -1 ){
44090 ** Test byte-range lock using fcntl(). If the call succeeds,
44091 ** assume that the file-system supports POSIX style locks.
44097 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
44137 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
44175 assert( pNew->pInode==NULL );
44180 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
44181 pNew->h = h;
44182 pNew->pVfs = pVfs;
44183 pNew->zPath = zFilename;
44184 pNew->ctrlFlags = (u8)ctrlFlags;
44186 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
44190 pNew->ctrlFlags |= UNIXFILE_PSOW;
44192 if( strcmp(pVfs->zName,"unix-excl")==0 ){
44193 pNew->ctrlFlags |= UNIXFILE_EXCL;
44197 pNew->pId = vxworksFindFileId(zFilename);
44198 if( pNew->pId==0 ){
44207 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
44212 pNew->lockingContext = (void*)zFilename;
44222 rc = findInodeInfo(pNew, &pNew->pInode);
44235 ** handle h - as it is guaranteed that no posix locks will be released
44243 h = -1;
44254 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
44261 pCtx->dbPath = zFilename;
44262 pCtx->reserved = 0;
44265 rc = findInodeInfo(pNew, &pNew->pInode);
44267 sqlite3_free(pNew->lockingContext);
44269 h = -1;
44290 pNew->lockingContext = zLockFile;
44299 rc = findInodeInfo(pNew, &pNew->pInode);
44300 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
44301 char *zSemName = pNew->pInode->aSemName;
44304 pNew->pId->zCanonicalName);
44307 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
44308 if( pNew->pInode->pSem == SEM_FAILED ){
44310 pNew->pInode->aSemName[0] = '\0';
44321 h = -1;
44323 pNew->ctrlFlags |= UNIXFILE_DELETE;
44329 pId->pMethods = pLockingStyle;
44382 ** pVfs->mxPathname bytes.
44389 /* It's odd to simulate an io-error here, but really this is just
44390 ** using the io-error infrastructure to test that SQLite handles this
44405 zBuf[nBuf-2] = 0;
44408 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){
44420 ** Routine to transform a unixFile into a proxy-locking unixFile.
44421 ** Implementation in the proxy-lock division, but used by unixOpen()
44429 ** file (not a journal or super-journal file) identified by pathname
44435 ** other file descriptor open on the same file is holding a file-lock.
44441 ** such file descriptor is located, -1 is returned.
44459 ** ignored and -1 is returned. The caller will try to open a new file
44468 while( pInode && (pInode->fileId.dev!=sStat.st_dev
44469 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
44470 pInode = pInode->pNext;
44474 assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
44475 sqlite3_mutex_enter(pInode->pLockMutex);
44477 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
44480 *pp = pUnused->pNext;
44482 sqlite3_mutex_leave(pInode->pLockMutex);
44522 ** this function queries the file-system for the permissions on the
44551 ** "<path to db>-journal"
44552 ** "<path to db>-wal"
44553 ** "<path to db>-journalNN"
44554 ** "<path to db>-walNN"
44560 ** a '-' character. However in 8+3 filename mode, or if a corrupt
44561 ** rollback journal specifies a super-journal with a goofy name, then
44562 ** the '-' might be missing or the '-' might be the first character in
44565 nDb = sqlite3Strlen30(zPath) - 1;
44567 if( zPath[nDb]=='-' ){
44573 nDb--;
44602 ** ReadWrite() -> (READWRITE | CREATE)
44603 ** ReadOnly() -> (READONLY)
44604 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
44606 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
44620 int fd = -1; /* File descriptor returned by open() */
44639 /* If creating a super- or main-file journal, this function will open
44640 ** a file-descriptor on the directory too. The first time unixSync()
44667 /* The main DB, main journal, WAL file and super-journal are never
44674 /* Assert that the upper layer has set one of the "file-type" flags. */
44701 fd = pUnused->fd;
44708 p->pPreallocatedUnused = pUnused;
44710 /* Database filenames are double-zero terminated if they are not
44718 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
44724 /* Generated temporary filenames are always double-zero terminated
44745 assert( !p->pPreallocatedUnused );
44750 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
44758 /* Failed to open the file for read/write access. Try read-only. */
44767 fd = pReadonly->fd;
44782 ** the case. The chown() system call will be a no-op if the current
44801 if( p->pPreallocatedUnused ){
44802 p->pPreallocatedUnused->fd = fd;
44803 p->pPreallocatedUnused->flags =
44822 p->openFlags = openFlags;
44827 if( fstatfs(fd, &fsInfo) == -1 ){
44833 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
44836 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
44852 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
44857 ** never use proxy, NULL means use proxy for non-local files only. */
44870 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
44888 sqlite3_free(p->pPreallocatedUnused);
44906 if( osUnlink(zPath)==(-1) ){
44975 int rc; /* Non-zero following any error */
44990 const char *zName, /* Name to append to pPath. Not zero-terminated */
44998 if( pPath->nUsed>1 ){
44999 assert( pPath->zOut[0]=='/' );
45000 while( pPath->zOut[--pPath->nUsed]!='/' ){}
45005 if( pPath->nUsed + nName + 2 >= pPath->nOut ){
45006 pPath->rc = SQLITE_ERROR;
45009 pPath->zOut[pPath->nUsed++] = '/';
45010 memcpy(&pPath->zOut[pPath->nUsed], zName, nName);
45011 pPath->nUsed += nName;
45013 if( pPath->rc==SQLITE_OK ){
45016 pPath->zOut[pPath->nUsed] = 0;
45017 zIn = pPath->zOut;
45020 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
45025 if( pPath->nSymlink++ > SQLITE_MAX_SYMLINK ){
45026 pPath->rc = SQLITE_CANTOPEN_BKPT;
45029 got = osReadlink(zIn, zLnk, sizeof(zLnk)-2);
45030 if( got<=0 || got>=(ssize_t)sizeof(zLnk)-2 ){
45031 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
45036 pPath->nUsed = 0;
45038 pPath->nUsed -= nName + 1;
45051 const char *zPath /* Path to append to pPath. Is zero-terminated */
45058 appendOnePathElement(pPath, &zPath[j], i-j);
45066 ** is stored as a nul-terminated string in the buffer pointed to by
45070 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
45088 if( osGetcwd(zPwd, sizeof(zPwd)-2)==0 ){
45130 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
45133 ** use dlsym() with -pedantic-errors?
45141 ** This work-around is unlikely to work correctly on any system where
45170 ** errors. The reports issued by valgrind are incorrect - we would
45172 ** uninitialized space in zBuf - but valgrind errors tend to worry
45220 ** -DHAVE_NANOSLEEP=0 (perhaps in conjuction with -DHAVE_USLEEP if
45240 ** The following variable, if set to a non-zero value, is interpreted as
45304 ** low-level error message when operating-system problems come up
45323 ** Proxy locking is a "uber-locking-method" in this sense: It uses the
45325 ** meta-layer over top of the primitive locking implemented above. For
45328 ** been defined - so that the primitive locking methods are available
45333 ** The default locking schemes in SQLite use byte-range locks on the
45338 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
45346 ** SHARED_RANGE 0x40000002 -> 0x40000200
45350 ** the read cache when byte-range locks are present. Enabling the read
45353 ** close-to-open semantics for ensuring cache coherency
45357 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
45366 ** -----------------
45398 ** -----------------------
45408 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
45409 ** by taking an sqlite-style shared lock on the conch file, reading the
45413 ** is patterned after the database file name as ".<databasename>-conch".
45421 ** The proxy file - a single-byte file used for all advisory file locks
45437 ** ---------------------
45441 ** Database files accessed on non-local file systems are
45486 int conchHeld; /* 1 if the conch is held, -1 if lockless */
45519 if( lPath[len-1]!='/' ){
45548 if( lockPath[i] == '/' && (i - start > 0) ){
45550 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
45551 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
45583 int fd = -1;
45592 ** 2. if that fails, and this is a lock file (not-conch), try creating
45594 ** 3. if that fails, try to open the file read-only
45599 fd = pUnused->fd;
45640 pNew->openFlags = openFlags;
45644 pUnused->fd = fd;
45645 pUnused->flags = openFlags;
45646 pNew->pPreallocatedUnused = pUnused;
45704 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
45716 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45717 unixFile *conchFile = pCtx->conchFile;
45720 char *cPath = pCtx->conchFilePath;
45724 int fd = -1;
45725 int rc = -1;
45728 /* create a new path by replace the trailing '-conch' with '-break' */
45731 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
45736 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
45757 robust_close(pFile, conchFile->h, __LINE__);
45758 conchFile->h = fd;
45759 conchFile->openFlags = O_RDWR | O_CREAT;
45776 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45777 unixFile *conchFile = pCtx->conchFile;
45784 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
45794 if( osFstat(conchFile->h, &buf) ){
45813 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
45835 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
45838 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
45848 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
45854 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45856 if( pCtx->conchHeld!=0 ){
45859 unixFile *conchFile = pCtx->conchFile;
45872 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
45873 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
45889 storeLastErrno(pFile, conchFile->lastErrno);
45901 ** retry with a new auto-generated path
45909 if( !pCtx->lockProxyPath ){
45910 /* for auto-named local lock file, just check the host ID and we'll
45914 size_t pathLen = (readLen - PROXY_PATHINDEX);
45917 pathLen=MAXPATHLEN-1;
45927 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
45928 readLen-PROXY_PATHINDEX)
45936 if( (conchFile->openFlags&O_RDWR) == 0 ){
45942 if( !pCtx->lockProxyPath ){
45943 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
45952 futimes(conchFile->h, NULL);
45954 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
45970 if( pCtx->lockProxyPath!=NULL ){
45971 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
45977 robust_ftruncate(conchFile->h, writeSize);
45979 full_fsync(conchFile->h,0,0);
45985 int err = osFstat(pFile->h, &buf);
45991 osFchmod(conchFile->h, cmode);
45994 rc = osFchmod(conchFile->h, cmode);
45995 }while( rc==(-1) && errno==EINTR );
46011 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
46014 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
46015 if( rc==SQLITE_OK && pFile->openFlags ){
46017 if( pFile->h>=0 ){
46018 robust_close(pFile, pFile->h, __LINE__);
46020 pFile->h = -1;
46021 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
46024 pFile->h = fd;
46030 if( rc==SQLITE_OK && !pCtx->lockProxy ){
46031 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
46032 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
46035 ** so try again via auto-naming
46047 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
46048 if( !pCtx->lockProxyPath ){
46054 pCtx->conchHeld = 1;
46056 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
46058 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
46059 afpCtx->dbPath = pCtx->lockProxyPath;
46062 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
46064 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
46067 } while (1); /* in case we need to retry the :auto: lock file -
46080 pCtx = (proxyLockingContext *)pFile->lockingContext;
46081 conchFile = pCtx->conchFile;
46082 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
46083 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
46085 if( pCtx->conchHeld>0 ){
46086 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
46088 pCtx->conchHeld = 0;
46089 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
46107 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
46119 for( i=(len-1); i>=0; i-- ){
46131 /* append the "-conch" suffix to the file */
46132 memcpy(&conchPath[i+1], "-conch", 7);
46139 /* Takes a fully configured proxy locking-style unix file and switches
46143 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
46144 char *oldPath = pCtx->lockProxyPath;
46147 if( pFile->eFileLock!=NO_LOCK ){
46156 unixFile *lockProxy = pCtx->lockProxy;
46157 pCtx->lockProxy=NULL;
46158 pCtx->conchHeld = 0;
46160 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
46165 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
46180 if( pFile->pMethod == &afpIoMethods ){
46183 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
46184 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
46188 if( pFile->pMethod == &dotlockIoMethods ){
46191 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
46192 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
46195 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
46196 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
46206 ** ->lockingContext
46207 ** ->pMethod
46215 if( pFile->eFileLock!=NO_LOCK ){
46225 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
46234 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
46236 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
46237 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
46239 ** (c) the file system is read-only, then enable no-locking access.
46247 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
46249 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
46254 pCtx->conchHeld = -1; /* read only FS/ lockless */
46260 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
46264 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
46265 if( pCtx->dbPath==NULL ){
46273 pCtx->oldLockingContext = pFile->lockingContext;
46274 pFile->lockingContext = pCtx;
46275 pCtx->pOldMethod = pFile->pMethod;
46276 pFile->pMethod = &proxyIoMethods;
46278 if( pCtx->conchFile ){
46279 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
46280 sqlite3_free(pCtx->conchFile);
46282 sqlite3DbFree(0, pCtx->lockProxyPath);
46283 sqlite3_free(pCtx->conchFilePath);
46286 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
46300 if( pFile->pMethod == &proxyIoMethods ){
46301 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
46303 if( pCtx->lockProxyPath ){
46304 *(const char **)pArg = pCtx->lockProxyPath;
46316 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
46319 /* turn off proxy locking - not supported. If support is added for
46325 /* turn off proxy locking - already off - NOOP */
46332 (proxyLockingContext*)pFile->lockingContext;
46334 || (pCtx->lockProxyPath &&
46335 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
46358 ** above this point are all utilities. The lock-related methods of the
46359 ** proxy-locking sqlite3_io_method object follow.
46366 ** to a non-zero value otherwise *pResOut is set to zero. The return value
46373 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46374 if( pCtx->conchHeld>0 ){
46375 unixFile *proxy = pCtx->lockProxy;
46376 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
46385 ** Lock the file with the lock specified by parameter eFileLock - one
46399 ** UNLOCKED -> SHARED
46400 ** SHARED -> RESERVED
46401 ** SHARED -> (PENDING) -> EXCLUSIVE
46402 ** RESERVED -> (PENDING) -> EXCLUSIVE
46403 ** PENDING -> EXCLUSIVE
46412 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46413 if( pCtx->conchHeld>0 ){
46414 unixFile *proxy = pCtx->lockProxy;
46415 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
46416 pFile->eFileLock = proxy->eFileLock;
46430 ** the requested locking level, this routine is a no-op.
46436 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46437 if( pCtx->conchHeld>0 ){
46438 unixFile *proxy = pCtx->lockProxy;
46439 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
46440 pFile->eFileLock = proxy->eFileLock;
46454 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
46455 unixFile *lockProxy = pCtx->lockProxy;
46456 unixFile *conchFile = pCtx->conchFile;
46460 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
46462 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
46465 pCtx->lockProxy = 0;
46468 if( pCtx->conchHeld ){
46472 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
46476 sqlite3DbFree(0, pCtx->lockProxyPath);
46477 sqlite3_free(pCtx->conchFilePath);
46478 sqlite3DbFree(0, pCtx->dbPath);
46480 pFile->lockingContext = pCtx->oldLockingContext;
46481 pFile->pMethod = pCtx->pOldMethod;
46483 return pFile->pMethod->xClose(id);
46503 ** This routine registers all VFS implementations for unix-like operating
46520 ** when compiling with -pedantic-errors on GCC.)
46523 ** finder-function. The finder-function returns a pointer to the
46526 ** macro for addition information on finder-functions.
46574 UNIXVFS("unix-none", nolockIoFinder ),
46575 UNIXVFS("unix-dotfile", dotlockIoFinder ),
46576 UNIXVFS("unix-excl", posixIoFinder ),
46578 UNIXVFS("unix-namedsem", semIoFinder ),
46581 UNIXVFS("unix-posix", posixIoFinder ),
46584 UNIXVFS("unix-flock", flockIoFinder ),
46587 UNIXVFS("unix-afp", afpIoFinder ),
46588 UNIXVFS("unix-nfs", nfsIoFinder ),
46589 UNIXVFS("unix-proxy", proxyIoFinder ),
46594 /* Double-check that the aSyscall[] array has been constructed
46620 ** READ-0 UNIX_SHM_BASE+3 123
46621 ** READ-1 UNIX_SHM_BASE+4 124
46622 ** READ-2 UNIX_SHM_BASE+5 125
46623 ** READ-3 UNIX_SHM_BASE+6 126
46624 ** READ-4 UNIX_SHM_BASE+7 127
46627 assert( UNIX_SHM_DMS==128 ); /* Byte offset of the deadman-switch */
46641 ** This routine is a no-op for unix.
46695 ** based on the sub-platform)?
46703 ** based on the sub-platform)?
46791 ** characters, so we allocate 4 bytes per character assuming worst-case of
46792 ** 4-bytes-per-character for UTF8.
46815 ** Returns non-zero if the character should be treated as a directory
46879 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
46892 typedef struct winShm winShm; /* A connection to shared-memory */
46893 typedef struct winShmNode winShmNode; /* A region of shared-memory */
46951 BOOL bNoLock; /* Non-zero if locking is disabled. */
46965 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
46975 * If this is non-zero, an isolated heap will be created by the native Win32
46981 * WARNING: It is important to note that when this setting is non-zero and the
46993 * This is the maximum possible initial size of the Win32-specific heap, in
47001 * This is the extra space for the initial size of the Win32-specific heap,
47014 # define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
47021 * Win32-specific heap. It cannot be negative.
47027 # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
47033 * initial size of the Win32-specific heap to exceed the maximum amount
47042 * The initial size of the Win32-specific heap. This value may be zero.
47051 * The maximum size of the Win32-specific heap. This value may be zero.
47067 ** The winMemData structure stores information required by the Win32-specific
47151 ** Many system calls are accessed through pointer-to-functions so that
47768 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
47880 int i = -1;
47884 for(i=0; i<ArraySize(aSyscall)-1; i++){
47899 ** "pnLargest" argument, if non-zero, will be used to return the size of the
47989 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
47990 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
47991 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
48057 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
48085 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
48110 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
48205 if( n==(SIZE_T)-1 ){
48227 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
48228 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
48231 if( !pWinMemData->hHeap ){
48239 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
48241 if( !pWinMemData->hHeap ){
48248 pWinMemData->bOwned = TRUE;
48249 assert( pWinMemData->bOwned );
48252 pWinMemData->hHeap = osGetProcessHeap();
48253 if( !pWinMemData->hHeap ){
48258 pWinMemData->bOwned = FALSE;
48259 assert( !pWinMemData->bOwned );
48261 assert( pWinMemData->hHeap!=0 );
48262 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
48264 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
48276 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
48277 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
48279 if( pWinMemData->hHeap ){
48280 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
48282 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
48284 if( pWinMemData->bOwned ){
48285 if( !osHeapDestroy(pWinMemData->hHeap) ){
48287 osGetLastError(), (void*)pWinMemData->hHeap);
48289 pWinMemData->bOwned = FALSE;
48291 pWinMemData->hHeap = NULL;
48296 ** Populate the low-level memory allocation function pointers in
48323 ** Convert a UTF-8 string to Microsoft Unicode.
48331 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
48339 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
48349 ** Convert a Microsoft Unicode string to UTF-8.
48357 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
48365 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
48385 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
48394 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
48404 ** Convert a Microsoft Unicode string to a multi-byte character string,
48414 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
48422 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
48432 ** Convert a multi-byte character string to UTF-8.
48450 ** Convert a UTF-8 string to a multi-byte character string.
48565 ** it accepts a UTF-8 string.
48609 ** it accepts a UTF-16 string.
48642 ** is zero if the error message fits in the buffer, or non-zero
48722 ** This function - winLogErrorAtLine() - is only ever called via the macro
48727 ** error code and, if possible, the human-readable equivalent from
48733 ** failed and the associated file-system path, if any.
48753 "os_win.c:%d: (%lu) %s(%s) - %s",
48762 ** will be retried following a locking error - probably caused by
48779 ** non-zero if the error code is transient in nature and the operation
48867 y.tm_year = pTm.wYear - 1900;
48868 y.tm_mon = pTm.wMonth - 1;
48882 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
48916 memset(&pFile->local, 0, sizeof(pFile->local));
48926 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
48927 if (!pFile->hMutex){
48928 pFile->lastErrno = osGetLastError();
48930 return winLogError(SQLITE_IOERR, pFile->lastErrno,
48935 winceMutexAcquire(pFile->hMutex);
48938 ** case-sensitive, take advantage of that by uppercasing the mutex name
48942 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
48947 ** must be zero-initialized */
48956 if( pFile->hShared ){
48957 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
48960 if( !pFile->shared ){
48961 pFile->lastErrno = osGetLastError();
48962 winLogError(SQLITE_IOERR, pFile->lastErrno,
48965 osCloseHandle(pFile->hShared);
48966 pFile->hShared = NULL;
48971 if( pFile->hShared==NULL ){
48973 pFile->lastErrno = lastErrno;
48974 winLogError(SQLITE_IOERR, pFile->lastErrno,
48978 winceMutexRelease(pFile->hMutex);
48979 osCloseHandle(pFile->hMutex);
48980 pFile->hMutex = NULL;
48986 memset(pFile->shared, 0, sizeof(winceLock));
48989 winceMutexRelease(pFile->hMutex);
48997 if (pFile->hMutex){
48999 winceMutexAcquire(pFile->hMutex);
49003 if (pFile->local.nReaders){
49004 pFile->shared->nReaders --;
49006 if (pFile->local.bReserved){
49007 pFile->shared->bReserved = FALSE;
49009 if (pFile->local.bPending){
49010 pFile->shared->bPending = FALSE;
49012 if (pFile->local.bExclusive){
49013 pFile->shared->bExclusive = FALSE;
49016 /* De-reference and close our copy of the shared memory handle */
49017 osUnmapViewOfFile(pFile->shared);
49018 osCloseHandle(pFile->hShared);
49021 winceMutexRelease(pFile->hMutex);
49022 osCloseHandle(pFile->hMutex);
49023 pFile->hMutex = NULL;
49043 if (!pFile->hMutex) return TRUE;
49044 winceMutexAcquire(pFile->hMutex);
49049 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
49050 pFile->shared->bExclusive = TRUE;
49051 pFile->local.bExclusive = TRUE;
49056 /* Want a read-only lock? */
49059 if (pFile->shared->bExclusive == 0){
49060 pFile->local.nReaders ++;
49061 if (pFile->local.nReaders == 1){
49062 pFile->shared->nReaders ++;
49072 if (pFile->shared->bPending == 0) {
49073 pFile->shared->bPending = TRUE;
49074 pFile->local.bPending = TRUE;
49082 if (pFile->shared->bReserved == 0) {
49083 pFile->shared->bReserved = TRUE;
49084 pFile->local.bReserved = TRUE;
49089 winceMutexRelease(pFile->hMutex);
49109 if (!pFile->hMutex) return TRUE;
49110 winceMutexAcquire(pFile->hMutex);
49115 if (pFile->local.bExclusive){
49117 pFile->local.bExclusive = FALSE;
49118 pFile->shared->bExclusive = FALSE;
49123 else if (pFile->local.nReaders){
49126 pFile->local.nReaders --;
49127 if (pFile->local.nReaders == 0)
49129 pFile->shared->nReaders --;
49138 if (pFile->local.bPending){
49139 pFile->local.bPending = FALSE;
49140 pFile->shared->bPending = FALSE;
49147 if (pFile->local.bReserved) {
49148 pFile->local.bReserved = FALSE;
49149 pFile->shared->bReserved = FALSE;
49154 winceMutexRelease(pFile->hMutex);
49234 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
49240 ** Otherwise, set pFile->lastErrno and return non-zero.
49249 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
49255 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
49261 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
49265 pFile->lastErrno = lastErrno;
49266 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
49267 "winSeekFile", pFile->zPath);
49268 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
49272 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
49283 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
49286 pFile->lastErrno = osGetLastError();
49287 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
49288 "winSeekFile", pFile->zPath);
49289 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
49293 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
49321 assert( pFile->pShm==0 );
49323 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
49325 osGetCurrentProcessId(), pFile, pFile->h));
49332 rc = osCloseHandle(pFile->h);
49338 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
49339 if( pAppData==NULL || !pAppData->bNoLock ){
49343 if( pFile->zDeleteOnClose ){
49346 osDeleteFileW(pFile->zDeleteOnClose)==0
49347 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
49352 sqlite3_free(pFile->zDeleteOnClose);
49356 pFile->h = NULL;
49358 OpenCounter(-1);
49360 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
49363 "winClose", pFile->zPath);
49390 pFile->h, pBuf, amt, offset, pFile->locktype));
49395 if( offset<pFile->mmapSize ){
49396 if( offset+amt <= pFile->mmapSize ){
49397 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
49398 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
49399 osGetCurrentProcessId(), pFile, pFile->h));
49402 int nCopy = (int)(pFile->mmapSize - offset);
49403 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
49405 amt -= nCopy;
49414 osGetCurrentProcessId(), pFile, pFile->h));
49417 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
49422 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
49427 pFile->lastErrno = lastErrno;
49429 osGetCurrentProcessId(), pFile, pFile->h));
49430 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
49431 "winRead", pFile->zPath);
49435 /* Unread parts of the buffer must be zero-filled */
49436 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
49438 osGetCurrentProcessId(), pFile, pFile->h));
49443 osGetCurrentProcessId(), pFile, pFile->h));
49468 pFile->h, pBuf, amt, offset, pFile->locktype));
49473 if( offset<pFile->mmapSize ){
49474 if( offset+amt <= pFile->mmapSize ){
49475 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
49476 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
49477 osGetCurrentProcessId(), pFile, pFile->h));
49480 int nCopy = (int)(pFile->mmapSize - offset);
49481 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
49483 amt -= nCopy;
49511 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
49513 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
49529 nRem -= nWrite;
49532 pFile->lastErrno = lastErrno;
49538 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
49539 || ( pFile->lastErrno==ERROR_DISK_FULL )){
49541 osGetCurrentProcessId(), pFile, pFile->h));
49542 return winLogError(SQLITE_FULL, pFile->lastErrno,
49543 "winWrite1", pFile->zPath);
49546 osGetCurrentProcessId(), pFile, pFile->h));
49547 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
49548 "winWrite2", pFile->zPath);
49553 osGetCurrentProcessId(), pFile, pFile->h));
49566 if( pFile->nFetchOut>0 ){
49567 /* File truncation is a no-op if there are outstanding memory mapped
49573 ** No real harm comes of this - the database file is not corrupted,
49577 ** The only feasible work-around is to defer the truncation until after
49578 ** all references to memory-mapped content are closed. That is doable,
49581 ** now to simply make transactions a no-op if there are pending reads. We
49591 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
49593 /* If the user has configured a chunk-size for this file, truncate the
49598 if( pFile->szChunk>0 ){
49599 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
49603 if( pFile->pMapRegion ){
49604 oldMmapSize = pFile->mmapSize;
49611 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
49613 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
49614 "winTruncate1", pFile->zPath);
49615 }else if( 0==osSetEndOfFile(pFile->h) &&
49617 pFile->lastErrno = lastErrno;
49618 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
49619 "winTruncate2", pFile->zPath);
49625 winMapfile(pFile, -1);
49633 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
49679 osGetCurrentProcessId(), pFile, pFile->h, flags,
49680 pFile->locktype));
49692 ** no-op
49695 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
49696 osGetCurrentProcessId(), pFile, pFile->h));
49700 if( pFile->pMapRegion ){
49701 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
49702 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
49704 pFile, pFile->pMapRegion));
49706 pFile->lastErrno = osGetLastError();
49707 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
49709 pFile, pFile->pMapRegion));
49710 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
49711 "winSync1", pFile->zPath);
49715 rc = osFlushFileBuffers(pFile->h);
49719 osGetCurrentProcessId(), pFile, pFile->h));
49722 pFile->lastErrno = osGetLastError();
49724 osGetCurrentProcessId(), pFile, pFile->h));
49725 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
49726 "winSync2", pFile->zPath);
49741 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
49746 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
49750 pFile->lastErrno = osGetLastError();
49751 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
49752 "winFileSize", pFile->zPath);
49761 lowerBits = osGetFileSize(pFile->h, &upperBits);
49765 pFile->lastErrno = lastErrno;
49766 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
49767 "winFileSize", pFile->zPath);
49772 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
49814 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
49821 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
49823 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
49831 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
49832 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
49833 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
49837 pFile->lastErrno = osGetLastError();
49840 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
49850 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
49852 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
49856 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
49860 pFile->lastErrno = lastErrno;
49861 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
49862 "winUnlockReadLock", pFile->zPath);
49864 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
49869 ** Lock the file with the lock specified by parameter locktype - one
49883 ** UNLOCKED -> SHARED
49884 ** SHARED -> RESERVED
49885 ** SHARED -> (PENDING) -> EXCLUSIVE
49886 ** RESERVED -> (PENDING) -> EXCLUSIVE
49887 ** PENDING -> EXCLUSIVE
49897 int newLocktype; /* Set pFile->locktype to this value before exiting */
49904 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
49910 if( pFile->locktype>=locktype ){
49911 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
49915 /* Do not allow any kind of write-lock on a read-only database
49917 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
49923 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
49925 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
49931 newLocktype = pFile->locktype;
49932 if( pFile->locktype==NO_LOCK
49933 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
49936 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
49939 ** around problems caused by indexing and/or anti-virus software on
49945 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
49946 pFile->h, cnt, res));
49948 pFile->lastErrno = lastErrno;
49950 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
49951 pFile->h, cnt, sqlite3ErrName(rc)));
49965 assert( pFile->locktype==NO_LOCK );
49977 assert( pFile->locktype==SHARED_LOCK );
49978 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
49996 assert( pFile->locktype>=SHARED_LOCK );
49998 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
50012 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
50021 pFile->lastErrno = lastErrno;
50023 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
50024 pFile->h, locktype, newLocktype));
50026 pFile->locktype = (u8)newLocktype;
50028 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
50035 ** non-zero, otherwise zero.
50042 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
50045 if( pFile->locktype>=RESERVED_LOCK ){
50047 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
50049 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
50051 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
50054 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
50057 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
50058 pFile->h, pResOut, *pResOut));
50067 ** the requested locking level, this routine is a no-op.
50080 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
50081 type = pFile->locktype;
50083 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
50088 "winUnlock", pFile->zPath);
50092 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
50098 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
50100 pFile->locktype = (u8)locktype;
50102 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
50107 ****************************** No-op Locking **********************************
50113 ** This locking mode is appropriate for use on read-only databases
50114 ** (ex: databases that are burned into CD-ROM, for example.) It can
50141 /******************* End of the no-op lock implementation *********************
50146 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
50148 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
50152 *pArg = (pFile->ctrlFlags & mask)!=0;
50154 pFile->ctrlFlags &= ~mask;
50156 pFile->ctrlFlags |= mask;
50171 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
50174 *(int*)pArg = pFile->locktype;
50175 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50179 *(int*)pArg = (int)pFile->lastErrno;
50180 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50184 pFile->szChunk = *(int *)pArg;
50185 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50189 if( pFile->szChunk>0 ){
50200 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
50203 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50208 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50213 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50217 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
50218 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50233 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50238 *phFile = pFile->h;
50239 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50245 HANDLE hOldFile = pFile->h;
50246 pFile->h = *phFile;
50249 hOldFile, pFile->h));
50255 int rc = winGetTempname(pFile->pVfs, &zTFile);
50259 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
50272 ** least a 64-bit type. */
50277 *(i64*)pArg = pFile->mmapSizeMax;
50278 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
50279 pFile->mmapSizeMax = newLimit;
50280 if( pFile->mmapSize>0 ){
50282 rc = winMapfile(pFile, -1);
50285 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
50290 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
50300 ** if two files are created in the same file-system directory (i.e.
50315 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
50357 ** log-summary, each thread has its own winFile object, but they all
50359 ** log-summary is opened only once per process.
50367 ** The following fields are read-only after the object is created:
50382 int szRegion; /* Size of shared-memory regions */
50384 u8 isReadonly; /* True if read-only */
50413 ** are read-only thereafter:
50418 ** All other fields are read/write. The winShm.pShmNode->mutex must be held
50445 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
50453 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
50455 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
50456 pFile->hFile.h, lockType, ofst, nByte));
50458 /* Release/Acquire the system-level lock */
50460 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
50465 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
50471 pFile->lastErrno = osGetLastError();
50475 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
50476 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
50477 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
50489 ** This is not a VFS shared-memory method; it is a utility function called
50490 ** by VFS shared-memory methods.
50496 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
50500 if( p->nRef==0 ){
50502 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
50503 for(i=0; i<p->nRegion; i++){
50504 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
50505 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
50508 bRc = osCloseHandle(p->aRegion[i].hMap);
50509 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
50513 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
50515 winClose((sqlite3_file *)&p->hFile);
50521 winDelete(pVfs, p->zFilename, 0);
50525 *pp = p->pNext;
50526 sqlite3_free(p->aRegion);
50529 pp = &p->pNext;
50541 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
50547 if( pShmNode->isReadonly ){
50548 pShmNode->isUnlocked = 1;
50551 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
50554 "winLockSharedMemory", pShmNode->zFilename);
50566 ** Open the shared-memory area associated with database file pDbFd.
50568 ** When opening a new shared-memory file, if no other instances of that
50579 assert( pDbFd->pShm==0 ); /* Not previously opened */
50586 nName = sqlite3Strlen30(pDbFd->zPath);
50592 pNew->zFilename = (char*)&pNew[1];
50593 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
50594 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
50600 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
50604 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
50614 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
50615 pShmNode->pNext = winShmNodeList;
50619 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
50620 if( pShmNode->mutex==0 ){
50626 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
50631 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
50632 (sqlite3_file*)&pShmNode->hFile,
50636 pShmNode->zFilename);
50639 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
50646 p->pShmNode = pShmNode;
50648 p->id = pShmNode->nextShmId++;
50650 pShmNode->nRef++;
50651 pDbFd->pShm = p;
50658 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
50661 sqlite3_mutex_enter(pShmNode->mutex);
50662 p->pNext = pShmNode->pFirst;
50663 pShmNode->pFirst = p;
50664 sqlite3_mutex_leave(pShmNode->mutex);
50670 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
50678 ** Close a connection to shared-memory. Delete the underlying
50685 winFile *pDbFd; /* Database holding shared-memory */
50687 winShmNode *pShmNode; /* The underlying shared-memory file */
50691 p = pDbFd->pShm;
50693 pShmNode = p->pShmNode;
50697 sqlite3_mutex_enter(pShmNode->mutex);
50698 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
50699 *pp = p->pNext;
50703 pDbFd->pShm = 0;
50704 sqlite3_mutex_leave(pShmNode->mutex);
50706 /* If pShmNode->nRef has reached 0, then close the underlying
50707 ** shared-memory file, too */
50709 assert( pShmNode->nRef>0 );
50710 pShmNode->nRef--;
50711 if( pShmNode->nRef==0 ){
50712 winShmPurge(pDbFd->pVfs, deleteFlag);
50720 ** Change the lock state for a shared-memory segment.
50729 winShm *p = pDbFd->pShm; /* The shared memory being locked */
50736 pShmNode = p->pShmNode;
50747 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
50749 sqlite3_mutex_enter(pShmNode->mutex);
50754 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
50756 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
50757 allMask |= pX->sharedMask;
50760 /* Unlock the system-level locks */
50769 p->exclMask &= ~mask;
50770 p->sharedMask &= ~mask;
50779 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
50780 if( (pX->exclMask & mask)!=0 ){
50784 allShared |= pX->sharedMask;
50798 p->sharedMask |= mask;
50804 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
50805 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
50817 assert( (p->sharedMask & mask)==0 );
50818 p->exclMask |= mask;
50822 sqlite3_mutex_leave(pShmNode->mutex);
50823 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
50824 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
50839 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
50846 ** shared-memory associated with the database file fd. Shared-memory regions
50847 ** are numbered starting from zero. Each shared-memory region is szRegion
50852 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
50855 ** isWrite is non-zero and the requested shared-memory region has not yet
50858 ** If the shared-memory region has already been allocated or is allocated by
50871 winShm *pShm = pDbFd->pShm;
50880 pShm = pDbFd->pShm;
50883 pShmNode = pShm->pShmNode;
50885 sqlite3_mutex_enter(pShmNode->mutex);
50886 if( pShmNode->isUnlocked ){
50889 pShmNode->isUnlocked = 0;
50891 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
50893 if( pShmNode->nRegion<=iRegion ){
50896 sqlite3_int64 sz; /* Current size of wal-index file */
50898 pShmNode->szRegion = szRegion;
50901 ** Check to see if it has been allocated (i.e. if the wal-index file is
50904 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
50907 "winShmMap1", pDbFd->zPath);
50915 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
50919 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
50922 "winShmMap2", pDbFd->zPath);
50929 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
50935 pShmNode->aRegion = apNew;
50937 if( pShmNode->isReadonly ){
50942 while( pShmNode->nRegion<=iRegion ){
50943 HANDLE hMap = NULL; /* file-mapping handle */
50947 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
50951 hMap = osCreateFileMappingW(pShmNode->hFile.h,
50955 hMap = osCreateFileMappingA(pShmNode->hFile.h,
50959 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
50960 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
50963 int iOffset = pShmNode->nRegion*szRegion;
50967 iOffset - iOffsetShift, szRegion + iOffsetShift
50971 0, iOffset - iOffsetShift, szRegion + iOffsetShift
50974 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
50975 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
50979 pShmNode->lastErrno = osGetLastError();
50980 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
50981 "winShmMap3", pDbFd->zPath);
50986 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
50987 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
50988 pShmNode->nRegion++;
50993 if( pShmNode->nRegion>iRegion ){
50996 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
51001 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
51002 sqlite3_mutex_leave(pShmNode->mutex);
51019 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
51021 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
51022 pFile->mmapSize, pFile->mmapSizeMax));
51023 if( pFile->pMapRegion ){
51024 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
51025 pFile->lastErrno = osGetLastError();
51026 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
51028 pFile->pMapRegion));
51029 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
51030 "winUnmapfile1", pFile->zPath);
51032 pFile->pMapRegion = 0;
51033 pFile->mmapSize = 0;
51035 if( pFile->hMap!=NULL ){
51036 if( !osCloseHandle(pFile->hMap) ){
51037 pFile->lastErrno = osGetLastError();
51038 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
51039 osGetCurrentProcessId(), pFile, pFile->hMap));
51040 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
51041 "winUnmapfile2", pFile->zPath);
51043 pFile->hMap = NULL;
51045 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
51051 ** Memory map or remap the file opened by file-descriptor pFd (if the file
51054 ** outstanding xFetch() references to it, this function is a no-op.
51056 ** If parameter nByte is non-negative, then it is the requested size of
51070 assert( nMap>=0 || pFd->nFetchOut==0 );
51071 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
51074 if( pFd->nFetchOut>0 ) return SQLITE_OK;
51079 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
51084 if( nMap>pFd->mmapSizeMax ){
51085 nMap = pFd->mmapSizeMax;
51087 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
51089 if( nMap==0 && pFd->mmapSize>0 ){
51092 if( nMap!=pFd->mmapSize ){
51099 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
51105 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
51107 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
51111 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
51115 if( pFd->hMap==NULL ){
51116 pFd->lastErrno = osGetLastError();
51117 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
51118 "winMapfile1", pFd->zPath);
51120 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
51127 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
51129 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
51132 osCloseHandle(pFd->hMap);
51133 pFd->hMap = NULL;
51134 pFd->lastErrno = osGetLastError();
51135 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
51136 "winMapfile2", pFd->zPath);
51138 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
51142 pFd->pMapRegion = pNew;
51143 pFd->mmapSize = nMap;
51146 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
51174 if( pFd->mmapSizeMax>0 ){
51180 if( pFd->pMapRegion==0 ){
51181 int rc = winMapfile(pFd, -1);
51188 if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){
51189 assert( pFd->pMapRegion!=0 );
51190 *pp = &((u8 *)pFd->pMapRegion)[iOff];
51191 pFd->nFetchOut++;
51202 ** If the third argument is non-NULL, then this function releases a
51218 assert( (p==0)==(pFd->nFetchOut==0) );
51221 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
51227 pFd->nFetchOut--;
51231 ** is unnecessary can be omitted - potentially improving
51236 assert( pFd->nFetchOut>=0 );
51324 ** supports for filenames into UTF-8. Space to hold the result is
51343 ** Convert a UTF-8 filename into whatever form the underlying
51363 ** This function returns non-zero if the specified UTF-8 string buffer
51371 if( winIsDirSep(zBuf[nLen-1]) ){
51411 /* It's odd to simulate an io-error here, but really this is just
51412 ** using the io-error infrastructure to test that SQLite handles this
51420 nMax = pVfs->mxPathname; nBuf = nMax + 2;
51423 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51431 nDir = nMax - (nPre + 15);
51436 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
51442 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
51484 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51497 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51505 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
51512 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
51518 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51536 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51542 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
51554 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51564 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51569 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
51579 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51593 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
51611 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
51615 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
51623 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
51629 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
51675 const char *zName, /* Name of the file (UTF-8) */
51692 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
51734 /* The main DB, main journal, WAL file and super-journal are never
51741 /* Assert that the upper layer has set one of the "file-type" flags. */
51750 pFile->h = INVALID_HANDLE_VALUE;
51772 /* Database filenames are double-zero terminated if they are not
51918 pFile->lastErrno = lastErrno;
51919 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
51936 pAppData = (winVfsAppData*)pVfs->pAppData;
51941 && ((pAppData==NULL) || !pAppData->bNoLock)
51947 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
51952 pFile->zDeleteOnClose = zConverted;
51960 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
51961 pFile->pVfs = pVfs;
51962 pFile->h = h;
51964 pFile->ctrlFlags |= WINFILE_RDONLY;
51969 pFile->ctrlFlags |= WINFILE_PSOW;
51971 pFile->lastErrno = NO_ERROR;
51972 pFile->zPath = zName;
51974 pFile->hMap = NULL;
51975 pFile->pMapRegion = 0;
51976 pFile->mmapSize = 0;
51977 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
52140 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
52186 ** Returns non-zero if the specified path name starts with the "long path"
52197 ** Returns non-zero if the specified path name starts with a drive letter
52207 ** Returns non-zero if the specified path name should be used verbatim. If
52208 ** non-zero is returned from this function, the calling function must simply
52209 ** use the provided path name verbatim -OR- resolve it into a full path name
52245 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
52271 assert( nFull>=pVfs->mxPathname );
52279 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
52285 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
52295 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
52301 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
52307 zRelative, zOut, pVfs->mxPathname+1)<0 ){
52317 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
52336 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
52339 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
52345 /* It's odd to simulate an io-error here, but really this is just
52346 ** using the io-error infrastructure to test that SQLite handles this
52358 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
52419 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
52450 int nFull = pVfs->mxPathname+1;
52525 for(j=0, k=p->i; j<sz; j++){
52526 p->a[k++] ^= x[j];
52527 if( k>=p->na ) k = 0;
52529 p->i = k;
52530 p->nXor += sz;
52601 ** The following variable, if set to a non-zero value, is interpreted as
52620 /* FILETIME structure is a 64-bit value representing the number of
52621 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
52628 /* 2^32 - to avoid use of LL and warnings in gcc */
52678 ** buffer with a nul-terminated UTF-8 encoded error message
52684 ** is zero if the error message fits in the buffer, or non-zero
52685 ** otherwise (if the message was truncated). If non-zero is returned,
52686 ** then it is not necessary to include the nul-terminator character
52743 "win32-longpath", /* zName */
52768 "win32-none", /* zName */
52793 "win32-longpath-none", /* zName */
52814 /* Double-check that the aSyscall[] array has been constructed
52867 ** 2016-09-07
52878 ** This file implements an in-memory VFS. A database is held as a contiguous
52894 /* Access to a lower-level VFS that (might) implement dynamic loading,
52897 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
52957 ** File-scope variables for holding the memdb files that are accessible
53061 sqlite3_mutex_enter(p->pMutex);
53064 sqlite3_mutex_leave(p->pMutex);
53071 ** Close an memdb-file.
53076 MemStore *p = ((MemFile*)pFile)->pStore;
53077 if( p->zFName ){
53086 if( p->nRef==1 ){
53087 memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore];
53100 p->nRef--;
53101 if( p->nRef<=0 ){
53102 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){
53103 sqlite3_free(p->aData);
53106 sqlite3_mutex_free(p->pMutex);
53115 ** Read data from an memdb-file.
53123 MemStore *p = ((MemFile*)pFile)->pStore;
53125 if( iOfst+iAmt>p->sz ){
53127 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
53131 memcpy(zBuf, p->aData+iOfst, iAmt);
53141 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || NEVER(p->nMmap>0) ){
53144 if( newSz>p->szMax ){
53148 if( newSz>p->szMax ) newSz = p->szMax;
53149 pNew = sqlite3Realloc(p->aData, newSz);
53151 p->aData = pNew;
53152 p->szAlloc = newSz;
53157 ** Write data to an memdb-file.
53165 MemStore *p = ((MemFile*)pFile)->pStore;
53167 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
53173 if( iOfst+iAmt>p->sz ){
53175 if( iOfst+iAmt>p->szAlloc
53181 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
53182 p->sz = iOfst+iAmt;
53184 memcpy(p->aData+iOfst, z, iAmt);
53190 ** Truncate an memdb-file.
53197 MemStore *p = ((MemFile*)pFile)->pStore;
53200 if( size>p->sz ){
53204 p->sz = size;
53211 ** Sync an memdb-file.
53220 ** Return the current file-size of an memdb-file.
53223 MemStore *p = ((MemFile*)pFile)->pStore;
53225 *pSize = p->sz;
53231 ** Lock an memdb-file.
53235 MemStore *p = pThis->pStore;
53237 if( eLock<=pThis->eLock ) return SQLITE_OK;
53240 assert( p->nWrLock==0 || p->nWrLock==1 );
53241 assert( pThis->eLock<=SQLITE_LOCK_SHARED || p->nWrLock==1 );
53242 assert( pThis->eLock==SQLITE_LOCK_NONE || p->nRdLock>=1 );
53244 if( eLock>SQLITE_LOCK_SHARED && (p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
53249 assert( pThis->eLock==SQLITE_LOCK_NONE );
53250 if( p->nWrLock>0 ){
53253 p->nRdLock++;
53260 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
53261 if( ALWAYS(pThis->eLock==SQLITE_LOCK_SHARED) ){
53262 if( p->nWrLock>0 ){
53265 p->nWrLock = 1;
53273 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
53274 if( p->nRdLock>1 ){
53276 }else if( pThis->eLock==SQLITE_LOCK_SHARED ){
53277 p->nWrLock = 1;
53283 if( rc==SQLITE_OK ) pThis->eLock = eLock;
53289 ** Unlock an memdb-file.
53293 MemStore *p = pThis->pStore;
53294 if( eLock>=pThis->eLock ) return SQLITE_OK;
53299 if( ALWAYS(pThis->eLock>SQLITE_LOCK_SHARED) ){
53300 p->nWrLock--;
53303 if( pThis->eLock>SQLITE_LOCK_SHARED ){
53304 p->nWrLock--;
53306 p->nRdLock--;
53309 pThis->eLock = eLock;
53317 ** occur on an in-memory database.
53327 ** File control method. For custom operations on an memdb-file.
53330 MemStore *p = ((MemFile*)pFile)->pStore;
53334 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
53339 if( iLimit<p->sz ){
53341 iLimit = p->szMax;
53343 iLimit = p->sz;
53346 p->szMax = iLimit;
53356 ** Return the sector-size in bytes for an memdb-file.
53364 ** Return the device characteristic flags supported by an memdb-file.
53374 /* Fetch a page of a memory-mapped file */
53381 MemStore *p = ((MemFile*)pFile)->pStore;
53383 if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)!=0 ){
53386 p->nMmap++;
53387 *pp = (void*)(p->aData + iOfst);
53393 /* Release a memory-mapped page */
53395 MemStore *p = ((MemFile*)pFile)->pStore;
53399 p->nMmap--;
53428 if( strcmp(memdb_g.apMemStore[i]->zFName,zName)==0 ){
53450 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE|SQLITE_DESERIALIZE_FREEONCLOSE;
53451 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
53452 p->zFName = (char*)&p[1];
53453 memcpy(p->zFName, zName, szName+1);
53454 p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
53455 if( p->pMutex==0 ){
53456 memdb_g.nMemStore--;
53461 p->nRef = 1;
53465 p->nRef++;
53474 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
53475 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
53477 pFile->pStore = p;
53481 pFd->pMethods = &memdb_io_methods;
53486 #if 0 /* Only used to delete rollback journals, super-journals, and WAL
53490 ** ensure the file-system modifications are synced to disk before
53537 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
53542 ** utf-8 string describing the most recent error encountered associated
53546 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
53553 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
53560 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
53568 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
53576 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
53584 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
53589 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
53592 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
53604 if( p->base.pMethods!=&memdb_io_methods ) return 0;
53605 pStore = p->pStore;
53607 if( pStore->zFName!=0 ) p = 0;
53638 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
53641 if( piSize ) *piSize = -1;
53644 MemStore *pStore = p->pStore;
53645 assert( pStore->pMutex==0 );
53646 if( piSize ) *piSize = pStore->sz;
53648 pOut = pStore->aData;
53650 pOut = sqlite3_malloc64( pStore->sz );
53651 if( pOut ) memcpy(pOut, pStore->aData, pStore->sz);
53655 pBt = db->aDb[iDb].pBt;
53659 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
53686 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
53726 sqlite3_mutex_enter(db->mutex);
53727 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
53738 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
53742 db->init.iDb = (u8)iDb;
53743 db->init.reopenMemdb = 1;
53745 db->init.reopenMemdb = 0;
53754 MemStore *pStore = p->pStore;
53755 pStore->aData = pData;
53757 pStore->sz = szDb;
53758 pStore->szAlloc = szBuf;
53759 pStore->szMax = szBuf;
53760 if( pStore->szMax<sqlite3GlobalConfig.mxMemdbSize ){
53761 pStore->szMax = sqlite3GlobalConfig.mxMemdbSize;
53763 pStore->mFlags = mFlags;
53772 sqlite3_mutex_leave(db->mutex);
53791 sz = pLower->szOsFile;
53816 ** This file implements an object that represents a fixed-length
53820 ** journalled during a transaction, or which pages have the "dont-write"
53849 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
53866 ** sub-dividing and re-hashing. */
53871 ** no fewer collisions than the no-op *1. */
53891 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
53900 u32 nSet; /* Number of bits that are set - only valid for aHash
53924 p->iSize = iSize;
53930 ** Check to see if the i-th bit is set. Return true or false.
53936 i--;
53937 if( i>=p->iSize ) return 0;
53938 while( p->iDivisor ){
53939 u32 bin = i/p->iDivisor;
53940 i = i%p->iDivisor;
53941 p = p->u.apSub[bin];
53946 if( p->iSize<=BITVEC_NBIT ){
53947 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
53950 while( p->u.aHash[h] ){
53951 if( p->u.aHash[h]==i ) return 1;
53962 ** Set the i-th bit. Return 0 on success and an error code if
53965 ** This routine might cause sub-bitmaps to be allocated. Failing
53966 ** to get the memory needed to hold the sub-bitmap is the only
53977 assert( i<=p->iSize );
53978 i--;
53979 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
53980 u32 bin = i/p->iDivisor;
53981 i = i%p->iDivisor;
53982 if( p->u.apSub[bin]==0 ){
53983 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
53984 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
53986 p = p->u.apSub[bin];
53988 if( p->iSize<=BITVEC_NBIT ){
53989 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
53995 /* worrying about sub-dividing and re-hashing. */
53996 if( !p->u.aHash[h] ){
53997 if (p->nSet<(BITVEC_NINT-1)) {
54006 if( p->u.aHash[h]==i ) return SQLITE_OK;
54009 } while( p->u.aHash[h] );
54014 if( p->nSet>=BITVEC_MXHASH ){
54017 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
54021 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
54022 memset(p->u.apSub, 0, sizeof(p->u.apSub));
54023 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
54033 p->nSet++;
54034 p->u.aHash[h] = i;
54039 ** Clear the i-th bit.
54047 i--;
54048 while( p->iDivisor ){
54049 u32 bin = i/p->iDivisor;
54050 i = i%p->iDivisor;
54051 p = p->u.apSub[bin];
54056 if( p->iSize<=BITVEC_NBIT ){
54057 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
54061 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
54062 memset(p->u.aHash, 0, sizeof(p->u.aHash));
54063 p->nSet = 0;
54066 u32 h = BITVEC_HASH(aiValues[j]-1);
54067 p->nSet++;
54068 while( p->u.aHash[h] ){
54072 p->u.aHash[h] = aiValues[j];
54083 if( p->iDivisor ){
54086 sqlite3BitvecDestroy(p->u.apSub[i]);
54097 return p->iSize;
54139 ** If a memory allocation error occurs, return -1.
54144 int rc = -1;
54167 i = aOp[pc+2] - 1;
54179 if( (--aOp[pc+1]) > 0 ) nx = 0;
54195 ** match (rc==0). Change rc to non-zero if a discrepancy
54200 + (sqlite3BitvecSize(pBitvec) - sz);
54246 ** such that p was added to the list more recently than p->pDirtyNext.
54251 ** page to eject from the cache mid-transaction. It is better to eject
54294 pPg = (PgHdr*)pLower->pExtra;
54295 printf("%3d: nRef %2lld flgs %02x data ", i, pPg->nRef, pPg->flags);
54296 a = (unsigned char *)pLower->pBuf;
54307 if( pCache->pCache==0 ) return;
54311 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
54313 if( pLower && ((PgHdr*)pLower)->pPage==0 ){
54314 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
54331 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54338 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54361 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
54362 pCache = pPg->pCache;
54364 if( pPg->flags & PGHDR_CLEAN ){
54365 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
54368 assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */
54369 assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg );
54370 assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg );
54371 assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg );
54375 if( pPg->flags & PGHDR_WRITEABLE ){
54376 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
54414 PCache *p = pPage->pCache;
54418 pPage->pgno));
54420 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
54421 assert( pPage->pDirtyPrev || pPage==p->pDirty );
54424 if( p->pSynced==pPage ){
54425 p->pSynced = pPage->pDirtyPrev;
54428 if( pPage->pDirtyNext ){
54429 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
54431 assert( pPage==p->pDirtyTail );
54432 p->pDirtyTail = pPage->pDirtyPrev;
54434 if( pPage->pDirtyPrev ){
54435 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
54441 assert( pPage==p->pDirty );
54442 p->pDirty = pPage->pDirtyNext;
54443 assert( p->bPurgeable || p->eCreate==2 );
54444 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
54445 assert( p->bPurgeable==0 || p->eCreate==1 );
54446 p->eCreate = 2;
54451 pPage->pDirtyPrev = 0;
54452 pPage->pDirtyNext = p->pDirty;
54453 if( pPage->pDirtyNext ){
54454 assert( pPage->pDirtyNext->pDirtyPrev==0 );
54455 pPage->pDirtyNext->pDirtyPrev = pPage;
54457 p->pDirtyTail = pPage;
54458 if( p->bPurgeable ){
54459 assert( p->eCreate==2 );
54460 p->eCreate = 1;
54463 p->pDirty = pPage;
54469 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
54470 if( !p->pSynced
54471 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
54473 p->pSynced = pPage;
54481 ** being used for an in-memory database, this function is a no-op.
54484 if( p->pCache->bPurgeable ){
54485 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
54486 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
54487 pcacheDump(p->pCache);
54492 ** Compute the number of pages of cache requested. p->szCache is the
54496 if( p->szCache>=0 ){
54497 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
54499 return p->szCache;
54502 /* IMPLEMENTATION-OF: R-59858-46238 If the argument N is negative, then the
54506 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
54519 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
54520 ** built-in default page cache is used instead of the application defined
54529 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
54560 p->szPage = 1;
54561 p->szExtra = szExtra;
54563 p->bPurgeable = bPurgeable;
54564 p->eCreate = 2;
54565 p->xStress = xStress;
54566 p->pStress = pStress;
54567 p->szCache = 100;
54568 p->szSpill = 1;
54578 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
54579 if( pCache->szPage ){
54582 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
54583 pCache->bPurgeable
54587 if( pCache->pCache ){
54588 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
54590 pCache->pCache = pNew;
54591 pCache->szPage = szPage;
54630 assert( pCache->pCache!=0 );
54632 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
54641 eCreate = createFlag & pCache->eCreate;
54643 assert( createFlag==0 || pCache->eCreate==eCreate );
54644 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
54645 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
54669 if( pCache->eCreate==2 ) return 0;
54671 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
54672 /* Find a dirty page to write-out and recycle. First try to find a
54673 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
54681 for(pPg=pCache->pSynced;
54682 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
54683 pPg=pPg->pDirtyPrev
54685 pCache->pSynced = pPg;
54687 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
54693 "spill page %d making room for %d - cache used: %d/%d",
54694 pPg->pgno, pgno,
54695 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
54698 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
54699 rc = pCache->xStress(pCache->pStress, pPg);
54706 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
54726 pPgHdr = (PgHdr*)pPage->pExtra;
54727 assert( pPgHdr->pPage==0 );
54728 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
54729 pPgHdr->pPage = pPage;
54730 pPgHdr->pData = pPage->pBuf;
54731 pPgHdr->pExtra = (void *)&pPgHdr[1];
54732 memset(pPgHdr->pExtra, 0, 8);
54733 pPgHdr->pCache = pCache;
54734 pPgHdr->pgno = pgno;
54735 pPgHdr->flags = PGHDR_CLEAN;
54753 pPgHdr = (PgHdr *)pPage->pExtra;
54755 if( !pPgHdr->pPage ){
54758 pCache->nRefSum++;
54759 pPgHdr->nRef++;
54769 assert( p->nRef>0 );
54770 p->pCache->nRefSum--;
54771 if( (--p->nRef)==0 ){
54772 if( p->flags&PGHDR_CLEAN ){
54785 assert(p->nRef>0);
54787 p->nRef++;
54788 p->pCache->nRefSum++;
54797 assert( p->nRef==1 );
54799 if( p->flags&PGHDR_DIRTY ){
54802 p->pCache->nRefSum--;
54803 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
54811 assert( p->nRef>0 );
54813 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
54814 p->flags &= ~PGHDR_DONT_WRITE;
54815 if( p->flags & PGHDR_CLEAN ){
54816 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
54817 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
54818 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
54832 assert( (p->flags & PGHDR_DIRTY)!=0 );
54833 assert( (p->flags & PGHDR_CLEAN)==0 );
54835 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
54836 p->flags |= PGHDR_CLEAN;
54837 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
54839 if( p->nRef==0 ){
54849 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
54850 while( (p = pCache->pDirty)!=0 ){
54860 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
54861 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54862 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
54864 pCache->pSynced = pCache->pDirtyTail;
54872 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54873 p->flags &= ~PGHDR_NEED_SYNC;
54875 pCache->pSynced = pCache->pDirtyTail;
54882 PCache *pCache = p->pCache;
54884 assert( p->nRef>0 );
54887 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
54888 pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0);
54890 PgHdr *pXPage = (PgHdr*)pOther->pExtra;
54891 assert( pXPage->nRef==0 );
54892 pXPage->nRef++;
54893 pCache->nRefSum++;
54896 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
54897 p->pgno = newPgno;
54898 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
54914 if( pCache->pCache ){
54918 for(p=pCache->pDirty; p; p=pNext){
54919 pNext = p->pDirtyNext;
54924 assert( p->pgno>0 );
54925 if( p->pgno>pgno ){
54926 assert( p->flags&PGHDR_DIRTY );
54930 if( pgno==0 && pCache->nRefSum ){
54932 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
54934 ** pCache->nRefSum>0 */
54935 memset(pPage1->pBuf, 0, pCache->szPage);
54939 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
54947 assert( pCache->pCache!=0 );
54949 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
54968 if( pA->pgno<pB->pgno ){
54969 pTail->pDirty = pA;
54971 pA = pA->pDirty;
54973 pTail->pDirty = pB;
54977 pTail->pDirty = pB;
54979 pB = pB->pDirty;
54981 pTail->pDirty = pA;
55006 pIn = p->pDirty;
55007 p->pDirty = 0;
55008 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
55017 if( NEVER(i==N_SORT_BUCKET-1) ){
55037 for(p=pCache->pDirty; p; p=p->pDirtyNext){
55038 p->pDirty = p->pDirtyNext;
55040 return pcacheSortDirtyList(pCache->pDirty);
55050 return pCache->nRefSum;
55057 return p->nRef;
55064 assert( pCache->pCache!=0 );
55065 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
55070 ** Get the suggested cache-size value.
55078 ** Set the suggested cache-size value.
55081 assert( pCache->pCache!=0 );
55082 pCache->szCache = mxPage;
55083 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
55088 ** Set the suggested cache-spill value. Make no changes if if the
55089 ** argument is zero. Return the effective cache-spill size, which will
55094 assert( p->pCache!=0 );
55097 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
55099 p->szSpill = mxPage;
55102 if( res<p->szSpill ) res = p->szSpill;
55110 assert( pCache->pCache!=0 );
55111 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
55116 ** in the page-cache hierarchy.
55128 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
55137 return (pCache->pDirty!=0);
55149 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
55177 ** -------------------------------------------------------------
55179 ** -------------------------------------------------------------
55195 ** size can vary according to architecture, compile-time options, and
55201 ** allocations. However, the btree layer needs a small (16-byte) overrun
55216 ** (1) The general-purpose memory allocator - sqlite3Malloc()
55217 ** (2) Global page-cache memory provided using sqlite3_config() with
55219 ** (3) PCache-local bulk allocation.
55229 ** Or if N is negative, then -1024*N bytes of memory are allocated and used
55233 ** (3) is exhausted, subsequent allocations fail over to the general-purpose
55249 ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
55259 ** but causes a 2-byte gap in the structure for most architectures (since
55260 ** pointers must be either 4 or 8-byte aligned). As this structure is located
55262 ** corrupt, code at the b-tree layer may overread the page buffer and
55268 ** The pLruNext and pLruPrev pointers form a double-linked circular list
55289 #define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
55290 #define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
55318 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
55324 ** open database file (including each in-memory database and each
55339 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
55357 PgHdr1 *pFree; /* List of unused pcache-local pages */
55358 void *pBulk; /* Bulk memory used by pcache-local */
55392 ** reading because (1) most platforms read a 32-bit integer atomically and
55409 # define pcache1EnterMutex(X) assert((X)->mutex==0)
55410 # define pcache1LeaveMutex(X) assert((X)->mutex==0)
55413 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
55414 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
55424 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
55443 while( n-- ){
55445 p->pNext = pcache1.pFree;
55454 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
55455 ** true if pCache->pFree ends up containing one or more free pages.
55462 if( pCache->nMax<3 ) return 0;
55465 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
55467 szBulk = -1024 * (i64)pcache1.nInitPage;
55469 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
55470 szBulk = pCache->szAlloc*(i64)pCache->nMax;
55472 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
55475 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
55477 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
55478 pX->page.pBuf = zBulk;
55479 pX->page.pExtra = &pX[1];
55480 pX->isBulkLocal = 1;
55481 pX->isAnchor = 0;
55482 pX->pNext = pCache->pFree;
55483 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
55484 pCache->pFree = pX;
55485 zBulk += pCache->szAlloc;
55486 }while( --nBulk );
55488 return pCache->pFree!=0;
55507 pcache1.pFree = pcache1.pFree->pNext;
55508 pcache1.nFreeSlot--;
55545 pSlot->pNext = pcache1.pFree;
55592 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
55593 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
55594 assert( pCache->pFree!=0 );
55595 p = pCache->pFree;
55596 pCache->pFree = p->pNext;
55597 p->pNext = 0;
55604 assert( pCache->pGroup==&pcache1.grp );
55605 pcache1LeaveMutex(pCache->pGroup);
55608 pPg = pcache1Alloc(pCache->szAlloc);
55611 pcache1EnterMutex(pCache->pGroup);
55614 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
55615 p->page.pBuf = pPg;
55616 p->page.pExtra = &p[1];
55617 p->isBulkLocal = 0;
55618 p->isAnchor = 0;
55619 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
55621 (*pCache->pnPurgeable)++;
55631 pCache = p->pCache;
55632 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
55633 if( p->isBulkLocal ){
55634 p->pNext = pCache->pFree;
55635 pCache->pFree = p;
55637 pcache1Free(p->page.pBuf);
55639 (*pCache->pnPurgeable)--;
55677 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
55698 assert( sqlite3_mutex_held(p->pGroup->mutex) );
55700 nNew = p->nHash*2;
55705 pcache1LeaveMutex(p->pGroup);
55706 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
55708 if( p->nHash ){ sqlite3EndBenignMalloc(); }
55709 pcache1EnterMutex(p->pGroup);
55711 for(i=0; i<p->nHash; i++){
55713 PgHdr1 *pNext = p->apHash[i];
55715 unsigned int h = pPage->iKey % nNew;
55716 pNext = pPage->pNext;
55717 pPage->pNext = apNew[h];
55721 sqlite3_free(p->apHash);
55722 p->apHash = apNew;
55723 p->nHash = nNew;
55730 ** LRU list, then this function is a no-op.
55737 assert( pPage->pLruNext );
55738 assert( pPage->pLruPrev );
55739 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
55740 pPage->pLruPrev->pLruNext = pPage->pLruNext;
55741 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
55742 pPage->pLruNext = 0;
55743 /* pPage->pLruPrev = 0;
55745 assert( pPage->isAnchor==0 );
55746 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
55747 pPage->pCache->nRecyclable--;
55761 PCache1 *pCache = pPage->pCache;
55764 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
55765 h = pPage->iKey % pCache->nHash;
55766 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
55767 *pp = (*pp)->pNext;
55769 pCache->nPage--;
55778 PGroup *pGroup = pCache->pGroup;
55780 assert( sqlite3_mutex_held(pGroup->mutex) );
55781 while( pGroup->nPurgeable>pGroup->nMaxPage
55782 && (p=pGroup->lru.pLruPrev)->isAnchor==0
55784 assert( p->pCache->pGroup==pGroup );
55789 if( pCache->nPage==0 && pCache->pBulk ){
55790 sqlite3_free(pCache->pBulk);
55791 pCache->pBulk = pCache->pFree = 0;
55806 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
55808 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
55809 assert( pCache->iMaxKey >= iLimit );
55810 assert( pCache->nHash > 0 );
55811 if( pCache->iMaxKey - iLimit < pCache->nHash ){
55816 h = iLimit % pCache->nHash;
55817 iStop = pCache->iMaxKey % pCache->nHash;
55818 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
55822 h = pCache->nHash/2;
55823 iStop = h - 1;
55828 assert( h<pCache->nHash );
55829 pp = &pCache->apHash[h];
55831 if( pPage->iKey>=iLimit ){
55832 pCache->nPage--;
55833 *pp = pPage->pNext;
55837 pp = &pPage->pNext;
55842 h = (h+1) % pCache->nHash;
55844 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
55861 ** private PGroup (mode-1). pcache1.separateCache is false if the single
55862 ** PGroup in pcache1.grp is used for all page caches (mode-2).
55864 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
55866 ** * Use a unified cache in single-threaded applications that have
55867 ** configured a start-time buffer for use as page-cache memory using
55868 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
55871 ** * Otherwise use separate caches (mode-1)
55925 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
55933 pGroup->mxPinned = 10;
55938 if( pGroup->lru.isAnchor==0 ){
55939 pGroup->lru.isAnchor = 1;
55940 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
55942 pCache->pGroup = pGroup;
55943 pCache->szPage = szPage;
55944 pCache->szExtra = szExtra;
55945 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
55946 pCache->bPurgeable = (bPurgeable ? 1 : 0);
55949 pCache->nMin = 10;
55950 pGroup->nMinPage += pCache->nMin;
55951 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
55952 pCache->pnPurgeable = &pGroup->nPurgeable;
55954 pCache->pnPurgeable = &pCache->nPurgeableDummy;
55957 if( pCache->nHash==0 ){
55974 if( pCache->bPurgeable ){
55975 PGroup *pGroup = pCache->pGroup;
55978 if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
55979 n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
55981 pGroup->nMaxPage += (n - pCache->nMax);
55982 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
55983 pCache->nMax = n;
55984 pCache->n90pct = pCache->nMax*9/10;
55997 if( pCache->bPurgeable ){
55998 PGroup *pGroup = pCache->pGroup;
56001 savedMaxPage = pGroup->nMaxPage;
56002 pGroup->nMaxPage = 0;
56004 pGroup->nMaxPage = savedMaxPage;
56015 pcache1EnterMutex(pCache->pGroup);
56016 n = pCache->nPage;
56017 pcache1LeaveMutex(pCache->pGroup);
56036 PGroup *pGroup = pCache->pGroup;
56040 assert( pCache->nPage >= pCache->nRecyclable );
56041 nPinned = pCache->nPage - pCache->nRecyclable;
56042 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
56043 assert( pCache->n90pct == pCache->nMax*9/10 );
56045 nPinned>=pGroup->mxPinned
56046 || nPinned>=pCache->n90pct
56047 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
56052 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
56053 assert( pCache->nHash>0 && pCache->apHash );
56056 if( pCache->bPurgeable
56057 && !pGroup->lru.pLruPrev->isAnchor
56058 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
56061 pPage = pGroup->lru.pLruPrev;
56065 pOther = pPage->pCache;
56066 if( pOther->szAlloc != pCache->szAlloc ){
56070 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
56082 unsigned int h = iKey % pCache->nHash;
56083 pCache->nPage++;
56084 pPage->iKey = iKey;
56085 pPage->pNext = pCache->apHash[h];
56086 pPage->pCache = pCache;
56087 pPage->pLruNext = 0;
56088 /* pPage->pLruPrev = 0;
56090 *(void **)pPage->page.pExtra = 0;
56091 pCache->apHash[h] = pPage;
56092 if( iKey>pCache->iMaxKey ){
56093 pCache->iMaxKey = iKey;
56109 ** For a non-purgeable cache (a cache used as the storage for an in-memory
56112 ** a non-purgeable cache.
56155 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
56167 pPage = pCache->apHash[iKey % pCache->nHash];
56168 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
56196 pcache1EnterMutex(pCache->pGroup);
56198 assert( pPage==0 || pCache->iMaxKey>=iKey );
56199 pcache1LeaveMutex(pCache->pGroup);
56213 assert( pCache->bPurgeable || createFlag!=1 );
56214 assert( pCache->bPurgeable || pCache->nMin==0 );
56215 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
56216 assert( pCache->nMin==0 || pCache->bPurgeable );
56217 assert( pCache->nHash>0 );
56219 if( pCache->pGroup->mutex ){
56241 PGroup *pGroup = pCache->pGroup;
56243 assert( pPage->pCache==pCache );
56249 assert( pPage->pLruNext==0 );
56252 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
56256 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
56257 pPage->pLruPrev = &pGroup->lru;
56258 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
56260 pCache->nRecyclable++;
56263 pcache1LeaveMutex(pCache->pGroup);
56279 assert( pPage->iKey==iOld );
56280 assert( pPage->pCache==pCache );
56283 pcache1EnterMutex(pCache->pGroup);
56286 hOld = iOld%pCache->nHash;
56287 pp = &pCache->apHash[hOld];
56289 pp = &(*pp)->pNext;
56291 *pp = pPage->pNext;
56294 hNew = iNew%pCache->nHash;
56295 pPage->iKey = iNew;
56296 pPage->pNext = pCache->apHash[hNew];
56297 pCache->apHash[hNew] = pPage;
56298 if( iNew>pCache->iMaxKey ){
56299 pCache->iMaxKey = iNew;
56302 pcache1LeaveMutex(pCache->pGroup);
56314 pcache1EnterMutex(pCache->pGroup);
56315 if( iLimit<=pCache->iMaxKey ){
56317 pCache->iMaxKey = iLimit-1;
56319 pcache1LeaveMutex(pCache->pGroup);
56329 PGroup *pGroup = pCache->pGroup;
56330 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
56332 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
56333 assert( pGroup->nMaxPage >= pCache->nMax );
56334 pGroup->nMaxPage -= pCache->nMax;
56335 assert( pGroup->nMinPage >= pCache->nMin );
56336 pGroup->nMinPage -= pCache->nMin;
56337 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
56340 sqlite3_free(pCache->pBulk);
56341 sqlite3_free(pCache->apHash);
56401 && p->isAnchor==0
56403 nFree += pcache1MemSize(p->page.pBuf);
56427 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
56487 ** a non-zero batch number, it will see all prior INSERTs.
56515 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
56573 p->pChunk = 0;
56574 p->db = db;
56575 p->pEntry = 0;
56576 p->pLast = 0;
56577 p->pForest = 0;
56578 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
56579 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
56580 p->rsFlags = ROWSET_SORTED;
56581 p->iBatch = 0;
56594 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
56595 pNextChunk = pChunk->pNextChunk;
56596 sqlite3DbFree(p->db, pChunk);
56598 p->pChunk = 0;
56599 p->nFresh = 0;
56600 p->pEntry = 0;
56601 p->pLast = 0;
56602 p->pForest = 0;
56603 p->rsFlags = ROWSET_SORTED;
56613 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
56621 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
56626 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
56630 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
56634 pNew->pNextChunk = p->pChunk;
56635 p->pChunk = pNew;
56636 p->pFresh = pNew->aEntry;
56637 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
56639 p->nFresh--;
56640 return p->pFresh++;
56654 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
56658 pEntry->v = rowid;
56659 pEntry->pRight = 0;
56660 pLast = p->pLast;
56662 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
56665 p->rsFlags &= ~ROWSET_SORTED;
56667 pLast->pRight = pEntry;
56669 p->pEntry = pEntry;
56671 p->pLast = pEntry;
56690 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
56691 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
56692 if( pA->v<=pB->v ){
56693 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
56694 pA = pA->pRight;
56696 pTail->pRight = pB;
56700 pTail = pTail->pRight = pB;
56701 pB = pB->pRight;
56703 pTail->pRight = pA;
56721 pNext = pIn->pRight;
56722 pIn->pRight = 0;
56750 if( pIn->pLeft ){
56752 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
56753 p->pRight = pIn;
56757 if( pIn->pRight ){
56758 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
56762 assert( (*ppLast)->pRight==0 );
56785 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
56789 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
56793 pLeft = rowSetNDeepTree(ppList, iDepth-1);
56795 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
56800 p->pLeft = pLeft;
56801 *ppList = p->pRight;
56802 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
56805 *ppList = p->pRight;
56806 p->pLeft = p->pRight = 0;
56822 pList = p->pRight;
56823 p->pLeft = p->pRight = 0;
56827 pList = p->pRight;
56828 p->pLeft = pLeft;
56829 p->pRight = rowSetNDeepTree(&pList, iDepth);
56849 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
56852 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
56853 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
56854 p->pEntry = rowSetEntrySort(p->pEntry);
56856 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
56860 if( p->pEntry ){
56861 *pRowid = p->pEntry->v;
56862 p->pEntry = p->pEntry->pRight;
56863 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
56878 ** on pRowSet->pEntry, then sort those entries into the forest at
56879 ** pRowSet->pForest so that they can be tested.
56885 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
56890 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
56891 p = pRowSet->pEntry;
56893 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
56894 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
56898 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
56899 ppPrevTree = &pTree->pRight;
56900 if( pTree->pLeft==0 ){
56901 pTree->pLeft = rowSetListToTree(p);
56905 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
56906 pTree->pLeft = 0;
56913 pTree->v = 0;
56914 pTree->pRight = 0;
56915 pTree->pLeft = rowSetListToTree(p);
56918 pRowSet->pEntry = 0;
56919 pRowSet->pLast = 0;
56920 pRowSet->rsFlags |= ROWSET_SORTED;
56922 pRowSet->iBatch = iBatch;
56928 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
56929 p = pTree->pLeft;
56931 if( p->v<iRowid ){
56932 p = p->pRight;
56933 }else if( p->v>iRowid ){
56934 p = p->pLeft;
56980 ** This header file defines the interface to the write-ahead logging
57021 /* Connection to a write-ahead log (WAL) file.
57026 /* Open and close a connection to a write-ahead log. */
57034 ** snapshot is like a read-transaction. It is the state of the database
57043 /* Read a page from the write-ahead log, if it is present. */
57070 Wal *pWal, /* Write-ahead log connection */
57094 /* Return true if the argument is non-NULL and the WAL module is using
57095 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
57096 ** WAL module is using shared-memory, return false.
57110 ** stored in each frame (i.e. the db page-size when the WAL was created).
57181 ** (6) If a super-journal file is used, then all writes to the database file
57182 ** are synced prior to the super-journal being deleted.
57198 ** method is a no-op, but that does not change the fact the SQLite will
57209 ** (11) A database file is well-formed at the beginning and at the conclusion
57233 ** to print out file-descriptors.
57236 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
57239 #define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
57247 ** OPEN <------+------+
57250 ** +---------> READER-------+ |
57253 ** |<-------WRITER_LOCKED------> ERROR
57256 ** |<------WRITER_CACHEMOD-------->|
57259 ** |<-------WRITER_DBMOD---------->|
57262 ** +<------WRITER_FINISHED-------->+
57267 ** OPEN -> READER [sqlite3PagerSharedLock]
57268 ** READER -> OPEN [pager_unlock]
57270 ** READER -> WRITER_LOCKED [sqlite3PagerBegin]
57271 ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
57272 ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
57273 ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
57274 ** WRITER_*** -> READER [pager_end_transaction]
57276 ** WRITER_*** -> ERROR [pager_error]
57277 ** ERROR -> OPEN [pager_unlock]
57283 ** state - the file may or may not be locked and the database size is
57293 ** rollback (non-WAL) mode are met. Unless the pager is (or recently
57294 ** was) in exclusive-locking mode, a user-level read transaction is
57298 ** it opens a read-transaction on the database and returns to state
57299 ** OPEN after the read-transaction is completed. However a connection
57301 ** this state even after the read-transaction is closed. The only way
57305 ** * A read transaction may be active (but a write-transaction cannot).
57307 ** * The dbSize variable may be trusted (even if a user-level read
57311 ** * Even if a read-transaction is not open, it is guaranteed that
57312 ** there is no hot-journal in the file-system.
57316 ** The pager moves to this state from READER when a write-transaction
57318 ** required to start a write-transaction are held, but no actual
57333 ** * If the connection is open in rollback-mode, a RESERVED or greater
57335 ** * If the connection is open in WAL-mode, a WAL write transaction
57374 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
57390 ** The ERROR state is entered when an IO or disk-full error (including
57392 ** difficult to be sure that the in-memory pager state (cache contents,
57393 ** db size etc.) are consistent with the contents of the file-system.
57395 ** Temporary pager files may enter the ERROR state, but in-memory pagers
57399 ** the contents of the page-cache may be left in an inconsistent state.
57411 ** page-cache and any other in-memory state at the same time. Everything
57412 ** is reloaded from disk (and, if necessary, hot-journal rollback performed)
57413 ** when a read-transaction is next opened on the pager (transitioning
57429 ** In other cases, the error is returned to the b-tree layer. The b-tree
57433 ** Condition (3) is necessary because it can be triggered by a read-only
57435 ** code were simply returned to the user, the b-tree layer would not
57437 ** read-only statement cannot leave the pager in an internally inconsistent
57443 ** * The pager is not an in-memory pager.
57453 ** state. There are two exceptions: immediately after exclusive-mode has
57469 ** following locking-states, according to the lock currently held on
57477 ** pagerUnlockDb() take a conservative approach - eLock is always updated
57488 ** from ERROR to OPEN state. At this point there may be a hot-journal file
57489 ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
57493 ** of hot-journal detection.
57499 ** a hot-journal may be mistaken for a journal being created by an active
57506 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
57507 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
57508 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
57533 ** set to 0. If a journal-header is written into the main journal while
57536 ** journal before the journal-header. This is required during savepoint
57545 Pgno iSubRec; /* Index of first record in sub-journal */
57570 ** For a real on-disk database, the current lock held on the database file -
57573 ** For a temporary or in-memory database (neither of which require any
57585 ** This boolean variable is used to make sure that the change-counter
57586 ** (the 4-byte header field at byte offset 24 of the database file) is
57589 ** It is set to true when the change-counter field is updated, which
57594 ** updating the change-counter is omitted for the current transaction.
57597 ** need only update the change-counter once, for the first transaction
57603 ** (or may not) specify a super-journal name to be written into the
57606 ** Whether or not a journal file contains a super-journal pointer affects
57609 ** If a journal file does not contain a super-journal pointer, it is
57611 ** it does contain a super-journal pointer the journal file is finalized
57615 ** Journal files that contain super-journal pointers cannot be finalized
57616 ** simply by overwriting the first journal-header with zeroes, as the
57617 ** super-journal pointer could interfere with hot-journal rollback of any
57627 ** This variables control the behavior of cache-spills (calls made by
57629 ** to the file-system in order to free up memory).
57641 ** This flag is set by sqlite3PagerWrite() when the file-system sector-size
57642 ** is larger than the database page-size in order to prevent a journal sync
57647 ** This is a boolean variable. If true, then any required sub-journal
57648 ** is opened as an in-memory journal file. If false, then in-memory
57649 ** sub-journals are only used for in-memory pager files.
57652 ** write-transaction is opened.
57663 ** is not an integer multiple of the page-size, the value stored in
57664 ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
57666 ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
57669 ** During a write-transaction, if pages with page-numbers greater than
57680 ** Throughout a write-transaction, dbFileSize contains the size of
57682 ** write-transaction is first opened, and updated when VFS calls are made
57702 ** write-transaction is opened (at the same time as dbFileSize and
57705 ** size-hint passed to the method call. See pager_write_pagelist() for
57713 ** sub-codes.
57739 u8 readOnly; /* True for a read-only database */
57741 u8 memVfs; /* VFS-implemented memory database */
57754 u8 changeCountDone; /* Set after incrementing the change-counter */
57755 u8 setSuper; /* Super-jrnl name is written into jrnl */
57756 u8 doNotSpill; /* Do not spill the cache when non-zero */
57757 u8 subjInMemory; /* True to use in-memory sub-journals */
57765 int nRec; /* Pages journalled since last j-header written */
57766 u32 cksumInit; /* Quasi-random value added to every checksum */
57767 u32 nSubRec; /* Number of records written to sub-journal */
57771 sqlite3_file *sjfd; /* File descriptor for sub-journal */
57784 ** End of the routinely-changing class members
57787 u16 nExtra; /* Add this many bytes to each in-memory page */
57808 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
57809 char *zWal; /* File name for write-ahead log */
57826 ** a non-testing build. These variables are not thread-safe.
57845 ** written, semi-random garbage data might appear in the journal
57852 ** of a 32-bit checksum on each page of data. The checksum covers both
57853 ** the page number and the pPager->pageSize bytes of data for the page.
57854 ** This cksum is initialized to a 32-bit random value that appears in the
57870 #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
57876 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
57879 ** The macro MEMDB is true if we are dealing with an in-memory database.
57887 # define MEMDB pPager->memDb
57892 ** interfaces to access the database using memory-mapped I/O.
57895 # define USEFETCH(x) ((x)->bUseFetch)
57902 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
57906 ** if( isOpen(pPager->jfd) ){ ...
57910 ** if( pPager->jfd->pMethods ){ ...
57912 #define isOpen(pFd) ((pFd)->pMethods!=0)
57917 ** by the b-tree layer. This is the case if:
57924 if( pPager->fd->pMethods==0 ) return 0;
57925 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
57927 if( pPager->pWal ){
57929 (void)sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
57938 # define pagerUseWal(x) ((x)->pWal!=0)
57960 assert( p->eState==PAGER_OPEN
57961 || p->eState==PAGER_READER
57962 || p->eState==PAGER_WRITER_LOCKED
57963 || p->eState==PAGER_WRITER_CACHEMOD
57964 || p->eState==PAGER_WRITER_DBMOD
57965 || p->eState==PAGER_WRITER_FINISHED
57966 || p->eState==PAGER_ERROR
57969 /* Regardless of the current state, a temp-file connection always behaves
57971 ** the change-counter field, so the changeCountDone flag is always set.
57973 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
57974 assert( p->tempFile==0 || pPager->changeCountDone );
57976 /* If the useJournal flag is clear, the journal-mode must be "OFF".
57977 ** And if the journal-mode is "OFF", the journal file must not be open.
57979 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
57980 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
57982 /* Check that MEMDB implies noSync. And an in-memory journal. Since
57983 ** this means an in-memory pager performs no IO at all, it cannot encounter
57985 ** a journal file. (although the in-memory journal implementation may
57987 ** is therefore not possible for an in-memory pager to enter the ERROR
57991 assert( !isOpen(p->fd) );
57992 assert( p->noSync );
57993 assert( p->journalMode==PAGER_JOURNALMODE_OFF
57994 || p->journalMode==PAGER_JOURNALMODE_MEMORY
57996 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
58003 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
58004 assert( p->eLock!=PENDING_LOCK );
58006 switch( p->eState ){
58009 assert( pPager->errCode==SQLITE_OK );
58010 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
58014 assert( pPager->errCode==SQLITE_OK );
58015 assert( p->eLock!=UNKNOWN_LOCK );
58016 assert( p->eLock>=SHARED_LOCK );
58020 assert( p->eLock!=UNKNOWN_LOCK );
58021 assert( pPager->errCode==SQLITE_OK );
58023 assert( p->eLock>=RESERVED_LOCK );
58025 assert( pPager->dbSize==pPager->dbOrigSize );
58026 assert( pPager->dbOrigSize==pPager->dbFileSize );
58027 assert( pPager->dbOrigSize==pPager->dbHintSize );
58028 assert( pPager->setSuper==0 );
58032 assert( p->eLock!=UNKNOWN_LOCK );
58033 assert( pPager->errCode==SQLITE_OK );
58040 assert( p->eLock>=RESERVED_LOCK );
58041 assert( isOpen(p->jfd)
58042 || p->journalMode==PAGER_JOURNALMODE_OFF
58043 || p->journalMode==PAGER_JOURNALMODE_WAL
58046 assert( pPager->dbOrigSize==pPager->dbFileSize );
58047 assert( pPager->dbOrigSize==pPager->dbHintSize );
58051 assert( p->eLock==EXCLUSIVE_LOCK );
58052 assert( pPager->errCode==SQLITE_OK );
58054 assert( p->eLock>=EXCLUSIVE_LOCK );
58055 assert( isOpen(p->jfd)
58056 || p->journalMode==PAGER_JOURNALMODE_OFF
58057 || p->journalMode==PAGER_JOURNALMODE_WAL
58058 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
58060 assert( pPager->dbOrigSize<=pPager->dbHintSize );
58064 assert( p->eLock==EXCLUSIVE_LOCK );
58065 assert( pPager->errCode==SQLITE_OK );
58067 assert( isOpen(p->jfd)
58068 || p->journalMode==PAGER_JOURNALMODE_OFF
58069 || p->journalMode==PAGER_JOURNALMODE_WAL
58070 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
58079 assert( pPager->errCode!=SQLITE_OK );
58080 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
58113 , p->zFilename
58114 , p->eState==PAGER_OPEN ? "OPEN" :
58115 p->eState==PAGER_READER ? "READER" :
58116 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
58117 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
58118 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
58119 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
58120 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
58121 , (int)p->errCode
58122 , p->eLock==NO_LOCK ? "NO_LOCK" :
58123 p->eLock==RESERVED_LOCK ? "RESERVED" :
58124 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
58125 p->eLock==SHARED_LOCK ? "SHARED" :
58126 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
58127 , p->exclusiveMode ? "exclusive" : "normal"
58128 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
58129 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
58130 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
58131 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
58132 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
58133 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
58134 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
58135 , p->journalOff, p->journalHdr
58136 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
58155 if( pPager->errCode ){
58156 pPager->xGet = getPageError;
58159 pPager->xGet = getPageMMap;
58162 pPager->xGet = getPageNormal;
58167 ** Return true if it is necessary to write page *pPg into the sub-journal.
58168 ** A page needs to be written into the sub-journal if there exists one
58171 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
58172 ** * The bit corresponding to the page-number is not set in
58176 Pager *pPager = pPg->pPager;
58178 Pgno pgno = pPg->pgno;
58180 for(i=0; i<pPager->nSavepoint; i++){
58181 p = &pPager->aSavepoint[i];
58182 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
58183 for(i=i+1; i<pPager->nSavepoint; i++){
58184 pPager->aSavepoint[i].bTruncateOnRelease = 0;
58197 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
58202 ** Read a 32-bit integer from the given file descriptor. Store the integer
58206 ** All values are stored on disk as big-endian.
58218 ** Write a 32-bit integer into a string buffer in big-endian byte order.
58224 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
58245 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
58248 if( isOpen(pPager->fd) ){
58249 assert( pPager->eLock>=eLock );
58250 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
58251 if( pPager->eLock!=UNKNOWN_LOCK ){
58252 pPager->eLock = (u8)eLock;
58256 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
58274 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
58275 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
58276 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
58277 pPager->eLock = (u8)eLock;
58285 ** This function determines whether or not the atomic-write or
58286 ** atomic-batch-write optimizations can be used with this pager. The
58287 ** atomic-write optimization can be used if:
58297 ** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
58298 ** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
58310 assert( isOpen(pPager->fd) );
58311 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
58317 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
58318 return -1;
58324 int nSector = pPager->sectorSize;
58325 int szPage = pPager->pageSize;
58347 ** Return a 32-bit hash of the page data for pPage.
58358 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
58361 pPage->pageHash = pager_pagehash(pPage);
58367 ** that the page is either dirty or still matches the calculated page-hash.
58371 Pager *pPager = pPg->pPager;
58372 assert( pPager->eState!=PAGER_ERROR );
58373 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
58385 ** This function attempts to read a super-journal file name from the
58388 ** used to store a super-journal file name at the end of a journal file.
58392 ** enough space to write the super-journal name). If the super-journal
58394 ** nul-terminator), then this is handled as if no super-journal name
58397 ** If a super-journal file name is present at the end of the journal
58399 ** nul-terminator byte is appended to the buffer following the
58400 ** super-journal file name.
58402 ** If it is determined that no super-journal file name is present
58410 u32 len; /* Length in bytes of super-journal name */
58419 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
58421 || len>szJ-16
58423 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
58424 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
58426 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len))
58431 /* See if the checksum matches the super-journal name */
58433 cksum -= zSuper[u];
58437 ** containing the super-journal filename is corrupted. This means
58439 ** super-journal filename.
58451 ** following the value in pPager->journalOff, assuming a sector
58452 ** size of pPager->sectorSize bytes.
58457 ** ---------------------------------------
58466 i64 c = pPager->journalOff;
58468 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
58472 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
58479 ** This function is a no-op if the journal file has not been written to
58482 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
58484 ** zero the 28-byte header at the start of the journal file. In either case,
58485 ** if the pager is not in no-sync mode, sync the journal file immediately
58488 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
58499 assert( isOpen(pPager->jfd) );
58500 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
58501 if( pPager->journalOff ){
58502 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
58506 rc = sqlite3OsTruncate(pPager->jfd, 0);
58509 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
58511 if( rc==SQLITE_OK && !pPager->noSync ){
58512 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
58523 rc = sqlite3OsFileSize(pPager->jfd, &sz);
58525 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
58538 ** - 8 bytes: Magic identifying journal format.
58539 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
58540 ** - 4 bytes: Random number used for page hash.
58541 ** - 4 bytes: Initial database page count.
58542 ** - 4 bytes: Sector size used by the process that wrote this journal.
58543 ** - 4 bytes: Database page size.
58545 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
58549 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
58550 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
58554 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
58564 for(ii=0; ii<pPager->nSavepoint; ii++){
58565 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
58566 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
58570 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
58573 ** Write the nRec Field - the number of page records that follow this
58576 ** if in full-sync mode), the zero is overwritten with the true number
58586 ** * When the pager is in no-sync mode. Corruption can follow a
58592 assert( isOpen(pPager->fd) || pPager->noSync );
58593 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
58594 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
58604 /* The random check-hash initializer */
58605 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
58606 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
58620 sqlite3OsFileSize(pPager->jfd, &sz);
58622 assert( pPager->journalOff==journalHdrOffset(pPager) );
58623 assert( sqlite3JournalIsInMemory(pPager->jfd) );
58626 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
58629 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
58631 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
58634 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
58642 nHeader-(sizeof(aJournalMagic)+20));
58653 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
58656 ** The loop is required here in case the sector-size is larger than the
58662 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
58663 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
58664 assert( pPager->journalHdr <= pPager->journalOff );
58665 pPager->journalOff += nHeader;
58675 ** pPager->journalOff. See comments above function writeJournalHdr() for
58680 ** database before the transaction began, in pages. Also, pPager->cksumInit
58699 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
58705 pPager->journalOff = journalHdrOffset(pPager);
58706 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
58709 iHdrOff = pPager->journalOff;
58716 if( isHot || iHdrOff!=pPager->journalHdr ){
58717 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
58726 /* Read the first three 32-bit fields of the journal header: The nRec
58727 ** field, the checksum-initializer and the database size at the start
58730 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
58731 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
58732 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
58737 if( pPager->journalOff==0 ){
58738 u32 iPageSize; /* Page-size field of journal header */
58739 u32 iSectorSize; /* Sector-size field of journal header */
58741 /* Read the page-size and sector-size journal header fields. */
58742 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
58743 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
58748 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
58753 iPageSize = pPager->pageSize;
58756 /* Check that the values read from the page-size and sector-size fields
58763 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
58765 /* If the either the page-size or sector-size in the journal-header is
58766 ** invalid, then the process that wrote the journal-header must have
58773 /* Update the page-size to match the value read from the journal.
58777 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
58780 /* Update the assumed sector-size to match the value used by
58786 pPager->sectorSize = iSectorSize;
58789 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
58795 ** Write the supplied super-journal name into the journal file for pager
58796 ** pPager at the current location. The super-journal name must be the last
58797 ** thing written to a journal file. If the pager is in full-sync mode, the
58802 ** + N bytes: super-journal filename in utf-8.
58803 ** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
58804 ** + 4 bytes: super-journal name checksum.
58807 ** The super-journal page checksum is the sum of the bytes in the super-journal
58808 ** name, where each byte is interpreted as a signed 8-bit integer.
58811 ** this call is a no-op.
58820 assert( pPager->setSuper==0 );
58824 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
58825 || !isOpen(pPager->jfd)
58829 pPager->setSuper = 1;
58830 assert( pPager->journalHdr <= pPager->journalOff );
58837 /* If in full-sync mode, advance to the next disk sector before writing
58838 ** the super-journal name. This is in case the previous page written to
58841 if( pPager->fullSync ){
58842 pPager->journalOff = journalHdrOffset(pPager);
58844 iHdrOff = pPager->journalOff;
58846 /* Write the super-journal data to the end of the journal file. If
58849 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_SJ_PGNO(pPager))))
58850 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4)))
58851 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper)))
58852 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum)))
58853 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
58858 pPager->journalOff += (nSuper+20);
58860 /* If the pager is in persistent-journal mode, then the physical
58861 ** journal-file may extend past the end of the super-journal name
58863 ** dangerous because the code to rollback a hot-journal file
58864 ** will not be able to find the super-journal name to determine
58870 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
58871 && jrnlSize>pPager->journalOff
58873 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
58879 ** Discard the entire contents of the in-memory page-cache.
58882 pPager->iDataVersion++;
58883 sqlite3BackupRestart(pPager->pBackup);
58884 sqlite3PcacheClear(pPager->pPCache);
58888 ** Return the pPager->iDataVersion value
58891 return pPager->iDataVersion;
58896 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
58901 for(ii=0; ii<pPager->nSavepoint; ii++){
58902 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
58904 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
58905 sqlite3OsClose(pPager->sjfd);
58907 sqlite3_free(pPager->aSavepoint);
58908 pPager->aSavepoint = 0;
58909 pPager->nSavepoint = 0;
58910 pPager->nSubRec = 0;
58922 for(ii=0; ii<pPager->nSavepoint; ii++){
58923 PagerSavepoint *p = &pPager->aSavepoint[ii];
58924 if( pgno<=p->nOrig ){
58925 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
58934 ** This function is a no-op if the pager is in exclusive mode and not
58938 ** If the pager is not in exclusive-access mode, the database file is
58939 ** completely unlocked. If the file is unlocked and the file-system does
58945 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
58946 ** or not, any journal file left in the file-system will be treated
58947 ** as a hot-journal and rolled back the next time a read-transaction
58952 assert( pPager->eState==PAGER_READER
58953 || pPager->eState==PAGER_OPEN
58954 || pPager->eState==PAGER_ERROR
58957 sqlite3BitvecDestroy(pPager->pInJournal);
58958 pPager->pInJournal = 0;
58962 assert( !isOpen(pPager->jfd) );
58963 sqlite3WalEndReadTransaction(pPager->pWal);
58964 pPager->eState = PAGER_OPEN;
58965 }else if( !pPager->exclusiveMode ){
58967 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
58981 || 1!=(pPager->journalMode & 5)
58983 sqlite3OsClose(pPager->jfd);
58992 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
58993 pPager->eLock = UNKNOWN_LOCK;
58997 ** without clearing the error code. This is intentional - the error
59000 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
59001 pPager->eState = PAGER_OPEN;
59007 ** normal and exclusive-locking mode.
59009 assert( pPager->errCode==SQLITE_OK || !MEMDB );
59010 if( pPager->errCode ){
59011 if( pPager->tempFile==0 ){
59013 pPager->changeCountDone = 0;
59014 pPager->eState = PAGER_OPEN;
59016 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
59018 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
59019 pPager->errCode = SQLITE_OK;
59023 pPager->journalOff = 0;
59024 pPager->journalHdr = 0;
59025 pPager->setSuper = 0;
59032 ** the error-code about to be returned by a pager API function. The
59036 ** IOERR sub-codes, the pager enters the ERROR state and the error code
59040 ** The ERROR state indicates that the contents of the pager-cache
59042 ** the contents of the pager-cache. If a transaction was active when
59045 ** it were a hot-journal).
59051 pPager->errCode==SQLITE_FULL ||
59052 pPager->errCode==SQLITE_OK ||
59053 (pPager->errCode & 0xff)==SQLITE_IOERR
59056 pPager->errCode = rc;
59057 pPager->eState = PAGER_ERROR;
59073 ** * For non-TEMP databases, always sync to disk. This is necessary
59082 if( pPager->tempFile==0 ) return 1;
59084 if( !isOpen(pPager->fd) ) return 0;
59085 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
59091 ** after rollback of a hot-journal, or if an error occurs while opening
59092 ** the journal file or writing the very first journal-header of a
59097 ** exclusive than a RESERVED lock, it is a no-op.
59103 ** transaction. Nor will it be considered to be a hot-journal by this
59106 ** the current journal-mode (Pager.journalMode value), as follows:
59110 ** in-memory journal.
59129 ** If running in non-exclusive rollback mode, the lock on the file is
59147 ** is no write-transaction active but a RESERVED or greater lock is
59150 ** 1. After a successful hot-journal rollback, it is called with
59155 ** read-transaction, this function is called with eState==PAGER_READER
59156 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
59159 assert( pPager->eState!=PAGER_ERROR );
59160 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
59165 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
59166 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
59168 if( isOpen(pPager->jfd) ){
59172 if( sqlite3JournalIsInMemory(pPager->jfd) ){
59173 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
59174 sqlite3OsClose(pPager->jfd);
59175 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
59176 if( pPager->journalOff==0 ){
59179 rc = sqlite3OsTruncate(pPager->jfd, 0);
59180 if( rc==SQLITE_OK && pPager->fullSync ){
59186 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
59189 pPager->journalOff = 0;
59190 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
59191 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
59193 rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
59194 pPager->journalOff = 0;
59197 ** a hot-journal was just rolled back. In this case the journal
59199 ** the database file, it will do so using an in-memory journal.
59201 int bDelete = !pPager->tempFile;
59202 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
59203 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
59204 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
59205 || pPager->journalMode==PAGER_JOURNALMODE_WAL
59207 sqlite3OsClose(pPager->jfd);
59209 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
59215 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
59216 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
59219 p->pageHash = 0;
59225 sqlite3BitvecDestroy(pPager->pInJournal);
59226 pPager->pInJournal = 0;
59227 pPager->nRec = 0;
59230 sqlite3PcacheCleanAll(pPager->pPCache);
59232 sqlite3PcacheClearWritable(pPager->pPCache);
59234 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
59238 /* Drop the WAL write-lock, if any. Also, if the connection was in
59242 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
59244 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
59245 /* This branch is taken when committing a transaction in rollback-journal
59251 assert( pPager->eLock==EXCLUSIVE_LOCK );
59252 rc = pager_truncate(pPager, pPager->dbSize);
59256 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
59260 if( !pPager->exclusiveMode
59261 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
59265 pPager->eState = PAGER_READER;
59266 pPager->setSuper = 0;
59280 ** call to pager_unlock() will discard all in-memory pages, unlock
59282 ** means that there is a hot-journal left in the file-system, the next
59292 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
59294 if( pPager->eState>=PAGER_WRITER_LOCKED ){
59298 }else if( !pPager->exclusiveMode ){
59299 assert( pPager->eState==PAGER_READER );
59302 }else if( pPager->eState==PAGER_ERROR
59303 && pPager->journalMode==PAGER_JOURNALMODE_MEMORY
59304 && isOpen(pPager->jfd)
59306 /* Special case for a ROLLBACK due to I/O error with an in-memory
59309 int errCode = pPager->errCode;
59310 u8 eLock = pPager->eLock;
59311 pPager->eState = PAGER_OPEN;
59312 pPager->errCode = SQLITE_OK;
59313 pPager->eLock = EXCLUSIVE_LOCK;
59315 pPager->errCode = errCode;
59316 pPager->eLock = eLock;
59322 ** Parameter aData must point to a buffer of pPager->pageSize bytes
59324 ** page of data and the current value of pPager->cksumInit.
59327 ** random initial value (pPager->cksumInit) and every 200th byte
59328 ** of the page data, starting with byte offset (pPager->pageSize%200).
59329 ** Each byte is interpreted as an 8-bit unsigned integer.
59341 u32 cksum = pPager->cksumInit; /* Checksum value to return */
59342 int i = pPager->pageSize-200; /* Loop counter */
59345 i -= 200;
59352 ** from the sub-journal (if isMainJrnl==0) and playback that page.
59356 ** The main rollback journal uses checksums - the statement journal does
59359 ** If the page number of the page record read from the (sub-)journal file
59369 ** If the page record is successfully read from the (sub-)journal file
59371 ** while reading the record from the (sub-)journal file or while writing
59373 ** is successfully read from the (sub-)journal file but appears to be
59377 ** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or
59391 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
59404 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
59405 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
59407 aData = pPager->pTmpSpace;
59413 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
59414 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
59415 ** only reads from the main journal, not the sub-journal.
59417 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
59418 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
59420 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
59422 /* Read the page number and page data from the journal or sub-journal
59425 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
59428 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
59430 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
59441 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
59445 rc = read32bits(jfd, (*pOffset)-4, &cksum);
59461 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
59462 pPager->nReserve = ((u8*)aData)[20];
59469 ** An exception to the above rule: If the database is in no-sync mode
59479 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
59496 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
59506 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
59508 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
59509 (isMainJrnl?"main-journal":"sub-journal")
59512 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
59514 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
59516 if( isOpen(pPager->fd)
59517 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
59520 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
59521 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
59525 ** This is usually safe even for an encrypted database - as the data
59527 ** is if the data was just read from an in-memory sub-journal. In that
59530 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
59532 if( pgno>pPager->dbFileSize ){
59533 pPager->dbFileSize = pgno;
59535 if( pPager->pBackup ){
59536 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
59540 ** the database and the page is not in-memory, there is a potential
59541 ** problem. When the page is next fetched by the b-tree layer, it
59547 ** if the page is on the free-list at the start of the transaction, then
59550 ** The solution is to add an in-memory page to the cache containing
59551 ** the data just read from the sub-journal. Mark the page as dirty
59552 ** and if the pager requires a journal-sync, then mark the page as
59553 ** requiring a journal-sync before it is written.
59556 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
59557 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
59559 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
59560 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
59572 pData = pPg->pData;
59573 memcpy(pData, (u8*)aData, pPager->pageSize);
59574 pPager->xReiniter(pPg);
59584 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
59592 ** Parameter zSuper is the name of a super-journal file. A single journal
59593 ** file that referred to the super-journal file has just been rolled back.
59594 ** This routine checks if it is possible to delete the super-journal file,
59600 ** When a super-journal file is created, it is populated with the names
59601 ** of all of its child journals, one after another, formatted as utf-8
59603 ** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
59606 ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
59608 ** A super-journal file may only be deleted once all of its child
59611 ** This function reads the contents of the super-journal file into
59616 ** * if the child journal contains a reference to super-journal
59622 ** the file-system using sqlite3OsDelete().
59630 ** the entire contents of the super-journal file. This could be
59631 ** a couple of kilobytes or so - potentially larger than the page
59635 sqlite3_vfs *pVfs = pPager->pVfs;
59637 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
59638 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
59639 char *zSuperJournal = 0; /* Contents of super-journal file */
59640 i64 nSuperJournal; /* Size of super-journal file */
59642 char *zSuperPtr; /* Space to hold super-journal filename */
59647 ** If successful, open the super-journal file for reading.
59649 pSuper = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
59656 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
59660 /* Load the entire super-journal file into space obtained from
59662 ** sufficient space (in zSuperPtr) to hold the names of super-journal
59663 ** files extracted from regular rollback-journals.
59667 nSuperPtr = pVfs->mxPathname+1;
59682 while( (zJournal-zSuperJournal)<nSuperJournal ){
59689 /* One of the journals pointed to by the super-journal exists.
59690 ** Open it and check if it points at the super-journal. If
59691 ** so, return without deleting the super-journal file.
59711 /* We have a match. Do not delete the super-journal file. */
59734 ** file in the file-system. This only happens when committing a transaction,
59735 ** or rolling back a transaction (including rolling back a hot-journal).
59738 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
59739 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
59754 assert( pPager->eState!=PAGER_ERROR );
59755 assert( pPager->eState!=PAGER_READER );
59759 if( isOpen(pPager->fd)
59760 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
59763 int szPage = pPager->pageSize;
59764 assert( pPager->eLock==EXCLUSIVE_LOCK );
59766 rc = sqlite3OsFileSize(pPager->fd, ¤tSize);
59770 rc = sqlite3OsTruncate(pPager->fd, newSize);
59772 char *pTmp = pPager->pTmpSpace;
59774 testcase( (newSize-szPage) == currentSize );
59775 testcase( (newSize-szPage) > currentSize );
59776 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &newSize);
59777 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
59780 pPager->dbFileSize = nPage;
59788 ** Return a sanitized version of the sector-size of OS file pFile. The
59807 ** super-journal pointers within created journal files.
59811 ** Otherwise, for non-temporary files, the effective sector size is
59818 ** pPager->sectorSize is to define the "blast radius" of bytes that
59826 assert( isOpen(pPager->fd) || pPager->tempFile );
59828 if( pPager->tempFile
59829 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
59835 pPager->sectorSize = 512;
59837 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
59848 ** (2) 4 byte big-endian integer which is the number of valid page records
59851 ** (3) 4 byte big-endian integer which is the initial value for the
59855 ** (5) 4 byte big-endian integer which is the sector size. The header
59857 ** (6) 4 byte big-endian integer which is the page size.
59861 ** + pPager->pageSize bytes of data.
59878 ** no-sync option for the journal. A power failure could lead to corruption
59882 ** If the file opened as the journal file is not a well-formed
59888 ** If an I/O or malloc() error occurs, the journal-file is not deleted
59899 sqlite3_vfs *pVfs = pPager->pVfs;
59906 char *zSuper = 0; /* Name of super-journal file if any */
59909 u32 savedPageSize = pPager->pageSize;
59914 assert( isOpen(pPager->jfd) );
59915 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
59920 /* Read the super-journal name from the journal, if it is present.
59921 ** If a super-journal file name is specified, but the file is not
59927 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
59931 zSuper = pPager->pTmpSpace;
59932 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
59940 pPager->journalOff = 0;
59962 ** working in no-sync mode. This means that the rest of the journal
59967 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
59968 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
59986 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
59987 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
59993 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
59998 pPager->dbSize = mxPg;
59999 if( pPager->mxPgno<mxPg ){
60000 pPager->mxPgno = mxPg;
60012 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
60017 pPager->journalOff = szJ;
60043 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
60047 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
60051 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
60055 ** malloc error that occurred after the change-counter was updated but
60056 ** before the transaction was committed, then the change-counter
60059 ** update the change-counter at all. This may lead to cache inconsistency
60063 pPager->changeCountDone = pPager->tempFile;
60066 /* Leave 4 bytes of space before the super-journal filename in memory.
60070 zSuper = &pPager->pTmpSpace[4];
60071 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
60075 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
60084 /* If there was a super-journal and this routine will return success,
60085 ** see if it is possible to delete the super-journal.
60087 assert( zSuper==&pPager->pTmpSpace[4] );
60088 memset(pPager->pTmpSpace, 0, 4);
60094 nPlayback, pPager->zJournal);
60109 ** pPg->pData. A shared lock or greater must be held on the database
60119 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
60125 assert( pPager->eState>=PAGER_READER && !MEMDB );
60126 assert( isOpen(pPager->fd) );
60129 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
60133 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
60137 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
60138 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
60144 if( pPg->pgno==1 ){
60151 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
60158 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
60160 u8 *dbFileVers = &((u8*)pPg->pData)[24];
60161 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
60165 PAGER_INCR(pPager->nRead);
60166 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
60168 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
60174 ** Update the value of the change-counter at offsets 24 and 92 in
60178 ** routine which only updates the change-counter if the update is actually
60179 ** needed, as determined by the pPager->changeCountDone state variable.
60186 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
60187 put32bits(((char*)pPg->pData)+24, change_counter);
60192 put32bits(((char*)pPg->pData)+92, change_counter);
60193 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
60222 pPager->xReiniter(pPg);
60236 sqlite3BackupRestart(pPager->pBackup);
60255 pPager->dbSize = pPager->dbOrigSize;
60256 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
60257 pList = sqlite3PcacheDirtyList(pPager->pPCache);
60259 PgHdr *pNext = pList->pDirty;
60260 rc = pagerUndoCallback((void *)pPager, pList->pgno);
60286 assert( pPager->pWal );
60290 for(p=pList; p && p->pDirty; p=p->pDirty){
60291 assert( p->pgno < p->pDirty->pgno );
60295 assert( pList->pDirty==0 || isCommit );
60303 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
60304 if( p->pgno<=nTruncate ){
60305 ppNext = &p->pDirty;
60313 pPager->aStat[PAGER_STAT_WRITE] += nList;
60315 if( pList->pgno==1 ) pager_write_changecounter(pList);
60316 rc = sqlite3WalFrames(pPager->pWal,
60317 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
60319 if( rc==SQLITE_OK && pPager->pBackup ){
60320 for(p=pList; p; p=p->pDirty){
60321 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
60326 pList = sqlite3PcacheDirtyList(pPager->pPCache);
60327 for(p=pList; p; p=p->pDirty){
60348 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
60355 sqlite3WalEndReadTransaction(pPager->pWal);
60357 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
60360 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
60379 /* Query the WAL sub-system for the database size. The WalDbsize()
60382 ** available from the WAL sub-system if the log file is empty or
60385 assert( pPager->eState==PAGER_OPEN );
60386 assert( pPager->eLock>=SHARED_LOCK );
60387 assert( isOpen(pPager->fd) );
60388 assert( pPager->tempFile==0 );
60389 nPage = sqlite3WalDbsize(pPager->pWal);
60392 ** WAL sub-system, determine the page count based on the size of
60394 ** integer multiple of the page-size, round up the result.
60396 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
60398 int rc = sqlite3OsFileSize(pPager->fd, &n);
60402 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
60409 if( nPage>pPager->mxPgno ){
60410 pPager->mxPgno = (Pgno)nPage;
60419 ** Check if the *-wal file that corresponds to the database opened by pPager
60420 ** exists if the database is not empty, or verify that the *-wal file does
60423 ** If the database is not empty and the *-wal file exists, open the pager
60424 ** in WAL mode. If the database is empty or if no *-wal file exists and
60432 ** a WAL on a none-empty database, this ensures there is no race condition
60438 assert( pPager->eState==PAGER_OPEN );
60439 assert( pPager->eLock>=SHARED_LOCK );
60441 if( !pPager->tempFile ){
60444 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
60453 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
60455 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
60458 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
60459 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
60469 ** the entire super-journal file. The case pSavepoint==NULL occurs when
60473 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
60486 ** * Pages are then played back from the sub-journal file, starting
60500 ** (or transaction). No page with a page-number greater than this value
60505 i64 iHdrOff; /* End of first segment of main-journal records */
60509 assert( pPager->eState!=PAGER_ERROR );
60510 assert( pPager->eState>=PAGER_WRITER_LOCKED );
60514 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
60523 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
60524 pPager->changeCountDone = pPager->tempFile;
60530 /* Use pPager->journalOff as the effective size of the main rollback
60533 ** past pPager->journalOff is off-limits to us.
60535 szJ = pPager->journalOff;
60541 ** greater than the current database size (pPager->dbSize) but those
60546 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
60547 pPager->journalOff = pSavepoint->iOffset;
60548 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
60549 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
60553 pPager->journalOff = 0;
60558 ** of the main journal file. Continue to skip out-of-range pages and
60561 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
60569 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
60574 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
60576 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
60578 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
60579 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
60583 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
60585 /* Finally, rollback pages from the sub-journal. Page that were
60587 ** will be skipped. Out-of-range pages are also skipped.
60591 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
60594 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
60596 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
60597 assert( offset==(i64)ii*(4+pPager->pageSize) );
60605 pPager->journalOff = szJ;
60612 ** Change the maximum number of in-memory pages that are allowed
60616 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
60620 ** Change the maximum number of in-memory pages that are allowed
60624 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
60632 sqlite3_file *fd = pPager->fd;
60633 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
60635 sz = pPager->szMmap;
60636 pPager->bUseFetch = (sz>0);
60638 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
60647 pPager->szMmap = szMmap;
60655 sqlite3PcacheShrink(pPager->pPCache);
60677 ** database (with some additional information - the nRec field
60678 ** of the journal header - being written in between the two
60688 ** The above is for a rollback-journal mode. For WAL mode, OFF continues
60699 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
60714 if( pPager->tempFile ){
60715 pPager->noSync = 1;
60716 pPager->fullSync = 0;
60717 pPager->extraSync = 0;
60719 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
60720 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
60721 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
60723 if( pPager->noSync ){
60724 pPager->syncFlags = 0;
60726 pPager->syncFlags = SQLITE_SYNC_FULL;
60728 pPager->syncFlags = SQLITE_SYNC_NORMAL;
60730 pPager->walSyncFlags = (pPager->syncFlags<<2);
60731 if( pPager->fullSync ){
60732 pPager->walSyncFlags |= pPager->syncFlags;
60734 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
60735 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
60738 pPager->doNotSpill &= ~SPILLFLAG_OFF;
60740 pPager->doNotSpill |= SPILLFLAG_OFF;
60781 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
60789 ** The pager invokes the busy-handler if sqlite3OsLock() returns
60790 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
60794 ** (which occurs during hot-journal rollback). Summary:
60797 ** --------------------------------------------------------
60798 ** NO_LOCK -> SHARED_LOCK | Yes
60799 ** SHARED_LOCK -> RESERVED_LOCK | No
60800 ** SHARED_LOCK -> EXCLUSIVE_LOCK | No
60801 ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
60803 ** If the busy-handler callback returns non-zero, the lock is
60809 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
60813 pPager->xBusyHandler = xBusyHandler;
60814 pPager->pBusyHandlerArg = pBusyHandlerArg;
60815 ap = (void **)&pPager->xBusyHandler;
60818 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
60826 ** is a no-op. The value returned is the error state error code (i.e.
60827 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
60836 ** * the database is either not an in-memory database or it is
60837 ** an in-memory database that currently consists of zero pages.
60861 ** is a no-op for that case anyhow.
60866 if( (pPager->memDb==0 || pPager->dbSize==0)
60867 && sqlite3PcacheRefCount(pPager->pPCache)==0
60868 && pageSize && pageSize!=(u32)pPager->pageSize
60873 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
60874 rc = sqlite3OsFileSize(pPager->fd, &nByte);
60877 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
60889 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
60892 sqlite3PageFree(pPager->pTmpSpace);
60893 pPager->pTmpSpace = pNew;
60894 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
60895 pPager->pageSize = pageSize;
60896 pPager->lckPgno = (Pgno)(PENDING_BYTE/pageSize) + 1;
60902 *pPageSize = pPager->pageSize;
60904 if( nReserve<0 ) nReserve = pPager->nReserve;
60906 pPager->nReserve = (i16)nReserve;
60921 return pPager->pTmpSpace;
60933 pPager->mxPgno = mxPage;
60935 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
60936 /* assert( pPager->mxPgno>=pPager->dbSize ); */
60940 return pPager->mxPgno;
60948 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
60957 sqlite3_io_error_pending = -1;
60984 assert( isOpen(pPager->fd) || pPager->tempFile );
60992 if( isOpen(pPager->fd) ){
60994 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
61003 ** This function may only be called when a read-transaction is open on
61006 ** However, if the file is between 1 and <page-size> bytes in size, then
61010 assert( pPager->eState>=PAGER_READER );
61011 assert( pPager->eState!=PAGER_WRITER_FINISHED );
61012 *pnPage = (int)pPager->dbSize;
61018 ** a similar or greater lock is already held, this function is a no-op
61033 /* Check that this is either a no-op (because the requested lock is
61034 ** already held), or one of the transitions that the busy-handler
61038 assert( (pPager->eLock>=locktype)
61039 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
61040 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
61045 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
61051 ** following is true for all dirty pages currently in the page-cache:
61057 ** be necessary to write the current content out to the sub-journal.
61066 ** sub-journal rolled back the content could not be restored and the
61072 Pager *pPager = pPg->pPager;
61073 assert( pPg->flags&PGHDR_DIRTY );
61074 if( pPg->pgno>pPager->dbSize ){ /* if (a) is false */
61075 Pgno pgno = pPg->pgno;
61077 for(i=0; i<pPg->pPager->nSavepoint; i++){
61078 PagerSavepoint *p = &pPager->aSavepoint[i];
61079 assert( p->nOrig<pgno || sqlite3BitvecTestNotNull(p->pInSavepoint,pgno) );
61084 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
61091 ** Truncate the in-memory database file image to nPage pages. This
61102 assert( pPager->dbSize>=nPage || CORRUPT_DB );
61103 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
61104 pPager->dbSize = nPage;
61119 ** This function is called before attempting a hot-journal rollback. It
61120 ** syncs the journal file to disk, then sets pPager->journalHdr to the
61124 ** Syncing a hot-journal to disk before attempting to roll it back ensures
61125 ** that if a power-failure occurs during the rollback, the process that
61134 if( !pPager->noSync ){
61135 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
61138 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
61162 if( pPager->pMmapFreelist ){
61163 *ppPage = p = pPager->pMmapFreelist;
61164 pPager->pMmapFreelist = p->pDirty;
61165 p->pDirty = 0;
61166 assert( pPager->nExtra>=8 );
61167 memset(p->pExtra, 0, 8);
61169 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
61171 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
61174 p->pExtra = (void *)&p[1];
61175 p->flags = PGHDR_MMAP;
61176 p->nRef = 1;
61177 p->pPager = pPager;
61180 assert( p->pExtra==(void *)&p[1] );
61181 assert( p->pPage==0 );
61182 assert( p->flags==PGHDR_MMAP );
61183 assert( p->pPager==pPager );
61184 assert( p->nRef==1 );
61186 p->pgno = pgno;
61187 p->pData = pData;
61188 pPager->nMmapOut++;
61199 Pager *pPager = pPg->pPager;
61200 pPager->nMmapOut--;
61201 pPg->pDirty = pPager->pMmapFreelist;
61202 pPager->pMmapFreelist = pPg;
61204 assert( pPager->fd->pMethods->iVersion>=3 );
61205 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
61214 for(p=pPager->pMmapFreelist; p; p=pNext){
61215 pNext = p->pDirty;
61222 ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
61229 if( pPager->tempFile ) return SQLITE_OK;
61230 if( pPager->dbSize==0 ) return SQLITE_OK;
61231 assert( pPager->zFilename && pPager->zFilename[0] );
61232 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
61234 /* If the HAS_MOVED file-control is unimplemented, assume that the file
61260 u8 *pTmp = (u8*)pPager->pTmpSpace;
61266 /* pPager->errCode = 0; */
61267 pPager->exclusiveMode = 0;
61271 assert( db || pPager->pWal==0 );
61272 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
61277 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
61278 pPager->pWal = 0;
61293 ** back or finalize it. The next database user will have to do hot-journal
61296 if( isOpen(pPager->jfd) ){
61305 sqlite3OsClose(pPager->jfd);
61306 sqlite3OsClose(pPager->fd);
61308 sqlite3PcacheClose(pPager->pPCache);
61309 assert( !pPager->aSavepoint && !pPager->pInJournal );
61310 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
61321 return pPg->pgno;
61335 ** disk and can be restored in the event of a hot-journal rollback.
61337 ** If the Pager.noSync flag is set, then this function is a no-op.
61338 ** Otherwise, the actions required depend on the journal-mode and the
61339 ** device characteristics of the file-system, as follows:
61341 ** * If the journal file is an in-memory journal file, no action need
61347 ** been written following it. If the pager is operating in full-sync
61353 ** Or, in pseudo-code:
61355 ** if( NOT <in-memory journal> ){
61357 ** if( <full-sync mode> ) xSync(<journal file>);
61370 assert( pPager->eState==PAGER_WRITER_CACHEMOD
61371 || pPager->eState==PAGER_WRITER_DBMOD
61379 if( !pPager->noSync ){
61380 assert( !pPager->tempFile );
61381 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
61382 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
61383 assert( isOpen(pPager->jfd) );
61387 ** that wrote to this database was operating in persistent-journal
61390 ** file happens to be a journal-header (written as part of the
61391 ** previous connection's transaction), and a crash or power-failure
61395 ** hot-journal rollback following recovery. It may roll back all
61397 ** out-of-date data that follows it. Database corruption.
61413 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
61416 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
61419 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
61426 ** full-synchronous mode, sync the journal first. This ensures that
61436 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
61439 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
61442 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
61444 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
61451 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
61452 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
61457 pPager->journalHdr = pPager->journalOff;
61459 pPager->nRec = 0;
61464 pPager->journalHdr = pPager->journalOff;
61472 sqlite3PcacheClearSyncFlags(pPager->pPCache);
61473 pPager->eState = PAGER_WRITER_DBMOD;
61481 ** in-memory pages in the list to the database file. The argument may
61483 ** a no-op.
61490 ** If the pager is a temp-file pager and the actual file-system file
61515 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
61516 assert( pPager->eLock==EXCLUSIVE_LOCK );
61517 assert( isOpen(pPager->fd) || pList->pDirty==0 );
61519 /* If the file is a temp-file has not yet been opened, open it now. It
61521 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
61523 if( !isOpen(pPager->fd) ){
61524 assert( pPager->tempFile && rc==SQLITE_OK );
61525 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
61531 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
61533 && pPager->dbHintSize<pPager->dbSize
61534 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
61536 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
61537 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
61538 pPager->dbHintSize = pPager->dbSize;
61542 Pgno pgno = pList->pgno;
61546 ** make the file smaller (presumably by auto-vacuum code). Do not write
61552 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
61553 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
61556 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
61557 if( pList->pgno==1 ) pager_write_changecounter(pList);
61559 pData = pList->pData;
61562 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
61569 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
61571 if( pgno>pPager->dbFileSize ){
61572 pPager->dbFileSize = pgno;
61574 pPager->aStat[PAGER_STAT_WRITE]++;
61577 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
61587 pList = pList->pDirty;
61594 ** Ensure that the sub-journal file is open. If it is already open, this
61595 ** function is a no-op.
61603 if( !isOpen(pPager->sjfd) ){
61608 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
61609 nStmtSpill = -1;
61611 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
61617 ** Append a record of the current state of page pPg to the sub-journal.
61619 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
61623 ** error code if the attempt to write to the sub-journal fails, or
61629 Pager *pPager = pPg->pPager;
61630 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
61632 /* Open the sub-journal, if it has not already been opened */
61633 assert( pPager->useJournal );
61634 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
61635 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
61638 || pPg->pgno>pPager->dbOrigSize
61642 /* If the sub-journal was opened successfully (or was already open),
61645 void *pData = pPg->pData;
61646 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
61649 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
61650 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
61652 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
61657 pPager->nSubRec++;
61658 assert( pPager->nSavepoint>0 );
61659 rc = addToSavepointBitvecs(pPager, pPg->pgno);
61674 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
61694 assert( pPg->pPager==pPager );
61695 assert( pPg->flags&PGHDR_DIRTY );
61713 if( NEVER(pPager->errCode) ) return SQLITE_OK;
61714 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
61715 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
61716 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
61717 if( pPager->doNotSpill
61718 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
61719 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
61724 pPager->aStat[PAGER_STAT_SPILL]++;
61725 pPg->pDirty = 0;
61735 if( pPager->tempFile==0 ){
61736 rc = sqlite3JournalCreate(pPager->jfd);
61742 if( pPg->flags&PGHDR_NEED_SYNC
61743 || pPager->eState==PAGER_WRITER_CACHEMOD
61750 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
61757 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
61768 int rc = pPager->errCode;
61770 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
61773 PgHdr *pNext = pList->pDirty;
61774 if( pList->nRef==0 ){
61790 ** If zFilename is NULL then a randomly-named temporary file is created
61794 ** This can be used to implement an in-memory database.
61820 int nExtra, /* Extra bytes append to each in-memory page */
61828 int tempFile = 0; /* True for temp files (incl. in-memory files) */
61829 int memDb = 0; /* True if this is an in-memory file */
61831 int readOnly = 0; /* True if this is a read-only file */
61841 /* Figure out how much space is required for each journal file-handle
61842 ** (there are two of them, the main journal and the sub-journal). */
61866 nPathname = pVfs->mxPathname+1;
61888 nUriByte = (int)(&z[1] - zUri);
61890 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
61892 ** the database being opened will be more than pVfs->mxPathname
61895 ** check for a hot-journal before reading.
61911 ** Database file handle (pVfs->szOsFile bytes)
61912 ** Sub-journal file handle (journalFileSize bytes)
61922 ** Some 3rd-party software, over which we have no control, depends on
61926 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
61929 ** filename format expected by 3rd-party software should be as follows:
61931 ** - Main Database Path
61932 ** - \0
61933 ** - Multiple URI components consisting of:
61934 ** - Key
61935 ** - \0
61936 ** - Value
61937 ** - \0
61938 ** - \0
61939 ** - Journal Path
61940 ** - \0
61941 ** - WAL Path (zWALName)
61942 ** - \0
61953 ROUND8(pVfs->szOsFile) + /* The main db file */
61971 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize);
61972 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile);
61973 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
61974 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
61975 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
61980 pPager->zFilename = (char*)pPtr;
61993 pPager->zJournal = (char*)pPtr;
61995 memcpy(pPtr, "-journal",8); pPtr += 8 + 1;
61997 sqlite3FileSuffix3(zFilename,pPager->zJournal);
61998 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
62001 pPager->zJournal = 0;
62007 pPager->zWal = (char*)pPtr;
62009 memcpy(pPtr, "-wal", 4); pPtr += 4 + 1;
62011 sqlite3FileSuffix3(zFilename, pPager->zWal);
62012 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
62015 pPager->zWal = 0;
62021 pPager->pVfs = pVfs;
62022 pPager->vfsFlags = vfsFlags;
62028 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
62030 pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
62042 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
62046 if( szPageDflt<pPager->sectorSize ){
62047 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
62050 szPageDflt = (u32)pPager->sectorSize;
62067 pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0);
62069 || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){
62079 ** This branch is also run for an in-memory database. An in-memory
62080 ** database is the same as a temp-file that is never written out to
62081 ** disk and uses an in-memory rollback journal.
62087 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
62088 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
62089 pPager->noLock = 1; /* Do no locking */
62097 assert( pPager->memDb==0 );
62098 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
62107 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
62113 sqlite3OsClose(pPager->fd);
62114 sqlite3PageFree(pPager->pTmpSpace);
62119 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
62120 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
62122 pPager->useJournal = (u8)useJournal;
62123 /* pPager->stmtOpen = 0; */
62124 /* pPager->stmtInUse = 0; */
62125 /* pPager->nRef = 0; */
62126 /* pPager->stmtSize = 0; */
62127 /* pPager->stmtJSize = 0; */
62128 /* pPager->nPage = 0; */
62129 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
62130 /* pPager->state = PAGER_UNLOCK; */
62131 /* pPager->errMask = 0; */
62132 pPager->tempFile = (u8)tempFile;
62136 pPager->exclusiveMode = (u8)tempFile;
62137 pPager->changeCountDone = pPager->tempFile;
62138 pPager->memDb = (u8)memDb;
62139 pPager->readOnly = (u8)readOnly;
62140 assert( useJournal || pPager->tempFile );
62142 /* pPager->pFirst = 0; */
62143 /* pPager->pFirstSynced = 0; */
62144 /* pPager->pLast = 0; */
62145 pPager->nExtra = (u16)nExtra;
62146 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
62147 assert( isOpen(pPager->fd) || tempFile );
62150 pPager->journalMode = PAGER_JOURNALMODE_OFF;
62152 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
62154 /* pPager->xBusyHandler = 0; */
62155 /* pPager->pBusyHandlerArg = 0; */
62156 pPager->xReiniter = xReinit;
62158 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
62159 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
62173 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
62174 zName--;
62176 p = zName - 4 - sizeof(Pager*);
62179 return pPager->fd;
62186 ** the file-system for the given pager. A hot journal is one that
62187 ** needs to be played back. According to this function, a hot-journal
62201 ** This routine does not check if there is a super-journal filename
62202 ** at the end of the file. If there is, and that super-journal file
62204 ** case this routine will return a false-positive. The pager_playback()
62208 ** If a hot-journal file is found to exist, *pExists is set to 1 and
62209 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
62211 ** to determine whether or not a hot-journal file exists, the IO error
62215 sqlite3_vfs * const pVfs = pPager->pVfs;
62218 int jrnlOpen = !!isOpen(pPager->jfd);
62220 assert( pPager->useJournal );
62221 assert( isOpen(pPager->fd) );
62222 assert( pPager->eState==PAGER_OPEN );
62224 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
62230 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
62240 ** in fact there is none. This results in a false-positive which will
62243 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
62247 assert( pPager->tempFile==0 );
62261 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
62262 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
62268 ** at least one non-zero bytes at the start of the journal file.
62274 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
62278 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
62283 sqlite3OsClose(pPager->jfd);
62310 ** has been successfully called. If a shared-lock is already held when
62311 ** this function is called, it is a no-op.
62318 ** the SHARED lock, the file-system is checked for a hot-journal,
62319 ** which is played back if present. Following any hot-journal
62321 ** the 'change-counter' field of the database file header and
62324 ** 2) If the pager is running in exclusive-mode, and there are currently
62331 ** occurs while locking the database, checking for a hot-journal file or
62337 /* This routine is only called from b-tree and only when there are no
62341 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
62343 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
62344 assert( pPager->errCode==SQLITE_OK );
62346 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
62347 int bHotJournal = 1; /* True if there exists a hot journal-file */
62350 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
62354 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
62361 if( pPager->eLock<=SHARED_LOCK ){
62368 if( pPager->readOnly ){
62378 ** hot-journal back.
62395 ** in exclusive-access mode the file descriptor will be kept open
62396 ** and possibly used for a transaction later on. Also, write-access
62403 ** may mean that the pager was in the error-state when this
62406 if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
62407 sqlite3_vfs * const pVfs = pPager->pVfs;
62410 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
62414 assert( !pPager->tempFile );
62415 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
62416 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
62419 sqlite3OsClose(pPager->jfd);
62426 ** playing back the hot-journal so that we don't end up with
62432 if( isOpen(pPager->jfd) ){
62436 rc = pager_playback(pPager, !pPager->tempFile);
62437 pPager->eState = PAGER_OPEN;
62439 }else if( !pPager->exclusiveMode ){
62445 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
62464 assert( pPager->eState==PAGER_OPEN );
62465 assert( (pPager->eLock==SHARED_LOCK)
62466 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
62470 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
62471 /* The shared-lock has just been acquired then check to
62475 ** single unnecessary sqlite3OsRead() call at the start-up.
62479 ** a 32-bit counter that is incremented with each change. The
62487 char dbFileVers[sizeof(pPager->dbFileVers)];
62490 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
62498 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
62508 sqlite3OsUnfetch(pPager->fd, 0, 0);
62513 /* If there is a WAL file in the file-system, open this database in WAL
62514 ** mode. Otherwise, the following function call is a no-op.
62518 assert( pPager->pWal==0 || rc==SQLITE_OK );
62527 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
62528 rc = pagerPagecount(pPager, &pPager->dbSize);
62535 assert( pPager->eState==PAGER_OPEN );
62537 pPager->eState = PAGER_READER;
62538 pPager->hasHeldSharedLock = 1;
62549 ** nothing to rollback, so this routine is a no-op.
62552 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
62553 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
62566 ** getPageNormal() -- The normal getter
62567 ** getPageError() -- Used if the pager is in an error state
62568 ** getPageMmap() -- Used if memory-mapped I/O is enabled
62590 ** a) When reading a free-list leaf page from the database, and
62608 ** to find a page in the in-memory cache first. If the page is not already
62610 ** just returns 0. This routine acquires a read-lock the first time it
62626 assert( pPager->errCode==SQLITE_OK );
62627 assert( pPager->eState>=PAGER_READER );
62629 assert( pPager->hasHeldSharedLock==1 );
62632 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
62635 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
62642 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
62644 assert( pPg->pgno==pgno );
62645 assert( pPg->pPager==pPager || pPg->pPager==0 );
62648 if( pPg->pPager && !noContent ){
62652 pPager->aStat[PAGER_STAT_HIT]++;
62667 pPg->pPager = pPager;
62669 assert( !isOpen(pPager->fd) || !MEMDB );
62670 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
62671 if( pgno>pPager->mxPgno ){
62673 if( pgno<=pPager->dbSize ){
62680 /* Failure to set the bits in the InJournal bit-vectors is benign.
62687 if( pgno<=pPager->dbOrigSize ){
62688 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
62695 memset(pPg->pData, 0, pPager->pageSize);
62698 assert( pPg->pPager==pPager );
62699 pPager->aStat[PAGER_STAT_MISS]++;
62720 /* The page getter for when memory-mapped I/O is enabled */
62731 /* It is acceptable to use a read-only (mmap) page for any page except
62732 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
62734 ** temporary or in-memory database. */
62736 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
62748 assert( pPager->eState>=PAGER_READER );
62750 assert( pPager->hasHeldSharedLock==1 );
62751 assert( pPager->errCode==SQLITE_OK );
62754 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
62762 rc = sqlite3OsFetch(pPager->fd,
62763 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
62766 if( pPager->eState>PAGER_READER || pPager->tempFile ){
62772 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
62798 assert( pPager->errCode!=SQLITE_OK );
62800 return pPager->errCode;
62816 rc = pPager->xGet(pPager, pgno, ppPage, flags);
62823 /* Normal, high-speed version of sqlite3PagerGet() */
62824 return pPager->xGet(pPager, pgno, ppPage, flags);
62829 ** Acquire a page if it is already in the in-memory cache. Do
62843 assert( pPager->pPCache!=0 );
62844 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
62845 assert( pPage==0 || pPager->hasHeldSharedLock );
62847 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
62857 ** The assert() at tag-20230419-2 proves that this constraint is always
62865 TESTONLY( Pager *pPager = pPg->pPager; )
62867 if( pPg->flags & PGHDR_MMAP ){
62868 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
62874 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 ); /* tag-20230419-2 */
62882 assert( pPg->pgno==1 );
62883 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
62884 pPager = pPg->pPager;
62895 ** to the start of it. If there are active savepoints, open the sub-journal
62913 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
62915 assert( pPager->eState==PAGER_WRITER_LOCKED );
62917 assert( pPager->pInJournal==0 );
62919 /* If already in the error state, this function is a no-op. But on
62922 if( NEVER(pPager->errCode) ) return pPager->errCode;
62924 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
62925 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
62926 if( pPager->pInJournal==0 ){
62931 if( !isOpen(pPager->jfd) ){
62932 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
62933 sqlite3MemJournalOpen(pPager->jfd);
62938 if( pPager->tempFile ){
62952 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
62956 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
62961 ** the sub-journal if necessary.
62965 pPager->nRec = 0;
62966 pPager->journalOff = 0;
62967 pPager->setSuper = 0;
62968 pPager->journalHdr = 0;
62974 sqlite3BitvecDestroy(pPager->pInJournal);
62975 pPager->pInJournal = 0;
62976 pPager->journalOff = 0;
62978 assert( pPager->eState==PAGER_WRITER_LOCKED );
62979 pPager->eState = PAGER_WRITER_CACHEMOD;
62986 ** Begin a write-transaction on the specified pager object. If a
62987 ** write-transaction has already been opened, this function is a no-op.
62994 ** If the subjInMemory argument is non-zero, then any sub-journal opened
62995 ** within this transaction will be opened as an in-memory file. This
62996 ** has no effect if the sub-journal is already opened (as it may be when
62998 ** sub-journal. If the subjInMemory argument is zero, then any required
62999 ** sub-journal is implemented in-memory if pPager is an in-memory database,
63005 if( pPager->errCode ) return pPager->errCode;
63006 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
63007 pPager->subjInMemory = (u8)subjInMemory;
63009 if( pPager->eState==PAGER_READER ){
63010 assert( pPager->pInJournal==0 );
63016 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
63021 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
63026 ** The busy-handler is not invoked if another connection already
63027 ** holds the write-lock. If possible, the upper layer will call it.
63029 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
63033 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
63048 ** transactions may copy data from the sub-journal into the database
63052 pPager->eState = PAGER_WRITER_LOCKED;
63053 pPager->dbHintSize = pPager->dbSize;
63054 pPager->dbFileSize = pPager->dbSize;
63055 pPager->dbOrigSize = pPager->dbSize;
63056 pPager->journalOff = 0;
63059 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
63060 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
63072 Pager *pPager = pPg->pPager;
63076 i64 iOff = pPager->journalOff;
63081 assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) );
63083 assert( pPager->journalHdr<=pPager->journalOff );
63084 pData2 = pPg->pData;
63088 ** page in the block above, set the need-sync flag for the page.
63094 pPg->flags |= PGHDR_NEED_SYNC;
63096 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
63098 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
63100 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
63103 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
63104 pPager->journalOff, pPager->pageSize));
63107 PAGERID(pPager), pPg->pgno,
63108 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
63110 pPager->journalOff += 8 + pPager->pageSize;
63111 pPager->nRec++;
63112 assert( pPager->pInJournal!=0 );
63113 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
63116 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
63123 ** main journal or sub-journal as required. If the page is written into
63129 Pager *pPager = pPg->pPager;
63132 /* This routine is not called unless a write-transaction has already
63136 assert( pPager->eState==PAGER_WRITER_LOCKED
63137 || pPager->eState==PAGER_WRITER_CACHEMOD
63138 || pPager->eState==PAGER_WRITER_DBMOD
63141 assert( pPager->errCode==0 );
63142 assert( pPager->readOnly==0 );
63146 ** obtained the necessary locks to begin the write-transaction, but the
63154 if( pPager->eState==PAGER_WRITER_LOCKED ){
63158 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
63168 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
63169 if( pPager->pInJournal!=0
63170 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
63173 if( pPg->pgno<=pPager->dbOrigSize ){
63179 if( pPager->eState!=PAGER_WRITER_DBMOD ){
63180 pPg->flags |= PGHDR_NEED_SYNC;
63183 PAGERID(pPager), pPg->pgno,
63184 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
63188 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
63193 pPg->flags |= PGHDR_WRITEABLE;
63198 if( pPager->nSavepoint>0 ){
63203 if( pPager->dbSize<pPg->pgno ){
63204 pPager->dbSize = pPg->pgno;
63227 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
63228 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
63235 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
63236 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
63238 /* This trick assumes that both the page-size and sector-size are
63242 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
63244 nPageCount = pPager->dbSize;
63245 if( pPg->pgno>nPageCount ){
63246 nPage = (pPg->pgno - pg1)+1;
63247 }else if( (pg1+nPagePerSector-1)>nPageCount ){
63248 nPage = nPageCount+1-pg1;
63253 assert(pg1<=pPg->pgno);
63254 assert((pg1+nPage)>pPg->pgno);
63259 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
63264 if( pPage->flags&PGHDR_NEED_SYNC ){
63271 if( pPage->flags&PGHDR_NEED_SYNC ){
63289 pPage->flags |= PGHDR_NEED_SYNC;
63295 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
63296 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
63308 ** fit on a single disk sector. In this case all co-resident pages
63315 Pager *pPager = pPg->pPager;
63316 assert( (pPg->flags & PGHDR_MMAP)==0 );
63317 assert( pPager->eState>=PAGER_WRITER_LOCKED );
63319 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
63320 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
63322 }else if( pPager->errCode ){
63323 return pPager->errCode;
63324 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
63325 assert( pPager->tempFile==0 );
63339 return pPg->flags & PGHDR_WRITEABLE;
63357 ** This optimization cannot be used with a temp-file, as the page may
63364 Pager *pPager = pPg->pPager;
63365 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
63366 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
63367 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
63368 pPg->flags |= PGHDR_DONT_WRITE;
63369 pPg->flags &= ~PGHDR_WRITEABLE;
63370 testcase( pPg->flags & PGHDR_NEED_SYNC );
63377 ** change-counter, stored as a 4-byte big-endian integer starting at
63381 ** But this only happens if the pPager->changeCountDone flag is false.
63391 ** The isDirectMode flag may only be non-zero if the library was compiled
63393 ** if isDirect is non-zero, then the database file is updated directly
63400 assert( pPager->eState==PAGER_WRITER_CACHEMOD
63401 || pPager->eState==PAGER_WRITER_DBMOD
63406 ** atomic-write optimization is enabled in this build, then isDirect
63410 ** The idea is that if the atomic-write optimization is not
63423 if( !pPager->changeCountDone && pPager->dbSize>0 ){
63426 assert( !pPager->tempFile && isOpen(pPager->fd) );
63433 ** operating in direct-mode, make page 1 writable. When not in
63435 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
63448 assert( pPager->dbFileSize>0 );
63449 zBuf = pPgHdr->pData;
63451 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
63452 pPager->aStat[PAGER_STAT_WRITE]++;
63455 /* Update the pager's copy of the change-counter. Otherwise, the
63457 ** flushed (as the change-counter values will not match). */
63459 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
63460 pPager->changeCountDone = 1;
63463 pPager->changeCountDone = 1;
63474 ** Sync the database file to disk. This is a no-op for in-memory databases
63477 ** If successful, or if called on a pager for which it is a no-op, this
63483 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
63485 if( rc==SQLITE_OK && !pPager->noSync ){
63487 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
63493 ** This function may only be called while a write-transaction is active in
63494 ** rollback. If the connection is in WAL mode, this call is a no-op.
63504 int rc = pPager->errCode;
63507 assert( pPager->eState==PAGER_WRITER_CACHEMOD
63508 || pPager->eState==PAGER_WRITER_DBMOD
63509 || pPager->eState==PAGER_WRITER_LOCKED
63521 ** of a super-journal file that should be written into the individual
63523 ** super-journal (a single database transaction).
63527 ** * The database file change-counter is updated,
63528 ** * the journal is synced (unless the atomic-write optimization is used),
63535 ** delete the super-journal file if specified).
63540 ** If the final parameter - noSync - is true, then the database file itself
63547 const char *zSuper, /* If not NULL, the super-journal name */
63552 assert( pPager->eState==PAGER_WRITER_LOCKED
63553 || pPager->eState==PAGER_WRITER_CACHEMOD
63554 || pPager->eState==PAGER_WRITER_DBMOD
63555 || pPager->eState==PAGER_ERROR
63560 if( NEVER(pPager->errCode) ) return pPager->errCode;
63566 pPager->zFilename, zSuper, pPager->dbSize));
63569 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
63571 assert( MEMDB==0 || pPager->tempFile );
63572 assert( isOpen(pPager->fd) || pPager->tempFile );
63574 /* If this is an in-memory db, or no pages have been written to, or this
63575 ** function has already been called, it is mostly a no-op. However, any
63577 sqlite3BackupRestart(pPager->pBackup);
63582 pList = sqlite3PcacheDirtyList(pPager->pPCache);
63585 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
63588 pList->pDirty = 0;
63592 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
63596 sqlite3PcacheCleanAll(pPager->pPCache);
63599 /* The bBatch boolean is true if the batch-atomic-write commit method
63600 ** should be used. No rollback journal is created if batch-atomic-write
63604 sqlite3_file *fd = pPager->fd;
63607 && !pPager->noSync
63608 && sqlite3JournalIsInMemory(pPager->jfd);
63614 /* The following block updates the change-counter. Exactly how it
63615 ** does this depends on whether or not the atomic-update optimization
63619 ** * The file-system supports the atomic-write property for
63620 ** blocks of size page-size, and
63621 ** * This commit is not part of a multi-file transaction, and
63626 ** counter in 'indirect-mode'. If the optimization is compiled in but
63629 ** pager_incr_changecounter() to update the change-counter in indirect
63633 ** then call pager_incr_changecounter() to update the change-counter
63639 assert( isOpen(pPager->jfd)
63640 || pPager->journalMode==PAGER_JOURNALMODE_OFF
63641 || pPager->journalMode==PAGER_JOURNALMODE_WAL
63643 if( !zSuper && isOpen(pPager->jfd)
63644 && pPager->journalOff==jrnlBufferSize(pPager)
63645 && pPager->dbSize>=pPager->dbOrigSize
63646 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
63648 /* Update the db file change counter via the direct-write method. The
63649 ** following call will modify the in-memory representation of page 1
63651 ** directly to the database file. Because of the atomic-write
63652 ** property of the host file-system, this is safe.
63656 rc = sqlite3JournalCreate(pPager->jfd);
63665 rc = sqlite3JournalCreate(pPager->jfd);
63674 /* Write the super-journal name into the journal file. If a
63675 ** super-journal file name has already been written to the journal file,
63676 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
63682 ** If the atomic-update optimization is being used, this sync will not
63685 ** Because the change-counter page was just modified, unless the
63686 ** atomic-update optimization is used it is almost certain that the
63690 ** xSync() call will be changed to a no-op by the OS anyhow.
63695 pList = sqlite3PcacheDirtyList(pPager->pPCache);
63701 if( rc==SQLITE_OK && pPager->dbSize>pPager->dbFileSize ){
63702 char *pTmp = pPager->pTmpSpace;
63703 int szPage = (int)pPager->pageSize;
63705 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage,
63706 ((i64)pPager->dbSize*pPager->pageSize)-szPage);
63717 rc = sqlite3JournalCreate(pPager->jfd);
63719 sqlite3OsClose(pPager->jfd);
63724 sqlite3OsClose(pPager->jfd);
63736 sqlite3PcacheCleanAll(pPager->pPCache);
63741 ** last page in the db image moved to the free-list. In this case the
63744 if( pPager->dbSize>pPager->dbFileSize ){
63745 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_SJ_PGNO(pPager));
63746 assert( pPager->eState==PAGER_WRITER_DBMOD );
63761 pPager->eState = PAGER_WRITER_FINISHED;
63770 ** synced to disk. The journal file still exists in the file-system
63772 ** be used as a hot-journal and the current transaction rolled back.
63776 ** for hot-journal rollback. Once this is done the transaction is
63788 if( NEVER(pPager->errCode) ) return pPager->errCode;
63789 pPager->iDataVersion++;
63791 assert( pPager->eState==PAGER_WRITER_LOCKED
63792 || pPager->eState==PAGER_WRITER_FINISHED
63793 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
63798 ** this transaction, the pager is running in exclusive-mode and is
63799 ** using persistent journals, then this function is a no-op.
63803 ** a hot-journal during hot-journal rollback, 0 changes will be made
63808 if( pPager->eState==PAGER_WRITER_LOCKED
63809 && pPager->exclusiveMode
63810 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
63812 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
63813 pPager->eState = PAGER_READER;
63818 rc = pager_end_transaction(pPager, pPager->setSuper, 1);
63824 ** transaction are reverted and the current write-transaction is closed.
63834 ** in-memory cache pages to the state they were in when the transaction
63843 ** In WAL mode, all cache-entries containing data modified within the
63845 ** their pre-transaction state by re-reading data from the database or
63852 /* PagerRollback() is a no-op if called in READER or OPEN state. If
63857 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
63858 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
63862 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
63863 rc2 = pager_end_transaction(pPager, pPager->setSuper, 0);
63865 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
63866 int eState = pPager->eState;
63873 pPager->errCode = SQLITE_ABORT;
63874 pPager->eState = PAGER_ERROR;
63882 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
63895 ** Return TRUE if the database file is opened read-only. Return FALSE
63899 return pPager->readOnly;
63907 return sqlite3PcacheRefCount(pPager->pPCache);
63916 int perPageSize = pPager->pageSize + pPager->nExtra
63918 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
63920 + pPager->pageSize;
63936 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
63937 a[1] = sqlite3PcachePagecount(pPager->pPCache);
63938 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
63939 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
63940 a[4] = pPager->eState;
63941 a[5] = pPager->errCode;
63942 a[6] = (int)pPager->aStat[PAGER_STAT_HIT] & 0x7fffffff;
63943 a[7] = (int)pPager->aStat[PAGER_STAT_MISS] & 0x7fffffff;
63944 a[8] = 0; /* Used to be pPager->nOvfl */
63945 a[9] = pPager->nRead;
63946 a[10] = (int)pPager->aStat[PAGER_STAT_WRITE] & 0x7fffffff;
63959 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
63975 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
63976 *pnVal += pPager->aStat[eStat];
63978 pPager->aStat[eStat] = 0;
63983 ** Return true if this is an in-memory or temp-file backed pager.
63986 return pPager->tempFile || pPager->memVfs;
63993 ** equal to nSavepoint, then this function is a no-op.
63996 ** occurs while opening the sub-journal file, then an IO error code is
64001 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
64005 assert( pPager->eState>=PAGER_WRITER_LOCKED );
64007 assert( nSavepoint>nCurrent && pPager->useJournal );
64014 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
64019 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
64020 pPager->aSavepoint = aNew;
64024 aNew[ii].nOrig = pPager->dbSize;
64025 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
64026 aNew[ii].iOffset = pPager->journalOff;
64030 aNew[ii].iSubRec = pPager->nSubRec;
64031 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
64037 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
64039 pPager->nSavepoint = ii+1;
64041 assert( pPager->nSavepoint==nSavepoint );
64046 assert( pPager->eState>=PAGER_WRITER_LOCKED );
64049 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
64069 ** (the first created). A value of (Pager.nSavepoint-1) means operate
64071 ** (Pager.nSavepoint-1), then this function is a no-op.
64088 int rc = pPager->errCode;
64097 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
64106 for(ii=nNew; ii<pPager->nSavepoint; ii++){
64107 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
64109 pPager->nSavepoint = nNew;
64111 /* Truncate the sub-journal so that it only includes the parts
64114 PagerSavepoint *pRel = &pPager->aSavepoint[nNew];
64115 if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd) ){
64116 /* Only truncate if it is an in-memory sub-journal. */
64117 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
64118 i64 sz = (pPager->pageSize+4)*(i64)pRel->iSubRec;
64119 rc = sqlite3OsTruncate(pPager->sjfd, sz);
64122 pPager->nSubRec = pRel->iSubRec;
64126 ** If this is a temp-file, it is possible that the journal file has
64130 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
64131 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
64142 pPager->journalMode==PAGER_JOURNALMODE_OFF
64143 && pPager->eState>=PAGER_WRITER_CACHEMOD
64145 pPager->errCode = SQLITE_ABORT;
64146 pPager->eState = PAGER_ERROR;
64158 ** Except, if the pager is in-memory only, then return an empty string if
64162 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
64163 ** participate in shared-cache.
64170 if( nullIfMemDb && (pPager->memDb || sqlite3IsMemdb(pPager->pVfs)) ){
64173 return pPager->zFilename;
64181 return pPager->pVfs;
64190 return pPager->fd;
64199 return pPager->jfd;
64201 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
64209 return pPager->zJournal;
64222 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
64230 ** If the fourth argument, isCommit, is non-zero, then this page is being
64240 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
64244 assert( pPg->nRef>0 );
64245 assert( pPager->eState==PAGER_WRITER_CACHEMOD
64246 || pPager->eState==PAGER_WRITER_DBMOD
64250 /* In order to be able to rollback, an in-memory database must journal
64253 assert( pPager->tempFile || !MEMDB );
64254 if( pPager->tempFile ){
64261 ** sub-journal now. This is required to handle the following scenario:
64269 ** If page X were not written to the sub-journal here, it would not
64273 ** subjournalPage() may need to allocate space to store pPg->pgno into
64277 if( (pPg->flags & PGHDR_DIRTY)!=0
64284 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
64285 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
64287 /* If the journal needs to be sync()ed before page pPg->pgno can
64288 ** be written to, store pPg->pgno in local variable needSyncPgno.
64291 ** the journal needs to be sync()ed before database page pPg->pgno
64294 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
64295 needSyncPgno = pPg->pgno;
64296 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
64297 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
64298 assert( pPg->flags&PGHDR_DIRTY );
64301 /* If the cache contains a page with page-number pgno, remove it
64306 pPg->flags &= ~PGHDR_NEED_SYNC;
64308 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
64310 if( NEVER(pPgOld->nRef>1) ){
64314 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
64315 if( pPager->tempFile ){
64316 /* Do not discard pages from an in-memory database since we might
64318 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
64324 origPgno = pPg->pgno;
64328 /* For an in-memory database, make sure the original page continues
64332 if( pPager->tempFile && pPgOld ){
64338 /* If needSyncPgno is non-zero, then the journal file needs to be
64340 ** Currently, no such page exists in the page-cache and the
64342 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
64345 ** If the attempt to load the page into the page-cache fails, (due
64355 if( needSyncPgno<=pPager->dbOrigSize ){
64356 assert( pPager->pTmpSpace!=0 );
64357 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
64361 pPgHdr->flags |= PGHDR_NEED_SYNC;
64377 assert( pPg->pgno!=iNew );
64378 pPg->flags = flags;
64386 assert( pPg->nRef>0 || pPg->pPager->memDb );
64387 return pPg->pData;
64395 return pPg->pExtra;
64399 ** Get/set the locking-mode for this pager. Parameter eMode must be one
64402 ** the locking-mode is set to the value specified.
64406 ** locking-mode.
64414 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
64415 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
64416 pPager->exclusiveMode = (u8)eMode;
64418 return (int)pPager->exclusiveMode;
64422 ** Set the journal-mode for this pager. Parameter eMode must be one of:
64434 ** * An in-memory database can only have its journal_mode set to _OFF
64439 ** The returned indicate the current (possibly updated) journal-mode.
64442 u8 eOld = pPager->journalMode; /* Prior journalmode */
64456 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
64458 /* Do allow the journalmode of an in-memory database to be set to
64471 assert( pPager->eState!=PAGER_ERROR );
64472 pPager->journalMode = (u8)eMode;
64485 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
64486 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
64495 sqlite3OsClose(pPager->jfd);
64496 if( pPager->eLock>=RESERVED_LOCK ){
64497 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
64500 int state = pPager->eState;
64505 if( pPager->eState==PAGER_READER ){
64510 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
64517 assert( state==pPager->eState );
64520 sqlite3OsClose(pPager->jfd);
64525 return (int)pPager->journalMode;
64532 return (int)pPager->journalMode;
64542 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
64543 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
64548 ** Get/set the size-limit used for persistent journal files.
64550 ** Setting the size limit to -1 means no limit is enforced.
64551 ** An attempt to set a limit smaller than -1 is a no-op.
64554 if( iLimit>=-1 ){
64555 pPager->journalSizeLimit = iLimit;
64556 sqlite3WalLimit(pPager->pWal, iLimit);
64558 return pPager->journalSizeLimit;
64562 ** Return a pointer to the pPager->pBackup variable. The backup module
64568 return &pPager->pBackup;
64573 ** Unless this is an in-memory or temporary database, clear the pager cache.
64576 assert( MEMDB==0 || pPager->tempFile );
64577 if( pPager->tempFile==0 ) pager_reset(pPager);
64598 if( pPager->pWal==0 && pPager->journalMode==PAGER_JOURNALMODE_WAL ){
64610 if( pPager->pWal ){
64611 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
64612 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
64613 pPager->pBusyHandlerArg,
64614 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
64622 return sqlite3WalCallback(pPager->pWal);
64627 ** primitives necessary for write-ahead logging.
64630 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
64631 if( pPager->noLock ) return 0;
64632 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
64643 assert( pPager->eLock>=SHARED_LOCK );
64644 eOrigLock = pPager->eLock;
64657 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
64658 ** lock on the database file and use heap-memory to store the wal-index
64659 ** in. Otherwise, use the normal shared-memory.
64664 assert( pPager->pWal==0 && pPager->tempFile==0 );
64665 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
64667 /* If the pager is already in exclusive-mode, the WAL module will use
64668 ** heap-memory for the wal-index instead of the VFS shared-memory
64672 if( pPager->exclusiveMode ){
64680 rc = sqlite3WalOpen(pPager->pVfs,
64681 pPager->fd, pPager->zWal, pPager->exclusiveMode,
64682 pPager->journalSizeLimit, &pPager->pWal
64696 ** file (not a temp file or an in-memory database), and the WAL file
64702 ** If the pager is open on a temp-file (or in-memory database), or if
64708 int *pbOpen /* OUT: Set to true if call is a no-op */
64713 assert( pPager->eState==PAGER_OPEN || pbOpen );
64714 assert( pPager->eState==PAGER_READER || !pbOpen );
64716 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
64718 if( !pPager->tempFile && !pPager->pWal ){
64722 sqlite3OsClose(pPager->jfd);
64726 pPager->journalMode = PAGER_JOURNALMODE_WAL;
64727 pPager->eState = PAGER_OPEN;
64748 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
64750 /* If the log file is not already open, but does exist in the file-system,
64754 if( !pPager->pWal ){
64759 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
64768 ** the database file, the log and log-summary files will be deleted.
64770 if( rc==SQLITE_OK && pPager->pWal ){
64773 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
64774 pPager->pageSize, (u8*)pPager->pTmpSpace);
64775 pPager->pWal = 0;
64777 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
64785 ** If pager pPager is a wal-mode database not in exclusive locking mode,
64792 if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){
64793 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
64804 sqlite3WalDb(pPager->pWal, db);
64816 if( pPager->pWal ){
64817 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
64832 if( pPager->pWal ){
64833 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
64846 if( pPager->pWal ){
64847 rc = sqlite3WalSnapshotRecover(pPager->pWal);
64868 if( pPager->pWal ){
64869 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
64881 assert( pPager->pWal );
64882 sqlite3WalSnapshotUnlock(pPager->pWal);
64890 ** A read-lock must be held on the pager when this function is called. If
64897 assert( pPager->eState>=PAGER_READER );
64898 return sqlite3WalFramesize(pPager->pWal);
64904 return sqlite3WalSystemErrno(pPager->pWal);
64924 ** This file contains the implementation of a write-ahead log (WAL) used in
64927 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
64946 ** big-endian 32-bit unsigned integer values:
64952 ** 16: Salt-1, random integer incremented with each checkpoint
64953 ** 20: Salt-2, a different random integer changing with each ckpt
64954 ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
64955 ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
64957 ** Immediately following the wal-header are zero or more frames. Each
64958 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
64959 ** of page data. The frame-header is six big-endian 32-bit unsigned
64965 ** 8: Salt-1 (copied from the header)
64966 ** 12: Salt-2 (copied from the header)
64967 ** 16: Checksum-1.
64968 ** 20: Checksum-2.
64973 ** (1) The salt-1 and salt-2 values in the frame-header match
64974 ** salt values in the wal-header
64976 ** (2) The checksum values in the final 8 bytes of the frame-header
64981 ** The checksum is computed using 32-bit big-endian integers if the
64983 ** is computed using little-endian if the magic number is 0x377f0682.
64985 ** big-endian format regardless of which byte order is used to compute
64987 ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
64990 ** for i from 0 to n-1 step 2:
64997 ** of the sequence being summed.) The s1 value spans all 32-bit
65000 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
65001 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
65002 ** The VFS.xSync operations serve as write barriers - all writes launched
65006 ** After each checkpoint, the salt-1 value is incremented and the salt-2
65035 ** data structure called the wal-index is maintained to expedite the
65038 ** WAL-INDEX FORMAT
65040 ** Conceptually, the wal-index is shared memory, though VFS implementations
65041 ** might choose to implement the wal-index using a mmapped file. Because
65042 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
65046 ** In the default unix and windows implementation, the wal-index is a mmapped
65047 ** file whose name is the database name with a "-shm" suffix added. For that
65048 ** reason, the wal-index is sometimes called the "shm" file.
65050 ** The wal-index is transient. After a crash, the wal-index can (and should
65052 ** to either truncate or zero the header of the wal-index when the last
65053 ** connection to it closes. Because the wal-index is transient, it can
65054 ** use an architecture-specific format; it does not have to be cross-platform.
65056 ** as big endian, the wal-index can store multi-byte values in the native
65059 ** The purpose of the wal-index is to answer this question quickly: Given
65064 ** The wal-index consists of a header region, followed by an one or
65067 ** The wal-index header contains the total number of frames within the WAL
65073 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
65075 ** wal-index. The values are:
65080 ** Each index block contains two sections, a page-mapping that contains the
65081 ** database page number associated with each wal frame, and a hash-table
65083 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
65084 ** for the first index block) 32-bit page numbers. The first entry in the
65085 ** first index-block contains the database page number corresponding to the
65090 ** The last index block in a wal-index usually contains less than the full
65091 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
65093 ** allocated size of the page-mapping array - the page-mapping array merely
65097 ** can be found by scanning the page-mapping sections of each index block
65103 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
65108 ** 1-based index of an entry in the mapping section of the same
65109 ** index block. Let K be the 1-based index of the largest entry in
65124 ** wrap-around.) Because the hash table is never more than half full,
65129 ** current index block. Otherwise the iMax-th mapping entry of the
65147 ** and to the wal-index) might be using a different value K1, where K1>K0.
65153 ** in the first place - which is what reader one wants. Meanwhile, the
65176 ** The maximum (and only) versions of the wal and wal-index formats
65180 ** values in the wal-header are correct and (b) the version field is not
65183 ** Similarly, if a client successfully reads a wal-index header (i.e. the
65185 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
65200 ** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
65210 #define WAL_NREADER (SQLITE_SHM_NLOCK-3)
65220 ** The following object holds a copy of the wal-index header content.
65222 ** The actual header in the wal-index consists of two copies of this
65229 ** Or it can be 1 to represent a 65536-byte page. The latter case was
65233 u32 iVersion; /* Wal-index version */
65237 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
65247 ** A copy of the following object occurs in the wal-index immediately
65270 ** holds read-lock K, then the value in aReadMark[K] is no greater than
65273 ** a special case; its value is never used and it exists as a place-holder
65302 ** We assume that 32-bit loads are atomic and so no locks are needed in
65315 ** This is a schematic view of the complete 136-byte header of the
65316 ** wal-index file (also known as the -shm file):
65318 ** +-----------------------------+
65320 ** +-----------------------------+ |
65322 ** +-----------------------------+ |
65324 ** +-------+-------+-------------+ |
65326 ** +-------+-------+-------------+ |
65328 ** +-----------------------------+ | WalIndexHdr object
65330 ** +-----------------------------+ |
65333 ** +-----------------------------+ |
65336 ** +-----------------------------+ |
65339 ** +-----------------------------+
65341 ** +-----------------------------+ |
65343 ** +-----------------------------+ |
65345 ** +-------+-------+-------------+ |
65347 ** +-------+-------+-------------+ | Second copy of the
65349 ** +-----------------------------+ |
65351 ** +-----------------------------+ |
65354 ** +-----------------------------+ |
65357 ** +-----------------------------+ |
65360 ** +-----------------------------+
65362 ** +-----------------------------+
65368 ** +-------+-------+------+------+
65370 ** +-------+-------+------+------+ ) 8 lock bytes
65372 ** +-------+-------+------+------+
65374 ** +-----------------------------+
65376 ** +-----------------------------+
65381 ** only support mandatory file-locks, we do not read or write data
65394 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
65395 ** big-endian format in the first 4 bytes of a WAL file.
65398 ** file are calculated by treating all data as an array of 32-bit
65399 ** big-endian words. Otherwise, they are calculated by interpreting
65400 ** all data as 32-bit little-endian words.
65405 ** Return the offset of frame iFrame in the write-ahead log file,
65407 ** is to the start of the write-ahead log frame-header.
65410 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
65414 ** An open write-ahead log file is represented by an instance of the
65425 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
65427 i16 readLock; /* Which read lock is being held. -1 for none */
65429 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
65436 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
65437 WalIndexHdr hdr; /* Wal-index header for current transaction */
65441 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
65476 ** Each page of the wal-index mapping contains a hash-table made up of
65490 ** walIteratorInit() - Create a new iterator,
65491 ** walIteratorNext() - Step an iterator,
65492 ** walIteratorFree() - Free an iterator.
65505 } aSegment[1]; /* One for every 32KB page in the wal-index */
65509 ** Define the parameters of the hash tables in the wal-index file. There
65510 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
65511 ** wal-index.
65513 ** Changing any of these constants will alter the wal-index format and
65521 ** The block of page numbers associated with the first hash-table in a
65522 ** wal-index is smaller than usual. This is so that there is a complete
65523 ** hash-table on each aligned 32KB page of the wal-index.
65525 #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
65527 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
65533 ** Structured Exception Handling (SEH) is a Windows-specific technique
65534 ** for catching exceptions raised while accessing memory-mapped files.
65536 ** The -DSQLITE_USE_SEH compile-time option means to use SEH to catch and
65537 ** deal with system-level errors that arise during WAL -shm file processing.
65538 ** Without this compile-time option, any system-level faults that appear
65539 ** while accessing the memory-mapped -shm file will cause a process-wide
65548 assert( walAssertLockmask(pWal) && pWal->nSehTry==0 ); \
65549 VVA_ONLY(pWal->nSehTry++);
65553 VVA_ONLY(pWal->nSehTry--); \
65554 assert( pWal->nSehTry==0 ); \
65557 /* Simulate a memory-mapping fault in the -shm file for testing purposes */
65563 ** indicates that the exception may have been caused by accessing the *-shm
65567 VVA_ONLY(pWal->nSehTry--);
65569 if( p && p->ExceptionRecord && p->ExceptionRecord->NumberParameters>=3 ){
65571 ** ExceptionInformation[] array is a read-write flag - 0 if the exception
65575 pWal->iSysErrno = (int)p->ExceptionRecord->ExceptionInformation[2];
65585 ** thrown by the system if the *-shm file mapping is accessed after it
65590 assert( pWal->nSehTry>0 );
65616 assert( (X==0 || Y==0) && pWal->pFree==X ); pWal->pFree = Y
65619 ** There are two ways to use this macro. To arrange for pWal->apWiData[iPg]
65628 #define SEH_SET_ON_ERROR(X,Y) pWal->iWiPg = X; pWal->pWiValue = Y
65631 # define SEH_TRY VVA_ONLY(pWal->nSehTry++);
65632 # define SEH_EXCEPT(X) VVA_ONLY(pWal->nSehTry--); assert( pWal->nSehTry==0 );
65633 # define SEH_INJECT_FAULT assert( pWal->nSehTry>0 );
65640 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
65641 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
65644 ** If the wal-index is currently smaller the iPage pages then the size
65645 ** of the wal-index might be increased, but only if it is safe to do
65646 ** so. It is safe to enlarge the wal-index if pWal->writeLock is true
65647 ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
65651 ** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page
65655 ** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
65664 /* Enlarge the pWal->apWiData[] array if required */
65665 if( pWal->nWiData<=iPage ){
65668 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
65673 memset((void*)&apNew[pWal->nWiData], 0,
65674 sizeof(u32*)*(iPage+1-pWal->nWiData));
65675 pWal->apWiData = apNew;
65676 pWal->nWiData = iPage+1;
65680 assert( pWal->apWiData[iPage]==0 );
65681 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
65682 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
65683 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
65685 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
65686 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
65688 assert( pWal->apWiData[iPage]!=0
65690 || (pWal->writeLock==0 && iPage==0) );
65691 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
65695 pWal->readOnly |= WAL_SHM_RDONLY;
65702 *ppPage = pWal->apWiData[iPage];
65712 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
65719 ** Return a pointer to the WalCkptInfo structure in the wal-index.
65722 assert( pWal->nWiData>0 && pWal->apWiData[0] );
65724 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
65728 ** Return a pointer to the WalIndexHdr structure in the wal-index.
65731 assert( pWal->nWiData>0 && pWal->apWiData[0] );
65733 return (volatile WalIndexHdr*)pWal->apWiData[0];
65737 ** The argument to this macro must be of type u32. On a little-endian
65739 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
65741 ** of the input value as a little-endian integer.
65758 int nativeCksum, /* True for native byte-order, false for non-native */
65822 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
65823 sqlite3OsShmBarrier(pWal->pDbFd);
65828 ** Add the SQLITE_NO_TSAN as part of the return-type of a function
65830 ** might give false-positive TSAN warnings.
65832 ** See tag-20200519-1.
65841 ** Write the header information in pWal->hdr into the wal-index.
65843 ** The checksum on pWal->hdr is updated before it is written.
65849 assert( pWal->writeLock );
65850 pWal->hdr.isInit = 1;
65851 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
65852 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
65853 /* Possible TSAN false-positive. See tag-20200519-1 */
65854 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
65856 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
65861 ** supplied by the caller. A frame-header is made up of a series of
65862 ** 4-byte big-endian integers, as follows:
65867 ** 8: Salt-1 (copied from the wal-header)
65868 ** 12: Salt-2 (copied from the wal-header)
65869 ** 16: Checksum-1.
65870 ** 20: Checksum-2.
65873 Wal *pWal, /* The write-ahead log */
65875 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
65879 int nativeCksum; /* True for native byte-order checksums */
65880 u32 *aCksum = pWal->hdr.aFrameCksum;
65884 if( pWal->iReCksum==0 ){
65885 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
65887 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
65889 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
65904 Wal *pWal, /* The write-ahead log */
65910 int nativeCksum; /* True for native byte-order checksums */
65911 u32 *aCksum = pWal->hdr.aFrameCksum;
65915 /* A frame is only valid if the salt values in the frame-header
65916 ** match the salt values in the wal-header.
65918 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
65930 ** all prior frames, the first 16 bytes of this frame-header,
65931 ** and the frame-data matches the checksum in the last 8
65932 ** bytes of this frame-header.
65934 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
65936 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
65960 return "WRITE-LOCK";
65962 return "CKPT-LOCK";
65964 return "RECOVER-LOCK";
65967 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
65968 lockIdx-WAL_READ_LOCK(0));
65977 ** A lock cannot be moved directly between shared and exclusive - it must go
65980 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
65984 if( pWal->exclusiveMode ) return SQLITE_OK;
65985 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
65987 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
65989 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
65991 if( rc==SQLITE_OK ) pWal->lockMask |= (1 << lockIdx);
65996 if( pWal->exclusiveMode ) return;
65997 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
66000 pWal->lockMask &= ~(1 << lockIdx);
66002 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
66006 if( pWal->exclusiveMode ) return SQLITE_OK;
66007 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
66009 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
66011 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
66014 pWal->lockMask |= (((1<<n)-1) << (SQLITE_SHM_NLOCK+lockIdx));
66020 if( pWal->exclusiveMode ) return;
66021 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
66024 pWal->lockMask &= ~(((1<<n)-1) << (SQLITE_SHM_NLOCK+lockIdx));
66026 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
66032 ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
66037 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
66038 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
66041 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
66046 ** of a page hash table in the wal-index. This becomes the return value
66051 volatile ht_slot *aHash; /* Start of the wal-index hash table */
66058 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
66061 ** Set output variable pLoc->aHash to point to the start of the hash table
66062 ** in the wal-index file. Set pLoc->iZero to one less than the frame
66065 ** (pLoc->iZero+N) in the log.
66067 ** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
66068 ** first frame indexed by the hash table, frame (pLoc->iZero).
66077 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
66080 if( pLoc->aPgno ){
66081 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
66083 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
66084 pLoc->iZero = 0;
66086 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
66095 ** Return the number of the wal-index page that contains the hash-table
66096 ** and page-number array that contain entries corresponding to WAL frame
66097 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
66101 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
66119 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
66121 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
66126 ** than pWal->hdr.mxFrame.
66128 ** This function is called whenever pWal->hdr.mxFrame is decreased due
66131 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
66133 ** pWal->hdr.mxFrame advances to the point where those hash tables are
66142 assert( pWal->writeLock );
66143 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
66144 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
66145 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
66147 if( pWal->hdr.mxFrame==0 ) return;
66149 /* Obtain pointers to the hash-table and page-number array containing
66150 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
66151 ** that the page said hash-table and array reside on is already mapped.(1)
66153 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
66154 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
66155 i = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
66156 if( NEVER(i) ) return; /* Defense-in-depth, in case (1) above is wrong */
66158 /* Zero all hash-table entries that correspond to frame numbers greater
66159 ** than pWal->hdr.mxFrame.
66161 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
66170 ** frame numbers greater than pWal->hdr.mxFrame.
66172 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]);
66195 ** Set an entry in the wal-index that will map database page number
66200 WalHashLoc sLoc; /* Wal-index hash table location */
66204 /* Assuming the wal-index file was successfully mapped, populate the
66209 int idx; /* Value to write to hash-table slot */
66212 idx = iFrame - sLoc.iZero;
66215 /* If this is the first entry to be added to this hash-table, zero the
66219 int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT] - (u8*)sLoc.aPgno);
66228 ** the hash-table before writing any new entries.
66230 if( sLoc.aPgno[idx-1] ){
66232 assert( !sLoc.aPgno[idx-1] );
66235 /* Write the aPgno[] array entry and the hash-table slot. */
66238 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
66240 sLoc.aPgno[idx-1] = iPage;
66256 ** thing to check, so only do this occasionally - not on every
66278 ** Recover the wal-index by reading the write-ahead log file.
66281 ** wal-index to prevent other threads/processes from doing anything
66282 ** with the WAL or wal-index while recovery is running. The
66299 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
66302 assert( pWal->writeLock );
66303 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
66304 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
66311 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
66313 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
66320 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
66328 u32 iPg; /* Current 32KB wal-index page */
66332 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
66345 || szPage&(szPage-1)
66351 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
66352 pWal->szPage = szPage;
66353 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
66354 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
66357 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
66358 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
66360 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
66361 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
66386 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
66391 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE);
66397 pWal->apWiData[iPg] = aPrivate;
66405 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
66412 /* If nTruncate is non-zero, this is a commit record. */
66414 pWal->hdr.mxFrame = iFrame;
66415 pWal->hdr.nPage = nTruncate;
66416 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
66419 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
66420 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
66423 pWal->apWiData[iPg] = aShare;
66436 memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr);
66467 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
66468 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
66471 /* Reset the checkpoint-header. This is safe because this thread is
66473 ** checkpointers. Then set the values of read-mark slots 1 through N.
66476 pInfo->nBackfill = 0;
66477 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
66478 pInfo->aReadMark[0] = 0;
66482 if( i==1 && pWal->hdr.mxFrame ){
66483 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
66485 pInfo->aReadMark[i] = READMARK_NOT_USED;
66499 if( pWal->hdr.nPage ){
66502 pWal->hdr.mxFrame, pWal->zWalName
66509 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
66514 ** Close an open wal-index.
66517 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
66519 for(i=0; i<pWal->nWiData; i++){
66520 sqlite3_free((void *)pWal->apWiData[i]);
66521 pWal->apWiData[i] = 0;
66524 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
66525 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
66536 ** client from unlinking the WAL or wal-index file. If another process
66545 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
66548 int bNoShm, /* True to run in heap-memory mode */
66560 ** of these constants would result in an incompatible on-disk format
66561 ** for the -shm file. Any change that causes one of these asserts to
66565 ** This table also serves as a helpful cross-reference when trying to
66566 ** interpret hex dumps of the -shm file.
66606 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
66611 pRet->pVfs = pVfs;
66612 pRet->pWalFd = (sqlite3_file *)&pRet[1];
66613 pRet->pDbFd = pDbFd;
66614 pRet->readLock = -1;
66615 pRet->mxWalSize = mxWalSize;
66616 pRet->zWalName = zWalName;
66617 pRet->syncHeader = 1;
66618 pRet->padToSectorBoundary = 1;
66619 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
66621 /* Open file handle on the write-ahead log file. */
66623 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
66625 pRet->readOnly = WAL_RDONLY;
66630 sqlite3OsClose(pRet->pWalFd);
66634 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
66636 pRet->padToSectorBoundary = 0;
66648 if( pWal ) pWal->mxWalSize = iLimit;
66670 iMin = p->iPrior;
66672 for(i=p->nSegment-1; i>=0; i--){
66673 struct WalSegment *pSegment = &p->aSegment[i];
66674 while( pSegment->iNext<pSegment->nEntry ){
66675 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
66679 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
66683 pSegment->iNext++;
66687 *piPage = p->iPrior = iRet;
66715 const u32 *aContent, /* Pages in wal - keys for the sort */
66779 ht_slot *aList; /* Pointer to sub-list content */
66787 struct Sublist aSub[13]; /* Array of sub-lists */
66791 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
66800 assert( p->aList && p->nList<=(1<<iSub) );
66801 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
66802 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
66813 assert( p->nList<=(1<<iSub) );
66814 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
66815 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
66825 assert( aContent[aList[i]] > aContent[aList[i-1]] );
66841 ** nBackfill or earlier may be included - excluding them is an optimization
66857 ht_slot *aTmp; /* Temp space used by merge-sort */
66863 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
66864 iLast = pWal->hdr.mxFrame;
66869 + (nSegment-1)*sizeof(struct WalSegment)
66878 p->nSegment = nSegment;
66891 nEntry = (int)(iLast - sLoc.iZero);
66893 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
66895 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
66902 p->aSegment[i].iZero = sLoc.iZero;
66903 p->aSegment[i].nEntry = nEntry;
66904 p->aSegment[i].aIndex = aIndex;
66905 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
66926 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&nMs
66934 ** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
66939 if( pWal->db ){
66940 int tmout = pWal->db->busyTimeout;
66953 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout);
66967 assert( pWal->readLock<0 || bLock==0 );
66969 assert( pWal->db );
66973 pWal->writeLock = 1;
66977 }else if( pWal->writeLock ){
66979 pWal->writeLock = 0;
66988 pWal->db = db;
67002 ** busy-handler function. Invoke it and retry the lock until either the
67003 ** lock is successfully obtained or the busy-handler returns 0.
67026 ** The cache of the wal-index header must be valid to call this function.
67027 ** Return the page-size in bytes used by the database.
67030 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
67039 ** file - there are no readers that may attempt to read a frame from
67042 ** This function updates the shared-memory structures so that the next
67047 ** new wal-index header. It should be passed a pseudo-random value (i.e.
67053 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
67054 pWal->nCkpt++;
67055 pWal->hdr.mxFrame = 0;
67057 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
67059 AtomicStore(&pInfo->nBackfill, 0);
67060 pInfo->nBackfillAttempted = 0;
67061 pInfo->aReadMark[1] = 0;
67062 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
67063 assert( pInfo->aReadMark[0]==0 );
67075 ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
67081 ** in the WAL and can be recovered following a power-loss or hard reset.
67088 ** This routine uses and updates the nBackfill field of the wal-index header.
67107 int szPage; /* Database page-size */
67120 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
67122 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
67131 mxSafeFrame = pWal->hdr.mxFrame;
67132 mxPage = pWal->hdr.nPage;
67134 u32 y = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT;
67136 assert( y<=pWal->hdr.mxFrame );
67140 AtomicStore(pInfo->aReadMark+i, iMark); SEH_INJECT_FAULT;
67152 if( pInfo->nBackfill<mxSafeFrame ){
67153 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
67160 u32 nBackfill = pInfo->nBackfill;
67161 pInfo->nBackfillAttempted = mxSafeFrame; SEH_INJECT_FAULT;
67164 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
67172 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0);
67173 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
67175 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
67178 ** maximum size of the pending-byte page (65536 bytes), then
67182 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT,&nReq);
67193 if( AtomicLoad(&db->u1.isInterrupted) ){
67194 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
67202 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
67204 iOffset = (iDbpage-1)*(i64)szPage;
67206 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
67209 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0);
67213 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
67214 i64 szDb = pWal->hdr.nPage*(i64)szPage;
67216 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
67218 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
67222 AtomicStore(&pInfo->nBackfill, mxSafeFrame); SEH_INJECT_FAULT;
67243 assert( pWal->writeLock );
67245 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
67250 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
67251 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
67254 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
67260 ** wal-index header in shared memory, as all subsequent reader or
67264 ** the wal-index header do not match the contents of the
67265 ** file-system. To avoid this, update the wal-index header to
67268 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
67270 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
67289 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
67291 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
67295 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
67302 ** an exception thrown by reading from the *-shm mapping after it has become
67317 ** 3) Set pWal->apWiData[pWal->iWiPg] to pWal->pWiValue if not NULL
67322 if( pWal->exclusiveMode==0 ){
67326 u32 mUnlock = pWal->lockMask & ~(
67327 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
67328 | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
67329 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
67336 sqlite3_free(pWal->pFree);
67337 pWal->pFree = 0;
67338 if( pWal->pWiValue ){
67339 pWal->apWiData[pWal->iWiPg] = pWal->pWiValue;
67340 pWal->pWiValue = 0;
67353 if( pWal->exclusiveMode==0 ){
67357 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
67358 | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
67359 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
67361 | (pWal->pSnapshot ? (pWal->lockMask & (1 << WAL_CKPT_LOCK)) : 0)
67364 assert( mExpect==pWal->lockMask );
67376 iRet = pWal->iSysErrno;
67377 pWal->iSysErrno = 0;
67398 int isDelete = 0; /* True to unlink wal and wal-index files */
67403 ** ordinary, rollback-mode locking methods, this guarantees that the
67406 ** the wal and wal-index files.
67411 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
67413 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
67414 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
67420 int bPersist = -1;
67422 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
67426 ** fsynced (rc==SQLITE_OK) and if we are not in persistent-wal
67429 }else if( pWal->mxWalSize>=0 ){
67433 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
67442 sqlite3OsClose(pWal->pWalFd);
67445 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
67449 sqlite3_free((void *)pWal->apWiData);
67456 ** Try to read the wal-index header. Return 0 on success and 1 if
67459 ** The wal-index is in shared memory. Another thread or process might
67466 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
67469 ** If the checksum cannot be verified return non-zero. If the header
67477 /* The first page of the wal-index must be mapped at this point. */
67478 assert( pWal->nWiData>0 && pWal->apWiData[0] );
67483 ** from the file. If this happens, return non-zero.
67485 ** tag-20200519-1:
67486 ** There are two copies of the header at the beginning of the wal-index.
67490 ** give false-positive warnings about these accesses because the tools do not
67491 ** account for the double-read and the memory barrier. The use of mutexes
67497 memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
67505 return 1; /* Malformed header - probably all zeros */
67507 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
67512 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
67514 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
67515 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
67516 testcase( pWal->szPage<=32768 );
67517 testcase( pWal->szPage>=65536 );
67528 #define WAL_RETRY (-1)
67531 ** Read the wal-index header from the wal-index and into pWal->hdr.
67532 ** If the wal-header appears to be corrupt, try to reconstruct the
67533 ** wal-index from the WAL before returning.
67535 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
67536 ** changed by this operation. If pWal->hdr is unchanged, set *pChanged
67539 ** If the wal-index header is successfully read, return SQLITE_OK.
67545 volatile u32 *page0; /* Chunk of wal-index containing header */
67547 /* Ensure that page 0 of the wal-index (the page that contains the
67548 ** wal-index header) is mapped. Return early if an error occurs here.
67555 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
67557 ** confirm that another write-capable connection has the shared-memory
67558 ** open, and hence the content of the shared-memory is unreliable,
67559 ** since the shared-memory might be inconsistent with the WAL file
67562 assert( pWal->writeLock==0 );
67563 assert( pWal->readOnly & WAL_SHM_RDONLY );
67564 pWal->bShmUnreliable = 1;
67565 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
67568 return rc; /* Any other non-OK return is just an error */
67571 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
67575 assert( page0!=0 || pWal->writeLock==0 );
67577 /* If the first page of the wal-index has been mapped, try to read the
67578 ** wal-index header immediately, without holding any lock. This usually
67579 ** works, but may fail if the wal-index header is corrupt or currently
67588 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
67594 int bWriteLock = pWal->writeLock;
67598 pWal->writeLock = 1;
67602 /* If the wal-index header is still malformed even while holding
67612 pWal->writeLock = 0;
67620 ** sure the wal-index was not constructed with some future format that
67623 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
67626 if( pWal->bShmUnreliable ){
67629 pWal->bShmUnreliable = 0;
67630 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
67636 pWal->exclusiveMode = WAL_NORMAL_MODE;
67643 ** Open a transaction in a connection where the shared-memory is read-only
67644 ** and where we cannot verify that there is a separate write-capable connection
67645 ** on hand to keep the shared-memory up-to-date with the WAL file.
67647 ** This can happen, for example, when the shared-memory is implemented by
67648 ** memory-mapping a *-shm file, where a prior writer has shut down and
67649 ** left the *-shm file on disk, and now the present connection is trying
67650 ** to use that database but lacks write permission on the *-shm file.
67655 ** The *-wal file has been read and an appropriate wal-index has been
67656 ** constructed in pWal->apWiData[] using heap memory instead of shared
67663 ** WAL_RETRY, then the heap memory wal-index has been discarded and
67665 ** beginning (including attempting to map the *-shm file).
67678 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
67680 assert( pWal->bShmUnreliable );
67681 assert( pWal->readOnly & WAL_SHM_RDONLY );
67682 assert( pWal->nWiData>0 && pWal->apWiData[0] );
67692 pWal->readLock = 0;
67694 /* Check to see if a separate writer has attached to the shared-memory area,
67695 ** thus making the shared-memory "reliable" again. Do this by invoking
67699 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
67700 ** cause the heap-memory WAL-index to be discarded and the actual
67712 ** even if some external agent does a "chmod" to make the shared-memory
67716 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
67717 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
67723 /* We reach this point only if the real shared-memory is still unreliable.
67724 ** Assume the in-memory WAL-index substitute is correct and load it
67725 ** into pWal->hdr.
67727 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
67732 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
67737 /* If the wal file is too small to contain a wal-header and the
67738 ** wal-index header has mxFrame==0, then it must be safe to proceed
67744 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
67749 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
67753 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
67755 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
67762 assert( (pWal->szPage & (pWal->szPage-1))==0 );
67763 assert( pWal->szPage>=512 && pWal->szPage<=65536 );
67764 szFrame = pWal->szPage + WAL_FRAME_HDRSIZE;
67773 ** wal file since the heap-memory wal-index was created. If so, the
67774 ** heap-memory wal-index is discarded and WAL_RETRY returned to
67776 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
67777 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
67778 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage);
67786 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
67790 /* If nTruncate is non-zero, then a complete transaction has been
67798 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
67799 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
67805 for(i=0; i<pWal->nWiData; i++){
67806 sqlite3_free((void*)pWal->apWiData[i]);
67807 pWal->apWiData[i] = 0;
67809 pWal->bShmUnreliable = 0;
67829 ** it reaches WAL_RETRY_PROTOCOL_LIMIT - indicating that walTryBeginRead()
67830 ** has many times been invoked and failed with WAL_RETRY - walTryBeginRead()
67859 ** to make a copy of the wal-index header into pWal->hdr. If the
67860 ** wal-index header has changed, *pChanged is set to 1 (as an indication
67862 ** flushed.) When useWal==1, the wal-index header is assumed to already
67873 ** bad luck when there is lots of contention for the wal-index, but that
67877 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
67878 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
67881 ** including frame number aReadMark[pWal->readLock]. The reader will
67882 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
67883 ** Or if pWal->readLock==0, then the reader will ignore the WAL
67886 ** this routine will always set pWal->readLock>0 on success.
67888 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
67898 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
67908 assert( pWal->readLock<0 ); /* Not currently locked */
67911 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
67919 ** paged out or take a page-fault that is time-consuming to resolve,
67935 VVA_ONLY( pWal->lockError = 1; )
67938 if( *pCnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
67940 /* In SQLITE_ENABLE_SETLK_TIMEOUT builds, configure the file-descriptor
67945 ** read-mark.
67956 sqlite3OsSleep(pWal->pVfs, nDelay);
67962 if( pWal->bShmUnreliable==0 ){
67981 if( pWal->apWiData[0]==0 ){
67986 ** code that determines whether or not the shared-memory region
68000 else if( pWal->bShmUnreliable ){
68005 assert( pWal->nWiData>0 );
68006 assert( pWal->apWiData[0]!=0 );
68009 if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
68011 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
68020 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
68037 pWal->readLock = 0;
68047 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
68051 mxFrame = pWal->hdr.mxFrame;
68053 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
68054 mxFrame = pWal->pSnapshot->mxFrame;
68058 u32 thisMark = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT;
68065 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
68071 AtomicStore(pInfo->aReadMark+i,mxFrame);
68082 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
68100 /* Now that the read-lock has been obtained, check that neither the
68101 ** value in the aReadMark[] array or the contents of the wal-index
68104 ** It is necessary to check that the wal-index header did not change
68105 ** between the time it was read and when the shared-lock was obtained
68108 ** that occur later in the log than pWal->hdr.mxFrame may have been
68111 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
68114 ** Before checking that the live wal-index header has not changed
68117 ** to read any frames earlier than minFrame from the wal file - they
68121 ** nBackfill and checking that the wal-header in shared-memory still
68122 ** matches the one cached in pWal->hdr, it is guaranteed that the
68123 ** checkpointer that set nBackfill was not working with a wal-index
68124 ** header newer than that cached in pWal->hdr. If it were, that could
68126 ** a version of page X that lies before pWal->minFrame (call that version
68129 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
68132 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
68134 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1; SEH_INJECT_FAULT;
68136 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
68137 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
68142 assert( mxReadMark<=pWal->hdr.mxFrame );
68143 pWal->readLock = (i16)mxI;
68154 void *pBuf1, /* Temp buffer pWal->szPage bytes in size */
68155 void *pBuf2 /* Temp buffer pWal->szPage bytes in size */
68157 int szPage = (int)pWal->szPage;
68161 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
68164 u32 i = pInfo->nBackfillAttempted;
68165 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){
68173 assert( i - sLoc.iZero - 1 >=0 );
68174 pgno = sLoc.aPgno[i-sLoc.iZero-1];
68175 iDbOff = (i64)(pgno-1) * szPage;
68179 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
68182 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
68190 pInfo->nBackfillAttempted = i-1;
68205 ** This is only really safe if the file-system is such that any page
68219 assert( pWal->readLock>=0 );
68222 void *pBuf1 = sqlite3_malloc(pWal->szPage);
68223 void *pBuf2 = sqlite3_malloc(pWal->szPage);
68227 pWal->ckptLock = 1;
68232 pWal->ckptLock = 0;
68254 WalIndexHdr *pSnapshot = pWal->pSnapshot;
68257 assert( pWal->ckptLock==0 );
68258 assert( pWal->nSehTry>0 );
68262 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
68270 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
68273 ** before checking pInfo->nBackfillAttempted. */
68295 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
68297 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
68298 ** is populated with the wal-index header corresponding to the head
68306 ** pSnapshot->mxFrame into the database file. Note that the
68311 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
68312 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
68316 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
68317 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
68320 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
68321 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
68323 assert( pWal->readLock>0 );
68324 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
68330 /* A client using a non-current snapshot may not ignore any frames
68336 pWal->minFrame = 1;
68357 ** it takes a snapshot of the state of the WAL and wal-index for the current
68359 ** Other threads might append new content to the WAL and wal-index but
68378 ** read-lock.
68382 if( pWal->readLock>=0 ){
68383 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
68384 pWal->readLock = -1;
68402 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
68407 assert( pWal->readLock>=0 || pWal->lockError );
68409 /* If the "last page" field of the wal-index header snapshot is 0, then
68411 ** in this case as an optimization. Likewise, if pWal->readLock==0,
68415 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
68425 ** that adds entries to the wal-index (and possibly to this hash
68429 ** read transaction was opened may have been written incorrectly -
68436 ** if we had exclusive access to the hash-table:
68439 ** This condition filters out normal hash-table collisions.
68443 ** table after the current read-transaction had started.
68445 iMinHash = walFramePage(pWal->minFrame);
68446 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
68462 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){
68466 if( (nCollide--)==0 ){
68477 ** of the wal-index file content. Make sure the results agree with the
68482 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
68483 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
68534 sz = pWal->hdr.szPage;
68540 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
68547 if( pWal && ALWAYS(pWal->readLock>=0) ){
68548 return pWal->hdr.nPage;
68571 /* If the write-lock is already held, then it was obtained before the
68572 ** read-transaction was even opened, making this call a no-op.
68574 if( pWal->writeLock ){
68575 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
68582 assert( pWal->readLock>=0 );
68583 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
68585 if( pWal->readOnly ){
68596 pWal->writeLock = 1;
68603 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
68611 pWal->writeLock = 0;
68621 if( pWal->writeLock ){
68623 pWal->writeLock = 0;
68624 pWal->iReCksum = 0;
68625 pWal->truncateOnCommit = 0;
68632 ** function moves the write-pointer back to the start of the transaction.
68644 if( ALWAYS(pWal->writeLock) ){
68645 Pgno iMax = pWal->hdr.mxFrame;
68649 /* Restore the clients cache of the wal-index header to the state it
68652 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
68654 for(iFrame=pWal->hdr.mxFrame+1;
68660 ** (b) has an outstanding reference, then xUndo is either a no-op
68672 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
68686 assert( pWal->writeLock );
68687 aWalData[0] = pWal->hdr.mxFrame;
68688 aWalData[1] = pWal->hdr.aFrameCksum[0];
68689 aWalData[2] = pWal->hdr.aFrameCksum[1];
68690 aWalData[3] = pWal->nCkpt;
68702 assert( pWal->writeLock );
68703 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
68705 if( aWalData[3]!=pWal->nCkpt ){
68706 /* This savepoint was opened immediately after the write-transaction
68711 aWalData[3] = pWal->nCkpt;
68714 if( aWalData[0]<pWal->hdr.mxFrame ){
68715 pWal->hdr.mxFrame = aWalData[0];
68716 pWal->hdr.aFrameCksum[0] = aWalData[1];
68717 pWal->hdr.aFrameCksum[1] = aWalData[2];
68732 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
68736 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
68743 if( pWal->readLock==0 ){
68745 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
68746 if( pInfo->nBackfill>0 ){
68749 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
68754 ** wal-index header to reflect this.
68757 ** at this point. But updating the actual wal-index header is also
68761 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
68767 pWal->readLock = -1;
68783 ** the next fsync should occur - passed from sqlite3WalFrames() into
68796 ** Do a sync when crossing the p->iSyncPoint boundary.
68809 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
68810 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
68811 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
68814 iAmt -= iFirstAmt;
68816 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
68817 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
68820 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
68835 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
68836 pData = pPage->pData;
68837 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
68841 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
68854 const int szPage = pWal->szPage;/* Database page size */
68857 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
68867 ** wal-file header. Otherwise, read them from the frame header of the
68869 assert( pWal->iReCksum>0 );
68870 if( pWal->iReCksum==1 ){
68873 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
68875 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
68876 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
68877 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
68879 iRead = pWal->iReCksum;
68880 pWal->iReCksum = 0;
68883 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
68890 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
68899 ** Write a set of frames to the log. The caller must hold the write-lock
68904 int szPage, /* Database page-size in bytes */
68922 assert( pWal->writeLock );
68929 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
68931 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
68936 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
68937 iFirst = pLive->mxFrame+1;
68941 ** log file, instead of appending to it at pWal->hdr.mxFrame.
68951 iFrame = pWal->hdr.mxFrame;
68953 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
68954 u32 aCksum[2]; /* Checksum for wal-header */
68959 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
68960 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
68961 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
68962 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
68966 pWal->szPage = szPage;
68967 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
68968 pWal->hdr.aFrameCksum[0] = aCksum[0];
68969 pWal->hdr.aFrameCksum[1] = aCksum[1];
68970 pWal->truncateOnCommit = 1;
68972 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
68973 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
68980 ** an out-of-order write following a WAL restart could result in
68985 if( pWal->syncHeader ){
68986 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
68990 if( (int)pWal->szPage!=szPage ){
68996 w.pFd = pWal->pWalFd;
69004 for(p=pList; p; p=p->pDirty){
69009 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
69011 if( iFirst && (p->pDirty || isCommit==0) ){
69013 VVA_ONLY(rc =) walFindFrame(pWal, p->pgno, &iWrite);
69018 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
69019 pWal->iReCksum = iWrite;
69021 pData = p->pData;
69022 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
69024 p->flags &= ~PGHDR_WAL_APPEND;
69031 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
69036 p->flags |= PGHDR_WAL_APPEND;
69040 if( isCommit && pWal->iReCksum ){
69061 if( pWal->padToSectorBoundary ){
69062 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
69063 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
69084 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
69085 i64 sz = pWal->mxWalSize;
69086 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
69090 pWal->truncateOnCommit = 0;
69093 /* Append data to the wal-index. It is not necessary to lock the
69094 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
69098 iFrame = pWal->hdr.mxFrame;
69099 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
69100 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
69102 rc = walIndexAppend(pWal, iFrame, p->pgno);
69107 nExtra--;
69108 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
69113 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
69116 pWal->hdr.mxFrame = iFrame;
69118 pWal->hdr.iChange++;
69119 pWal->hdr.nPage = nTruncate;
69121 /* If this is a commit, update the wal-index header too. */
69124 pWal->iCallback = iFrame;
69133 ** Write a set of frames to the log. The caller must hold the write-lock
69141 int szPage, /* Database page-size in bytes */
69162 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
69178 int isChanged = 0; /* True if a new wal-index header is loaded */
69182 assert( pWal->ckptLock==0 );
69183 assert( pWal->writeLock==0 );
69185 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
69189 if( pWal->readOnly ) return SQLITE_READONLY;
69196 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
69198 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
69201 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
69208 pWal->ckptLock = 1;
69210 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
69214 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
69215 ** immediately, and a busy-handler is configured, it is invoked and the
69216 ** writer lock retried until either the busy-handler returns 0 or the
69222 pWal->writeLock = 1;
69232 /* Read the wal-index header. */
69235 /* For a passive checkpoint, do not re-enable blocking locks after
69236 ** reading the wal-index header. A passive checkpoint should not block
69238 ** attempt to obtain is a lock on a read-slot, and it should give up
69243 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
69244 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
69250 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
69258 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
69260 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
69267 /* If a new wal-index header was loaded before the checkpoint was
69268 ** performed, then the pager-cache associated with pWal is now
69269 ** out of date. So zero the cached wal-index header to ensure that
69273 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
69281 if( pWal->ckptLock ){
69283 pWal->ckptLock = 0;
69300 ret = pWal->iCallback;
69301 pWal->iCallback = 0;
69312 ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
69314 ** transition out of exclusive-mode is successful, return 1. This
69319 ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
69321 ** WAL is already in exclusive-locking mode - meaning that this
69322 ** routine is a no-op. The pager must already hold the exclusive lock
69325 ** If op is negative, then do a dry-run of the op==1 case but do
69332 assert( pWal->writeLock==0 );
69333 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
69335 /* pWal->readLock is usually set, but might be -1 if there was a
69336 ** prior error while attempting to acquire are read-lock. This cannot
69339 ** upgrade to exclusive-mode following such an error.
69342 assert( pWal->readLock>=0 || pWal->lockError );
69344 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
69347 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
69348 pWal->exclusiveMode = WAL_NORMAL_MODE;
69349 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
69350 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
69352 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
69358 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
69359 assert( pWal->readLock>=0 );
69360 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
69361 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
69364 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
69370 ** Return true if the argument is non-NULL and the WAL module is using
69371 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
69372 ** WAL module is using shared-memory, return false.
69375 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
69388 assert( pWal->readLock>=0 && pWal->writeLock==0 );
69390 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
69398 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
69405 /* Try to open on pSnapshot when the next read-transaction starts
69411 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
69415 ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
69424 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
69425 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
69426 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
69427 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
69448 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
69449 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
69475 ** read-lock. This function returns the database page-size if it is known,
69479 assert( pWal==0 || pWal->readLock>=0 );
69480 return (pWal ? pWal->szPage : 0);
69487 return pWal->pWalFd;
69524 ** This file implements an external (disk-based) database using BTrees.
69528 ** "Sorting And Searching", pages 473-480. Addison-Wesley
69534 ** ----------------------------------------------------------------
69535 ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
69536 ** ----------------------------------------------------------------
69541 ** on Ptr(N) and its subpages have values greater than Key(N-1). And
69563 ** page, or a pointer-map page.
69582 ** 40 60 15 4-byte meta values passed to higher layers
69587 ** 52 4 Largest root-page (auto/incr_vacuum)
69588 ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
69591 ** 68 4 Application-ID
69593 ** 92 4 The version-valid-for number
69596 ** All of the integer values are big-endian (most significant byte first).
69604 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
69619 ** cell pointer array, and the cell content area. Page 1 also has a 100-byte
69622 ** |----------------|
69624 ** |----------------|
69626 ** |----------------|
69630 ** |----------------|
69633 ** |----------------| ^ Grows upwards
69636 ** |----------------|
69655 ** The cell pointer array contains zero or more 2-byte numbers which are
69686 ** appears first. A variable-length integer may not be more than 9 bytes long.
69688 ** allows a 64-bit integer to be encoded in 9 bytes.
69711 ** filled with data (pagesize - 4 bytes). The last page can have as little
69734 #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
69741 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
69752 ** You can change this value at compile-time by specifying a
69753 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
69754 ** header must be exactly 16 bytes including the zero-terminator so
69766 ** first byte of on-disk image of every BTree page.
69776 ** is derived from the raw on-disk page content.
69783 ** stored in MemPage.pBt->mutex.
69787 u8 intKey; /* True if table b-trees. False for index b-trees */
69800 int nFree; /* Number of free bytes on the page. -1 for unknown */
69803 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
69804 ** non-overflow cell */
69808 u8 *aDataEnd; /* One byte past the end of the entire page - not just
69810 ** corruption-induced buffer overflow. */
69866 u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */
69880 ** If the shared-data extension is enabled, there may be multiple users
69892 # error wrong numeric code for no-transaction
69895 # error wrong numeric code for read-transaction
69898 # error wrong numeric code for write-transaction
69921 ** If a BtShared client fails to obtain a write-lock on a database
69922 ** table (because there exists one or more read-locks on the table),
69923 ** the shared-cache enters 'pending-lock' state and isPending is
69926 ** The shared-cache leaves the 'pending lock' state when either of
69932 ** while in the 'pending-lock' state, no connection may start a new
69935 ** This feature is included to help prevent writer-starvation.
69944 u8 autoVacuum; /* True if auto-vacuum is enabled */
69945 u8 incrVacuum; /* True if incr-vacuum is enabled */
69949 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
69952 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
69953 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
69962 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
69963 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
69967 BtLock *pLock; /* List of locks held on this shared-btree struct */
69983 #define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
69985 #define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
69997 u16 nSize; /* Size of the cell content on the main b-tree page */
70001 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
70004 ** root-node and 3 for all other internal nodes.
70013 ** b-tree within a database file.
70023 ** found at self->pBt->mutex.
70031 ** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
70032 ** sqlite3BtreePrevious() is no-op if skipNext<0.
70057 u8 curIntKey; /* Value of apPage[0]->intKey */
70059 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
70062 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
70088 ** Cursor is valid except that the Cursor.skipNext field is non-zero
70090 ** operation should be a no-op.
70115 #define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/((pBt)->pageSize))+1))
70118 ** These macros define the location of the pointer-map entry for a
70123 ** PTRMAP_PAGENO returns the database page number of the pointer-map
70127 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
70129 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
70133 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
70150 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
70153 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
70161 ** overflow pages. The page-number identifies the previous
70164 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
70177 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
70178 assert( p->pBt->inTransaction>=p->inTrans );
70183 ** if the database supports auto-vacuum or not. Because it is used
70189 #define ISAUTOVACUUM(pBt) (pBt->autoVacuum)
70200 ** the database. As the integrity-check proceeds, for each page used in
70201 ** the database the corresponding bit is set. This allows integrity-check to
70208 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
70220 u32 *heap; /* Min-heap used for analyzing cell coverage */
70226 ** Routines to read or write a two- and four-byte big-endian integer values.
70235 ** two-byte aligned address. get2byteAligned() is only used for accessing the
70254 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
70256 ** p->locked boolean to true.
70259 assert( p->locked==0 );
70260 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
70261 assert( sqlite3_mutex_held(p->db->mutex) );
70263 sqlite3_mutex_enter(p->pBt->mutex);
70264 p->pBt->db = p->db;
70265 p->locked = 1;
70269 ** Release the BtShared mutex associated with B-Tree handle p and
70270 ** clear the p->locked boolean.
70273 BtShared *pBt = p->pBt;
70274 assert( p->locked==1 );
70275 assert( sqlite3_mutex_held(pBt->mutex) );
70276 assert( sqlite3_mutex_held(p->db->mutex) );
70277 assert( p->db==pBt->db );
70279 sqlite3_mutex_leave(pBt->mutex);
70280 p->locked = 0;
70290 ** and this routine is a no-op. The underlying mutex is non-recursive.
70295 ** by all database connections. The p->pNext is a list of other
70298 ** p, then first unlock all of the others on p->pNext, then wait
70307 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
70308 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
70309 assert( p->pNext==0 || p->pNext->db==p->db );
70310 assert( p->pPrev==0 || p->pPrev->db==p->db );
70311 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
70314 assert( !p->locked || p->wantToLock>0 );
70315 assert( p->sharable || p->wantToLock==0 );
70318 assert( sqlite3_mutex_held(p->db->mutex) );
70322 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
70324 if( !p->sharable ) return;
70325 p->wantToLock++;
70326 if( p->locked ) return;
70343 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
70344 p->pBt->db = p->db;
70345 p->locked = 1;
70354 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
70355 assert( pLater->sharable );
70356 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
70357 assert( !pLater->locked || pLater->wantToLock>0 );
70358 if( pLater->locked ){
70363 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
70364 if( pLater->wantToLock ){
70375 assert( sqlite3_mutex_held(p->db->mutex) );
70376 if( p->sharable ){
70377 assert( p->wantToLock>0 );
70378 p->wantToLock--;
70379 if( p->wantToLock==0 ){
70388 ** B-Tree is not marked as sharable.
70393 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
70394 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
70395 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
70396 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
70398 return (p->sharable==0 || p->locked);
70410 ** There is a corresponding leave-all procedures.
70421 assert( sqlite3_mutex_held(db->mutex) );
70422 for(i=0; i<db->nDb; i++){
70423 p = db->aDb[i].pBt;
70424 if( p && p->sharable ){
70429 db->noSharedCache = skipOk;
70432 if( db->noSharedCache==0 ) btreeEnterAll(db);
70437 assert( sqlite3_mutex_held(db->mutex) );
70438 for(i=0; i<db->nDb; i++){
70439 p = db->aDb[i].pBt;
70444 if( db->noSharedCache==0 ) btreeLeaveAll(db);
70456 if( !sqlite3_mutex_held(db->mutex) ){
70459 for(i=0; i<db->nDb; i++){
70461 p = db->aDb[i].pBt;
70462 if( p && p->sharable &&
70463 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
70474 ** db->aDb[iDb].pSchema structure. The mutexes required for schema
70478 ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
70486 if( db->pVfs==0 && db->nDb==0 ) return 1;
70488 assert( iDb>=0 && iDb<db->nDb );
70489 if( !sqlite3_mutex_held(db->mutex) ) return 0;
70491 p = db->aDb[iDb].pBt;
70493 return p->sharable==0 || p->locked==1;
70501 ** these two routines, all mutex operations are no-ops in that case and
70505 ** the ones below, are no-ops and are null #defines in btree.h.
70509 p->pBt->db = p->db;
70513 for(i=0; i<db->nDb; i++){
70514 Btree *p = db->aDb[i].pBt;
70516 p->pBt->db = p->db;
70531 sqlite3BtreeEnter(pCur->pBtree);
70535 sqlite3BtreeLeave(pCur->pBtree);
70555 ** This file implements an external (disk-based) database using BTrees.
70579 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
70584 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
70587 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
70600 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
70645 ** shared-cache table level locks. If the library is compiled with the
70646 ** shared-cache feature disabled, then there is only ever one user
70648 ** So define the lock related functions as no-ops.
70663 u64 n = pBt->nSeek;
70664 pBt->nSeek = 0;
70675 ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
70683 p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
70694 # define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
70697 /* Default value for SHARED_LOCK_TRACE macro if shared-cache is disabled
70701 #define SHARED_LOCK_TRACE(X,MSG,TAB,TYPE) /*no-op*/
70706 /* ^---- Change to 1 and recompile to enable shared-lock tracing
70709 ** Print all shared-cache locks on a BtShared. Debugging use only.
70719 printf("%s-%p %u%s:", zMsg, pBt, iRoot, eLockType==READ_LOCK?"R":"W");
70721 printf("%s-%p:", zMsg, pBt);
70723 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
70724 printf(" %p/%u%s", pLock->pBtree, pLock->iTable,
70725 pLock->eLock==READ_LOCK ? "R" : "W");
70726 while( pLock->pNext && pLock->pBtree==pLock->pNext->pBtree ){
70727 pLock = pLock->pNext;
70728 printf(",%u%s", pLock->iTable, pLock->eLock==READ_LOCK ? "R" : "W");
70736 #endif /* Shared-lock tracing */
70745 ** For example, when writing to a table with root-page iRoot via
70758 ** hold a write-lock on the schema table (root page 1). This is also
70763 Pgno iRoot, /* Root page of b-tree */
70764 int isIndex, /* True if iRoot is the root of an index b-tree */
70767 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
70772 ** and has the read-uncommitted flag set, then no lock is required.
70775 if( (pBtree->sharable==0)
70776 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
70783 ** the correct locks are held. So do not bother - just return true.
70786 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
70790 /* Figure out the root-page that the lock should be held on. For table
70791 ** b-trees, this is just the root page of the b-tree being read or
70792 ** written. For index b-trees, it is the root page of the associated
70797 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
70799 if( pIdx->tnum==iRoot ){
70806 iTab = pIdx->pTable->tnum;
70814 SHARED_LOCK_TRACE(pBtree->pBt,"hasLock",iRoot,eLockType);
70816 /* Search for the required lock. Either a write-lock on root-page iTab, a
70817 ** write-lock on the schema table, or (if the client is reading) a
70818 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
70819 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
70820 if( pLock->pBtree==pBtree
70821 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
70822 && pLock->eLock>=eLockType
70844 ** read-uncommitted flag set, then it is OK for the other object to
70854 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
70855 if( p->pgnoRoot==iRoot
70856 && p->pBtree!=pBtree
70857 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
70868 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
70873 BtShared *pBt = p->pBt;
70878 assert( p->db!=0 );
70879 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
70881 /* If requesting a write-lock, then the Btree must have an open write
70885 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
70886 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
70888 /* This routine is a no-op if the shared-cache is not enabled */
70889 if( !p->sharable ){
70896 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
70897 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
70901 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
70902 /* The condition (pIter->eLock!=eLock) in the following if(...)
70905 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
70911 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
70912 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
70913 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
70914 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
70916 assert( p==pBt->pWriter );
70917 pBt->btsFlags |= BTS_PENDING;
70928 ** Add a lock on the table with root-page iTable to the shared-btree used
70945 BtShared *pBt = p->pBt;
70953 assert( p->db!=0 );
70955 /* A connection with the read-uncommitted flag set will never try to
70956 ** obtain a read-lock using this function. The only read-lock obtained
70957 ** by a connection in read-uncommitted mode is on the sqlite_schema
70959 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
70961 /* This function should only be called on a sharable b-tree after it
70962 ** has been determined that no other b-tree holds a conflicting lock. */
70963 assert( p->sharable );
70967 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
70968 if( pIter->iTable==iTable && pIter->pBtree==p ){
70982 pLock->iTable = iTable;
70983 pLock->pBtree = p;
70984 pLock->pNext = pBt->pLock;
70985 pBt->pLock = pLock;
70989 ** and the requested lock. This means if a write-lock was already held
70990 ** and a read-lock requested, we don't incorrectly downgrade the lock.
70993 if( eLock>pLock->eLock ){
70994 pLock->eLock = eLock;
71011 BtShared *pBt = p->pBt;
71012 BtLock **ppIter = &pBt->pLock;
71015 assert( p->sharable || 0==*ppIter );
71016 assert( p->inTrans>0 );
71022 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
71023 assert( pLock->pBtree->inTrans>=pLock->eLock );
71024 if( pLock->pBtree==p ){
71025 *ppIter = pLock->pNext;
71026 assert( pLock->iTable!=1 || pLock==&p->lock );
71027 if( pLock->iTable!=1 ){
71031 ppIter = &pLock->pNext;
71035 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
71036 if( pBt->pWriter==p ){
71037 pBt->pWriter = 0;
71038 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
71039 }else if( pBt->nTransaction==2 ){
71049 pBt->btsFlags &= ~BTS_PENDING;
71054 ** This function changes all write-locks held by Btree p into read-locks.
71057 BtShared *pBt = p->pBt;
71061 if( pBt->pWriter==p ){
71063 pBt->pWriter = 0;
71064 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
71065 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
71066 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
71067 pLock->eLock = READ_LOCK;
71085 return sqlite3_mutex_held(p->pBt->mutex);
71089 ** database connetion. This is important in shared-cache mode. If the database
71090 ** connection pointers get out-of-sync, it is possible for routines like
71093 ** statements only and for the purpose of double-checking that the btree code
71094 ** does keep the database connection pointers up-to-date.
71098 return (p->pBtree->db==p->pBt->db);
71106 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
71109 ** Invalidate the overflow page-list cache for all cursors opened
71114 assert( sqlite3_mutex_held(pBt->mutex) );
71115 for(p=pBt->pCursor; p; p=p->pNext){
71128 ** cursors open on any row within the table with root-page pgnoRoot.
71141 assert( pBtree->hasIncrblobCur );
71143 pBtree->hasIncrblobCur = 0;
71144 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
71145 if( (p->curFlags & BTCF_Incrblob)!=0 ){
71146 pBtree->hasIncrblobCur = 1;
71147 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
71148 p->eState = CURSOR_INVALID;
71161 ** when a page that previously contained data becomes a free-list leaf
71166 ** free-list leaf pages:
71169 ** a free-list leaf page, the page is not written to the database
71170 ** (as free-list leaf pages contain no meaningful data). Sometimes
71174 ** 2) When a free-list leaf page is reused, its content is not read
71180 ** a page is moved to the free-list and then reused within the same
71182 ** it is moved to the free-list and it is also not journalled when it
71183 ** is extracted from the free-list and reused, then the original data
71188 ** moved to become a free-list leaf page, the corresponding bit is
71189 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
71196 if( !pBt->pHasContent ){
71197 assert( pgno<=pBt->nPage );
71198 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
71199 if( !pBt->pHasContent ){
71203 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
71204 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
71212 ** This function is called when a free-list leaf page is removed from the
71213 ** free-list for reuse. It returns false if it is safe to retrieve the
71214 ** page from the pager layer with the 'no-content' flag set. True otherwise.
71217 Bitvec *p = pBt->pHasContent;
71223 ** invoked at the conclusion of each write-transaction.
71226 sqlite3BitvecDestroy(pBt->pHasContent);
71227 pBt->pHasContent = 0;
71235 if( pCur->iPage>=0 ){
71236 for(i=0; i<pCur->iPage; i++){
71237 releasePageNotNull(pCur->apPage[i]);
71239 releasePageNotNull(pCur->pPage);
71240 pCur->iPage = -1;
71247 ** function saves the current cursor key in variables pCur->nKey and
71248 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
71252 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
71253 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
71254 ** set to point to a malloced buffer pCur->nKey bytes in size containing
71259 assert( CURSOR_VALID==pCur->eState );
71260 assert( 0==pCur->pKey );
71263 if( pCur->curIntKey ){
71265 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
71270 ** up to the size of 1 varint plus 1 8-byte value when the cursor
71274 pCur->nKey = sqlite3BtreePayloadSize(pCur);
71275 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
71277 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
71279 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
71280 pCur->pKey = pKey;
71288 assert( !pCur->curIntKey || !pCur->pKey );
71302 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
71303 assert( 0==pCur->pKey );
71306 if( pCur->curFlags & BTCF_Pinned ){
71309 if( pCur->eState==CURSOR_SKIPNEXT ){
71310 pCur->eState = CURSOR_VALID;
71312 pCur->skipNext = 0;
71318 pCur->eState = CURSOR_REQUIRESEEK;
71321 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
71330 ** the table with root-page iRoot. "Saving the cursor position" means that
71341 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
71351 assert( sqlite3_mutex_held(pBt->mutex) );
71352 assert( pExcept==0 || pExcept->pBt==pBt );
71353 for(p=pBt->pCursor; p; p=p->pNext){
71354 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
71357 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
71372 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
71373 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
71379 testcase( p->iPage>=0 );
71383 p = p->pNext;
71393 sqlite3_free(pCur->pKey);
71394 pCur->pKey = 0;
71395 pCur->eState = CURSOR_INVALID;
71414 KeyInfo *pKeyInfo = pCur->pKeyInfo;
71419 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
71424 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
71443 assert( pCur->eState>=CURSOR_REQUIRESEEK );
71444 if( pCur->eState==CURSOR_FAULT ){
71445 return pCur->skipNext;
71447 pCur->eState = CURSOR_INVALID;
71451 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
71454 sqlite3_free(pCur->pKey);
71455 pCur->pKey = 0;
71456 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
71457 if( skipNext ) pCur->skipNext = skipNext;
71458 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
71459 pCur->eState = CURSOR_SKIPNEXT;
71466 (p->eState>=CURSOR_REQUIRESEEK ? \
71486 assert( sizeof(pCur->eState)==1 );
71518 assert( pCur->eState!=CURSOR_VALID );
71524 if( pCur->eState!=CURSOR_VALID ){
71565 pCur->hints = x;
71572 ** number for the pointer-map page that contains the entry for the
71582 assert( sqlite3_mutex_held(pBt->mutex) );
71584 nPagesPerMapPage = (pBt->usableSize/5)+1;
71585 iPtrMap = (pgno-2)/nPagesPerMapPage;
71599 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
71600 ** a no-op. If an error occurs, the appropriate error code is written
71612 assert( sqlite3_mutex_held(pBt->mutex) );
71613 /* The super-journal page number must never be used as a pointer map page */
71616 assert( pBt->autoVacuum );
71622 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
71639 assert( offset <= (int)pBt->usableSize-5 );
71643 TRACE(("PTRMAP_UPDATE: %u->(%u,%u)\n", key, eType, parent));
71669 assert( sqlite3_mutex_held(pBt->mutex) );
71672 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
71683 assert( offset <= (int)pBt->usableSize-5 );
71705 ** 4-byte child pointer found on interior pages, if there is one.
71710 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
71712 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
71718 ** on a single B-tree page. Make necessary adjustments to the CellInfo
71739 minLocal = pPage->minLocal;
71740 maxLocal = pPage->maxLocal;
71741 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
71745 pInfo->nLocal = (u16)surplus;
71747 pInfo->nLocal = (u16)minLocal;
71749 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
71758 maxLocal = pPage->maxLocal;
71764 minLocal = pPage->minLocal;
71765 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
71789 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71790 assert( pPage->leaf==0 );
71791 assert( pPage->childPtrSize==4 );
71795 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
71796 pInfo->nPayload = 0;
71797 pInfo->nLocal = 0;
71798 pInfo->pPayload = 0;
71810 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71811 assert( pPage->leaf==0 || pPage->leaf==1 );
71812 assert( pPage->intKeyLeaf );
71813 assert( pPage->childPtrSize==0 );
71834 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
71837 ** This routine is a high-runner.
71871 pInfo->nKey = *(i64*)&iKey;
71872 pInfo->nPayload = nPayload;
71873 pInfo->pPayload = pIter;
71874 testcase( nPayload==pPage->maxLocal );
71875 testcase( nPayload==(u32)pPage->maxLocal+1 );
71876 if( nPayload<=pPage->maxLocal ){
71880 pInfo->nSize = nPayload + (u16)(pIter - pCell);
71881 if( pInfo->nSize<4 ) pInfo->nSize = 4;
71882 pInfo->nLocal = (u16)nPayload;
71895 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71896 assert( pPage->leaf==0 || pPage->leaf==1 );
71897 assert( pPage->intKeyLeaf==0 );
71898 pIter = pCell + pPage->childPtrSize;
71908 pInfo->nKey = nPayload;
71909 pInfo->nPayload = nPayload;
71910 pInfo->pPayload = pIter;
71911 testcase( nPayload==pPage->maxLocal );
71912 testcase( nPayload==(u32)pPage->maxLocal+1 );
71913 if( nPayload<=pPage->maxLocal ){
71917 pInfo->nSize = nPayload + (u16)(pIter - pCell);
71918 if( pInfo->nSize<4 ) pInfo->nSize = 4;
71919 pInfo->nLocal = (u16)nPayload;
71929 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
71937 ** data area of the btree-page. The return number includes the cell
71957 pPage->xParseCell(pPage, pCell, &debuginfo);
71960 assert( pPage->childPtrSize==4 );
71970 testcase( nSize==pPage->maxLocal );
71971 testcase( nSize==(u32)pPage->maxLocal+1 );
71972 if( nSize<=pPage->maxLocal ){
71973 nSize += (u32)(pIter - pCell);
71976 int minLocal = pPage->minLocal;
71977 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
71978 testcase( nSize==pPage->maxLocal );
71979 testcase( nSize==(u32)pPage->maxLocal+1 );
71980 if( nSize>pPage->maxLocal ){
71983 nSize += 4 + (u16)(pIter - pCell);
71999 pPage->xParseCell(pPage, pCell, &debuginfo);
72002 assert( pPage->childPtrSize==0 );
72012 testcase( nSize==pPage->maxLocal );
72013 testcase( nSize==(u32)pPage->maxLocal+1 );
72014 if( nSize<=pPage->maxLocal ){
72015 nSize += (u32)(pIter - pCell);
72018 int minLocal = pPage->minLocal;
72019 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
72020 testcase( nSize==pPage->maxLocal );
72021 testcase( nSize==(u32)pPage->maxLocal+1 );
72022 if( nSize>pPage->maxLocal ){
72025 nSize += 4 + (u16)(pIter - pCell);
72040 pPage->xParseCell(pPage, pCell, &debuginfo);
72045 assert( pPage->childPtrSize==4 );
72048 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
72049 return (u16)(pIter - pCell);
72062 pPage->xParseCell(pPage, pCell, &debuginfo);
72074 /* pIter now points at the 64-bit integer key value, a variable length
72085 testcase( nSize==pPage->maxLocal );
72086 testcase( nSize==(u32)pPage->maxLocal+1 );
72087 if( nSize<=pPage->maxLocal ){
72088 nSize += (u32)(pIter - pCell);
72091 int minLocal = pPage->minLocal;
72092 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
72093 testcase( nSize==pPage->maxLocal );
72094 testcase( nSize==(u32)pPage->maxLocal+1 );
72095 if( nSize>pPage->maxLocal ){
72098 nSize += 4 + (u16)(pIter - pCell);
72109 return pPage->xCellSize(pPage, findCell(pPage, iCell));
72117 ** pointer to an overflow page, insert an entry into the pointer-map for
72124 pPage->xParseCell(pPage, pCell, &info);
72127 if( SQLITE_OVERFLOW(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
72132 ovfl = get4byte(&pCell[info.nSize-4]);
72133 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
72141 ** page so that there are no free-blocks on the free-block list.
72146 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
72147 ** b-tree page so that there are no freeblocks or fragment bytes, all
72153 int pc; /* Address of the i-th cell */
72167 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72168 assert( pPage->pBt!=0 );
72169 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
72170 assert( pPage->nOverflow==0 );
72171 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72172 data = pPage->aData;
72173 hdr = pPage->hdrOffset;
72174 cellOffset = pPage->cellOffset;
72175 nCell = pPage->nCell;
72178 usableSize = pPage->pBt->usableSize;
72183 ** offsets to each pointer in the cell-pointer array than it is to
72187 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
72190 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
72204 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
72211 assert( cbrk+(iFree-top) <= usableSize );
72212 memmove(&data[cbrk], &data[top], iFree-top);
72224 iCellLast = usableSize - 4;
72227 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
72231 u8 *pAddr; /* The i-th cell pointer */
72243 size = pPage->xCellSize(pPage, &src[pc]);
72244 cbrk -= size;
72258 assert( pPage->nFree>=0 );
72259 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
72266 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
72267 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72272 ** Search the free-list on page pPg for space to store a cell nByte bytes in
72274 ** from the free-list.
72276 ** If no suitable space can be found on the free-list, return NULL.
72286 const int hdr = pPg->hdrOffset; /* Offset to page header */
72287 u8 * const aData = pPg->aData; /* Page data */
72292 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
72297 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
72298 ** freeblock form a big-endian integer which is the size of the freeblock
72299 ** in bytes, including the 4-byte header. */
72302 if( (x = size - nByte)>=0 ){
72306 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
72310 /* Remove the slot from the free-list. Update the number of
72320 /* The slot remains on the free-list. Reduce its size to account
72337 if( pc>maxPC+nByte-4 ){
72345 ** Allocate nByte bytes of space from within the B-Tree page passed
72346 ** as the first argument. Write into *pIdx the index into pPage->aData[]
72358 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
72359 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
72365 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72366 assert( pPage->pBt );
72367 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72369 assert( pPage->nFree>=nByte );
72370 assert( pPage->nOverflow==0 );
72371 assert( nByte < (int)(pPage->pBt->usableSize-8) );
72373 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
72374 gap = pPage->cellOffset + 2*pPage->nCell;
72376 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
72379 ** However, that integer is too large to be stored in a 2-byte unsigned
72384 if( top==0 && pPage->pBt->usableSize==65536 ){
72389 }else if( top>(int)pPage->pBt->usableSize ){
72404 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
72405 *pIdx = g2 = (int)(pSpace-data);
72421 assert( pPage->nCell>0 || CORRUPT_DB );
72422 assert( pPage->nFree>=0 );
72423 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
72436 top -= nByte;
72438 assert( top+nByte <= (int)pPage->pBt->usableSize );
72444 ** Return a section of the pPage->aData to the freelist.
72445 ** The first byte of the new free block is pPage->aData[iStart]
72464 unsigned char *data = pPage->aData; /* Page content */
72467 assert( pPage->pBt!=0 );
72468 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72469 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
72470 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
72471 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72473 assert( CORRUPT_DB || iStart<=pPage->pBt->usableSize-4 );
72478 hdr = pPage->hdrOffset;
72490 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
72502 nFrag = iFreeBlk - iEnd;
72505 if( iEnd > pPage->pBt->usableSize ){
72508 iSize = iEnd - iStart;
72520 nFrag += iStart - iPtrEnd;
72521 iSize = iEnd - iPtr;
72526 data[hdr+7] -= nFrag;
72530 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
72549 pPage->nFree += iOrigSize;
72566 BtShared *pBt; /* A copy of pPage->pBt */
72568 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
72569 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72570 pBt = pPage->pBt;
72571 pPage->max1bytePayload = pBt->max1bytePayload;
72573 pPage->childPtrSize = 0;
72574 pPage->leaf = 1;
72576 pPage->intKeyLeaf = 1;
72577 pPage->xCellSize = cellSizePtrTableLeaf;
72578 pPage->xParseCell = btreeParseCellPtr;
72579 pPage->intKey = 1;
72580 pPage->maxLocal = pBt->maxLeaf;
72581 pPage->minLocal = pBt->minLeaf;
72583 pPage->intKey = 0;
72584 pPage->intKeyLeaf = 0;
72585 pPage->xCellSize = cellSizePtrIdxLeaf;
72586 pPage->xParseCell = btreeParseCellPtrIndex;
72587 pPage->maxLocal = pBt->maxLocal;
72588 pPage->minLocal = pBt->minLocal;
72590 pPage->intKey = 0;
72591 pPage->intKeyLeaf = 0;
72592 pPage->xCellSize = cellSizePtrIdxLeaf;
72593 pPage->xParseCell = btreeParseCellPtrIndex;
72597 pPage->childPtrSize = 4;
72598 pPage->leaf = 0;
72600 pPage->intKey = 0;
72601 pPage->intKeyLeaf = 0;
72602 pPage->xCellSize = cellSizePtr;
72603 pPage->xParseCell = btreeParseCellPtrIndex;
72604 pPage->maxLocal = pBt->maxLocal;
72605 pPage->minLocal = pBt->minLocal;
72607 pPage->intKeyLeaf = 0;
72608 pPage->xCellSize = cellSizePtrNoPayload;
72609 pPage->xParseCell = btreeParseCellPtrNoPayload;
72610 pPage->intKey = 1;
72611 pPage->maxLocal = pBt->maxLeaf;
72612 pPage->minLocal = pBt->minLeaf;
72614 pPage->intKey = 0;
72615 pPage->intKeyLeaf = 0;
72616 pPage->xCellSize = cellSizePtr;
72617 pPage->xParseCell = btreeParseCellPtrIndex;
72626 ** in the pPage->nFree field.
72629 int pc; /* Address of a freeblock within pPage->aData[] */
72631 u8 *data; /* Equal to pPage->aData */
72638 assert( pPage->pBt!=0 );
72639 assert( pPage->pBt->db!=0 );
72640 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72641 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
72642 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
72643 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
72644 assert( pPage->isInit==1 );
72645 assert( pPage->nFree<0 );
72647 usableSize = pPage->pBt->usableSize;
72648 hdr = pPage->hdrOffset;
72649 data = pPage->aData;
72650 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
72654 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
72655 iCellLast = usableSize - 4;
72658 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
72662 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
72666 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
72693 ** of the cell-content area plus the number of free bytes within
72694 ** the cell-content area. If this is greater than the usable-size
72696 ** serves to verify that the offset to the start of the cell-content
72702 pPage->nFree = (u16)(nFree - iCellFirst);
72715 int pc; /* Address of a freeblock within pPage->aData[] */
72716 u8 *data; /* Equal to pPage->aData */
72720 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
72721 usableSize = pPage->pBt->usableSize;
72722 iCellLast = usableSize - 4;
72723 data = pPage->aData;
72724 cellOffset = pPage->cellOffset;
72725 if( !pPage->leaf ) iCellLast--;
72726 for(i=0; i<pPage->nCell; i++){
72733 sz = pPage->xCellSize(pPage, &data[pc]);
72746 ** not contain a well-formed database page, then return
72748 ** guarantee that the page is well-formed. It only shows that
72752 u8 *data; /* Equal to pPage->aData */
72755 assert( pPage->pBt!=0 );
72756 assert( pPage->pBt->db!=0 );
72757 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72758 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
72759 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
72760 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
72761 assert( pPage->isInit==0 );
72763 pBt = pPage->pBt;
72764 data = pPage->aData + pPage->hdrOffset;
72765 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
72766 ** the b-tree page type. */
72770 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
72771 pPage->maskPage = (u16)(pBt->pageSize - 1);
72772 pPage->nOverflow = 0;
72773 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
72774 pPage->aCellIdx = data + pPage->childPtrSize + 8;
72775 pPage->aDataEnd = pPage->aData + pBt->pageSize;
72776 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
72777 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
72779 pPage->nCell = get2byte(&data[3]);
72780 if( pPage->nCell>MX_CELL(pBt) ){
72784 testcase( pPage->nCell==MX_CELL(pBt) );
72785 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
72789 assert( pPage->nCell>0
72790 || get2byteNotZero(&data[5])==(int)pBt->usableSize
72792 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
72793 pPage->isInit = 1;
72794 if( pBt->db->flags & SQLITE_CellSizeCk ){
72805 unsigned char *data = pPage->aData;
72806 BtShared *pBt = pPage->pBt;
72807 u8 hdr = pPage->hdrOffset;
72810 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
72811 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
72812 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
72813 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72814 assert( sqlite3_mutex_held(pBt->mutex) );
72815 if( pBt->btsFlags & BTS_FAST_SECURE ){
72816 memset(&data[hdr], 0, pBt->usableSize - hdr);
72822 put2byte(&data[hdr+5], pBt->usableSize);
72823 pPage->nFree = (u16)(pBt->usableSize - first);
72825 pPage->cellOffset = first;
72826 pPage->aDataEnd = &data[pBt->pageSize];
72827 pPage->aCellIdx = &data[first];
72828 pPage->aDataOfst = &data[pPage->childPtrSize];
72829 pPage->nOverflow = 0;
72830 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
72831 pPage->maskPage = (u16)(pBt->pageSize - 1);
72832 pPage->nCell = 0;
72833 pPage->isInit = 1;
72843 if( pgno!=pPage->pgno ){
72844 pPage->aData = sqlite3PagerGetData(pDbPage);
72845 pPage->pDbPage = pDbPage;
72846 pPage->pBt = pBt;
72847 pPage->pgno = pgno;
72848 pPage->hdrOffset = pgno==1 ? 100 : 0;
72850 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
72875 assert( sqlite3_mutex_held(pBt->mutex) );
72876 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
72889 assert( sqlite3_mutex_held(pBt->mutex) );
72890 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
72899 ** error, return ((unsigned int)-1).
72902 return pBt->nPage;
72906 return btreePagecount(p->pBt);
72916 int bReadOnly /* True for a read-only page */
72921 assert( sqlite3_mutex_held(pBt->mutex) );
72927 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
72933 if( pPage->isInit==0 ){
72942 assert( pPage->pgno==pgno || CORRUPT_DB );
72943 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
72955 assert( pPage->aData );
72956 assert( pPage->pBt );
72957 assert( pPage->pDbPage!=0 );
72958 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
72959 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
72960 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72961 sqlite3PagerUnrefNotNull(pPage->pDbPage);
72968 assert( pPage->aData );
72969 assert( pPage->pBt );
72970 assert( pPage->pDbPage!=0 );
72971 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
72972 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
72973 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72974 sqlite3PagerUnrefPageOne(pPage->pDbPage);
72994 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
72999 (*ppPage)->isInit = 0;
73019 if( pPage->isInit ){
73020 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73021 pPage->isInit = 0;
73028 ** the call for every page that comes in for re-initializing. */
73039 assert( pBt->db );
73040 assert( sqlite3_mutex_held(pBt->db->mutex) );
73041 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
73049 ** be exclusively in memory, or it might use a disk-based memory cache.
73053 ** If zFilename is ":memory:" then an in-memory database is created
73066 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
73083 /* Set the variable isMemdb to true for an in-memory database, or
73084 ** false for a file-based database.
73096 assert( sqlite3_mutex_held(db->mutex) );
73115 p->inTrans = TRANS_NONE;
73116 p->db = db;
73118 p->lock.pBtree = p;
73119 p->lock.iTable = 1;
73130 int nFullPathname = pVfs->mxPathname+1;
73134 p->sharable = 1;
73160 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
73161 assert( pBt->nRef>0 );
73162 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
73163 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
73165 for(iDb=db->nDb-1; iDb>=0; iDb--){
73166 Btree *pExisting = db->aDb[iDb].pBt;
73167 if( pExisting && pExisting->pBt==pBt ){
73175 p->pBt = pBt;
73176 pBt->nRef++;
73190 p->sharable = 1;
73207 /* Suppress false-positive compiler warning from PVS-Studio */
73215 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
73218 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
73219 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
73224 pBt->openFlags = (u8)flags;
73225 pBt->db = db;
73226 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
73227 p->pBt = pBt;
73229 pBt->pCursor = 0;
73230 pBt->pPage1 = 0;
73231 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
73233 pBt->btsFlags |= BTS_SECURE_DELETE;
73235 pBt->btsFlags |= BTS_OVERWRITE;
73237 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
73238 ** determined by the 2-byte integer located at an offset of 16 bytes from
73240 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
73241 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
73242 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
73243 pBt->pageSize = 0;
73245 /* If the magic name ":memory:" will create an in-memory database, then
73246 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
73249 ** regular file-name. In this case the auto-vacuum applies as per normal.
73252 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
73253 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
73258 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
73259 ** determined by the one-byte unsigned integer found at an offset of 20
73262 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
73264 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
73265 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
73268 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
73270 pBt->usableSize = pBt->pageSize - nReserve;
73271 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
73276 pBt->nRef = 1;
73277 if( p->sharable ){
73281 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
73282 if( pBt->mutex==0 ){
73288 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
73300 if( p->sharable ){
73303 for(i=0; i<db->nDb; i++){
73304 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
73305 while( pSib->pPrev ){ pSib = pSib->pPrev; }
73306 if( (uptr)p->pBt<(uptr)pSib->pBt ){
73307 p->pNext = pSib;
73308 p->pPrev = 0;
73309 pSib->pPrev = p;
73311 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
73312 pSib = pSib->pNext;
73314 p->pNext = pSib->pNext;
73315 p->pPrev = pSib;
73316 if( p->pNext ){
73317 p->pNext->pPrev = p;
73319 pSib->pNext = p;
73330 if( pBt && pBt->pPager ){
73331 sqlite3PagerClose(pBt->pPager, 0);
73339 /* If the B-Tree was successfully opened, set the pager-cache size to the
73340 ** default value. Except, when opening on an existing shared pager-cache,
73341 ** do not change the pager-cache size.
73347 pFile = sqlite3PagerFile(pBt->pPager);
73348 if( pFile->pMethods ){
73349 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
73372 assert( sqlite3_mutex_notheld(pBt->mutex) );
73375 pBt->nRef--;
73376 if( pBt->nRef<=0 ){
73378 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
73381 while( ALWAYS(pList) && pList->pNext!=pBt ){
73382 pList=pList->pNext;
73385 pList->pNext = pBt->pNext;
73389 sqlite3_mutex_free(pBt->mutex);
73401 ** Make sure pBt->pTmpSpace points to an allocation of
73402 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
73407 assert( pBt->pTmpSpace==0 );
73410 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 );
73411 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
73412 if( pBt->pTmpSpace==0 ){
73413 BtCursor *pCur = pBt->pCursor;
73414 pBt->pCursor = pCur->pNext; /* Unlink the cursor */
73419 /* One of the uses of pBt->pTmpSpace is to format cells before
73432 ** left-child pointer to the beginning of a cell.
73434 memset(pBt->pTmpSpace, 0, 8);
73435 pBt->pTmpSpace += 4;
73440 ** Free the pBt->pTmpSpace allocation
73443 if( pBt->pTmpSpace ){
73444 pBt->pTmpSpace -= 4;
73445 sqlite3PageFree(pBt->pTmpSpace);
73446 pBt->pTmpSpace = 0;
73454 BtShared *pBt = p->pBt;
73457 assert( sqlite3_mutex_held(p->db->mutex) );
73463 BtCursor *pCur = pBt->pCursor;
73466 pCur = pCur->pNext;
73467 assert( pTmp->pBtree!=p );
73474 ** The call to sqlite3BtreeRollback() drops any table-locks held by
73480 /* If there are still other outstanding references to the shared-btree
73482 ** up the shared-btree.
73484 assert( p->wantToLock==0 && p->locked==0 );
73485 if( !p->sharable || removeFromSharingList(pBt) ){
73491 assert( !pBt->pCursor );
73492 sqlite3PagerClose(pBt->pPager, p->db);
73493 if( pBt->xFreeSchema && pBt->pSchema ){
73494 pBt->xFreeSchema(pBt->pSchema);
73496 sqlite3DbFree(0, pBt->pSchema);
73502 assert( p->wantToLock==0 );
73503 assert( p->locked==0 );
73504 if( p->pPrev ) p->pPrev->pNext = p->pNext;
73505 if( p->pNext ) p->pNext->pPrev = p->pPrev;
73520 BtShared *pBt = p->pBt;
73521 assert( sqlite3_mutex_held(p->db->mutex) );
73523 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
73539 BtShared *pBt = p->pBt;
73541 assert( sqlite3_mutex_held(p->db->mutex) );
73543 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
73554 BtShared *pBt = p->pBt;
73555 assert( sqlite3_mutex_held(p->db->mutex) );
73557 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
73568 ** is a very low but non-zero probability of damage. Level 3 reduces the
73576 BtShared *pBt = p->pBt;
73577 assert( sqlite3_mutex_held(p->db->mutex) );
73579 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
73608 BtShared *pBt = p->pBt;
73611 pBt->nReserveWanted = nReserve;
73612 x = pBt->pageSize - pBt->usableSize;
73614 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
73620 ((pageSize-1)&pageSize)==0 ){
73622 assert( !pBt->pCursor );
73624 pBt->pageSize = (u32)pageSize;
73627 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
73628 pBt->usableSize = pBt->pageSize - (u16)nReserve;
73629 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
73638 return p->pBt->pageSize;
73643 ** may only be called if it is guaranteed that the b-tree mutex is already
73647 ** known that the shared b-tree mutex is held, but the mutex on the
73654 assert( sqlite3_mutex_held(p->pBt->mutex) );
73655 n = p->pBt->pageSize - p->pBt->usableSize;
73666 ** The amount of reserve can only grow - never shrink.
73671 n1 = (int)p->pBt->nReserveWanted;
73686 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
73697 ** newFlag==(-1) No changes
73702 ** freelist leaf pages are not written back to the database. Thus in-page
73716 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
73717 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
73719 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
73725 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
73726 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
73727 ** is disabled. The default value for the auto-vacuum property is
73734 BtShared *pBt = p->pBt;
73739 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
73742 pBt->autoVacuum = av ?1:0;
73743 pBt->incrVacuum = av==2 ?1:0;
73751 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
73761 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
73762 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
73771 ** If the user has not set the safety-level for this database connection
73772 ** using "PRAGMA synchronous", and if the safety-level is not already
73781 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
73782 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
73783 if( pDb->bSyncSet==0
73784 && pDb->safety_level!=safety_level
73785 && pDb!=&db->aDb[1]
73787 pDb->safety_level = safety_level;
73788 sqlite3PagerSetFlags(pBt->pPager,
73789 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
73806 ** well-formed database file, then SQLITE_CORRUPT is returned.
73816 assert( sqlite3_mutex_held(pBt->mutex) );
73817 assert( pBt->pPage1==0 );
73818 rc = sqlite3PagerSharedLock(pBt->pPager);
73826 nPage = get4byte(28+(u8*)pPage1->aData);
73827 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
73828 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
73831 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
73837 u8 *page1 = pPage1->aData;
73839 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
73848 pBt->btsFlags |= BTS_READ_ONLY;
73855 pBt->btsFlags |= BTS_READ_ONLY;
73866 ** may not be the latest version - there may be a newer one in the log
73869 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
73871 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
73887 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
73896 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
73897 ** determined by the 2-byte integer located at an offset of 16 bytes from
73900 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
73902 if( ((pageSize-1)&pageSize)!=0
73909 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
73913 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
73914 ** determined by the one-byte unsigned integer found at an offset of 20
73916 usableSize = pageSize - page1[20];
73917 if( (u32)pageSize!=pBt->pageSize ){
73919 ** of BtShared.pageSize, we have discovered that the page-size is
73920 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
73922 ** again with the correct page-size.
73925 pBt->usableSize = usableSize;
73926 pBt->pageSize = pageSize;
73927 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
73929 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
73930 pageSize-usableSize);
73934 if( sqlite3WritableSchema(pBt->db)==0 ){
73941 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
73947 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
73948 pBt->pageSize = pageSize;
73949 pBt->usableSize = usableSize;
73951 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
73952 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
73958 ** cells can will fit on one page. We assume a 10-byte page header.
73960 ** 2-byte pointer to the cell
73961 ** 4-byte child pointer
73962 ** 9-byte nKey value
73963 ** 4-byte nData value
73964 ** 4-byte overflow page pointer
73965 ** So a cell consists of a 2-byte pointer, a header which is as much as
73969 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
73970 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
73971 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
73972 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
73973 if( pBt->maxLocal>127 ){
73974 pBt->max1bytePayload = 127;
73976 pBt->max1bytePayload = (u8)pBt->maxLocal;
73978 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
73979 pBt->pPage1 = pPage1;
73980 pBt->nPage = nPage;
73985 pBt->pPage1 = 0;
74005 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
74006 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
74007 && pCur->eState!=CURSOR_FAULT ) r++;
74019 ** If there is a transaction in progress, this routine is a no-op.
74022 assert( sqlite3_mutex_held(pBt->mutex) );
74023 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
74024 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
74025 MemPage *pPage1 = pBt->pPage1;
74026 assert( pPage1->aData );
74027 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
74028 pBt->pPage1 = 0;
74043 assert( sqlite3_mutex_held(pBt->mutex) );
74044 if( pBt->nPage>0 ){
74047 pP1 = pBt->pPage1;
74049 data = pP1->aData;
74050 rc = sqlite3PagerWrite(pP1->pDbPage);
74054 data[16] = (u8)((pBt->pageSize>>8)&0xff);
74055 data[17] = (u8)((pBt->pageSize>>16)&0xff);
74058 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
74059 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
74063 memset(&data[24], 0, 100-24);
74065 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
74067 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
74068 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
74069 put4byte(&data[36 + 4*4], pBt->autoVacuum);
74070 put4byte(&data[36 + 7*4], pBt->incrVacuum);
74072 pBt->nPage = 1;
74085 p->pBt->nPage = 0;
74086 rc = newDatabase(p->pBt);
74092 ** Attempt to start a new transaction. A write-transaction
74093 ** is started if the second argument is nonzero, otherwise a read-
74097 ** upgraded to exclusive by calling this routine a second time - the
74100 ** A write-transaction must be started before attempting any
74114 ** if there is one. But if there was previously a read-lock, do not
74115 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
74116 ** returned when there is already a read-lock in order to avoid a deadlock.
74131 BtShared *pBt = p->pBt;
74132 Pager *pPager = pBt->pPager;
74138 /* If the btree is already in a write-transaction, or it
74139 ** is already in a read-transaction and a read-transaction
74140 ** is requested, this is a no-op.
74142 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
74145 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
74147 if( (p->db->flags & SQLITE_ResetDatabase)
74150 pBt->btsFlags &= ~BTS_READ_ONLY;
74153 /* Write transactions are not possible on a read-only database */
74154 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
74163 ** on this shared-btree structure and a second write transaction is
74166 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
74167 || (pBt->btsFlags & BTS_PENDING)!=0
74169 pBlock = pBt->pWriter->db;
74172 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
74173 if( pIter->pBtree!=p ){
74174 pBlock = pIter->pBtree->db;
74180 sqlite3ConnectionBlocked(p->db, pBlock);
74187 /* Any read-only or read-write transaction implies a read-lock on
74188 ** page 1. So if some other shared-cache client already has a write-lock
74193 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
74194 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
74196 sqlite3PagerWalDb(pPager, p->db);
74201 if( pBt->pPage1==0 && wrflag ){
74202 assert( pBt->inTransaction==TRANS_NONE );
74208 /* Call lockBtree() until either pBt->pPage1 is populated or
74210 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
74211 ** reading page 1 it discovers that the page-size of the database
74212 ** file is not pBt->pageSize. In this case lockBtree() will update
74213 ** pBt->pageSize to the page-size of the file on disk.
74215 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
74218 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
74221 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
74224 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
74237 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
74245 if( p->inTrans==TRANS_NONE ){
74246 pBt->nTransaction++;
74248 if( p->sharable ){
74249 assert( p->lock.pBtree==p && p->lock.iTable==1 );
74250 p->lock.eLock = READ_LOCK;
74251 p->lock.pNext = pBt->pLock;
74252 pBt->pLock = &p->lock;
74256 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
74257 if( p->inTrans>pBt->inTransaction ){
74258 pBt->inTransaction = p->inTrans;
74261 MemPage *pPage1 = pBt->pPage1;
74263 assert( !pBt->pWriter );
74264 pBt->pWriter = p;
74265 pBt->btsFlags &= ~BTS_EXCLUSIVE;
74266 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
74269 /* If the db-size header field is incorrect (as it may be if an old
74272 ** re-read the database size from page 1 if a savepoint or transaction
74275 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
74276 rc = sqlite3PagerWrite(pPage1->pDbPage);
74278 put4byte(&pPage1->aData[28], pBt->nPage);
74287 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
74292 ** the sub-journal is not already open, then it will be opened here.
74294 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
74304 if( p->sharable
74305 || p->inTrans==TRANS_NONE
74306 || (p->inTrans==TRANS_READ && wrflag!=0)
74310 pBt = p->pBt;
74312 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
74317 ** the sub-journal is not already open, then it will be opened here.
74319 return sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
74328 ** Set the pointer-map entries for all children of page pPage. Also, if
74336 BtShared *pBt = pPage->pBt;
74337 Pgno pgno = pPage->pgno;
74339 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
74340 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
74342 nCell = pPage->nCell;
74349 if( !pPage->leaf ){
74355 if( !pPage->leaf ){
74356 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
74368 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
74371 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
74374 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
74378 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
74379 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
74382 if( get4byte(pPage->aData)!=iFrom ){
74385 put4byte(pPage->aData, iTo);
74391 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
74393 nCell = pPage->nCell;
74399 pPage->xParseCell(pPage, pCell, &info);
74401 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
74404 if( iFrom==get4byte(pCell+info.nSize-4) ){
74405 put4byte(pCell+info.nSize-4, iTo);
74410 if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
74422 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
74425 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
74437 ** the journal needs to be sync()ed before database page pDbPage->pgno
74445 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
74450 Pgno iDbPage = pDbPage->pgno;
74451 Pager *pPager = pBt->pPager;
74456 assert( sqlite3_mutex_held(pBt->mutex) );
74457 assert( pDbPage->pBt==pBt );
74463 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
74467 pDbPage->pgno = iFreePage;
74469 /* If pDbPage was a btree-page, then it may have child pages and/or cells
74483 Pgno nextOvfl = get4byte(pDbPage->aData);
74501 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
74519 ** Perform a single step of an incremental-vacuum. If successful, return
74524 ** More specifically, this function attempts to re-organize the database so
74530 ** If the bCommit parameter is non-zero, this function assumes that the
74532 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
74536 Pgno nFreeList; /* Number of pages still on the free-list */
74539 assert( sqlite3_mutex_held(pBt->mutex) );
74546 nFreeList = get4byte(&pBt->pPage1->aData[36]);
74561 /* Remove the page from the files free-list. This is not required
74562 ** if bCommit is non-zero. In that case, the free-list will be
74590 ** looping until a free-page located within the first nFin pages
74623 iLastPg--;
74625 pBt->bDoTruncate = 1;
74626 pBt->nPage = iLastPg;
74632 ** The database opened by the first argument is an auto-vacuum database
74634 ** size of the database in pages following an auto-vacuum operation.
74641 nEntry = pBt->usableSize/5;
74642 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
74643 nFin = nOrig - nFree - nPtrmap;
74645 nFin--;
74648 nFin--;
74655 ** A write-transaction must be opened before calling this function.
74664 BtShared *pBt = p->pBt;
74667 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
74668 if( !pBt->autoVacuum ){
74672 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
74684 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
74685 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
74697 ** is committed for an auto-vacuum database.
74707 pBt = p->pBt;
74708 pPager = pBt->pPager;
74711 assert( sqlite3_mutex_held(pBt->mutex) );
74713 assert(pBt->autoVacuum);
74714 if( !pBt->incrVacuum ){
74724 ** is either a pointer-map page or the pending-byte page. If one
74730 nFree = get4byte(&pBt->pPage1->aData[36]);
74731 db = p->db;
74732 if( db->xAutovacPages ){
74734 for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){
74735 if( db->aDb[iDb].pBt==p ) break;
74737 nVac = db->xAutovacPages(
74738 db->pAutovacPagesArg,
74739 db->aDb[iDb].zDbSName,
74742 pBt->pageSize
74758 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
74762 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
74764 put4byte(&pBt->pPage1->aData[32], 0);
74765 put4byte(&pBt->pPage1->aData[36], 0);
74767 put4byte(&pBt->pPage1->aData[28], nFin);
74768 pBt->bDoTruncate = 1;
74769 pBt->nPage = nFin;
74785 ** This routine does the first phase of a two-phase commit. This routine
74797 ** This call is a no-op if no write-transaction is currently active on pBt.
74800 ** the name of a super-journal file that should be written into the
74801 ** individual journal file, or is NULL, indicating no super-journal file
74804 ** When this is called, the super-journal should already have been
74808 ** the write-transaction for this database file is to delete the journal.
74812 if( p->inTrans==TRANS_WRITE ){
74813 BtShared *pBt = p->pBt;
74816 if( pBt->autoVacuum ){
74823 if( pBt->bDoTruncate ){
74824 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
74827 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
74838 BtShared *pBt = p->pBt;
74839 sqlite3 *db = p->db;
74843 pBt->bDoTruncate = 0;
74845 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
74847 ** handle, downgrade to a read-only transaction. The other statements
74850 p->inTrans = TRANS_READ;
74856 if( p->inTrans!=TRANS_NONE ){
74858 pBt->nTransaction--;
74859 if( 0==pBt->nTransaction ){
74860 pBt->inTransaction = TRANS_NONE;
74866 p->inTrans = TRANS_NONE;
74876 ** This routine implements the second phase of a 2-phase commit. The
74888 ** is non-zero then this b-tree transaction is part of a multi-file
74890 ** (by deleting a super-journal file) and the caller will ignore this
74892 ** reset the b-tree objects internal state to indicate that the write
74901 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
74905 /* If the handle has a write-transaction open, commit the shared-btrees
74908 if( p->inTrans==TRANS_WRITE ){
74910 BtShared *pBt = p->pBt;
74911 assert( pBt->inTransaction==TRANS_WRITE );
74912 assert( pBt->nTransaction>0 );
74913 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
74918 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
74919 pBt->inTransaction = TRANS_READ;
74953 ** flag is true, then only write-cursors need be tripped - read-only
74957 ** rolled back modified the database schema. In this case b-tree root
74962 ** saving the current position of a read-only cursor, all cursors,
74963 ** including all read-cursors are tripped.
74975 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
74976 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
74977 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
74986 p->eState = CURSOR_FAULT;
74987 p->skipNext = errCode;
74997 ** Set the pBt->nPage field correctly, according to the current
74998 ** state of the database. Assume pBt->pPage1 is valid.
75001 int nPage = get4byte(&pPage1->aData[28]);
75003 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
75004 testcase( pBt->nPage!=(u32)nPage );
75005 pBt->nPage = nPage;
75021 BtShared *pBt = p->pBt;
75040 if( p->inTrans==TRANS_WRITE ){
75043 assert( TRANS_WRITE==pBt->inTransaction );
75044 rc2 = sqlite3PagerRollback(pBt->pPager);
75049 /* The rollback may have destroyed the pPage1->aData value. So
75051 ** sure pPage1->aData is set correctly. */
75057 pBt->inTransaction = TRANS_READ;
75077 ** A statement sub-transaction is implemented as an anonymous savepoint. The
75079 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
75080 ** are no active savepoints and no other statement-transactions open,
75086 BtShared *pBt = p->pBt;
75088 assert( p->inTrans==TRANS_WRITE );
75089 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
75091 assert( iStatement>p->db->nSavepoint );
75092 assert( pBt->inTransaction==TRANS_WRITE );
75098 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
75110 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
75117 if( p && p->inTrans==TRANS_WRITE ){
75118 BtShared *pBt = p->pBt;
75120 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
75126 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
75129 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
75130 pBt->nPage = 0;
75133 btreeSetNPage(pBt, pBt->pPage1);
75135 /* pBt->nPage might be zero if the database was corrupt when
75137 assert( CORRUPT_DB || pBt->nPage>0 );
75146 ** iTable. If a read-only cursor is requested, it is assumed that
75147 ** the caller already has at least a read-only transaction open
75148 ** on the database already. If a write-cursor is requested, then
75165 ** 3: The database must be writable (not on read-only media)
75176 ** operations on this cursor can be no-ops and all READ operations can
75177 ** return a null row (2-bytes: 0x01 0x00).
75180 ** root page of a b-tree. If it is not, then the cursor acquired
75189 int wrFlag, /* 1 to write. 0 read-only */
75193 BtShared *pBt = p->pBt; /* Shared b-tree handle */
75203 ** b-tree database, the connection is holding the required table locks,
75211 assert( p->inTrans>TRANS_NONE );
75212 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
75213 assert( pBt->pPage1 && pBt->pPage1->aData );
75214 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
75227 pCur->pgnoRoot = iTable;
75228 pCur->iPage = -1;
75229 pCur->pKeyInfo = pKeyInfo;
75230 pCur->pBtree = p;
75231 pCur->pBt = pBt;
75232 pCur->curFlags = 0;
75235 for(pX=pBt->pCursor; pX; pX=pX->pNext){
75236 if( pX->pgnoRoot==iTable ){
75237 pX->curFlags |= BTCF_Multiple;
75238 pCur->curFlags = BTCF_Multiple;
75241 pCur->eState = CURSOR_INVALID;
75242 pCur->pNext = pBt->pCursor;
75243 pBt->pCursor = pCur;
75245 pCur->curFlags |= BTCF_WriteFlag;
75246 pCur->curPagerFlags = 0;
75247 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt);
75249 pCur->curPagerFlags = PAGER_GET_READONLY;
75256 int wrFlag, /* 1 to write. 0 read-only */
75269 int wrFlag, /* 1 to write. 0 read-only */
75273 if( p->sharable ){
75285 ** to users so they cannot do the sizeof() themselves - they must call
75298 ** of run-time by skipping the initialization of those elements.
75309 Btree *pBtree = pCur->pBtree;
75311 BtShared *pBt = pCur->pBt;
75313 assert( pBt->pCursor!=0 );
75314 if( pBt->pCursor==pCur ){
75315 pBt->pCursor = pCur->pNext;
75317 BtCursor *pPrev = pBt->pCursor;
75319 if( pPrev->pNext==pCur ){
75320 pPrev->pNext = pCur->pNext;
75323 pPrev = pPrev->pNext;
75328 sqlite3_free(pCur->aOverflow);
75329 sqlite3_free(pCur->pKey);
75330 if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){
75333 assert( pBtree->sharable==0 );
75338 pCur->pBtree = 0;
75353 if( a->nKey!=b->nKey ) return 0;
75354 if( a->pPayload!=b->pPayload ) return 0;
75355 if( a->nPayload!=b->nPayload ) return 0;
75356 if( a->nLocal!=b->nLocal ) return 0;
75357 if( a->nSize!=b->nSize ) return 0;
75363 btreeParseCell(pCur->pPage, pCur->ix, &info);
75364 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
75370 if( pCur->info.nSize==0 ){
75371 pCur->curFlags |= BTCF_ValidNKey;
75372 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
75381 ** that is currently pointing to a row in a (non-empty) table.
75385 return pCur && pCur->eState==CURSOR_VALID;
75390 return pCur->eState==CURSOR_VALID;
75401 assert( pCur->eState==CURSOR_VALID );
75402 assert( pCur->curIntKey );
75404 return pCur->info.nKey;
75411 assert( (pCur->curFlags & BTCF_Pinned)==0 );
75412 pCur->curFlags |= BTCF_Pinned;
75415 assert( (pCur->curFlags & BTCF_Pinned)!=0 );
75416 pCur->curFlags &= ~BTCF_Pinned;
75425 assert( pCur->eState==CURSOR_VALID );
75427 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
75428 (i64)(pCur->info.pPayload - pCur->pPage->aData);
75436 ** The caller must guarantee that the cursor is pointing to a non-NULL
75442 assert( pCur->eState==CURSOR_VALID );
75444 return pCur->info.nPayload;
75462 assert( pCur->eState==CURSOR_VALID );
75463 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
75469 ** linked list of overflow pages. If possible, it uses the auto-vacuum
75470 ** pointer-map data instead of reading the content of page ovfl to do so.
75482 ** the pointer-map was used to obtain the value for *pPgnoNext), then
75495 assert( sqlite3_mutex_held(pBt->mutex) );
75500 ** autovacuum pointer-map pages. Guess that the next page in
75505 if( pBt->autoVacuum ){
75529 next = get4byte(pPage->aData);
75557 int eOp, /* 0 -> copy from page, 1 -> copy to page */
75590 ** the overflow page-list cache array (BtCursor.aOverflow).
75594 ** Once an overflow page-list cache has been allocated, it must be
75596 ** the cursor is moved to a different row. Additionally, in auto-vacuum
75597 ** mode, the following events may invalidate an overflow page-list cache.
75608 int eOp /* zero to read. non-zero to write. */
75613 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
75614 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
75621 assert( pCur->eState==CURSOR_VALID );
75622 if( pCur->ix>=pPage->nCell ){
75628 aPayload = pCur->info.pPayload;
75629 assert( offset+amt <= pCur->info.nPayload );
75631 assert( aPayload > pPage->aData );
75632 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
75635 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
75642 if( offset<pCur->info.nLocal ){
75644 if( a+offset>pCur->info.nLocal ){
75645 a = pCur->info.nLocal - offset;
75647 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
75650 amt -= a;
75652 offset -= pCur->info.nLocal;
75657 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
75660 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
75669 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
75670 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
75671 if( pCur->aOverflow==0
75672 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
75678 aNew = (Pgno*)sqlite3Realloc(pCur->aOverflow, nOvfl*2*sizeof(Pgno));
75683 pCur->aOverflow = aNew;
75686 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
75687 pCur->curFlags |= BTCF_ValidOvfl;
75690 assert( pCur->aOverflow[0]==nextPage
75691 || pCur->aOverflow[0]==0
75693 assert( pCur->aOverflow[0]!=0 || pCur->aOverflow[offset/ovflSize]==0 );
75695 /* If the overflow page-list cache has been allocated and the
75699 if( pCur->aOverflow[offset/ovflSize] ){
75701 nextPage = pCur->aOverflow[iIdx];
75708 /* If required, populate the overflow page-list cache. */
75709 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT;
75710 assert( pCur->aOverflow[iIdx]==0
75711 || pCur->aOverflow[iIdx]==nextPage
75713 pCur->aOverflow[iIdx] = nextPage;
75719 ** page-list cache, if any, then fall back to the getOverflowPage()
75722 assert( pCur->curFlags & BTCF_ValidOvfl );
75723 assert( pCur->pBtree->db==pBt->db );
75724 if( pCur->aOverflow[iIdx+1] ){
75725 nextPage = pCur->aOverflow[iIdx+1];
75729 offset -= ovflSize;
75736 a = ovflSize - offset;
75744 ** 3) there are no dirty pages in the page-cache
75745 ** 4) the database is file-backed, and
75750 ** output buffer, bypassing the page-cache altogether. This speeds
75755 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
75756 && &pBuf[-4]>=pBufStart /* (6) */
75758 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
75760 u8 *aWrite = &pBuf[-4];
75763 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
75771 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
75782 amt -= a;
75803 ** pCur can be pointing to either a table or an index b-tree.
75805 ** pCur is pointing to an index b-tree then the key section is read.
75817 assert( pCur->eState==CURSOR_VALID );
75818 assert( pCur->iPage>=0 && pCur->pPage );
75835 if ( pCur->eState==CURSOR_INVALID ){
75843 if( pCur->eState==CURSOR_VALID ){
75855 ** the key if index btrees (pPage->intKey==0) and is the data for
75856 ** table btrees (pPage->intKey==1). The number of bytes of available
75876 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
75877 assert( pCur->eState==CURSOR_VALID );
75878 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
75880 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
75881 assert( pCur->info.nSize>0 );
75882 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
75883 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
75884 amt = pCur->info.nLocal;
75885 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
75889 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
75892 return (void*)pCur->info.pPayload;
75899 ** b-tree page. Write the number of available bytes into *pAmt.
75919 ** This function returns SQLITE_CORRUPT if the page-header flags field of
75921 ** if an intkey page appears to be the parent of a non-intkey page, or
75922 ** vice-versa).
75927 assert( pCur->eState==CURSOR_VALID );
75928 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
75929 assert( pCur->iPage>=0 );
75930 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
75933 pCur->info.nSize = 0;
75934 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
75935 pCur->aiIdx[pCur->iPage] = pCur->ix;
75936 pCur->apPage[pCur->iPage] = pCur->pPage;
75937 pCur->ix = 0;
75938 pCur->iPage++;
75939 rc = getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur->curPagerFlags);
75940 assert( pCur->pPage!=0 || rc!=SQLITE_OK );
75942 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
75944 releasePage(pCur->pPage);
75948 pCur->pPage = pCur->apPage[--pCur->iPage];
75955 ** Page pParent is an internal (non-leaf) tree page. This function
75956 ** asserts that page number iChild is the left-child if the iIdx'th
75958 ** cells in pParent, that page number iChild is the right-child of
75964 assert( iIdx<=pParent->nCell );
75965 if( iIdx==pParent->nCell ){
75966 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
75978 ** pCur->idx is set to the cell index that contains the pointer
75980 ** right-most child page then pCur->idx is set to one more than
75986 assert( pCur->eState==CURSOR_VALID );
75987 assert( pCur->iPage>0 );
75988 assert( pCur->pPage );
75990 pCur->apPage[pCur->iPage-1],
75991 pCur->aiIdx[pCur->iPage-1],
75992 pCur->pPage->pgno
75994 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
75995 pCur->info.nSize = 0;
75996 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
75997 pCur->ix = pCur->aiIdx[pCur->iPage-1];
75998 pLeaf = pCur->pPage;
75999 pCur->pPage = pCur->apPage[--pCur->iPage];
76004 ** Move the cursor to point to the root page of its b-tree structure.
76011 ** If the b-tree structure is empty, the cursor state is set to
76017 ** page-header flags indicate that the [virtual] root-page is the expected
76018 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
76020 ** indicating a table b-tree, or if the caller did specify a KeyInfo
76022 ** b-tree).
76032 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
76033 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
76035 if( pCur->iPage>=0 ){
76036 if( pCur->iPage ){
76037 releasePageNotNull(pCur->pPage);
76038 while( --pCur->iPage ){
76039 releasePageNotNull(pCur->apPage[pCur->iPage]);
76041 pRoot = pCur->pPage = pCur->apPage[0];
76044 }else if( pCur->pgnoRoot==0 ){
76045 pCur->eState = CURSOR_INVALID;
76048 assert( pCur->iPage==(-1) );
76049 if( pCur->eState>=CURSOR_REQUIRESEEK ){
76050 if( pCur->eState==CURSOR_FAULT ){
76051 assert( pCur->skipNext!=SQLITE_OK );
76052 return pCur->skipNext;
76056 rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage,
76057 pCur->curPagerFlags);
76059 pCur->eState = CURSOR_INVALID;
76062 pCur->iPage = 0;
76063 pCur->curIntKey = pCur->pPage->intKey;
76065 pRoot = pCur->pPage;
76066 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB );
76068 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
76069 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
76070 ** NULL, the caller expects a table b-tree. If this is not the case,
76075 ** if pCur->iPage>=0). But this is not so if the database is corrupted
76076 ** in such a way that page pRoot is linked into a second b-tree table
76078 assert( pRoot->intKey==1 || pRoot->intKey==0 );
76079 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
76080 return SQLITE_CORRUPT_PAGE(pCur->pPage);
76084 pCur->ix = 0;
76085 pCur->info.nSize = 0;
76086 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
76088 if( pRoot->nCell>0 ){
76089 pCur->eState = CURSOR_VALID;
76090 }else if( !pRoot->leaf ){
76092 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
76093 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
76094 pCur->eState = CURSOR_VALID;
76097 pCur->eState = CURSOR_INVALID;
76104 ** Move the cursor down to the left-most leaf entry beneath the
76107 ** The left-most leaf is the one with the smallest key - the first
76116 assert( pCur->eState==CURSOR_VALID );
76117 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
76118 assert( pCur->ix<pPage->nCell );
76119 pgno = get4byte(findCell(pPage, pCur->ix));
76126 ** Move the cursor down to the right-most leaf entry beneath the
76129 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
76130 ** finds the right-most entry beneath the *page*.
76132 ** The right-most entry is the one with the largest key - the last
76141 assert( pCur->eState==CURSOR_VALID );
76142 while( !(pPage = pCur->pPage)->leaf ){
76143 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
76144 pCur->ix = pPage->nCell;
76148 pCur->ix = pPage->nCell-1;
76149 assert( pCur->info.nSize==0 );
76150 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
76162 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
76165 assert( pCur->pPage->nCell>0 );
76169 assert( pCur->pgnoRoot==0 || (pCur->pPage!=0 && pCur->pPage->nCell==0) );
76186 for(ii=0; ii<pCur->iPage; ii++){
76187 if( pCur->aiIdx[ii]!=pCur->apPage[ii]->nCell ) return 0;
76189 return pCur->ix==pCur->pPage->nCell-1 && pCur->pPage->leaf!=0;
76200 assert( pCur->eState==CURSOR_VALID );
76204 pCur->curFlags |= BTCF_AtLast;
76206 pCur->curFlags &= ~BTCF_AtLast;
76209 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
76217 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
76219 /* If the cursor already points to the last entry, this is a no-op. */
76220 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
76260 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
76262 assert( pCur->pKeyInfo==0 );
76263 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 );
76267 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 ){
76268 if( pCur->info.nKey==intKey ){
76272 if( pCur->info.nKey<intKey ){
76273 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
76275 *pRes = -1;
76282 if( pCur->info.nKey+1==intKey ){
76287 if( pCur->info.nKey==intKey ){
76298 pCur->pBtree->nSeek++; /* Performance measurement during testing */
76304 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
76305 *pRes = -1;
76310 assert( pCur->pPage );
76311 assert( pCur->pPage->isInit );
76312 assert( pCur->eState==CURSOR_VALID );
76313 assert( pCur->pPage->nCell > 0 );
76314 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
76315 assert( pCur->curIntKey );
76320 MemPage *pPage = pCur->pPage;
76323 /* pPage->nCell must be greater than zero. If this is the root-page
76325 ** not run. If this is not the root-page, then the moveToChild() routine
76327 ** be the right kind (index or table) of b-tree page. Otherwise
76329 assert( pPage->nCell>0 );
76330 assert( pPage->intKey );
76332 upr = pPage->nCell-1;
76334 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
76338 if( pPage->intKeyLeaf ){
76340 if( pCell>=pPage->aDataEnd ){
76348 if( lwr>upr ){ c = -1; break; }
76350 upr = idx-1;
76354 pCur->ix = (u16)idx;
76355 if( !pPage->leaf ){
76359 pCur->curFlags |= BTCF_ValidNKey;
76360 pCur->info.nKey = nCellKey;
76361 pCur->info.nSize = 0;
76369 assert( lwr==upr+1 || !pPage->leaf );
76370 assert( pPage->isInit );
76371 if( pPage->leaf ){
76372 assert( pCur->ix<pCur->pPage->nCell );
76373 pCur->ix = (u16)idx;
76379 if( lwr>=pPage->nCell ){
76380 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
76384 pCur->ix = (u16)lwr;
76389 pCur->info.nSize = 0;
76390 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
76395 ** Compare the "idx"-th cell on the page the cursor pCur is currently
76416 MemPage *pPage = pCur->pPage;
76422 if( nCell<=pPage->max1bytePayload ){
76423 /* This branch runs if the record-size field of the cell is a
76425 ** b-tree page. */
76426 testcase( pCell+nCell+1==pPage->aDataEnd );
76429 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
76431 /* The record-size field is a 2 byte varint and the record
76432 ** fits entirely on the main b-tree page. */
76433 testcase( pCell+nCell+2==pPage->aDataEnd );
76444 ** Return true (non-zero) if pCur is current pointing to the last
76449 assert( pCur->eState==CURSOR_VALID );
76450 for(i=0; i<pCur->iPage; i++){
76451 MemPage *pPage = pCur->apPage[i];
76452 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
76480 ** The pIdxKey->eqSeen field is set to 1 if there
76492 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
76494 assert( pCur->pKeyInfo!=0 );
76497 pCur->pBtree->nSeek++; /* Performance measurement during testing */
76501 pIdxKey->errCode = 0;
76502 assert( pIdxKey->default_rc==1
76503 || pIdxKey->default_rc==0
76504 || pIdxKey->default_rc==-1
76519 if( pCur->eState==CURSOR_VALID
76520 && pCur->pPage->leaf
76524 if( pCur->ix==pCur->pPage->nCell-1
76525 && (c = indexCellCompare(pCur, pCur->ix, pIdxKey, xRecordCompare))<=0
76526 && pIdxKey->errCode==SQLITE_OK
76531 if( pCur->iPage>0
76533 && pIdxKey->errCode==SQLITE_OK
76535 pCur->curFlags &= ~BTCF_ValidOvfl;
76536 if( !pCur->pPage->isInit ){
76541 pIdxKey->errCode = SQLITE_OK;
76547 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
76548 *pRes = -1;
76555 assert( pCur->pPage );
76556 assert( pCur->pPage->isInit );
76557 assert( pCur->eState==CURSOR_VALID );
76558 assert( pCur->pPage->nCell > 0 );
76559 assert( pCur->curIntKey==0 );
76564 MemPage *pPage = pCur->pPage;
76567 /* pPage->nCell must be greater than zero. If this is the root-page
76569 ** not run. If this is not the root-page, then the moveToChild() routine
76571 ** be the right kind (index or table) of b-tree page. Otherwise
76573 assert( pPage->nCell>0 );
76574 assert( pPage->intKey==0 );
76576 upr = pPage->nCell-1;
76582 /* The maximum supported page-size is 65536 bytes. This means that
76583 ** the maximum number of record bytes stored on an index B-Tree
76584 ** page is less than 16384 bytes and may be stored as a 2-byte
76587 ** stored entirely within the b-tree page by inspecting the first
76591 if( nCell<=pPage->max1bytePayload ){
76592 /* This branch runs if the record-size field of the cell is a
76594 ** b-tree page. */
76595 testcase( pCell+nCell+1==pPage->aDataEnd );
76598 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
76600 /* The record-size field is a 2 byte varint and the record
76601 ** fits entirely on the main b-tree page. */
76602 testcase( pCell+nCell+2==pPage->aDataEnd );
76615 u8 * const pCellBody = pCell - pPage->childPtrSize;
76617 pPage->xParseCell(pPage, pCellBody, &pCur->info);
76618 nCell = (int)pCur->info.nKey;
76623 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
76632 pCur->ix = (u16)idx;
76635 pCur->curFlags &= ~BTCF_ValidOvfl;
76644 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
76645 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
76650 upr = idx-1;
76655 pCur->ix = (u16)idx;
76656 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
76663 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
76664 assert( pPage->isInit );
76665 if( pPage->leaf ){
76666 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
76667 pCur->ix = (u16)idx;
76672 if( lwr>=pPage->nCell ){
76673 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
76678 /* This block is similar to an in-lined version of:
76680 ** pCur->ix = (u16)lwr;
76684 pCur->info.nSize = 0;
76685 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
76686 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
76689 pCur->aiIdx[pCur->iPage] = (u16)lwr;
76690 pCur->apPage[pCur->iPage] = pCur->pPage;
76691 pCur->ix = 0;
76692 pCur->iPage++;
76693 rc = getAndInitPage(pCur->pBt, chldPg, &pCur->pPage, pCur->curPagerFlags);
76695 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
76697 releasePage(pCur->pPage);
76701 pCur->pPage = pCur->apPage[--pCur->iPage];
76705 ***** End of in-lined moveToChild() call */
76708 pCur->info.nSize = 0;
76709 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
76726 return (CURSOR_VALID!=pCur->eState);
76739 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
76744 if( pCur->eState!=CURSOR_VALID ) return 0;
76745 if( NEVER(pCur->pPage->leaf==0) ) return -1;
76747 n = pCur->pPage->nCell;
76748 for(i=0; i<pCur->iPage; i++){
76749 n *= pCur->apPage[i]->nCell;
76780 if( pCur->eState!=CURSOR_VALID ){
76781 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
76786 if( CURSOR_INVALID==pCur->eState ){
76789 if( pCur->eState==CURSOR_SKIPNEXT ){
76790 pCur->eState = CURSOR_VALID;
76791 if( pCur->skipNext>0 ) return SQLITE_OK;
76795 pPage = pCur->pPage;
76796 idx = ++pCur->ix;
76797 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
76798 if( !pPage->isInit ){
76802 if( idx>=pPage->nCell ){
76803 if( !pPage->leaf ){
76804 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
76809 if( pCur->iPage==0 ){
76810 pCur->eState = CURSOR_INVALID;
76814 pPage = pCur->pPage;
76815 }while( pCur->ix>=pPage->nCell );
76816 if( pPage->intKey ){
76822 if( pPage->leaf ){
76833 pCur->info.nSize = 0;
76834 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
76835 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
76836 pPage = pCur->pPage;
76837 if( (++pCur->ix)>=pPage->nCell ){
76838 pCur->ix--;
76841 if( pPage->leaf ){
76873 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
76874 assert( pCur->info.nSize==0 );
76875 if( pCur->eState!=CURSOR_VALID ){
76880 if( CURSOR_INVALID==pCur->eState ){
76883 if( CURSOR_SKIPNEXT==pCur->eState ){
76884 pCur->eState = CURSOR_VALID;
76885 if( pCur->skipNext<0 ) return SQLITE_OK;
76889 pPage = pCur->pPage;
76890 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
76891 if( !pPage->isInit ){
76894 if( !pPage->leaf ){
76895 int idx = pCur->ix;
76900 while( pCur->ix==0 ){
76901 if( pCur->iPage==0 ){
76902 pCur->eState = CURSOR_INVALID;
76907 assert( pCur->info.nSize==0 );
76908 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
76910 pCur->ix--;
76911 pPage = pCur->pPage;
76912 if( pPage->intKey && !pPage->leaf ){
76924 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
76925 pCur->info.nSize = 0;
76926 if( pCur->eState!=CURSOR_VALID
76927 || pCur->ix==0
76928 || pCur->pPage->leaf==0
76932 pCur->ix--;
76953 ** anywhere on the free-list, then it is guaranteed to be returned. If
76973 assert( sqlite3_mutex_held(pBt->mutex) );
76974 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
76975 pPage1 = pBt->pPage1;
76977 /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36
76979 n = get4byte(&pPage1->aData[36]);
76980 testcase( n==mxPage-1 );
76987 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
76990 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
76991 ** shows that the page 'nearby' is somewhere on the free-list, then
76992 ** the entire-list will be searched for that page.
76999 assert( pBt->autoVacuum );
77011 /* Decrement the free-list count by 1. Set iTrunk to the index of the
77012 ** first free-list trunk page. iPrevTrunk is initially 1.
77014 rc = sqlite3PagerWrite(pPage1->pDbPage);
77016 put4byte(&pPage1->aData[36], n-1);
77019 ** is not true. Otherwise, it runs once for each trunk-page on the
77020 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
77026 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
77029 iTrunk = get4byte(&pPrevTrunk->aData[0]);
77031 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
77034 iTrunk = get4byte(&pPage1->aData[32]);
77038 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
77047 assert( pTrunk->aData!=0 );
77048 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
77050 k = get4byte(&pTrunk->aData[4]);
77056 rc = sqlite3PagerWrite(pTrunk->pDbPage);
77061 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
77064 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
77065 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
77079 rc = sqlite3PagerWrite(pTrunk->pDbPage);
77085 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
77087 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
77091 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
77095 ** pointers to free-list leaves. The first leaf becomes a trunk
77099 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
77109 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
77114 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
77115 put4byte(&pNewTrunk->aData[4], k-1);
77116 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
77119 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
77120 put4byte(&pPage1->aData[32], iNewTrunk);
77122 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
77126 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
77130 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
77136 unsigned char *aData = pTrunk->aData;
77150 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
77152 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
77177 *pPgno, closest+1, k, pTrunk->pgno, n-1));
77178 rc = sqlite3PagerWrite(pTrunk->pDbPage);
77180 if( closest<k-1 ){
77183 put4byte(&aData[4], k-1);
77187 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
77204 ** pager layer with the 'no-content' flag set. This prevents the pager
77206 ** current transaction has already run one or more incremental-vacuum
77209 ** not set the no-content flag. This causes the pager to load and journal
77214 ** file on disk. So the effects of disabling the no-content optimization
77218 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
77220 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
77222 pBt->nPage++;
77223 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
77226 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
77227 /* If *pPgno refers to a pointer-map page, allocate two new pages
77229 ** becomes a new pointer-map page, the second is used by the caller.
77232 TRACE(("ALLOCATE: %u from end of file (pointer-map page)\n", pBt->nPage));
77233 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
77234 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
77236 rc = sqlite3PagerWrite(pPg->pDbPage);
77240 pBt->nPage++;
77241 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
77244 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
77245 *pPgno = pBt->nPage;
77250 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
77263 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
77264 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
77269 ** This function is used to add page iPage to the database file free-list.
77270 ** It is assumed that the page is not already a part of the free-list.
77281 MemPage *pTrunk = 0; /* Free-list trunk page */
77282 Pgno iTrunk = 0; /* Page number of free-list trunk page */
77283 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
77286 u32 nFree; /* Initial number of pages on free-list */
77288 assert( sqlite3_mutex_held(pBt->mutex) );
77290 assert( !pMemPage || pMemPage->pgno==iPage );
77292 if( iPage<2 || iPage>pBt->nPage ){
77297 sqlite3PagerRef(pPage->pDbPage);
77303 rc = sqlite3PagerWrite(pPage1->pDbPage);
77305 nFree = get4byte(&pPage1->aData[36]);
77306 put4byte(&pPage1->aData[36], nFree+1);
77308 if( pBt->btsFlags & BTS_SECURE_DELETE ){
77313 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
77317 memset(pPage->aData, 0, pPage->pBt->pageSize);
77320 /* If the database supports auto-vacuum, write an entry in the pointer-map
77328 /* Now manipulate the actual database free-list structure. There are two
77329 ** possibilities. If the free-list is currently empty, or if the first
77330 ** trunk page in the free-list is full, then this page will become a
77331 ** new free-list trunk page. Otherwise, it will become a leaf of the
77332 ** first trunk page in the current free-list. This block tests if it
77333 ** is possible to add the page as a new free-list leaf.
77338 iTrunk = get4byte(&pPage1->aData[32]);
77348 nLeaf = get4byte(&pTrunk->aData[4]);
77349 assert( pBt->usableSize>32 );
77350 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
77354 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
77359 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
77362 ** usableSize/4 - 8 entries will be reported as corrupt. In order
77364 ** we will continue to restrict the number of entries to usableSize/4 - 8
77367 ** to read "usableSize/4-2" instead of "usableSize/4-8".
77369 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
77374 rc = sqlite3PagerWrite(pTrunk->pDbPage);
77376 put4byte(&pTrunk->aData[4], nLeaf+1);
77377 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
77378 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
77379 sqlite3PagerDontWrite(pPage->pDbPage);
77383 TRACE(("FREE-PAGE: %u leaf on trunk page %u\n",pPage->pgno,pTrunk->pgno));
77389 ** the page being freed as a leaf page of the first trunk in the free-list.
77390 ** Possibly because the free-list is empty, or possibly because the
77391 ** first trunk in the free-list is full. Either way, the page being freed
77392 ** will become the new first trunk page in the free-list.
77397 rc = sqlite3PagerWrite(pPage->pDbPage);
77401 put4byte(pPage->aData, iTrunk);
77402 put4byte(&pPage->aData[4], 0);
77403 put4byte(&pPage1->aData[32], iPage);
77404 TRACE(("FREE-PAGE: %u new trunk page replacing %u\n", pPage->pgno, iTrunk));
77408 pPage->isInit = 0;
77416 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
77434 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77435 assert( pInfo->nLocal!=pInfo->nPayload );
77436 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
77437 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
77438 if( pCell + pInfo->nSize > pPage->aDataEnd ){
77442 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
77443 pBt = pPage->pBt;
77444 assert( pBt->usableSize > 4 );
77445 ovflPageSize = pBt->usableSize - 4;
77446 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
77448 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
77450 while( nOvfl-- ){
77465 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
77472 ** freePage2() may zero the page contents if secure-delete mode is
77483 sqlite3PagerUnref(pOvfl->pDbPage);
77498 pPage->xParseCell(pPage, pCell, &sInfo); \
77513 ** Note that pCell does not necessary need to point to the pPage->aData
77515 ** be constructed in this temporary area then copied into pPage->aData
77535 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77539 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
77540 || sqlite3PagerIswriteable(pPage->pDbPage) );
77543 nHeader = pPage->childPtrSize;
77544 if( pPage->intKey ){
77545 nPayload = pX->nData + pX->nZero;
77546 pSrc = pX->pData;
77547 nSrc = pX->nData;
77548 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
77550 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
77552 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
77553 nSrc = nPayload = (int)pX->nKey;
77554 pSrc = pX->pKey;
77560 if( nPayload<=pPage->maxLocal ){
77574 memset(pPayload+nSrc, 0, nPayload-nSrc);
77581 mn = pPage->minLocal;
77582 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
77583 testcase( n==pPage->maxLocal );
77584 testcase( n==pPage->maxLocal+1 );
77585 if( n > pPage->maxLocal ) n = mn;
77591 pBt = pPage->pBt;
77608 pPage->xParseCell(pPage, pCell, &info);
77609 assert( nHeader==(int)(info.pPayload - pCell) );
77610 assert( info.nKey==pX->nKey );
77623 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
77627 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
77628 || sqlite3PagerIswriteable(pPage->pDbPage) );
77638 nPayload -= n;
77642 nSrc -= n;
77643 spaceLeft -= n;
77647 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
77648 if( pBt->autoVacuum ){
77658 /* If the database supports auto-vacuum, and the second or subsequent
77659 ** overflow page is being allocated, add an entry to the pointer-map
77663 ** to the pointer-map. If we write nothing to this pointer-map slot,
77668 if( pBt->autoVacuum && rc==SQLITE_OK ){
77683 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
77687 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
77688 || sqlite3PagerIswriteable(pPage->pDbPage) );
77693 pPrior = pOvfl->aData;
77695 pPayload = &pOvfl->aData[4];
77696 spaceLeft = pBt->usableSize - 4;
77704 ** Remove the i-th cell from pPage. This routine effects pPage only.
77713 u8 *data; /* pPage->aData */
77720 assert( idx<pPage->nCell );
77722 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
77723 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77724 assert( pPage->nFree>=0 );
77725 data = pPage->aData;
77726 ptr = &pPage->aCellIdx[2*idx];
77727 assert( pPage->pBt->usableSize > (u32)(ptr-data) );
77729 hdr = pPage->hdrOffset;
77731 testcase( pc+sz==pPage->pBt->usableSize );
77732 if( pc+sz > pPage->pBt->usableSize ){
77741 pPage->nCell--;
77742 if( pPage->nCell==0 ){
77745 put2byte(&data[hdr+5], pPage->pBt->usableSize);
77746 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
77747 - pPage->childPtrSize - 8;
77749 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
77750 put2byte(&data[hdr+3], pPage->nCell);
77751 pPage->nFree += 2;
77762 ** in pPage->apOvfl[] and make it point to the cell content (either
77764 ** Allocating a new entry in pPage->aCell[] implies that
77765 ** pPage->nOverflow is incremented.
77777 int i, /* New cell becomes the i-th cell of the page */
77781 Pgno iChild /* If non-zero, replace first 4 bytes with this value */
77786 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
77788 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
77789 assert( MX_CELL(pPage->pBt)<=10921 );
77790 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
77791 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
77792 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
77793 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77794 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
77795 assert( pPage->nFree>=0 );
77797 if( pPage->nOverflow || sz+2>pPage->nFree ){
77803 j = pPage->nOverflow++;
77804 /* Comparison against ArraySize-1 since we hold back one extra slot
77807 assert( j < ArraySize(pPage->apOvfl)-1 );
77808 pPage->apOvfl[j] = pCell;
77809 pPage->aiOvfl[j] = (u16)i;
77816 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
77817 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
77819 int rc = sqlite3PagerWrite(pPage->pDbPage);
77823 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
77824 data = pPage->aData;
77825 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
77831 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
77832 assert( idx+sz <= (int)pPage->pBt->usableSize );
77833 pPage->nFree -= (u16)(2 + sz);
77839 memcpy(&data[idx+4], pCell+4, sz-4);
77841 pIns = pPage->aCellIdx + i*2;
77842 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
77844 pPage->nCell++;
77846 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
77847 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
77849 if( pPage->pBt->autoVacuum ){
77874 int i, /* New cell becomes the i-th cell of the page */
77881 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
77883 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
77884 assert( MX_CELL(pPage->pBt)<=10921 );
77885 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
77886 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
77887 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
77888 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77889 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
77890 assert( pPage->nFree>=0 );
77891 assert( pPage->nOverflow==0 );
77892 if( sz+2>pPage->nFree ){
77893 j = pPage->nOverflow++;
77894 /* Comparison against ArraySize-1 since we hold back one extra slot
77897 assert( j < ArraySize(pPage->apOvfl)-1 );
77898 pPage->apOvfl[j] = pCell;
77899 pPage->aiOvfl[j] = (u16)i;
77906 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
77907 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
77909 int rc = sqlite3PagerWrite(pPage->pDbPage);
77913 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
77914 data = pPage->aData;
77915 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
77921 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
77922 assert( idx+sz <= (int)pPage->pBt->usableSize );
77923 pPage->nFree -= (u16)(2 + sz);
77925 pIns = pPage->aCellIdx + i*2;
77926 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
77928 pPage->nCell++;
77930 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
77931 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
77933 if( pPage->pBt->autoVacuum ){
77959 ** tunable - as if you could change them and recompile and it would all work.
77974 ** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
77986 ** -----------
77988 ** -----------
77991 ** --------- --------- ---------
77992 ** |Child-1| |Child-2| |Child-3|
77993 ** --------- --------- ---------
77997 ** 1. All cells from Child-1 in order
77999 ** 3. All cells from Child-2 in order
78001 ** 5. All cells from Child-3 in order
78003 ** For a table-btree (with rowids) the items 2 and 4 are empty because
78007 ** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
78011 ** ixNx[0] = Number of cells in Child-1.
78012 ** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
78013 ** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
78014 ** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
78017 ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
78020 ** ixNx[0] = Number of cells in Child-1.
78021 ** ixNx[1] = Number of cells in Child-1 and Child-2.
78040 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
78044 MemPage *pRef = p->pRef;
78045 u16 *szCell = p->szCell;
78046 assert( idx>=0 && idx+N<=p->nCell );
78048 assert( p->apCell[idx]!=0 );
78050 szCell[idx] = pRef->xCellSize(pRef, p->apCell[idx]);
78053 szCell[idx]==pRef->xCellSize(pRef, p->apCell[idx]) );
78056 N--;
78064 assert( N>=0 && N<p->nCell );
78065 assert( p->szCell[N]==0 );
78066 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
78067 return p->szCell[N];
78070 assert( N>=0 && N<p->nCell );
78071 if( p->szCell[N] ) return p->szCell[N];
78076 ** Array apCell[] contains pointers to nCell b-tree page cells. The
78094 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
78095 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
78096 const int usableSize = pPg->pBt->usableSize;
78101 u8 *pCellptr = pPg->aCellIdx;
78102 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
78104 int k; /* Current slot in pCArray->apEnd[] */
78105 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
78111 memcpy(&pTmp[j], &aData[j], usableSize - j);
78113 for(k=0; ALWAYS(k<NB*2) && pCArray->ixNx[k]<=i; k++){}
78114 pSrcEnd = pCArray->apEnd[k];
78118 u8 *pCell = pCArray->apCell[i];
78119 u16 sz = pCArray->szCell[i];
78123 pCell = &pTmp[pCell - aData];
78130 pData -= sz;
78131 put2byte(pCellptr, (pData - aData));
78135 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
78138 if( pCArray->ixNx[k]<=i ){
78140 pSrcEnd = pCArray->apEnd[k];
78144 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
78145 pPg->nCell = nCell;
78146 pPg->nOverflow = 0;
78149 put2byte(&aData[hdr+3], pPg->nCell);
78150 put2byte(&aData[hdr+5], pData - aData);
78156 ** The pCArray objects contains pointers to b-tree cells and the cell sizes.
78159 ** will fit), non-zero is returned. Otherwise, if the cells are added
78162 ** Argument pCellptr points to the first entry in the cell-pointer array
78164 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
78166 ** that it is safe to overwrite this part of the cell-pointer array.
78174 ** end of the space required by this page for the cell-pointer area (for
78175 ** all cells - not just those inserted by the current call). If the content
78177 ** cells in apCell[], then the cells do not fit and non-zero is returned.
78181 u8 *pBegin, /* End of cell-pointer array */
78182 u8 **ppData, /* IN/OUT: Page content-area pointer */
78183 u8 *pCellptr, /* Pointer to cell-pointer area */
78188 int i = iFirst; /* Loop counter - cell index to insert */
78189 u8 *aData = pPg->aData; /* Complete page */
78192 int k; /* Current slot in pCArray->apEnd[] */
78194 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
78196 for(k=0; ALWAYS(k<NB*2) && pCArray->ixNx[k]<=i ; k++){}
78197 pEnd = pCArray->apEnd[k];
78201 assert( pCArray->szCell[i]!=0 );
78202 sz = pCArray->szCell[i];
78204 if( (pData - pBegin)<sz ) return 1;
78205 pData -= sz;
78208 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
78211 assert( (pSlot+sz)<=pCArray->apCell[i]
78212 || pSlot>=(pCArray->apCell[i]+sz)
78214 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
78215 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
78221 memmove(pSlot, pCArray->apCell[i], sz);
78222 put2byte(pCellptr, (pSlot - aData));
78226 if( pCArray->ixNx[k]<=i ){
78228 pEnd = pCArray->apEnd[k];
78236 ** The pCArray object contains pointers to b-tree cells and their sizes.
78239 ** that is currently stored within the body of pPg to the pPg free-list.
78240 ** The cell-pointers and other fields of the page are not updated.
78242 ** This function returns the total number of cells added to the free-list.
78250 u8 * const aData = pPg->aData;
78251 u8 * const pEnd = &aData[pPg->pBt->usableSize];
78252 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
78261 u8 *pCell = pCArray->apCell[i];
78269 sz = pCArray->szCell[i]; assert( sz>0 );
78270 iOfst = (u16)(pCell - aData);
78284 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
78297 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
78304 ** balanced. The current page, pPg, has pPg->nCell cells starting with
78305 ** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
78311 ** The pPg->nFree field is invalid when this function returns. It is the
78321 u8 * const aData = pPg->aData;
78322 const int hdr = pPg->hdrOffset;
78323 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
78324 int nCell = pPg->nCell; /* Cells stored on pPg */
78328 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
78332 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
78333 memcpy(pTmp, aData, pPg->pBt->usableSize);
78339 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
78341 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
78342 nCell -= nShift;
78345 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
78347 nCell -= nTail;
78352 if( NEVER(pData>pPg->aDataEnd) ) goto editpage_fail;
78356 int nAdd = MIN(nNew,iOld-iNew);
78357 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
78359 pCellptr = pPg->aCellIdx;
78369 for(i=0; i<pPg->nOverflow; i++){
78370 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
78372 pCellptr = &pPg->aCellIdx[iCell * 2];
78374 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
78387 pCellptr = &pPg->aCellIdx[nCell*2];
78390 iNew+nCell, nNew-nCell, pCArray
78393 pPg->nCell = nNew;
78394 pPg->nOverflow = 0;
78396 put2byte(&aData[hdr+3], pPg->nCell);
78397 put2byte(&aData[hdr+5], pData - aData);
78401 u8 *pCell = pCArray->apCell[i+iNew];
78402 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
78403 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
78404 pCell = &pTmp[pCell - aData];
78407 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
78423 ** a new entry is being inserted on the extreme right-end of the
78427 ** Instead of trying to balance the 3 right-most leaf pages, just add
78428 ** a new page to the right-hand side and put the one new entry in
78434 ** pPage is the leaf page which is the right-most page in the tree.
78436 ** which is also the right-most entry on the page.
78445 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
78450 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
78451 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
78452 assert( pPage->nOverflow==1 );
78454 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
78455 assert( pPage->nFree>=0 );
78456 assert( pParent->nFree>=0 );
78458 /* Allocate a new page. This page will become the right-sibling of
78467 u8 *pCell = pPage->apOvfl[0];
78468 u16 szCell = pPage->xCellSize(pPage, pCell);
78472 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
78473 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
78479 b.apEnd[0] = pPage->aDataEnd;
78486 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
78488 /* If this is an auto-vacuum database, update the pointer map
78498 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
78499 if( szCell>pNew->minLocal ){
78505 ** consists of a 4-byte page number (the page number of pPage) and
78509 ** To find the largest key value on pPage, first find the right-most
78511 ** record-length (a variable length integer at most 32-bits in size)
78513 ** The first of the while(...) loops below skips over the record-length
78517 pCell = findCell(pPage, pPage->nCell-1);
78525 rc = insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
78526 0, pPage->pgno);
78529 /* Set the right-child pointer of pParent to point to the new page. */
78530 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
78544 ** for setting pointer-map entries.
78552 BtShared *pBt = pPage->pBt;
78553 assert( pPage->isInit );
78555 for(j=0; j<pPage->nCell; j++){
78560 pPage->xParseCell(pPage, z, &info);
78562 Pgno ovfl = get4byte(&z[info.nSize-4]);
78564 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
78566 if( !pPage->leaf ){
78569 assert( n==pPage->pgno && e==PTRMAP_BTREE );
78572 if( !pPage->leaf ){
78573 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
78575 assert( n==pPage->pgno && e==PTRMAP_BTREE );
78583 ** This function is used to copy the contents of the b-tree node stored
78585 ** the pointer-map entries for each child page are updated so that the
78601 BtShared * const pBt = pFrom->pBt;
78602 u8 * const aFrom = pFrom->aData;
78603 u8 * const aTo = pTo->aData;
78604 int const iFromHdr = pFrom->hdrOffset;
78605 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
78610 assert( pFrom->isInit );
78611 assert( pFrom->nFree>=iToHdr );
78612 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
78614 /* Copy the b-tree node content from page pFrom to page pTo. */
78616 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
78617 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
78624 pTo->isInit = 0;
78632 /* If this is an auto-vacuum database, update the pointer-map entries
78633 ** for any b-tree or overflow pages that pTo now contains the pointers to.
78675 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
78684 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
78685 int isRoot, /* True if pParent is a root-page */
78693 int nxDiv; /* Next divider slot in pParent->aCell[] */
78698 int pageFlags; /* Value of pPage->aData[0] */
78704 u8 *pRight; /* Location in parent of right-sibling pointer */
78705 u8 *apDiv[NB-1]; /* Divider cells in pParent */
78706 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
78708 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
78717 pBt = pParent->pBt;
78718 assert( sqlite3_mutex_held(pBt->mutex) );
78719 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
78726 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
78727 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
78732 assert( pParent->nFree>=0 );
78745 i = pParent->nOverflow + pParent->nCell;
78753 nxDiv = i-2+bBulk;
78755 nxDiv = iParentIdx-1;
78757 i = 2-bBulk;
78760 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
78761 pRight = &pParent->aData[pParent->hdrOffset+8];
78763 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
78774 if( apOld[i]->nFree<0 ){
78781 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl);
78782 if( (i--)==0 ) break;
78784 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
78785 apDiv[i] = pParent->apOvfl[0];
78787 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
78788 pParent->nOverflow = 0;
78790 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
78792 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
78801 ** But not if we are in secure-delete mode. In secure-delete mode,
78806 if( pBt->btsFlags & BTS_FAST_SECURE ){
78811 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
78812 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
78814 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
78817 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
78821 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
78831 + pBt->pageSize; /* aSpace1 */
78833 assert( szScratch<=7*(int)pBt->pageSize );
78860 leafCorrection = b.pRef->leaf*4;
78861 leafData = b.pRef->intKeyLeaf;
78864 int limit = pOld->nCell;
78865 u8 *aData = pOld->aData;
78866 u16 maskPage = pOld->maskPage;
78867 u8 *piCell = aData + pOld->cellOffset;
78871 /* Verify that all sibling pages are of the same "type" (table-leaf,
78872 ** table-interior, index-leaf, or index-interior).
78874 if( pOld->aData[0]!=apOld[0]->aData[0] ){
78896 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
78897 if( pOld->nOverflow>0 ){
78898 if( NEVER(limit<pOld->aiOvfl[0]) ){
78902 limit = pOld->aiOvfl[0];
78908 for(k=0; k<pOld->nOverflow; k++){
78909 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
78910 b.apCell[b.nCell] = pOld->apOvfl[k];
78914 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
78921 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
78924 if( i<nOld-1 && !leafData){
78931 assert( sz<=pBt->maxLocal+23 );
78932 assert( iSpace1 <= (int)pBt->pageSize );
78936 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
78937 if( !pOld->leaf ){
78939 assert( pOld->hdrOffset==0 || CORRUPT_DB );
78942 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
78949 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
78961 ** size of all cells on the i-th page and cntNew[] which is the index
78968 ** szNew[i]: Spaced used on the i-th sibling page.
78970 ** the right of the i-th sibling page.
78974 usableSpace = pBt->usableSize - 12 + leafCorrection;
78977 b.apEnd[k] = p->aDataEnd;
78979 if( k && b.ixNx[k]==b.ixNx[k-1] ){
78980 k--; /* Omit b.ixNx[] entry for child pages with no cells */
78984 b.apEnd[k] = pParent->aDataEnd;
78987 assert( p->nFree>=0 );
78988 szNew[i] = usableSpace - p->nFree;
78989 for(j=0; j<p->nOverflow; j++){
78990 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
79001 szNew[k-1] = 0;
79002 cntNew[k-1] = b.nCell;
79004 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
79005 szNew[i] -= sz;
79014 cntNew[i]--;
79028 szNew[i+1] -= sz;
79032 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
79041 ** always nearly full, while the right-most sibling might be nearly empty.
79046 ** be so out of balance as to be illegal. For example, the right-most
79049 for(i=k-1; i>0; i--){
79051 int szLeft = szNew[i-1]; /* Size of sibling on the left */
79052 int r; /* Index of right-most cell in left sibling */
79055 r = cntNew[i-1] - 1;
79056 d = r + 1 - leafData;
79065 && (bBulk || szRight+szD+2 > szLeft-(szR+(i==k-1?0:2)))){
79069 szLeft -= szR + 2;
79070 cntNew[i-1] = r;
79071 r--;
79072 d--;
79075 szNew[i-1] = szLeft;
79076 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
79082 /* Sanity check: For a non-corrupt database file one of the following
79089 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
79091 apOld[0]->pgno, apOld[0]->nCell,
79092 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
79093 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
79099 pageFlags = apOld[0]->aData[0];
79105 rc = sqlite3PagerWrite(pNew->pDbPage);
79107 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
79122 /* Set the pointer-map entry for the new sibling page. */
79124 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
79145 aPgno[i] = apNew[i]->pgno;
79146 assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE );
79147 assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY );
79149 for(i=0; i<nNew-1; i++){
79152 if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j;
79161 Pgno pgnoA = apNew[i]->pgno;
79162 Pgno pgnoB = apNew[iB]->pgno;
79163 Pgno pgnoTemp = (PENDING_BYTE/pBt->pageSize)+1;
79164 u16 fgA = apNew[i]->pDbPage->flags;
79165 u16 fgB = apNew[iB]->pDbPage->flags;
79166 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB);
79167 sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA);
79168 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB);
79169 apNew[i]->pgno = pgnoB;
79170 apNew[iB]->pgno = pgnoA;
79176 apNew[0]->pgno, szNew[0], cntNew[0],
79177 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
79178 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
79179 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
79180 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
79181 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
79182 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
79183 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
79184 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
79187 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
79189 assert( apNew[nNew-1]!=0 );
79190 put4byte(pRight, apNew[nNew-1]->pgno);
79192 /* If the sibling pages are not leaves, ensure that the right-child pointer
79193 ** of the right-most new sibling page is set to the value that was
79194 ** originally in the same field of the right-most old sibling page. */
79196 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
79197 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
79212 ** associated with the right-child of each sibling may also need to be
79219 int cntOldNext = pNew->nCell + pNew->nOverflow;
79230 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
79244 || pNew->pgno!=aPgno[iOld]
79245 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
79248 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
79250 if( cachedCellSize(&b,i)>pNew->minLocal ){
79259 for(i=0; i<nNew-1; i++){
79272 if( !pNew->leaf ){
79273 memcpy(&pNew->aData[8], pCell, 4);
79275 /* If the tree is a leaf-data tree, and the siblings are leaves,
79277 ** cell consists of the integer key for the right-most cell of
79278 ** the sibling-page assembled above only.
79281 j--;
79282 pNew->xParseCell(pNew, b.apCell[j], &info);
79287 pCell -= 4;
79288 /* Obscure case for non-leaf-data trees: If the cell at pCell was
79295 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
79301 sz = pParent->xCellSize(pParent, pCell);
79305 assert( sz<=pBt->maxLocal+23 );
79306 assert( iOvflSpace <= (int)pBt->pageSize );
79313 rc = insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno);
79315 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
79322 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
79324 ** the left-hand sibling apNew[iPg-1] has been updated.
79328 ** the right-hand sibling apNew[iPg+1] has been updated.
79332 ** The iPg value in the following loop starts at nNew-1 goes down
79333 ** to 0, then back up to nNew-1 again, thus making two passes over
79340 for(i=1-nNew; i<nNew; i++){
79341 int iPg = i<0 ? -i : i;
79347 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
79354 ** only after iPg-1 has already been updated. */
79355 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
79365 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
79366 iNew = cntNew[iPg-1] + !leafData;
79367 nNewCell = cntNew[iPg] - iNew;
79373 apNew[iPg]->nFree = usableSpace-szNew[iPg];
79374 assert( apNew[iPg]->nOverflow==0 );
79375 assert( apNew[iPg]->nCell==nNewCell );
79385 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
79386 /* The root page of the b-tree now contains no cells. The only sibling
79387 ** page is the right-child of the parent. Copy the contents of the
79389 ** b-tree structure by one. This is described as the "balance-shallower"
79390 ** sub-algorithm in some documentation.
79392 ** If this is an auto-vacuum database, the call to copyNodeContent()
79393 ** sets all pointer-map entries corresponding to database image pages
79402 rc = defragmentPage(apNew[0], -1);
79404 assert( apNew[0]->nFree ==
79405 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
79406 - apNew[0]->nCell*2)
79412 /* Fix the pointer map entries associated with the right-child of each
79416 u32 key = get4byte(&apNew[i]->aData[8]);
79417 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
79421 assert( pParent->isInit );
79432 if( ISAUTOVACUUM(pBt) && rc==SQLITE_OK && apNew[0]->isInit ){
79459 ** This function is called when the root page of a b-tree structure is
79464 ** page is then overwritten to make it an empty page with the right-child
79467 ** Before returning, all pointer-map entries corresponding to pages
79468 ** that the new child-page now contains pointers to are updated. The
79469 ** entry corresponding to the new right-child pointer of the root
79481 BtShared *pBt = pRoot->pBt; /* The BTree */
79483 assert( pRoot->nOverflow>0 );
79484 assert( sqlite3_mutex_held(pBt->mutex) );
79486 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
79487 ** page that will become the new right-child of pPage. Copy the contents
79490 rc = sqlite3PagerWrite(pRoot->pDbPage);
79492 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
79495 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
79503 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
79504 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
79505 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
79507 TRACE(("BALANCE: copy root %u into %u\n", pRoot->pgno, pChild->pgno));
79510 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
79511 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
79512 memcpy(pChild->apOvfl, pRoot->apOvfl,
79513 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
79514 pChild->nOverflow = pRoot->nOverflow;
79516 /* Zero the contents of pRoot. Then install pChild as the right-child. */
79517 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
79518 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
79526 ** on the same B-tree as pCur.
79529 ** pointing to the same b-tree. If an insert occurs on one SQL table
79531 ** table linked to the same b-tree. If the secondary insert causes a
79537 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
79539 && pOther->eState==CURSOR_VALID
79540 && pOther->pPage==pCur->pPage
79542 return SQLITE_CORRUPT_PAGE(pCur->pPage);
79568 MemPage *pPage = pCur->pPage;
79570 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
79571 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
79577 }else if( (iPage = pCur->iPage)==0 ){
79578 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
79579 /* The root page of the b-tree is overfull. In this case call the
79580 ** balance_deeper() function to create a new child for the root-page
79581 ** and copy the current contents of the root-page to it. The
79582 ** next iteration of the do-loop will balance the child page.
79586 rc = balance_deeper(pPage, &pCur->apPage[1]);
79588 pCur->iPage = 1;
79589 pCur->ix = 0;
79590 pCur->aiIdx[0] = 0;
79591 pCur->apPage[0] = pPage;
79592 pCur->pPage = pCur->apPage[1];
79593 assert( pCur->pPage->nOverflow );
79598 }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
79604 MemPage * const pParent = pCur->apPage[iPage-1];
79605 int const iIdx = pCur->aiIdx[iPage-1];
79607 rc = sqlite3PagerWrite(pParent->pDbPage);
79608 if( rc==SQLITE_OK && pParent->nFree<0 ){
79613 if( pPage->intKeyLeaf
79614 && pPage->nOverflow==1
79615 && pPage->aiOvfl[0]==pPage->nCell
79616 && pParent->pgno!=1
79617 && pParent->nCell==iIdx
79622 ** happens, the next iteration of the do-loop will balance pParent
79641 ** become overfull or underfull. The next iteration of the do-loop
79646 ** A subsequent iteration of the do-loop will deal with this by
79648 ** but it doesn't deal with overflow cells - just moves them to a
79655 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
79657 pCur->hints&BTREE_BULKLOAD);
79673 pPage->nOverflow = 0;
79675 /* The next iteration of the do-loop balances the parent page. */
79677 pCur->iPage--;
79678 assert( pCur->iPage>=0 );
79679 pCur->pPage = pCur->apPage[pCur->iPage];
79699 int nData = pX->nData - iOffset;
79705 int rc = sqlite3PagerWrite(pPage->pDbPage);
79707 memset(pDest + i, 0, iAmt - i);
79714 iAmt-nData);
79718 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
79719 int rc = sqlite3PagerWrite(pPage->pDbPage);
79725 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
79740 int iOffset; /* Next byte of pX->pData to write */
79741 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
79743 MemPage *pPage = pCur->pPage; /* Page being written */
79748 assert( pCur->info.nLocal<nTotal ); /* pCur is an overflow cell */
79751 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
79752 0, pCur->info.nLocal);
79756 iOffset = pCur->info.nLocal;
79759 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
79760 pBt = pPage->pBt;
79761 ovflPageSize = pBt->usableSize - 4;
79765 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
79769 ovflPgno = get4byte(pPage->aData);
79771 ovflPageSize = nTotal - iOffset;
79773 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
79776 sqlite3PagerUnref(pPage->pDbPage);
79788 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
79789 MemPage *pPage = pCur->pPage; /* Page being written */
79791 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
79792 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
79796 if( pCur->info.nLocal==nTotal ){
79798 return btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
79799 0, pCur->info.nLocal);
79822 ** If the seekResult parameter is non-zero, then a successful call to
79833 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
79834 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
79844 int loc = seekResult; /* -1: before desired location +1: after */
79848 Btree *p = pCur->pBtree;
79853 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
79857 ** In some cases, the call to btreeMoveto() below is a no-op. For
79858 ** example, when inserting data into a table with auto-generated integer
79861 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
79866 if( pCur->curFlags & BTCF_Multiple ){
79867 rc = saveAllCursors(p->pBt, pCur->pgnoRoot, pCur);
79869 if( loc && pCur->iPage<0 ){
79875 return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot);
79882 if( pCur->eState>=CURSOR_REQUIRESEEK ){
79883 testcase( pCur->eState==CURSOR_REQUIRESEEK );
79884 testcase( pCur->eState==CURSOR_FAULT );
79890 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
79891 && p->pBt->inTransaction==TRANS_WRITE
79892 && (p->pBt->btsFlags & BTS_READ_ONLY)==0 );
79893 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
79896 ** expecting an index b-tree, then the caller should be inserting blob
79900 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
79902 if( pCur->pKeyInfo==0 ){
79903 assert( pX->pKey==0 );
79904 /* If this is an insert into a table b-tree, invalidate any incrblob
79906 if( p->hasIncrblobCur ){
79907 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
79915 assert( pCur->curFlags & BTCF_ValidNKey );
79916 assert( pX->nKey==pCur->info.nKey );
79925 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
79928 assert( pX->nData>=0 && pX->nZero>=0 );
79929 if( pCur->info.nSize!=0
79930 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
79941 rc = sqlite3BtreeTableMoveto(pCur, pX->nKey,
79959 if( pX->nMem ){
79961 r.pKeyInfo = pCur->pKeyInfo;
79962 r.aMem = pX->aMem;
79963 r.nField = pX->nMem;
79968 rc = btreeMoveto(pCur, pX->pKey, pX->nKey,
79980 if( pCur->info.nKey==pX->nKey ){
79982 x2.pData = pX->pKey;
79983 x2.nData = pX->nKey;
79989 assert( pCur->eState==CURSOR_VALID
79990 || (pCur->eState==CURSOR_INVALID && loc) || CORRUPT_DB );
79992 pPage = pCur->pPage;
79993 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
79994 assert( pPage->leaf || !pPage->intKey );
79995 if( pPage->nFree<0 ){
79996 if( NEVER(pCur->eState>CURSOR_INVALID) ){
79997 /* ^^^^^--- due to the moveToRoot() call above */
80006 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
80008 assert( pPage->isInit || CORRUPT_DB );
80009 newCell = p->pBt->pTmpSpace;
80014 szNew = p->pBt->nPreformatSize;
80019 if( ISAUTOVACUUM(p->pBt) && szNew>pPage->maxLocal ){
80021 pPage->xParseCell(pPage, newCell, &info);
80023 Pgno ovfl = get4byte(&newCell[szNew-4]);
80024 ptrmapPut(p->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
80032 assert( szNew==pPage->xCellSize(pPage, newCell) );
80033 assert( szNew <= MX_CELL_SIZE(p->pBt) );
80034 idx = pCur->ix;
80035 pCur->info.nSize = 0;
80039 if( idx>=pPage->nCell ){
80042 rc = sqlite3PagerWrite(pPage->pDbPage);
80047 if( !pPage->leaf ){
80051 testcase( pCur->curFlags & BTCF_ValidOvfl );
80054 && (!ISAUTOVACUUM(p->pBt) || szNew<pPage->minLocal)
80064 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
80066 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
80069 if( oldCell+szNew > pPage->aDataEnd ){
80077 }else if( loc<0 && pPage->nCell>0 ){
80078 assert( pPage->leaf );
80079 idx = ++pCur->ix;
80080 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
80082 assert( pPage->leaf );
80085 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
80086 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
80100 ** multiple records into an intkey b-tree using a single cursor (as can
80103 ** the b-tree if possible. If the cursor is left pointing to the last
80108 if( pPage->nOverflow ){
80110 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
80117 pCur->pPage->nOverflow = 0;
80118 pCur->eState = CURSOR_INVALID;
80121 if( pCur->pKeyInfo ){
80122 assert( pCur->pKey==0 );
80123 pCur->pKey = sqlite3Malloc( pX->nKey );
80124 if( pCur->pKey==0 ){
80127 memcpy(pCur->pKey, pX->pKey, pX->nKey);
80130 pCur->eState = CURSOR_REQUIRESEEK;
80131 pCur->nKey = pX->nKey;
80134 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
80156 BtShared *pBt = pDest->pBt;
80157 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
80163 if( pSrc->info.nPayload<0x80 ){
80164 *(aOut++) = pSrc->info.nPayload;
80166 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
80168 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
80169 nIn = pSrc->info.nLocal;
80170 aIn = pSrc->info.pPayload;
80171 if( aIn+nIn>pSrc->pPage->aDataEnd ){
80172 return SQLITE_CORRUPT_PAGE(pSrc->pPage);
80174 nRem = pSrc->info.nPayload;
80175 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
80177 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
80181 Pager *pSrcPager = pSrc->pBt->pPager;
80188 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
80189 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
80190 if( nOut<pSrc->info.nPayload ){
80192 pBt->nPreformatSize += 4;
80196 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
80197 return SQLITE_CORRUPT_PAGE(pSrc->pPage);
80199 ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
80203 nRem -= nOut;
80209 nOut -= nCopy;
80210 nIn -= nCopy;
80222 nIn = pSrc->pBt->usableSize - 4;
80233 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc);
80238 pPgnoOut = pPageOut->aData;
80241 nOut = MIN(pBt->usableSize - 4, nRem);
80265 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
80270 Btree *p = pCur->pBtree;
80271 BtShared *pBt = p->pBt;
80281 assert( pBt->inTransaction==TRANS_WRITE );
80282 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
80283 assert( pCur->curFlags & BTCF_WriteFlag );
80284 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
80285 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
80287 if( pCur->eState!=CURSOR_VALID ){
80288 if( pCur->eState>=CURSOR_REQUIRESEEK ){
80290 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
80291 if( rc || pCur->eState!=CURSOR_VALID ) return rc;
80293 return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot);
80296 assert( pCur->eState==CURSOR_VALID );
80298 iCellDepth = pCur->iPage;
80299 iCellIdx = pCur->ix;
80300 pPage = pCur->pPage;
80301 if( pPage->nCell<=iCellIdx ){
80305 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
80308 if( pCell<&pPage->aCellIdx[pPage->nCell] ){
80314 ** will cause a b-tree rebalance, then this is done by saving the cursor
80330 if( !pPage->leaf
80331 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
80332 (int)(pBt->usableSize*2/3)
80333 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
80335 /* A b-tree rebalance will be required after deleting this entry.
80349 ** sub-tree headed by the child page of the cell being deleted. This makes
80351 if( !pPage->leaf ){
80359 if( pCur->curFlags & BTCF_Multiple ){
80360 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
80364 /* If this is a delete operation to remove a row from a table b-tree,
80366 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
80367 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
80373 rc = sqlite3PagerWrite(pPage->pDbPage);
80380 ** is currently pointing to the largest entry in the sub-tree headed
80381 ** by the child-page of the cell that was just deleted from an internal
80384 if( !pPage->leaf ){
80385 MemPage *pLeaf = pCur->pPage;
80390 if( pLeaf->nFree<0 ){
80394 if( iCellDepth<pCur->iPage-1 ){
80395 n = pCur->apPage[iCellDepth+1]->pgno;
80397 n = pCur->pPage->pgno;
80399 pCell = findCell(pLeaf, pLeaf->nCell-1);
80400 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_PAGE(pLeaf);
80401 nCell = pLeaf->xCellSize(pLeaf, pCell);
80403 pTmp = pBt->pTmpSpace;
80405 rc = sqlite3PagerWrite(pLeaf->pDbPage);
80407 rc = insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n);
80409 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
80428 assert( pCur->pPage->nOverflow==0 );
80429 assert( pCur->pPage->nFree>=0 );
80430 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
80432 ** then balance() will always be a no-op. No need to invoke it. */
80437 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
80438 releasePageNotNull(pCur->pPage);
80439 pCur->iPage--;
80440 while( pCur->iPage>iCellDepth ){
80441 releasePage(pCur->apPage[pCur->iPage--]);
80443 pCur->pPage = pCur->apPage[pCur->iPage];
80449 assert( (pCur->iPage==iCellDepth || CORRUPT_DB) );
80450 assert( pPage==pCur->pPage || CORRUPT_DB );
80451 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
80452 pCur->eState = CURSOR_SKIPNEXT;
80453 if( iCellIdx>=pPage->nCell ){
80454 pCur->skipNext = -1;
80455 pCur->ix = pPage->nCell-1;
80457 pCur->skipNext = 1;
80463 pCur->eState = CURSOR_REQUIRESEEK;
80483 BtShared *pBt = p->pBt;
80487 int ptfFlags; /* Page-type flags for the root page of new table */
80490 assert( pBt->inTransaction==TRANS_WRITE );
80491 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
80499 if( pBt->autoVacuum ){
80500 Pgno pgnoMove; /* Move a page here to make room for the root-page */
80505 ** out to be an overflow page, delete all overflow page-map caches
80511 ** root page of the new table should go. meta[3] is the largest root-page
80512 ** created so far, so the new root-page is (meta[3]+1).
80520 /* The new root-page may not be allocated on a pointer-map page, or the
80539 /* pgnoRoot is the page that will be used for the root-page of
80583 rc = sqlite3PagerWrite(pRoot->pDbPage);
80592 /* Update the pointer-map and meta-data with the new root-page number. */
80603 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
80615 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
80622 sqlite3PagerUnref(pRoot->pDbPage);
80623 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
80652 assert( sqlite3_mutex_held(pBt->mutex) );
80658 if( (pBt->openFlags & BTREE_SINGLE)==0
80659 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
80664 hdr = pPage->hdrOffset;
80665 for(i=0; i<pPage->nCell; i++){
80667 if( !pPage->leaf ){
80674 if( !pPage->leaf ){
80675 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
80677 if( pPage->intKey ) pnChange = 0;
80680 testcase( !pPage->intKey );
80681 *pnChange += pPage->nCell;
80685 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
80686 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
80708 BtShared *pBt = p->pBt;
80710 assert( p->inTrans==TRANS_WRITE );
80716 ** is the root of a table b-tree - if it is not, the following call is
80717 ** a no-op). */
80718 if( p->hasIncrblobCur ){
80733 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
80759 BtShared *pBt = p->pBt;
80762 assert( p->inTrans==TRANS_WRITE );
80782 if( pBt->autoVacuum ){
80787 /* If the table being dropped is the table with the largest root-page
80796 /* The table being dropped does not have the largest root-page
80798 ** gap left by the deleted root-page.
80821 /* Set the new 'max-root-page' value in the database header. This
80823 ** be a root-page number, less one again if that is the
80826 maxRootPgno--;
80829 maxRootPgno--;
80851 ** This function may only be called if the b-tree connection already
80854 ** Read the meta-information out of a database file. Meta[0]
80857 ** is read-only, the others are read/write.
80871 BtShared *pBt = p->pBt;
80874 assert( p->inTrans>TRANS_NONE );
80876 assert( pBt->pPage1 );
80880 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion;
80882 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
80885 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
80886 ** database, mark the database as read-only. */
80889 pBt->btsFlags |= BTS_READ_ONLY;
80897 ** Write meta-information back into the database. Meta[0] is
80898 ** read-only and may not be written.
80901 BtShared *pBt = p->pBt;
80906 assert( p->inTrans==TRANS_WRITE );
80907 assert( pBt->pPage1!=0 );
80908 pP1 = pBt->pPage1->aData;
80909 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
80914 assert( pBt->autoVacuum || iMeta==0 );
80916 pBt->incrVacuum = (u8)iMeta;
80925 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
80926 ** number of entries in the b-tree and write the result to *pnEntry.
80943 ** page in the B-Tree structure (not including overflow pages).
80945 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
80947 MemPage *pPage; /* Current page of the b-tree */
80949 /* If this is a leaf page or the tree is not an int-key tree, then
80953 pPage = pCur->pPage;
80954 if( pPage->leaf || !pPage->intKey ){
80955 nEntry += pPage->nCell;
80961 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
80963 ** to visit is the right-child of its parent.
80968 if( pPage->leaf ){
80970 if( pCur->iPage==0 ){
80971 /* All pages of the b-tree have been visited. Return successfully. */
80976 }while ( pCur->ix>=pCur->pPage->nCell );
80978 pCur->ix++;
80979 pPage = pCur->pPage;
80983 ** points at. This is the right-child if (iIdx==pPage->nCell).
80985 iIdx = pCur->ix;
80986 if( iIdx==pPage->nCell ){
80987 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
81002 return p->pBt->pPager;
81010 pCheck->rc = SQLITE_NOMEM;
81011 pCheck->mxErr = 0; /* Causes integrity_check processing to stop */
81012 if( pCheck->nErr==0 ) pCheck->nErr++;
81020 sqlite3 *db = pCheck->db;
81021 if( AtomicLoad(&db->u1.isInterrupted) ){
81022 pCheck->rc = SQLITE_INTERRUPT;
81023 pCheck->nErr++;
81024 pCheck->mxErr = 0;
81027 if( db->xProgress ){
81028 assert( db->nProgressOps>0 );
81029 pCheck->nStep++;
81030 if( (pCheck->nStep % db->nProgressOps)==0
81031 && db->xProgress(db->pProgressArg)
81033 pCheck->rc = SQLITE_INTERRUPT;
81034 pCheck->nErr++;
81035 pCheck->mxErr = 0;
81051 if( !pCheck->mxErr ) return;
81052 pCheck->mxErr--;
81053 pCheck->nErr++;
81055 if( pCheck->errMsg.nChar ){
81056 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
81058 if( pCheck->zPfx ){
81059 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx,
81060 pCheck->v0, pCheck->v1, pCheck->v2);
81062 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
81064 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
81073 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
81077 assert( pCheck->aPgRef!=0 );
81078 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 );
81079 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
81086 assert( pCheck->aPgRef!=0 );
81087 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 );
81088 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
81094 ** reference to the page, add an error message to pCheck->zErrMsg.
81101 if( iPage>pCheck->nCkPage || iPage==0 ){
81115 ** Check that the entry in the pointer-map for page iChild maps to
81129 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
81156 int nErrAtStart = pCheck->nErr;
81157 while( iPage!=0 && pCheck->mxErr ){
81161 N--;
81162 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
81170 if( pCheck->pBt->autoVacuum ){
81174 if( n>pCheck->pBt->usableSize/4-2 ){
81177 N--;
81182 if( pCheck->pBt->autoVacuum ){
81188 N -= n;
81193 /* If this database supports auto-vacuum and iPage is not the last
81194 ** page in this overflow list, check that the pointer-map entry for
81197 if( pCheck->pBt->autoVacuum && N>0 ){
81206 if( N && nErrAtStart==pCheck->nErr ){
81210 expected-N, expected);
81216 ** An implementation of a min-heap.
81226 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
81255 aHeap[0]--;
81292 int depth = -1, d2; /* Depth of a subtree */
81308 u32 *heap = 0; /* Min-heap used for checking cell coverage */
81309 u32 x, prev = 0; /* Next and previous entry on the min-heap */
81310 const char *saved_zPfx = pCheck->zPfx;
81311 int saved_v1 = pCheck->v1;
81312 int saved_v2 = pCheck->v2;
81318 if( pCheck->mxErr==0 ) goto end_of_check;
81319 pBt = pCheck->pBt;
81320 usableSize = pBt->usableSize;
81323 pCheck->zPfx = "Tree %u page %u: ";
81324 pCheck->v1 = iPage;
81328 if( rc==SQLITE_IOERR_NOMEM ) pCheck->rc = SQLITE_NOMEM;
81334 savedIsInit = pPage->isInit;
81335 pPage->isInit = 0;
81347 data = pPage->aData;
81348 hdr = pPage->hdrOffset;
81351 pCheck->zPfx = "Tree %u page %u cell %u: ";
81355 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
81358 assert( pPage->nCell==nCell );
81359 if( pPage->leaf || pPage->intKey==0 ){
81360 pCheck->nRow += nCell;
81363 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
81364 ** immediately follows the b-tree page header. */
81365 cellStart = hdr + 12 - 4*pPage->leaf;
81366 assert( pPage->aCellIdx==&data[cellStart] );
81367 pCellIdx = &data[cellStart + 2*(nCell-1)];
81369 if( !pPage->leaf ){
81370 /* Analyze the right-child page of internal pages */
81373 if( pBt->autoVacuum ){
81374 pCheck->zPfx = "Tree %u page %u right child: ";
81383 heap = pCheck->heap;
81387 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
81389 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
81393 pCheck->v2 = i;
81396 pCellIdx -= 2;
81397 if( pc<contentOffset || pc>usableSize-4 ){
81399 pc, contentOffset, usableSize-4);
81404 pPage->xParseCell(pPage, pCell, &info);
81412 if( pPage->intKey ){
81424 assert( pc + info.nSize - 4 <= usableSize );
81425 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
81426 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
81428 if( pBt->autoVacuum ){
81435 if( !pPage->leaf ){
81439 if( pBt->autoVacuum ){
81450 /* Populate the coverage-checking heap for leaf pages */
81451 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
81458 pCheck->zPfx = 0;
81459 if( doCoverageCheck && pCheck->mxErr>0 ){
81460 /* For leaf pages, the min-heap has already been initialized and the
81463 if( !pPage->leaf ){
81464 heap = pCheck->heap;
81466 for(i=nCell-1; i>=0; i--){
81469 size = pPage->xCellSize(pPage, &data[pc]);
81470 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
81474 /* Add the freeblocks to the min-heap
81476 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
81483 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
81486 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
81487 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
81488 ** big-endian integer which is the offset in the b-tree page of the next
81492 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
81495 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
81498 /* Analyze the min-heap looking for overlap between cells and/or
81501 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
81506 ** The loop below pulls entries from the min-heap in order and compares
81512 prev = contentOffset - 1; /* Implied first min-heap entry */
81519 nFrag += (x>>16) - (prev&0xffff) - 1;
81523 nFrag += usableSize - (prev&0xffff) - 1;
81524 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
81525 ** is stored in the fifth field of the b-tree page header.
81526 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
81537 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
81539 pCheck->zPfx = saved_zPfx;
81540 pCheck->v1 = saved_v1;
81541 pCheck->v2 = saved_v2;
81552 ** A read-only or read-write transaction must be opened before calling
81557 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
81561 ** root pages is incomplete. This is a "partial integrity-check". This
81581 BtShared *pBt = p->pBt;
81582 u64 savedDbFlags = pBt->db->flags;
81599 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
81600 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
81605 sCheck.pPager = pBt->pPager;
81619 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
81632 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
81633 get4byte(&pBt->pPage1->aData[36]));
81641 if( pBt->autoVacuum ){
81645 mxInHdr = get4byte(&pBt->pPage1->aData[52]);
81652 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
81659 testcase( pBt->db->flags & SQLITE_CellSizeCk );
81660 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
81666 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
81675 pBt->db->flags = savedDbFlags;
81686 /* If the database supports auto-vacuum, make sure no tables contain
81687 ** references to pointer-map pages.
81690 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
81694 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
81714 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
81722 ** an empty string if the database is in-memory or a TEMP database.
81728 assert( p->pBt->pPager!=0 );
81729 return sqlite3PagerFilename(p->pBt->pPager, 1);
81741 assert( p->pBt->pPager!=0 );
81742 return sqlite3PagerJournalname(p->pBt->pPager);
81750 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
81751 return p ? p->inTrans : 0;
81759 ** transaction on the shared-cache the argument Btree is connected to.
81766 BtShared *pBt = p->pBt;
81768 if( pBt->inTransaction!=TRANS_NONE ){
81771 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
81784 assert( sqlite3_mutex_held(p->db->mutex) );
81785 return p->nBackup!=0;
81790 ** a single shared-btree. The memory is used by client code for its own
81791 ** purposes (for example, to store a high-level schema associated with
81792 ** the shared-btree). The btree layer manages reference counting issues.
81794 ** The first time this is called on a shared-btree, nBytes bytes of memory
81803 ** Just before the shared-btree is closed, the function passed as the
81809 BtShared *pBt = p->pBt;
81811 if( !pBt->pSchema && nBytes ){
81812 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
81813 pBt->xFreeSchema = xFree;
81816 return pBt->pSchema;
81826 assert( sqlite3_mutex_held(p->db->mutex) );
81843 assert( p->inTrans!=TRANS_NONE );
81844 if( p->sharable ){
81874 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
81875 assert( pCsr->curFlags & BTCF_Incrblob );
81881 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
81882 if( pCsr->eState!=CURSOR_VALID ){
81888 ** version of the b-tree page modified by the accessPayload call below.
81894 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
81900 ** (c) the connection holds a write-lock on the table (if required),
81901 ** (d) there are no conflicting read-locks, and
81904 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
81907 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
81908 && pCsr->pBt->inTransaction==TRANS_WRITE );
81909 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
81910 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
81911 assert( pCsr->pPage->intKey );
81920 pCur->curFlags |= BTCF_Incrblob;
81921 pCur->pBtree->hasIncrblobCur = 1;
81931 BtShared *pBt = pBtree->pBt;
81939 pBt->btsFlags &= ~BTS_NO_WAL;
81940 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
81944 u8 *aData = pBt->pPage1->aData;
81948 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
81957 pBt->btsFlags &= ~BTS_NO_WAL;
81966 return (pCsr->hints & mask)!=0;
81970 ** Return true if the given Btree is read-only.
81973 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
81982 ** If no transaction is active and the database is not a temp-db, clear
81983 ** the in-memory pager cache.
81986 BtShared *pBt = p->pBt;
81987 if( pBt->inTransaction==TRANS_NONE ){
81988 sqlite3PagerClearCache(pBt->pPager);
81997 return p->sharable;
82006 testcase( p->sharable );
82007 return p->pBt->nRef;
82035 Btree *pDest; /* Destination b-tree file */
82037 int bDestLocked; /* True once a write-transaction is open on pDest */
82041 Btree *pSrc; /* Source b-tree file */
82059 ** structure may be accessed via two groups of thread-safe entry points:
82073 ** backup_pagecount() are not thread-safe functions. If they are called
82081 ** Non-sharable Btrees (in-memory databases for example), do not have
82117 return pDb->aDb[i].pBt;
82126 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
82131 ** Check that there is no open read-transaction on the b-tree passed as the
82133 ** is an open read-transaction, return SQLITE_ERROR and leave an error
82175 sqlite3_mutex_enter(pSrcDb->mutex);
82176 sqlite3_mutex_enter(pDestDb->mutex);
82185 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
82196 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
82197 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
82198 p->pDestDb = pDestDb;
82199 p->pSrcDb = pSrcDb;
82200 p->iNext = 1;
82201 p->isAttached = 0;
82203 if( 0==p->pSrc || 0==p->pDest
82204 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
82216 p->pSrc->nBackup++;
82219 sqlite3_mutex_leave(pDestDb->mutex);
82220 sqlite3_mutex_leave(pSrcDb->mutex);
82244 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
82245 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
82246 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
82252 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
82253 assert( p->bDestLocked );
82254 assert( !isFatalError(p->rc) );
82255 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
82263 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
82266 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
82284 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
82296 ** this function is a no-op.
82316 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
82317 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
82318 p->pNext = *pp;
82320 p->isAttached = 1;
82324 ** Copy nPage pages from the source b-tree to the destination.
82335 sqlite3_mutex_enter(p->pSrcDb->mutex);
82336 sqlite3BtreeEnter(p->pSrc);
82337 if( p->pDestDb ){
82338 sqlite3_mutex_enter(p->pDestDb->mutex);
82341 rc = p->rc;
82343 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
82344 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
82346 int nSrcPage = -1; /* Size of source db in pages */
82349 /* If the source pager is currently in a write-transaction, return
82352 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
82358 /* If there is no open read-transaction on the source database, open
82362 if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p->pSrc) ){
82363 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
82373 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
82378 if( SQLITE_OK==rc && p->bDestLocked==0
82379 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
82380 (int*)&p->iDestSchema))
82382 p->bDestLocked = 1;
82387 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
82388 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
82389 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
82397 /* Now that there is a read-lock on the source database, query the
82400 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
82402 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
82403 const Pgno iSrcPg = p->iNext; /* Source page number */
82404 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
82412 p->iNext++;
82415 p->nPagecount = nSrcPage;
82416 p->nRemaining = nSrcPage+1-p->iNext;
82417 if( p->iNext>(Pgno)nSrcPage ){
82419 }else if( !p->isAttached ){
82425 ** is to make sure that the schema-version really does change in
82431 rc = sqlite3BtreeNewDb(p->pDest);
82435 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
82438 if( p->pDestDb ){
82439 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
82442 rc = sqlite3BtreeSetVersion(p->pDest, 2);
82459 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
82460 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
82463 nDestTruncate = (nSrcPage+ratio-1)/ratio;
82464 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
82465 nDestTruncate--;
82473 /* If the source page-size is smaller than the destination page-size,
82479 ** pending-byte page in the source database may need to be
82492 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
82504 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
82548 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
82558 ** "committing" a read-only transaction cannot fail.
82562 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
82563 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
82570 p->rc = rc;
82572 if( p->pDestDb ){
82573 sqlite3_mutex_leave(p->pDestDb->mutex);
82575 sqlite3BtreeLeave(p->pSrc);
82576 sqlite3_mutex_leave(p->pSrcDb->mutex);
82590 pSrcDb = p->pSrcDb;
82591 sqlite3_mutex_enter(pSrcDb->mutex);
82592 sqlite3BtreeEnter(p->pSrc);
82593 if( p->pDestDb ){
82594 sqlite3_mutex_enter(p->pDestDb->mutex);
82598 if( p->pDestDb ){
82599 p->pSrc->nBackup--;
82601 if( p->isAttached ){
82602 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
82605 pp = &(*pp)->pNext;
82608 *pp = p->pNext;
82612 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
82615 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
82616 if( p->pDestDb ){
82617 sqlite3Error(p->pDestDb, rc);
82620 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
82622 sqlite3BtreeLeave(p->pSrc);
82623 if( p->pDestDb ){
82624 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
82644 return p->nRemaining;
82658 return p->nPagecount;
82680 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
82681 if( !isFatalError(p->rc) && iPage<p->iNext ){
82687 assert( p->pDestDb );
82688 sqlite3_mutex_enter(p->pDestDb->mutex);
82690 sqlite3_mutex_leave(p->pDestDb->mutex);
82693 p->rc = rc;
82696 }while( (p = p->pNext)!=0 );
82715 for(p=pBackup; p; p=p->pNext){
82716 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
82717 p->iNext = 1;
82739 if( pFd->pMethods ){
82752 b.pSrcDb = pFrom->db;
82761 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
82768 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
82806 #define ISPOWEROF2(X) (((X)&((X)-1))==0)
82819 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
82825 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
82828 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );
82830 if( p->flags & MEM_Null ){
82832 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
82839 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
82842 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
82843 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
82844 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
82847 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
82855 assert( (p->flags & MEM_Cleared)==0 );
82859 assert( p->szMalloc==0
82860 || (p->flags==MEM_Undefined
82861 && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc))
82862 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc));
82872 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
82874 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
82875 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
82876 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
82877 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
82890 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );
82892 if( p->flags & MEM_Int ){
82894 /* Work-around for GCC bug
82897 assert( (p->flags&MEM_Int)*2==sizeof(x) );
82898 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int)*2);
82899 p->n = sqlite3Int64ToText(x, zBuf);
82901 p->n = sqlite3Int64ToText(p->u.i, zBuf);
82906 (p->flags & MEM_IntReal)!=0 ? (double)p->u.i : p->u.r);
82909 p->n = acc.nChar;
82926 ** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
82941 if( (p->flags & MEM_Str)==0 ) return 1;
82942 if( p->db && p->db->mallocFailed ) return 1;
82943 if( p->flags & MEM_Term ){
82944 /* Insure that the string is properly zero-terminated. Pay particular
82945 ** attention to the case where p->n is odd */
82946 if( p->szMalloc>0 && p->z==p->zMalloc ){
82947 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 );
82948 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 );
82950 assert( p->z[p->n]==0 );
82951 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 );
82952 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 );
82954 if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;
82957 z = p->z;
82960 if( p->enc!=SQLITE_UTF8 ){
82962 if( p->enc==SQLITE_UTF16BE ) z++;
82979 ** routine is a no-op.
82993 if( !(pMem->flags&MEM_Str) ){
82994 pMem->enc = desiredEnc;
82997 if( pMem->enc==desiredEnc ){
83000 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83010 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
83011 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
83017 ** Make sure pMem->z points to a writable allocation of at least n bytes.
83020 ** pMem->z into the new allocation. pMem must be either a string or
83022 ** in pMem->z is discarded.
83027 testcase( pMem->db==0 );
83031 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
83032 testcase( bPreserve && pMem->z==0 );
83034 assert( pMem->szMalloc==0
83035 || (pMem->flags==MEM_Undefined
83036 && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc))
83037 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc));
83038 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
83039 if( pMem->db ){
83040 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
83042 pMem->zMalloc = sqlite3Realloc(pMem->z, n);
83043 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
83044 pMem->z = pMem->zMalloc;
83048 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
83049 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
83051 if( pMem->zMalloc==0 ){
83053 pMem->z = 0;
83054 pMem->szMalloc = 0;
83057 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
83060 if( bPreserve && pMem->z ){
83061 assert( pMem->z!=pMem->zMalloc );
83062 memcpy(pMem->zMalloc, pMem->z, pMem->n);
83064 if( (pMem->flags&MEM_Dyn)!=0 ){
83065 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
83066 pMem->xDel((void *)(pMem->z));
83069 pMem->z = pMem->zMalloc;
83070 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
83075 ** Change the pMem->zMalloc allocation to be at least szNew bytes.
83076 ** If pMem->zMalloc already meets or exceeds the requested size, this
83077 ** routine is a no-op.
83080 ** The pMem->xDel destructor is called, if it exists. Though MEM_Str
83089 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
83090 if( pMem->szMalloc<szNew ){
83093 assert( (pMem->flags & MEM_Dyn)==0 );
83094 pMem->z = pMem->zMalloc;
83095 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);
83100 ** If pMem is already a string, detect if it is a zero-terminated
83104 ** this routine is a no-op.
83107 if( (pMem->flags & (MEM_Str|MEM_Term|MEM_Ephem|MEM_Static))!=MEM_Str ){
83111 if( pMem->enc!=SQLITE_UTF8 ) return;
83112 if( NEVER(pMem->z==0) ) return;
83113 if( pMem->flags & MEM_Dyn ){
83114 if( pMem->xDel==sqlite3_free
83115 && sqlite3_msize(pMem->z) >= (u64)(pMem->n+1)
83117 pMem->z[pMem->n] = 0;
83118 pMem->flags |= MEM_Term;
83121 if( pMem->xDel==sqlite3RCStrUnref ){
83122 /* Blindly assume that all RCStr objects are zero-terminated */
83123 pMem->flags |= MEM_Term;
83126 }else if( pMem->szMalloc >= pMem->n+1 ){
83127 pMem->z[pMem->n] = 0;
83128 pMem->flags |= MEM_Term;
83138 ** to be a double-zero byte at an even byte boundary in order to
83143 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
83146 pMem->z[pMem->n] = 0;
83147 pMem->z[pMem->n+1] = 0;
83148 pMem->z[pMem->n+2] = 0;
83149 pMem->flags |= MEM_Term;
83161 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83163 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
83165 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
83170 pMem->flags &= ~MEM_Ephem;
83172 pMem->pScopyFrom = 0;
83179 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
83186 assert( pMem->flags & MEM_Zero );
83187 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
83190 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83193 nByte = pMem->n + pMem->u.nZero;
83195 if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
83201 assert( pMem->z!=0 );
83202 assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte );
83204 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
83205 pMem->n += pMem->u.nZero;
83206 pMem->flags &= ~(MEM_Zero|MEM_Term);
83216 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83217 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
83218 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
83219 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
83244 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83245 assert( !(pMem->flags&MEM_Zero) );
83246 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );
83247 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );
83253 pMem->enc = 0;
83257 vdbeMemRenderNum(nByte, pMem->z, pMem);
83258 assert( pMem->z!=0 );
83259 assert( pMem->n==(int)sqlite3Strlen30NN(pMem->z) );
83260 pMem->enc = SQLITE_UTF8;
83261 pMem->flags |= MEM_Str|MEM_Term;
83262 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
83280 assert( pMem->db!=0 );
83281 assert( pFunc->xFinalize!=0 );
83282 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
83283 assert( sqlite3_mutex_held(pMem->db->mutex) );
83287 t.db = pMem->db;
83292 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
83293 assert( (pMem->flags & MEM_Dyn)==0 );
83294 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
83311 assert( pFunc->xValue!=0 );
83312 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
83313 assert( pAccum->db!=0 );
83314 assert( sqlite3_mutex_held(pAccum->db->mutex) );
83320 ctx.enc = ENC(pAccum->db);
83321 pFunc->xValue(&ctx);
83336 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
83338 if( p->flags&MEM_Agg ){
83339 sqlite3VdbeMemFinalize(p, p->u.pDef);
83340 assert( (p->flags & MEM_Agg)==0 );
83341 testcase( p->flags & MEM_Dyn );
83343 if( p->flags&MEM_Dyn ){
83344 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
83345 p->xDel((void *)p->z);
83347 p->flags = MEM_Null;
83352 ** by p->xDel and memory in p->zMalloc.
83362 if( p->szMalloc ){
83363 sqlite3DbFreeNN(p->db, p->zMalloc);
83364 p->szMalloc = 0;
83366 p->z = 0;
83381 if( VdbeMemDynamic(p) || p->szMalloc ){
83391 if( p->szMalloc ) vdbeMemClear(p);
83398 ** a floating-point then the value returned is the integer part.
83401 ** an SQL-NULL value, return 0.
83407 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
83413 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83415 flags = pMem->flags;
83418 return pMem->u.i;
83420 return sqlite3RealToI64(pMem->u.r);
83421 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
83437 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
83442 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83444 if( pMem->flags & MEM_Real ){
83445 return pMem->u.r;
83446 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
83447 testcase( pMem->flags & MEM_IntReal );
83448 return (double)pMem->u.i;
83449 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
83462 testcase( pMem->flags & MEM_IntReal );
83463 if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;
83464 if( pMem->flags & MEM_Null ) return ifNull;
83474 assert( pMem->flags & (MEM_Real|MEM_IntReal) );
83476 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83479 if( pMem->flags & MEM_IntReal ){
83482 i64 ix = sqlite3RealToI64(pMem->u.r);
83486 ** (1) the round-trip conversion real->int->real is a no-op, and
83494 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
83495 pMem->u.i = ix;
83506 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83510 pMem->u.i = sqlite3VdbeIntValue(pMem);
83521 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83524 pMem->u.r = sqlite3VdbeRealValue(pMem);
83534 ** For some versions of GCC on 32-bit machines, if you do the more obvious
83536 ** though the r1 and (double)i values are bit-for-bit the same.
83542 && i >= -2251799813685248LL && i < 2251799813685248LL);
83550 if( r<-9223372036854774784.0 ) return SMALLEST_INT64;
83565 testcase( pMem->flags & MEM_Int );
83566 testcase( pMem->flags & MEM_Real );
83567 testcase( pMem->flags & MEM_IntReal );
83568 testcase( pMem->flags & MEM_Null );
83569 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){
83572 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
83573 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83574 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
83575 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
83576 || sqlite3RealSameAsInt(pMem->u.r, (ix = sqlite3RealToI64(pMem->u.r)))
83578 pMem->u.i = ix;
83584 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );
83585 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
83597 if( pMem->flags & MEM_Null ) return SQLITE_OK;
83600 if( (pMem->flags & MEM_Blob)==0 ){
83602 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
83603 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
83605 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
83625 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
83627 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
83628 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
83629 if( encoding!=SQLITE_UTF8 ) pMem->n &= ~1;
83645 pMem->flags = flags;
83646 pMem->db = db;
83647 pMem->szMalloc = 0;
83667 pMem->flags = MEM_Null;
83681 pMem->flags = MEM_Blob|MEM_Zero;
83682 pMem->n = 0;
83684 pMem->u.nZero = n;
83685 pMem->enc = SQLITE_UTF8;
83686 pMem->z = 0;
83694 assert( pMem->z!=0 );
83695 assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte );
83696 memset(pMem->z, 0, nByte);
83697 pMem->n = n>0?n:0;
83698 pMem->flags = MEM_Blob;
83699 pMem->enc = SQLITE_UTF8;
83707 ** a 64-bit integer.
83711 pMem->u.i = val;
83712 pMem->flags = MEM_Int;
83723 pMem->u.i = val;
83724 pMem->flags = MEM_Int;
83735 /* A no-op destructor */
83748 assert( pMem->flags==MEM_Null );
83750 pMem->u.zPType = zPType ? zPType : "";
83751 pMem->z = pPtr;
83752 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
83753 pMem->eSubtype = 'p';
83754 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
83765 pMem->u.r = val;
83766 pMem->flags = MEM_Real;
83777 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
83778 && pMem->xDel==sqlite3RowSetDelete;
83790 sqlite3 *db = pMem->db;
83797 pMem->z = (char*)p;
83798 pMem->flags = MEM_Blob|MEM_Dyn;
83799 pMem->xDel = sqlite3RowSetDelete;
83805 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
83808 assert( p->db!=0 );
83809 if( p->flags & (MEM_Str|MEM_Blob) ){
83810 int n = p->n;
83811 if( p->flags & MEM_Zero ){
83812 n += p->u.nZero;
83814 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
83825 ** This is used for testing and debugging only - to help ensure that shallow
83831 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
83832 if( pX->pScopyFrom==pMem ){
83834 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
83836 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
83844 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
83845 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
83848 ** undefined so that we can quickly detect the shallow-copy error */
83849 pX->flags = MEM_Undefined;
83850 pX->pScopyFrom = 0;
83853 pMem->pScopyFrom = 0;
83859 ** pTo are freed. The pFrom->z field is not duplicated. If
83860 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
83870 assert( pTo->db==pFrom->db );
83873 if( (pFrom->flags&MEM_Static)==0 ){
83874 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
83876 pTo->flags |= srcType;
83890 pTo->flags &= ~MEM_Dyn;
83891 if( pTo->flags&(MEM_Str|MEM_Blob) ){
83892 if( 0==(pFrom->flags&MEM_Static) ){
83893 pTo->flags |= MEM_Ephem;
83908 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
83909 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
83910 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
83914 pFrom->flags = MEM_Null;
83915 pFrom->szMalloc = 0;
83938 ** non-negative for blobs.
83947 i64 nByte = n; /* New value for pMem->n */
83949 u16 flags; /* New value for pMem->flags */
83952 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83962 if( pMem->db ){
83963 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
83984 sqlite3DbFree(pMem->db, (void*)z);
83990 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
84008 memcpy(pMem->z, z, nAlloc);
84011 pMem->z = (char *)z;
84013 pMem->zMalloc = pMem->z;
84014 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
84016 pMem->xDel = xDel;
84021 pMem->n = (int)(nByte & 0x7fffffff);
84022 pMem->flags = flags;
84023 pMem->enc = enc;
84042 ** pMem->zMalloc to hold the content from the btree, if possible. New
84043 ** pMem->zMalloc space will be allocated if necessary. The calling routine
84057 pMem->flags = MEM_Null;
84062 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
84064 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
84065 pMem->flags = MEM_Blob;
84066 pMem->n = (int)amt;
84087 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available);
84088 assert( pMem->z!=0 );
84091 pMem->flags = MEM_Blob|MEM_Ephem;
84092 pMem->n = (int)amt;
84103 ** to a zero-terminated version of that string.
84107 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
84110 assert( (pVal->flags & (MEM_Null))==0 );
84111 if( pVal->flags & (MEM_Blob|MEM_Str) ){
84113 pVal->flags |= MEM_Str;
84114 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
84117 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
84118 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
84123 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
84126 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
84128 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
84129 || pVal->db->mallocFailed );
84130 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
84132 return pVal->z;
84144 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
84150 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
84153 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
84155 return pVal->z;
84157 if( pVal->flags&MEM_Null ){
84167 ** that third-party extensions can get access to it?
84171 && ALWAYS((pVal->flags & (MEM_Str|MEM_Blob))!=0)
84172 && (pVal->flags & MEM_Dyn)!=0
84173 && pVal->xDel==xFree
84187 p->flags = MEM_Null;
84188 p->db = db;
84209 ** Otherwise, if the second argument is non-zero, then this function is
84218 UnpackedRecord *pRec = p->ppRec[0];
84221 Index *pIdx = p->pIdx; /* Index being probed */
84224 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
84229 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
84230 if( pRec->pKeyInfo ){
84231 assert( pRec->pKeyInfo->nAllField==nCol );
84232 assert( pRec->pKeyInfo->enc==ENC(db) );
84233 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
84235 pRec->aMem[i].flags = MEM_Null;
84236 pRec->aMem[i].db = db;
84244 p->ppRec[0] = pRec;
84247 pRec->nField = p->iVal+1;
84248 sqlite3VdbeMemSetNull(&pRec->aMem[p->iVal]);
84249 return &pRec->aMem[p->iVal];
84296 assert( (p->flags & EP_TokenOnly)==0 );
84298 pList = p->x.pList;
84299 if( pList ) nVal = pList->nExpr;
84301 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
84306 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
84307 || (pFunc->funcFlags & (SQLITE_FUNC_NEEDCOLL|SQLITE_FUNC_RUNONLY))!=0
84319 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
84334 pFunc->xSFunc(&ctx, nVal, apVal);
84337 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
84344 pCtx->pParse->nErr++;
84351 pCtx->pParse->rc = rc;
84393 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
84394 if( op==TK_REGISTER ) op = pExpr->op2;
84400 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
84405 aff = sqlite3AffinityType(pExpr->u.zToken,0);
84406 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
84412 /* zero-blobs only come from functions, not literal values. And
84423 ** case when the value is -9223372036854775808. Except - do not do this
84426 Expr *pLeft = pExpr->pLeft;
84427 if( (pLeft->op==TK_INTEGER || pLeft->op==TK_FLOAT) ){
84429 || pLeft->u.zToken[0]!='0' || (pLeft->u.zToken[1] & ~0x20)!='X'
84432 op = pExpr->op;
84433 negInt = -1;
84434 zNeg = "-";
84443 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
84446 if( op==TK_INTEGER && 0==sqlite3DecOrHexToI64(pExpr->u.zToken, &iVal) ){
84449 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
84451 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
84456 assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) );
84457 sqlite3AtoF(pVal->z, &pVal->u.r, pVal->n, SQLITE_UTF8);
84458 pVal->flags = MEM_Real;
84460 /* This case is required by -9223372036854775808 and other strings
84468 assert( (pVal->flags & MEM_IntReal)==0 );
84469 if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
84470 testcase( pVal->flags & MEM_Int );
84471 testcase( pVal->flags & MEM_Real );
84472 pVal->flags &= ~MEM_Str;
84478 /* This branch happens for multiple negative signs. Ex: -(-5) */
84479 if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
84483 if( pVal->flags & MEM_Real ){
84484 pVal->u.r = -pVal->u.r;
84485 }else if( pVal->u.i==SMALLEST_INT64 ){
84487 pVal->u.r = -(double)SMALLEST_INT64;
84489 pVal->u.r = LARGEST_INT64;
84493 pVal->u.i = -pVal->u.i;
84506 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
84507 assert( pExpr->u.zToken[1]=='\'' );
84510 zVal = &pExpr->u.zToken[2];
84511 nVal = sqlite3Strlen30(zVal)-1;
84526 pVal->flags = MEM_Int;
84527 pVal->u.i = pExpr->u.zToken[4]==0;
84537 if( pCtx==0 || NEVER(pCtx->pParse->nErr==0) )
84598 sqlite3 *db = pParse->db;
84603 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
84609 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
84611 int iBindVar = pExpr->iColumn;
84612 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
84613 if( (v = pParse->pReprepare)!=0 ){
84616 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
84618 pVal->db = pParse->db;
84625 assert( pVal==0 || pVal->db==db );
84676 if( pExpr==0 || pExpr->op!=TK_SELECT ){
84687 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
84719 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
84761 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
84762 pMem->enc = ENC(db);
84774 int nCol = pRec->pKeyInfo->nAllField;
84775 Mem *aMem = pRec->aMem;
84780 sqlite3KeyInfoUnref(pRec->pKeyInfo);
84805 sqlite3DbFreeNN(((Mem*)v)->db, v);
84814 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
84818 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
84819 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
84820 return p->n;
84822 if( (p->flags & MEM_Str)!=0 && enc!=SQLITE_UTF8 && pVal->enc!=SQLITE_UTF8 ){
84823 return p->n;
84825 if( (p->flags & MEM_Blob)!=0 ){
84826 if( p->flags & MEM_Zero ){
84827 return p->n + p->u.nZero;
84829 return p->n;
84832 if( p->flags & MEM_Null ) return 0;
84863 sqlite3 *db = pParse->db;
84867 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
84868 p->db = db;
84869 if( db->pVdbe ){
84870 db->pVdbe->ppVPrev = &p->pVNext;
84872 p->pVNext = db->pVdbe;
84873 p->ppVPrev = &db->pVdbe;
84874 db->pVdbe = p;
84875 assert( p->eVdbeState==VDBE_INIT_STATE );
84876 p->pParse = pParse;
84877 pParse->pVdbe = p;
84878 assert( pParse->aLabel==0 );
84879 assert( pParse->nLabel==0 );
84880 assert( p->nOpAlloc==0 );
84881 assert( pParse->szOpAlloc==0 );
84890 return p->pParse;
84898 sqlite3DbFree(p->db, p->zErrMsg);
84900 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
84909 p->prepFlags = prepFlags;
84911 p->expmask = 0;
84913 assert( p->zSql==0 );
84914 p->zSql = sqlite3DbStrNDup(p->db, z, n);
84919 ** Add a new element to the Vdbe->pDblStr list.
84925 sizeof(*pStr)+n+1-sizeof(pStr->z));
84927 pStr->pNextStr = p->pDblStr;
84928 p->pDblStr = pStr;
84929 memcpy(pStr->z, z, n+1);
84937 ** zId of length nId is a double-quoted identifier. Check to see if
84942 const char *zId /* The double-quoted identifier, already dequoted */
84946 if( pVdbe->pDblStr==0 ) return 0;
84947 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
84948 if( strcmp(zId, pStr->z)==0 ) return 1;
84955 ** Swap byte-code between two VDBE structures.
84967 assert( pA->db==pB->db );
84971 pTmp = pA->pVNext;
84972 pA->pVNext = pB->pVNext;
84973 pB->pVNext = pTmp;
84974 ppTmp = pA->ppVPrev;
84975 pA->ppVPrev = pB->ppVPrev;
84976 pB->ppVPrev = ppTmp;
84977 zTmp = pA->zSql;
84978 pA->zSql = pB->zSql;
84979 pB->zSql = zTmp;
84981 zTmp = pA->zNormSql;
84982 pA->zNormSql = pB->zNormSql;
84983 pB->zNormSql = zTmp;
84985 pB->expmask = pA->expmask;
84986 pB->prepFlags = pA->prepFlags;
84987 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
84988 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
84996 ** If an out-of-memory error occurs while resizing the array, return
85003 Parse *p = v->pParse;
85005 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
85013 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
85014 : (sqlite3_int64)v->nOpAlloc+nOp);
85016 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
85022 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
85023 sqlite3OomFault(p->db);
85028 assert( nNew>=(v->nOpAlloc+nOp) );
85029 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
85031 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
85032 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
85033 v->aOp = pNew;
85065 assert( p->nOpAlloc<=p->nOp );
85067 assert( p->nOpAlloc>p->nOp );
85079 if( p->db->mallocFailed==0 ){
85080 VdbeOp *pOp = &p->aOp[addr];
85081 pOp->p4type = P4_INT32;
85082 pOp->p4.i = p4;
85113 i = p->nOp;
85114 assert( p->eVdbeState==VDBE_INIT_STATE );
85116 if( p->nOpAlloc<=i ){
85119 assert( p->aOp!=0 );
85120 p->nOp++;
85121 pOp = &p->aOp[i];
85123 pOp->opcode = (u8)op;
85124 pOp->p5 = 0;
85125 pOp->p1 = p1;
85126 pOp->p2 = p2;
85127 pOp->p3 = p3;
85128 pOp->p4.p = 0;
85129 pOp->p4type = P4_NOTUSED;
85134 pOp->zComment = 0;
85137 pOp->nExec = 0;
85138 pOp->nCycle = 0;
85141 if( p->db->flags & SQLITE_VdbeAddopTrace ){
85142 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
85143 test_addop_breakpoint(i, &p->aOp[i]);
85147 pOp->iSrcLine = 0;
85165 i = p->nOp;
85166 if( p->nOpAlloc<=i ){
85169 p->nOp++;
85170 pOp = &p->aOp[i];
85172 pOp->opcode = (u8)op;
85173 pOp->p5 = 0;
85174 pOp->p1 = p1;
85175 pOp->p2 = p2;
85176 pOp->p3 = p3;
85177 pOp->p4.i = p4;
85178 pOp->p4type = P4_INT32;
85183 pOp->zComment = 0;
85186 pOp->nExec = 0;
85187 pOp->nCycle = 0;
85190 if( p->db->flags & SQLITE_VdbeAddopTrace ){
85191 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
85192 test_addop_breakpoint(i, &p->aOp[i]);
85196 pOp->iSrcLine = 0;
85284 Vdbe *v = pParse->pVdbe;
85289 nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
85290 pCtx = sqlite3DbMallocRawNN(pParse->db, nByte);
85292 assert( pParse->db->mallocFailed );
85293 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
85296 pCtx->pOut = 0;
85297 pCtx->pFunc = (FuncDef*)pFunc;
85298 pCtx->pVdbe = 0;
85299 pCtx->isError = 0;
85300 pCtx->argc = nArg;
85301 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
85334 if( pParse->addrExplain==0 ) return 0;
85335 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
85336 return pOp->p2;
85361 if( pParse->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
85369 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
85371 v = pParse->pVdbe;
85372 iThis = v->nOp;
85373 addr = sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
85375 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
85377 pParse->addrExplain = iThis;
85379 sqlite3VdbeScanStatus(v, iThis, -1, -1, 0, 0);
85389 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
85405 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
85406 sqlite3MayAbort(p->pParse);
85409 /* Insert the end of a co-routine
85415 ** co-routine has its own independent set of registers, because co-routines
85417 ** that could cause problems if two or more co-routines are using the same
85420 v->pParse->nTempReg = 0;
85421 v->pParse->nRangeReg = 0;
85433 ** always negative and P2 values are suppose to be non-negative.
85440 ** Parse.aLabel[x] Stores the address that the x-th label resolves
85442 ** labels stores -1, but that is not required.
85450 return --pParse->nLabel;
85459 int nNewSize = 10 - p->nLabel;
85460 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
85461 nNewSize*sizeof(p->aLabel[0]));
85462 if( p->aLabel==0 ){
85463 p->nLabelAlloc = 0;
85467 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
85469 if( nNewSize>=100 && (nNewSize/100)>(p->nLabelAlloc/100) ){
85472 p->nLabelAlloc = nNewSize;
85473 p->aLabel[j] = v->nOp;
85477 Parse *p = v->pParse;
85479 assert( v->eVdbeState==VDBE_INIT_STATE );
85480 assert( j<-p->nLabel );
85483 if( p->db->flags & SQLITE_VdbeAddopTrace ){
85484 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
85487 if( p->nLabelAlloc + p->nLabel < 0 ){
85490 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
85491 p->aLabel[j] = v->nOp;
85507 for(i=1; ALWAYS(i<p->nOp); i++){
85508 if( ALWAYS(p->aOp[i].opcode==OP_Expire) ){
85509 p->aOp[1].opcode = OP_Noop;
85519 ** in a Vdbe main program and each of the sub-programs (triggers) it may
85530 ** sqlite3DbFree(v->db, sIter.apSub);
85539 int iSub; /* 0 = main program, 1 = first sub-program etc. */
85542 Vdbe *v = p->v;
85547 if( p->iSub<=p->nSub ){
85549 if( p->iSub==0 ){
85550 aOp = v->aOp;
85551 nOp = v->nOp;
85553 aOp = p->apSub[p->iSub-1]->aOp;
85554 nOp = p->apSub[p->iSub-1]->nOp;
85556 assert( p->iAddr<nOp );
85558 pRet = &aOp[p->iAddr];
85559 p->iAddr++;
85560 if( p->iAddr==nOp ){
85561 p->iSub++;
85562 p->iAddr = 0;
85565 if( pRet->p4type==P4_SUBPROGRAM ){
85566 int nByte = (p->nSub+1)*sizeof(SubProgram*);
85568 for(j=0; j<p->nSub; j++){
85569 if( p->apSub[j]==pRet->p4.pProgram ) break;
85571 if( j==p->nSub ){
85572 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
85573 if( !p->apSub ){
85576 p->apSub[p->nSub++] = pRet->p4.pProgram;
85589 ** sub-programs contains any of the following:
85606 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
85622 int opcode = pOp->opcode;
85629 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
85634 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
85638 ** where a "DELETE FROM tbl" has a statement-journal but does not
85639 ** require one. This is not so bad - it is an inefficiency, not a bug. */
85640 if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1;
85645 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
85650 sqlite3DbFree(v->db, sIter.apSub);
85657 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
85661 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
85670 || (pC->eCurType!=CURTYPE_SORTER
85671 && pC->eCurType!=CURTYPE_PSEUDO
85672 && !pC->isEphemeral)
85674 p->nWrite++;
85685 assert( p->nWrite==0 || p->usesStmtJournal );
85713 Parse *pParse = p->pParse;
85714 int *aLabel = pParse->aLabel;
85716 assert( pParse->db->mallocFailed==0 ); /* tag-20230419-1 */
85717 p->readOnly = 1;
85718 p->bIsReader = 0;
85719 pOp = &p->aOp[p->nOp-1];
85720 assert( p->aOp[0].opcode==OP_Init );
85728 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
85731 switch( pOp->opcode ){
85733 if( pOp->p2!=0 ) p->readOnly = 0;
85738 p->bIsReader = 1;
85746 p->readOnly = 0;
85747 p->bIsReader = 1;
85751 assert( pOp->p2>=0 );
85756 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
85761 assert( (pOp - p->aOp) >= 3 );
85762 assert( pOp[-1].opcode==OP_Integer );
85763 n = pOp[-1].p1;
85770 if( pOp->p2<0 ){
85772 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
85773 ** have non-negative values for P2. */
85774 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
85775 assert( ADDR(pOp->p2)<-pParse->nLabel );
85776 assert( aLabel!=0 ); /* True because of tag-20230419-1 */
85777 pOp->p2 = aLabel[ADDR(pOp->p2)];
85782 assert( pOp->p2>0
85783 || (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP0)!=0 );
85786 assert( pOp->p2<p->nOp
85787 || (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)==0 );
85792 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
85793 ** have non-negative values for P2. */
85794 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
85796 assert( pOp>p->aOp );
85797 pOp--;
85801 sqlite3DbNNFreeNN(p->db, pParse->aLabel);
85802 pParse->aLabel = 0;
85804 pParse->nLabel = 0;
85806 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
85831 Vdbe *v, /* The byte-code program under construction */
85841 pParse = v->pParse;
85843 if( pParse->nErr ) return;
85845 assert( iLast<v->nOp );
85846 pOp = &v->aOp[iFirst];
85848 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 ){
85849 int iDest = pOp->p2; /* Jump destination */
85851 if( pOp->opcode==OP_Gosub ) continue;
85852 if( pOp->p3==20230325 && pOp->opcode==OP_NotNull ){
85853 /* This is a deliberately taken illegal branch. tag-20230325-2 */
85859 if( j>=-pParse->nLabel || pParse->aLabel[j]<0 ){
85862 iDest = pParse->aLabel[j];
85866 for(; j<v->nOp; j++){
85867 VdbeOp *pX = &v->aOp[j];
85868 if( pX->opcode==OP_Return ){
85869 if( pX->p1==iRetReg ) break;
85872 if( pX->opcode==OP_Noop ) continue;
85873 if( pX->opcode==OP_Explain ) continue;
85901 assert( p->eVdbeState==VDBE_INIT_STATE );
85902 return p->nOp;
85911 ** sqlite3VdbeAddOpList() will always be non-NULL.
85915 assert( p->nOp + N <= p->nOpAlloc );
85929 for(i=0; i<p->nOp; i++){
85930 assert( p->aOp[i].opcode!=OP_ResultRow );
85958 VdbeOp *aOp = p->aOp;
85959 assert( aOp && !p->db->mallocFailed );
85962 assert( DbMaskAllZero(p->btreeMask) );
85965 *pnOp = p->nOp;
85966 p->aOp = 0;
85974 ** Non-zero P2 arguments to jump instructions are automatically adjusted
85981 int iLineno /* Source-file line number of first opcode */
85986 assert( p->eVdbeState==VDBE_INIT_STATE );
85987 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
85990 pFirst = pOut = &p->aOp[p->nOp];
85992 pOut->opcode = aOp->opcode;
85993 pOut->p1 = aOp->p1;
85994 pOut->p2 = aOp->p2;
85995 assert( aOp->p2>=0 );
85996 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
85997 pOut->p2 += p->nOp;
85999 pOut->p3 = aOp->p3;
86000 pOut->p4type = P4_NOTUSED;
86001 pOut->p4.p = 0;
86002 pOut->p5 = 0;
86004 pOut->zComment = 0;
86007 pOut->iSrcLine = iLineno+i;
86012 if( p->db->flags & SQLITE_VdbeAddopTrace ){
86013 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
86017 p->nOp += nOp;
86033 if( IS_STMT_SCANSTATUS(p->db) ){
86034 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
86036 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
86038 ScanStatus *pNew = &aNew[p->nScan++];
86040 pNew->addrExplain = addrExplain;
86041 pNew->addrLoop = addrLoop;
86042 pNew->addrVisit = addrVisit;
86043 pNew->nEst = nEst;
86044 pNew->zName = sqlite3DbStrDup(p->db, zName);
86045 p->aScan = aNew;
86063 if( IS_STMT_SCANSTATUS(p->db) ){
86066 for(ii=p->nScan-1; ii>=0; ii--){
86067 pScan = &p->aScan[ii];
86068 if( pScan->addrExplain==addrExplain ) break;
86072 if( addrEnd<0 ) addrEnd = sqlite3VdbeCurrentAddr(p)-1;
86073 for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){
86074 if( pScan->aAddrRange[ii]==0 ){
86075 pScan->aAddrRange[ii] = addrStart;
86076 pScan->aAddrRange[ii+1] = addrEnd;
86095 if( IS_STMT_SCANSTATUS(p->db) ){
86098 for(ii=p->nScan-1; ii>=0; ii--){
86099 pScan = &p->aScan[ii];
86100 if( pScan->addrExplain==addrExplain ) break;
86104 if( addrLoop>0 ) pScan->addrLoop = addrLoop;
86105 if( addrVisit>0 ) pScan->addrVisit = addrVisit;
86118 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
86122 sqlite3VdbeGetOp(p,addr)->p1 = val;
86125 assert( addr>=0 || p->db->mallocFailed );
86126 sqlite3VdbeGetOp(p,addr)->p2 = val;
86130 sqlite3VdbeGetOp(p,addr)->p3 = val;
86133 assert( p->nOp>0 || p->db->mallocFailed );
86134 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
86144 if( pOp->p3==iDest && pOp->opcode==OP_Column ){
86145 pOp->p5 |= OPFLAG_TYPEOFARG;
86154 sqlite3VdbeChangeP2(p, addr, p->nOp);
86160 ** the previous opcode (and is thus a no-op) then simply back up
86165 ** strives to omit useless byte-code like this:
86171 if( addr==p->nOp-1 ){
86172 assert( p->aOp[addr].opcode==OP_Once
86173 || p->aOp[addr].opcode==OP_If
86174 || p->aOp[addr].opcode==OP_FkIfZero );
86175 assert( p->aOp[addr].p4type==0 );
86177 sqlite3VdbeGetLastOp(p)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
86179 p->nOp--;
86181 sqlite3VdbeChangeP2(p, addr, p->nOp);
86192 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
86201 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
86206 freeEphemeralFunction(db, p->pFunc);
86224 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
86238 if( db->pnBytesFreed==0 ){
86246 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
86250 if( db->pnBytesFreed==0 ) sqlite3DeleteTable(db, (Table*)p4);
86265 Op *pOp = &aOp[nOp-1];
86267 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
86269 sqlite3DbFree(db, pOp->zComment);
86272 pOp--;
86280 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
86284 p->pNext = pVdbe->pProgram;
86285 pVdbe->pProgram = p;
86292 return pVdbe->pProgram!=0;
86300 if( p->db->mallocFailed ) return 0;
86301 assert( addr>=0 && addr<p->nOp );
86302 pOp = &p->aOp[addr];
86303 freeP4(p->db, pOp->p4type, pOp->p4.p);
86304 pOp->p4type = P4_NOTUSED;
86305 pOp->p4.z = 0;
86306 pOp->opcode = OP_Noop;
86315 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
86316 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
86334 if( N==0 || OptimizationDisabled(pParse->db, SQLITE_ReleaseReg) ) return;
86335 assert( pParse->pVdbe );
86337 assert( iFirst+N-1<=pParse->nMem );
86342 N--;
86344 while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){
86345 mask &= ~MASKBIT32(N-1);
86346 N--;
86350 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask);
86351 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
86379 if( pOp->p4type ){
86380 assert( pOp->p4type > P4_FREE_IF_LE );
86381 pOp->p4type = 0;
86382 pOp->p4.p = 0;
86385 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
86388 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
86389 pOp->p4type = P4_DYNAMIC;
86396 db = p->db;
86397 assert( p->eVdbeState==VDBE_INIT_STATE );
86398 assert( p->aOp!=0 || db->mallocFailed );
86399 if( db->mallocFailed ){
86403 assert( p->nOp>0 );
86404 assert( addr<p->nOp );
86406 addr = p->nOp - 1;
86408 pOp = &p->aOp[addr];
86409 if( n>=0 || pOp->p4type ){
86416 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
86417 pOp->p4type = P4_INT32;
86420 pOp->p4.p = (void*)zP4;
86421 pOp->p4type = (signed char)n;
86428 ** to the value defined by the arguments. This is a high-speed
86439 if( p->db->mallocFailed ){
86440 freeP4(p->db, n, pP4);
86443 assert( p->nOp>0 );
86444 pOp = &p->aOp[p->nOp-1];
86445 assert( pOp->p4type==P4_NOTUSED );
86446 pOp->p4type = n;
86447 pOp->p4.p = pP4;
86456 Vdbe *v = pParse->pVdbe;
86467 ** insert a No-op and add the comment to that new instruction. This
86472 assert( p->nOp>0 || p->aOp==0 );
86473 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->pParse->nErr>0 );
86474 if( p->nOp ){
86475 assert( p->aOp );
86476 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
86477 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
86504 sqlite3VdbeGetLastOp(v)->iSrcLine = iLine;
86509 ** Return the opcode for a given address. The address must be non-negative.
86525 assert( p->eVdbeState==VDBE_INIT_STATE );
86526 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
86527 if( p->db->mallocFailed ){
86530 return &p->aOp[addr];
86537 return sqlite3VdbeGetOp(p, p->nOp - 1);
86546 if( c=='1' ) return pOp->p1;
86547 if( c=='2' ) return pOp->p2;
86548 if( c=='3' ) return pOp->p3;
86549 if( c=='4' ) return pOp->p4.i;
86550 return pOp->p5;
86561 ** "PX" -> "r[X]"
86562 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
86563 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
86564 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
86567 sqlite3 *db, /* Optional - Oom error reporting only */
86579 zOpName = sqlite3OpcodeName(pOp->opcode);
86595 if( pOp->zComment && pOp->zComment[0] ){
86596 sqlite3_str_appendall(&x, pOp->zComment);
86613 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
86616 sqlite3_context *pCtx = pOp->p4.pCtx;
86617 if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){
86619 }else if( pCtx->argc>1 ){
86620 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
86623 x.nChar -= 2;
86629 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
86638 if( !seenCom && pOp->zComment ){
86639 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
86641 }else if( pOp->zComment ){
86642 sqlite3_str_appendall(&x, pOp->zComment);
86658 switch( pExpr->op ){
86661 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
86664 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
86670 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
86674 if( pExpr->iColumn<0 ){
86677 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
86715 displayP4Expr(p, pExpr->pLeft);
86716 if( pExpr->pRight ){
86718 displayP4Expr(p, pExpr->pRight);
86736 switch( pOp->p4type ){
86739 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
86740 assert( pKeyInfo->aSortFlags!=0 );
86741 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
86742 for(j=0; j<pKeyInfo->nKeyField; j++){
86743 CollSeq *pColl = pKeyInfo->aColl[j];
86744 const char *zColl = pColl ? pColl->zName : "";
86747 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "",
86748 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "",
86756 displayP4Expr(&x, pOp->p4.pExpr);
86762 CollSeq *pColl = pOp->p4.pColl;
86763 assert( pColl->enc<4 );
86764 sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
86765 encnames[pColl->enc]);
86769 FuncDef *pDef = pOp->p4.pFunc;
86770 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
86774 FuncDef *pDef = pOp->p4.pCtx->pFunc;
86775 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
86779 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
86783 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
86787 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
86791 Mem *pMem = pOp->p4.pMem;
86792 if( pMem->flags & MEM_Str ){
86793 zP4 = pMem->z;
86794 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
86795 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
86796 }else if( pMem->flags & MEM_Real ){
86797 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
86798 }else if( pMem->flags & MEM_Null ){
86801 assert( pMem->flags & MEM_Blob );
86808 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
86815 u32 *ai = pOp->p4.ai;
86829 zP4 = pOp->p4.pTab->zName;
86833 zP4 = pOp->p4.z;
86845 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
86849 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
86850 ** p->btreeMask of databases that will require a lock.
86853 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
86854 assert( i<(int)sizeof(p->btreeMask)*8 );
86855 DbMaskSet(p->btreeMask, i);
86856 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
86857 DbMaskSet(p->lockMask, i);
86863 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
86867 ** that the correct busy-handler callback is invoked if required.
86869 ** If SQLite is not threadsafe but does support shared-cache mode, then
86874 ** If SQLite is not threadsafe and does not support shared-cache mode, this
86875 ** function is a no-op.
86877 ** The p->btreeMask field is a bitmask of all btrees that the prepared
86878 ** statement p will ever use. Let N be the number of bits in p->btreeMask
86888 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
86889 db = p->db;
86890 aDb = db->aDb;
86891 nDb = db->nDb;
86893 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
86909 db = p->db;
86910 aDb = db->aDb;
86911 nDb = db->nDb;
86913 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
86919 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
86932 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
86946 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
86947 zP4 ? zP4 : "", pOp->p5,
86960 ** This is a high-runner, so only those fields that really do need to
86975 p->flags = flags;
86976 p->db = db;
86977 p->szMalloc = 0;
86979 p->pScopyFrom = 0;
86982 }while( (--N)>0 );
86997 sqlite3 *db = p->db;
86998 if( db->pnBytesFreed ){
87000 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
87014 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
87015 ** sqlite3MemRelease() were called from here. With -O2, this jumps
87020 testcase( p->flags & MEM_Agg );
87021 testcase( p->flags & MEM_Dyn );
87022 if( p->flags&(MEM_Agg|MEM_Dyn) ){
87023 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
87025 p->flags = MEM_Undefined;
87026 }else if( p->szMalloc ){
87027 sqlite3DbNNFreeNN(db, p->zMalloc);
87028 p->szMalloc = 0;
87029 p->flags = MEM_Undefined;
87033 p->flags = MEM_Undefined;
87048 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
87064 pFrame->pParent = pFrame->v->pDelFrame;
87065 pFrame->v->pDelFrame = pFrame;
87085 int nSub = 0; /* Number of sub-vdbes seen so far */
87086 SubProgram **apSub = 0; /* Array of sub-vdbes */
87097 ** encountered, but p->pc will eventually catch up to nRow.
87099 nRow = p->nOp;
87101 if( pSub->flags&MEM_Blob ){
87104 nSub = pSub->n/sizeof(Vdbe*);
87105 apSub = (SubProgram **)pSub->z;
87108 nRow += apSub[i]->nOp;
87115 p->rc = SQLITE_OK;
87119 if( i<p->nOp ){
87122 aOp = p->aOp;
87127 i -= p->nOp;
87130 for(j=0; i>=apSub[j]->nOp; j++){
87131 i -= apSub[j]->nOp;
87132 assert( i<apSub[j]->nOp || j+1<nSub );
87134 aOp = apSub[j]->aOp;
87139 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
87149 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
87150 if( p->rc!=SQLITE_OK ){
87154 apSub = (SubProgram **)pSub->z;
87157 pSub->n = nSub*sizeof(SubProgram*);
87158 nRow += aOp[i].p4.pProgram->nOp;
87165 if( pOp->opcode==OP_OpenRead ) break;
87166 if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
87167 if( pOp->opcode==OP_ReopenIdx ) break;
87191 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
87193 for(i=0; i<p->nChildCsr; i++){
87194 if( apCsr[i] ) sqlite3VdbeFreeCursorNN(p->v, apCsr[i]);
87196 releaseMemArray(aMem, p->nChildMem);
87197 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
87198 sqlite3DbFree(p->v->db, p);
87209 ** When p->explain==1, each instruction is listed. When
87210 ** p->explain==2, only OP_Explain instructions are listed and these
87211 ** are shown in a different format. p->explain==2 is used to implement
87213 ** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
87217 ** When p->explain==1, first the main program is listed, then each of
87224 sqlite3 *db = p->db; /* The database connection */
87227 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
87228 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
87232 assert( p->explain );
87233 assert( p->eVdbeState==VDBE_RUN_STATE );
87234 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
87238 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
87242 if( p->rc==SQLITE_NOMEM ){
87254 assert( p->nMem>9 );
87255 pSub = &p->aMem[9];
87261 rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
87265 if( AtomicLoad(&db->u1.isInterrupted) ){
87266 p->rc = SQLITE_INTERRUPT;
87268 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
87271 if( p->explain==2 ){
87272 sqlite3VdbeMemSetInt64(pMem, pOp->p1);
87273 sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
87274 sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
87275 sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free);
87276 assert( p->nResColumn==4 );
87279 sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
87280 -1, SQLITE_UTF8, SQLITE_STATIC);
87281 sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
87282 sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
87283 sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
87285 sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
87289 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free);
87294 sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free);
87295 assert( p->nResColumn==8 );
87297 p->pResultRow = pMem;
87298 if( db->mallocFailed ){
87299 p->rc = SQLITE_NOMEM;
87302 p->rc = SQLITE_OK;
87317 if( p->zSql ){
87318 z = p->zSql;
87319 }else if( p->nOp>=1 ){
87320 const VdbeOp *pOp = &p->aOp[0];
87321 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
87322 z = pOp->p4.z;
87335 int nOp = p->nOp;
87339 pOp = &p->aOp[0];
87340 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
87343 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
87347 if( z[i-1]!=' ' ){
87370 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
87389 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
87392 if( nByte <= p->nFree ){
87393 p->nFree -= nByte;
87394 pBuf = &p->pSpace[p->nFree];
87396 p->nNeeded += nByte;
87412 assert( p->eVdbeState==VDBE_INIT_STATE
87413 || p->eVdbeState==VDBE_READY_STATE
87414 || p->eVdbeState==VDBE_HALT_STATE );
87418 assert( p->nOp>0 );
87420 p->eVdbeState = VDBE_READY_STATE;
87423 for(i=0; i<p->nMem; i++){
87424 assert( p->aMem[i].db==p->db );
87427 p->pc = -1;
87428 p->rc = SQLITE_OK;
87429 p->errorAction = OE_Abort;
87430 p->nChange = 0;
87431 p->cacheCtr = 1;
87432 p->minWriteFileFormat = 255;
87433 p->iStatement = 0;
87434 p->nFkConstraint = 0;
87436 for(i=0; i<p->nOp; i++){
87437 p->aOp[i].nExec = 0;
87438 p->aOp[i].nCycle = 0;
87474 assert( p->nOp>0 );
87476 assert( p->eVdbeState==VDBE_INIT_STATE );
87477 assert( pParse==p->pParse );
87478 p->pVList = pParse->pVList;
87479 pParse->pVList = 0;
87480 db = p->db;
87481 assert( db->mallocFailed==0 );
87482 nVar = pParse->nVar;
87483 nMem = pParse->nMem;
87484 nCursor = pParse->nTab;
87485 nArg = pParse->nMaxArg;
87499 n = ROUND8P(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
87500 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
87502 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
87507 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
87508 if( pParse->explain ){
87510 p->explain = pParse->explain;
87511 p->nResColumn = 12 - 4*p->explain;
87513 p->expired = 0;
87521 ** This two-pass approach that reuses as much memory as possible from
87526 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
87527 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
87528 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
87529 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
87531 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
87533 if( !db->mallocFailed ){
87534 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
87535 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
87536 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
87537 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
87541 if( db->mallocFailed ){
87542 p->nVar = 0;
87543 p->nCursor = 0;
87544 p->nMem = 0;
87546 p->nCursor = nCursor;
87547 p->nVar = (ynVar)nVar;
87548 initMemArray(p->aVar, nVar, db, MEM_Null);
87549 p->nMem = nMem;
87550 initMemArray(p->aMem, nMem, db, MEM_Undefined);
87551 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
87564 VdbeTxtBlbCache *pCache = pCx->pCache;
87565 assert( pCx->colCache );
87566 pCx->colCache = 0;
87567 pCx->pCache = 0;
87568 if( pCache->pCValue ){
87569 sqlite3RCStrUnref(pCache->pCValue);
87570 pCache->pCValue = 0;
87572 sqlite3DbFree(p->db, pCache);
87576 if( pCx->colCache ){
87580 switch( pCx->eCurType ){
87582 sqlite3VdbeSorterClose(p->db, pCx);
87586 assert( pCx->uc.pCursor!=0 );
87587 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
87592 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
87593 const sqlite3_module *pModule = pVCur->pVtab->pModule;
87594 assert( pVCur->pVtab->nRef>0 );
87595 pVCur->pVtab->nRef--;
87596 pModule->xClose(pVCur);
87608 for(i=0; i<p->nCursor; i++){
87609 VdbeCursor *pC = p->apCsr[i];
87612 p->apCsr[i] = 0;
87619 ** is used, for example, when a trigger sub-program is halted to restore
87623 Vdbe *v = pFrame->v;
87625 v->aOp = pFrame->aOp;
87626 v->nOp = pFrame->nOp;
87627 v->aMem = pFrame->aMem;
87628 v->nMem = pFrame->nMem;
87629 v->apCsr = pFrame->apCsr;
87630 v->nCursor = pFrame->nCursor;
87631 v->db->lastRowid = pFrame->lastRowid;
87632 v->nChange = pFrame->nChange;
87633 v->db->nChange = pFrame->nDbChange;
87634 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
87635 v->pAuxData = pFrame->pAuxData;
87636 pFrame->pAuxData = 0;
87637 return pFrame->pc;
87649 if( p->pFrame ){
87651 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
87653 p->pFrame = 0;
87654 p->nFrame = 0;
87656 assert( p->nFrame==0 );
87658 releaseMemArray(p->aMem, p->nMem);
87659 while( p->pDelFrame ){
87660 VdbeFrame *pDel = p->pDelFrame;
87661 p->pDelFrame = pDel->pParent;
87666 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
87667 assert( p->pAuxData==0 );
87678 sqlite3 *db = p->db;
87680 if( p->nResAlloc ){
87681 releaseMemArray(p->aColName, p->nResAlloc*COLNAME_N);
87682 sqlite3DbFree(db, p->aColName);
87685 p->nResColumn = p->nResAlloc = (u16)nResColumn;
87686 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
87687 if( p->aColName==0 ) return;
87688 initMemArray(p->aColName, n, db, MEM_Null);
87710 assert( idx<p->nResAlloc );
87712 if( p->db->mallocFailed ){
87716 assert( p->aColName!=0 );
87717 pColName = &(p->aColName[idx+var*p->nResAlloc]);
87718 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
87719 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
87726 ** write-transaction spanning more than one database file, this routine
87727 ** takes care of the super-journal trickery.
87731 int nTrans = 0; /* Number of databases with an active write-transaction
87732 ** that are candidates for a two-phase commit using a
87733 ** super-journal */
87746 ** be done before determining whether a super-journal file is
87755 ** one database file has an open write transaction, a super-journal
87758 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
87759 Btree *pBt = db->aDb[i].pBt;
87761 /* Whether or not a database might need a super-journal depends upon
87763 ** journal modes use a super-journal and which do not */
87776 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
87791 /* If there are any write-transactions at all, invoke the commit hook */
87792 if( needXcommit && db->xCommitCallback ){
87793 rc = db->xCommitCallback(db->pCommitArg);
87799 /* The simple case - no more than one database file (not counting the
87801 ** super-journal.
87805 ** that case we do not support atomic multi-file commits, so use the
87808 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
87811 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
87812 Btree *pBt = db->aDb[i].pBt;
87823 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
87824 Btree *pBt = db->aDb[i].pBt;
87834 /* The complex case - There is a multi-file write-transaction active.
87835 ** This requires a super-journal file to ensure the transaction is
87840 sqlite3_vfs *pVfs = db->pVfs;
87841 char *zSuper = 0; /* File-name for the super-journal */
87842 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
87849 /* Select a super-journal file name */
87867 sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
87869 /* The antipenultimate character of the super-journal name must
87871 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' );
87876 /* Open the super-journal. */
87883 sqlite3DbFree(db, zSuper-4);
87888 ** super-journal file. If an error occurs at this point close
87889 ** and delete the super-journal file. All the individual journal files
87890 ** still have 'null' as the super-journal pointer, so they will roll
87893 for(i=0; i<db->nDb; i++){
87894 Btree *pBt = db->aDb[i].pBt;
87906 sqlite3DbFree(db, zSuper-4);
87912 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
87920 sqlite3DbFree(db, zSuper-4);
87925 ** sets the super-journal pointer in each individual journal. If
87926 ** an error occurs here, do not delete the super-journal file.
87930 ** super-journal file will be orphaned. But we cannot delete it,
87931 ** in case the super-journal file name was written into the journal
87934 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
87935 Btree *pBt = db->aDb[i].pBt;
87943 sqlite3DbFree(db, zSuper-4);
87947 /* Delete the super-journal file. This commits the transaction. After
87952 sqlite3DbFree(db, zSuper-4);
87967 for(i=0; i<db->nDb; i++){
87968 Btree *pBt = db->aDb[i].pBt;
87987 ** This is an internal self-check only - it is not an essential processing
87990 ** This is a no-op if NDEBUG is defined.
87998 p = db->pVdbe;
88002 if( p->readOnly==0 ) nWrite++;
88003 if( p->bIsReader ) nRead++;
88005 p = p->pVNext;
88007 assert( cnt==db->nVdbeActive );
88008 assert( nWrite==db->nVdbeWrite );
88009 assert( nRead==db->nVdbeRead );
88016 ** If the Vdbe passed as the first argument opened a statement-transaction,
88026 sqlite3 *const db = p->db;
88029 const int iSavepoint = p->iStatement-1;
88032 assert( db->nStatement>0 );
88033 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
88035 for(i=0; i<db->nDb; i++){
88037 Btree *pBt = db->aDb[i].pBt;
88050 db->nStatement--;
88051 p->iStatement = 0;
88066 db->nDeferredCons = p->nStmtDefCons;
88067 db->nDeferredImmCons = p->nStmtDefImmCons;
88072 if( p->db->nStatement && p->iStatement ){
88091 sqlite3 *db = p->db;
88092 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
88093 || (!deferred && p->nFkConstraint>0)
88095 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
88096 p->errorAction = OE_Abort;
88098 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ) return SQLITE_ERROR;
88120 sqlite3 *db = p->db;
88138 assert( p->eVdbeState==VDBE_RUN_STATE );
88139 if( db->mallocFailed ){
88140 p->rc = SQLITE_NOMEM_BKPT;
88147 if( p->bIsReader ){
88148 int mrc; /* Primary error code from p->rc */
88156 if( p->rc ){
88157 mrc = p->rc & 0xff;
88166 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
88171 ** Even if the statement is read-only, it is important to perform
88173 ** occurred while writing to the journal, sub-journal or database
88178 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
88179 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
88187 db->autoCommit = 1;
88188 p->nChange = 0;
88194 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
88198 /* If the auto-commit flag is set and this is the only active writer
88205 && db->autoCommit
88206 && db->nVdbeWrite==(p->readOnly==0)
88208 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
88211 if( NEVER(p->readOnly) ){
88216 }else if( db->flags & SQLITE_CorruptRdOnly ){
88218 db->flags &= ~SQLITE_CorruptRdOnly;
88220 /* The auto-commit flag is true, the vdbe program was successful
88226 if( rc==SQLITE_BUSY && p->readOnly ){
88231 p->rc = rc;
88233 p->nChange = 0;
88235 db->nDeferredCons = 0;
88236 db->nDeferredImmCons = 0;
88237 db->flags &= ~(u64)SQLITE_DeferFKs;
88240 }else if( p->rc==SQLITE_SCHEMA && db->nVdbeActive>1 ){
88241 p->nChange = 0;
88244 p->nChange = 0;
88246 db->nStatement = 0;
88248 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
88250 }else if( p->errorAction==OE_Abort ){
88255 db->autoCommit = 1;
88256 p->nChange = 0;
88260 /* If eStatementOp is non-zero, then a statement transaction needs to
88269 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
88270 p->rc = rc;
88271 sqlite3DbFree(db, p->zErrMsg);
88272 p->zErrMsg = 0;
88276 db->autoCommit = 1;
88277 p->nChange = 0;
88282 ** has been rolled back, update the database connection change-counter.
88284 if( p->changeCntOn ){
88286 sqlite3VdbeSetChanges(db, p->nChange);
88290 p->nChange = 0;
88298 db->nVdbeActive--;
88299 if( !p->readOnly ) db->nVdbeWrite--;
88300 if( p->bIsReader ) db->nVdbeRead--;
88301 assert( db->nVdbeActive>=db->nVdbeRead );
88302 assert( db->nVdbeRead>=db->nVdbeWrite );
88303 assert( db->nVdbeWrite>=0 );
88304 p->eVdbeState = VDBE_HALT_STATE;
88306 if( db->mallocFailed ){
88307 p->rc = SQLITE_NOMEM_BKPT;
88310 /* If the auto-commit flag is set to true, then any locks that were held
88312 ** to invoke any required unlock-notify callbacks.
88314 if( db->autoCommit ){
88318 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
88319 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
88325 ** in p->rc. This routine sets that result back to SQLITE_OK.
88328 p->rc = SQLITE_OK;
88340 sqlite3 *db = p->db;
88341 int rc = p->rc;
88342 if( p->zErrMsg ){
88343 db->bBenignMalloc++;
88345 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
88346 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
88348 db->bBenignMalloc--;
88349 }else if( db->pErr ){
88350 sqlite3ValueSetNull(db->pErr);
88352 db->errCode = rc;
88353 db->errByteOffset = -1;
88363 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
88364 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
88365 assert( v->db->init.busy==0 );
88368 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
88370 sqlite3DbFree(v->db, zExpanded);
88395 db = p->db;
88401 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
88408 if( p->pc>=0 ){
88410 if( db->pErr || p->zErrMsg ){
88413 db->errCode = p->rc;
88422 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
88423 if( p->aMem ){
88424 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
88427 if( p->zErrMsg ){
88428 sqlite3DbFree(db, p->zErrMsg);
88429 p->zErrMsg = 0;
88431 p->pResultRow = 0;
88433 p->nWrite = 0;
88442 fprintf(out, "---- ");
88443 for(i=0; i<p->nOp; i++){
88444 fprintf(out, "%02x", p->aOp[i].opcode);
88447 if( p->zSql ){
88449 fprintf(out, "-- ");
88450 for(i=0; (c = p->zSql[i])!=0; i++){
88451 if( pc=='\n' ) fprintf(out, "-- ");
88457 for(i=0; i<p->nOp; i++){
88459 i64 cnt = p->aOp[i].nExec;
88460 i64 cycles = p->aOp[i].nCycle;
88467 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
88473 return p->rc & db->errMask;
88485 if( p->eVdbeState>=VDBE_READY_STATE ){
88487 assert( (rc & p->db->errMask)==rc );
88513 || (pAux->iAuxOp==iOp
88514 && pAux->iAuxArg>=0
88515 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
88517 testcase( pAux->iAuxArg==31 );
88518 if( pAux->xDeleteAux ){
88519 pAux->xDeleteAux(pAux->pAux);
88521 *pp = pAux->pNextAux;
88524 pp= &pAux->pNextAux;
88540 assert( p->db==0 || p->db==db );
88541 if( p->aColName ){
88542 releaseMemArray(p->aColName, p->nResAlloc*COLNAME_N);
88543 sqlite3DbNNFreeNN(db, p->aColName);
88545 for(pSub=p->pProgram; pSub; pSub=pNext){
88546 pNext = pSub->pNext;
88547 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
88550 if( p->eVdbeState!=VDBE_INIT_STATE ){
88551 releaseMemArray(p->aVar, p->nVar);
88552 if( p->pVList ) sqlite3DbNNFreeNN(db, p->pVList);
88553 if( p->pFree ) sqlite3DbNNFreeNN(db, p->pFree);
88555 vdbeFreeOpArray(db, p->aOp, p->nOp);
88556 if( p->zSql ) sqlite3DbNNFreeNN(db, p->zSql);
88558 sqlite3DbFree(db, p->zNormSql);
88561 for(pThis=p->pDblStr; pThis; pThis=pNxt){
88562 pNxt = pThis->pNextStr;
88570 for(i=0; i<p->nScan; i++){
88571 sqlite3DbFree(db, p->aScan[i].zName);
88573 sqlite3DbFree(db, p->aScan);
88585 db = p->db;
88587 assert( sqlite3_mutex_held(db->mutex) );
88589 if( db->pnBytesFreed==0 ){
88590 assert( p->ppVPrev!=0 );
88591 *p->ppVPrev = p->pVNext;
88592 if( p->pVNext ){
88593 p->pVNext->ppVPrev = p->ppVPrev;
88609 assert( p->deferredMoveto );
88610 assert( p->isTable );
88611 assert( p->eCurType==CURTYPE_BTREE );
88612 rc = sqlite3BtreeTableMoveto(p->uc.pCursor, p->movetoTarget, 0, &res);
88618 p->deferredMoveto = 0;
88619 p->cacheStatus = CACHE_STALE;
88632 assert( p->eCurType==CURTYPE_BTREE );
88633 assert( p->uc.pCursor!=0 );
88634 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
88635 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
88636 p->cacheStatus = CACHE_STALE;
88637 if( isDifferentRow ) p->nullRow = 1;
88646 assert( p->eCurType==CURTYPE_BTREE || IsNullCursor(p) );
88647 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
88659 ** sqlite3VdbeSerialPut() <--- in-lined into OP_MakeRecord as of 2022-04-02
88664 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
88671 ** serial-type and data blob separately.
88676 ** -------------- --------------- ---------------
88688 ** N>=12 and even (N-12)/2 BLOB
88689 ** N>=13 and odd (N-13)/2 text
88697 ** Return the serial-type for the value stored in pMem.
88701 ** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
88702 ** opcode in the byte-code engine. But by moving this routine in-line, we
88708 int flags = pMem->flags;
88718 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
88719 i64 i = pMem->u.i;
88744 ** as an integer, then we might as well make it an 8-byte floating
88746 pMem->u.r = (double)pMem->u.i;
88747 pMem->flags &= ~MEM_IntReal;
88748 pMem->flags |= MEM_Real;
88757 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
88758 assert( pMem->n>=0 );
88759 n = (u32)pMem->n;
88761 n += pMem->u.nZero;
88789 ** Return the length of the data corresponding to the supplied serial-type.
88793 return (serial_type-12)/2;
88796 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
88806 ** If we are on an architecture with mixed-endian floating
88810 ** For most architectures, this is a no-op.
88812 ** (later): It is reported to me that the mixed-endian problem
88814 ** that early versions of GCC stored the two words of a 64-bit
88822 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
88826 ** (2007-08-30) Frank van Vugt has studied this problem closely
88829 ** emulation that uses only 32-bit mantissas instead of a full
88830 ** 48-bits as required by the IEEE standard. (This is the
88833 ** the necessary byte swapping is carried out using a 64-bit integer
88834 ** rather than a 64-bit float. Frank assures us that the code here
88857 ** big-endian integer. Return the equivalent native integer
88883 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
88884 ** twos-complement integer. */
88885 pMem->u.i = *(i64*)&x;
88886 pMem->flags = MEM_Int;
88887 testcase( pMem->u.i<0 );
88889 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
88894 ** defined that 64-bit floating point values really are mixed
88903 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
88905 memcpy(&pMem->u.r, &x, sizeof(x));
88906 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
88916 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
88918 memcpy(&pMem->u.r, &x, sizeof(x));
88920 pMem->flags = MEM_Null;
88923 pMem->flags = MEM_Real;
88933 ** UPDATE no-change flag set */
88934 pMem->flags = MEM_Null|MEM_Zero;
88935 pMem->n = 0;
88936 pMem->u.nZero = 0;
88941 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
88942 pMem->flags = MEM_Null;
88946 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
88948 pMem->u.i = ONE_BYTE_INT(buf);
88949 pMem->flags = MEM_Int;
88950 testcase( pMem->u.i<0 );
88953 case 2: { /* 2-byte signed integer */
88954 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
88955 ** twos-complement integer. */
88956 pMem->u.i = TWO_BYTE_INT(buf);
88957 pMem->flags = MEM_Int;
88958 testcase( pMem->u.i<0 );
88961 case 3: { /* 3-byte signed integer */
88962 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
88963 ** twos-complement integer. */
88964 pMem->u.i = THREE_BYTE_INT(buf);
88965 pMem->flags = MEM_Int;
88966 testcase( pMem->u.i<0 );
88969 case 4: { /* 4-byte signed integer */
88970 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
88971 ** twos-complement integer. */
88972 pMem->u.i = FOUR_BYTE_INT(buf);
88974 /* Work around a sign-extension bug in the HP compiler for HP/UX */
88975 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
88977 pMem->flags = MEM_Int;
88978 testcase( pMem->u.i<0 );
88981 case 5: { /* 6-byte signed integer */
88982 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
88983 ** twos-complement integer. */
88984 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
88985 pMem->flags = MEM_Int;
88986 testcase( pMem->u.i<0 );
88989 case 6: /* 8-byte signed integer */
88998 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
88999 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
89000 pMem->u.i = serial_type-8;
89001 pMem->flags = MEM_Int;
89005 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
89007 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
89008 ** (N-13)/2 bytes in length. */
89010 pMem->z = (char *)buf;
89011 pMem->n = (serial_type-12)/2;
89012 pMem->flags = aFlag[serial_type&1];
89037 nByte = ROUND8P(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
89038 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
89040 p->aMem = (Mem*)&((char*)p)[ROUND8P(sizeof(UnpackedRecord))];
89041 assert( pKeyInfo->aSortFlags!=0 );
89042 p->pKeyInfo = pKeyInfo;
89043 p->nField = pKeyInfo->nKeyField + 1;
89048 ** Given the nKey-byte encoding of a record in pKey[], populate the
89063 Mem *pMem = p->aMem;
89065 p->default_rc = 0;
89074 pMem->enc = pKeyInfo->enc;
89075 pMem->db = pKeyInfo->db;
89076 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
89077 pMem->szMalloc = 0;
89078 pMem->z = 0;
89082 if( (++u)>=p->nField ) break;
89089 sqlite3VdbeMemSetNull(pMem-1);
89091 assert( u<=pKeyInfo->nKeyField + 1 );
89092 p->nField = u;
89121 pKeyInfo = pPKey2->pKeyInfo;
89122 if( pKeyInfo->db==0 ) return 1;
89123 mem1.enc = pKeyInfo->enc;
89124 mem1.db = pKeyInfo->db;
89140 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
89141 assert( pKeyInfo->aSortFlags!=0 );
89142 assert( pKeyInfo->nKeyField>0 );
89178 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
89179 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
89182 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
89183 && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null))
89185 rc = -rc;
89187 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){
89188 rc = -rc; /* Invert the result for DESC sort order. */
89193 }while( idx1<szHdr1 && i<pPKey2->nField );
89204 rc = pPKey2->default_rc;
89211 if( pKeyInfo->db->mallocFailed ) return 1;
89220 ** limit given by pKeyInfo->nAllField.
89222 ** If this constraint is not satisfied, it means that the high-speed
89246 assert( nField <= pKeyInfo->nAllField );
89256 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
89264 if( pMem1->enc==pColl->enc ){
89267 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
89273 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
89274 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
89277 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
89278 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
89283 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
89293 ** with MEM_Zero. Return true if it could be a zero-blob.
89310 int n1 = pB1->n;
89311 int n2 = pB2->n;
89313 /* It is possible to have a Blob value that has some non-zero content
89317 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
89318 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
89320 if( (pB1->flags|pB2->flags) & MEM_Zero ){
89321 if( pB1->flags & pB2->flags & MEM_Zero ){
89322 return pB1->u.nZero - pB2->u.nZero;
89323 }else if( pB1->flags & MEM_Zero ){
89324 if( !isAllZero(pB2->z, pB2->n) ) return -1;
89325 return pB1->u.nZero - n2;
89327 if( !isAllZero(pB1->z, pB1->n) ) return +1;
89328 return n1 - pB2->u.nZero;
89331 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
89333 return n1 - n2;
89347 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
89362 return (x<r) ? -1 : (x>r);
89365 if( r<-9223372036854775808.0 ) return +1;
89366 if( r>=9223372036854775808.0 ) return -1;
89368 if( i<y ) return -1;
89373 return (((double)i)<r) ? -1 : (((double)i)>r);
89390 f1 = pMem1->flags;
89391 f2 = pMem2->flags;
89399 return (f2&MEM_Null) - (f1&MEM_Null);
89411 if( pMem1->u.i < pMem2->u.i ) return -1;
89412 if( pMem1->u.i > pMem2->u.i ) return +1;
89416 if( pMem1->u.r < pMem2->u.r ) return -1;
89417 if( pMem1->u.r > pMem2->u.r ) return +1;
89424 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
89426 if( pMem1->u.i < pMem2->u.i ) return -1;
89427 if( pMem1->u.i > pMem2->u.i ) return +1;
89430 return -1;
89437 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
89439 return -1;
89453 return -1;
89456 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
89457 assert( pMem1->enc==SQLITE_UTF8 ||
89458 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
89464 assert( !pColl || pColl->xCmp );
89479 ** The first argument passed to this function is a serial-type that
89480 ** corresponds to an integer - all values between 1 and 9 inclusive
89516 return (serial_type - 8);
89528 ** If argument bSkip is non-zero, it is assumed that the caller has already
89532 ** fields that appear in both keys are equal, then pPKey2->default_rc is
89535 ** If database corruption is discovered, set pPKey2->errCode to
89537 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
89538 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
89550 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
89579 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
89584 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
89586 assert( pPKey2->pKeyInfo->aSortFlags!=0 );
89587 assert( pPKey2->pKeyInfo->nKeyField>0 );
89589 while( 1 /*exit-by-break*/ ){
89593 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
89594 testcase( pRhs->flags & MEM_Int );
89595 testcase( pRhs->flags & MEM_IntReal );
89599 rc = serial_type==10 ? -1 : +1;
89601 rc = -1;
89604 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
89607 i64 rhs = pRhs->u.i;
89609 rc = -1;
89617 else if( pRhs->flags & MEM_Real ){
89624 rc = serial_type==10 ? -1 : +1;
89626 rc = -1;
89630 rc = -1; /* mem1 is a NaN */
89631 }else if( mem1.u.r<pRhs->u.r ){
89632 rc = -1;
89633 }else if( mem1.u.r>pRhs->u.r ){
89640 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
89646 else if( pRhs->flags & MEM_Str ){
89650 rc = -1;
89654 mem1.n = (serial_type - 12) / 2;
89658 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
89660 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
89662 }else if( pKeyInfo->aColl[i] ){
89663 mem1.enc = pKeyInfo->enc;
89664 mem1.db = pKeyInfo->db;
89668 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
89671 int nCmp = MIN(mem1.n, pRhs->n);
89672 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
89673 if( rc==0 ) rc = mem1.n - pRhs->n;
89679 else if( pRhs->flags & MEM_Blob ){
89680 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
89684 rc = -1;
89686 int nStr = (serial_type - 12) / 2;
89690 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
89692 }else if( pRhs->flags & MEM_Zero ){
89696 rc = nStr - pRhs->u.nZero;
89699 int nCmp = MIN(nStr, pRhs->n);
89700 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
89701 if( rc==0 ) rc = nStr - pRhs->n;
89720 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
89724 !=(serial_type==0 || (pRhs->flags&MEM_Null)))
89726 rc = -rc;
89735 if( i==pPKey2->nField ) break;
89741 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
89755 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
89756 || pPKey2->pKeyInfo->db->mallocFailed
89758 pPKey2->eqSeen = 1;
89759 return pPKey2->default_rc;
89772 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
89790 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
89793 case 1: { /* 1-byte signed integer */
89798 case 2: { /* 2-byte signed integer */
89803 case 3: { /* 3-byte signed integer */
89808 case 4: { /* 4-byte signed integer */
89814 case 5: { /* 6-byte signed integer */
89819 case 6: { /* 8-byte signed integer */
89846 assert( pPKey2->u.i == pPKey2->aMem[0].u.i );
89847 v = pPKey2->u.i;
89849 res = pPKey2->r1;
89851 res = pPKey2->r2;
89852 }else if( pPKey2->nField>1 ){
89858 ** fields. Return pPKey2->default_rc in this case. */
89859 res = pPKey2->default_rc;
89860 pPKey2->eqSeen = 1;
89870 ** uses the collation sequence BINARY and (c) that the size-of-header varint
89881 assert( pPKey2->aMem[0].flags & MEM_Str );
89882 assert( pPKey2->aMem[0].n == pPKey2->n );
89883 assert( pPKey2->aMem[0].z == pPKey2->u.z );
89884 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
89894 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
89896 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
89902 nStr = (serial_type-12) / 2;
89904 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
89907 nCmp = MIN( pPKey2->n, nStr );
89908 res = memcmp(&aKey1[szHdr], pPKey2->u.z, nCmp);
89911 res = pPKey2->r2;
89913 res = pPKey2->r1;
89915 res = nStr - pPKey2->n;
89917 if( pPKey2->nField>1 ){
89920 res = pPKey2->default_rc;
89921 pPKey2->eqSeen = 1;
89924 res = pPKey2->r2;
89926 res = pPKey2->r1;
89933 || pPKey2->pKeyInfo->db->mallocFailed
89945 ** that the size-of-header varint that occurs at the start of each record
89957 if( p->pKeyInfo->nAllField<=13 ){
89958 int flags = p->aMem[0].flags;
89959 if( p->pKeyInfo->aSortFlags[0] ){
89960 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){
89963 p->r1 = 1;
89964 p->r2 = -1;
89966 p->r1 = -1;
89967 p->r2 = 1;
89970 p->u.i = p->aMem[0].u.i;
89977 && p->pKeyInfo->aColl[0]==0
89980 p->u.z = p->aMem[0].z;
89981 p->n = p->aMem[0].n;
90006 ** than 2GiB are support - anything large must be database corruption.
90008 ** this code can safely assume that nCellKey is 32-bits
90031 /* The last field of the index should be an integer - the ROWID.
90033 getVarint32NR((u8*)&m.z[szHdr-1], typeRowid);
90052 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
90087 assert( pC->eCurType==CURTYPE_BTREE );
90088 pCur = pC->uc.pCursor;
90112 assert( sqlite3_mutex_held(db->mutex) );
90113 db->nChange = nChange;
90114 db->nTotalChange += nChange;
90122 v->changeCntOn = 1;
90131 ** programs obsolete. Removing user-defined functions or collating
90145 for(p = db->pVdbe; p; p=p->pVNext){
90146 p->expired = iCode+1;
90154 return v->db;
90161 return v->prepFlags;
90175 Mem *pMem = &v->aVar[iVar-1];
90176 assert( (v->db->flags & SQLITE_EnableQPSG)==0
90177 || (v->db->mDbFlags & DBFLAG_InternalFunc)!=0 );
90178 if( 0==(pMem->flags & MEM_Null) ){
90179 sqlite3_value *pRet = sqlite3ValueNew(v->db);
90192 ** to sqlite3_reoptimize() that re-preparing the statement may result
90197 assert( (v->db->flags & SQLITE_EnableQPSG)==0
90198 || (v->db->mDbFlags & DBFLAG_InternalFunc)!=0 );
90200 v->expmask |= 0x80000000;
90202 v->expmask |= ((u32)1 << (iVar-1));
90211 ** throw an error if it is given inputs that would make it non-deterministic.
90212 ** This routine is invoked by date/time functions that use non-deterministic
90218 if( pCtx->pVdbe==0 ) return 1;
90220 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
90221 if( pOp->opcode==OP_PureFunc ){
90224 if( pOp->p5 & NC_IsCheck ){
90226 }else if( pOp->p5 & NC_GenCol ){
90231 zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
90232 pCtx->pFunc->zName, zContext);
90233 sqlite3_result_error(pCtx, zMsg, -1);
90244 ** byte-code register values correctly initialized.
90247 if( pExpr->op==TK_REGISTER ){
90248 assert( (pWalker->u.aMem[pExpr->iTable].flags & MEM_Undefined)==0 );
90261 if( pVtab->zErrMsg ){
90262 sqlite3 *db = p->db;
90263 sqlite3DbFree(db, p->zErrMsg);
90264 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
90265 sqlite3_free(pVtab->zErrMsg);
90266 pVtab->zErrMsg = 0;
90275 ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
90286 Mem *pMem = &p->aMem[i];
90287 if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
90296 ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
90302 Vdbe *v, /* Vdbe pre-update hook is invoked by */
90311 sqlite3 *db = v->db;
90314 const char *zTbl = pTab->zName;
90318 if( pTab->tabFlags & TF_WithoutRowid ){
90319 nRealCol = sqlite3PrimaryKeyIndex(pTab)->nColumn;
90320 }else if( pTab->tabFlags & TF_HasVirtual ){
90321 nRealCol = pTab->nNVCol;
90323 nRealCol = pTab->nCol;
90327 assert( db->pPreUpdate==0 );
90334 iKey2 = v->aMem[iReg].u.i;
90341 assert( pCsr->eCurType==CURTYPE_BTREE );
90342 assert( pCsr->nField==nRealCol
90343 || (pCsr->nField==nRealCol+1 && op==SQLITE_DELETE && iReg==-1)
90352 preupdate.keyinfo.nKeyField = pTab->nCol;
90359 db->pPreUpdate = &preupdate;
90360 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
90361 db->pPreUpdate = 0;
90367 for(i=0; i<pCsr->nField; i++){
90398 ** Return TRUE (non-zero) of the statement supplied as an argument needs
90407 return p==0 || p->expired;
90417 if( p->db==0 ){
90441 assert( p->startTime>0 );
90442 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
90443 assert( db->init.busy==0 );
90444 assert( p->zSql!=0 );
90445 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
90446 iElapse = (iNow - p->startTime)*1000000;
90448 if( db->xProfile ){
90449 db->xProfile(db->pProfileArg, p->zSql, iElapse);
90452 if( db->mTrace & SQLITE_TRACE_PROFILE ){
90453 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
90455 p->startTime = 0;
90462 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
90464 # define checkProfileCallback(DB,P) /*no-op*/
90479 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
90480 ** pointer is a harmless no-op. */
90484 sqlite3 *db = v->db;
90486 sqlite3_mutex_enter(db->mutex);
90488 assert( v->eVdbeState>=VDBE_READY_STATE );
90511 sqlite3 *db = v->db;
90512 sqlite3_mutex_enter(db->mutex);
90516 assert( (rc & (db->errMask))==rc );
90518 sqlite3_mutex_leave(db->mutex);
90539 mutex = p->db->mutex;
90542 for(i=0; i<p->nVar; i++){
90543 sqlite3VdbeMemRelease(&p->aVar[i]);
90544 p->aVar[i].flags = MEM_Null;
90546 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
90547 if( p->expmask ){
90548 p->expired = 1;
90561 if( p->flags & (MEM_Blob|MEM_Str) ){
90563 assert( p->flags==MEM_Null && p->z==0 );
90566 p->flags |= MEM_Blob;
90567 return p->n ? p->z : 0;
90589 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
90593 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
90596 && p->eSubtype=='p'
90597 && strcmp(p->u.zPType, zPType)==0
90599 return (void*)p->z;
90618 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
90619 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
90692 if( pVal->flags & MEM_Null ){
90694 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
90696 }else if( pVal->flags & MEM_Int ){
90698 }else if( pVal->flags & MEM_Str ){
90701 assert( eType == aType[pVal->flags&MEM_AffMask] );
90704 return aType[pVal->flags&MEM_AffMask];
90707 return pVal->enc;
90712 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
90717 return (pVal->flags&MEM_FromBind)!=0;
90729 pNew->flags &= ~MEM_Dyn;
90730 pNew->db = 0;
90731 if( pNew->flags&(MEM_Str|MEM_Blob) ){
90732 pNew->flags &= ~(MEM_Static|MEM_Dyn);
90733 pNew->flags |= MEM_Ephem;
90738 }else if( pNew->flags & MEM_Null ){
90740 pNew->flags &= ~(MEM_Term|MEM_Subtype);
90754 ** The following routines are used by user-defined functions to specify
90771 Mem *pOut = pCtx->pOut;
90784 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
90825 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90841 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90852 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90853 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
90859 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90860 pCtx->isError = SQLITE_ERROR;
90861 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
90868 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90869 pCtx->isError = SQLITE_ERROR;
90870 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
90877 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90878 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
90884 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90885 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
90891 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90892 sqlite3VdbeMemSetNull(pCtx->pOut);
90907 pOut = pCtx->pOut;
90908 assert( sqlite3_mutex_held(pOut->db->mutex) );
90910 pOut->flags = MEM_Null;
90919 if( pCtx->pFunc!=0
90920 && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE)==0
90925 pCtx->pFunc->zName);
90926 sqlite3_result_error(pCtx, zErr, -1);
90930 pOut = pCtx->pOut;
90931 assert( sqlite3_mutex_held(pOut->db->mutex) );
90932 pOut->eSubtype = eSubtype & 0xff;
90933 pOut->flags |= MEM_Subtype;
90947 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90963 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90973 sqlite3VdbeMemZeroTerminateIfAble(pCtx->pOut);
90983 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90992 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91001 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91015 pOut = pCtx->pOut;
91016 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91018 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
91032 pOut = pCtx->pOut;
91033 assert( sqlite3_mutex_held(pOut->db->mutex) );
91034 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
91039 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
91042 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
91049 pCtx->isError = errCode ? errCode : -1;
91051 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
91053 if( pCtx->pOut->flags & MEM_Null ){
91054 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8,
91064 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91065 pCtx->isError = SQLITE_TOOBIG;
91066 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
91075 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91076 sqlite3VdbeMemSetNull(pCtx->pOut);
91077 pCtx->isError = SQLITE_NOMEM_BKPT;
91078 sqlite3OomFault(pCtx->pOut->db);
91084 ** test-control.
91087 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91088 if( pCtx->pOut->flags & MEM_Int ){
91089 pCtx->pOut->flags &= ~MEM_Int;
91090 pCtx->pOut->flags |= MEM_IntReal;
91104 for(i=0; i<db->nDb; i++){
91105 Btree *pBt = db->aDb[i].pBt;
91111 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
91112 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
91135 db = p->db;
91136 if( p->eVdbeState!=VDBE_RUN_STATE ){
91138 if( p->eVdbeState==VDBE_READY_STATE ){
91139 if( p->expired ){
91140 p->rc = SQLITE_SCHEMA;
91142 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
91144 ** error has occurred, then return the error code in p->rc to the
91157 if( db->nVdbeActive==0 ){
91158 AtomicStore(&db->u1.isInterrupted, 0);
91161 assert( db->nVdbeWrite>0 || db->autoCommit==0
91162 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
91166 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
91167 && !db->init.busy && p->zSql ){
91168 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
91170 assert( p->startTime==0 );
91174 db->nVdbeActive++;
91175 if( p->readOnly==0 ) db->nVdbeWrite++;
91176 if( p->bIsReader ) db->nVdbeRead++;
91177 p->pc = 0;
91178 p->eVdbeState = VDBE_RUN_STATE;
91181 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){
91186 ** This "automatic-reset" change is not technically an incompatibility,
91192 ** returns, and those were broken by the automatic-reset change. As a
91193 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
91199 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
91207 assert( p->eVdbeState==VDBE_READY_STATE );
91213 p->rcApp = SQLITE_OK;
91216 if( p->explain ){
91221 db->nVdbeExec++;
91223 db->nVdbeExec--;
91227 assert( p->rc==SQLITE_OK );
91228 assert( db->mallocFailed==0 );
91229 db->errCode = SQLITE_ROW;
91236 p->pResultRow = 0;
91237 if( rc==SQLITE_DONE && db->autoCommit ){
91238 assert( p->rc==SQLITE_OK );
91239 p->rc = doWalCallbacks(db);
91240 if( p->rc!=SQLITE_OK ){
91243 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
91245 ** error has occurred, then return the error code in p->rc to the
91252 db->errCode = rc;
91253 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
91254 p->rc = SQLITE_NOMEM_BKPT;
91255 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;
91260 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
91264 return (rc&db->errMask);
91268 ** This is the top-level implementation of sqlite3_step(). Call
91281 db = v->db;
91282 sqlite3_mutex_enter(db->mutex);
91285 int savedPc = v->pc;
91296 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
91297 sqlite3DbFree(db, v->zErrMsg);
91298 if( !db->mallocFailed ){
91299 v->zErrMsg = sqlite3DbStrDup(db, zErr);
91300 v->rc = rc = sqlite3ApiExit(db, rc);
91302 v->zErrMsg = 0;
91303 v->rc = rc = SQLITE_NOMEM_BKPT;
91312 ** SQLITE_SCHEMA. tag-20220401a */
91313 v->minWriteFileFormat = 254;
91315 assert( v->expired==0 );
91317 sqlite3_mutex_leave(db->mutex);
91330 assert( p && p->pFunc );
91331 return p->pFunc->pUserData;
91338 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
91348 assert( p && p->pOut );
91350 return p->pOut->db;
91364 ** performance by substituting a NULL result, or some other light-weight
91373 return sqlite3_value_nochange(p->pOut);
91401 if( (pVal->flags & MEM_Dyn)==0 || pVal->xDel!=sqlite3VdbeValueListFree ){
91404 assert( (pVal->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
91406 assert( pVal->eSubtype=='p' );
91407 assert( pVal->u.zPType!=0 && strcmp(pVal->u.zPType,"ValueList")==0 );
91408 pRhs = (ValueList*)pVal->z;
91411 rc = sqlite3BtreeNext(pRhs->pCsr, 0);
91414 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy);
91415 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) );
91416 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE;
91422 sz = sqlite3BtreePayloadSize(pRhs->pCsr);
91423 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
91427 sqlite3_value *pOut = pRhs->pOut;
91430 pOut->enc = ENC(pOut->db);
91431 if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){
91468 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
91469 assert( p->pVdbe!=0 );
91472 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
91475 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
91483 ** its pMem->z element.
91486 Mem *pMem = p->pMem;
91487 assert( (pMem->flags & MEM_Agg)==0 );
91490 pMem->z = 0;
91493 pMem->flags = MEM_Agg;
91494 pMem->u.pDef = p->pFunc;
91495 if( pMem->z ){
91496 memset(pMem->z, 0, nByte);
91499 return (void*)pMem->z;
91508 assert( p && p->pFunc && p->pFunc->xFinalize );
91509 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
91511 if( (p->pMem->flags & MEM_Agg)==0 ){
91514 return (void*)p->pMem->z;
91520 ** the user-function defined by pCtx.
91522 ** The left-most argument is 0.
91534 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91536 if( pCtx->pVdbe==0 ) return 0;
91538 assert( pCtx->pVdbe!=0 );
91540 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
91541 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
91542 return pAuxData->pAux;
91550 ** argument to the user-function defined by pCtx. Any previous value is
91553 ** The left-most argument is 0.
91571 pVdbe= pCtx->pVdbe;
91572 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91579 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
91580 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
91585 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
91587 pAuxData->iAuxOp = pCtx->iOp;
91588 pAuxData->iAuxArg = iArg;
91589 pAuxData->pNextAux = pVdbe->pAuxData;
91590 pVdbe->pAuxData = pAuxData;
91591 if( pCtx->isError==0 ) pCtx->isError = -1;
91592 }else if( pAuxData->xDeleteAux ){
91593 pAuxData->xDeleteAux(pAuxData->pAux);
91596 pAuxData->pAux = pAux;
91597 pAuxData->xDeleteAux = xDelete;
91617 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
91618 return p->pMem->n;
91628 return pVm->nResColumn;
91637 if( pVm==0 || pVm->pResultRow==0 ) return 0;
91638 return pVm->nResColumn;
91647 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
91648 ** instead of an 8-byte one. This all works fine, except that when
91650 ** that a Mem structure is located on an 8-byte boundary. To prevent
91652 ** using gcc, we force nullMem to be 8-byte aligned using the magical
91690 assert( pVm->db );
91691 sqlite3_mutex_enter(pVm->db->mutex);
91692 if( pVm->pResultRow!=0 && i<pVm->nResColumn && i>=0 ){
91693 pOut = &pVm->pResultRow[i];
91695 sqlite3Error(pVm->db, SQLITE_RANGE);
91728 assert( p->db!=0 );
91729 assert( sqlite3_mutex_held(p->db->mutex) );
91730 p->rc = sqlite3ApiExit(p->db, p->rc);
91731 sqlite3_mutex_leave(p->db->mutex);
91781 if( pOut->flags&MEM_Static ){
91782 pOut->flags &= ~MEM_Static;
91783 pOut->flags |= MEM_Ephem;
91828 ** Convert the N-th element of pStmt->pColName[] into a string using
91862 db = p->db;
91864 sqlite3_mutex_enter(db->mutex);
91866 if( p->explain ){
91868 n = p->explain==1 ? 8 : 4;
91871 int i = iExplainColNames16[N + 8*p->explain - 8];
91874 ret = (void*)azExplainColNames8[N + 8*p->explain - 8];
91878 n = p->nResColumn;
91880 u8 prior_mallocFailed = db->mallocFailed;
91884 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
91888 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
91893 assert( db->mallocFailed==0 || db->mallocFailed==1 );
91894 if( db->mallocFailed > prior_mallocFailed ){
91900 sqlite3_mutex_leave(db->mutex);
91998 ** The error code stored in database p->db is overwritten with the return
92006 sqlite3_mutex_enter(p->db->mutex);
92007 if( p->eVdbeState!=VDBE_READY_STATE ){
92008 sqlite3Error(p->db, SQLITE_MISUSE_BKPT);
92009 sqlite3_mutex_leave(p->db->mutex);
92011 "bind on a busy prepared statement: [%s]", p->zSql);
92014 if( i>=(unsigned int)p->nVar ){
92015 sqlite3Error(p->db, SQLITE_RANGE);
92016 sqlite3_mutex_leave(p->db->mutex);
92019 pVar = &p->aVar[i];
92021 pVar->flags = MEM_Null;
92022 p->db->errCode = SQLITE_OK;
92027 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
92033 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
92034 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
92035 p->expired = 1;
92055 rc = vdbeUnbind(p, (u32)(i-1));
92058 pVar = &p->aVar[i-1];
92061 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
92064 sqlite3Error(p->db, rc);
92065 rc = sqlite3ApiExit(p->db, rc);
92068 sqlite3_mutex_leave(p->db->mutex);
92104 rc = vdbeUnbind(p, (u32)(i-1));
92106 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
92107 sqlite3_mutex_leave(p->db->mutex);
92117 rc = vdbeUnbind(p, (u32)(i-1));
92119 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
92120 sqlite3_mutex_leave(p->db->mutex);
92127 rc = vdbeUnbind(p, (u32)(i-1));
92129 sqlite3_mutex_leave(p->db->mutex);
92142 rc = vdbeUnbind(p, (u32)(i-1));
92144 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
92145 sqlite3_mutex_leave(p->db->mutex);
92190 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
92194 assert( pValue->flags & (MEM_Real|MEM_IntReal) );
92196 (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i
92201 if( pValue->flags & MEM_Zero ){
92202 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
92204 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
92209 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
92210 pValue->enc);
92223 rc = vdbeUnbind(p, (u32)(i-1));
92226 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
92228 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
92230 sqlite3_mutex_leave(p->db->mutex);
92240 sqlite3_mutex_enter(p->db->mutex);
92241 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
92247 rc = sqlite3ApiExit(p->db, rc);
92248 sqlite3_mutex_leave(p->db->mutex);
92258 return p ? p->nVar : 0;
92265 ** The result is always UTF-8.
92270 return sqlite3VListNumToName(p->pVList, i);
92280 return sqlite3VListNameToNum(p->pVList, zName, nName);
92293 assert( pTo->db==pFrom->db );
92294 assert( pTo->nVar==pFrom->nVar );
92295 sqlite3_mutex_enter(pTo->db->mutex);
92296 for(i=0; i<pFrom->nVar; i++){
92297 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
92299 sqlite3_mutex_leave(pTo->db->mutex);
92319 if( pFrom->nVar!=pTo->nVar ){
92322 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
92323 if( pTo->expmask ){
92324 pTo->expired = 1;
92326 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
92327 if( pFrom->expmask ){
92328 pFrom->expired = 1;
92341 return pStmt ? ((Vdbe*)pStmt)->db : 0;
92349 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
92357 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
92369 sqlite3_mutex_enter(v->db->mutex);
92370 if( ((int)v->explain)==eMode ){
92374 }else if( (v->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
92376 }else if( v->eVdbeState!=VDBE_READY_STATE ){
92378 }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){
92380 v->explain = eMode;
92383 v->explain = eMode;
92385 v->haveEqpOps = eMode==2;
92387 if( v->explain ){
92388 v->nResColumn = 12 - 4*v->explain;
92390 v->nResColumn = v->nResAlloc;
92392 sqlite3_mutex_leave(v->db->mutex);
92401 return v!=0 && v->eVdbeState==VDBE_RUN_STATE;
92418 sqlite3_mutex_enter(pDb->mutex);
92420 pNext = (sqlite3_stmt*)pDb->pVdbe;
92422 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext;
92424 sqlite3_mutex_leave(pDb->mutex);
92436 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
92443 sqlite3 *db = pVdbe->db;
92444 sqlite3_mutex_enter(db->mutex);
92446 db->pnBytesFreed = (int*)&v;
92447 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
92448 db->lookaside.pEnd = db->lookaside.pStart;
92450 db->pnBytesFreed = 0;
92451 db->lookaside.pEnd = db->lookaside.pTrueEnd;
92452 sqlite3_mutex_leave(db->mutex);
92454 v = pVdbe->aCounter[op];
92455 if( resetFlag ) pVdbe->aCounter[op] = 0;
92465 return p ? p->zSql : 0;
92485 sqlite3_mutex_enter(p->db->mutex);
92487 sqlite3_mutex_leave(p->db->mutex);
92500 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
92501 sqlite3_mutex_enter(p->db->mutex);
92502 p->zNormSql = sqlite3Normalize(p, p->zSql);
92503 sqlite3_mutex_leave(p->db->mutex);
92505 return p->zNormSql;
92524 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
92531 ** This function is called from within a pre-update callback to retrieve
92544 p = db->pPreUpdate;
92546 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
92547 if( !p || p->op==SQLITE_INSERT ){
92551 if( p->pPk ){
92552 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
92554 if( iIdx>=p->pCsr->nField || iIdx<0 ){
92560 if( p->pUnpacked==0 ){
92564 assert( p->pCsr->eCurType==CURTYPE_BTREE );
92565 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
92568 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
92570 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
92571 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
92577 p->aRecord = aRec;
92580 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
92581 if( iIdx==p->pTab->iPKey ){
92582 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
92583 }else if( iIdx>=p->pUnpacked->nField ){
92585 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
92586 if( pMem->flags & (MEM_Int|MEM_IntReal) ){
92587 testcase( pMem->flags & MEM_Int );
92588 testcase( pMem->flags & MEM_IntReal );
92601 ** This function is called from within a pre-update callback to retrieve
92607 p = db!=0 ? db->pPreUpdate : 0;
92609 p = db->pPreUpdate;
92611 return (p ? p->keyinfo.nKeyField : 0);
92617 ** This function is designed to be called from within a pre-update callback
92621 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
92622 ** top-level trigger etc.).
92630 p = db!=0 ? db->pPreUpdate : 0;
92632 p = db->pPreUpdate;
92634 return (p ? p->v->nFrame : 0);
92640 ** This function is designed to be called from within a pre-update callback
92646 p = db!=0 ? db->pPreUpdate : 0;
92648 p = db->pPreUpdate;
92650 return (p ? p->iBlobWrite : -1);
92656 ** This function is called from within a pre-update callback to retrieve
92669 p = db->pPreUpdate;
92670 if( !p || p->op==SQLITE_DELETE ){
92674 if( p->pPk && p->op!=SQLITE_UPDATE ){
92675 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
92677 if( iIdx>=p->pCsr->nField || iIdx<0 ){
92682 if( p->op==SQLITE_INSERT ){
92683 /* For an INSERT, memory cell p->iNewReg contains the serialized record
92685 UnpackedRecord *pUnpack = p->pNewUnpacked;
92687 Mem *pData = &p->v->aMem[p->iNewReg];
92690 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
92695 p->pNewUnpacked = pUnpack;
92697 pMem = &pUnpack->aMem[iIdx];
92698 if( iIdx==p->pTab->iPKey ){
92699 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
92700 }else if( iIdx>=pUnpack->nField ){
92704 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
92709 assert( p->op==SQLITE_UPDATE );
92710 if( !p->aNew ){
92711 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
92712 if( !p->aNew ){
92717 assert( iIdx>=0 && iIdx<p->pCsr->nField );
92718 pMem = &p->aNew[iIdx];
92719 if( pMem->flags==0 ){
92720 if( iIdx==p->pTab->iPKey ){
92721 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
92723 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
92760 aOp = p->aOp;
92761 nOp = p->nOp;
92762 if( p->pFrame ){
92764 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
92765 aOp = pFrame->aOp;
92766 nOp = pFrame->nOp;
92786 for(idx=0; idx<p->nScan; idx++){
92787 pScan = &p->aScan[idx];
92788 if( pScan->zName ){
92789 iScan--;
92794 if( idx>=p->nScan ) return 1;
92795 assert( pScan==0 || pScan==&p->aScan[idx] );
92796 pScan = &p->aScan[idx];
92800 if( pScan->addrLoop>0 ){
92801 *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec;
92803 *(sqlite3_int64*)pOut = -1;
92808 if( pScan->addrVisit>0 ){
92809 *(sqlite3_int64*)pOut = aOp[pScan->addrVisit].nExec;
92811 *(sqlite3_int64*)pOut = -1;
92817 LogEst x = pScan->nEst;
92826 *(const char**)pOut = pScan->zName;
92830 if( pScan->addrExplain ){
92831 *(const char**)pOut = aOp[ pScan->addrExplain ].p4.z;
92838 if( pScan->addrExplain ){
92839 *(int*)pOut = aOp[ pScan->addrExplain ].p1;
92841 *(int*)pOut = -1;
92846 if( pScan->addrExplain ){
92847 *(int*)pOut = aOp[ pScan->addrExplain ].p2;
92849 *(int*)pOut = -1;
92855 if( pScan->aAddrRange[0]==0 ){
92856 res = -1;
92859 for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){
92860 int iIns = pScan->aAddrRange[ii];
92861 int iEnd = pScan->aAddrRange[ii+1];
92872 if( pOp->p1!=iEnd ) continue;
92873 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE)==0 ){
92909 for(ii=0; p!=0 && ii<p->nOp; ii++){
92910 Op *pOp = &p->aOp[ii];
92911 pOp->nExec = 0;
92912 pOp->nCycle = 0;
92934 ** The Vdbe parse-tree explainer is also found here.
92942 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
92967 ** This function returns a pointer to a nul-terminated string in memory
92971 ** then the returned string holds a copy of zRawSql with "-- " prepended
92977 ** with large (multi-megabyte) strings and blobs.
92987 ** parameter index is known, locate the value in p->aVar[]. Then render
93006 db = p->db;
93007 sqlite3StrAccumInit(&out, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
93008 if( db->nVdbeExec>1 ){
93012 sqlite3_str_append(&out, "-- ", 3);
93013 assert( (zRawSql - zStart) > 0 );
93014 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
93016 }else if( p->nVar==0 ){
93045 assert( idx>0 && idx<=p->nVar );
93046 pVar = &p->aVar[idx-1];
93047 if( pVar->flags & MEM_Null ){
93049 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
93050 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
93051 }else if( pVar->flags & MEM_Real ){
93052 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
93053 }else if( pVar->flags & MEM_Str ){
93060 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
93068 nOut = pVar->n;
93072 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
93075 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
93077 if( nOut<pVar->n ){
93078 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
93084 }else if( pVar->flags & MEM_Zero ){
93085 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
93088 assert( pVar->flags & MEM_Blob );
93090 nOut = pVar->n;
93095 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
93099 if( nOut<pVar->n ){
93100 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
93189 ** use this information to make sure that the zero-blob functionality
93196 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
93197 sqlite3_max_blobsize = p->n;
93207 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
93209 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
93265 ** M: 2 - two-way branch (I=0: fall-thru 1: jump )
93266 ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
93267 ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
93269 ** In other words, if M is 2, then I is either 0 (for fall-through) or
93271 ** ordinary fall-through, I is 1 if the branch was taken, and I is 2
93282 ** always taken, the flags should be 0x05 since the fall-through and
93284 ** flags should be 0x06 since only the fall-through approach is allowed.
93287 ** interested in equal or not-equal. In other words, I==0 and I==2
93292 ** should be no-ops except for special builds used to measure test coverage.
93297 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
93301 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
93313 ** iSrcLine - the line number of the VdbeCoverage() macro, with
93315 ** I - Mask of bits 0x07 indicating which cases are are
93317 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
93322 ** M - Type of jump. Same as M argument above
93347 if( ((P)->flags&MEM_Ephem)!=0 \
93351 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
93378 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
93379 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
93381 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
93389 assert( iCur>=0 && iCur<p->nCursor );
93390 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
93391 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]);
93392 p->apCsr[iCur] = 0;
93397 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
93398 ** to hold cursors, it is faster to in-line the logic. */
93399 assert( pMem->flags==MEM_Undefined );
93400 assert( (pMem->flags & MEM_Dyn)==0 );
93401 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc );
93402 if( pMem->szMalloc<nByte ){
93403 if( pMem->szMalloc>0 ){
93404 sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
93406 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte);
93407 if( pMem->zMalloc==0 ){
93408 pMem->szMalloc = 0;
93411 pMem->szMalloc = nByte;
93414 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
93416 pCx->eCurType = eCurType;
93417 pCx->nField = nField;
93418 pCx->aOffset = &pCx->aType[nField];
93420 pCx->uc.pCursor = (BtCursor*)
93421 &pMem->z[ROUND8P(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
93422 sqlite3BtreeCursorZero(pCx->uc.pCursor);
93440 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
93460 u8 enc = pRec->enc;
93462 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
93463 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
93465 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
93466 pRec->flags |= MEM_Int;
93468 pRec->u.r = rValue;
93469 pRec->flags |= MEM_Real;
93472 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
93475 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
93476 pRec->flags &= ~MEM_Str;
93486 ** floating-point representation if an integer representation
93500 ** No-op. pRec is unchanged.
93510 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
93511 if( (pRec->flags & (MEM_Real|MEM_IntReal))==0 ){
93512 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
93523 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
93524 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
93525 testcase( pRec->flags & MEM_Int );
93526 testcase( pRec->flags & MEM_Real );
93527 testcase( pRec->flags & MEM_IntReal );
93531 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
93566 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
93572 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
93573 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
93575 pMem->u.i = 0;
93578 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
93580 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
93581 pMem->u.i = ix;
93586 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
93587 pMem->u.i = ix;
93597 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
93598 ** But it does set pMem->u.r and pMem->u.i appropriately.
93601 assert( (pMem->flags & MEM_Null)==0
93602 || pMem->db==0 || pMem->db->mallocFailed );
93603 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){
93604 testcase( pMem->flags & MEM_Int );
93605 testcase( pMem->flags & MEM_Real );
93606 testcase( pMem->flags & MEM_IntReal );
93607 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null);
93609 assert( pMem->flags & (MEM_Str|MEM_Blob) );
93610 testcase( pMem->flags & MEM_Str );
93611 testcase( pMem->flags & MEM_Blob );
93622 int f = pMem->flags;
93640 for(i=0; i<25 && i<pMem->n; i++){
93641 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
93644 for(i=0; i<25 && i<pMem->n; i++){
93645 char z = pMem->z[i];
93650 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
93667 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
93668 for(j=0; j<25 && j<pMem->n; j++){
93669 c = pMem->z[j];
93672 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
93674 sqlite3_str_appendf(pStr, "(0-term)");
93685 if( p->flags & MEM_Undefined ){
93687 }else if( p->flags & MEM_Null ){
93688 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
93689 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
93690 printf(" si:%lld", p->u.i);
93691 }else if( (p->flags & (MEM_IntReal))!=0 ){
93692 printf(" ir:%lld", p->u.i);
93693 }else if( p->flags & MEM_Int ){
93694 printf(" i:%lld", p->u.i);
93696 }else if( p->flags & MEM_Real ){
93697 printf(" r:%.17g", p->u.r);
93708 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
93713 if( p->pScopyFrom ){
93714 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
93733 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
93739 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
93748 ** the number of non-transaction savepoints currently in the
93758 for(p=db->pSavepoint; p; p=p->pNext) n++;
93759 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
93765 ** Return the register of pOp->p2 after first preparing it to be
93770 pOut->flags = MEM_Int;
93775 assert( pOp->p2>0 );
93776 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
93777 pOut = &p->aMem[pOp->p2];
93779 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
93782 pOut->flags = MEM_Int;
93788 ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
93789 ** with pOp->p3. Return the hash.
93795 assert( pOp->p4type==P4_INT32 );
93796 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
93798 if( p->flags & (MEM_Int|MEM_IntReal) ){
93799 h += p->u.i;
93800 }else if( p->flags & MEM_Real ){
93802 }else if( p->flags & (MEM_Str|MEM_Blob) ){
93806 h += 4093 + (p->flags & (MEM_Str|MEM_Blob));
93823 int t, /* The serial-type code for the column value */
93830 sqlite3 *db = pDest->db;
93831 int encoding = pDest->enc;
93833 assert( pC->eCurType==CURTYPE_BTREE );
93834 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) return SQLITE_TOOBIG;
93835 if( len > 4000 && pC->pKeyInfo==0 ){
93842 ** Only do this on table-btrees so that writes to index-btrees do not
93848 if( pC->colCache==0 ){
93849 pC->pCache = sqlite3DbMallocZero(db, sizeof(VdbeTxtBlbCache) );
93850 if( pC->pCache==0 ) return SQLITE_NOMEM;
93851 pC->colCache = 1;
93853 pCache = pC->pCache;
93854 if( pCache->pCValue==0
93855 || pCache->iCol!=iCol
93856 || pCache->cacheStatus!=cacheStatus
93857 || pCache->colCacheCtr!=colCacheCtr
93858 || pCache->iOffset!=sqlite3BtreeOffset(pC->uc.pCursor)
93860 if( pCache->pCValue ) sqlite3RCStrUnref(pCache->pCValue);
93861 pBuf = pCache->pCValue = sqlite3RCStrNew( len+3 );
93863 rc = sqlite3BtreePayload(pC->uc.pCursor, iOffset, len, pBuf);
93868 pCache->iCol = iCol;
93869 pCache->cacheStatus = cacheStatus;
93870 pCache->colCacheCtr = colCacheCtr;
93871 pCache->iOffset = sqlite3BtreeOffset(pC->uc.pCursor);
93873 pBuf = pCache->pCValue;
93880 pDest->flags |= MEM_Term;
93886 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, iOffset, len, pDest);
93888 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
93890 pDest->z[len] = 0;
93891 pDest->flags |= MEM_Term;
93894 pDest->flags &= ~MEM_Ephem;
93910 return azTypes[sqlite3_value_type(pMem)-1];
93920 Op *aOp = p->aOp; /* Copy of p->aOp */
93928 sqlite3 *db = p->db; /* The database */
93936 Mem *aMem = p->aMem; /* Copy of p->aMem */
93948 assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */
93949 if( DbMaskNonZero(p->lockMask) ){
93953 if( db->xProgress ){
93954 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
93955 assert( 0 < db->nProgressOps );
93956 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
93961 if( p->rc==SQLITE_NOMEM ){
93966 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
93967 testcase( p->rc!=SQLITE_OK );
93968 p->rc = SQLITE_OK;
93969 assert( p->bIsReader || p->readOnly!=0 );
93970 p->iCurrentTime = 0;
93971 assert( p->explain==0 );
93972 db->busyHandler.nBusy = 0;
93973 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
93977 if( p->pc==0
93978 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
93983 if( p->db->flags & SQLITE_VdbeListing ){
93985 for(i=0; i<p->nOp; i++){
93989 if( p->db->flags & SQLITE_VdbeEQP ){
93990 for(i=0; i<p->nOp; i++){
93998 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
94002 for(pOp=&aOp[p->pc]; 1; pOp++){
94007 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
94011 pOp->nExec++;
94012 pnCycle = &pOp->nCycle;
94013 if( sqlite3NProfileCnt==0 ) *pnCycle -= sqlite3Hwtime();
94016 pOp->nExec++;
94017 pnCycle = &pOp->nCycle;
94018 *pnCycle -= sqlite3Hwtime();
94025 if( db->flags & SQLITE_VdbeTrace ){
94026 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
94027 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
94037 sqlite3_interrupt_count--;
94047 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
94049 assert( pOp->p1>0 );
94050 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
94051 assert( memIsValid(&aMem[pOp->p1]) );
94052 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
94053 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
94056 assert( pOp->p2>0 );
94057 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
94058 assert( memIsValid(&aMem[pOp->p2]) );
94059 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
94060 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
94063 assert( pOp->p3>0 );
94064 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
94065 assert( memIsValid(&aMem[pOp->p3]) );
94066 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
94067 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
94070 assert( pOp->p2>0 );
94071 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
94072 memAboutToChange(p, &aMem[pOp->p2]);
94075 assert( pOp->p3>0 );
94076 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
94077 memAboutToChange(p, &aMem[pOp->p3]);
94085 switch( pOp->opcode ){
94092 ** the switch statement will break with convention and be flush-left. Another
94130 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
94140 if( pOp->p5 ){
94141 assert( pOp->p2 < (int)(pOp - aOp) );
94142 assert( pOp->p2 > 1 );
94143 pOp = &aOp[pOp->p2 - 2];
94150 pOp = &aOp[pOp->p2 - 1];
94161 ** faster according to "valgrind --tool=cachegrind" */
94163 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
94168 ** If the progress callback returns non-zero, exit the virtual machine with
94171 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
94172 assert( db->nProgressOps!=0 );
94173 nProgressLimit += db->nProgressOps;
94174 if( db->xProgress(db->pProgressArg) ){
94191 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
94192 pIn1 = &aMem[pOp->p1];
94195 pIn1->flags = MEM_Int;
94196 pIn1->u.i = (int)(pOp-aOp);
94197 REGISTER_TRACE(pOp->p1, pIn1);
94208 ** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
94215 ** P2 is not used by the byte-code engine. However, if P2 is positive
94220 ** value is a byte-code indentation hint. See tag-20220407a in
94224 pIn1 = &aMem[pOp->p1];
94225 if( pIn1->flags & MEM_Int ){
94226 if( pOp->p3 ){ VdbeBranchTaken(1, 2); }
94227 pOp = &aOp[pIn1->u.i];
94228 }else if( ALWAYS(pOp->p3) ){
94246 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
94247 assert( pOp->p2>=0 && pOp->p2<p->nOp );
94248 assert( pOp->p3>=0 && pOp->p3<p->nOp );
94249 pOut = &aMem[pOp->p1];
94251 pOut->u.i = pOp->p3 - 1;
94252 pOut->flags = MEM_Int;
94253 if( pOp->p2==0 ) break;
94258 assert( pOp->p2>0 ); /* There are never any jumps to instruction 0 */
94259 assert( pOp->p2<p->nOp ); /* Jumps must be in range */
94260 pOp = &aOp[pOp->p2 - 1];
94276 pIn1 = &aMem[pOp->p1];
94277 assert( pIn1->flags==MEM_Int );
94278 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
94279 pCaller = &aOp[pIn1->u.i];
94280 assert( pCaller->opcode==OP_Yield );
94281 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
94282 pIn1->u.i = (int)(pOp - p->aOp) - 1;
94283 pOp = &aOp[pCaller->p2 - 1];
94302 pIn1 = &aMem[pOp->p1];
94304 pIn1->flags = MEM_Int;
94305 pcDest = (int)pIn1->u.i;
94306 pIn1->u.i = (int)(pOp - aOp);
94307 REGISTER_TRACE(pOp->p1, pIn1);
94317 ** value in register P3 is not NULL, then this routine is a no-op.
94321 pIn3 = &aMem[pOp->p3];
94323 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
94325 if( (pIn3->flags & MEM_Null)==0 ) break;
94365 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
94371 assert( pOp->p1!=SQLITE_INTERNAL );
94373 if( p->pFrame && pOp->p1==SQLITE_OK ){
94374 /* Halt the sub-program. Return control to the parent frame. */
94375 pFrame = p->pFrame;
94376 p->pFrame = pFrame->pParent;
94377 p->nFrame--;
94378 sqlite3VdbeSetChanges(db, p->nChange);
94380 if( pOp->p2==OE_Ignore ){
94381 /* Instruction pcx is the OP_Program that invoked the sub-program
94383 ** instruction is set to OE_Ignore, then the sub-program is throwing
94386 pcx = p->aOp[pcx].p2-1;
94388 aOp = p->aOp;
94389 aMem = p->aMem;
94393 p->rc = pOp->p1;
94394 p->errorAction = (u8)pOp->p2;
94395 assert( pOp->p5<=4 );
94396 if( p->rc ){
94397 if( pOp->p5 ){
94400 testcase( pOp->p5==1 );
94401 testcase( pOp->p5==2 );
94402 testcase( pOp->p5==3 );
94403 testcase( pOp->p5==4 );
94404 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
94405 if( pOp->p4.z ){
94406 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
94409 sqlite3VdbeError(p, "%s", pOp->p4.z);
94411 pcx = (int)(pOp - aOp);
94412 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
94417 p->rc = SQLITE_BUSY;
94419 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
94420 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
94421 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
94429 ** The 32-bit integer value P1 is written into register P2.
94433 pOut->u.i = pOp->p1;
94440 ** P4 is a pointer to a 64-bit integer value.
94445 assert( pOp->p4.pI64!=0 );
94446 pOut->u.i = *pOp->p4.pI64;
94454 ** P4 is a pointer to a 64-bit floating point value.
94459 pOut->flags = MEM_Real;
94460 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
94461 pOut->u.r = *pOp->p4.pReal;
94469 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
94475 assert( pOp->p4.z!=0 );
94477 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
94481 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
94485 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
94487 pOut->szMalloc = 0;
94488 pOut->flags |= MEM_Static;
94489 if( pOp->p4type==P4_DYNAMIC ){
94490 sqlite3DbFree(db, pOp->p4.z);
94492 pOp->p4type = P4_DYNAMIC;
94493 pOp->p4.z = pOut->z;
94494 pOp->p1 = pOut->n;
94497 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
94500 pOp->opcode = OP_String;
94519 assert( pOp->p4.z!=0 );
94521 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
94522 pOut->z = pOp->p4.z;
94523 pOut->n = pOp->p1;
94524 pOut->enc = encoding;
94527 if( pOp->p3>0 ){
94528 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
94529 pIn3 = &aMem[pOp->p3];
94530 assert( pIn3->flags & MEM_Int );
94531 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
94540 ** Mark the beginning of a subroutine that can be entered in-line
94544 ** If the subroutine is entered in-line, then the OP_Return will simply
94550 ** a no-op that simply falls through to the next instruction (assuming that
94552 ** entered in-line, then the OP_Return will cause in-line execution to
94567 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
94576 cnt = pOp->p3-pOp->p2;
94577 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
94578 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
94579 pOut->n = 0;
94581 pOut->uTemp = 0;
94587 pOut->flags = nullFlag;
94588 pOut->n = 0;
94589 cnt--;
94603 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
94604 pOut = &aMem[pOp->p1];
94605 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
94614 ** a zero-filled blob that is P1 bytes long in P2.
94617 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
94619 if( pOp->p4.z==0 ){
94620 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1);
94623 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
94625 pOut->enc = encoding;
94638 assert( pOp->p1>0 && pOp->p1<=p->nVar );
94639 pVar = &p->aVar[pOp->p1 - 1];
94643 pOut = &aMem[pOp->p2];
94646 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
94647 pOut->flags |= MEM_Static|MEM_FromBind;
94655 ** Move the P3 values in register P1..P1+P3-1 over into
94656 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
94658 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
94666 n = pOp->p3;
94667 p1 = pOp->p1;
94668 p2 = pOp->p2;
94675 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
94676 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
94681 pIn1->pScopyFrom = 0;
94683 for(i=1; i<p->nMem; i++){
94694 }while( --n );
94714 n = pOp->p3;
94715 pIn1 = &aMem[pOp->p1];
94716 pOut = &aMem[pOp->p2];
94722 if( (pOut->flags & MEM_Subtype)!=0 && (pOp->p5 & 0x0002)!=0 ){
94723 pOut->flags &= ~MEM_Subtype;
94726 pOut->pScopyFrom = 0;
94728 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
94729 if( (n--)==0 ) break;
94750 pIn1 = &aMem[pOp->p1];
94751 pOut = &aMem[pOp->p2];
94755 pOut->pScopyFrom = pIn1;
94756 pOut->mScopyFlags = pIn1->flags;
94770 pIn1 = &aMem[pOp->p1];
94771 assert( (pIn1->flags & MEM_Int)!=0 );
94772 pOut = &aMem[pOp->p2];
94773 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
94781 ** constraint violations, this is a no-op.
94798 ** The registers P1 through P1+P2-1 contain a single row of
94801 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
94805 assert( p->nResColumn==pOp->p2 );
94806 assert( pOp->p1>0 || CORRUPT_DB );
94807 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
94809 p->cacheCtr = (p->cacheCtr + 2)|1;
94810 p->pResultRow = &aMem[pOp->p1];
94813 Mem *pMem = p->pResultRow;
94815 for(i=0; i<pOp->p2; i++){
94817 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
94827 if( db->mallocFailed ) goto no_mem;
94828 if( db->mTrace & SQLITE_TRACE_ROW ){
94829 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
94831 p->pc = (int)(pOp - aOp) + 1;
94854 pIn1 = &aMem[pOp->p1];
94855 pIn2 = &aMem[pOp->p2];
94856 pOut = &aMem[pOp->p3];
94859 flags1 = pIn1->flags;
94861 testcase( pIn2->flags & MEM_Null );
94862 if( (flags1 | pIn2->flags) & MEM_Null ){
94868 flags1 = pIn1->flags & ~MEM_Str;
94871 flags1 = pIn1->flags & ~MEM_Str;
94873 flags2 = pIn2->flags;
94876 flags2 = pIn2->flags & ~MEM_Str;
94879 flags2 = pIn2->flags & ~MEM_Str;
94881 nByte = pIn1->n + pIn2->n;
94882 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
94890 memcpy(pOut->z, pIn2->z, pIn2->n);
94891 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
94892 pIn2->flags = flags2;
94894 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
94895 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
94896 pIn1->flags = flags1;
94898 pOut->z[nByte]=0;
94899 pOut->z[nByte+1] = 0;
94900 pOut->flags |= MEM_Term;
94901 pOut->n = (int)nByte;
94902 pOut->enc = encoding;
94923 ** Synopsis: r[P3]=r[P2]-r[P1]
94957 pIn1 = &aMem[pOp->p1];
94958 type1 = pIn1->flags;
94959 pIn2 = &aMem[pOp->p2];
94960 type2 = pIn2->flags;
94961 pOut = &aMem[pOp->p3];
94964 iA = pIn1->u.i;
94965 iB = pIn2->u.i;
94966 switch( pOp->opcode ){
94972 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
94978 if( iA==-1 ) iA = 1;
94983 pOut->u.i = iB;
94994 switch( pOp->opcode ){
94996 case OP_Subtract: rB -= rA; break;
95008 if( iA==-1 ) iA = 1;
95014 pOut->u.i = rB;
95020 pOut->u.r = rB;
95035 ** be returned. This is used by the built-in min(), max() and nullif()
95044 ** publicly. Only built-in functions have access to this feature.
95047 assert( pOp->p4type==P4_COLLSEQ );
95048 if( pOp->p1 ){
95049 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
95057 ** Take the bit-wise AND of the values in register P1 and P2 and
95064 ** Take the bit-wise OR of the values in register P1 and P2 and
95093 pIn1 = &aMem[pOp->p1];
95094 pIn2 = &aMem[pOp->p2];
95095 pOut = &aMem[pOp->p3];
95096 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
95102 op = pOp->opcode;
95113 op = 2*OP_ShiftLeft + 1 - op;
95114 iB = iB>(-64) ? -iB : 64;
95118 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
95125 /* Sign-extend on a right shift of a negative number */
95126 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
95131 pOut->u.i = iA;
95145 pIn1 = &aMem[pOp->p1];
95148 *(u64*)&pIn1->u.i += (u64)pOp->p2;
95160 pIn1 = &aMem[pOp->p1];
95161 if( (pIn1->flags & MEM_Int)==0 ){
95163 if( (pIn1->flags & MEM_Int)==0 ){
95165 if( pOp->p2==0 ){
95189 pIn1 = &aMem[pOp->p1];
95190 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
95191 testcase( pIn1->flags & MEM_Int );
95192 testcase( pIn1->flags & MEM_IntReal );
95194 REGISTER_TRACE(pOp->p1, pIn1);
95217 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
95218 testcase( pOp->p2==SQLITE_AFF_TEXT );
95219 testcase( pOp->p2==SQLITE_AFF_BLOB );
95220 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
95221 testcase( pOp->p2==SQLITE_AFF_INTEGER );
95222 testcase( pOp->p2==SQLITE_AFF_REAL );
95223 pIn1 = &aMem[pOp->p1];
95227 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
95230 REGISTER_TRACE(pOp->p1, pIn1);
95241 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
95285 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
95335 u16 flags1; /* Copy of initial value of pIn1->flags */
95336 u16 flags3; /* Copy of initial value of pIn3->flags */
95338 pIn1 = &aMem[pOp->p1];
95339 pIn3 = &aMem[pOp->p3];
95340 flags1 = pIn1->flags;
95341 flags3 = pIn3->flags;
95344 if( pIn3->u.i > pIn1->u.i ){
95345 if( sqlite3aGTb[pOp->opcode] ){
95346 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
95351 }else if( pIn3->u.i < pIn1->u.i ){
95352 if( sqlite3aLTb[pOp->opcode] ){
95353 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
95356 iCompare = -1;
95359 if( sqlite3aEQb[pOp->opcode] ){
95360 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
95366 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
95371 if( pOp->p5 & SQLITE_NULLEQ ){
95377 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
95378 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
95384 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
95392 if( pOp->p5 & SQLITE_JUMPIFNULL ){
95400 /* Neither operand is NULL and we couldn't do the special high-speed
95401 ** integer comparison case. So do a general-case comparison. */
95402 affinity = pOp->p5 & SQLITE_AFF_MASK;
95407 assert( flags3==pIn3->flags || CORRUPT_DB );
95408 flags3 = pIn3->flags;
95416 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
95418 testcase( pIn1->flags & MEM_Int );
95419 testcase( pIn1->flags & MEM_Real );
95420 testcase( pIn1->flags & MEM_IntReal );
95422 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
95423 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
95427 pIn3->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
95429 testcase( pIn3->flags & MEM_Int );
95430 testcase( pIn3->flags & MEM_Real );
95431 testcase( pIn3->flags & MEM_IntReal );
95433 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
95434 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
95437 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
95438 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
95450 res2 = sqlite3aLTb[pOp->opcode];
95452 res2 = sqlite3aEQb[pOp->opcode];
95454 res2 = sqlite3aGTb[pOp->opcode];
95460 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
95461 pIn3->flags = flags3;
95462 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
95463 pIn1->flags = flags1;
95465 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
95487 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
95490 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
95515 assert( pOp->p4type==P4_INTARRAY );
95516 assert( pOp->p4.ai );
95523 ** Synopsis: r[P1@P3] <-> r[P2@P3]
95525 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
95526 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
95555 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
95559 assert( pOp[-1].opcode==OP_Permutation );
95560 assert( pOp[-1].p4type==P4_INTARRAY );
95561 aPermute = pOp[-1].p4.ai + 1;
95564 n = pOp->p3;
95565 pKeyInfo = pOp->p4.pKeyInfo;
95568 p1 = pOp->p1;
95569 p2 = pOp->p2;
95574 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
95575 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
95577 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
95578 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
95587 assert( i<pKeyInfo->nKeyField );
95588 pColl = pKeyInfo->aColl[i];
95589 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
95593 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
95596 iCompare = -iCompare;
95598 if( bRev ) iCompare = -iCompare;
95615 assert( pOp>aOp && pOp[-1].opcode==OP_Compare );
95618 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
95620 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
95622 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
95652 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
95653 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
95654 if( pOp->opcode==OP_And ){
95661 pOut = &aMem[pOp->p3];
95665 pOut->u.i = v1;
95692 assert( pOp->p4type==P4_INT32 );
95693 assert( pOp->p4.i==0 || pOp->p4.i==1 );
95694 assert( pOp->p3==0 || pOp->p3==1 );
95695 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
95696 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
95708 pIn1 = &aMem[pOp->p1];
95709 pOut = &aMem[pOp->p2];
95710 if( (pIn1->flags & MEM_Null)==0 ){
95722 ** ones-complement of the P1 value into register P2. If P1 holds
95726 pIn1 = &aMem[pOp->p1];
95727 pOut = &aMem[pOp->p2];
95729 if( (pIn1->flags & MEM_Null)==0 ){
95730 pOut->flags = MEM_Int;
95731 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
95739 ** encountered on each invocation of the byte-code program. Jump to P2
95742 ** Top-level programs determine first invocation by comparing the P1
95750 ** because the self-altering code trick does not work for recursive
95755 assert( p->aOp[0].opcode==OP_Init );
95756 if( p->pFrame ){
95757 iAddr = (int)(pOp - p->aOp);
95758 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
95762 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
95764 if( p->aOp[0].p1==pOp->p1 ){
95770 pOp->p1 = p->aOp[0].p1;
95777 ** is considered true if it is numeric and non-zero. If the value
95778 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
95782 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
95792 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
95796 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
95808 pIn1 = &aMem[pOp->p1];
95809 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
95810 if( (pIn1->flags & MEM_Null)!=0 ){
95829 ** If P1 is -1, then P3 is a register number and the datatype is taken
95852 assert( pOp->p1>=(-1) && pOp->p1<p->nCursor );
95853 assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) );
95854 if( pOp->p1>=0 ){
95855 pC = p->apCsr[pOp->p1];
95857 assert( pOp->p3>=0 );
95858 if( pOp->p3<pC->nHdrParsed ){
95859 serialType = pC->aType[pOp->p3];
95886 typeMask = 1 << (pOp->p4.i - 1);
95894 assert( memIsValid(&aMem[pOp->p3]) );
95895 typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1);
95902 VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2);
95903 if( typeMask & pOp->p5 ){
95917 if( (aMem[pOp->p1].flags & MEM_Null)!=0
95918 || (aMem[pOp->p3].flags & MEM_Null)!=0
95920 sqlite3VdbeMemSetNull(aMem + pOp->p2);
95922 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0);
95933 pIn1 = &aMem[pOp->p1];
95934 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
95935 if( (pIn1->flags & MEM_Null)==0 ){
95949 ** If P1 is not an open cursor, then this opcode is a no-op.
95953 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95954 pC = p->apCsr[pOp->p1];
95955 if( pC && pC->nullRow ){
95956 sqlite3VdbeMemSetNull(aMem + pOp->p3);
95976 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
95980 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95981 pC = p->apCsr[pOp->p1];
95982 pOut = &p->aMem[pOp->p3];
95983 if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){
95986 if( pC->deferredMoveto ){
95990 if( sqlite3BtreeEof(pC->uc.pCursor) ){
95993 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
96005 ** information about the format of the data.) Extract the P2-th column
96025 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
96026 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
96034 u64 offset64; /* 64-bit offset */
96038 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96039 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
96040 pC = p->apCsr[pOp->p1];
96041 p2 = (u32)pOp->p2;
96045 assert( p2<(u32)pC->nField
96046 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) );
96047 aOffset = pC->aOffset;
96048 assert( aOffset==pC->aType+pC->nField );
96049 assert( pC->eCurType!=CURTYPE_VTAB );
96050 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
96051 assert( pC->eCurType!=CURTYPE_SORTER );
96053 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
96054 if( pC->nullRow ){
96055 if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){
96056 /* For the special case of as pseudo-cursor, the seekResult field
96058 pReg = &aMem[pC->seekResult];
96059 assert( pReg->flags & MEM_Blob );
96061 pC->payloadSize = pC->szRow = pReg->n;
96062 pC->aRow = (u8*)pReg->z;
96064 pDest = &aMem[pOp->p3];
96070 pCrsr = pC->uc.pCursor;
96071 if( pC->deferredMoveto ){
96073 assert( !pC->isEphemeral );
96074 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){
96075 pC = pC->pAltCursor;
96076 p2 = iMap - 1;
96086 assert( pC->eCurType==CURTYPE_BTREE );
96089 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
96090 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
96091 assert( pC->szRow<=pC->payloadSize );
96092 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
96094 pC->cacheStatus = p->cacheCtr;
96095 if( (aOffset[0] = pC->aRow[0])<0x80 ){
96096 pC->iHdrOffset = 1;
96098 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset);
96100 pC->nHdrParsed = 0;
96102 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
96103 /* pC->aRow does not have to hold the entire row, but it does at least
96104 ** need to cover the header of the record. If pC->aRow does not contain
96107 pC->aRow = 0;
96108 pC->szRow = 0;
96116 ** 3-byte type for each of the maximum of 32768 columns plus three
96119 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
96124 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
96136 zData = pC->aRow;
96137 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
96141 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){
96148 ** parsed and valid information is in aOffset[] and pC->aType[].
96150 if( pC->nHdrParsed<=p2 ){
96152 ** to extract additional fields up through the p2+1-th field
96154 if( pC->iHdrOffset<aOffset[0] ){
96156 if( pC->aRow==0 ){
96158 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
96162 zData = pC->aRow;
96165 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
96167 i = pC->nHdrParsed;
96169 zHdr = zData + pC->iHdrOffset;
96173 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
96178 pC->aType[i] = t;
96189 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
96190 || (offset64 > pC->payloadSize)
96196 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
96201 pC->nHdrParsed = i;
96202 pC->iHdrOffset = (u32)(zHdr - zData);
96203 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
96212 if( pC->nHdrParsed<=p2 ){
96213 pDest = &aMem[pOp->p3];
96215 if( pOp->p4type==P4_MEM ){
96216 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
96223 t = pC->aType[p2];
96226 /* Extract the content for the p2+1-th column. Control can only
96227 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
96230 assert( p2<pC->nHdrParsed );
96232 pDest = &aMem[pOp->p3];
96238 assert( t==pC->aType[p2] );
96239 if( pC->szRow>=aOffset[p2+1] ){
96241 ** page - where the content is not on an overflow page */
96242 zData = pC->aRow + aOffset[p2];
96247 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
96251 pDest->n = len = (t-12)/2;
96252 pDest->enc = encoding;
96253 if( pDest->szMalloc < len+2 ){
96254 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
96255 pDest->flags = MEM_Null;
96258 pDest->z = pDest->zMalloc;
96260 memcpy(pDest->z, zData, len);
96261 pDest->z[len] = 0;
96262 pDest->z[len+1] = 0;
96263 pDest->flags = aFlag[t&1];
96267 pDest->enc = encoding;
96268 assert( pDest->db==db );
96270 if( ((p5 = (pOp->p5 & OPFLAG_BYTELENARG))!=0
96293 p->cacheCtr, colCacheCtr, pDest);
96304 REGISTER_TRACE(pOp->p3, pDest);
96309 pOp = &aOp[aOp[0].p3-1];
96329 ** is zero. When P3 is non-zero, no type checking occurs for
96336 ** <li> P2 should be the number of non-virtual columns in the
96348 assert( pOp->p4type==P4_TABLE );
96349 pTab = pOp->p4.pTab;
96350 assert( pTab->tabFlags & TF_Strict );
96351 assert( pTab->nNVCol==pOp->p2 );
96352 aCol = pTab->aCol;
96353 pIn1 = &aMem[pOp->p1];
96354 for(i=0; i<pTab->nCol; i++){
96357 if( pOp->p3 ){ pIn1++; continue; }
96359 assert( pIn1 < &aMem[pOp->p1+pOp->p2] );
96361 if( (pIn1->flags & MEM_Null)==0 ){
96364 if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error;
96369 if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error;
96373 if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error;
96377 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real );
96378 assert( (pIn1->flags & MEM_IntReal)==0 );
96379 if( pIn1->flags & MEM_Int ){
96382 ** so that we keep the high-resolution integer value but know that
96384 testcase( pIn1->u.i==140737488355328LL );
96385 testcase( pIn1->u.i==140737488355327LL );
96386 testcase( pIn1->u.i==-140737488355328LL );
96387 testcase( pIn1->u.i==-140737488355329LL );
96388 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
96389 pIn1->flags |= MEM_IntReal;
96390 pIn1->flags &= ~MEM_Int;
96392 pIn1->u.r = (double)pIn1->u.i;
96393 pIn1->flags |= MEM_Real;
96394 pIn1->flags &= ~MEM_Int;
96396 }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){
96407 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
96410 assert( pIn1 == &aMem[pOp->p1+pOp->p2] );
96415 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1],
96416 pTab->zName, aCol[i].zCnName);
96426 ** P4 is a string that is P2 characters long. The N-th character of the
96427 ** string indicates the column affinity that should be used for the N-th
96433 zAffinity = pOp->p4.z;
96435 assert( pOp->p2>0 );
96436 assert( zAffinity[pOp->p2]==0 );
96437 pIn1 = &aMem[pOp->p1];
96438 while( 1 /*exit-by-break*/ ){
96439 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
96442 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
96445 ** so that we keep the high-resolution integer value but know that
96447 testcase( pIn1->u.i==140737488355328LL );
96448 testcase( pIn1->u.i==140737488355327LL );
96449 testcase( pIn1->u.i==-140737488355328LL );
96450 testcase( pIn1->u.i==-140737488355329LL );
96451 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
96452 pIn1->flags |= MEM_IntReal;
96453 pIn1->flags &= ~MEM_Int;
96455 pIn1->u.r = (double)pIn1->u.i;
96456 pIn1->flags |= MEM_Real;
96457 pIn1->flags &= ~(MEM_Int|MEM_Str);
96460 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
96475 ** P4 may be a string that is P2 characters long. The N-th character of the
96476 ** string indicates the column affinity that should be used for the N-th
96485 ** compile-time option is enabled:
96488 ** of the right-most table that can be null-trimmed.
96492 ** accept no-change records with serial_type 10. This value is
96514 ** ------------------------------------------------------------------------
96515 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
96516 ** ------------------------------------------------------------------------
96523 ** hdr-size field is also a varint which is the offset from the beginning
96529 nField = pOp->p1;
96530 zAffinity = pOp->p4.z;
96531 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
96533 nField = pOp->p2;
96534 pLast = &pData0[nField-1];
96537 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
96538 pOut = &aMem[pOp->p3];
96548 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
96549 pRec->flags |= MEM_IntReal;
96550 pRec->flags &= ~(MEM_Int);
96552 REGISTER_TRACE((int)(pRec-aMem), pRec);
96562 ** have a non-NULL default value. Also, the record must be left with
96564 ** index of the right-most column with a non-NULL default value */
96565 if( pOp->p5 ){
96566 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
96567 pLast--;
96568 nField--;
96575 ** the Mem.uTemp field of each term should hold the serial-type that will
96579 ** --------------- ---------------
96581 ** 1 1-byte signed integer
96582 ** 2 2-byte signed integer
96583 ** 3 3-byte signed integer
96584 ** 4 4-byte signed integer
96585 ** 5 6-byte signed integer
96586 ** 6 8-byte signed integer
96602 if( pRec->flags & MEM_Null ){
96603 if( pRec->flags & MEM_Zero ){
96607 ** Give such values a special internal-use-only serial-type of 10
96611 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
96613 pRec->uTemp = 10;
96615 pRec->uTemp = 0;
96618 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
96620 i64 i = pRec->u.i;
96622 testcase( pRec->flags & MEM_Int );
96623 testcase( pRec->flags & MEM_IntReal );
96636 if( (i&1)==i && p->minWriteFileFormat>=4 ){
96637 pRec->uTemp = 8+(u32)uu;
96640 pRec->uTemp = 1;
96644 pRec->uTemp = 2;
96647 pRec->uTemp = 3;
96650 pRec->uTemp = 4;
96653 pRec->uTemp = 5;
96656 if( pRec->flags & MEM_IntReal ){
96658 ** as an integer, then we might as well make it an 8-byte floating
96660 pRec->u.r = (double)pRec->u.i;
96661 pRec->flags &= ~MEM_IntReal;
96662 pRec->flags |= MEM_Real;
96663 pRec->uTemp = 7;
96665 pRec->uTemp = 6;
96668 }else if( pRec->flags & MEM_Real ){
96671 pRec->uTemp = 7;
96673 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
96674 assert( pRec->n>=0 );
96675 len = (u32)pRec->n;
96676 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
96677 if( pRec->flags & MEM_Zero ){
96678 serial_type += pRec->u.nZero*2;
96681 len += pRec->u.nZero;
96683 nZero += pRec->u.nZero;
96688 pRec->uTemp = serial_type;
96691 pRec--;
96694 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
96712 ** the new record. The output register (pOp->p3) is not allowed to
96716 if( nByte+nZero<=pOut->szMalloc ){
96719 pOut->z = pOut->zMalloc;
96723 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
96730 pOut->n = (int)nByte;
96731 pOut->flags = MEM_Blob;
96733 pOut->u.nZero = nZero;
96734 pOut->flags |= MEM_Zero;
96737 zHdr = (u8 *)pOut->z;
96748 while( 1 /*exit-by-break*/ ){
96749 serial_type = pRec->uTemp;
96750 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
96752 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
96761 assert( sizeof(v)==sizeof(pRec->u.r) );
96762 memcpy(&v, &pRec->u.r, sizeof(v));
96765 v = pRec->u.i;
96788 if( serial_type>=14 && pRec->n>0 ){
96789 assert( pRec->z!=0 );
96790 memcpy(zPayload, pRec->z, pRec->n);
96791 zPayload += pRec->n;
96795 if( pRec->n ){
96796 assert( pRec->z!=0 );
96797 memcpy(zPayload, pRec->z, pRec->n);
96798 zPayload += pRec->n;
96804 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
96805 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
96807 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
96808 REGISTER_TRACE(pOp->p3, pOut);
96819 ** every btree page of the table. But if P3 is non-zero, an estimate
96826 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
96827 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
96829 if( pOp->p3 ){
96837 pOut->u.i = nEntry;
96858 p1 = pOp->p1;
96859 zName = pOp->p4.z;
96864 assert( db->pSavepoint==0 || db->autoCommit==0 );
96866 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
96868 assert( p->bIsReader );
96871 if( db->nVdbeWrite>0 ){
96875 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
96884 ** that the db->aVTrans[] array is empty. */
96885 assert( db->autoCommit==0 || db->nVTrans==0 );
96887 db->nStatement+db->nSavepoint);
96894 pNew->zName = (char *)&pNew[1];
96895 memcpy(pNew->zName, zName, nName+1);
96899 if( db->autoCommit ){
96900 db->autoCommit = 0;
96901 db->isTransactionSavepoint = 1;
96903 db->nSavepoint++;
96907 pNew->pNext = db->pSavepoint;
96908 db->pSavepoint = pNew;
96909 pNew->nDeferredCons = db->nDeferredCons;
96910 pNew->nDeferredImmCons = db->nDeferredImmCons;
96920 pSavepoint = db->pSavepoint;
96921 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
96922 pSavepoint = pSavepoint->pNext
96929 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
96933 sqlite3VdbeError(p, "cannot release savepoint - "
96942 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
96947 db->autoCommit = 1;
96949 p->pc = (int)(pOp - aOp);
96950 db->autoCommit = 0;
96951 p->rc = rc = SQLITE_BUSY;
96954 rc = p->rc;
96956 db->autoCommit = 0;
96958 db->isTransactionSavepoint = 0;
96962 iSavepoint = db->nSavepoint - iSavepoint - 1;
96964 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
96965 for(ii=0; ii<db->nDb; ii++){
96966 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
96975 for(ii=0; ii<db->nDb; ii++){
96976 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
96984 db->mDbFlags |= DBFLAG_SchemaChange;
96991 while( db->pSavepoint!=pSavepoint ){
96992 pTmp = db->pSavepoint;
96993 db->pSavepoint = pTmp->pNext;
96995 db->nSavepoint--;
97003 assert( pSavepoint==db->pSavepoint );
97004 db->pSavepoint = pSavepoint->pNext;
97007 db->nSavepoint--;
97011 db->nDeferredCons = pSavepoint->nDeferredCons;
97012 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
97022 if( p->eVdbeState==VDBE_HALT_STATE ){
97031 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
97042 desiredAutoCommit = pOp->p1;
97043 iRollback = pOp->p2;
97046 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
97047 assert( p->bIsReader );
97049 if( desiredAutoCommit!=db->autoCommit ){
97053 db->autoCommit = 1;
97054 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
97058 sqlite3VdbeError(p, "cannot commit transaction - "
97065 db->autoCommit = (u8)desiredAutoCommit;
97068 p->pc = (int)(pOp - aOp);
97069 db->autoCommit = (u8)(1-desiredAutoCommit);
97070 p->rc = rc = SQLITE_BUSY;
97074 if( p->rc==SQLITE_OK ){
97083 (iRollback)?"cannot rollback - no transaction is active":
97084 "cannot commit - no transaction is active"));
97096 ** If P2 is non-zero, then a write-transaction is started, or if a
97097 ** read-transaction is already active, it is upgraded to a write-transaction.
97098 ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
97106 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
97132 assert( p->bIsReader );
97133 assert( p->readOnly==0 || pOp->p2==0 );
97134 assert( pOp->p2>=0 && pOp->p2<=2 );
97135 assert( pOp->p1>=0 && pOp->p1<db->nDb );
97136 assert( DbMaskTest(p->btreeMask, pOp->p1) );
97138 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){
97139 if( db->flags & SQLITE_QueryOnly ){
97149 pDb = &db->aDb[pOp->p1];
97150 pBt = pDb->pBt;
97153 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
97158 p->pc = (int)(pOp - aOp);
97159 p->rc = rc;
97165 if( p->usesStmtJournal
97166 && pOp->p2
97167 && (db->autoCommit==0 || db->nVdbeRead>1)
97170 if( p->iStatement==0 ){
97171 assert( db->nStatement>=0 && db->nSavepoint>=0 );
97172 db->nStatement++;
97173 p->iStatement = db->nSavepoint + db->nStatement;
97176 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
97178 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
97184 p->nStmtDefCons = db->nDeferredCons;
97185 p->nStmtDefImmCons = db->nDeferredImmCons;
97188 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
97190 && pOp->p5
97191 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i)
97194 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
97198 sqlite3DbFree(db, p->zErrMsg);
97199 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
97200 /* If the schema-cookie from the database file matches the cookie
97201 ** stored with the in-memory representation of the schema, do
97204 ** If virtual-tables are in use, this is not just an optimization.
97205 ** Often, v-tables store their data in other SQLite tables, which
97206 ** are queried from within xNext() and other v-table methods using
97207 ** prepared queries. If such a query is out-of-date, we do not want to
97209 ** v-table would have to be ready for the sqlite3_vtab structure itself
97211 ** a v-table method.
97213 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
97214 sqlite3ResetOneSchema(db, pOp->p1);
97216 p->expired = 1;
97222 p->changeCntOn = 0;
97236 ** There must be a read-lock on the database (either a transaction
97245 assert( p->bIsReader );
97246 iDb = pOp->p1;
97247 iCookie = pOp->p3;
97248 assert( pOp->p3<SQLITE_N_BTREE_META );
97249 assert( iDb>=0 && iDb<db->nDb );
97250 assert( db->aDb[iDb].pBt!=0 );
97251 assert( DbMaskTest(p->btreeMask, iDb) );
97253 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
97255 pOut->u.i = iMeta;
97270 ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
97278 assert( pOp->p2<SQLITE_N_BTREE_META );
97279 assert( pOp->p1>=0 && pOp->p1<db->nDb );
97280 assert( DbMaskTest(p->btreeMask, pOp->p1) );
97281 assert( p->readOnly==0 );
97282 pDb = &db->aDb[pOp->p1];
97283 assert( pDb->pBt!=0 );
97284 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
97286 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
97287 if( pOp->p2==BTREE_SCHEMA_VERSION ){
97289 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5;
97290 db->mDbFlags |= DBFLAG_SchemaChange;
97291 sqlite3FkClearTriggerCache(db, pOp->p1);
97292 }else if( pOp->p2==BTREE_FILE_FORMAT ){
97294 pDb->pSchema->file_format = pOp->p3;
97296 if( pOp->p1==1 ){
97300 p->expired = 0;
97309 ** Open a read-only cursor for the database table whose root page is
97326 ** object, then table being opened must be an [index b-tree] where the
97328 ** sequence of that index b-tree. Otherwise, if P4 is an integer
97329 ** value, then the table being opened must be a [table b-tree] with a
97339 ** b-tree and if it is this opcode becomes a no-op. In other words,
97361 ** OPFLAG_P2ISREG bit is set in P5 - see below).
97365 ** object, then table being opened must be an [index b-tree] where the
97367 ** sequence of that index b-tree. Otherwise, if P4 is an integer
97368 ** value, then the table being opened must be a [table b-tree] with a
97400 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
97401 assert( pOp->p4type==P4_KEYINFO );
97402 pCur = p->apCsr[pOp->p1];
97403 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
97404 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
97405 assert( pCur->eCurType==CURTYPE_BTREE );
97406 sqlite3BtreeClearCursor(pCur->uc.pCursor);
97414 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
97415 assert( p->bIsReader );
97416 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
97417 || p->readOnly==0 );
97419 if( p->expired==1 ){
97426 p2 = (u32)pOp->p2;
97427 iDb = pOp->p3;
97428 assert( iDb>=0 && iDb<db->nDb );
97429 assert( DbMaskTest(p->btreeMask, iDb) );
97430 pDb = &db->aDb[iDb];
97431 pX = pDb->pBt;
97433 if( pOp->opcode==OP_OpenWrite ){
97435 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
97437 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
97438 p->minWriteFileFormat = pDb->pSchema->file_format;
97443 if( pOp->p5 & OPFLAG_P2ISREG ){
97445 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
97446 assert( pOp->opcode==OP_OpenWrite );
97449 assert( (pIn2->flags & MEM_Int)!=0 );
97451 p2 = (int)pIn2->u.i;
97458 if( pOp->p4type==P4_KEYINFO ){
97459 pKeyInfo = pOp->p4.pKeyInfo;
97460 assert( pKeyInfo->enc==ENC(db) );
97461 assert( pKeyInfo->db==db );
97462 nField = pKeyInfo->nAllField;
97463 }else if( pOp->p4type==P4_INT32 ){
97464 nField = pOp->p4.i;
97466 assert( pOp->p1>=0 );
97469 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE);
97471 pCur->iDb = iDb;
97472 pCur->nullRow = 1;
97473 pCur->isOrdered = 1;
97474 pCur->pgnoRoot = p2;
97476 pCur->wrFlag = wrFlag;
97478 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
97479 pCur->pKeyInfo = pKeyInfo;
97481 ** SQLite used to check if the root-page flags were sane at this point
97484 pCur->isTable = pOp->p4type!=P4_KEYINFO;
97489 testcase( pOp->p5 & OPFLAG_BULKCSR );
97490 testcase( pOp->p2 & OPFLAG_SEEKEQ );
97491 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
97492 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
97503 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
97509 pOrig = p->apCsr[pOp->p2];
97511 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */
97513 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE);
97515 pCx->nullRow = 1;
97516 pCx->isEphemeral = 1;
97517 pCx->pKeyInfo = pOrig->pKeyInfo;
97518 pCx->isTable = pOrig->isTable;
97519 pCx->pgnoRoot = pOrig->pgnoRoot;
97520 pCx->isOrdered = pOrig->isOrdered;
97521 pCx->ub.pBtx = pOrig->ub.pBtx;
97522 pCx->noReuse = 1;
97523 pOrig->noReuse = 1;
97524 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
97525 pCx->pKeyInfo, pCx->uc.pCursor);
97539 ** the main database is read-only. The ephemeral
97556 ** can be used as zero-length data for OP_Insert. This is an optimization
97578 assert( pOp->p1>=0 );
97579 assert( pOp->p2>=0 );
97580 if( pOp->p3>0 ){
97583 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
97584 assert( pOp->opcode==OP_OpenEphemeral );
97585 assert( aMem[pOp->p3].flags & MEM_Null );
97586 aMem[pOp->p3].n = 0;
97587 aMem[pOp->p3].z = "";
97589 pCx = p->apCsr[pOp->p1];
97590 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField) ){
97594 assert( pCx->isEphemeral );
97595 pCx->seqCount = 0;
97596 pCx->cacheStatus = CACHE_STALE;
97597 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0);
97599 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE);
97601 pCx->isEphemeral = 1;
97602 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx,
97603 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
97606 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0);
97611 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
97613 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
97614 assert( pOp->p4type==P4_KEYINFO );
97615 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot,
97616 BTREE_BLOBKEY | pOp->p5);
97618 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
97619 assert( pKeyInfo->db==db );
97620 assert( pKeyInfo->enc==ENC(db) );
97621 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
97622 pKeyInfo, pCx->uc.pCursor);
97624 pCx->isTable = 0;
97626 pCx->pgnoRoot = SCHEMA_ROOT;
97627 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR,
97628 0, pCx->uc.pCursor);
97629 pCx->isTable = 1;
97632 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
97634 sqlite3BtreeClose(pCx->ub.pBtx);
97639 pCx->nullRow = 1;
97647 ** tables using an external merge-sort algorithm.
97649 ** If argument P3 is non-zero, then it indicates that the sorter may
97656 assert( pOp->p1>=0 );
97657 assert( pOp->p2>=0 );
97658 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER);
97660 pCx->pKeyInfo = pOp->p4.pKeyInfo;
97661 assert( pCx->pKeyInfo->db==db );
97662 assert( pCx->pKeyInfo->enc==ENC(db) );
97663 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
97677 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97678 pC = p->apCsr[pOp->p1];
97680 if( (pC->seqCount++)==0 ){
97694 ** A pseudo-table created by this opcode is used to hold a single
97697 ** is the only cursor opcode that works with a pseudo-table.
97700 ** the pseudo-table. If P2 is 0 or negative then the pseudo-cursor
97706 assert( pOp->p1>=0 );
97707 assert( pOp->p3>=0 );
97708 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO);
97710 pCx->nullRow = 1;
97711 pCx->seekResult = pOp->p2;
97712 pCx->isTable = 1;
97713 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
97715 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
97717 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
97718 assert( pOp->p5==0 );
97725 ** currently open, this instruction is a no-op.
97728 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97729 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
97730 p->apCsr[pOp->p1] = 0;
97739 ** table or index for cursor P1 are used. P4 is a 64-bit integer
97742 ** by the cursor. The high-order bit is set if any column after
97747 pC = p->apCsr[pOp->p1];
97748 assert( pC->eCurType==CURTYPE_BTREE );
97749 pC->maskUsed = *(u64*)pOp->p4.pI64;
97757 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
97784 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
97802 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
97820 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
97856 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97857 assert( pOp->p2!=0 );
97858 pC = p->apCsr[pOp->p1];
97860 assert( pC->eCurType==CURTYPE_BTREE );
97864 assert( pC->isOrdered );
97865 assert( pC->uc.pCursor!=0 );
97866 oc = pOp->opcode;
97868 pC->nullRow = 0;
97870 pC->seekOp = pOp->opcode;
97873 pC->deferredMoveto = 0;
97874 pC->cacheStatus = CACHE_STALE;
97875 if( pC->isTable ){
97878 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
97884 pIn3 = &aMem[pOp->p3];
97885 flags3 = pIn3->flags;
97890 newType = pIn3->flags; /* Record the type after applying numeric affinity */
97891 pIn3->flags = flags3; /* But convert the type back to its original */
97902 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
97907 c = sqlite3IntFloatCompare(iKey, pIn3->u.r);
97913 ** (x > 4.9) -> (x >= 5)
97914 ** (x <= 4.9) -> (x < 5)
97917 assert( OP_SeekGE==(OP_SeekGT-1) );
97918 assert( OP_SeekLT==(OP_SeekLE-1) );
97920 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
97932 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res);
97933 pC->movetoTarget = iKey; /* Used by OP_Delete */
97943 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
97945 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
97947 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
97948 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
97955 nField = pOp->p4.i;
97956 assert( pOp->p4type==P4_INT32 );
97958 r.pKeyInfo = pC->pKeyInfo;
97963 ** r.default_rc = -1;
97968 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
97969 assert( oc!=OP_SeekGT || r.default_rc==-1 );
97970 assert( oc!=OP_SeekLE || r.default_rc==-1 );
97974 r.aMem = &aMem[pOp->p3];
97980 if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
97985 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
98000 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
98016 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
98029 res = sqlite3BtreeEof(pC->uc.pCursor);
98033 assert( pOp->p2>0 );
98046 ** Synopsis: Scan-ahead up to P1 rows
98058 ** This opcode helps to optimize IN operators on a multi-column index
98061 ** of the b-tree instead. A correct answer is obtained if this opcode
98062 ** is omitted or is a no-op.
98069 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
98081 ** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
98121 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
98124 assert( pOp->p2>=(int)(pOp-aOp)+2 );
98126 if( pOp->p5==0 ){
98128 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
98129 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
98130 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
98131 assert( aOp[pOp->p2-1].opcode==OP_IdxGT
98132 || aOp[pOp->p2-1].opcode==OP_IdxGE );
98133 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
98136 assert( pOp->p2==(int)(pOp-aOp)+2 );
98137 assert( aOp[pOp->p2-1].opcode==OP_SeekGE );
98141 assert( pOp->p1>0 );
98142 pC = p->apCsr[pOp[1].p1];
98144 assert( pC->eCurType==CURTYPE_BTREE );
98145 assert( !pC->isTable );
98146 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
98148 if( db->flags&SQLITE_VdbeTrace ){
98149 printf("... cursor not valid - fall through\n");
98154 nStep = pOp->p1;
98156 r.pKeyInfo = pC->pKeyInfo;
98173 if( res>0 && pOp->p5==0 ){
98177 if( db->flags&SQLITE_VdbeTrace ){
98178 printf("... %d steps and then skip\n", pOp->p1 - nStep);
98188 if( db->flags&SQLITE_VdbeTrace ){
98189 printf("... %d steps and then success\n", pOp->p1 - nStep);
98198 if( db->flags&SQLITE_VdbeTrace ){
98199 printf("... fall through after %d steps\n", pOp->p1);
98205 nStep--;
98206 pC->cacheStatus = CACHE_STALE;
98207 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
98232 ** early, thus saving work. This is part of the IN-early-out optimization.
98234 ** P1 must be a valid b-tree cursor.
98238 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98239 pC = p->apCsr[pOp->p1];
98241 assert( pOp->p3>=pOp->p2 );
98242 if( pC->seekHit<pOp->p2 ){
98244 if( db->flags&SQLITE_VdbeTrace ){
98245 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
98248 pC->seekHit = pOp->p2;
98249 }else if( pC->seekHit>pOp->p3 ){
98251 if( db->flags&SQLITE_VdbeTrace ){
98252 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
98255 pC->seekHit = pOp->p3;
98269 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98270 pCur = p->apCsr[pOp->p1];
98271 VdbeBranchTaken(pCur==0 || pCur->nullRow, 2);
98272 if( pCur==0 || pCur->nullRow ){
98333 ** This opcode is used in IN clause processing for a multi-column key.
98335 ** left-most element, and if there are no matches on the most recent
98354 ** record are not-NULL then a check is done to determine if any row in the
98370 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98371 pC = p->apCsr[pOp->p1];
98374 if( db->flags&SQLITE_VdbeTrace ){
98375 printf("seekHit is %d\n", pC->seekHit);
98378 if( pC->seekHit>=pOp->p4.i ) break;
98392 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
98395 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98396 assert( pOp->p4type==P4_INT32 );
98397 pC = p->apCsr[pOp->p1];
98400 pC->seekOp = pOp->opcode;
98402 r.aMem = &aMem[pOp->p3];
98403 assert( pC->eCurType==CURTYPE_BTREE );
98404 assert( pC->uc.pCursor!=0 );
98405 assert( pC->isTable==0 );
98406 r.nField = (u16)pOp->p4.i;
98409 r.pKeyInfo = pC->pKeyInfo;
98415 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
98418 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult);
98421 assert( r.aMem->flags & MEM_Blob );
98422 assert( pOp->opcode!=OP_NoConflict );
98426 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
98428 sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey);
98429 pIdxKey->default_rc = 0;
98430 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult);
98436 alreadyExists = (pC->seekResult==0);
98437 pC->nullRow = 1-alreadyExists;
98438 pC->deferredMoveto = 0;
98439 pC->cacheStatus = CACHE_STALE;
98440 if( pOp->opcode==OP_Found ){
98448 if( pOp->opcode==OP_NoConflict ){
98460 if( pOp->opcode==OP_IfNoHope ){
98461 pC->seekHit = pOp->p4.i;
98483 ** (with arbitrary multi-value keys).
98502 ** P3 register to contain a non-integer value, in which case the jump is
98506 ** (with arbitrary multi-value keys).
98520 pIn3 = &aMem[pOp->p3];
98521 testcase( pIn3->flags & MEM_Int );
98522 testcase( pIn3->flags & MEM_IntReal );
98523 testcase( pIn3->flags & MEM_Real );
98524 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
98525 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
98526 /* If pIn3->u.i does not contain an integer, compute iKey as the
98540 pIn3 = &aMem[pOp->p3];
98541 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
98542 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98543 iKey = pIn3->u.i;
98545 pC = p->apCsr[pOp->p1];
98548 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
98550 assert( pC->isTable );
98551 assert( pC->eCurType==CURTYPE_BTREE );
98552 pCrsr = pC->uc.pCursor;
98557 pC->movetoTarget = iKey; /* Used by OP_Delete */
98558 pC->nullRow = 0;
98559 pC->cacheStatus = CACHE_STALE;
98560 pC->deferredMoveto = 0;
98562 pC->seekResult = res;
98565 if( pOp->p2==0 ){
98584 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98585 assert( p->apCsr[pOp->p1]!=0 );
98586 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
98588 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
98621 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98622 pC = p->apCsr[pOp->p1];
98624 assert( pC->isTable );
98625 assert( pC->eCurType==CURTYPE_BTREE );
98626 assert( pC->uc.pCursor!=0 );
98629 ** thing) is obtained in a two-step algorithm.
98641 assert( pC->isTable );
98653 if( !pC->useRandomRowid ){
98654 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
98659 v = 1; /* IMP: R-61914-48074 */
98661 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
98662 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
98664 pC->useRandomRowid = 1;
98666 v++; /* IMP: R-29538-34987 */
98672 if( pOp->p3 ){
98674 assert( pOp->p3>0 );
98675 if( p->pFrame ){
98676 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
98678 assert( pOp->p3<=pFrame->nMem );
98679 pMem = &pFrame->aMem[pOp->p3];
98682 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
98683 pMem = &aMem[pOp->p3];
98688 REGISTER_TRACE(pOp->p3, pMem);
98690 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
98691 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
98692 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
98695 if( v<pMem->u.i+1 ){
98696 v = pMem->u.i + 1;
98698 pMem->u.i = v;
98701 if( pC->useRandomRowid ){
98702 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
98706 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
98712 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v,
98718 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
98721 assert( v>0 ); /* EV: R-40812-03570 */
98723 pC->deferredMoveto = 0;
98724 pC->cacheStatus = CACHE_STALE;
98726 pOut->u.i = v;
98755 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
98758 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
98759 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
98772 const char *zDb; /* database name - used by the update hook */
98773 Table *pTab; /* Table structure - used by update and pre-update hooks */
98776 pData = &aMem[pOp->p2];
98777 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98779 pC = p->apCsr[pOp->p1];
98781 assert( pC->eCurType==CURTYPE_BTREE );
98782 assert( pC->deferredMoveto==0 );
98783 assert( pC->uc.pCursor!=0 );
98784 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
98785 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
98786 REGISTER_TRACE(pOp->p2, pData);
98789 pKey = &aMem[pOp->p3];
98790 assert( pKey->flags & MEM_Int );
98792 REGISTER_TRACE(pOp->p3, pKey);
98793 x.nKey = pKey->u.i;
98795 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
98796 assert( pC->iDb>=0 );
98797 zDb = db->aDb[pC->iDb].zDbSName;
98798 pTab = pOp->p4.pTab;
98799 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
98806 /* Invoke the pre-update hook, if any */
98808 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
98809 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1);
98811 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
98812 /* Prevent post-update hook from running in cases when it should not */
98816 if( pOp->p5 & OPFLAG_ISNOOP ) break;
98819 assert( (pOp->p5 & OPFLAG_LASTROWID)==0 || (pOp->p5 & OPFLAG_NCHANGE)!=0 );
98820 if( pOp->p5 & OPFLAG_NCHANGE ){
98821 p->nChange++;
98822 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
98824 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
98825 x.pData = pData->z;
98826 x.nData = pData->n;
98827 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
98828 if( pData->flags & MEM_Zero ){
98829 x.nZero = pData->u.nZero;
98835 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
98836 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
98839 pC->deferredMoveto = 0;
98840 pC->cacheStatus = CACHE_STALE;
98843 /* Invoke the update-hook if required. */
98846 assert( db->xUpdateCallback!=0 );
98847 assert( pTab->aCol!=0 );
98848 db->xUpdateCallback(db->pUpdateArg,
98849 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
98850 zDb, pTab->zName, x.nKey);
98858 ** of table - intkey or index. This opcode is used as part of copying
98871 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
98872 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
98874 pDest = p->apCsr[pOp->p1];
98875 pSrc = p->apCsr[pOp->p2];
98876 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
98877 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
98889 ** the next Next instruction will be a no-op. As a result, in this case
98904 ** pre-update-hook for deletes is run, but the btree is otherwise unchanged.
98908 ** P1 must not be pseudo-table. It has to be a real table with
98912 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
98914 ** this case. Specifically, if one is configured, the pre-update hook is
98915 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
98928 opflags = pOp->p2;
98929 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98930 pC = p->apCsr[pOp->p1];
98932 assert( pC->eCurType==CURTYPE_BTREE );
98933 assert( pC->uc.pCursor!=0 );
98934 assert( pC->deferredMoveto==0 );
98938 if( pOp->p4type==P4_TABLE
98939 && HasRowid(pOp->p4.pTab)
98940 && pOp->p5==0
98941 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
98944 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
98946 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
98947 assert( CORRUPT_DB || pC->movetoTarget==iKey );
98951 /* If the update-hook or pre-update-hook will be invoked, set zDb to
98956 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
98957 assert( pC->iDb>=0 );
98958 assert( pOp->p4.pTab!=0 );
98959 zDb = db->aDb[pC->iDb].zDbSName;
98960 pTab = pOp->p4.pTab;
98961 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
98962 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
98970 /* Invoke the pre-update-hook if required. */
98971 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab );
98972 if( db->xPreUpdateCallback && pTab ){
98975 || (aMem[pOp->p3].flags & MEM_Int)
98979 zDb, pTab, pC->movetoTarget,
98980 pOp->p3, -1
98987 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
98992 if( p->pFrame==0 ){
98993 if( pC->isEphemeral==0
98994 && (pOp->p5 & OPFLAG_AUXDELETE)==0
98995 && (pC->wrFlag & OPFLAG_FORDELETE)==0
98999 if( pOp->p2 & OPFLAG_NCHANGE ){
99000 nExtraDelete--;
99005 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
99006 pC->cacheStatus = CACHE_STALE;
99008 pC->seekResult = 0;
99011 /* Invoke the update-hook if required. */
99013 p->nChange++;
99014 if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){
99015 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
99016 pC->movetoTarget);
99017 assert( pC->iDb>=0 );
99031 sqlite3VdbeSetChanges(db, p->nChange);
99032 p->nChange = 0;
99056 pC = p->apCsr[pOp->p1];
99058 assert( pOp->p4type==P4_INT32 );
99059 pIn3 = &aMem[pOp->p3];
99060 nKeyCol = pOp->p4.i;
99076 ** a register that is the source for a pseudo-table cursor created using
99077 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
99084 pOut = &aMem[pOp->p2];
99085 pC = p->apCsr[pOp->p1];
99088 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
99089 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99091 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
99108 ** of a real table, not a pseudo-table.
99112 ** register will be invalidated as soon as the cursor moves - including
99130 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99131 pC = p->apCsr[pOp->p1];
99133 assert( pC->eCurType==CURTYPE_BTREE );
99135 assert( pC->nullRow==0 );
99136 assert( pC->uc.pCursor!=0 );
99137 pCrsr = pC->uc.pCursor;
99147 assert( pC->deferredMoveto==0 );
99151 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
99157 if( !pOp->p3 ) Deephemeralize(pOut);
99159 REGISTER_TRACE(pOp->p2, pOut);
99180 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99181 pC = p->apCsr[pOp->p1];
99183 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
99184 if( pC->nullRow ){
99185 pOut->flags = MEM_Null;
99187 }else if( pC->deferredMoveto ){
99188 v = pC->movetoTarget;
99190 }else if( pC->eCurType==CURTYPE_VTAB ){
99191 assert( pC->uc.pVCur!=0 );
99192 pVtab = pC->uc.pVCur->pVtab;
99193 pModule = pVtab->pModule;
99194 assert( pModule->xRowid );
99195 rc = pModule->xRowid(pC->uc.pVCur, &v);
99200 assert( pC->eCurType==CURTYPE_BTREE );
99201 assert( pC->uc.pCursor!=0 );
99204 if( pC->nullRow ){
99205 pOut->flags = MEM_Null;
99208 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
99210 pOut->u.i = v;
99221 ** pseudo-cursor that always returns NULL for every column.
99226 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99227 pC = p->apCsr[pOp->p1];
99230 ** pseudo-cursor that always gives null rows. */
99231 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO);
99233 pC->seekResult = 0;
99234 pC->isTable = 1;
99235 pC->noReuse = 1;
99236 pC->uc.pCursor = sqlite3BtreeFakeValidCursor();
99238 pC->nullRow = 1;
99239 pC->cacheStatus = CACHE_STALE;
99240 if( pC->eCurType==CURTYPE_BTREE ){
99241 assert( pC->uc.pCursor!=0 );
99242 sqlite3BtreeClearCursor(pC->uc.pCursor);
99245 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
99278 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99279 pC = p->apCsr[pOp->p1];
99281 assert( pC->eCurType==CURTYPE_BTREE );
99282 pCrsr = pC->uc.pCursor;
99286 pC->seekOp = pOp->opcode;
99288 if( pOp->opcode==OP_SeekEnd ){
99289 assert( pOp->p2==0 );
99290 pC->seekResult = -1;
99296 pC->nullRow = (u8)res;
99297 pC->deferredMoveto = 0;
99298 pC->cacheStatus = CACHE_STALE;
99300 if( pOp->p2>0 ){
99310 ** with cursor P1 and let X be 10*log2(N) if N is positive or -1
99321 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99322 assert( pOp->p4type==P4_INT32 );
99323 assert( pOp->p3>=-1 && pOp->p3<=640*2 );
99324 assert( pOp->p4.i>=-1 && pOp->p4.i<=640*2 );
99325 pC = p->apCsr[pOp->p1];
99327 pCrsr = pC->uc.pCursor;
99332 sz = -1; /* -Infinity encoding */
99338 res = sz>=pOp->p3 && sz<=pOp->p4.i;
99370 sqlite3_search_count--;
99372 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
99396 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99397 assert( pOp->p5==0 );
99398 assert( pOp->p2>=0 && pOp->p2<p->nOp );
99400 pC = p->apCsr[pOp->p1];
99402 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
99405 pC->seekOp = OP_Rewind;
99410 assert( pC->eCurType==CURTYPE_BTREE );
99411 pCrsr = pC->uc.pCursor;
99414 pC->deferredMoveto = 0;
99415 pC->cacheStatus = CACHE_STALE;
99418 pC->nullRow = (u8)res;
99419 if( pOp->p2>0 ){
99437 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
99446 ** number P5-1 in the prepared statement is incremented.
99462 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
99471 ** number P5-1 in the prepared statement is incremented.
99483 pC = p->apCsr[pOp->p1];
99489 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99490 assert( pOp->p5==0
99491 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
99492 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
99493 pC = p->apCsr[pOp->p1];
99495 assert( pC->deferredMoveto==0 );
99496 assert( pC->eCurType==CURTYPE_BTREE );
99497 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
99498 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
99499 || pC->seekOp==OP_NullRow);
99500 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
99504 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99505 assert( pOp->p5==0
99506 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
99507 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
99508 pC = p->apCsr[pOp->p1];
99510 assert( pC->deferredMoveto==0 );
99511 assert( pC->eCurType==CURTYPE_BTREE );
99512 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
99513 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
99514 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
99515 || pC->seekOp==OP_IfNoHope);
99516 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3);
99519 pC->cacheStatus = CACHE_STALE;
99522 pC->nullRow = 0;
99523 p->aCounter[pOp->p5]++;
99531 pC->nullRow = 1;
99547 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
99567 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99568 pC = p->apCsr[pOp->p1];
99572 pIn2 = &aMem[pOp->p2];
99573 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
99574 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
99575 assert( pC->eCurType==CURTYPE_BTREE );
99576 assert( pC->isTable==0 );
99579 x.nKey = pIn2->n;
99580 x.pKey = pIn2->z;
99581 x.aMem = aMem + pOp->p3;
99582 x.nMem = (u16)pOp->p4.i;
99583 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
99584 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
99585 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
99587 assert( pC->deferredMoveto==0 );
99588 pC->cacheStatus = CACHE_STALE;
99603 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99604 pC = p->apCsr[pOp->p1];
99608 pIn2 = &aMem[pOp->p2];
99609 assert( pIn2->flags & MEM_Blob );
99610 assert( pC->isTable==0 );
99631 ** this (self-correcting and non-critical) error if in writable_schema mode.
99639 assert( pOp->p3>0 );
99640 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
99641 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99642 pC = p->apCsr[pOp->p1];
99644 assert( pC->eCurType==CURTYPE_BTREE );
99646 pCrsr = pC->uc.pCursor;
99648 r.pKeyInfo = pC->pKeyInfo;
99649 r.nField = (u16)pOp->p3;
99651 r.aMem = &aMem[pOp->p2];
99657 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
99661 assert( pC->deferredMoveto==0 );
99662 pC->cacheStatus = CACHE_STALE;
99663 pC->seekResult = 0;
99680 ** is non-zero, then reading column a(i)-1 from cursor P3 is
99701 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99702 pC = p->apCsr[pOp->p1];
99704 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) );
99705 assert( pC->uc.pCursor!=0 );
99706 assert( pC->isTable==0 || IsNullCursor(pC) );
99707 assert( pC->deferredMoveto==0 );
99708 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
99719 if( !pC->nullRow ){
99721 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
99725 if( pOp->opcode==OP_DeferredSeek ){
99726 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
99727 pTabCur = p->apCsr[pOp->p3];
99729 assert( pTabCur->eCurType==CURTYPE_BTREE );
99730 assert( pTabCur->uc.pCursor!=0 );
99731 assert( pTabCur->isTable );
99732 pTabCur->nullRow = 0;
99733 pTabCur->movetoTarget = rowid;
99734 pTabCur->deferredMoveto = 1;
99735 pTabCur->cacheStatus = CACHE_STALE;
99736 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
99737 assert( !pTabCur->isEphemeral );
99738 pTabCur->ub.aAltMap = pOp->p4.ai;
99739 assert( !pC->isEphemeral );
99740 pTabCur->pAltCursor = pC;
99743 pOut->u.i = rowid;
99746 assert( pOp->opcode==OP_IdxRowid );
99747 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
99756 ** already occurred, this instruction is a no-op.
99761 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99762 pC = p->apCsr[pOp->p1];
99763 if( pC->deferredMoveto ){
99822 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99823 pC = p->apCsr[pOp->p1];
99825 assert( pC->isOrdered );
99826 assert( pC->eCurType==CURTYPE_BTREE );
99827 assert( pC->uc.pCursor!=0);
99828 assert( pC->deferredMoveto==0 );
99829 assert( pOp->p4type==P4_INT32 );
99830 r.pKeyInfo = pC->pKeyInfo;
99831 r.nField = (u16)pOp->p4.i;
99832 if( pOp->opcode<OP_IdxLT ){
99833 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
99834 r.default_rc = -1;
99836 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
99839 r.aMem = &aMem[pOp->p3];
99845 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
99856 assert( pC->eCurType==CURTYPE_BTREE );
99857 pCur = pC->uc.pCursor;
99875 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
99876 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
99877 res = -res;
99879 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
99900 ** value of the root page that moved - its value before the move occurred -
99911 ** and non-autovacuum modes.
99920 assert( p->readOnly==0 );
99921 assert( pOp->p1>1 );
99923 pOut->flags = MEM_Null;
99924 if( db->nVdbeRead > db->nVDestroy+1 ){
99926 p->errorAction = OE_Abort;
99929 iDb = pOp->p3;
99930 assert( DbMaskTest(p->btreeMask, iDb) );
99932 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
99933 pOut->flags = MEM_Int;
99934 pOut->u.i = iMoved;
99938 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
99958 ** If the P3 value is non-zero, then the row change count is incremented
99970 assert( p->readOnly==0 );
99971 assert( DbMaskTest(p->btreeMask, pOp->p2) );
99972 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange);
99973 if( pOp->p3 ){
99974 p->nChange += nChange;
99975 if( pOp->p3>0 ){
99976 assert( memIsValid(&aMem[pOp->p3]) );
99977 memAboutToChange(p, &aMem[pOp->p3]);
99978 aMem[pOp->p3].u.i += nChange;
99996 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99997 pC = p->apCsr[pOp->p1];
100000 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
100002 assert( pC->eCurType==CURTYPE_BTREE );
100003 assert( pC->isEphemeral );
100004 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
100013 ** Allocate a new b-tree in the main database file if P1==0 or in the
100017 ** The root page number of the new b-tree is stored in register P2.
100026 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
100027 assert( pOp->p1>=0 && pOp->p1<db->nDb );
100028 assert( DbMaskTest(p->btreeMask, pOp->p1) );
100029 assert( p->readOnly==0 );
100030 pDb = &db->aDb[pOp->p1];
100031 assert( pDb->pBt!=0 );
100032 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
100034 pOut->u.i = pgno;
100047 ** 0x0002 Set db->nAnalysisLimit to P2 while the statements in
100060 db->nSqlExec++;
100063 xAuth = db->xAuth;
100065 mTrace = db->mTrace;
100066 savedAnalysisLimit = db->nAnalysisLimit;
100067 if( pOp->p1 & 0x0001 ){
100069 db->xAuth = 0;
100071 db->mTrace = 0;
100073 if( pOp->p1 & 0x0002 ){
100074 db->nAnalysisLimit = pOp->p2;
100076 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, &zErr);
100077 db->nSqlExec--;
100079 db->xAuth = xAuth;
100081 db->mTrace = mTrace;
100082 db->nAnalysisLimit = savedAnalysisLimit;
100099 ** then runs the new virtual machine. It is thus a re-entrant opcode.
100112 for(iDb=0; iDb<db->nDb; iDb++){
100113 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
100117 iDb = pOp->p1;
100118 assert( iDb>=0 && iDb<db->nDb );
100120 || db->mallocFailed
100121 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) );
100124 if( pOp->p4.z==0 ){
100125 sqlite3SchemaClear(db->aDb[iDb].pSchema);
100126 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
100127 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
100128 db->mDbFlags |= DBFLAG_SchemaChange;
100129 p->expired = 0;
100136 initData.pzErrMsg = &p->zErrMsg;
100138 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
100141 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
100145 assert( db->init.busy==0 );
100146 db->init.busy = 1;
100149 assert( !db->mallocFailed );
100153 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
100159 db->init.busy = 0;
100180 assert( pOp->p1>=0 && pOp->p1<db->nDb );
100181 rc = sqlite3AnalysisLoad(db, pOp->p1);
100189 ** Remove the internal (in-memory) data structures that describe
100197 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
100203 ** Remove the internal (in-memory) data structures that describe
100211 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
100217 ** Remove the internal (in-memory) data structures that describe
100225 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
100257 assert( p->bIsReader );
100258 assert( pOp->p4type==P4_INTARRAY );
100259 nRoot = pOp->p2;
100260 aRoot = pOp->p4.ai;
100264 assert( pOp->p1>0 && (pOp->p1+1)<=(p->nMem+1 - p->nCursor) );
100265 pnErr = &aMem[pOp->p1];
100266 assert( (pnErr->flags & MEM_Int)!=0 );
100267 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
100268 pIn1 = &aMem[pOp->p1+1];
100269 assert( pOp->p5<db->nDb );
100270 assert( DbMaskTest(p->btreeMask, pOp->p5) );
100271 rc = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1],
100272 &aMem[pOp->p3], nRoot, (int)pnErr->u.i+1, &nErr, &z);
100280 pnErr->u.i -= nErr-1;
100281 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
100298 pIn1 = &aMem[pOp->p1];
100299 pIn2 = &aMem[pOp->p2];
100300 assert( (pIn2->flags & MEM_Int)!=0 );
100301 if( (pIn1->flags & MEM_Blob)==0 ){
100305 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
100320 pIn1 = &aMem[pOp->p1];
100321 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
100322 if( (pIn1->flags & MEM_Blob)==0
100323 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
100332 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
100340 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
100349 ** must have P4==0, the final set must have P4==-1, and for all other sets
100354 ** (b) when P4==-1 there is no need to insert the value, as it will
100364 pIn1 = &aMem[pOp->p1];
100365 pIn3 = &aMem[pOp->p3];
100366 iSet = pOp->p4.i;
100367 assert( pIn3->flags&MEM_Int );
100372 if( (pIn1->flags & MEM_Blob)==0 ){
100376 assert( pOp->p4type==P4_INT32 );
100377 assert( iSet==-1 || iSet>=0 );
100379 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
100384 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
100397 ** cell in an array of values used as arguments to the sub-program. P2
100398 ** contains the address to jump to if the sub-program throws an IGNORE
100403 ** memory required by the sub-vdbe at runtime.
100407 ** If P5 is non-zero, then recursive program invocation is enabled.
100410 int nMem; /* Number of memory registers for sub-program */
100411 int nByte; /* Bytes of runtime space required for sub-program */
100416 SubProgram *pProgram; /* Sub-program to execute */
100419 pProgram = pOp->p4.pProgram;
100420 pRt = &aMem[pOp->p3];
100421 assert( pProgram->nOp>0 );
100424 ** disabled for backwards compatibility (p5 is set if this sub-program
100434 if( pOp->p5 ){
100435 t = pProgram->token;
100436 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
100440 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
100450 if( (pRt->flags&MEM_Blob)==0 ){
100456 nMem = pProgram->nMem + pProgram->nCsr;
100458 if( pProgram->nCsr==0 ) nMem++;
100461 + pProgram->nCsr * sizeof(VdbeCursor*)
100462 + (pProgram->nOp + 7)/8;
100468 pRt->flags = MEM_Blob|MEM_Dyn;
100469 pRt->z = (char*)pFrame;
100470 pRt->n = nByte;
100471 pRt->xDel = sqlite3VdbeFrameMemDel;
100473 pFrame->v = p;
100474 pFrame->nChildMem = nMem;
100475 pFrame->nChildCsr = pProgram->nCsr;
100476 pFrame->pc = (int)(pOp - aOp);
100477 pFrame->aMem = p->aMem;
100478 pFrame->nMem = p->nMem;
100479 pFrame->apCsr = p->apCsr;
100480 pFrame->nCursor = p->nCursor;
100481 pFrame->aOp = p->aOp;
100482 pFrame->nOp = p->nOp;
100483 pFrame->token = pProgram->token;
100485 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
100488 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
100490 pMem->flags = MEM_Undefined;
100491 pMem->db = db;
100494 pFrame = (VdbeFrame*)pRt->z;
100495 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
100496 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
100497 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
100498 assert( pProgram->nCsr==pFrame->nChildCsr );
100499 assert( (int)(pOp - aOp)==pFrame->pc );
100502 p->nFrame++;
100503 pFrame->pParent = p->pFrame;
100504 pFrame->lastRowid = db->lastRowid;
100505 pFrame->nChange = p->nChange;
100506 pFrame->nDbChange = p->db->nChange;
100507 assert( pFrame->pAuxData==0 );
100508 pFrame->pAuxData = p->pAuxData;
100509 p->pAuxData = 0;
100510 p->nChange = 0;
100511 p->pFrame = pFrame;
100512 p->aMem = aMem = VdbeFrameMem(pFrame);
100513 p->nMem = pFrame->nChildMem;
100514 p->nCursor = (u16)pFrame->nChildCsr;
100515 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
100516 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
100517 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
100518 p->aOp = aOp = pProgram->aOp;
100519 p->nOp = pProgram->nOp;
100525 for(i=0; i<p->nMem; i++){
100526 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
100531 pOp = &aOp[-1];
100537 ** This opcode is only ever present in sub-programs called via the
100551 pFrame = p->pFrame;
100552 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
100564 ** If P1 is non-zero, the database constraint counter is incremented
100569 if( db->flags & SQLITE_DeferFKs ){
100570 db->nDeferredImmCons += pOp->p2;
100571 }else if( pOp->p1 ){
100572 db->nDeferredCons += pOp->p2;
100574 p->nFkConstraint += pOp->p2;
100582 ** This opcode tests if a foreign key constraint-counter is currently zero.
100586 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
100588 ** zero, the jump is taken if the statement constraint-counter is zero
100592 if( pOp->p1 ){
100593 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
100594 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
100596 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
100597 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
100609 ** within a sub-program). Set the value of register P1 to the maximum of
100617 if( p->pFrame ){
100618 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
100619 pIn1 = &pFrame->aMem[pOp->p1];
100621 pIn1 = &aMem[pOp->p1];
100625 pIn2 = &aMem[pOp->p2];
100627 if( pIn1->u.i<pIn2->u.i){
100628 pIn1->u.i = pIn2->u.i;
100635 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
100645 pIn1 = &aMem[pOp->p1];
100646 assert( pIn1->flags&MEM_Int );
100647 VdbeBranchTaken( pIn1->u.i>0, 2);
100648 if( pIn1->u.i>0 ){
100649 pIn1->u.i -= pOp->p3;
100656 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
100669 ** and r[P2] is set to -1.
100675 pIn1 = &aMem[pOp->p1];
100676 pIn3 = &aMem[pOp->p3];
100678 assert( pIn1->flags & MEM_Int );
100679 assert( pIn3->flags & MEM_Int );
100680 x = pIn1->u.i;
100681 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
100689 pOut->u.i = -1;
100691 pOut->u.i = x;
100697 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
100701 ** If it is non-zero (negative or positive) and then also jump to P2.
100705 pIn1 = &aMem[pOp->p1];
100706 assert( pIn1->flags&MEM_Int );
100707 VdbeBranchTaken(pIn1->u.i<0, 2);
100708 if( pIn1->u.i ){
100709 if( pIn1->u.i>0 ) pIn1->u.i--;
100716 ** Synopsis: if (--r[P1])==0 goto P2
100722 pIn1 = &aMem[pOp->p1];
100723 assert( pIn1->flags&MEM_Int );
100724 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
100725 VdbeBranchTaken(pIn1->u.i==0, 2);
100726 if( pIn1->u.i==0 ) goto jump_to_p2;
100775 assert( pOp->p4type==P4_FUNCDEF );
100776 n = pOp->p5;
100777 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
100778 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
100779 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
100781 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
100783 pCtx->pMem = 0;
100784 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
100785 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
100786 pCtx->pFunc = pOp->p4.pFunc;
100787 pCtx->iOp = (int)(pOp - aOp);
100788 pCtx->pVdbe = p;
100789 pCtx->skipFlag = 0;
100790 pCtx->isError = 0;
100791 pCtx->enc = encoding;
100792 pCtx->argc = n;
100793 pOp->p4type = P4_FUNCCTX;
100794 pOp->p4.pCtx = pCtx;
100797 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
100799 pOp->opcode = OP_AggStep1;
100808 assert( pOp->p4type==P4_FUNCCTX );
100809 pCtx = pOp->p4.pCtx;
100810 pMem = &aMem[pOp->p3];
100813 if( pOp->p1 ){
100816 assert( pMem->uTemp==0x1122e0e3 );
100819 pMem->uTemp = 0x1122e0e3;
100827 if( pCtx->pMem != pMem ){
100828 pCtx->pMem = pMem;
100829 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
100833 for(i=0; i<pCtx->argc; i++){
100834 assert( memIsValid(pCtx->argv[i]) );
100835 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
100839 pMem->n++;
100840 assert( pCtx->pOut->flags==MEM_Null );
100841 assert( pCtx->isError==0 );
100842 assert( pCtx->skipFlag==0 );
100844 if( pOp->p1 ){
100845 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
100848 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
100850 if( pCtx->isError ){
100851 if( pCtx->isError>0 ){
100852 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
100853 rc = pCtx->isError;
100855 if( pCtx->skipFlag ){
100856 assert( pOp[-1].opcode==OP_CollSeq );
100857 i = pOp[-1].p1;
100859 pCtx->skipFlag = 0;
100861 sqlite3VdbeMemRelease(pCtx->pOut);
100862 pCtx->pOut->flags = MEM_Null;
100863 pCtx->isError = 0;
100866 assert( pCtx->pOut->flags==MEM_Null );
100867 assert( pCtx->skipFlag==0 );
100900 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
100901 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
100902 pMem = &aMem[pOp->p1];
100903 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
100905 if( pOp->p3 ){
100906 memAboutToChange(p, &aMem[pOp->p3]);
100907 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
100908 pMem = &aMem[pOp->p3];
100912 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
100921 REGISTER_TRACE((int)(pMem-aMem), pMem);
100928 ** Checkpoint database P1. This is a no-op if P1 is not currently in
100935 ** mem[P3+2] are initialized to -1.
100942 assert( p->readOnly==0 );
100944 aRes[1] = aRes[2] = -1;
100945 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
100946 || pOp->p2==SQLITE_CHECKPOINT_FULL
100947 || pOp->p2==SQLITE_CHECKPOINT_RESTART
100948 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
100950 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
100956 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
100973 ** Write a string containing the final journal-mode to register P2.
100985 eNew = pOp->p3;
100994 assert( pOp->p1>=0 && pOp->p1<db->nDb );
100995 assert( p->readOnly==0 );
100997 pBt = db->aDb[pOp->p1].pBt;
101012 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
101020 if( !db->autoCommit || db->nVdbeRead>1 ){
101031 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
101059 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
101060 pOut->z = (char *)sqlite3JournalModename(eNew);
101061 pOut->n = sqlite3Strlen30(pOut->z);
101062 pOut->enc = SQLITE_UTF8;
101080 assert( p->readOnly==0 );
101081 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
101082 pOp->p2 ? &aMem[pOp->p2] : 0);
101098 assert( pOp->p1>=0 && pOp->p1<db->nDb );
101099 assert( DbMaskTest(p->btreeMask, pOp->p1) );
101100 assert( p->readOnly==0 );
101101 pBt = db->aDb[pOp->p1].pBt;
101120 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
101130 assert( pOp->p2==0 || pOp->p2==1 );
101131 if( !pOp->p1 ){
101132 sqlite3ExpirePreparedStatements(db, pOp->p2);
101134 p->expired = pOp->p2+1;
101146 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
101147 pC = p->apCsr[pOp->p1];
101149 assert( pC->eCurType==CURTYPE_BTREE );
101150 sqlite3BtreeCursorPin(pC->uc.pCursor);
101161 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
101162 pC = p->apCsr[pOp->p1];
101164 assert( pC->eCurType==CURTYPE_BTREE );
101165 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
101174 ** the shared-cache feature is enabled.
101180 ** P2 contains the root-page of the table to lock.
101186 u8 isWriteLock = (u8)pOp->p3;
101187 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
101188 int p1 = pOp->p1;
101189 assert( p1>=0 && p1<db->nDb );
101190 assert( DbMaskTest(p->btreeMask, p1) );
101192 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
101195 const char *z = pOp->p4.z;
101217 pVTab = pOp->p4.pVtab;
101219 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
101239 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
101240 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
101241 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
101244 assert( zTab || db->mallocFailed );
101246 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
101261 db->nVDestroy++;
101262 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
101263 db->nVDestroy--;
101264 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
101283 assert( p->bIsReader );
101286 pVtab = pOp->p4.pVtab->pVtab;
101287 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
101291 pModule = pVtab->pModule;
101292 rc = pModule->xOpen(pVtab, &pVCur);
101297 pVCur->pVtab = pVtab;
101300 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB);
101302 pCur->uc.pVCur = pVCur;
101303 pVtab->nRef++;
101305 assert( db->mallocFailed );
101306 pModule->xClose(pVCur);
101329 pOut = &aMem[pOp->p2];
101331 assert( pOp->p4type==P4_TABLEREF );
101332 pTab = pOp->p4.pTab;
101334 assert( pTab->nTabRef>0 );
101336 if( pTab->u.vtab.p==0 ) break;
101337 pVtab = pTab->u.vtab.p->pVtab;
101339 pModule = pVtab->pModule;
101341 assert( pModule->iVersion>=4 );
101342 assert( pModule->xIntegrity!=0 );
101343 sqlite3VtabLock(pTab->u.vtab.p);
101344 assert( pOp->p1>=0 && pOp->p1<db->nDb );
101345 rc = pModule->xIntegrity(pVtab, db->aDb[pOp->p1].zDbSName, pTab->zName,
101346 pOp->p3, &zErr);
101347 sqlite3VtabUnlock(pTab->u.vtab.p);
101353 sqlite3VdbeMemSetStr(pOut, zErr, -1, SQLITE_UTF8, sqlite3_free);
101374 pC = p->apCsr[pOp->p1];
101377 pRhs->pCsr = pC->uc.pCursor;
101378 pRhs->pOut = &aMem[pOp->p3];
101380 pOut->flags = MEM_Null;
101420 pQuery = &aMem[pOp->p3];
101422 pCur = p->apCsr[pOp->p1];
101424 REGISTER_TRACE(pOp->p3, pQuery);
101426 assert( pCur->eCurType==CURTYPE_VTAB );
101427 pVCur = pCur->uc.pVCur;
101428 pVtab = pVCur->pVtab;
101429 pModule = pVtab->pModule;
101432 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
101433 nArg = (int)pArgc->u.i;
101434 iQuery = (int)pQuery->u.i;
101437 apArg = p->apArg;
101441 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
101444 res = pModule->xEof(pVCur);
101445 pCur->nullRow = 0;
101456 ** Store in register P3 the value of the P2-th column of
101457 ** the current row of the virtual-table of cursor P1.
101474 VdbeCursor *pCur = p->apCsr[pOp->p1];
101476 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
101477 pDest = &aMem[pOp->p3];
101479 if( pCur->nullRow ){
101483 assert( pCur->eCurType==CURTYPE_VTAB );
101484 pVtab = pCur->uc.pVCur->pVtab;
101485 pModule = pVtab->pModule;
101486 assert( pModule->xColumn );
101493 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
101494 if( pOp->p5 & OPFLAG_NOCHNG ){
101496 pDest->flags = MEM_Null|MEM_Zero;
101497 pDest->u.nZero = 0;
101501 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
101508 REGISTER_TRACE(pOp->p3, pDest);
101529 pCur = p->apCsr[pOp->p1];
101531 assert( pCur->eCurType==CURTYPE_VTAB );
101532 if( pCur->nullRow ){
101535 pVtab = pCur->uc.pVCur->pVtab;
101536 pModule = pVtab->pModule;
101537 assert( pModule->xNext );
101545 rc = pModule->xNext(pCur->uc.pVCur);
101548 res = pModule->xEof(pCur->uc.pVCur);
101570 isLegacy = (db->flags & SQLITE_LegacyAlter);
101571 db->flags |= SQLITE_LegacyAlter;
101572 pVtab = pOp->p4.pVtab->pVtab;
101573 pName = &aMem[pOp->p1];
101574 assert( pVtab->pModule->xRename );
101576 assert( p->readOnly==0 );
101577 REGISTER_TRACE(pOp->p1, pName);
101578 assert( pName->flags & MEM_Str );
101579 testcase( pName->enc==SQLITE_UTF8 );
101580 testcase( pName->enc==SQLITE_UTF16BE );
101581 testcase( pName->enc==SQLITE_UTF16LE );
101584 rc = pVtab->pModule->xRename(pVtab, pName->z);
101585 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
101587 p->expired = 0;
101600 ** invocation. The value in register (P3+P2-1) corresponds to the
101630 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
101631 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
101633 assert( p->readOnly==0 );
101634 if( db->mallocFailed ) goto no_mem;
101636 pVtab = pOp->p4.pVtab->pVtab;
101637 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
101641 pModule = pVtab->pModule;
101642 nArg = pOp->p2;
101643 assert( pOp->p4type==P4_VTAB );
101644 if( ALWAYS(pModule->xUpdate) ){
101645 u8 vtabOnConflict = db->vtabOnConflict;
101646 apArg = p->apArg;
101647 pX = &aMem[pOp->p3];
101654 db->vtabOnConflict = pOp->p5;
101655 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
101656 db->vtabOnConflict = vtabOnConflict;
101658 if( rc==SQLITE_OK && pOp->p1 ){
101659 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
101660 db->lastRowid = rowid;
101662 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
101663 if( pOp->p5==OE_Ignore ){
101666 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
101669 p->nChange++;
101684 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
101704 pBt = db->aDb[pOp->p1].pBt;
101706 if( pOp->p3 ){
101708 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
101710 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
101725 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
101744 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
101753 ** purely non-deterministic. Some built-in date/time functions can be
101754 ** either deterministic of non-deterministic, depending on their arguments.
101755 ** When those function are used in a non-deterministic way, they will check
101766 assert( pOp->p4type==P4_FUNCCTX );
101767 pCtx = pOp->p4.pCtx;
101773 pOut = &aMem[pOp->p3];
101774 if( pCtx->pOut != pOut ){
101775 pCtx->pVdbe = p;
101776 pCtx->pOut = pOut;
101777 pCtx->enc = encoding;
101778 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
101780 assert( pCtx->pVdbe==p );
101784 for(i=0; i<pCtx->argc; i++){
101785 assert( memIsValid(pCtx->argv[i]) );
101786 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
101790 assert( pCtx->isError==0 );
101791 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
101794 if( pCtx->isError ){
101795 if( pCtx->isError>0 ){
101797 rc = pCtx->isError;
101799 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
101800 pCtx->isError = 0;
101804 assert( (pOut->flags&MEM_Str)==0
101805 || pOut->enc==encoding
101806 || db->mallocFailed );
101809 REGISTER_TRACE(pOp->p3, pOut);
101820 pIn1 = &aMem[pOp->p1];
101821 pIn1->flags &= ~MEM_Subtype;
101832 pIn1 = &aMem[pOp->p1];
101833 pOut = &aMem[pOp->p2];
101834 if( pIn1->flags & MEM_Subtype ){
101835 sqlite3VdbeMemSetInt64(pOut, pIn1->eSubtype);
101849 pIn1 = &aMem[pOp->p1];
101850 pOut = &aMem[pOp->p2];
101851 if( pIn1->flags & MEM_Null ){
101852 pOut->flags &= ~MEM_Subtype;
101854 assert( pIn1->flags & MEM_Int );
101855 pOut->flags |= MEM_Subtype;
101856 pOut->eSubtype = (u8)(pIn1->u.i & 0xff);
101870 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
101871 pIn1 = &aMem[pOp->p1];
101872 assert( pIn1->flags & MEM_Blob );
101873 assert( pIn1->n>0 );
101876 if( db->flags&SQLITE_VdbeTrace ){
101878 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
101881 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
101884 h %= (pIn1->n*8);
101885 pIn1->z[h/8] |= 1<<(h&7);
101901 ** false positive - if the jump is taken when it should fall through.
101906 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
101907 pIn1 = &aMem[pOp->p1];
101908 assert( (pIn1->flags & MEM_Blob)!=0 );
101909 assert( pIn1->n >= 1 );
101912 if( db->flags&SQLITE_VdbeTrace ){
101914 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
101917 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
101920 h %= (pIn1->n*8);
101921 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){
101923 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++;
101926 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++;
101946 ** the UTF-8 string contained in P4 is emitted on the trace callback.
101965 ** The "--" string is broken up to prevent false-positives with srcck1.c.
101968 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
101970 ** using the X argument when X begins with "--" and invoking
101973 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
101976 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
101979 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
101980 && p->minWriteFileFormat!=254 /* tag-20220401a */
101981 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
101984 if( db->mTrace & SQLITE_TRACE_LEGACY ){
101986 db->trace.xLegacy(db->pTraceArg, z);
101990 if( db->nVdbeExec>1 ){
101991 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
101992 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
101995 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
101999 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
102002 for(j=0; j<db->nDb; j++){
102003 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
102004 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
102009 if( (db->flags & SQLITE_SqlTrace)!=0
102010 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
102012 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
102016 assert( pOp->p2>0 );
102017 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
102018 if( pOp->opcode==OP_Trace ) break;
102019 for(i=1; i<p->nOp; i++){
102020 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
102022 pOp->p1 = 0;
102024 pOp->p1++;
102025 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
102035 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
102040 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
102041 assert( pOp->p4type==P4_EXPR );
102042 pC = p->apCsr[pOp->p1];
102044 assert( pC->eCurType==CURTYPE_BTREE );
102045 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
102046 pOp->p4.pExpr, aMem);
102103 assert( pOp->p1>0 );
102104 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
102105 pMem = &aMem[pOp->p1];
102106 constMask = pOp->p3;
102107 for(i=0; i<pOp->p2; i++, pMem++){
102109 pMem->pScopyFrom = 0;
102110 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
102126 ** the same as a no-op. This opcodesnever appears in a real VM program.
102129 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
102136 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
102158 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
102161 if( db->flags & SQLITE_VdbeTrace ){
102162 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
102165 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
102168 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
102185 if( db->mallocFailed ){
102192 if( db->flags & SQLITE_VdbeTrace ){
102193 const char *zTrace = p->zSql;
102200 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
102203 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
102206 p->rc = rc;
102210 (int)(pOp - aOp), p->zSql, p->zErrMsg);
102211 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
102213 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){
102214 db->flags |= SQLITE_CorruptRdOnly;
102218 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
102238 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
102239 nProgressLimit += db->nProgressOps;
102240 if( db->xProgress(db->pProgressArg) ){
102247 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
102248 if( DbMaskNonZero(p->lockMask) ){
102252 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
102276 assert( AtomicLoad(&db->u1.isInterrupted) );
102322 ** the b-tree cursor associated with blob handle p to point to row iRow.
102333 ** If an error does occur, then the b-tree cursor is closed. All subsequent
102340 Vdbe *v = (Vdbe *)p->pStmt;
102345 sqlite3VdbeMemSetInt64(&v->aMem[1], iRow);
102351 if( v->pc>4 ){
102352 v->pc = 4;
102353 assert( v->aOp[v->pc].opcode==OP_NotExists );
102356 rc = sqlite3_step(p->pStmt);
102359 VdbeCursor *pC = v->apCsr[0];
102362 assert( pC->eCurType==CURTYPE_BTREE );
102363 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
102364 testcase( pC->nHdrParsed==p->iCol );
102365 testcase( pC->nHdrParsed==p->iCol+1 );
102367 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
102371 sqlite3_finalize(p->pStmt);
102372 p->pStmt = 0;
102374 p->iOffset = pC->aType[p->iCol + pC->nField];
102375 p->nByte = sqlite3VdbeSerialTypeLen(type);
102376 p->pCsr = pC->uc.pCursor;
102377 sqlite3BtreeIncrblobCursor(p->pCsr);
102383 }else if( p->pStmt ){
102384 rc = sqlite3_finalize(p->pStmt);
102385 p->pStmt = 0;
102387 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
102390 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
102410 int wrFlag, /* True -> read/write access, false -> read-only */
102414 int iCol; /* Index of zColumn in row-record */
102434 sqlite3_mutex_enter(db->mutex);
102469 pBlob->pTab = pTab;
102470 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
102473 for(iCol=0; iCol<pTab->nCol; iCol++) {
102474 if( sqlite3StrICmp(pTab->aCol[iCol].zCnName, zColumn)==0 ){
102478 if( iCol==pTab->nCol ){
102493 if( db->flags&SQLITE_ForeignKeys ){
102500 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
102502 for(j=0; j<pFKey->nCol; j++){
102503 if( pFKey->aCol[j].iFrom==iCol ){
102510 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
102512 for(j=0; j<pIdx->nKeyCol; j++){
102514 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
102528 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
102529 assert( pBlob->pStmt || db->mallocFailed );
102530 if( pBlob->pStmt ){
102534 ** of writing code to use the b-tree layer directly is that the
102539 ** Code external to the Vdbe then "borrows" the b-tree cursor and
102544 ** which closes the b-tree cursor and (possibly) commits the
102557 Vdbe *v = (Vdbe *)pBlob->pStmt;
102558 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
102562 pTab->pSchema->schema_cookie,
102563 pTab->pSchema->iGeneration);
102565 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
102571 if( db->mallocFailed==0 ){
102578 aOp[0].p2 = pTab->tnum;
102580 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
102582 if( db->mallocFailed==0 ){
102586 ** parameter of the other to pTab->tnum. */
102588 aOp[1].p2 = pTab->tnum;
102599 aOp[1].p4.i = pTab->nCol+1;
102600 aOp[3].p2 = pTab->nCol;
102609 pBlob->iCol = iCol;
102610 pBlob->db = db;
102612 if( db->mallocFailed ){
102621 if( rc==SQLITE_OK && db->mallocFailed==0 ){
102624 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
102631 sqlite3_mutex_leave(db->mutex);
102645 sqlite3_stmt *pStmt = p->pStmt;
102646 db = p->db;
102647 sqlite3_mutex_enter(db->mutex);
102649 sqlite3_mutex_leave(db->mutex);
102673 db = p->db;
102674 sqlite3_mutex_enter(db->mutex);
102675 v = (Vdbe*)p->pStmt;
102677 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
102681 /* If there is no statement handle, then the blob-handle has
102687 ** returned, clean-up the statement handle.
102689 assert( db == v->db );
102690 sqlite3BtreeEnterCursor(p->pCsr);
102693 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
102694 /* If a pre-update hook is registered and this is a write cursor,
102697 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
102704 ** using the incremental-blob API, this works. For the sessions module
102708 iKey = sqlite3BtreeIntegerKey(p->pCsr);
102709 assert( v->apCsr[0]!=0 );
102710 assert( v->apCsr[0]->eCurType==CURTYPE_BTREE );
102712 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1, p->iCol
102717 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
102718 sqlite3BtreeLeaveCursor(p->pCsr);
102721 p->pStmt = 0;
102723 v->rc = rc;
102728 sqlite3_mutex_leave(db->mutex);
102754 return (p && p->pStmt) ? p->nByte : 0;
102773 db = p->db;
102774 sqlite3_mutex_enter(db->mutex);
102776 if( p->pStmt==0 ){
102777 /* If there is no statement handle, then the blob-handle has
102783 ((Vdbe*)p->pStmt)->rc = SQLITE_OK;
102793 assert( rc==SQLITE_OK || p->pStmt==0 );
102794 sqlite3_mutex_leave(db->mutex);
102803 ** 2011-07-09
102818 ** The VdbeSorter object implements a multi-threaded external merge sort
102822 ** Here is the (internal, non-API) interface between this module and the
102875 ** an in-memory merge sort. In this case, no temporary files are required
102882 ** A PMA created at this point is known as a "level-0 PMA". Higher levels
102883 ** of PMAs may be created by merging existing PMAs together - for example
102884 ** merging two or more level-0 PMAs together creates a level-1 PMA.
102888 ** page-cache of the main database. Specifically, the threshold is set to
102892 ** If the sorter is running in single-threaded mode, then all PMAs generated
102894 ** multi-threaded mode then up to (N+1) temporary files may be opened, where
102901 ** The sorter is running in multi-threaded mode if (a) the library was built
102902 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
102911 ** sorter is running in single-threaded mode, then these PMAs are merged
102916 ** Or, if running in multi-threaded mode, then a background thread is
102926 ** Rewind() is called, then a hierarchy of incremental-merges is used.
102932 ** If running in multi-threaded mode and there are more than
102945 ** characteristics of the sorter in multi-threaded mode.
102952 ** Hard-coded maximum amount of data to accumulate in memory before flushing
102965 typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
102967 typedef struct SorterList SorterList; /* In-memory list of records */
102980 ** An in-memory list of objects to be sorted.
102989 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
103012 ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
103026 ** aReadr[0] -> Banana
103027 ** aReadr[1] -> Feijoa
103028 ** aReadr[2] -> Elderberry
103029 ** aReadr[3] -> Currant
103030 ** aReadr[4] -> Grapefruit
103031 ** aReadr[5] -> Apple
103032 ** aReadr[6] -> Durian
103033 ** aReadr[7] -> EOF
103042 ** aReadr[5] -> Eggplant
103047 ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
103070 ** single-threaded operation, there is exactly one instance of this object
103071 ** and for multi-threaded operation there are two or more instances.
103082 ** 1. When flushing the contents of memory to a level-0 PMA on disk, to
103100 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
103104 SorterFile file; /* Temp file for level-0 PMAs */
103128 SorterList list; /* List of in-memory records */
103171 ** There are two types of IncrMerger object - single (bUseThread==0) and
103172 ** multi-threaded (bUseThread==1).
103174 ** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
103189 ** A single-threaded IncrMerger does not open any temporary files of its
103191 ** at offset iStartOff of file pTask->file2. And instead of using a
103193 ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
103211 ** size. But I/O is more efficient if it occurs in page-sized blocks where
103213 ** the PMA so that aligned, page-size blocks are written.
103216 int eFWErr; /* Non-zero if in an error state */
103230 ** by this module. If using a separate allocation for each in-memory record
103238 ** has finished passing records to the sorter, or when the in-memory buffer
103271 sqlite3_free(pReadr->aAlloc);
103272 sqlite3_free(pReadr->aBuffer);
103273 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
103274 vdbeIncrFree(pReadr->pIncr);
103295 if( p->aMap ){
103296 *ppOut = &p->aMap[p->iReadOff];
103297 p->iReadOff += nByte;
103301 assert( p->aBuffer );
103304 ** p->nBuffer bytes of data from the file into it. Or, if there are less
103305 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
103306 iBuf = p->iReadOff % p->nBuffer;
103312 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
103313 nRead = p->nBuffer;
103315 nRead = (int)(p->iEof - p->iReadOff);
103320 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
103324 nAvail = p->nBuffer - iBuf;
103327 /* The requested data is available in the in-memory buffer. In this
103330 *ppOut = &p->aBuffer[iBuf];
103331 p->iReadOff += nByte;
103333 /* The requested data is not all available in the in-memory buffer.
103334 ** In this case, allocate space at p->aAlloc[] to copy the requested
103335 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
103338 /* Extend the p->aAlloc[] allocation if required. */
103339 if( p->nAlloc<nByte ){
103341 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
103343 aNew = sqlite3Realloc(p->aAlloc, nNew);
103345 p->nAlloc = nNew;
103346 p->aAlloc = aNew;
103350 ** p->aAlloc[]. */
103351 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
103352 p->iReadOff += nAvail;
103353 nRem = nByte - nAvail;
103355 /* The following loop copies up to p->nBuffer bytes per iteration into
103356 ** the p->aAlloc[] buffer. */
103363 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
103366 assert( aNext!=p->aAlloc );
103367 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
103368 nRem -= nCopy;
103371 *ppOut = p->aAlloc;
103384 if( p->aMap ){
103385 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
103387 iBuf = p->iReadOff % p->nBuffer;
103388 if( iBuf && (p->nBuffer-iBuf)>=9 ){
103389 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
103416 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
103417 sqlite3_file *pFd = pFile->pFd;
103418 if( pFd->pMethods->iVersion>=3 ){
103419 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
103439 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
103442 if( pReadr->aMap ){
103443 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
103444 pReadr->aMap = 0;
103446 pReadr->iReadOff = iOff;
103447 pReadr->iEof = pFile->iEof;
103448 pReadr->pFd = pFile->pFd;
103450 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
103451 if( rc==SQLITE_OK && pReadr->aMap==0 ){
103452 int pgsz = pTask->pSorter->pgsz;
103453 int iBuf = pReadr->iReadOff % pgsz;
103454 if( pReadr->aBuffer==0 ){
103455 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
103456 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
103457 pReadr->nBuffer = pgsz;
103460 int nRead = pgsz - iBuf;
103461 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
103462 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
103465 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
103483 if( pReadr->iReadOff>=pReadr->iEof ){
103484 IncrMerger *pIncr = pReadr->pIncr;
103488 if( rc==SQLITE_OK && pIncr->bEof==0 ){
103490 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
103508 pReadr->nKey = (int)nRec;
103509 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
103518 ** starting at offset iStart and ending at offset iEof-1. This function
103534 assert( pFile->iEof>iStart );
103535 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
103536 assert( pReadr->aBuffer==0 );
103537 assert( pReadr->aMap==0 );
103543 pReadr->iEof = pReadr->iReadOff + nByte;
103560 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
103564 UnpackedRecord *r2 = pTask->pUnpacked;
103566 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
103574 ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
103578 ** it is assumed that (pTask->pUnpacked) contains the unpacked version
103579 ** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
103582 ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
103587 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
103591 UnpackedRecord *r2 = pTask->pUnpacked;
103593 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
103606 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
103621 res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
103623 res = n1 - n2;
103627 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
103633 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
103634 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
103635 res = res * -1;
103648 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
103670 if( (res = v1[i] - v2[i])!=0 ){
103672 res = v1[0] & 0x80 ? -1 : +1;
103678 res = s1 - s2;
103683 res = -1;
103685 res = s1 - s2;
103690 if( *v1 & 0x80 ) res = -1;
103697 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
103702 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
103703 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
103704 res = res * -1;
103713 ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
103716 ** is non-zero and the sorter is able to guarantee a stable sort, nField
103724 ** The sorter can guarantee a stable sort when running in single-threaded
103725 ** mode, but not in multi-threaded mode.
103737 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
103738 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
103752 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
103760 nWorker = SORTER_MAX_MERGE_COUNT-1;
103764 assert( pCsr->pKeyInfo );
103765 assert( !pCsr->isEphemeral );
103766 assert( pCsr->eCurType==CURTYPE_SORTER );
103767 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
103771 pCsr->uc.pSorter = pSorter;
103775 Btree *pBt = db->aDb[0].pBt;
103776 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
103777 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
103778 pKeyInfo->db = 0;
103780 pKeyInfo->nKeyField = nField;
103783 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(pBt);
103785 pSorter->nTask = nWorker + 1;
103786 pSorter->iPrev = (u8)(nWorker - 1);
103787 pSorter->bUseThreads = (pSorter->nTask>1);
103788 pSorter->db = db;
103789 for(i=0; i<pSorter->nTask; i++){
103790 SortSubtask *pTask = &pSorter->aTask[i];
103791 pTask->pSorter = pSorter;
103797 pSorter->mnPmaSize = szPma * pgsz;
103799 mxCache = db->aDb[0].pSchema->cache_size;
103801 /* A negative cache-size value C indicates that the cache is abs(C)
103803 mxCache = mxCache * -1024;
103808 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
103813 assert( pSorter->iMemory==0 );
103814 pSorter->nMemory = pgsz;
103815 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
103816 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
103820 if( pKeyInfo->nAllField<13
103821 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
103822 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL)==0
103824 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
103839 pNext = p->u.pNext;
103849 sqlite3DbFree(db, pTask->pUnpacked);
103851 /* pTask->list.aMemory can only be non-zero if it was handed memory
103853 if( pTask->list.aMemory ){
103854 sqlite3_free(pTask->list.aMemory);
103858 assert( pTask->list.aMemory==0 );
103859 vdbeSorterRecordFree(0, pTask->list.pList);
103861 if( pTask->file.pFd ){
103862 sqlite3OsCloseFree(pTask->file.pFd);
103864 if( pTask->file2.pFd ){
103865 sqlite3OsCloseFree(pTask->file2.pFd);
103873 int iTask = (pTask - pTask->pSorter->aTask);
103874 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
103888 int iTask = (pTask - pTask->pSorter->aTask);
103889 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
103899 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
103912 ** Join thread pTask->thread.
103916 if( pTask->pThread ){
103918 int bDone = pTask->bDone;
103922 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
103925 assert( pTask->bDone==1 );
103926 pTask->bDone = 0;
103927 pTask->pThread = 0;
103940 assert( pTask->pThread==0 && pTask->bDone==0 );
103941 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
103946 ** level-0 PMAs.
103955 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
103958 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
103959 for(i=pSorter->nTask-1; i>=0; i--){
103960 SortSubtask *pTask = &pSorter->aTask[i];
103990 pNew->nTree = N;
103991 pNew->pTask = 0;
103992 pNew->aReadr = (PmaReader*)&pNew[1];
103993 pNew->aTree = (int*)&pNew->aReadr[N];
104004 for(i=0; i<pMerger->nTree; i++){
104005 vdbePmaReaderClear(&pMerger->aReadr[i]);
104018 if( pIncr->bUseThread ){
104019 vdbeSorterJoinThread(pIncr->pTask);
104020 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
104021 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
104024 vdbeMergeEngineFree(pIncr->pMerger);
104035 assert( pSorter->bUseThreads || pSorter->pReader==0 );
104037 if( pSorter->pReader ){
104038 vdbePmaReaderClear(pSorter->pReader);
104039 sqlite3DbFree(db, pSorter->pReader);
104040 pSorter->pReader = 0;
104043 vdbeMergeEngineFree(pSorter->pMerger);
104044 pSorter->pMerger = 0;
104045 for(i=0; i<pSorter->nTask; i++){
104046 SortSubtask *pTask = &pSorter->aTask[i];
104048 pTask->pSorter = pSorter;
104050 if( pSorter->list.aMemory==0 ){
104051 vdbeSorterRecordFree(0, pSorter->list.pList);
104053 pSorter->list.pList = 0;
104054 pSorter->list.szPMA = 0;
104055 pSorter->bUsePMA = 0;
104056 pSorter->iMemory = 0;
104057 pSorter->mxKeysize = 0;
104058 sqlite3DbFree(db, pSorter->pUnpacked);
104059 pSorter->pUnpacked = 0;
104067 assert( pCsr->eCurType==CURTYPE_SORTER );
104068 pSorter = pCsr->uc.pSorter;
104071 sqlite3_free(pSorter->list.aMemory);
104073 pCsr->uc.pSorter = 0;
104079 ** The first argument is a file-handle open on a temporary file. The file
104088 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
104102 ** Allocate space for a file-handle and open a temporary file. If successful,
104103 ** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
104113 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
104130 ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
104134 if( pTask->pUnpacked==0 ){
104135 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
104136 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
104137 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
104138 pTask->pUnpacked->errCode = 0;
104159 res = pTask->xCompare(
104160 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
104165 pp = &p1->u.pNext;
104166 p1 = p1->u.pNext;
104173 pp = &p2->u.pNext;
104174 p2 = p2->u.pNext;
104190 if( p->typeMask==SORTER_TYPE_INTEGER ){
104192 }else if( p->typeMask==SORTER_TYPE_TEXT ){
104199 ** Sort the linked list of records headed at pTask->pList. Return
104212 p = pList->pList;
104213 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
104218 if( pList->aMemory ){
104219 if( (u8*)p==pList->aMemory ){
104222 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
104223 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
104226 pNext = p->u.pNext;
104229 p->u.pNext = 0;
104243 pList->pList = p;
104245 assert( pTask->pUnpacked->errCode==SQLITE_OK
104246 || pTask->pUnpacked->errCode==SQLITE_NOMEM
104248 return pTask->pUnpacked->errCode;
104252 ** Initialize a PMA-writer object.
104261 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
104262 if( !p->aBuffer ){
104263 p->eFWErr = SQLITE_NOMEM_BKPT;
104265 p->iBufEnd = p->iBufStart = (iStart % nBuf);
104266 p->iWriteOff = iStart - p->iBufStart;
104267 p->nBuffer = nBuf;
104268 p->pFd = pFd;
104278 while( nRem>0 && p->eFWErr==0 ){
104280 if( nCopy>(p->nBuffer - p->iBufEnd) ){
104281 nCopy = p->nBuffer - p->iBufEnd;
104284 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
104285 p->iBufEnd += nCopy;
104286 if( p->iBufEnd==p->nBuffer ){
104287 p->eFWErr = sqlite3OsWrite(p->pFd,
104288 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
104289 p->iWriteOff + p->iBufStart
104291 p->iBufStart = p->iBufEnd = 0;
104292 p->iWriteOff += p->nBuffer;
104294 assert( p->iBufEnd<p->nBuffer );
104296 nRem -= nCopy;
104301 ** Flush any buffered data to disk and clean up the PMA-writer object.
104302 ** The results of using the PMA-writer after this call are undefined.
104311 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
104312 p->eFWErr = sqlite3OsWrite(p->pFd,
104313 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
104314 p->iWriteOff + p->iBufStart
104317 *piEof = (p->iWriteOff + p->iBufEnd);
104318 sqlite3_free(p->aBuffer);
104319 rc = p->eFWErr;
104336 ** Write the current contents of in-memory linked-list pList to a level-0
104337 ** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
104345 ** * One or more records packed end-to-end in order of ascending keys.
104350 sqlite3 *db = pTask->pSorter->db;
104355 /* Set iSz to the expected size of file pTask->file after writing the PMA.
104357 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
104362 assert( pList->szPMA>0 );
104365 if( pTask->file.pFd==0 ){
104366 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
104367 assert( rc!=SQLITE_OK || pTask->file.pFd );
104368 assert( pTask->file.iEof==0 );
104369 assert( pTask->nPMA==0 );
104374 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
104386 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
104387 pTask->file.iEof);
104388 pTask->nPMA++;
104389 vdbePmaWriteVarint(&writer, pList->szPMA);
104390 for(p=pList->pList; p; p=pNext){
104391 pNext = p->u.pNext;
104392 vdbePmaWriteVarint(&writer, p->nVal);
104393 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
104394 if( pList->aMemory==0 ) sqlite3_free(p);
104396 pList->pList = p;
104397 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
104401 assert( rc!=SQLITE_OK || pList->pList==0 );
104402 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
104418 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
104419 SortSubtask *pTask = pMerger->pTask;
104422 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
104433 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
104434 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
104436 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
104439 if( pReadr1->pFd==0 ){
104441 }else if( pReadr2->pFd==0 ){
104442 iRes = -1;
104444 iRes = pTask->xCompare(pTask, &bCached,
104445 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
104451 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
104456 ** was actually called above, then pTask->pUnpacked now contains
104465 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
104466 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
104469 if( pReadr1->pFd ) bCached = 0;
104470 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
104471 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
104474 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
104477 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
104482 ** The main routine for background threads that write level-0 PMAs.
104487 assert( pTask->bDone==0 );
104488 rc = vdbeSorterListToPMA(pTask, &pTask->list);
104489 pTask->bDone = 1;
104500 pSorter->bUsePMA = 1;
104501 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
104506 int nWorker = (pSorter->nTask-1);
104510 pSorter->bUsePMA = 1;
104512 /* Select a sub-task to sort and flush the current list of in-memory
104513 ** records to disk. If the sorter is running in multi-threaded mode,
104514 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
104515 ** the background thread from a sub-tasks previous turn is still running,
104516 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
104517 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
104518 ** sub-tasks are preferred as they use background threads - the final
104519 ** sub-task uses the main thread. */
104521 int iTest = (pSorter->iPrev + i + 1) % nWorker;
104522 pTask = &pSorter->aTask[iTest];
104523 if( pTask->bDone ){
104526 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
104532 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
104539 assert( pTask->pThread==0 && pTask->bDone==0 );
104540 assert( pTask->list.pList==0 );
104541 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
104543 aMem = pTask->list.aMemory;
104545 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
104546 pTask->list = pSorter->list;
104547 pSorter->list.pList = 0;
104548 pSorter->list.szPMA = 0;
104550 pSorter->list.aMemory = aMem;
104551 pSorter->nMemory = sqlite3MallocSize(aMem);
104552 }else if( pSorter->list.aMemory ){
104553 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
104554 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
104580 assert( pCsr->eCurType==CURTYPE_SORTER );
104581 pSorter = pCsr->uc.pSorter;
104582 getVarint32NR((const u8*)&pVal->z[1], t);
104584 pSorter->typeMask &= SORTER_TYPE_INTEGER;
104586 pSorter->typeMask &= SORTER_TYPE_TEXT;
104588 pSorter->typeMask = 0;
104596 ** If using the single large allocation mode (pSorter->aMemory!=0), then
104603 ** * The total memory allocated for the in-memory list is greater
104604 ** than (page-size * cache-size), or
104606 ** * The total memory allocated for the in-memory list is greater
104607 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
104609 nReq = pVal->n + sizeof(SorterRecord);
104610 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
104611 if( pSorter->mxPmaSize ){
104612 if( pSorter->list.aMemory ){
104613 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
104616 (pSorter->list.szPMA > pSorter->mxPmaSize)
104617 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
104622 pSorter->list.szPMA = 0;
104623 pSorter->iMemory = 0;
104624 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
104628 pSorter->list.szPMA += nPMA;
104629 if( nPMA>pSorter->mxKeysize ){
104630 pSorter->mxKeysize = nPMA;
104633 if( pSorter->list.aMemory ){
104634 int nMin = pSorter->iMemory + nReq;
104636 if( nMin>pSorter->nMemory ){
104638 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
104639 int iListOff = -1;
104640 if( pSorter->list.pList ){
104641 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
104644 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
104646 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
104649 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
104651 pSorter->list.aMemory = aNew;
104652 pSorter->nMemory = nNew;
104655 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
104656 pSorter->iMemory += ROUND8(nReq);
104657 if( pSorter->list.pList ){
104658 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
104665 pNew->u.pNext = pSorter->list.pList;
104668 memcpy(SRVAL(pNew), pVal->z, pVal->n);
104669 pNew->nVal = pVal->n;
104670 pSorter->list.pList = pNew;
104676 ** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
104678 ** except that the number-of-bytes varint is omitted from the start.
104683 i64 iStart = pIncr->iStartOff;
104684 SorterFile *pOut = &pIncr->aFile[1];
104685 SortSubtask *pTask = pIncr->pTask;
104686 MergeEngine *pMerger = pIncr->pMerger;
104688 assert( pIncr->bEof==0 );
104692 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
104695 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
104696 int nKey = pReader->nKey;
104701 if( pReader->pFd==0 ) break;
104702 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
104706 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
104707 assert( pIncr->pMerger->pTask==pTask );
104708 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
104711 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
104720 ** multi-threaded IncrMerger objects.
104725 pIncr->pTask->bDone = 1;
104734 assert( pIncr->bUseThread );
104735 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
104745 ** For single-threaded objects, this is accomplished by literally reading
104746 ** keys from pIncr->pMerger and repopulating aFile[0].
104748 ** For multi-threaded objects, all that is required is to wait until the
104760 if( pIncr->bUseThread ){
104761 rc = vdbeSorterJoinThread(pIncr->pTask);
104764 SorterFile f0 = pIncr->aFile[0];
104765 pIncr->aFile[0] = pIncr->aFile[1];
104766 pIncr->aFile[1] = f0;
104770 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
104771 pIncr->bEof = 1;
104780 pIncr->aFile[0] = pIncr->aFile[1];
104781 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
104782 pIncr->bEof = 1;
104804 pIncr->pMerger = pMerger;
104805 pIncr->pTask = pTask;
104806 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
104807 pTask->file2.iEof += pIncr->mxSz;
104818 ** Set the "use-threads" flag on object pIncr.
104821 pIncr->bUseThread = 1;
104822 pIncr->pTask->file2.iEof -= pIncr->mxSz;
104829 ** Recompute pMerger->aTree[iOut] by comparing the next keys on the
104835 int iOut /* Store the result in pMerger->aTree[iOut] */
104843 assert( iOut<pMerger->nTree && iOut>0 );
104845 if( iOut>=(pMerger->nTree/2) ){
104846 i1 = (iOut - pMerger->nTree/2) * 2;
104849 i1 = pMerger->aTree[iOut*2];
104850 i2 = pMerger->aTree[iOut*2+1];
104853 p1 = &pMerger->aReadr[i1];
104854 p2 = &pMerger->aReadr[i2];
104856 if( p1->pFd==0 ){
104858 }else if( p2->pFd==0 ){
104861 SortSubtask *pTask = pMerger->pTask;
104864 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
104865 res = pTask->xCompare(
104866 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
104875 pMerger->aTree[iOut] = iRes;
104882 ** Only INCRINIT_NORMAL is valid in single-threaded builds (when
104928 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
104932 assert( pMerger->pTask==0 );
104933 pMerger->pTask = pTask;
104935 nTree = pMerger->nTree;
104940 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
104943 ** on this PmaReader before any of the multi-threaded PmaReaders takes
104944 ** better advantage of multi-processor hardware. */
104945 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
104947 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
104952 for(i=pMerger->nTree-1; i>0; i--){
104955 return pTask->pUnpacked->errCode;
104960 ** incremental-reader (pReadr->pIncr!=0). This function serves to open
104962 ** object at (pReadr->pIncr).
104965 ** in the sub-tree headed by pReadr are also initialized. Data is then
104970 ** to be a multi-threaded PmaReader and this function is being called in a
104971 ** background thread. In this case all PmaReaders in the sub-tree are
104979 ** to block on thread (pTask->thread) before accessing aFile[1]. But, since
104980 ** this entire function is being run by thread (pTask->thread), that will
104984 ** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
104985 ** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
104993 IncrMerger *pIncr = pReadr->pIncr;
104994 SortSubtask *pTask = pIncr->pTask;
104995 sqlite3 *db = pTask->pSorter->db;
104997 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
105000 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
105002 /* Set up the required files for pIncr. A multi-threaded IncrMerge object
105003 ** requires two temp files to itself, whereas a single-threaded object
105004 ** only requires a region of pTask->file2. */
105006 int mxSz = pIncr->mxSz;
105008 if( pIncr->bUseThread ){
105009 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
105011 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
105015 /*if( !pIncr->bUseThread )*/{
105016 if( pTask->file2.pFd==0 ){
105017 assert( pTask->file2.iEof>0 );
105018 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
105019 pTask->file2.iEof = 0;
105022 pIncr->aFile[1].pFd = pTask->file2.pFd;
105023 pIncr->iStartOff = pTask->file2.iEof;
105024 pTask->file2.iEof += mxSz;
105030 if( rc==SQLITE_OK && pIncr->bUseThread ){
105032 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
105034 ** pIncr->pTask->thread.
105063 pReader->pIncr->pTask->bDone = 1;
105069 ** If the PmaReader passed as the first argument is not an incremental-reader
105070 ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
105074 ** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
105080 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
105084 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
105085 if( pIncr->bUseThread ){
105087 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
105098 ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
105099 ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
105104 ** first PMA to read from pTask->file. Assuming no error occurs, it is
105112 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
105113 MergeEngine **ppOut /* OUT: New merge-engine */
105125 PmaReader *pReadr = &pNew->aReadr[i];
105126 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
105127 iOff = pReadr->iEof;
105144 ** nPMA<=16 -> TreeDepth() == 0
105145 ** nPMA<=256 -> TreeDepth() == 1
105146 ** nPMA<=65536 -> TreeDepth() == 2
105159 ** pRoot is the root of an incremental merge-tree with depth nDepth (according
105187 PmaReader *pReadr = &p->aReadr[iIter];
105189 if( pReadr->pIncr==0 ){
105194 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
105198 p = pReadr->pIncr->pMerger;
105204 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
105213 ** that has already written two or more level-0 PMAs to one or more temp
105231 /* If the sorter uses more than one task, then create the top-level
105233 ** one PmaReader per sub-task. */
105234 assert( pSorter->bUseThreads || pSorter->nTask==1 );
105235 if( pSorter->nTask>1 ){
105236 pMain = vdbeMergeEngineNew(pSorter->nTask);
105241 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
105242 SortSubtask *pTask = &pSorter->aTask[iTask];
105243 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
105244 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
105246 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
105249 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
105250 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
105256 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
105257 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
105258 int nReader; /* Number of level-0 PMAs to merge */
105260 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
105271 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
105296 ** (for multi-threaded sorters) so that it can be used to iterate through
105303 SortSubtask *pTask0 = &pSorter->aTask[0];
105306 sqlite3 *db = pTask0->pSorter->db;
105309 for(i=0; i<pSorter->nTask; i++){
105310 pSorter->aTask[i].xCompare = xCompare;
105317 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
105318 if( pSorter->bUseThreads ){
105321 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
105325 pSorter->pReader = pReadr;
105329 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
105331 vdbeIncrMergerSetThreads(pReadr->pIncr);
105332 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
105334 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
105336 assert( pIncr->pTask!=pLast );
105339 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
105344 ** b) If it is using task (nTask-1), it is configured to run
105345 ** in single-threaded mode. This is important, as the
105349 PmaReader *p = &pMain->aReadr[iTask];
105350 assert( p->pIncr==0 || (
105351 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
105352 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
105366 pSorter->pMerger = pMain;
105387 assert( pCsr->eCurType==CURTYPE_SORTER );
105388 pSorter = pCsr->uc.pSorter;
105393 ** from the in-memory list. */
105394 if( pSorter->bUsePMA==0 ){
105395 if( pSorter->list.pList ){
105397 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
105404 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
105408 assert( pSorter->list.pList );
105418 assert( pSorter->pReader==0 );
105439 assert( pCsr->eCurType==CURTYPE_SORTER );
105440 pSorter = pCsr->uc.pSorter;
105441 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
105442 if( pSorter->bUsePMA ){
105443 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
105444 assert( pSorter->bUseThreads==0 || pSorter->pReader );
105445 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
105447 if( pSorter->bUseThreads ){
105448 rc = vdbePmaReaderNext(pSorter->pReader);
105449 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
105452 /*if( !pSorter->bUseThreads )*/ {
105454 assert( pSorter->pMerger!=0 );
105455 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
105456 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
105460 SorterRecord *pFree = pSorter->list.pList;
105461 pSorter->list.pList = pFree->u.pNext;
105462 pFree->u.pNext = 0;
105463 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
105464 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
105478 if( pSorter->bUsePMA ){
105481 if( pSorter->bUseThreads ){
105482 pReader = pSorter->pReader;
105485 /*if( !pSorter->bUseThreads )*/{
105486 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
105488 *pnKey = pReader->nKey;
105489 pKey = pReader->aKey;
105491 *pnKey = pSorter->list.pList->nVal;
105492 pKey = SRVAL(pSorter->list.pList);
105504 assert( pCsr->eCurType==CURTYPE_SORTER );
105505 pSorter = pCsr->uc.pSorter;
105510 pOut->n = nKey;
105512 memcpy(pOut->z, pKey, nKey);
105545 assert( pCsr->eCurType==CURTYPE_SORTER );
105546 pSorter = pCsr->uc.pSorter;
105547 r2 = pSorter->pUnpacked;
105548 pKeyInfo = pCsr->pKeyInfo;
105550 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
105552 r2->nField = nKeyCol;
105554 assert( r2->nField==nKeyCol );
105559 if( r2->aMem[i].flags & MEM_Null ){
105560 *pRes = -1;
105565 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
105572 ** 2020-03-23
105583 ** This file implements virtual-tables for examining the bytecode content
105590 /* An instance of the bytecode() table-valued function.
105594 sqlite3_vtab base; /* Base class - must be first */
105603 sqlite3_vtab_cursor base; /* Base class - must be first */
105618 ** Create a new bytecode() table-valued function.
105667 pNew->db = db;
105668 pNew->bTablesUsed = isTabUsed*2;
105691 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
105692 *ppCursor = &pCur->base;
105700 sqlite3_free(pCur->zP4);
105701 pCur->zP4 = 0;
105702 sqlite3VdbeMemRelease(&pCur->sub);
105703 sqlite3VdbeMemSetNull(&pCur->sub);
105704 if( pCur->needFinalize ){
105705 sqlite3_finalize(pCur->pStmt);
105707 pCur->pStmt = 0;
105708 pCur->needFinalize = 0;
105709 pCur->zType = 0;
105710 pCur->zSchema = 0;
105711 pCur->zName = 0;
105730 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
105732 if( pCur->zP4 ){
105733 sqlite3_free(pCur->zP4);
105734 pCur->zP4 = 0;
105736 if( pCur->zName ){
105737 pCur->zName = 0;
105738 pCur->zType = 0;
105739 pCur->zSchema = 0;
105742 (Vdbe*)pCur->pStmt,
105743 pCur->showSubprograms ? &pCur->sub : 0,
105744 pTab->bTablesUsed,
105745 &pCur->iRowid,
105746 &pCur->iAddr,
105747 &pCur->aOp);
105749 sqlite3VdbeMemSetNull(&pCur->sub);
105750 pCur->aOp = 0;
105761 return pCur->aOp==0;
105774 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
105775 Op *pOp = pCur->aOp + pCur->iAddr;
105776 if( pVTab->bTablesUsed ){
105780 if( i<=2 && pCur->zType==0 ){
105783 int iDb = pOp->p3;
105784 Pgno iRoot = (Pgno)pOp->p2;
105785 sqlite3 *db = pVTab->db;
105786 pSchema = db->aDb[iDb].pSchema;
105787 pCur->zSchema = db->aDb[iDb].zDbSName;
105788 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
105790 if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
105791 pCur->zName = pTab->zName;
105792 pCur->zType = "table";
105796 if( pCur->zName==0 ){
105797 for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
105799 if( pIdx->tnum==iRoot ){
105800 pCur->zName = pIdx->zName;
105801 pCur->zType = "index";
105811 sqlite3_result_int(ctx, pCur->iAddr);
105814 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
105815 -1, SQLITE_STATIC);
105818 sqlite3_result_int(ctx, pOp->p1);
105821 sqlite3_result_int(ctx, pOp->p2);
105824 sqlite3_result_int(ctx, pOp->p3);
105828 if( pCur->zP4==0 ){
105829 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
105832 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
105835 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
105836 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
105841 sqlite3_result_int(ctx, pOp->p5);
105844 Op *aOp = pCur->aOp;
105846 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
105847 if( pCur->iRowid==pCur->iAddr+1 ){
105850 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
105859 sqlite3_result_int64(ctx, pOp->nExec);
105862 sqlite3_result_int64(ctx, pOp->nCycle);
105872 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
105875 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
105878 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
105881 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
105893 *pRowid = pCur->iRowid;
105909 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
105914 pCur->iRowid = 0;
105915 pCur->iAddr = 0;
105916 pCur->showSubprograms = idxNum==0;
105923 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
105924 pCur->needFinalize = 1;
105927 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
105929 if( pCur->pStmt==0 ){
105930 pVTab->base.zErrMsg = sqlite3_mprintf(
105932 pVTab->bTablesUsed ? "tables_used" : "bytecode"
105954 int iBaseCol = pVTab->bTablesUsed ? 4 : 10;
105955 pIdxInfo->estimatedCost = (double)100;
105956 pIdxInfo->estimatedRows = 100;
105957 pIdxInfo->idxNum = 0;
105958 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
105959 if( p->usable==0 ) continue;
105960 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
105962 pIdxInfo->aConstraintUsage[i].omit = 1;
105963 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
105965 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
105966 pIdxInfo->aConstraintUsage[i].omit = 1;
105967 pIdxInfo->idxNum = 1;
106032 ** This file contains code use to implement an in-memory rollback journal.
106033 ** The in-memory rollback journal is used to journal transactions for
106036 ** Update: The in-memory journal is also used to temporarily cache
106037 ** smaller journals that are not critical for power-loss recovery.
106055 ** The zChunk array is always at least 8 bytes in size - usually much more.
106072 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
106084 ** This structure is a subclass of sqlite3_file. Each open memory-journal
106089 int nChunkSize; /* In-memory chunk-size */
106092 FileChunk *pFirst; /* Head of in-memory chunk-list */
106102 ** Read data from the in-memory journal file. This is the implementation
106117 if( (iAmt+iOfst)>p->endpoint.iOffset ){
106120 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
106121 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
106123 for(pChunk=p->pFirst;
106124 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
106125 pChunk=pChunk->pNext
106127 iOff += p->nChunkSize;
106130 pChunk = p->readpoint.pChunk;
106134 iChunkOffset = (int)(iOfst%p->nChunkSize);
106136 int iSpace = p->nChunkSize - iChunkOffset;
106137 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
106138 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
106140 nRead -= iSpace;
106142 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
106143 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
106144 p->readpoint.pChunk = pChunk;
106156 pNext = pIter->pNext;
106175 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
106177 nChunk = copy.endpoint.iOffset - iOff;
106179 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
106184 /* No error has occurred. Free the in-memory buffers. */
106190 ** the original before returning. This way, SQLite uses the in-memory
106191 ** journal data to roll back changes made to the internal page-cache
106218 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
106228 /* An in-memory journal file should only ever be appended to. Random
106230 ** the in-memory journal is being used by a connection using the
106231 ** atomic-write optimization. In this case the first 28 bytes of the
106233 assert( iOfst<=p->endpoint.iOffset );
106234 if( iOfst>0 && iOfst!=p->endpoint.iOffset ){
106237 if( iOfst==0 && p->pFirst ){
106238 assert( p->nChunkSize>iAmt );
106239 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
106242 FileChunk *pChunk = p->endpoint.pChunk;
106243 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
106244 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
106249 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
106253 pNew->pNext = 0;
106255 assert( p->pFirst );
106256 pChunk->pNext = pNew;
106258 assert( !p->pFirst );
106259 p->pFirst = pNew;
106261 pChunk = p->endpoint.pChunk = pNew;
106265 memcpy((u8*)pChunk->zChunk + iChunkOffset, zWrite, iSpace);
106267 nWrite -= iSpace;
106268 p->endpoint.iOffset += iSpace;
106277 ** Truncate the in-memory file.
106281 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 );
106282 if( size<p->endpoint.iOffset ){
106285 memjrnlFreeChunks(p->pFirst);
106286 p->pFirst = 0;
106288 i64 iOff = p->nChunkSize;
106289 for(pIter=p->pFirst; ALWAYS(pIter) && iOff<size; pIter=pIter->pNext){
106290 iOff += p->nChunkSize;
106293 memjrnlFreeChunks(pIter->pNext);
106294 pIter->pNext = 0;
106298 p->endpoint.pChunk = pIter;
106299 p->endpoint.iOffset = size;
106300 p->readpoint.pChunk = 0;
106301 p->readpoint.iOffset = 0;
106311 memjrnlFreeChunks(p->pFirst);
106319 ** syncing an in-memory journal is a no-op.
106331 *pSize = (sqlite_int64) p->endpoint.iOffset;
106366 ** all content is always stored in main-memory. Finally, if nSpill is a
106367 ** positive value, then the journal file is initially created in-memory
106383 /* Zero the file-handle object. If nSpill was passed zero, initialize
106386 ** made on the journal file-handle. */
106393 p->nChunkSize = nSpill;
106395 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
106396 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
106399 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
106400 p->nSpill = nSpill;
106401 p->flags = flags;
106402 p->zJournal = zName;
106403 p->pVfs = pVfs;
106408 ** Open an in-memory journal file.
106411 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
106418 ** in-memory-only journal file (i.e. is one that was opened with a +ve
106425 if( pJfd->pMethods==&MemJournalMethods && (
106427 p->nSpill>0
106432 NEVER(p->nSpill>0)
106435 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
106445 ** The file-handle passed as the only argument is open on a journal file.
106450 return p->pMethods==&MemJournalMethods;
106455 ** pVfs to create the underlying on-disk files.
106458 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
106489 for(pWin=pList; pWin; pWin=pWin->pNextWin){
106491 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
106493 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
106495 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
106497 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
106499 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
106521 ** return from the top-level walk call.
106531 rc = pWalker->xExprCallback(pWalker, pExpr);
106534 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
106535 if( pExpr->pLeft && sqlite3WalkExprNN(pWalker, pExpr->pLeft) ){
106538 if( pExpr->pRight ){
106540 pExpr = pExpr->pRight;
106544 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
106546 if( pExpr->x.pList ){
106547 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
106551 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort;
106572 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
106573 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
106580 ** This is a no-op callback for Walker->xSelectCallback2. If this
106581 ** callback is set, then the Select->pWinDefn list is traversed.
106586 /* No-op */
106596 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
106597 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
106598 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
106599 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
106600 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
106601 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
106603 if( p->pWinDefn ){
106605 if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback
106606 || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT)
106608 || pWalker->xSelectCallback2==sqlite3SelectPopWith
106613 int rc = walkWindowList(pWalker, p->pWinDefn, 0);
106633 pSrc = p->pSrc;
106635 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
106636 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
106639 if( pItem->fg.isTabFunc
106640 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
106652 ** on the compound select chain, p->pPrior.
106657 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
106664 ** is a no-op returning WRC_Continue.
106669 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
106671 rc = pWalker->xSelectCallback(pWalker, p);
106678 if( pWalker->xSelectCallback2 ){
106679 pWalker->xSelectCallback2(pWalker, p);
106681 p = p->pPrior;
106691 pWalker->walkerDepth++;
106696 pWalker->walkerDepth--;
106701 ** No-op routine for the parse-tree walker.
106715 ** No-op routine for the parse-tree walker for SELECT statements.
106755 ** is a helper function - a callback for the tree walker.
106760 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
106774 ** Turn the pExpr expression into an alias for the iCol-th column of the
106795 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
106799 Expr *pOrig; /* The iCol-th column of the result set */
106803 assert( iCol>=0 && iCol<pEList->nExpr );
106804 pOrig = pEList->a[iCol].pExpr;
106807 if( pExpr->pAggInfo ) return;
106808 db = pParse->db;
106810 if( db->mallocFailed ){
106816 if( pExpr->op==TK_COLLATE ){
106818 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
106824 if( ALWAYS(pExpr->y.pWin!=0) ){
106825 pExpr->y.pWin->pOwner = pExpr;
106835 ** and mark the expression-list item by setting ExprList.a[].fg.eEName
106838 ** Check to see if the zSpan/eEName of the expression-list item passed to this
106845 ** and setting zSpan to "DATABASE.TABLE.<rowid-alias>". This type of pItem
106858 int eEName = pItem->fg.eEName;
106863 zSpan = pItem->zEName;
106883 ** Return TRUE if the double-quoted string mis-feature should be supported.
106886 if( db->init.busy ) return 1; /* Always support for legacy schemas */
106887 if( pTopNC->ncFlags & NC_IsDDL ){
106889 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){
106892 return (db->flags & SQLITE_DqsDDL)!=0;
106895 return (db->flags & SQLITE_DqsDML)!=0;
106900 ** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
106907 n = pExpr->iColumn;
106909 pExTab = pExpr->y.pTab;
106911 assert( n < pExTab->nCol );
106912 if( (pExTab->tabFlags & TF_HasGenerated)!=0
106913 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
106915 testcase( pExTab->nCol==BMS-1 );
106916 testcase( pExTab->nCol==BMS );
106917 return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
106919 testcase( n==BMS-1 );
106921 if( n>=BMS ) n = BMS-1;
106938 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
106940 pNew->iTable = pMatch->iCursor;
106941 pNew->iColumn = iColumn;
106942 pNew->y.pTab = pMatch->pTab;
106943 assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 );
106950 ** Return TRUE (non-zero) if zTab is a valid name for the schema table pTab.
106955 const char *zDb /* non-NULL if a database qualifier is present */
106959 assert( pTab->tnum==1 );
106961 zLegacy = pTab->zName;
106981 ** pExpr->iDb Set the index in db->aDb[] of the database X
106983 ** pExpr->iTable Set to the cursor number for the table obtained
106985 ** pExpr->y.pTab Points to the Table structure of X.Y (even if
106987 ** pExpr->iColumn Set to the column number within the table.
106988 ** pExpr->op Set to TK_COLUMN.
106989 ** pExpr->pLeft Any expression this points to is deleted
106990 ** pExpr->pRight Any expression this points to is deleted.
107014 sqlite3 *db = pParse->db; /* The database connection */
107019 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
107023 const char *zCol = pRight->u.zToken;
107030 /* Initialize the node to no-match */
107031 pExpr->iTable = -1;
107039 testcase( pNC->ncFlags & NC_PartIdx );
107040 testcase( pNC->ncFlags & NC_IsCheck );
107041 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
107048 for(i=0; i<db->nDb; i++){
107049 assert( db->aDb[i].zDbSName );
107050 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
107051 pSchema = db->aDb[i].pSchema;
107055 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
107058 pSchema = db->aDb[0].pSchema;
107059 zDb = db->aDb[0].zDbSName;
107064 /* Start at the inner-most context and move outward until a match is found */
107068 SrcList *pSrcList = pNC->pSrcList;
107071 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
107073 pTab = pItem->pTab;
107074 assert( pTab!=0 && pTab->zName!=0 );
107075 assert( pTab->nCol>0 || pParse->nErr );
107076 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
107077 if( pItem->fg.isNestedFrom ){
107082 ** This pItem -------------^
107085 assert( pItem->pSelect!=0 );
107086 pEList = pItem->pSelect->pEList;
107088 assert( pEList->nExpr==pTab->nCol );
107089 for(j=0; j<pEList->nExpr; j++){
107091 if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb, &bRowid) ){
107096 if( pItem->fg.isUsing==0
107097 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
107105 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
107106 /* An INNER or LEFT JOIN. Use the left-most table */
107109 if( (pItem->fg.jointype & JT_LEFT)==0 ){
107110 /* A RIGHT JOIN. Use the right-most table */
107116 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
107128 pExpr->iColumn = j;
107129 pEList->a[j].fg.bUsed = 1;
107131 /* rowid cannot be part of a USING clause - assert() this. */
107132 assert( bRowid==0 || pEList->a[j].fg.bUsingTerm==0 );
107133 if( pEList->a[j].fg.bUsingTerm ) break;
107140 if( pTab->pSchema!=pSchema ) continue;
107143 if( pItem->zAlias!=0 ){
107144 if( sqlite3StrICmp(zTab, pItem->zAlias)!=0 ){
107147 }else if( sqlite3StrICmp(zTab, pTab->zName)!=0 ){
107148 if( pTab->tnum!=1 ) continue;
107152 if( IN_RENAME_OBJECT && pItem->zAlias ){
107153 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
107157 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
107158 if( pCol->hName==hCol
107159 && sqlite3StrICmp(pCol->zCnName, zCol)==0
107162 if( pItem->fg.isUsing==0
107163 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
107171 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
107172 /* An INNER or LEFT JOIN. Use the left-most table */
107175 if( (pItem->fg.jointype & JT_LEFT)==0 ){
107176 /* A RIGHT JOIN. Use the right-most table */
107182 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
107187 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
107188 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
107189 if( pItem->fg.isNestedFrom ){
107204 ** non-VIEW candidate plus multiple VIEW candidates. In other
107205 ** words non-VIEW candidate terms take precedence over VIEWs.
107210 && ALWAYS(pMatch->pTab!=0)
107211 && (pMatch->pTab->tabFlags & TF_Ephemeral)!=0
107212 && (pTab->tabFlags & TF_Ephemeral)==0)
107220 /* The (much more common) non-SQLITE_ALLOW_ROWID_IN_VIEW case is
107222 ** always be a non-VIEW
107230 pExpr->iTable = pMatch->iCursor;
107232 pExpr->y.pTab = pMatch->pTab;
107233 if( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ){
107236 pSchema = pExpr->y.pTab->pSchema;
107249 if( pParse->pTriggerTab!=0 ){
107250 int op = pParse->eTriggerOp;
107252 if( pParse->bReturning ){
107253 if( (pNC->ncFlags & NC_UBaseReg)!=0
107255 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0
107256 || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0))
107258 pExpr->iTable = op!=TK_DELETE;
107259 pTab = pParse->pTriggerTab;
107262 pExpr->iTable = 1;
107263 pTab = pParse->pTriggerTab;
107265 pExpr->iTable = 0;
107266 pTab = pParse->pTriggerTab;
107271 if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
107272 Upsert *pUpsert = pNC->uNC.pUpsert;
107274 pTab = pUpsert->pUpsertSrc->a[0].pTab;
107275 pExpr->iTable = EXCLUDED_TABLE_NUMBER;
107283 pSchema = pTab->pSchema;
107285 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
107286 if( pCol->hName==hCol
107287 && sqlite3StrICmp(pCol->zCnName, zCol)==0
107289 if( iCol==pTab->iPKey ){
107290 iCol = -1;
107295 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
107296 /* IMP: R-51414-32910 */
107297 iCol = -1;
107299 if( iCol<pTab->nCol ){
107303 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){
107304 testcase( iCol==(-1) );
107307 pExpr->iColumn = iCol;
107308 pExpr->y.pTab = pTab;
107311 pExpr->iTable = pNC->uNC.pUpsert->regData +
107319 pExpr->y.pTab = pTab;
107320 if( pParse->bReturning ){
107322 pExpr->op2 = TK_COLUMN;
107323 pExpr->iColumn = iCol;
107324 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
107327 pExpr->iColumn = (i16)iCol;
107331 pExpr->affExpr = SQLITE_AFF_INTEGER;
107332 }else if( pExpr->iTable==0 ){
107335 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
107339 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
107355 && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0
107357 && ALWAYS(VisibleRowid(pMatch->pTab) || pMatch->fg.isNestedFrom)
107361 if( pMatch->pTab!=0 && IsView(pMatch->pTab) ){
107365 if( pMatch->fg.isNestedFrom==0 ) pExpr->iColumn = -1;
107366 pExpr->affExpr = SQLITE_AFF_INTEGER;
107371 ** might refer to an result-set alias. This happens, for example, when
107381 ** The ability to use an output result-set column in the WHERE, GROUP BY,
107388 && (pNC->ncFlags & NC_UEList)!=0
107391 pEList = pNC->uNC.pEList;
107393 for(j=0; j<pEList->nExpr; j++){
107394 char *zAs = pEList->a[j].zEName;
107395 if( pEList->a[j].fg.eEName==ENAME_NAME
107399 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
107400 assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 );
107401 assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 );
107402 pOrig = pEList->a[j].pExpr;
107403 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
107408 && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )
107433 pNC = pNC->pNext;
107440 ** supplied) and the value of Z is enclosed in double-quotes, then
107445 ** Because no reference was made to outer contexts, the pNC->nRef
107449 assert( pExpr->op==TK_ID );
107453 /* If a double-quoted identifier does not match any known column name,
107457 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
107468 "double-quoted string literal: \"%w\"", zCol);
107470 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
107472 pExpr->op = TK_STRING;
107473 memset(&pExpr->y, 0, sizeof(pExpr->y));
107493 if( pFJMatch->nExpr==cnt-1 ){
107497 sqlite3ExprDelete(db, pExpr->pLeft);
107498 pExpr->pLeft = 0;
107499 sqlite3ExprDelete(db, pExpr->pRight);
107500 pExpr->pRight = 0;
107502 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
107503 pExpr->op = TK_FUNCTION;
107504 pExpr->u.zToken = "coalesce";
107505 pExpr->x.pList = pFJMatch;
107519 sqlite3ErrorMsg(pParse, "%s: \"%s\" - should this be a"
107520 " string literal in single-quotes?",
107525 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
107526 pParse->checkSchema = 1;
107527 pTopNC->nNcErr++;
107534 sqlite3ExprDelete(db, pExpr->pLeft);
107535 pExpr->pLeft = 0;
107536 sqlite3ExprDelete(db, pExpr->pRight);
107537 pExpr->pRight = 0;
107556 if( pExpr->iColumn>=0 ){
107557 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
107559 pMatch->fg.rowidUsed = 1;
107563 pExpr->op = eNewExprOp;
107568 if( pParse->db->xAuth
107569 && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER)
107571 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
107578 pTopNC->nRef++;
107580 pTopNC = pTopNC->pNext;
107595 SrcItem *pItem = &pSrc->a[iSrc];
107598 pTab = p->y.pTab = pItem->pTab;
107599 p->iTable = pItem->iCursor;
107600 if( p->y.pTab->iPKey==iCol ){
107601 p->iColumn = -1;
107603 p->iColumn = (ynVar)iCol;
107604 if( (pTab->tabFlags & TF_HasGenerated)!=0
107605 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0
107607 testcase( pTab->nCol==63 );
107608 testcase( pTab->nCol==64 );
107609 pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;
107612 testcase( iCol==BMS-1 );
107613 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
107622 ** pNC->ncFlags values determined by validMask.
107644 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
107646 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
107649 else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";
107652 if( pExpr ) pExpr->op = TK_NULL;
107653 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
107657 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R);
107661 ** Return 1024 times this value. Or return -1 if p is not a floating point
107665 double r = -1.0;
107666 if( p->op!=TK_FLOAT ) return -1;
107668 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
107670 if( r>1.0 ) return -1;
107689 pNC = pWalker->u.pNC;
107691 pParse = pNC->pParse;
107692 assert( pParse==pWalker->pParse );
107695 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
107696 SrcList *pSrcList = pNC->pSrcList;
107698 for(i=0; i<pNC->pSrcList->nSrc; i++){
107699 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
107703 switch( pExpr->op ){
107711 SrcList *pSrcList = pNC->pSrcList;
107713 assert( pSrcList && pSrcList->nSrc>=1 );
107714 pItem = pSrcList->a;
107715 pExpr->op = TK_COLUMN;
107717 pExpr->y.pTab = pItem->pTab;
107718 pExpr->iTable = pItem->iCursor;
107719 pExpr->iColumn--;
107720 pExpr->affExpr = SQLITE_AFF_INTEGER;
107726 ** "expr IS NOT NULL" --> "TRUE"
107727 ** "expr IS NULL" --> "FALSE"
107732 ** If this optimization occurs, also restore the NameContext ref-counts
107738 ** 2024-03-28: Beware of aggregates. A bare column of aggregated table
107756 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
107757 anRef[i] = p->nRef;
107759 sqlite3WalkExpr(pWalker, pExpr->pLeft);
107761 if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
107766 for(i=0, p=pNC; p; p=p->pNext, i++){
107767 if( (p->ncFlags & NC_Where)==0 ){
107777 pExpr->op==TK_NOTNULL
107782 pExpr->u.iValue = (pExpr->op==TK_NOTNULL);
107783 pExpr->flags |= EP_IntValue;
107784 pExpr->op = TK_INTEGER;
107785 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
107786 p->nRef = anRef[i];
107788 sqlite3ExprDelete(pParse->db, pExpr->pLeft);
107789 pExpr->pLeft = 0;
107798 ** be one call to lookupName(). Then the compiler will in-line
107807 if( pExpr->op==TK_ID ){
107813 Expr *pLeft = pExpr->pLeft;
107814 testcase( pNC->ncFlags & NC_IdxExpr );
107815 testcase( pNC->ncFlags & NC_GenCol );
107818 pRight = pExpr->pRight;
107819 if( pRight->op==TK_ID ){
107822 assert( pRight->op==TK_DOT );
107824 zDb = pLeft->u.zToken;
107825 pLeft = pRight->pLeft;
107826 pRight = pRight->pRight;
107829 zTable = pLeft->u.zToken;
107833 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
107842 ExprList *pList = pExpr->x.pList; /* The argument list */
107843 int n = pList ? pList->nExpr : 0; /* Number of arguments */
107849 u8 enc = ENC(pParse->db); /* The database encoding */
107850 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
107852 Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
107855 assert( pExpr->pLeft==0 || pExpr->pLeft->op==TK_ORDER );
107856 zId = pExpr->u.zToken;
107857 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
107859 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
107866 is_agg = pDef->xFinalize!=0;
107867 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
107870 pExpr->iTable = exprProbability(pList->a[1].pExpr);
107871 if( pExpr->iTable<0 ){
107875 pNC->nNcErr++;
107878 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
107880 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
107881 ** short-hand for likelihood(X,0.0625).
107882 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
107884 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
107887 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
107892 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
107897 pNC->nNcErr++;
107899 pExpr->op = TK_NULL;
107904 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
107911 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
107912 /* Clearly non-deterministic functions like random(), but also
107918 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
107922 pExpr->op2 = pNC->ncFlags & NC_SelfRef;
107923 if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
107925 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
107926 && pParse->nested==0
107927 && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0
107929 /* Internal-use-only functions are disallowed unless the
107931 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
107936 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
107945 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
107946 || (pDef->xValue==0 && pDef->xInverse==0)
107947 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
107949 if( pDef && pDef->xValue==0 && pWin ){
107953 pNC->nNcErr++;
107955 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
107956 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
107957 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
107960 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
107966 pNC->nNcErr++;
107970 if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
107972 pNC->nNcErr++;
107976 else if( no_such_func && pParse->db->init.busy==0
107978 && pParse->explain==0
107982 pNC->nNcErr++;
107986 pNC->nNcErr++;
107991 "FILTER may not be used with non-aggregate %#T()",
107994 pNC->nNcErr++;
107997 else if( is_agg==0 && pExpr->pLeft ){
107999 pNC->nNcErr++;
108006 pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
108008 pNC->ncFlags &= ~NC_AllowAgg;
108012 else if( ExprHasProperty(pExpr, EP_WinFunc) || pExpr->pLeft ){
108017 if( pExpr->pLeft ){
108018 assert( pExpr->pLeft->op==TK_ORDER );
108019 assert( ExprUseXList(pExpr->pLeft) );
108020 sqlite3WalkExprList(pWalker, pExpr->pLeft->x.pList);
108024 Select *pSel = pNC->pWinSelect;
108025 assert( pWin==0 || (ExprUseYWin(pExpr) && pWin==pExpr->y.pWin) );
108027 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
108028 if( pParse->db->mallocFailed ) break;
108030 sqlite3WalkExprList(pWalker, pWin->pPartition);
108031 sqlite3WalkExprList(pWalker, pWin->pOrderBy);
108032 sqlite3WalkExpr(pWalker, pWin->pFilter);
108034 pNC->ncFlags |= NC_HasWin;
108039 pExpr->op = TK_AGG_FUNCTION;
108040 pExpr->op2 = 0;
108043 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
108048 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0
108050 pExpr->op2 += (1 + pNC2->nNestedSelect);
108051 pNC2 = pNC2->pNext;
108055 pExpr->op2 += pNC2->nNestedSelect;
108058 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
108059 testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 );
108060 pNC2->ncFlags |= NC_HasAgg
108061 | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER)
108065 pNC->ncFlags |= savedAllowFlags;
108067 /* FIX ME: Compute pExpr->affinity based on the expected return
108074 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
108077 testcase( pExpr->op==TK_IN );
108079 int nRef = pNC->nRef;
108080 testcase( pNC->ncFlags & NC_IsCheck );
108081 testcase( pNC->ncFlags & NC_PartIdx );
108082 testcase( pNC->ncFlags & NC_IdxExpr );
108083 testcase( pNC->ncFlags & NC_GenCol );
108084 assert( pExpr->x.pSelect );
108085 if( pNC->ncFlags & NC_SelfRef ){
108088 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
108090 assert( pNC->nRef>=nRef );
108091 if( nRef!=pNC->nRef ){
108093 pExpr->x.pSelect->selFlags |= SF_Correlated;
108095 pNC->ncFlags |= NC_Subquery;
108100 testcase( pNC->ncFlags & NC_IsCheck );
108101 testcase( pNC->ncFlags & NC_PartIdx );
108102 testcase( pNC->ncFlags & NC_IdxExpr );
108103 testcase( pNC->ncFlags & NC_GenCol );
108110 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
108114 if( ALWAYS(pRight) && (pRight->op==TK_ID || pRight->op==TK_TRUEFALSE) ){
108117 if( pRight->op==TK_TRUEFALSE ){
108118 pExpr->op2 = pExpr->op;
108119 pExpr->op = TK_TRUTH;
108133 if( pParse->db->mallocFailed ) break;
108134 assert( pExpr->pLeft!=0 );
108135 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
108136 if( pExpr->op==TK_BETWEEN ){
108138 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
108140 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
108143 assert( pExpr->pRight!=0 );
108144 nRight = sqlite3ExprVectorSize(pExpr->pRight);
108147 testcase( pExpr->op==TK_EQ );
108148 testcase( pExpr->op==TK_NE );
108149 testcase( pExpr->op==TK_LT );
108150 testcase( pExpr->op==TK_LE );
108151 testcase( pExpr->op==TK_GT );
108152 testcase( pExpr->op==TK_GE );
108153 testcase( pExpr->op==TK_IS );
108154 testcase( pExpr->op==TK_ISNOT );
108155 testcase( pExpr->op==TK_BETWEEN );
108157 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
108162 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
108163 return pParse->nErr ? WRC_Abort : WRC_Continue;
108170 ** to the AS-name of one of the terms of the expression list. If it is,
108187 if( pE->op==TK_ID ){
108190 zCol = pE->u.zToken;
108191 for(i=0; i<pEList->nExpr; i++){
108192 if( pEList->a[i].fg.eEName==ENAME_NAME
108193 && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
108211 ** Attempt to match pE against result set columns in the left-most
108213 ** as an indication to the caller that it should sort by the i-th column.
108214 ** The left-most column is 1. In other words, the value returned is the
108218 ** If there is no match, return 0. Return -1 if an error occurs.
108230 u8 savedSuppErr; /* Saved value of db->suppressErr */
108233 pEList = pSelect->pEList;
108239 nc.pSrcList = pSelect->pSrc;
108243 db = pParse->db;
108244 savedSuppErr = db->suppressErr;
108245 db->suppressErr = 1;
108247 db->suppressErr = savedSuppErr;
108251 ** in the result set. Return an 1-based index of the matching
108252 ** result-set entry.
108254 for(i=0; i<pEList->nExpr; i++){
108255 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
108265 ** Generate an ORDER BY or GROUP BY term out-of-range error.
108270 int i, /* The index (1-based) of the term out of range */
108275 "%r %s BY term out of range - should be "
108277 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
108289 ** beginning with the left-most SELECT and working toward the right.
108305 pOrderBy = pSelect->pOrderBy;
108307 db = pParse->db;
108308 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
108312 for(i=0; i<pOrderBy->nExpr; i++){
108313 pOrderBy->a[i].fg.done = 0;
108315 pSelect->pNext = 0;
108316 while( pSelect->pPrior ){
108317 pSelect->pPrior->pNext = pSelect;
108318 pSelect = pSelect->pPrior;
108323 pEList = pSelect->pEList;
108325 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
108326 int iCol = -1;
108328 if( pItem->fg.done ) continue;
108329 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
108332 if( iCol<=0 || iCol>pEList->nExpr ){
108333 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
108351 if( !db->mallocFailed ){
108367 pNew->flags |= EP_IntValue;
108368 pNew->u.iValue = iCol;
108369 if( pItem->pExpr==pE ){
108370 pItem->pExpr = pNew;
108372 Expr *pParent = pItem->pExpr;
108373 assert( pParent->op==TK_COLLATE );
108374 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
108375 assert( pParent->pLeft==pE );
108376 pParent->pLeft = pNew;
108379 pItem->u.x.iOrderByCol = (u16)iCol;
108381 pItem->fg.done = 1;
108386 pSelect = pSelect->pNext;
108388 for(i=0; i<pOrderBy->nExpr; i++){
108389 if( pOrderBy->a[i].fg.done==0 ){
108406 ** return non-zero. Return zero if no errors are seen.
108415 sqlite3 *db = pParse->db;
108419 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
108420 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
108424 pEList = pSelect->pEList;
108426 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
108427 if( pItem->u.x.iOrderByCol ){
108428 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
108429 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0);
108432 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0);
108445 Window *pWin = pExpr->y.pWin;
108456 if( pSelect->pWin ){
108474 ** If the order-by term is an integer I between 1 and N (where N is the
108476 ** in the resolution is a copy of the I-th result-set expression. If
108477 ** the order-by term is an identifier that corresponds to the AS-name of
108478 ** a result-set expression, then the term resolves to a copy of the
108479 ** result-set expression. Otherwise, the expression is resolved in
108480 ** the usual way - using sqlite3ResolveExprNames().
108499 nResult = pSelect->pEList->nExpr;
108500 pParse = pNC->pParse;
108501 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
108502 Expr *pE = pItem->pExpr;
108506 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
108508 /* If an AS-name match is found, mark this ORDER BY column as being
108509 ** a copy of the iCol-th result-set column. The subsequent call to
108511 ** copy of the iCol-th result-set expression. */
108512 pItem->u.x.iOrderByCol = (u16)iCol;
108519 ** order-by term to a copy of the result-set expression */
108524 pItem->u.x.iOrderByCol = (u16)iCol;
108529 pItem->u.x.iOrderByCol = 0;
108533 for(j=0; j<pSelect->pEList->nExpr; j++){
108534 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
108539 pItem->u.x.iOrderByCol = j+1;
108557 Select *pLeftmost; /* Left-most of SELECT of a compound */
108562 if( p->selFlags & SF_Resolved ){
108565 pOuterNC = pWalker->u.pNC;
108566 pParse = pWalker->pParse;
108567 db = pParse->db;
108577 if( (p->selFlags & SF_Expanded)==0 ){
108579 return pParse->nErr ? WRC_Abort : WRC_Prune;
108582 isCompound = p->pPrior!=0;
108586 assert( (p->selFlags & SF_Expanded)!=0 );
108587 assert( (p->selFlags & SF_Resolved)==0 );
108588 p->selFlags |= SF_Resolved;
108596 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
108602 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
108603 ** as if it were part of the sub-query, not the parent. This block
108604 ** moves the pOrderBy down to the sub-query. It will be moved back
108606 if( p->selFlags & SF_Converted ){
108607 Select *pSub = p->pSrc->a[0].pSelect;
108608 assert( p->pSrc->nSrc==1 && p->pOrderBy );
108609 assert( pSub->pPrior && pSub->pOrderBy==0 );
108610 pSub->pOrderBy = p->pOrderBy;
108611 p->pOrderBy = 0;
108616 if( pOuterNC ) pOuterNC->nNestedSelect++;
108617 for(i=0; i<p->pSrc->nSrc; i++){
108618 SrcItem *pItem = &p->pSrc->a[i];
108619 assert( pItem->zName!=0 || pItem->pSelect!=0 );/* Test of tag-20240424-1*/
108620 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
108621 int nRef = pOuterNC ? pOuterNC->nRef : 0;
108622 const char *zSavedContext = pParse->zAuthContext;
108624 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
108625 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
108626 pParse->zAuthContext = zSavedContext;
108627 if( pParse->nErr ) return WRC_Abort;
108628 assert( db->mallocFailed==0 );
108631 ** expressions in the sub-select were resolved, the sub-select
108637 assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef );
108638 pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
108642 if( pOuterNC && ALWAYS(pOuterNC->nNestedSelect>0) ){
108643 pOuterNC->nNestedSelect--;
108646 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
108647 ** resolve the result-set expression list.
108650 sNC.pSrcList = p->pSrc;
108654 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
108657 /* If there are no aggregate functions in the result-set, and no GROUP BY
108660 assert( (p->selFlags & SF_Aggregate)==0 );
108661 pGroupBy = p->pGroupBy;
108665 p->selFlags |= SF_Aggregate | (sNC.ncFlags&(NC_MinMaxAgg|NC_OrderAgg));
108670 /* Add the output column list to the name-context before parsing the
108676 ** re-evaluated for each reference to it.
108679 sNC.uNC.pEList = p->pEList;
108681 if( p->pHaving ){
108682 if( (p->selFlags & SF_Aggregate)==0 ){
108683 sqlite3ErrorMsg(pParse, "HAVING clause on a non-aggregate query");
108686 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
108689 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
108692 /* Resolve names in table-valued-function arguments */
108693 for(i=0; i<p->pSrc->nSrc; i++){
108694 SrcItem *pItem = &p->pSrc->a[i];
108695 if( pItem->fg.isTabFunc
108696 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
108705 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
108706 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
108707 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
108722 ** the sub-query back to the parent query. At this point each term
108726 if( p->selFlags & SF_Converted ){
108727 Select *pSub = p->pSrc->a[0].pSelect;
108728 p->pOrderBy = pSub->pOrderBy;
108729 pSub->pOrderBy = 0;
108734 ** below, after all of the result-sets for all of the elements of
108737 ** If there is an ORDER BY clause on a term of a compound-select other
108738 ** than the right-most term, then that is a syntax error. But the error
108742 if( p->pOrderBy!=0
108743 && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
108744 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
108748 if( db->mallocFailed ){
108759 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
108762 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
108763 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
108773 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
108774 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
108780 p = p->pPrior;
108796 ** table columns and result-set columns. At the same time, do error
108804 ** the symbolic name assigned to an ATTACH-ed database.
108815 ** Expr.iColumn The column index in X.Y. -1 for the rowid.
108819 ** To resolve result-set references, look for expression nodes of the
108820 ** form Z (with no X and Y prefix) where the Z matches the right-hand
108821 ** size of an AS clause in the result-set of a SELECT. The Z expression
108822 ** is replaced by a copy of the left-hand side of the result-set expression.
108823 ** Table-name and function resolution occurs on the substituted expression
108850 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
108851 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
108852 w.pParse = pNC->pParse;
108854 w.xSelectCallback = (pNC->ncFlags & NC_NoSelect) ? 0 : resolveSelectStep;
108858 w.pParse->nHeight += pExpr->nHeight;
108859 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
108866 w.pParse->nHeight -= pExpr->nHeight;
108870 testcase( pNC->ncFlags & NC_HasAgg );
108871 testcase( pNC->ncFlags & NC_HasWin );
108872 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
108873 pNC->ncFlags |= savedHasAgg;
108874 return pNC->nNcErr>0 || w.pParse->nErr>0;
108893 w.pParse = pNC->pParse;
108898 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
108899 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
108900 for(i=0; i<pList->nExpr; i++){
108901 Expr *pExpr = pList->a[i].pExpr;
108904 w.pParse->nHeight += pExpr->nHeight;
108905 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
108911 w.pParse->nHeight -= pExpr->nHeight;
108915 testcase( pNC->ncFlags & NC_HasAgg );
108916 testcase( pNC->ncFlags & NC_HasWin );
108917 if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg) ){
108918 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
108919 savedHasAgg |= pNC->ncFlags &
108921 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
108923 if( w.pParse->nErr>0 ) return SQLITE_ERROR;
108925 pNC->ncFlags |= savedHasAgg;
108931 ** descendants of the SELECT, including compounds off of p->pPrior,
108962 ** ------------
108970 ** nodes of the expression is set to -1 and the Expr.iColumn value is
108982 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
108983 NameContext sNC; /* Name context for pParse->pNewTable */
108993 sSrc.a[0].zName = pTab->zName;
108995 sSrc.a[0].iCursor = -1;
108996 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
108997 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
109036 if( iCol<0 || NEVER(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER;
109037 return pTab->aCol[iCol].affinity;
109044 ** or a sub-select with a column as the return value, then the
109058 op = pExpr->op;
109059 while( 1 /* exit-by-break */ ){
109060 if( op==TK_COLUMN || (op==TK_AGG_COLUMN && pExpr->y.pTab!=0) ){
109062 assert( pExpr->y.pTab!=0 );
109063 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
109067 assert( pExpr->x.pSelect!=0 );
109068 assert( pExpr->x.pSelect->pEList!=0 );
109069 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 );
109070 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
109075 return sqlite3AffinityType(pExpr->u.zToken, 0);
109079 assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) );
109080 assert( pExpr->iColumn < pExpr->iTable );
109081 assert( pExpr->iColumn >= 0 );
109082 assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr );
109084 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
109089 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
109092 assert( pExpr->op==TK_COLLATE
109093 || pExpr->op==TK_IF_NULL_ROW
109094 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) );
109095 pExpr = pExpr->pLeft;
109096 op = pExpr->op;
109099 if( op!=TK_REGISTER || (op = pExpr->op2)==TK_REGISTER ) break;
109101 return pExpr->affExpr;
109116 switch( pExpr->op ){
109120 pExpr = pExpr->pLeft;
109155 ExprList *pList = pExpr->x.pList;
109157 assert( pList->nExpr > 0);
109158 for(ii=1; ii<pList->nExpr; ii+=2){
109159 res |= sqlite3ExprDataType(pList->a[ii].pExpr);
109161 if( pList->nExpr % 2 ){
109162 res |= sqlite3ExprDataType(pList->a[pList->nExpr-1].pExpr);
109179 ** If a memory allocation error occurs, that fact is recorded in pParse->db
109188 if( pCollName->n>0 ){
109189 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
109191 pNew->pLeft = pExpr;
109192 pNew->flags |= EP_Collate|EP_Skip;
109214 assert( pExpr->op==TK_COLLATE );
109215 pExpr = pExpr->pLeft;
109229 assert( pExpr->x.pList->nExpr>0 );
109230 assert( pExpr->op==TK_FUNCTION );
109231 pExpr = pExpr->x.pList->a[0].pExpr;
109232 }else if( pExpr->op==TK_COLLATE ){
109233 pExpr = pExpr->pLeft;
109256 sqlite3 *db = pParse->db;
109260 int op = p->op;
109261 if( op==TK_REGISTER ) op = p->op2;
109262 if( (op==TK_AGG_COLUMN && p->y.pTab!=0)
109267 assert( p->y.pTab!=0 );
109268 if( (j = p->iColumn)>=0 ){
109269 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
109275 p = p->pLeft;
109280 p = p->x.pList->a[0].pExpr;
109285 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
109288 if( p->flags & EP_Collate ){
109289 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
109290 p = p->pLeft;
109292 Expr *pNext = p->pRight;
109294 assert( !ExprUseXList(p) || p->x.pList==0 || p->pRight==0 );
109295 if( ExprUseXList(p) && p->x.pList!=0 && !db->mallocFailed ){
109297 for(i=0; i<p->x.pList->nExpr; i++){
109298 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
109299 pNext = p->x.pList->a[i].pExpr;
109328 if( p==0 ) p = pParse->db->pDfltColl;
109339 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
109371 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
109372 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
109373 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
109374 assert( pExpr->pLeft );
109375 aff = sqlite3ExprAffinity(pExpr->pLeft);
109376 if( pExpr->pRight ){
109377 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
109379 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
109436 if( pLeft->flags & EP_Collate ){
109438 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
109459 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
109461 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
109482 if( pParse->nErr ) return 0;
109489 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
109491 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
109511 ** is a sub-select, return the number of columns in the sub-select. For
109515 u8 op = pExpr->op;
109516 if( op==TK_REGISTER ) op = pExpr->op2;
109519 return pExpr->x.pList->nExpr;
109522 return pExpr->x.pSelect->pEList->nExpr;
109529 ** Return a pointer to a subexpression of pVector that is the i-th
109539 ** just the expression for the i-th term of the result set, and may
109544 assert( i<sqlite3ExprVectorSize(pVector) || pVector->op==TK_ERROR );
109546 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
109547 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
109549 return pVector->x.pSelect->pEList->a[i].pExpr;
109552 return pVector->x.pList->a[i].pExpr;
109561 ** the iField-th column of the vector expression pVector.
109581 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
109586 if( pVector->op==TK_SELECT ){
109594 ** pLeft->iTable: First in an array of register holding result, or 0
109607 pRet->iTable = nField;
109608 pRet->iColumn = iField;
109609 pRet->pLeft = pVector;
109612 if( pVector->op==TK_VECTOR ){
109615 ppVector = &pVector->x.pList->a[iField].pExpr;
109623 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
109631 ** sub-select returns more than one column, the first in an array
109639 if( pExpr->op==TK_SELECT ){
109647 ** Argument pVector points to a vector expression - either a TK_VECTOR
109655 ** containing the results of the sub-select.
109672 u8 op = pVector->op;
109676 return pVector->iTable+iField;
109680 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
109685 *ppExpr = pVector->x.pList->a[iField].pExpr;
109698 ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
109699 ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
109700 ** otherwise: op==pExpr->op and p5==0
109709 Vdbe *v = pParse->pVdbe;
109710 Expr *pLeft = pExpr->pLeft;
109711 Expr *pRight = pExpr->pRight;
109722 if( pParse->nErr ) return;
109727 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
109728 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
109729 || pExpr->op==TK_LT || pExpr->op==TK_GT
109730 || pExpr->op==TK_LE || pExpr->op==TK_GE
109732 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
109733 || (pExpr->op==TK_ISNOT && op==TK_NE) );
109734 assert( p5==0 || pExpr->op!=op );
109735 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
109763 if( (opx==TK_LT || opx==TK_GT) && i<nLeft-1 ){
109773 if( i==nLeft-1 ){
109781 if( i==nLeft-2 ) opx = op;
109799 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
109820 if( p->nHeight>*pnHeight ){
109821 *pnHeight = p->nHeight;
109828 for(i=0; i<p->nExpr; i++){
109829 heightOfExpr(p->a[i].pExpr, pnHeight);
109835 for(p=pSelect; p; p=p->pPrior){
109836 heightOfExpr(p->pWhere, pnHeight);
109837 heightOfExpr(p->pHaving, pnHeight);
109838 heightOfExpr(p->pLimit, pnHeight);
109839 heightOfExprList(p->pEList, pnHeight);
109840 heightOfExprList(p->pGroupBy, pnHeight);
109841 heightOfExprList(p->pOrderBy, pnHeight);
109856 int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
109857 if( NEVER(p->pRight) && p->pRight->nHeight>nHeight ){
109858 nHeight = p->pRight->nHeight;
109861 heightOfSelect(p->x.pSelect, &nHeight);
109862 }else if( p->x.pList ){
109863 heightOfExprList(p->x.pList, &nHeight);
109864 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
109866 p->nHeight = nHeight + 1;
109878 if( pParse->nErr ) return;
109880 sqlite3ExprCheckHeight(pParse, p->nHeight);
109898 if( pParse->nErr ) return;
109899 if( p && ExprUseXList(p) && p->x.pList ){
109900 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
109912 pExpr->w.iOfst = iOfst;
109926 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
109929 ** Special case (tag-20240227-a): If op==TK_INTEGER and pToken points to
109930 ** a string that can be translated into a 32-bit integer, then the token is
109934 ** See also tag-20240227-b.
109948 if( op!=TK_INTEGER || pToken->z==0
109949 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
109950 nExtra = pToken->n+1; /* tag-20240227-a */
109957 pNew->op = (u8)op;
109958 pNew->iAgg = -1;
109961 pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
109962 pNew->u.iValue = iValue;
109964 pNew->u.zToken = (char*)&pNew[1];
109965 assert( pToken->z!=0 || pToken->n==0 );
109966 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
109967 pNew->u.zToken[pToken->n] = 0;
109968 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
109974 pNew->nHeight = 1;
109981 ** Allocate a new expression node from a zero-terminated token that has
110008 assert( db->mallocFailed );
110013 assert( pRoot->x.pSelect==0 );
110015 pRoot->pRight = pRight;
110016 pRoot->flags |= EP_Propagate & pRight->flags;
110018 pRoot->nHeight = pRight->nHeight+1;
110020 pRoot->nHeight = 1;
110024 pRoot->pLeft = pLeft;
110025 pRoot->flags |= EP_Propagate & pLeft->flags;
110027 if( pLeft->nHeight>=pRoot->nHeight ){
110028 pRoot->nHeight = pLeft->nHeight+1;
110039 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
110049 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
110052 p->op = op & 0xff;
110053 p->iAgg = -1;
110054 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
110055 sqlite3ExprCheckHeight(pParse, p->nHeight);
110057 sqlite3ExprDelete(pParse->db, pLeft);
110058 sqlite3ExprDelete(pParse->db, pRight);
110069 pExpr->x.pSelect = pSelect;
110073 assert( pParse->db->mallocFailed );
110074 sqlite3SelectDelete(pParse->db, pSelect);
110101 for(ii=0; ii<pEList->nExpr; ii++){
110103 Expr *pExpr = pEList->a[ii].pExpr;
110105 if( pExpr->op==TK_VECTOR ){
110107 nExprElem = pExpr->x.pList->nExpr;
110112 sqlite3ErrorMsg(pParse, "IN(...) element has %d term%s - expected %d",
110118 pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values,0);
110119 pExpr->x.pList = 0;
110122 pSel->op = TK_ALL;
110123 pSel->pPrior = pRet;
110129 if( pRet && pRet->pPrior ){
110130 pRet->selFlags |= SF_MultiValue;
110132 sqlite3ExprListDelete(pParse->db, pEList);
110145 sqlite3 *db = pParse->db;
110151 u32 f = pLeft->flags | pRight->flags;
110175 sqlite3 *db = pParse->db;
110183 pNew->w.iOfst = (int)(pToken->z - pParse->zTail);
110185 && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG]
110186 && !pParse->nested
110190 pNew->x.pList = pList;
110200 ** the arguments of a non-aggregate function.
110204 "ORDER BY may not be used with non-aggregate %#T()", p
110224 sqlite3 *db = pParse->db;
110226 assert( db->mallocFailed );
110230 assert( db->mallocFailed );
110234 assert( pExpr->op==TK_FUNCTION );
110235 assert( pExpr->pLeft==0 );
110237 if( pExpr->x.pList==0 || NEVER(pExpr->x.pList->nExpr==0) ){
110238 /* Ignore ORDER BY on zero-argument aggregates */
110253 pOB->x.pList = pOrderBy;
110255 pExpr->pLeft = pOB;
110263 ** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
110265 ** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
110266 ** top-level SQL
110276 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 );
110278 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0
110279 || (pParse->db->flags & SQLITE_TrustedSchema)==0
110310 sqlite3 *db = pParse->db;
110316 z = pExpr->u.zToken;
110323 x = (ynVar)(++pParse->nVar);
110331 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
110332 i = z[1]-'0'; /* The common case of ?N for a single digit N */
110335 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
110339 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
110340 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
110341 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
110343 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
110344 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
110348 if( x>pParse->nVar ){
110349 pParse->nVar = (int)x;
110351 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
110359 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
110361 x = (ynVar)(++pParse->nVar);
110366 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
110369 pExpr->iColumn = x;
110370 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
110372 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
110383 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
110385 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
110386 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
110389 assert( p->pLeft==0 );
110390 assert( p->pRight==0 );
110391 assert( !ExprUseXSelect(p) || p->x.pSelect==0 );
110392 assert( !ExprUseXList(p) || p->x.pList==0 );
110397 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 );
110398 if( p->pRight ){
110400 sqlite3ExprDeleteNN(db, p->pRight);
110403 sqlite3SelectDelete(db, p->x.pSelect);
110405 sqlite3ExprListDelete(db, p->x.pList);
110408 sqlite3WindowDelete(db, p->y.pWin);
110412 if( p->pLeft && p->op!=TK_SELECT_COLUMN ){
110413 Expr *pLeft = p->pLeft;
110443 }else if( p->pOn ){
110444 sqlite3ExprDeleteNN(db, p->pOn);
110445 }else if( p->pUsing ){
110446 sqlite3IdListDelete(db, p->pUsing);
110457 ** Return 0 if the delete was successfully deferred. Return non-zero
110472 sqlite3ExprDeleteNN(pParse->db, p);
110498 ** The dupedExprStructSize() function returns two values OR-ed together:
110511 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
110532 if( p->pLeft || p->x.pList ){
110535 assert( p->pRight==0 );
110549 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
110550 nByte += sqlite3Strlen30NN(p->u.zToken)+1;
110570 if( p->pLeft ) nByte += dupedExprSize(p->pLeft);
110571 if( p->pRight ) nByte += dupedExprSize(p->pRight);
110579 ** compression while doing sqlite3ExprDup(). The top-level Expr does the
110595 ** of the input Expr p together with its p->u.zToken (if any). pEdupBuf
110607 int nToken = -1; /* Space needed for p->u.zToken. -1 means unknown */
110616 sEdupBuf.zAlloc = pEdupBuf->zAlloc;
110618 sEdupBuf.zEnd = pEdupBuf->zEnd;
110627 }else if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
110628 nToken = sqlite3Strlen30NN(p->u.zToken)+1;
110649 ** by the copy of the p->u.zToken string (if any).
110654 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
110655 nToken = sqlite3Strlen30(p->u.zToken) + 1;
110661 assert( (int)(sEdupBuf.zEnd - sEdupBuf.zAlloc) >= nNewSize+nToken );
110666 assert( (int)(sEdupBuf.zEnd - sEdupBuf.zAlloc) >=
110670 memset(&sEdupBuf.zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
110676 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
110677 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
110678 pNew->flags |= staticFlag;
110684 /* Copy the p->u.zToken string, if any. */
110687 char *zToken = pNew->u.zToken = (char*)&sEdupBuf.zAlloc[nNewSize];
110688 memcpy(zToken, p->u.zToken, nToken);
110693 if( ((p->flags|pNew->flags)&(EP_TokenOnly|EP_Leaf))==0 ){
110695 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
110697 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
110699 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList,
110700 p->op!=TK_ORDER ? dupFlags : 0);
110705 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
110710 /* Fill in pNew->pLeft and pNew->pRight. */
110712 if( p->op==TK_SELECT_COLUMN ){
110713 pNew->pLeft = p->pLeft;
110714 assert( p->pRight==0
110715 || p->pRight==p->pLeft
110716 || ExprHasProperty(p->pLeft, EP_Subquery) );
110718 pNew->pLeft = p->pLeft ?
110719 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &sEdupBuf) : 0;
110721 pNew->pRight = p->pRight ?
110722 exprDup(db, p->pRight, EXPRDUP_REDUCE, &sEdupBuf) : 0;
110724 if( p->op==TK_SELECT_COLUMN ){
110725 pNew->pLeft = p->pLeft;
110726 assert( p->pRight==0
110727 || p->pRight==p->pLeft
110728 || ExprHasProperty(p->pLeft, EP_Subquery) );
110730 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
110732 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
110744 ** and the db->mallocFailed flag set.
110750 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
110754 pRet->nCte = p->nCte;
110755 for(i=0; i<p->nCte; i++){
110756 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
110757 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
110758 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
110759 pRet->a[i].eM10d = p->a[i].eM10d;
110774 ** objects found there, assembling them onto the linked list at Select->pWin.
110777 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
110778 Select *pSelect = pWalker->u.pSelect;
110779 Window *pWin = pExpr->y.pWin;
110782 assert( pWin->ppThis==0 );
110788 return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
110817 ** part of the in-memory representation of the database schema.
110834 pNew->nExpr = p->nExpr;
110835 pNew->nAlloc = p->nAlloc;
110836 pItem = pNew->a;
110837 pOldItem = p->a;
110838 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
110839 Expr *pOldExpr = pOldItem->pExpr;
110841 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
110843 && pOldExpr->op==TK_SELECT_COLUMN
110844 && (pNewExpr = pItem->pExpr)!=0
110846 if( pNewExpr->pRight ){
110847 pPriorSelectColOld = pOldExpr->pRight;
110848 pPriorSelectColNew = pNewExpr->pRight;
110849 pNewExpr->pLeft = pNewExpr->pRight;
110851 if( pOldExpr->pLeft!=pPriorSelectColOld ){
110852 pPriorSelectColOld = pOldExpr->pLeft;
110854 pNewExpr->pRight = pPriorSelectColNew;
110856 pNewExpr->pLeft = pPriorSelectColNew;
110859 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
110860 pItem->fg = pOldItem->fg;
110861 pItem->fg.done = 0;
110862 pItem->u = pOldItem->u;
110881 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
110884 pNew->nSrc = pNew->nAlloc = p->nSrc;
110885 for(i=0; i<p->nSrc; i++){
110886 SrcItem *pNewItem = &pNew->a[i];
110887 const SrcItem *pOldItem = &p->a[i];
110889 pNewItem->pSchema = pOldItem->pSchema;
110890 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
110891 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
110892 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
110893 pNewItem->fg = pOldItem->fg;
110894 pNewItem->iCursor = pOldItem->iCursor;
110895 pNewItem->addrFillSub = pOldItem->addrFillSub;
110896 pNewItem->regReturn = pOldItem->regReturn;
110897 pNewItem->regResult = pOldItem->regResult;
110898 if( pNewItem->fg.isIndexedBy ){
110899 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
110900 }else if( pNewItem->fg.isTabFunc ){
110901 pNewItem->u1.pFuncArg =
110902 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
110904 pNewItem->u1.nRow = pOldItem->u1.nRow;
110906 pNewItem->u2 = pOldItem->u2;
110907 if( pNewItem->fg.isCte ){
110908 pNewItem->u2.pCteUse->nUse++;
110910 pTab = pNewItem->pTab = pOldItem->pTab;
110912 pTab->nTabRef++;
110914 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
110915 if( pOldItem->fg.isUsing ){
110916 assert( pNewItem->fg.isUsing );
110917 pNewItem->u3.pUsing = sqlite3IdListDup(db, pOldItem->u3.pUsing);
110919 pNewItem->u3.pOn = sqlite3ExprDup(db, pOldItem->u3.pOn, flags);
110921 pNewItem->colUsed = pOldItem->colUsed;
110930 assert( p->eU4!=EU4_EXPR );
110931 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew)+(p->nId-1)*sizeof(p->a[0]) );
110933 pNew->nId = p->nId;
110934 pNew->eU4 = p->eU4;
110935 for(i=0; i<p->nId; i++){
110936 struct IdList_item *pNewItem = &pNew->a[i];
110937 const struct IdList_item *pOldItem = &p->a[i];
110938 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
110939 pNewItem->u4 = pOldItem->u4;
110950 for(p=pDup; p; p=p->pPrior){
110953 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
110954 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
110955 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
110956 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
110957 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
110958 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
110959 pNew->op = p->op;
110960 pNew->pNext = pNext;
110961 pNew->pPrior = 0;
110962 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
110963 pNew->iLimit = 0;
110964 pNew->iOffset = 0;
110965 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
110966 pNew->addrOpenEphm[0] = -1;
110967 pNew->addrOpenEphm[1] = -1;
110968 pNew->nSelectRow = p->nSelectRow;
110969 pNew->pWith = sqlite3WithDup(db, p->pWith);
110971 pNew->pWin = 0;
110972 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
110973 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
110975 pNew->selId = p->selId;
110976 if( db->mallocFailed ){
110980 pNew->pNext = 0;
110985 pp = &pNew->pPrior;
111007 ** NULL is returned. If non-NULL is returned, then it is guaranteed
111018 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList)+sizeof(pList->a[0])*4 );
111023 pList->nAlloc = 4;
111024 pList->nExpr = 1;
111025 pItem = &pList->a[0];
111027 pItem->pExpr = pExpr;
111037 pList->nAlloc *= 2;
111039 sizeof(*pList)+(pList->nAlloc-1)*sizeof(pList->a[0]));
111047 pItem = &pList->a[pList->nExpr++];
111049 pItem->pExpr = pExpr;
111059 return sqlite3ExprListAppendNew(pParse->db,pExpr);
111061 if( pList->nAlloc<pList->nExpr+1 ){
111062 return sqlite3ExprListAppendGrow(pParse->db,pList,pExpr);
111064 pItem = &pList->a[pList->nExpr++];
111066 pItem->pExpr = pExpr;
111087 sqlite3 *db = pParse->db;
111090 int iFirst = pList ? pList->nExpr : 0;
111101 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
111103 pColumns->nId, n);
111107 for(i=0; i<pColumns->nId; i++){
111108 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i, pColumns->nId);
111109 assert( pSubExpr!=0 || db->mallocFailed );
111113 assert( pList->nExpr==iFirst+i+1 );
111114 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
111115 pColumns->a[i].zName = 0;
111119 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
111120 Expr *pFirst = pList->a[iFirst].pExpr;
111122 assert( pFirst->op==TK_SELECT_COLUMN );
111126 pFirst->pRight = pExpr;
111131 pFirst->iTable = pColumns->nId;
111146 assert( p->nExpr>0 );
111158 pItem = &p->a[p->nExpr-1];
111159 assert( pItem->fg.bNulls==0 );
111163 pItem->fg.sortFlags = (u8)iSortOrder;
111166 pItem->fg.bNulls = 1;
111168 pItem->fg.sortFlags |= KEYINFO_ORDER_BIGNULL;
111178 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
111187 assert( pList!=0 || pParse->db->mallocFailed!=0 );
111188 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 );
111191 assert( pList->nExpr>0 );
111192 pItem = &pList->a[pList->nExpr-1];
111193 assert( pItem->zEName==0 );
111194 assert( pItem->fg.eEName==ENAME_NAME );
111195 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
111197 /* If dequote==0, then pName->z does not point to part of a DDL
111199 ** to the token-map. */
111200 sqlite3Dequote(pItem->zEName);
111202 sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName);
111213 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
111222 sqlite3 *db = pParse->db;
111223 assert( pList!=0 || db->mallocFailed!=0 );
111225 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
111226 assert( pList->nExpr>0 );
111227 if( pItem->zEName==0 ){
111228 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
111229 pItem->fg.eEName = ENAME_SPAN;
111243 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
111244 testcase( pEList && pEList->nExpr==mx );
111245 testcase( pEList && pEList->nExpr==mx+1 );
111246 if( pEList && pEList->nExpr>mx ){
111255 int i = pList->nExpr;
111256 struct ExprList_item *pItem = pList->a;
111257 assert( pList->nExpr>0 );
111260 sqlite3ExprDelete(db, pItem->pExpr);
111261 if( pItem->zEName ) sqlite3DbNNFreeNN(db, pItem->zEName);
111263 }while( --i>0 );
111274 ** Return the bitwise-OR of all Expr.flags fields in the given
111281 for(i=0; i<pList->nExpr; i++){
111282 Expr *pExpr = pList->a[i].pExpr;
111284 m |= pExpr->flags;
111290 ** This is a SELECT-node callback for the expression walker that
111292 ** pWalker->eCode to zero and abort.
111298 pWalker->eCode = 0;
111319 ** then convert it into an TK_TRUEFALSE term. Return non-zero if
111324 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
111326 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
111328 pExpr->op = TK_TRUEFALSE;
111341 assert( pExpr->op==TK_TRUEFALSE );
111343 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
111344 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
111345 return pExpr->u.zToken[4]==0;
111363 if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
111364 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
111365 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
111367 pExpr = pExpr->op==TK_AND ? pRight : pLeft;
111369 pExpr = pExpr->op==TK_AND ? pLeft : pRight;
111385 ** This routine sets pWalker->eCode to 0 if pExpr is not a constant.
111386 ** It makes no changes to pWalker->eCode if pExpr is constant. In
111400 assert( pExpr->op==TK_FUNCTION );
111402 || (pList = pExpr->x.pList)==0
111406 n = pList->nExpr;
111408 if( pWalker->eCode==0 ) return WRC_Abort;
111410 db = pWalker->pParse->db;
111411 pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
111413 || pDef->xFinalize!=0
111414 || (pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
111417 pWalker->eCode = 0;
111432 ** sqlite3ExprIsConstant() pWalker->eCode==1
111433 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
111434 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
111435 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
111451 assert( pWalker->eCode>0 );
111453 /* If pWalker->eCode is 2 then any term of the expression that comes from
111456 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_OuterON) ){
111457 pWalker->eCode = 0;
111461 switch( pExpr->op ){
111463 ** and either pWalker->eCode==4 or 5 or the function has the
111466 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc))
111469 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL);
111471 }else if( pWalker->pParse ){
111474 pWalker->eCode = 0;
111487 testcase( pExpr->op==TK_ID );
111488 testcase( pExpr->op==TK_COLUMN );
111489 testcase( pExpr->op==TK_AGG_FUNCTION );
111490 testcase( pExpr->op==TK_AGG_COLUMN );
111491 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
111494 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
111502 testcase( pExpr->op==TK_REGISTER );
111503 testcase( pExpr->op==TK_IF_NULL_ROW );
111504 testcase( pExpr->op==TK_DOT );
111505 testcase( pExpr->op==TK_RAISE );
111506 pWalker->eCode = 0;
111509 if( pWalker->eCode==5 ){
111513 pExpr->op = TK_NULL;
111514 }else if( pWalker->eCode==4 ){
111517 pWalker->eCode = 0;
111522 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
111523 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
111541 ** Walk an expression tree. Return non-zero if the expression is constant
111544 ** For the purposes of this function, a double-quoted string (ex: "abc")
111545 ** is considered a variable but a single-quoted string (ex: 'abc') is
111550 ** function calls will be considered to be non-constant. If pParse is
111559 ** Walk an expression tree. Return non-zero if
111568 ** can be added to the pParse->pConstExpr list and evaluated once when
111576 ** This routine examines sub-SELECT statements as an expression is being
111577 ** walked as part of sqlite3ExprIsTableConstant(). Sub-SELECTs are considered
111578 ** constant as long as they are uncorrelated - meaning that they do not
111583 assert( pWalker->eCode==3 || pWalker->eCode==0 );
111584 if( (pSelect->selFlags & SF_Correlated)!=0 ){
111585 pWalker->eCode = 0;
111592 ** Walk an expression tree. Return non-zero if the expression is constant
111594 ** expression must not refer to any non-deterministic function nor any
111620 ** pSrc = &pSrcList->a[iSrc]. In other words, check to see if pExpr
111622 ** sources anywhere else in the query. Return true (non-zero) if pExpr
111629 ** To be an single-source constraint, the following must be true:
111631 ** (1) pExpr cannot refer to any table other than pSrc->iCursor.
111634 ** true and the subquery is non-correlated
111636 ** (2b) pExpr cannot use non-deterministic functions.
111656 ** Without this restriction, accepting pExpr as a single-table
111660 ** on push-down.
111666 int bAllowSubq /* Allow non-correlated subqueries */
111668 const SrcItem *pSrc = &pSrcList->a[iSrc];
111669 if( pSrc->fg.jointype & JT_LTORJ ){
111672 if( pSrc->fg.jointype & JT_LEFT ){
111674 if( pExpr->w.iJoin!=pSrc->iCursor ) return 0; /* rule (4b) */
111679 && (pSrcList->a[0].fg.jointype & JT_LTORJ)!=0 /* Fast pre-test of (6b) */
111683 if( pExpr->w.iJoin==pSrcList->a[jj].iCursor ){
111684 if( (pSrcList->a[jj].fg.jointype & JT_LTORJ)!=0 ){
111692 return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor, bAllowSubq);
111700 ExprList *pGroupBy = pWalker->u.pGroupBy;
111705 for(i=0; i<pGroupBy->nExpr; i++){
111706 Expr *p = pGroupBy->a[i].pExpr;
111707 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
111708 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
111715 /* Check if pExpr is a sub-select. If so, consider it variable. */
111717 pWalker->eCode = 0;
111725 ** Walk the expression tree passed as the first argument. Return non-zero
111733 ** assumes that no other collating sequence will have a finer-grained
111756 ** in a CREATE TABLE statement. Return non-zero if the expression is
111757 ** acceptable for use as a DEFAULT. That is to say, return non-zero if
111771 ** For the purposes of this function, a double-quoted string (ex: "abc")
111772 ** is considered a variable but a single-quoted string (ex: 'abc') is
111800 ** to fit in a 32-bit integer, return 1 and put the value of the integer
111802 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
111808 /* If an expression is an integer literal that fits in a signed 32-bit
111810 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
111811 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
111813 if( p->flags & EP_IntValue ){
111814 *pValue = p->u.iValue;
111817 switch( p->op ){
111819 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
111824 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
111826 *pValue = -v;
111853 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
111854 p = p->pLeft;
111857 op = p->op;
111858 if( op==TK_REGISTER ) op = p->op2;
111868 || NEVER(p->y.pTab==0) /* Reference to column of index on expr */
111870 || (p->iColumn==XN_ROWID && IsView(p->y.pTab))
111872 || (p->iColumn>=0
111873 && p->y.pTab->aCol!=0 /* Possible due to prior error */
111874 && ALWAYS(p->iColumn<p->y.pTab->nCol)
111875 && p->y.pTab->aCol[p->iColumn].notNull==0);
111895 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
111896 if( p->op==TK_UMINUS ) unaryMinus = 1;
111897 p = p->pLeft;
111899 op = p->op;
111900 if( op==TK_REGISTER ) op = p->op2;
111915 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
111916 return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
111925 ** Return TRUE if the given string is a row-id column name.
111936 ** pTab. An alias is usable if there is not an explicit user-defined column
111945 for(iCol=0; iCol<pTab->nCol; iCol++){
111946 if( sqlite3_stricmp(azOpt[ii], pTab->aCol[iCol].zCnName)==0 ) break;
111948 if( iCol==pTab->nCol ){
111971 p = pX->x.pSelect;
111972 if( p->pPrior ) return 0; /* Not a compound SELECT */
111973 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
111974 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
111975 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
111978 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
111979 if( p->pLimit ) return 0; /* Has no LIMIT clause */
111980 if( p->pWhere ) return 0; /* Has no WHERE clause */
111981 pSrc = p->pSrc;
111983 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
111984 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
111985 pTab = pSrc->a[0].pTab;
111989 pEList = p->pEList;
111992 for(i=0; i<pEList->nExpr; i++){
111993 Expr *pRes = pEList->a[i].pExpr;
111994 if( pRes->op!=TK_COLUMN ) return 0;
111995 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
112003 ** Generate code that checks the left-most column of index table iCur to see if
112005 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
112023 ** right-hand side. Return TRUE if that list is constant.
112029 pLHS = pIn->pLeft;
112030 pIn->pLeft = 0;
112032 pIn->pLeft = pLHS;
112042 ** The job of this routine is to find or create a b-tree object that can
112046 ** A cursor is opened on the b-tree object that is the RHS of the IN operator
112049 ** The returned value of this function indicates the b-tree type, as follows:
112051 ** IN_INDEX_ROWID - The cursor was opened on a database table.
112052 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
112053 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
112054 ** IN_INDEX_EPH - The cursor was opened on a specially created and
112056 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
112059 ** An existing b-tree might be used if the RHS expression pX is a simple
112066 ** pX->iTable made to point to the ephemeral table instead of an
112069 ** will be set on pX and the pX->y.sub fields will be set to show where
112078 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
112079 ** through the set members) then the b-tree must not contain duplicates.
112081 ** to be unique - either because it is an INTEGER PRIMARY KEY or due to
112084 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
112087 ** index can be found with the specified <columns> as its left-most.
112091 ** routine might decide that creating an ephemeral b-tree for membership
112096 ** When the b-tree is being used for membership tests, the calling function
112105 ** the value in that register will be NULL if the b-tree contains one or more
112106 ** NULL values, and it will be some non-NULL value if the b-tree contains no
112135 assert( pX->op==TK_IN );
112137 iTab = pParse->nTab++;
112146 ExprList *pEList = pX->x.pSelect->pEList;
112147 for(i=0; i<pEList->nExpr; i++){
112148 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
112150 if( i==pEList->nExpr ){
112158 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
112159 sqlite3 *db = pParse->db; /* Database connection */
112162 ExprList *pEList = p->pEList;
112163 int nExpr = pEList->nExpr;
112165 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
112166 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
112167 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
112168 pTab = p->pSrc->a[0].pTab;
112171 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
112174 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
112177 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
112185 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
112197 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
112198 int iCol = pEList->a[i].pExpr->iColumn;
112220 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
112223 if( pIdx->nColumn<nExpr ) continue;
112224 if( pIdx->pPartIdxWhere!=0 ) continue;
112225 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
112227 testcase( pIdx->nColumn==BMS-2 );
112228 testcase( pIdx->nColumn==BMS-1 );
112229 if( pIdx->nColumn>=BMS-1 ) continue;
112231 if( pIdx->nKeyCol>nExpr
112232 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
112240 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
112241 Expr *pRhs = pEList->a[i].pExpr;
112246 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
112247 assert( pIdx->azColl[j] );
112248 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
112260 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
112261 if( colUsed==(MASKBIT(nExpr)-1) ){
112265 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
112266 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
112268 VdbeComment((v, "%s", pIdx->zName));
112270 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
112274 i64 mask = (1<<nExpr)-1;
112278 *prRhsHasNull = ++pParse->nMem;
112300 && (!sqlite3InRhsIsConstant(pParse,pX) || pX->x.pList->nExpr<=2)
112302 pParse->nTab--; /* Back out the allocation of the unused cursor */
112303 iTab = -1; /* Cursor is not allocated */
112308 /* Could not find an existing table or index to use as the RHS b-tree.
112311 u32 savedNQueryLoop = pParse->nQueryLoop;
112315 pParse->nQueryLoop = 0;
112317 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
112319 assert( pX->op==TK_IN );
112324 pParse->nQueryLoop = savedNQueryLoop;
112329 n = sqlite3ExprVectorSize(pX->pLeft);
112340 ** function allocates and returns a nul-terminated string containing
112347 Expr *pLeft = pExpr->pLeft;
112349 Select *pSelect = ExprUseXSelect(pExpr) ? pExpr->x.pSelect : 0;
112352 assert( pExpr->op==TK_IN );
112353 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
112360 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
112376 ** "sub-select returns N columns - expected M"
112379 if( pParse->nErr==0 ){
112380 const char *zFmt = "sub-select returns %d columns - expected %d";
112388 ** it is not permitted. If pExpr is a sub-select vector, this routine
112391 ** "sub-select returns N columns - expected 1"
112400 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
112414 ** x IN (4,5,11) -- IN operator with list on right-hand side
112415 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
112419 ** table is computed, the cursor number is also stored in pExpr->iTable,
112443 v = pParse->pVdbe;
112449 ** * The right-hand side is a correlated subquery
112450 ** * The right-hand side is an expression list containing variables
112456 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
112465 pExpr->x.pSelect->selId));
112468 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
112469 pExpr->y.sub.iAddr);
112470 assert( iTab!=pExpr->iTable );
112471 sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
112480 pExpr->y.sub.regReturn = ++pParse->nMem;
112481 pExpr->y.sub.iAddr =
112482 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
112488 pLeft = pExpr->pLeft;
112494 pExpr->iTable = iTab;
112495 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
112498 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
112503 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
112511 Select *pSelect = pExpr->x.pSelect;
112512 ExprList *pEList = pSelect->pEList;
112515 addrOnce?"":"CORRELATED ", pSelect->selId
112519 if( ALWAYS(pEList->nExpr==nVal) ){
112526 pSelect->iLimit = 0;
112527 testcase( pSelect->selFlags & SF_Distinct );
112529 pCopy = sqlite3SelectDup(pParse->db, pSelect, 0);
112530 rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest);
112531 sqlite3SelectDelete(pParse->db, pCopy);
112532 sqlite3DbFree(pParse->db, dest.zAffSdst);
112539 assert( pEList->nExpr>0 );
112543 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
112544 pParse, p, pEList->a[i].pExpr
112548 }else if( ALWAYS(pExpr->x.pList!=0) ){
112558 ExprList *pList = pExpr->x.pList;
112569 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
112575 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
112576 Expr *pE2 = pItem->pExpr;
112580 ** this code only executes once. Because for a non-constant
112584 sqlite3VdbeChangeToNoop(v, addrOnce-1);
112606 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
112607 || pParse->nErr );
112608 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
112609 pExpr->y.sub.iAddr, 1);
112620 ** (SELECT a FROM b) -- subquery
112621 ** EXISTS (SELECT a FROM b) -- EXISTS subquery
112625 ** Return the register that holds the result. For a multi-column SELECT,
112627 ** return value is the register of the left-most result column.
112642 Vdbe *v = pParse->pVdbe;
112644 if( pParse->nErr ) return 0;
112645 testcase( pExpr->op==TK_EXISTS );
112646 testcase( pExpr->op==TK_SELECT );
112647 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
112649 pSel = pExpr->x.pSelect;
112654 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
112656 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
112657 pExpr->y.sub.iAddr);
112658 return pExpr->iTable;
112665 pExpr->y.sub.regReturn = ++pParse->nMem;
112666 pExpr->y.sub.iAddr =
112667 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
112672 ** * The right-hand side is a correlated subquery
112673 ** * The right-hand side is an expression list containing variables
112694 addrOnce?"":"CORRELATED ", pSel->selId));
112695 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, -1);
112696 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
112697 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
112698 pParse->nMem += nReg;
112699 if( pExpr->op==TK_SELECT ){
112703 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
112710 if( pSel->pLimit ){
112711 /* The subquery already has a limit. If the pre-existing limit is X
112713 sqlite3 *db = pParse->db;
112716 pLimit->affExpr = SQLITE_AFF_NUMERIC;
112718 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
112720 sqlite3ExprDeferredDelete(pParse, pSel->pLimit->pLeft);
112721 pSel->pLimit->pLeft = pLimit;
112723 /* If there is no pre-existing limit add a limit of 1 */
112724 pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
112725 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
112727 pSel->iLimit = 0;
112729 pExpr->op2 = pExpr->op;
112730 pExpr->op = TK_ERROR;
112733 pExpr->iTable = rReg = dest.iSDParm;
112738 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
112742 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
112743 || pParse->nErr );
112744 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
112745 pExpr->y.sub.iAddr, 1);
112755 ** sub-select on the RHS of the IN() operator has the same number of
112757 ** a sub-query, that the LHS is a vector of size 1.
112760 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
112761 if( ExprUseXSelect(pIn) && !pParse->db->mallocFailed ){
112762 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
112763 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
112767 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
112781 ** The left-hand side (LHS) is a scalar or vector expression. The
112782 ** right-hand side (RHS) is an array of zero or more scalar values, or a
112797 ** See the separate in-operator.md documentation file in the canonical
112821 int addrTop; /* Top of the step-6 loop */
112823 u8 okConstFactor = pParse->okConstFactor;
112826 pLeft = pExpr->pLeft;
112829 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
112831 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
112833 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
112839 v = pParse->pVdbe;
112847 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
112852 ** nVector-1. */
112872 assert( pParse->okConstFactor==okConstFactor );
112873 pParse->okConstFactor = 0;
112875 pParse->okConstFactor = okConstFactor;
112892 ** This is step (1) in the in-operator.md optimized algorithm.
112902 pList = pExpr->x.pList;
112903 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
112908 for(ii=0; ii<pList->nExpr; ii++){
112909 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree);
112910 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
112914 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
112918 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
112919 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
112920 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
112921 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
112952 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
112953 if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error;
112960 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
112965 /* In this case, the RHS is the ROWID of table b-tree and so we also
112966 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
112984 /* Step 4. If the RHS is known to be non-NULL and we did not find
113044 sqlite3DbFree(pParse->db, aiMap);
113045 sqlite3DbFree(pParse->db, zAff);
113052 ** value described by z[0..n-1] into register iMem.
113054 ** The z[] string will probably not be zero-terminated. But the
113063 if( negateFlag ) value = -value;
113072 ** text z[0..n-1] into register iMem.
113074 ** Expr.u.zToken is always UTF8 and zero-terminated.
113077 Vdbe *v = pParse->pVdbe;
113078 if( pExpr->flags & EP_IntValue ){
113079 int i = pExpr->u.iValue;
113081 if( negFlag ) i = -i;
113086 const char *z = pExpr->u.zToken;
113091 sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr);
113096 negFlag?"-":"",pExpr);
113104 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
113112 ** appropriate for the iIdxCol-th column of index pIdx.
113121 i16 iTabCol = pIdx->aiColumn[iIdxCol];
113123 assert( pIdx->aColExpr );
113124 assert( pIdx->aColExpr->nExpr>iIdxCol );
113125 pParse->iSelfTab = iTabCur + 1;
113126 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
113127 pParse->iSelfTab = 0;
113129 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
113146 Vdbe *v = pParse->pVdbe;
113147 int nErr = pParse->nErr;
113149 assert( pParse->iSelfTab!=0 );
113150 if( pParse->iSelfTab>0 ){
113151 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
113156 if( pCol->affinity>=SQLITE_AFF_TEXT ){
113157 sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
113160 if( pParse->nErr>nErr ) pParse->db->errByteOffset = -1;
113165 ** Generate code to extract the value of the iCol-th column of a table.
113178 if( iCol<0 || iCol==pTab->iPKey ){
113180 VdbeComment((v, "%s.rowid", pTab->zName));
113188 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){
113190 if( pCol->colFlags & COLFLAG_BUSY ){
113192 pCol->zCnName);
113194 int savedSelfTab = pParse->iSelfTab;
113195 pCol->colFlags |= COLFLAG_BUSY;
113196 pParse->iSelfTab = iTabCur+1;
113198 pParse->iSelfTab = savedSelfTab;
113199 pCol->colFlags &= ~COLFLAG_BUSY;
113218 ** Generate code that will extract the iColumn-th column from
113232 assert( pParse->pVdbe!=0 );
113235 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
113237 VdbeOp *pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
113238 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
113239 if( pOp->opcode==OP_VColumn ) pOp->p5 = (p5 & OPFLAG_NOCHNG);
113245 ** Generate code to move content from registers iFrom...iFrom+nReg-1
113246 ** over to iTo..iTo+nReg-1.
113249 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
113260 p->op2 = p->op;
113261 p->op = TK_REGISTER;
113262 p->iTable = iReg;
113283 if( p->op==TK_SELECT ){
113291 iResult = pParse->nMem+1;
113292 pParse->nMem += nResult;
113295 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
113303 ** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
113307 if( sqlite3VdbeGetLastOp(v)->opcode==OP_Copy ){
113314 ** in-line rather than by using the usual callbacks.
113323 Vdbe *v = pParse->pVdbe;
113326 nFarg = pFarg->nExpr;
113327 assert( nFarg>0 ); /* All in-line functions have at least one argument */
113330 /* Attempt a direct implementation of the built-in COALESCE() and
113332 ** arguments past the first non-NULL argument.
113337 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
113341 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
113356 Expr *pArg = pFarg->a[0].pExpr;
113357 if( pArg->op==TK_COLUMN && pArg->iTable>=0 ){
113358 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
113366 /* The UNLIKELY() function is a no-op. The result is the value
113370 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
113375 ** Test-only SQL functions that are only usable if enabled
113383 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
113392 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
113401 pA1 = pFarg->a[1].pExpr;
113402 if( pA1->op==TK_COLUMN ){
113404 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable,1),
113421 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
113425 (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
113434 ** Check to see if pExpr is one of the indexed expressions on pParse->pIdxEpr.
113446 for(p=pParse->pIdxEpr; p; p=p->pIENext){
113448 int iDataCur = p->iDataCur;
113450 if( pParse->iSelfTab ){
113451 if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
113452 iDataCur = -1;
113454 if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue;
113455 assert( p->aff>=SQLITE_AFF_BLOB && p->aff<=SQLITE_AFF_NUMERIC );
113457 if( (exprAff<=SQLITE_AFF_BLOB && p->aff!=SQLITE_AFF_BLOB)
113458 || (exprAff==SQLITE_AFF_TEXT && p->aff!=SQLITE_AFF_TEXT)
113459 || (exprAff>=SQLITE_AFF_NUMERIC && p->aff!=SQLITE_AFF_NUMERIC)
113465 v = pParse->pVdbe;
113467 if( p->bMaybeNullRow ){
113472 sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target);
113474 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
113475 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
113477 p = pParse->pIdxEpr;
113478 pParse->pIdxEpr = 0;
113480 pParse->pIdxEpr = p;
113483 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
113484 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
113488 return -1; /* Not found */
113504 for(p=pParse->pIdxPartExpr; p; p=p->pIENext){
113505 if( pExpr->iColumn==p->iIdxCol && pExpr->iTable==p->iDataCur ){
113506 Vdbe *v = pParse->pVdbe;
113510 if( p->bMaybeNullRow ){
113511 addr = sqlite3VdbeAddOp1(v, OP_IfNullRow, p->iIdxCur);
113513 ret = sqlite3ExprCodeTarget(pParse, p->pExpr, iTarget);
113514 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Affinity, ret, 1, 0,
113515 (const char*)&p->aff, 1);
113539 Vdbe *v = pParse->pVdbe; /* The VM under construction */
113542 int regFree1 = 0; /* If non-zero free this temporary register */
113543 int regFree2 = 0; /* If non-zero free this temporary register */
113548 assert( target>0 && target<=pParse->nMem );
113554 }else if( pParse->pIdxEpr!=0
113561 op = pExpr->op;
113566 AggInfo *pAggInfo = pExpr->pAggInfo;
113569 assert( pExpr->iAgg>=0 );
113570 if( pExpr->iAgg>=pAggInfo->nColumn ){
113576 ** tag-20230325-2 */
113582 pCol = &pAggInfo->aCol[pExpr->iAgg];
113583 if( !pAggInfo->directMode ){
113584 return AggInfoColumnReg(pAggInfo, pExpr->iAgg);
113585 }else if( pAggInfo->useSortingIdx ){
113586 Table *pTab = pCol->pTab;
113587 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
113588 pCol->iSorterColumn, target);
113591 }else if( pCol->iColumn<0 ){
113592 VdbeComment((v,"%s.rowid",pTab->zName));
113595 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
113596 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
113601 }else if( pExpr->y.pTab==0 ){
113604 sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, pExpr->iColumn, target);
113611 int iTab = pExpr->iTable;
113615 ** constraints, and that constant is coded by the pExpr->pLeft
113621 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
113623 assert( pExpr->y.pTab!=0 );
113624 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
113630 &zAff[(aff-'B')*2], P4_STATIC);
113635 if( pParse->iSelfTab<0 ){
113639 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
113645 int iCol = pExpr->iColumn;
113647 pTab = pExpr->y.pTab;
113650 assert( iCol<pTab->nCol );
113652 return -1-pParse->iSelfTab;
113654 pCol = pTab->aCol + iCol;
113656 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
113658 if( pCol->colFlags & COLFLAG_GENERATED ){
113659 if( pCol->colFlags & COLFLAG_BUSY ){
113661 pCol->zCnName);
113664 pCol->colFlags |= COLFLAG_BUSY;
113665 if( pCol->colFlags & COLFLAG_NOTAVAIL ){
113668 pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
113672 if( pCol->affinity==SQLITE_AFF_REAL ){
113682 iTab = pParse->iSelfTab - 1;
113685 else if( pParse->pIdxPartExpr
113691 assert( pExpr->y.pTab!=0 );
113692 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
113693 pExpr->iColumn, iTab, target,
113694 pExpr->op2);
113708 codeReal(v, pExpr->u.zToken, 0, target);
113714 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
113722 assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed );
113732 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
113733 assert( pExpr->u.zToken[1]=='\'' );
113734 z = &pExpr->u.zToken[2];
113735 n = sqlite3Strlen30(z) - 1;
113744 assert( pExpr->u.zToken!=0 );
113745 assert( pExpr->u.zToken[0]!=0 );
113746 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
113750 return pExpr->iTable;
113755 sqlite3ExprCode(pParse, pExpr->pLeft, target);
113759 sqlite3AffinityType(pExpr->u.zToken, 0));
113767 /* fall-through */
113774 Expr *pLeft = pExpr->pLeft;
113779 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
113781 codeCompare(pParse, pLeft, pExpr->pRight, op, r1, r2,
113823 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
113824 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
113831 Expr *pLeft = pExpr->pLeft;
113833 if( pLeft->op==TK_INTEGER ){
113837 }else if( pLeft->op==TK_FLOAT ){
113839 codeReal(v, pLeft->u.zToken, 1, target);
113848 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2);
113858 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
113866 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
113868 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
113869 bNormal = pExpr->op2==TK_IS;
113881 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
113891 AggInfo *pInfo = pExpr->pAggInfo;
113893 || NEVER(pExpr->iAgg<0)
113894 || NEVER(pExpr->iAgg>=pInfo->nFunc)
113899 return AggInfoFuncReg(pInfo, pExpr->iAgg);
113910 sqlite3 *db = pParse->db; /* The database connection */
113916 return pExpr->y.pWin->regResult;
113925 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
113929 pFarg = pExpr->x.pList;
113930 nFarg = pFarg ? pFarg->nExpr : 0;
113932 zId = pExpr->u.zToken;
113935 if( pDef==0 && pParse->explain ){
113939 if( pDef==0 || pDef->xFinalize!=0 ){
113943 if( (pDef->funcFlags & SQLITE_FUNC_INLINE)!=0 && ALWAYS(pFarg!=0) ){
113944 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 );
113945 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 );
113947 SQLITE_PTR_TO_INT(pDef->pUserData), target);
113948 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){
113953 if( i<32 && sqlite3ExprIsConstant(pParse, pFarg->a[i].pExpr) ){
113957 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
113958 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
113963 r1 = pParse->nMem+1;
113964 pParse->nMem += nFarg;
113974 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
113977 assert( pFarg->a[0].pExpr!=0 );
113978 exprOp = pFarg->a[0].pExpr->op;
113984 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_LENGTHARG );
113985 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_TYPEOFARG );
113986 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_BYTELENARG);
113987 pFarg->a[0].pExpr->op2 = pDef->funcFlags & OPFLAG_BYTELENARG;
114009 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
114011 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
114014 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
114015 if( !pColl ) pColl = db->pDfltColl;
114019 pDef, pExpr->op2);
114035 if( pParse->db->mallocFailed ){
114039 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1
114049 Expr *pLeft = pExpr->pLeft;
114050 if( pLeft->iTable==0 || pParse->withinRJSubrtn > pLeft->op2 ){
114051 pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft);
114052 pLeft->op2 = pParse->withinRJSubrtn;
114054 assert( pLeft->op==TK_SELECT || pLeft->op==TK_ERROR );
114056 if( pExpr->iTable!=n ){
114058 pExpr->iTable, n);
114060 return pLeft->iTable + pExpr->iColumn;
114083 ** X is stored in pExpr->pLeft.
114084 ** Y is stored in pExpr->pList->a[0].pExpr.
114085 ** Z is stored in pExpr->pList->a[1].pExpr.
114093 /* A TK_COLLATE Expr node without the EP_Collate tag is a so-called
114094 ** "SOFT-COLLATE" that is added to constraints that are pushed down
114095 ** from outer queries into sub-queries by the WHERE-clause push-down
114099 assert( pExpr->pLeft );
114100 sqlite3ExprCode(pParse, pExpr->pLeft, target);
114104 pExpr = pExpr->pLeft;
114105 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */
114110 pExpr = pExpr->pLeft;
114111 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
114116 ** to a column in the new.* or old.* pseudo-tables available to
114118 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
114119 ** is set to the column of the pseudo-table to read, or to -1 to
114124 ** to reference another column of the old.* pseudo-table, where
114126 ** set to (n+1), where n is the number of columns in each pseudo-table.
114127 ** For a reference to any other column in the new.* pseudo-table, p1
114136 ** p1==0 -> old.rowid p1==3 -> new.rowid
114137 ** p1==1 -> old.a p1==4 -> new.a
114138 ** p1==2 -> old.b p1==5 -> new.b
114145 pTab = pExpr->y.pTab;
114146 iCol = pExpr->iColumn;
114147 p1 = pExpr->iTable * (pTab->nCol+1) + 1
114150 assert( pExpr->iTable==0 || pExpr->iTable==1 );
114151 assert( iCol>=-1 && iCol<pTab->nCol );
114152 assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
114153 assert( p1>=0 && p1<(pTab->nCol*2+2) );
114157 (pExpr->iTable ? "new" : "old"),
114158 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName)
114165 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
114167 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
114180 ** that derive from the right-hand table of a LEFT JOIN. The
114181 ** Expr.iTable value is the table number for the right-hand table.
114187 u8 okConstFactor = pParse->okConstFactor;
114188 AggInfo *pAggInfo = pExpr->pAggInfo;
114190 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
114191 if( !pAggInfo->directMode ){
114192 inReg = AggInfoColumnReg(pAggInfo, pExpr->iAgg);
114195 if( pExpr->pAggInfo->useSortingIdx ){
114196 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
114197 pAggInfo->aCol[pExpr->iAgg].iSorterColumn,
114203 addrINR = sqlite3VdbeAddOp3(v, OP_IfNullRow, pExpr->iTable, 0, target);
114211 pParse->okConstFactor = 0; /* note (1) above */
114212 sqlite3ExprCode(pParse, pExpr->pLeft, target);
114214 pParse->okConstFactor = okConstFactor;
114230 ** X (if it exists) is in pExpr->pLeft.
114231 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
114234 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
114251 sqlite3 *db = pParse->db;
114253 assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 );
114254 assert(pExpr->x.pList->nExpr > 0);
114255 pEList = pExpr->x.pList;
114256 aListelem = pEList->a;
114257 nExpr = pEList->nExpr;
114259 if( (pX = pExpr->pLeft)!=0 ){
114261 if( db->mallocFailed ){
114265 testcase( pX->op==TK_COLUMN );
114273 ** The value in regFree1 might get SCopy-ed into the file result.
114278 for(i=0; i<nExpr-1; i=i+2){
114286 testcase( pTest->op==TK_COLUMN );
114288 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
114294 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
114305 assert( pExpr->affExpr==OE_Rollback
114306 || pExpr->affExpr==OE_Abort
114307 || pExpr->affExpr==OE_Fail
114308 || pExpr->affExpr==OE_Ignore
114310 if( !pParse->pTriggerTab && !pParse->nested ){
114312 "RAISE() may only be used within a trigger-program");
114315 if( pExpr->affExpr==OE_Abort ){
114319 if( pExpr->affExpr==OE_Ignore ){
114321 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
114325 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR,
114326 pExpr->affExpr, pExpr->u.zToken, 0, 0);
114363 p = pParse->pConstExpr;
114367 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
114368 if( pItem->fg.reusable
114369 && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0
114371 return pItem->u.iConstExprReg;
114375 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
114377 Vdbe *v = pParse->pVdbe;
114381 pParse->okConstFactor = 0;
114382 if( !pParse->db->mallocFailed ){
114383 if( regDest<0 ) regDest = ++pParse->nMem;
114386 pParse->okConstFactor = 1;
114387 sqlite3ExprDelete(pParse->db, pExpr);
114392 struct ExprList_item *pItem = &p->a[p->nExpr-1];
114393 pItem->fg.reusable = regDest<0;
114394 if( regDest<0 ) regDest = ++pParse->nMem;
114395 pItem->u.iConstExprReg = regDest;
114397 pParse->pConstExpr = p;
114420 && pExpr->op!=TK_REGISTER
114424 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
114447 assert( target>0 && target<=pParse->nMem );
114448 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
114449 if( pParse->pVdbe==0 ) return;
114456 && (ExprHasProperty(pX,EP_Subquery) || pX->op==TK_REGISTER)
114462 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
114472 sqlite3 *db = pParse->db;
114474 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
114485 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pParse,pExpr) ){
114497 ** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
114522 Vdbe *v = pParse->pVdbe;
114525 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
114526 n = pList->nExpr;
114528 for(pItem=pList->a, i=0; i<n; i++, pItem++){
114529 Expr *pExpr = pItem->pExpr;
114531 if( pItem->fg.bSorterRef ){
114532 i--;
114533 n--;
114536 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
114538 i--;
114539 n--;
114541 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
114552 && (pOp=sqlite3VdbeGetLastOp(v))->opcode==OP_Copy
114553 && pOp->p1+pOp->p3+1==inReg
114554 && pOp->p2+pOp->p3+1==target+i
114555 && pOp->p5==0 /* The do-not-merge flag must be clear */
114557 pOp->p3++;
114599 sqlite3 *db = pParse->db;
114606 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
114607 if( db->mallocFailed==0 ){
114613 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
114616 compRight.pRight = pExpr->x.pList->a[1].pExpr;
114626 pDel->flags |= EP_OuterON;
114660 Vdbe *v = pParse->pVdbe;
114670 op = pExpr->op;
114680 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
114682 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
114686 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
114687 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
114693 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
114700 isNot = pExpr->op2==TK_ISNOT;
114701 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
114705 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
114708 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
114726 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
114728 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
114729 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
114730 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
114750 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
114778 /* No-op */
114803 Vdbe *v = pParse->pVdbe;
114814 /* The value of pExpr->op and op are related as follows:
114816 ** pExpr->op op
114817 ** --------- ----------
114827 ** For other values of pExpr->op, op is undefined and unused.
114832 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
114836 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
114837 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
114838 assert( pExpr->op!=TK_NE || op==OP_Eq );
114839 assert( pExpr->op!=TK_EQ || op==OP_Ne );
114840 assert( pExpr->op!=TK_LT || op==OP_Ge );
114841 assert( pExpr->op!=TK_LE || op==OP_Gt );
114842 assert( pExpr->op!=TK_GT || op==OP_Le );
114843 assert( pExpr->op!=TK_GE || op==OP_Lt );
114845 switch( pExpr->op ){
114851 }else if( pExpr->op==TK_AND ){
114853 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
114854 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
114858 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
114860 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
114867 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
114874 isNot = pExpr->op2==TK_ISNOT;
114875 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
114880 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
114885 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
114892 testcase( pExpr->op==TK_IS );
114893 testcase( pExpr->op==TK_ISNOT );
114894 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
114903 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
114905 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
114906 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
114907 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
114925 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
114955 /* no-op */
114976 sqlite3 *db = pParse->db;
114978 if( db->mallocFailed==0 ){
114988 ** If pExpr is a simple SQL value - an integer, real, string, blob
114989 ** or NULL value - then the VDBE currently being prepared is configured
114990 ** to re-prepare each time a new value is bound to variable pVar.
114993 ** same as that currently bound to variable pVar, non-zero is returned.
115006 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
115008 iVar = pVar->iColumn;
115009 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
115010 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
115013 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
115028 ** other than the top-level COLLATE operator.
115030 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
115042 ** this routine is used, it does not hurt to get an extra 2 - that
115047 ** pParse->pReprepare can be matched against literals in pB. The
115048 ** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
115063 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
115066 combinedFlags = pA->flags | pB->flags;
115068 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
115073 if( pA->op!=pB->op || pA->op==TK_RAISE ){
115074 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
115077 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
115080 if( pA->op==TK_AGG_COLUMN && pB->op==TK_COLUMN
115081 && pB->iTable<0 && pA->iTable==iTab
115090 if( pA->u.zToken ){
115091 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
115092 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
115094 assert( pA->op==pB->op );
115099 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
115104 }else if( pA->op==TK_NULL ){
115106 }else if( pA->op==TK_COLLATE ){
115107 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
115109 if( pB->u.zToken!=0
115110 && pA->op!=TK_COLUMN
115111 && pA->op!=TK_AGG_COLUMN
115112 && strcmp(pA->u.zToken,pB->u.zToken)!=0
115117 if( (pA->flags & (EP_Distinct|EP_Commuted))
115118 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
115122 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
115123 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
115124 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
115125 if( pA->op!=TK_STRING
115126 && pA->op!=TK_TRUEFALSE
115129 if( pA->iColumn!=pB->iColumn ) return 2;
115130 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
115131 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
115144 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
115147 ** This routine might return non-zero for equivalent ExprLists. The
115153 ** always differs from a non-NULL pointer.
115159 if( pA->nExpr!=pB->nExpr ) return 1;
115160 for(i=0; i<pA->nExpr; i++){
115162 Expr *pExprA = pA->a[i].pExpr;
115163 Expr *pExprB = pB->a[i].pExpr;
115164 if( pA->a[i].fg.sortFlags!=pB->a[i].fg.sortFlags ) return 1;
115171 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level
115182 ** Return non-zero if Expr p can only be true if pNN is not NULL.
115184 ** Or if seenNot is true, return non-zero if Expr p can only be
115185 ** non-NULL if pNN is not NULL
115192 int seenNot /* Return true only if p can be any non-NULL value */
115197 return pNN->op!=TK_NULL;
115199 switch( p->op ){
115202 assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) );
115203 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
115208 pList = p->x.pList;
115210 assert( pList->nExpr==2 );
115212 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
115213 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
115217 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
115237 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
115244 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
115248 if( p->op2!=TK_IS ) return 0;
115249 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
115253 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
115276 ** compared against literal values in pE2 and pParse->pVdbe->expmask is
115293 if( pE2->op==TK_OR
115294 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
115295 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
115299 if( pE2->op==TK_NOTNULL
115300 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
115308 ** set pWalker->eCode to one only if *both* of the input expressions
115309 ** separately have the implies-not-null-row property.
115312 if( pWalker->eCode==0 ){
115314 if( pWalker->eCode ){
115315 pWalker->eCode = 0;
115323 ** If the expression node requires that the table at pWalker->iCur
115324 ** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
115326 ** pWalker->mWFlags is non-zero if this inquiry is being undertaking on
115331 ** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
115332 ** (never setting pWalker->eCode) is a harmless missed optimization.
115335 testcase( pExpr->op==TK_AGG_COLUMN );
115336 testcase( pExpr->op==TK_AGG_FUNCTION );
115338 if( ExprHasProperty(pExpr, EP_InnerON) && pWalker->mWFlags ){
115339 /* If iCur is used in an inner-join ON clause to the left of a
115340 ** RIGHT JOIN, that does *not* mean that the table must be non-null.
115342 ** To keep things simple, any use of iCur from any inner-join is
115346 switch( pExpr->op ){
115355 testcase( pExpr->op==TK_ISNOT );
115356 testcase( pExpr->op==TK_ISNULL );
115357 testcase( pExpr->op==TK_NOTNULL );
115358 testcase( pExpr->op==TK_IS );
115359 testcase( pExpr->op==TK_VECTOR );
115360 testcase( pExpr->op==TK_FUNCTION );
115361 testcase( pExpr->op==TK_TRUTH );
115362 testcase( pExpr->op==TK_CASE );
115366 if( pWalker->u.iCur==pExpr->iTable ){
115367 pWalker->eCode = 1;
115374 /* Both sides of an AND or OR must separately imply non-null-row.
115378 ** If only one of x or y is non-null-row, then the overall expression
115381 testcase( pExpr->op==TK_OR );
115382 testcase( pExpr->op==TK_AND );
115383 bothImplyNotNullRow(pWalker, pExpr->pLeft, pExpr->pRight);
115389 ** the left-hand side of the IN is NULL then the IN itself will be
115391 if( ExprUseXList(pExpr) && ALWAYS(pExpr->x.pList->nExpr>0) ){
115392 sqlite3WalkExpr(pWalker, pExpr->pLeft);
115397 /* In "x NOT BETWEEN y AND z" either x must be non-null-row or else
115398 ** both y and z must be non-null row */
115400 assert( pExpr->x.pList->nExpr==2 );
115401 sqlite3WalkExpr(pWalker, pExpr->pLeft);
115402 bothImplyNotNullRow(pWalker, pExpr->x.pList->a[0].pExpr,
115403 pExpr->x.pList->a[1].pExpr);
115415 Expr *pLeft = pExpr->pLeft;
115416 Expr *pRight = pExpr->pRight;
115417 testcase( pExpr->op==TK_EQ );
115418 testcase( pExpr->op==TK_NE );
115419 testcase( pExpr->op==TK_LT );
115420 testcase( pExpr->op==TK_LE );
115421 testcase( pExpr->op==TK_GT );
115422 testcase( pExpr->op==TK_GE );
115425 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
115426 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
115427 if( (pLeft->op==TK_COLUMN
115428 && ALWAYS(pLeft->y.pTab!=0)
115429 && IsVirtual(pLeft->y.pTab))
115430 || (pRight->op==TK_COLUMN
115431 && ALWAYS(pRight->y.pTab!=0)
115432 && IsVirtual(pRight->y.pTab))
115444 ** Return true (non-zero) if expression p can only be true if at least
115445 ** one column of table iTab is non-null. In other words, return true
115462 ** be non-NULL, then the LEFT JOIN can be safely converted into an
115469 if( p->op==TK_NOTNULL ){
115470 p = p->pLeft;
115472 while( p->op==TK_AND ){
115473 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab, isRJ) ) return 1;
115474 p = p->pRight;
115501 ** pWalker->u.pIdxCover->iCur can be satisfied using the index
115502 ** pWalker->u.pIdxCover->pIdx.
115505 if( pExpr->op==TK_COLUMN
115506 && pExpr->iTable==pWalker->u.pIdxCover->iCur
115507 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
115509 pWalker->eCode = 1;
115561 struct RefSrcList *p = pWalker->u.pRefSrcList;
115562 SrcList *pSrc = pSelect->pSrc;
115565 if( pSrc->nSrc==0 ) return WRC_Continue;
115566 j = p->nExclude;
115567 p->nExclude += pSrc->nSrc;
115568 piNew = sqlite3DbRealloc(p->db, p->aiExclude, p->nExclude*sizeof(int));
115570 p->nExclude = 0;
115573 p->aiExclude = piNew;
115575 for(i=0; i<pSrc->nSrc; i++, j++){
115576 p->aiExclude[j] = pSrc->a[i].iCursor;
115581 struct RefSrcList *p = pWalker->u.pRefSrcList;
115582 SrcList *pSrc = pSelect->pSrc;
115583 if( p->nExclude ){
115584 assert( p->nExclude>=pSrc->nSrc );
115585 p->nExclude -= pSrc->nSrc;
115591 ** Set the 0x01 bit of pWalker->eCode if there is a reference to any
115594 ** Set the 0x02 bit of pWalker->eCode if there is a reference to a
115598 if( pExpr->op==TK_COLUMN
115599 || pExpr->op==TK_AGG_COLUMN
115602 struct RefSrcList *p = pWalker->u.pRefSrcList;
115603 SrcList *pSrc = p->pRef;
115604 int nSrc = pSrc ? pSrc->nSrc : 0;
115606 if( pExpr->iTable==pSrc->a[i].iCursor ){
115607 pWalker->eCode |= 1;
115611 for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){}
115612 if( i>=p->nExclude ){
115613 pWalker->eCode |= 2;
115628 ** -1 pExpr only references no tables at all, or it only
115637 assert( pParse->db!=0 );
115644 x.db = pParse->db;
115646 assert( pExpr->op==TK_AGG_FUNCTION );
115648 sqlite3WalkExprList(&w, pExpr->x.pList);
115649 if( pExpr->pLeft ){
115650 assert( pExpr->pLeft->op==TK_ORDER );
115651 assert( ExprUseXList(pExpr->pLeft) );
115652 assert( pExpr->pLeft->x.pList!=0 );
115653 sqlite3WalkExprList(&w, pExpr->pLeft->x.pList);
115657 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
115660 if( x.aiExclude ) sqlite3DbNNFreeNN(pParse->db, x.aiExclude);
115666 return -1;
115683 && pExpr->pAggInfo!=0
115685 AggInfo *pAggInfo = pExpr->pAggInfo;
115686 int iAgg = pExpr->iAgg;
115687 Parse *pParse = pWalker->pParse;
115688 sqlite3 *db = pParse->db;
115690 if( pExpr->op!=TK_AGG_FUNCTION ){
115691 if( iAgg<pAggInfo->nColumn
115692 && pAggInfo->aCol[iAgg].pCExpr==pExpr
115696 pAggInfo->aCol[iAgg].pCExpr = pExpr;
115700 assert( pExpr->op==TK_AGG_FUNCTION );
115701 if( ALWAYS(iAgg<pAggInfo->nFunc)
115702 && pAggInfo->aFunc[iAgg].pFExpr==pExpr
115706 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
115720 pWalker->pParse = pParse;
115721 pWalker->xExprCallback = agginfoPersistExprCb;
115722 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
115726 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
115731 pInfo->aCol = sqlite3ArrayAllocate(
115733 pInfo->aCol,
115734 sizeof(pInfo->aCol[0]),
115735 &pInfo->nColumn,
115742 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
115747 pInfo->aFunc = sqlite3ArrayAllocate(
115749 pInfo->aFunc,
115750 sizeof(pInfo->aFunc[0]),
115751 &pInfo->nFunc,
115761 ** If no prior entry is found, create a new one and return -1. The
115762 ** new column will have an index of pAggInfo->nColumn-1.
115772 assert( pAggInfo->iFirstReg==0 );
115773 pCol = pAggInfo->aCol;
115774 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
115775 if( pCol->pCExpr==pExpr ) return;
115776 if( pCol->iTable==pExpr->iTable
115777 && pCol->iColumn==pExpr->iColumn
115778 && pExpr->op!=TK_IF_NULL_ROW
115783 k = addAggInfoColumn(pParse->db, pAggInfo);
115786 assert( pParse->db->mallocFailed );
115789 pCol = &pAggInfo->aCol[k];
115791 pCol->pTab = pExpr->y.pTab;
115792 pCol->iTable = pExpr->iTable;
115793 pCol->iColumn = pExpr->iColumn;
115794 pCol->iSorterColumn = -1;
115795 pCol->pCExpr = pExpr;
115796 if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){
115798 ExprList *pGB = pAggInfo->pGroupBy;
115799 struct ExprList_item *pTerm = pGB->a;
115800 n = pGB->nExpr;
115802 Expr *pE = pTerm->pExpr;
115803 if( pE->op==TK_COLUMN
115804 && pE->iTable==pExpr->iTable
115805 && pE->iColumn==pExpr->iColumn
115807 pCol->iSorterColumn = j;
115812 if( pCol->iSorterColumn<0 ){
115813 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
115817 assert( pExpr->pAggInfo==0 || pExpr->pAggInfo==pAggInfo );
115818 pExpr->pAggInfo = pAggInfo;
115819 if( pExpr->op==TK_COLUMN ){
115820 pExpr->op = TK_AGG_COLUMN;
115822 pExpr->iAgg = (i16)k;
115832 NameContext *pNC = pWalker->u.pNC;
115833 Parse *pParse = pNC->pParse;
115834 SrcList *pSrcList = pNC->pSrcList;
115835 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
115837 assert( pNC->ncFlags & NC_UAggInfo );
115838 assert( pAggInfo->iFirstReg==0 );
115839 switch( pExpr->op ){
115843 assert( pParse->iSelfTab==0 );
115844 if( (pNC->ncFlags & NC_InAggFunc)==0 ) break;
115845 if( pParse->pIdxEpr==0 ) break;
115846 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
115847 int iDataCur = pIEpr->iDataCur;
115849 if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break;
115853 for(i=0; i<pSrcList->nSrc; i++){
115854 if( pSrcList->a[0].iCursor==pIEpr->iDataCur ) break;
115856 if( i>=pSrcList->nSrc ) break;
115857 if( NEVER(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */
115858 if( pParse->nErr ){ return WRC_Abort; }
115866 tmp.iTable = pIEpr->iIdxCur;
115867 tmp.iColumn = pIEpr->iIdxCol;
115869 if( pParse->nErr ){ return WRC_Abort; }
115870 assert( pAggInfo->aCol!=0 );
115871 assert( tmp.iAgg<pAggInfo->nColumn );
115872 pAggInfo->aCol[tmp.iAgg].pCExpr = pExpr;
115873 pExpr->pAggInfo = pAggInfo;
115874 pExpr->iAgg = tmp.iAgg;
115880 testcase( pExpr->op==TK_AGG_COLUMN );
115881 testcase( pExpr->op==TK_COLUMN );
115882 testcase( pExpr->op==TK_IF_NULL_ROW );
115886 SrcItem *pItem = pSrcList->a;
115887 for(i=0; i<pSrcList->nSrc; i++, pItem++){
115889 if( pExpr->iTable==pItem->iCursor ){
115892 } /* endif pExpr->iTable==pItem->iCursor */
115898 if( (pNC->ncFlags & NC_InAggFunc)==0
115899 && pWalker->walkerDepth==pExpr->op2
115900 && pExpr->pAggInfo==0
115905 struct AggInfo_func *pItem = pAggInfo->aFunc;
115906 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
115907 if( NEVER(pItem->pFExpr==pExpr) ) break;
115908 if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){
115912 if( i>=pAggInfo->nFunc ){
115913 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
115915 u8 enc = ENC(pParse->db);
115916 i = addAggInfoFunc(pParse->db, pAggInfo);
115920 pItem = &pAggInfo->aFunc[i];
115921 pItem->pFExpr = pExpr;
115923 nArg = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
115924 pItem->pFunc = sqlite3FindFunction(pParse->db,
115925 pExpr->u.zToken, nArg, enc, 0);
115926 assert( pItem->bOBUnique==0 );
115927 if( pExpr->pLeft
115928 && (pItem->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)==0
115934 assert( pExpr->pLeft->op==TK_ORDER );
115935 assert( ExprUseXList(pExpr->pLeft) );
115936 pItem->iOBTab = pParse->nTab++;
115937 pOBList = pExpr->pLeft->x.pList;
115938 assert( pOBList->nExpr>0 );
115939 assert( pItem->bOBUnique==0 );
115940 if( pOBList->nExpr==1
115942 && sqlite3ExprCompare(0,pOBList->a[0].pExpr,
115943 pExpr->x.pList->a[0].pExpr,0)==0
115945 pItem->bOBPayload = 0;
115946 pItem->bOBUnique = ExprHasProperty(pExpr, EP_Distinct);
115948 pItem->bOBPayload = 1;
115950 pItem->bUseSubtype =
115951 (pItem->pFunc->funcFlags & SQLITE_SUBTYPE)!=0;
115953 pItem->iOBTab = -1;
115955 if( ExprHasProperty(pExpr, EP_Distinct) && !pItem->bOBUnique ){
115956 pItem->iDistinct = pParse->nTab++;
115958 pItem->iDistinct = -1;
115962 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
115966 pExpr->iAgg = (i16)i;
115967 pExpr->pAggInfo = pAggInfo;
115979 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
115994 assert( pNC->pSrcList!=0 );
116008 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
116009 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
116018 if( pParse->nTempReg==0 ){
116019 return ++pParse->nMem;
116021 return pParse->aTempReg[--pParse->nTempReg];
116031 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
116032 pParse->aTempReg[pParse->nTempReg++] = iReg;
116043 i = pParse->iRangeReg;
116044 n = pParse->nRangeReg;
116046 pParse->iRangeReg += nReg;
116047 pParse->nRangeReg -= nReg;
116049 i = pParse->nMem+1;
116050 pParse->nMem += nReg;
116060 if( nReg>pParse->nRangeReg ){
116061 pParse->nRangeReg = nReg;
116062 pParse->iRangeReg = iReg;
116069 ** Always invoke this procedure after coding a subroutine or co-routine
116071 ** the sub/co-routine does not use registers in common with the code that
116072 ** invokes the sub/co-routine.
116075 pParse->nTempReg = 0;
116076 pParse->nRangeReg = 0;
116084 if( pParse->nMem<iReg ) pParse->nMem = iReg;
116095 const ExprList *pList = pParse->pConstExpr;
116098 for(i=0; i<pList->nExpr; i++){
116099 if( pList->a[i].u.iConstExprReg>=iMin ){
116100 iMin = pList->a[i].u.iConstExprReg + 1;
116104 pParse->nTempReg = 0;
116105 pParse->nRangeReg = 0;
116118 if( pParse->nRangeReg>0
116119 && pParse->iRangeReg+pParse->nRangeReg > iFirst
116120 && pParse->iRangeReg <= iLast
116124 for(i=0; i<pParse->nTempReg; i++){
116125 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
116129 if( pParse->pConstExpr ){
116130 ExprList *pList = pParse->pConstExpr;
116131 for(i=0; i<pList->nExpr; i++){
116132 int iReg = pList->a[i].u.iConstExprReg;
116169 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
116174 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
116176 || (pTab->tabFlags & TF_Eponymous)!=0
116177 || ( (pTab->tabFlags & TF_Shadow)!=0
116178 && sqlite3ReadOnlyShadowTables(pParse->db)
116182 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
116202 pParse->colNamesSet = 1;
116226 ** Generate VM code to replace any double-quoted strings (but not double-quoted
116228 ** database zDb with their single-quoted equivalents. If argument bTemp is
116254 Vdbe *v = pParse->pVdbe;
116257 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0, p5);
116258 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0, p5);
116274 char *zName = 0; /* NULL-terminated version of pName */
116275 sqlite3 *db = pParse->db; /* Database connection */
116276 int nTabName; /* Number of UTF-8 characters in zTabName */
116279 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
116281 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
116282 assert( pSrc->nSrc==1 );
116283 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
116285 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
116287 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
116288 zDb = db->aDb[iDb].zDbSName;
116318 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
116325 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
116336 if( pVTab->pVtab->pModule->xRename==0 ){
116352 /* figure out how many UTF-8 characters are in zName */
116353 zTabName = pTab->zName;
116354 nTabName = sqlite3Utf8CharLen(zTabName, -1);
116391 zDb, zName, pTab->zName);
116412 ** of any resources used by the v-table implementation (including other
116417 int i = ++pParse->nMem;
116452 ** The Table structure pParse->pNewTable was extended to include
116456 Table *pNew; /* Copy of pParse->pNewTable */
116461 char *zCol; /* Null-terminated column definition */
116468 db = pParse->db;
116469 assert( db->pParse==pParse );
116470 if( pParse->nErr ) return;
116471 assert( db->mallocFailed==0 );
116472 pNew = pParse->pNewTable;
116476 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
116477 zDb = db->aDb[iDb].zDbSName;
116478 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
116479 pCol = &pNew->aCol[pNew->nCol-1];
116486 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
116496 if( pCol->colFlags & COLFLAG_PRIMKEY ){
116500 if( pNew->pIndex ){
116505 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
116510 assert( pDflt==0 || pDflt->op==TK_SPAN );
116511 if( pDflt && pDflt->pLeft->op==TK_NULL ){
116515 if( (db->flags&SQLITE_ForeignKeys) && pNew->u.tab.pFKey && pDflt ){
116517 "Cannot add a REFERENCES column with non-NULL default value");
116519 if( pCol->notNull && !pDflt ){
116534 assert( db->mallocFailed == 1 );
116539 "Cannot add a column with non-constant default");
116543 }else if( pCol->colFlags & COLFLAG_STORED ){
116549 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
116551 char *zEnd = &zCol[pColDef->n-1];
116553 *zEnd-- = '\0';
116564 zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset,
116579 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
116589 if( pNew->pCheck!=0
116590 || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED)!=0)
116591 || (pTab->tabFlags & TF_Strict)!=0
116596 " WHEN quick_check GLOB 'non-* value in*'"
116603 " OR quick_check GLOB 'non-* value in*'",
116611 ** This function is called by the parser after the table-name in
116612 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
116613 ** pSrc is the full-name of the table being altered.
116631 sqlite3 *db = pParse->db;
116634 assert( pParse->pNewTable==0 );
116636 if( db->mallocFailed ) goto exit_begin_add_column;
116637 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
116658 assert( pTab->u.tab.addColOffset>0 );
116659 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
116670 pParse->pNewTable = pNew;
116671 pNew->nTabRef = 1;
116672 pNew->nCol = pTab->nCol;
116673 assert( pNew->nCol>0 );
116674 nAlloc = (((pNew->nCol-1)/8)*8)+8;
116675 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
116676 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
116677 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
116678 if( !pNew->aCol || !pNew->zName ){
116679 assert( db->mallocFailed );
116682 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
116683 for(i=0; i<pNew->nCol; i++){
116684 Column *pCol = &pNew->aCol[i];
116685 pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName);
116686 pCol->hName = sqlite3StrIHash(pCol->zCnName);
116689 pNew->u.tab.pDfltList = sqlite3ExprListDup(db, pTab->u.tab.pDfltList, 0);
116690 pNew->pSchema = db->aDb[iDb].pSchema;
116691 pNew->u.tab.addColOffset = pTab->u.tab.addColOffset;
116692 assert( pNew->nTabRef==1 );
116703 ** it loads an error message into pParse and returns non-zero.
116723 zType, pTab->zName
116740 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
116744 sqlite3 *db = pParse->db; /* Database connection */
116754 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
116762 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
116764 zDb = db->aDb[iSchema].zDbSName;
116768 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
116777 for(iCol=0; iCol<pTab->nCol; iCol++){
116778 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zCnName, zOld) ) break;
116780 if( iCol==pTab->nCol ){
116785 /* Ensure the schema contains no double-quoted strings */
116796 assert( pNew->n>0 );
116797 bQuote = sqlite3Isquote(pNew->z[0]);
116804 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
116805 pTab->zName
116812 zDb, pTab->zName, iCol, zNew, bQuote
116867 ** rename-token list.
116869 ** 2. Dereferences each pointer in the rename-token list.
116872 ** address-sanitizer or similar. If any of these pointers no longer
116873 ** point to valid objects, an exception is raised by the memory-checking
116887 assert( pParse==pParse->db->pParse );
116888 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
116889 if( pParse->nErr==0 ){
116892 for(p=pParse->pRename; p; p=p->pNext){
116893 if( p->p ){
116894 assert( p->p!=pPtr );
116895 i += *(u8*)(p->p) | 1;
116911 ** in pParse->pRename.
116923 assert( pPtr || pParse->db->mallocFailed );
116925 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
116926 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
116928 pNew->p = pPtr;
116929 pNew->t = *pToken;
116930 pNew->pNext = pParse->pRename;
116931 pParse->pRename = pNew;
116946 for(p=pParse->pRename; p; p=p->pNext){
116947 if( p->p==pFrom ){
116948 p->p = pTo;
116958 Parse *pParse = pWalker->pParse;
116961 sqlite3RenameTokenRemap(pParse, 0, (const void*)&pExpr->y.pTab);
116971 With *pWith = pSelect->pWith;
116973 Parse *pParse = pWalker->pParse;
116976 assert( pWith->nCte>0 );
116977 if( (pWith->a[0].pSelect->selFlags & SF_Expanded)==0 ){
116978 /* Push a copy of the With object onto the with-stack. We use a copy
116980 ** and SF_Resolved) below. And the parser code that uses the with-stack
116983 pCopy = sqlite3WithDup(pParse->db, pWith);
116986 for(i=0; i<pWith->nCte; i++){
116987 Select *p = pWith->a[i].pSelect;
116992 if( sNC.pParse->db->mallocFailed ) return;
116994 sqlite3RenameExprlistUnmap(pParse, pWith->a[i].pCols);
116996 if( pCopy && pParse->pWith==pCopy ){
116997 pParse->pWith = pCopy->pOuter;
117011 for(ii=0; ii<pIdList->nId; ii++){
117012 sqlite3RenameTokenRemap(pParse, 0, (const void*)pIdList->a[ii].zName);
117020 Parse *pParse = pWalker->pParse;
117022 if( pParse->nErr ) return WRC_Abort;
117023 testcase( p->selFlags & SF_View );
117024 testcase( p->selFlags & SF_CopyCte );
117025 if( p->selFlags & (SF_View|SF_CopyCte) ){
117028 if( ALWAYS(p->pEList) ){
117029 ExprList *pList = p->pEList;
117030 for(i=0; i<pList->nExpr; i++){
117031 if( pList->a[i].zEName && pList->a[i].fg.eEName==ENAME_NAME ){
117032 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
117036 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
117037 SrcList *pSrc = p->pSrc;
117038 for(i=0; i<pSrc->nSrc; i++){
117039 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
117040 if( pSrc->a[i].fg.isUsing==0 ){
117041 sqlite3WalkExpr(pWalker, pSrc->a[i].u3.pOn);
117043 unmapColumnIdlistNames(pParse, pSrc->a[i].u3.pUsing);
117056 u8 eMode = pParse->eParseMode;
117062 pParse->eParseMode = PARSE_MODE_UNMAP;
117064 pParse->eParseMode = eMode;
117068 ** Remove all nodes that are part of expression-list pEList from the
117079 for(i=0; i<pEList->nExpr; i++){
117080 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME) ){
117081 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
117094 pNext = p->pNext;
117117 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
117118 if( (*pp)->p==pPtr ){
117121 *pp = pToken->pNext;
117122 pToken->pNext = pCtx->pList;
117123 pCtx->pList = pToken;
117124 pCtx->nList++;
117135 ** descend into sub-select statements.
117138 if( p->selFlags & (SF_View|SF_CopyCte) ){
117139 testcase( p->selFlags & SF_View );
117140 testcase( p->selFlags & SF_CopyCte );
117154 ** constructed in RenameCtx object at pWalker->u.pRename.
117157 RenameCtx *p = pWalker->u.pRename;
117158 if( pExpr->op==TK_TRIGGER
117159 && pExpr->iColumn==p->iCol
117160 && pWalker->pParse->pTriggerTab==p->pTab
117162 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
117163 }else if( pExpr->op==TK_COLUMN
117164 && pExpr->iColumn==p->iCol
117166 && p->pTab==pExpr->y.pTab
117168 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
117183 RenameToken *pBest = pCtx->pList;
117187 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
117188 if( pToken->t.z>pBest->t.z ) pBest = pToken;
117190 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
117191 *pp = pBest->pNext;
117198 ** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
117200 ** sub-routine is currently stored in pParse->zErrMsg. This function
117214 zErr = sqlite3MPrintf(pParse->db, "error in %s %s%s%s: %s",
117216 pParse->zErrMsg
117218 sqlite3_result_error(pCtx, zErr, -1);
117219 sqlite3DbFree(pParse->db, zErr);
117223 ** For each name in the the expression-list pEList (i.e. each
117224 ** pEList->a[i].zName) that matches the string in zOld, extract the
117225 ** corresponding rename-token from Parse object pParse and add it
117236 for(i=0; i<pEList->nExpr; i++){
117237 const char *zName = pEList->a[i].zEName;
117238 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)
117249 ** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
117250 ** that matches the string in zOld, extract the corresponding rename-token
117261 for(i=0; i<pIdList->nId; i++){
117262 const char *zName = pIdList->a[i].zName;
117291 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
117292 p->eParseMode = PARSE_MODE_RENAME;
117293 p->db = db;
117294 p->nQueryLoop = 1;
117296 if( db->mallocFailed ) rc = SQLITE_NOMEM;
117298 && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0)
117309 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
117310 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
117315 db->init.iDb = 0;
117355 nQuot = sqlite3Strlen30(zQuot)-1;
117359 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
117368 /* At this point pRename->pList contains a list of RenameToken objects
117370 ** with the new column name, or with single-quoted versions of themselves.
117375 while( pRename->pList ){
117382 if( bQuote==0 && sqlite3IsIdChar(*pBest->t.z) ){
117388 if( pBest->t.z[pBest->t.n]=='"' ) nReplace++;
117391 /* Dequote the double-quoted token. Then requote it again, this time
117394 ** add another space after the new, single-quoted version of the
117397 memcpy(zBuf1, pBest->t.z, pBest->t.n);
117398 zBuf1[pBest->t.n] = 0;
117401 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
117407 iOff = pBest->t.z - zSql;
117408 if( pBest->t.n!=nReplace ){
117409 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
117410 nOut - (iOff + pBest->t.n)
117412 nOut += nReplace - pBest->t.n;
117419 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
117430 ** Set all pEList->a[].fg.eEName fields in the expression-list to val.
117435 for(i=0; i<pEList->nExpr; i++){
117436 assert( val==ENAME_NAME || pEList->a[i].fg.eEName==ENAME_NAME );
117437 pEList->a[i].fg.eEName = val;
117443 ** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
117449 sqlite3 *db = pParse->db;
117450 Trigger *pNew = pParse->pNewTrigger;
117457 assert( pNew->pTabSchema );
117458 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
117459 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
117461 pParse->eTriggerOp = pNew->op;
117464 if( ALWAYS(pParse->pTriggerTab) ){
117465 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab)!=0;
117469 if( rc==SQLITE_OK && pNew->pWhen ){
117470 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
117473 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
117474 if( pStep->pSelect ){
117475 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
117476 if( pParse->nErr ) rc = pParse->rc;
117478 if( rc==SQLITE_OK && pStep->zTarget ){
117482 pParse, pStep->pExprList, pSrc, 0, 0, 0, 0, 0, 0
117485 pStep->pExprList = 0;
117489 /* pStep->pExprList contains an expression-list used for an UPDATE
117493 ** pStep->pExprList to ENAME_SPAN (from their current value of
117497 renameSetENames(pStep->pExprList, ENAME_SPAN);
117499 renameSetENames(pStep->pExprList, ENAME_NAME);
117500 rc = pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
117501 assert( pStep->pExprList==0 || pStep->pExprList==pSel->pEList );
117502 assert( pSrc==pSel->pSrc );
117503 if( pStep->pExprList ) pSel->pEList = 0;
117504 pSel->pSrc = 0;
117507 if( pStep->pFrom ){
117509 for(i=0; i<pStep->pFrom->nSrc && rc==SQLITE_OK; i++){
117510 SrcItem *p = &pStep->pFrom->a[i];
117511 if( p->pSelect ){
117512 sqlite3SelectPrep(pParse, p->pSelect, 0);
117517 if( db->mallocFailed ){
117521 if( rc==SQLITE_OK && pStep->pWhere ){
117522 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
117525 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
117527 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
117528 if( pStep->pUpsert && rc==SQLITE_OK ){
117529 Upsert *pUpsert = pStep->pUpsert;
117530 pUpsert->pUpsertSrc = pSrc;
117533 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
117535 ExprList *pUpsertSet = pUpsert->pUpsertSet;
117539 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
117542 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
117564 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
117567 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
117568 sqlite3WalkSelect(pWalker, pStep->pSelect);
117569 sqlite3WalkExpr(pWalker, pStep->pWhere);
117570 sqlite3WalkExprList(pWalker, pStep->pExprList);
117571 if( pStep->pUpsert ){
117572 Upsert *pUpsert = pStep->pUpsert;
117573 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
117574 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
117575 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
117576 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
117578 if( pStep->pFrom ){
117580 for(i=0; i<pStep->pFrom->nSrc; i++){
117581 sqlite3WalkSelect(pWalker, pStep->pFrom->a[i].pSelect);
117592 sqlite3 *db = pParse->db;
117594 if( pParse->pVdbe ){
117595 sqlite3VdbeFinalize(pParse->pVdbe);
117597 sqlite3DeleteTable(db, pParse->pNewTable);
117598 while( (pIdx = pParse->pNewIndex)!=0 ){
117599 pParse->pNewIndex = pIdx->pNext;
117602 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
117603 sqlite3DbFree(db, pParse->zErrMsg);
117604 renameTokenFree(db, pParse->pRename);
117620 ** 7. bQuote: Non-zero if the new column name should be quoted.
117624 ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
117654 sqlite3_xauth xAuth = db->xAuth;
117664 if( pTab==0 || iCol>=pTab->nCol ){
117668 zOld = pTab->aCol[iCol].zCnName;
117670 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
117673 db->xAuth = 0;
117688 Select *pSelect = sParse.pNewTable->u.view.pSelect;
117689 pSelect->selFlags &= ~SF_View;
117692 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
117699 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
117703 if( iCol<sParse.pNewTable->nCol ){
117705 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zCnName
117709 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
117711 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
117712 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
117713 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
117715 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
117716 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
117719 for(i=0; i<sParse.pNewTable->nCol; i++){
117721 &sParse.pNewTable->aCol[i]);
117728 for(pFKey=sParse.pNewTable->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
117729 for(i=0; i<pFKey->nCol; i++){
117730 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
117731 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
117733 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
117734 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
117736 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
117742 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
117743 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
117750 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
117751 if( pStep->zTarget ){
117752 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
117754 if( pStep->pUpsert ){
117755 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
117758 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
117759 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
117767 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
117791 db->xAuth = xAuth;
117800 RenameCtx *p = pWalker->u.pRename;
117801 if( pExpr->op==TK_COLUMN
117803 && p->pTab==pExpr->y.pTab
117805 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
117815 RenameCtx *p = pWalker->u.pRename;
117816 SrcList *pSrc = pSelect->pSrc;
117817 if( pSelect->selFlags & (SF_View|SF_CopyCte) ){
117818 testcase( pSelect->selFlags & SF_View );
117819 testcase( pSelect->selFlags & SF_CopyCte );
117823 assert( pWalker->pParse->db->mallocFailed );
117826 for(i=0; i<pSrc->nSrc; i++){
117827 SrcItem *pItem = &pSrc->a[i];
117828 if( pItem->pTab==p->pTab ){
117829 renameTokenFind(pWalker->pParse, p, pItem->zName);
117855 ** -> 'CREATE TABLE t1(a REFERENCES t3)'
117878 sqlite3_xauth xAuth = db->xAuth;
117879 db->xAuth = 0;
117895 int isLegacy = (db->flags & SQLITE_LegacyAlter);
117901 Select *pSelect = pTab->u.view.pSelect;
117906 assert( pSelect->selFlags & SF_View );
117907 pSelect->selFlags &= ~SF_View;
117908 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
117912 sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect);
117918 if( (isLegacy==0 || (db->flags & SQLITE_ForeignKeys))
117923 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
117924 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
117925 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
117934 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
117937 sqlite3WalkExprList(&sWalker, pTab->pCheck);
117939 renameTokenFind(&sParse, &sCtx, pTab->zName);
117945 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
117947 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
117955 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
117956 && sCtx.pTab->pSchema==pTrigger->pTabSchema
117958 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
117965 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
117966 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
117967 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
117969 if( pStep->pFrom ){
117971 for(i=0; i<pStep->pFrom->nSrc; i++){
117972 SrcItem *pItem = &pStep->pFrom->a[i];
117973 if( 0==sqlite3_stricmp(pItem->zName, zOld) ){
117974 renameTokenFind(&sParse, &sCtx, pItem->zName);
118002 db->xAuth = xAuth;
118010 if( pExpr->op==TK_STRING && (pExpr->flags & EP_DblQuoted) ){
118011 renameTokenFind(pWalker->pParse, pWalker->u.pRename, (const void*)pExpr);
118019 ** double-quotes use single quotes instead.
118053 sqlite3_xauth xAuth = db->xAuth;
118054 db->xAuth = 0;
118079 Select *pSelect = sParse.pNewTable->u.view.pSelect;
118080 pSelect->selFlags &= ~SF_View;
118083 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
118089 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
118091 for(i=0; i<sParse.pNewTable->nCol; i++){
118094 &sParse.pNewTable->aCol[i]));
118099 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
118100 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
118126 db->xAuth = xAuth;
118165 int isLegacy = (db->flags & SQLITE_LegacyAlter);
118170 sqlite3_xauth xAuth = db->xAuth;
118171 db->xAuth = 0;
118179 int flags = db->flags;
118180 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML|SQLITE_DqsDDL);
118182 db->flags |= (flags & (SQLITE_DqsDML|SQLITE_DqsDDL));
118188 sqlite3SelectPrep(&sParse, sParse.pNewTable->u.view.pSelect, &sNC);
118197 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
118215 db->xAuth = xAuth;
118224 ** argv[0]: An integer - the index of the schema containing the table
118226 ** argv[2]: An integer - the index of the column to remove.
118240 const char *zDb = db->aDb[iSchema].zDbSName;
118249 sqlite3_xauth xAuth = db->xAuth;
118250 db->xAuth = 0;
118257 if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){
118263 pCol = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol].zCnName);
118264 if( iCol<pTab->nCol-1 ){
118266 pEnd = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol+1].zCnName);
118267 zEnd = (const char*)pEnd->t.z;
118270 zEnd = (const char*)&zSql[pTab->u.tab.addColOffset];
118271 while( ALWAYS(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--;
118274 zNew = sqlite3MPrintf(db, "%.*s%s", pCol->t.z-zSql, zSql, zEnd);
118275 sqlite3_result_text(context, zNew, -1, SQLITE_TRANSIENT);
118281 db->xAuth = xAuth;
118297 sqlite3 *db = pParse->db; /* Database handle */
118302 int iCol; /* Index of column zCol in pTab->aCol[] */
118305 assert( pParse->pNewTable==0 );
118307 if( NEVER(db->mallocFailed) ) goto exit_drop_column;
118308 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
118319 assert( db->mallocFailed );
118330 if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY|COLFLAG_UNIQUE) ){
118332 (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY) ? "PRIMARY KEY" : "UNIQUE",
118339 if( pTab->nCol<=1 ){
118345 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
118347 zDb = db->aDb[iDb].zDbSName;
118350 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, zCol) ){
118360 , zDb, iDb, iCol, pTab->zName
118368 if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL)==0 ){
118374 int nField = 0; /* Number of non-virtual columns after drop */
118377 iCur = pParse->nTab++;
118380 reg = ++pParse->nMem;
118383 pParse->nMem += pTab->nCol;
118386 pParse->nMem += pPk->nColumn;
118387 for(i=0; i<pPk->nKeyCol; i++){
118390 nField = pPk->nKeyCol;
118392 regRec = ++pParse->nMem;
118393 for(i=0; i<pTab->nCol; i++){
118394 if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
118399 if( iPos<pPk->nKeyCol ) continue;
118400 regOut = reg+1+iPos-(iPos>iColPos);
118404 if( i==pTab->iPKey ){
118407 char aff = pTab->aCol[i].affinity;
118409 pTab->aCol[i].affinity = SQLITE_AFF_NUMERIC;
118412 pTab->aCol[i].affinity = aff;
118419 pParse->nMem++;
118425 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iCur, regRec, reg+1, pPk->nKeyCol);
118441 ** Register built-in functions used to help implement ALTER TABLE
118458 ** 2005-07-08
118508 ** columns. The N-th integer (for N>1) is the average number of rows in
118509 ** the index which have the same value for the first N-1 columns. For
118510 ** a K-column index, there will be K+1 integers in the stat column. If
118534 ** inclusive are samples of the left-most key value in the index taken at
118541 ** For i between 0 and S-1. Conceptually, the index space is divided into
118568 ** of entries in the index whose left-most column exactly matches
118569 ** the left-most column of the sample. The second integer in nEq
118575 ** left-most column is less than the left-most column of the sample.
118576 ** The K-th integer in the nLt entry is the number of index entries
118591 ** looks at the left-most column of the index. The sqlite_stat3.sample
118592 ** column contains the actual value of the left-most column instead
118613 ** appropriate compile-time options are provided.
118642 sqlite3 *db = pParse->db;
118656 pDb = &db->aDb[iDb];
118665 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
118668 ** side-effect of the CREATE TABLE statement is to leave the rootpage
118669 ** of the new table in register pParse->regRoot. This is important
118672 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
118674 aRoot[i] = (u32)pParse->regRoot;
118681 aRoot[i] = pStat->tnum;
118686 pDb->zDbSName, zTab, zWhereType, zWhere
118689 }else if( db->xPreUpdateCallback ){
118690 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
118716 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
118741 int nLimit; /* Analysis row-scan limit */
118744 u8 nSkipAhead; /* Number of times of skip-ahead */
118749 u32 iPrn; /* Pseudo-random number used for sampling */
118764 if( p->nRowid ){
118765 sqlite3DbFree(db, p->u.aRowid);
118766 p->nRowid = 0;
118776 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
118777 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
118778 if( p->u.aRowid ){
118779 p->nRowid = n;
118780 memcpy(p->u.aRowid, pData, n);
118782 p->nRowid = 0;
118792 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
118793 p->nRowid = 0;
118794 p->u.iRowid = iRowid;
118804 pTo->isPSample = pFrom->isPSample;
118805 pTo->iCol = pFrom->iCol;
118806 pTo->iHash = pFrom->iHash;
118807 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
118808 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
118809 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
118810 if( pFrom->nRowid ){
118811 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
118813 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
118824 if( p->mxSample ){
118826 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
118827 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
118828 sampleClear(p->db, &p->current);
118831 sqlite3DbFree(p->db, p);
118840 ** L: A limit on the number of rows to scan, or 0 for no-limit
118898 p->db = db;
118899 p->nEst = sqlite3_value_int64(argv[2]);
118900 p->nRow = 0;
118901 p->nLimit = sqlite3_value_int64(argv[3]);
118902 p->nCol = nCol;
118903 p->nKeyCol = nKeyCol;
118904 p->nSkipAhead = 0;
118905 p->current.anDLt = (tRowcnt*)&p[1];
118908 p->current.anEq = &p->current.anDLt[nColUp];
118909 p->mxSample = p->nLimit==0 ? mxSample : 0;
118912 int i; /* Used to iterate through p->aSample[] */
118914 p->iGet = -1;
118915 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
118916 p->current.anLt = &p->current.anEq[nColUp];
118917 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
118920 p->a = (struct StatSample*)&p->current.anLt[nColUp];
118921 p->aBest = &p->a[mxSample];
118922 pSpace = (u8*)(&p->a[mxSample+nCol]);
118924 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
118925 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
118926 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
118928 assert( (pSpace - (u8*)p)==n );
118931 p->aBest[i].iCol = i;
118956 ** pNew and pOld are both candidate non-periodic samples selected for
118957 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
118964 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
118971 int nCol = pAccum->nCol;
118973 assert( pNew->iCol==pOld->iCol );
118974 for(i=pNew->iCol+1; i<nCol; i++){
118975 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
118976 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
118978 if( pNew->iHash>pOld->iHash ) return 1;
118988 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
118995 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
118996 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
118998 assert( pOld->isPSample==0 && pNew->isPSample==0 );
118999 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
119003 if( pNew->iCol<pOld->iCol ) return 1;
119004 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
119010 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
119011 ** remove the least desirable sample from p->a[] to make room.
119023 if( nEqZero>p->nMaxEqZero ){
119024 p->nMaxEqZero = nEqZero;
119026 if( pNew->isPSample==0 ){
119028 assert( pNew->anEq[pNew->iCol]>0 );
119035 for(i=p->nSample-1; i>=0; i--){
119036 StatSample *pOld = &p->a[i];
119037 if( pOld->anEq[pNew->iCol]==0 ){
119038 if( pOld->isPSample ) return;
119039 assert( pOld->iCol>pNew->iCol );
119047 pUpgrade->iCol = pNew->iCol;
119048 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
119054 if( p->nSample>=p->mxSample ){
119055 StatSample *pMin = &p->a[p->iMin];
119056 tRowcnt *anEq = pMin->anEq;
119057 tRowcnt *anLt = pMin->anLt;
119058 tRowcnt *anDLt = pMin->anDLt;
119059 sampleClear(p->db, pMin);
119060 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
119061 pSample = &p->a[p->nSample-1];
119062 pSample->nRowid = 0;
119063 pSample->anEq = anEq;
119064 pSample->anDLt = anDLt;
119065 pSample->anLt = anLt;
119066 p->nSample = p->mxSample-1;
119069 /* The "rows less-than" for the rowid column must be greater than that
119070 ** for the last sample in the p->a[] array. Otherwise, the samples would
119072 assert( p->nSample==0
119073 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
119076 pSample = &p->a[p->nSample];
119078 p->nSample++;
119081 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
119084 if( p->nSample>=p->mxSample ){
119085 int iMin = -1;
119086 for(i=0; i<p->mxSample; i++){
119087 if( p->a[i].isPSample ) continue;
119088 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
119093 p->iMin = iMin;
119101 ** p->current contains a sample that reflects the previous row of the
119110 for(i=(p->nCol-2); i>=iChng; i--){
119111 StatSample *pBest = &p->aBest[i];
119112 pBest->anEq[i] = p->current.anEq[i];
119113 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
119119 ** p->nMaxEqZero or greater set to zero. */
119120 for(i=p->nSample-1; i>=0; i--){
119122 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
119126 if( iChng<p->nMaxEqZero ){
119127 for(i=p->nSample-1; i>=0; i--){
119129 for(j=iChng; j<p->nCol; j++){
119130 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
119133 p->nMaxEqZero = iChng;
119143 ** C Index of left-most column to differ from previous row
119153 ** if it wants the byte-code to do special processing.
119170 assert( p->nCol>0 );
119171 assert( iChng<p->nCol );
119173 if( p->nRow==0 ){
119176 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
119181 if( p->mxSample ) samplePushPrevious(p, iChng);
119188 p->current.anEq[i]++;
119191 for(i=iChng; i<p->nCol; i++){
119192 p->current.anDLt[i]++;
119194 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
119195 p->current.anEq[i] = 1;
119200 p->nRow++;
119202 if( p->mxSample ){
119205 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
119207 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
119210 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
119212 nLt = p->current.anLt[p->nCol-1];
119214 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
119215 p->current.isPSample = 1;
119216 p->current.iCol = 0;
119217 sampleInsert(p, &p->current, p->nCol-1);
119218 p->current.isPSample = 0;
119222 for(i=0; i<(p->nCol-1); i++){
119223 p->current.iCol = i;
119224 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
119225 sampleCopy(p, &p->aBest[i], &p->current);
119230 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
119231 p->nSkipAhead++;
119232 sqlite3_result_int(context, p->current.anDLt[0]>0);
119270 ** a one-parameter function, stat_get(P), that always returns the
119287 assert( eCall==STAT_GET_STAT1 || p->mxSample );
119312 ** I = (K+D-1)/D
119322 sqlite3StrAccumInit(&sStat, 0, 0, 0, (p->nKeyCol+1)*100);
119324 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
119325 for(i=0; i<p->nKeyCol; i++){
119326 u64 nDistinct = p->current.anDLt[i] + 1;
119327 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
119328 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
119331 assert( p->current.anEq[i] || p->nRow==0 );
119338 if( p->iGet<0 ){
119340 p->iGet = 0;
119342 if( p->iGet<p->nSample ){
119343 StatSample *pS = p->a + p->iGet;
119344 if( pS->nRowid==0 ){
119345 sqlite3_result_int64(context, pS->u.iRowid);
119347 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
119356 assert( p->iGet<p->nSample );
119358 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
119359 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
119361 aCnt = p->a[p->iGet].anDLt;
119362 p->iGet++;
119366 sqlite3StrAccumInit(&sStat, 0, 0, 0, p->nCol*100);
119367 for(i=0; i<p->nCol; i++){
119370 if( sStat.nChar ) sStat.nChar--;
119392 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1);
119405 ** of the k-th column of the pIdx index.
119413 assert( k>=0 && k<pIdx->nColumn );
119414 i = pIdx->aiColumn[k];
119416 VdbeComment((v,"%s.rowid",pIdx->zName));
119418 assert( pIdx->bHasExpr );
119419 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
119421 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
119440 sqlite3 *db = pParse->db; /* Database handle */
119446 int jZeroRows = -1; /* Jump from here if number of rows is zero */
119460 int doOnce = 1; /* Flag for a one-time computation */
119476 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
119481 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
119485 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
119486 db->aDb[iDb].zDbSName ) ){
119492 if( db->xPreUpdateCallback ){
119495 pStat1->zName = (char*)&pStat1[1];
119496 memcpy(pStat1->zName, "sqlite_stat1", 13);
119497 pStat1->nCol = 3;
119498 pStat1->iPKey = -1;
119499 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNAMIC);
119503 /* Establish a read-lock on the table at the shared-cache level.
119504 ** Open a read-only cursor on the table. Also allocate a cursor number
119507 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
119510 pParse->nTab = MAX(pParse->nTab, iTab);
119512 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
119514 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
119522 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
119524 nCol = pIdx->nKeyCol;
119525 zIdxName = pTab->zName;
119526 nColTest = nCol - 1;
119528 nCol = pIdx->nColumn;
119529 zIdxName = pIdx->zName;
119530 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
119535 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
119538 ** Pseudo-code for loop that calls stat_push():
119580 /* Open a read-only cursor on the index being analyzed. */
119581 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
119582 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
119584 VdbeComment((v, "%s", pIdx->zName));
119599 sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2);
119608 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid);
119637 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
119638 /* For a single-column UNIQUE index, once we have found a non-NULL
119645 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
119665 sqlite3VdbeJumpHere(v, addrNextRow-1);
119688 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
119690 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
119691 for(j=0; j<pPk->nKeyCol; j++){
119692 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
119693 assert( k>=0 && k<pIdx->nColumn );
119697 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
119698 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
119706 if( db->nAnalysisLimit ){
119722 if( pIdx->pPartIdxWhere ){
119723 /* Partial indexes might get a zero-entry in sqlite_stat1. But
119734 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
119740 if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){
119763 for(pX=pTab->pIndex; pX; pX=pX->pNext){
119766 nColX = pX->nKeyCol;
119768 nColX = pX->nColumn;
119779 ** https://sqlite.org/forum/forumpost/83cb4a95a0 (2023-03-25)
119783 sqlite3ClearTempRegCache(pParse); /* tag-20230325-1 */
119804 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
119818 VdbeComment((v, "%s", pTab->zName));
119828 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
119850 sqlite3 *db = pParse->db;
119851 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
119858 iStatCur = pParse->nTab;
119859 pParse->nTab += 3;
119861 iMem = pParse->nMem+1;
119862 iTab = pParse->nTab;
119864 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
119886 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
119887 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
119889 iStatCur = pParse->nTab;
119890 pParse->nTab += 3;
119892 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
119894 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
119896 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
119904 ** ANALYZE -- 1
119905 ** ANALYZE <database> -- 2
119906 ** ANALYZE ?<database>.?<tablename> -- 3
119913 sqlite3 *db = pParse->db;
119924 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
119932 for(i=0; i<db->nDb; i++){
119936 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
119943 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
119947 analyzeTable(pParse, pIdx->pTable, pIdx);
119955 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
119971 ** The first argument points to a nul-terminated string containing a
119995 v = v*10 + c - '0';
120014 pIndex->bUnordered = 0;
120015 pIndex->noSkipScan = 0;
120018 pIndex->bUnordered = 1;
120019 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
120022 pIndex->szIdxRow = sqlite3LogEst(sz);
120024 pIndex->noSkipScan = 1;
120027 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
120028 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
120040 && aLog[0] <= aLog[nOut-1] /* And only a single value seen */
120042 pIndex->bLowQual = 1;
120053 ** argv[2] = results of analysis - on integer for each column
120070 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
120079 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
120085 int nCol = pIndex->nKeyCol+1;
120090 if( pIndex->aiRowEst==0 ){
120091 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
120092 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
120094 aiRowEst = pIndex->aiRowEst;
120096 pIndex->bUnordered = 0;
120097 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
120098 pIndex->hasStat1 = 1;
120099 if( pIndex->pPartIdxWhere==0 ){
120100 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
120101 pTable->tabFlags |= TF_HasStat1;
120105 fakeIdx.szIdxRow = pTable->szTabRow;
120109 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
120110 pTable->szTabRow = fakeIdx.szIdxRow;
120111 pTable->tabFlags |= TF_HasStat1;
120125 if( pIdx->aSample ){
120127 for(j=0; j<pIdx->nSample; j++){
120128 IndexSample *p = &pIdx->aSample[j];
120129 sqlite3DbFree(db, p->p);
120131 sqlite3DbFree(db, pIdx->aSample);
120133 if( db->pnBytesFreed==0 ){
120134 pIdx->nSample = 0;
120135 pIdx->aSample = 0;
120145 ** Populate the pIdx->aAvgEq[] array based on the samples currently
120146 ** stored in pIdx->aSample[].
120150 IndexSample *aSample = pIdx->aSample;
120151 IndexSample *pFinal = &aSample[pIdx->nSample-1];
120154 if( pIdx->nSampleCol>1 ){
120159 nCol = pIdx->nSampleCol-1;
120160 pIdx->aAvgEq[nCol] = 1;
120163 int nSample = pIdx->nSample;
120171 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
120172 nRow = pFinal->anLt[iCol];
120173 nDist100 = (i64)100 * pFinal->anDLt[iCol];
120174 nSample--;
120176 nRow = pIdx->aiRowEst[0];
120177 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
120179 pIdx->nRowEst0 = nRow;
120186 if( i==(pIdx->nSample-1)
120195 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
120198 pIdx->aAvgEq[iCol] = avgEq;
120242 IndexSample *pSample; /* A slot in pIdx->aSample[] */
120244 assert( db->lookaside.bDisable );
120249 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
120267 assert( pIdx==0 || pIdx->nSample==0 );
120269 if( pIdx->aSample!=0 ){
120273 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
120274 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
120275 nIdxCol = pIdx->nKeyCol;
120277 nIdxCol = pIdx->nColumn;
120279 pIdx->nSampleCol = nIdxCol;
120280 pIdx->mxSample = nSample;
120285 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
120286 if( pIdx->aSample==0 ){
120290 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
120291 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
120292 pIdx->pTable->tabFlags |= TF_HasStat4;
120294 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
120295 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
120296 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
120298 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
120307 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
120320 if( pIdx->nSample>=pIdx->mxSample ){
120327 nCol = pIdx->nSampleCol;
120332 pSample = &pIdx->aSample[pIdx->nSample];
120333 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
120334 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
120335 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
120344 pSample->n = sqlite3_column_bytes(pStmt, 4);
120345 pSample->p = sqlite3DbMallocZero(db, pSample->n + 8);
120346 if( pSample->p==0 ){
120350 if( pSample->n ){
120351 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
120353 pIdx->nSample++;
120368 assert( db->lookaside.bDisable );
120399 ** If an OOM error occurs, this function always sets db->mallocFailed.
120408 Schema *pSchema = db->aDb[iDb].pSchema;
120411 assert( iDb>=0 && iDb<db->nDb );
120412 assert( db->aDb[iDb].pBt!=0 );
120416 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
120418 pTab->tabFlags &= ~TF_HasStat1;
120420 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
120422 pIdx->hasStat1 = 0;
120425 pIdx->aSample = 0;
120431 sInfo.zDatabase = db->aDb[iDb].zDbSName;
120447 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
120449 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
120459 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
120461 sqlite3_free(pIdx->aiRowEst);
120462 pIdx->aiRowEst = 0;
120515 if( pExpr->op!=TK_ID ){
120518 pExpr->op = TK_STRING;
120530 sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
120536 ** An SQL user-function registered to do the work of an ATTACH statement. The
120546 ** If the db->init.reopenMemdb flags is set, then instead of attaching a
120547 ** new database, close the database on db->init.iDb and reopen it as an
120575 # define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
120582 ** from sqlite3_deserialize() to close database db->init.iDb and
120594 pNew = &db->aDb[db->init.iDb];
120595 if( ALWAYS(pNew->pBt) ) sqlite3BtreeClose(pNew->pBt);
120596 pNew->pBt = pNewBt;
120597 pNew->pSchema = pNewSchema;
120613 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
120614 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
120615 db->aLimit[SQLITE_LIMIT_ATTACHED]
120619 for(i=0; i<db->nDb; i++){
120627 /* Allocate the new entry in the db->aDb[] array and initialize the schema
120630 if( db->aDb==db->aDbStatic ){
120631 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
120633 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
120635 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
120638 db->aDb = aNew;
120639 pNew = &db->aDb[db->nDb];
120646 flags = db->openFlags;
120647 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
120650 sqlite3_result_error(context, zErr, -1);
120656 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
120657 db->nDb++;
120658 pNew->zDbSName = sqlite3DbStrDup(db, zName);
120660 db->noSharedCache = 0;
120666 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
120667 if( !pNew->pSchema ){
120669 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
120674 sqlite3BtreeEnter(pNew->pBt);
120675 pPager = sqlite3BtreePager(pNew->pBt);
120676 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
120677 sqlite3BtreeSecureDelete(pNew->pBt,
120678 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
120680 sqlite3BtreeSetPagerFlags(pNew->pBt,
120681 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
120683 sqlite3BtreeLeave(pNew->pBt);
120685 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
120686 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
120693 ** remove the entry from the db->aDb[] array. i.e. put everything back the
120698 db->init.iDb = 0;
120699 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
120710 if( newAuth<db->auth.authLevel ){
120717 int iDb = db->nDb - 1;
120719 if( db->aDb[iDb].pBt ){
120720 sqlite3BtreeClose(db->aDb[iDb].pBt);
120721 db->aDb[iDb].pBt = 0;
120722 db->aDb[iDb].pSchema = 0;
120725 db->nDb = iDb;
120742 sqlite3_result_error(context, zErrDyn, -1);
120749 ** An SQL user-function registered to do the work of an DETACH statement. The
120771 for(i=0; i<db->nDb; i++){
120772 pDb = &db->aDb[i];
120773 if( pDb->pBt==0 ) continue;
120777 if( i>=db->nDb ){
120785 if( sqlite3BtreeTxnState(pDb->pBt)!=SQLITE_TXN_NONE
120786 || sqlite3BtreeIsInBackup(pDb->pBt)
120794 assert( db->aDb[1].pSchema );
120795 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
120798 if( pTrig->pTabSchema==pDb->pSchema ){
120799 pTrig->pTabSchema = pTrig->pSchema;
120804 sqlite3BtreeClose(pDb->pBt);
120805 pDb->pBt = 0;
120806 pDb->pSchema = 0;
120811 sqlite3_result_error(context, zErr, -1);
120830 sqlite3* db = pParse->db;
120835 if( pParse->nErr ) goto attach_end;
120850 if( pAuthArg->op==TK_STRING ){
120852 zAuthArg = pAuthArg->u.zToken;
120870 assert( v || db->mallocFailed );
120872 sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3,
120873 pFunc->nArg, pFunc, 0);
120932 DbFixer *pFix = p->u.pFix;
120933 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL);
120934 if( pExpr->op==TK_VARIABLE ){
120935 if( pFix->pParse->db->init.busy ){
120936 pExpr->op = TK_NULL;
120938 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
120949 DbFixer *pFix = p->u.pFix;
120952 sqlite3 *db = pFix->pParse->db;
120953 int iDb = sqlite3FindDbName(db, pFix->zDb);
120954 SrcList *pList = pSelect->pSrc;
120957 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
120958 if( pFix->bTemp==0 ){
120959 if( pItem->zDatabase ){
120960 if( iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
120961 sqlite3ErrorMsg(pFix->pParse,
120963 pFix->zType, pFix->pName, pItem->zDatabase);
120966 sqlite3DbFree(db, pItem->zDatabase);
120967 pItem->zDatabase = 0;
120968 pItem->fg.notCte = 1;
120970 pItem->pSchema = pFix->pSchema;
120971 pItem->fg.fromDDL = 1;
120974 if( pList->a[i].fg.isUsing==0
120975 && sqlite3WalkExpr(&pFix->w, pList->a[i].u3.pOn)
120981 if( pSelect->pWith ){
120982 for(i=0; i<pSelect->pWith->nCte; i++){
120983 if( sqlite3WalkSelect(p, pSelect->pWith->a[i].pSelect) ){
121002 sqlite3 *db = pParse->db;
121003 assert( db->nDb>iDb );
121004 pFix->pParse = pParse;
121005 pFix->zDb = db->aDb[iDb].zDbSName;
121006 pFix->pSchema = db->aDb[iDb].pSchema;
121007 pFix->zType = zType;
121008 pFix->pName = pName;
121009 pFix->bTemp = (iDb==1);
121010 pFix->w.pParse = pParse;
121011 pFix->w.xExprCallback = fixExprCb;
121012 pFix->w.xSelectCallback = fixSelectCb;
121013 pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback;
121014 pFix->w.walkerDepth = 0;
121015 pFix->w.eCode = 0;
121016 pFix->w.u.pFix = pFix;
121030 ** pParse->zErrMsg and these routines return non-zero. If everything
121042 res = sqlite3WalkSelect(&pFix->w, &s);
121051 return sqlite3WalkSelect(&pFix->w, pSelect);
121057 return sqlite3WalkExpr(&pFix->w, pExpr);
121067 if( sqlite3WalkSelect(&pFix->w, pStep->pSelect)
121068 || sqlite3WalkExpr(&pFix->w, pStep->pWhere)
121069 || sqlite3WalkExprList(&pFix->w, pStep->pExprList)
121070 || sqlite3FixSrcList(pFix, pStep->pFrom)
121077 for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){
121078 if( sqlite3WalkExprList(&pFix->w, pUp->pUpsertTarget)
121079 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertTargetWhere)
121080 || sqlite3WalkExprList(&pFix->w, pUp->pUpsertSet)
121081 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertWhere)
121088 pStep = pStep->pNext;
121111 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
121158 ** means that the SQL statement will never-run - the sqlite3_exec() call
121174 sqlite3_mutex_enter(db->mutex);
121175 db->xAuth = (sqlite3_xauth)xAuth;
121176 db->pAuthArg = pArg;
121177 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
121178 sqlite3_mutex_leave(db->mutex);
121183 ** Write an error message into pParse->zErrMsg that explains that the
121184 ** user-supplied authorization function returned an illegal value.
121188 pParse->rc = SQLITE_ERROR;
121206 sqlite3 *db = pParse->db; /* Database handle */
121207 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
121210 if( db->init.busy ) return SQLITE_OK;
121211 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
121213 ,db->auth.zAuthUser
121218 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
121220 pParse->rc = SQLITE_AUTH;
121244 int iSrc; /* Index in pTabList->a[] of table being read */
121248 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
121250 assert( pParse->db->xAuth!=0 );
121251 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
121258 if( pExpr->op==TK_TRIGGER ){
121259 pTab = pParse->pTriggerTab;
121262 for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
121263 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
121264 pTab = pTabList->a[iSrc].pTab;
121269 iCol = pExpr->iColumn;
121273 assert( iCol<pTab->nCol );
121274 zCol = pTab->aCol[iCol].zCnName;
121275 }else if( pTab->iPKey>=0 ){
121276 assert( pTab->iPKey<pTab->nCol );
121277 zCol = pTab->aCol[pTab->iPKey].zCnName;
121281 assert( iDb>=0 && iDb<pParse->db->nDb );
121282 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
121283 pExpr->op = TK_NULL;
121300 sqlite3 *db = pParse->db;
121306 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
121307 if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE ){
121311 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
121312 ** callback are either NULL pointers or zero-terminated strings that
121320 testcase( pParse->zAuthContext==0 );
121322 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
121324 ,db->auth.zAuthUser
121329 pParse->rc = SQLITE_AUTH;
121340 ** popped. Or if pParse==0, this routine is a no-op.
121348 pContext->pParse = pParse;
121349 pContext->zAuthContext = pParse->zAuthContext;
121350 pParse->zAuthContext = zContext;
121358 if( pContext->pParse ){
121359 pContext->pParse->zAuthContext = pContext->zAuthContext;
121360 pContext->pParse = 0;
121407 ** Record the fact that we want to lock a table at run-time.
121430 for(i=0; i<pToplevel->nTableLock; i++){
121431 p = &pToplevel->aTableLock[i];
121432 if( p->iDb==iDb && p->iTab==iTab ){
121433 p->isWriteLock = (p->isWriteLock || isWriteLock);
121438 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
121439 pToplevel->aTableLock =
121440 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
121441 if( pToplevel->aTableLock ){
121442 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
121443 p->iDb = iDb;
121444 p->iTab = iTab;
121445 p->isWriteLock = isWriteLock;
121446 p->zLockName = zName;
121448 pToplevel->nTableLock = 0;
121449 sqlite3OomFault(pToplevel->db);
121460 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
121470 Vdbe *pVdbe = pParse->pVdbe;
121473 for(i=0; i<pParse->nTableLock; i++){
121474 TableLock *p = &pParse->aTableLock[i];
121475 int p1 = p->iDb;
121476 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
121477 p->zLockName, P4_STATIC);
121485 ** Return TRUE if the given yDbMask object is empty - if it contains no
121512 assert( pParse->pToplevel==0 );
121513 db = pParse->db;
121514 assert( db->pParse==pParse );
121515 if( pParse->nested ) return;
121516 if( pParse->nErr ){
121517 if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM;
121520 assert( db->mallocFailed==0 );
121525 v = pParse->pVdbe;
121527 if( db->init.busy ){
121528 pParse->rc = SQLITE_DONE;
121532 if( v==0 ) pParse->rc = SQLITE_ERROR;
121534 assert( !pParse->isMultiWrite
121535 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
121537 if( pParse->bReturning ){
121538 Returning *pReturning = pParse->u1.pReturning;
121542 if( pReturning->nRetCol ){
121545 sqlite3VdbeAddOp1(v, OP_Rewind, pReturning->iRetCur);
121547 reg = pReturning->iRetReg;
121548 for(i=0; i<pReturning->nRetCol; i++){
121549 sqlite3VdbeAddOp3(v, OP_Column, pReturning->iRetCur, i, reg+i);
121552 sqlite3VdbeAddOp2(v, OP_Next, pReturning->iRetCur, addrRewind+1);
121560 if( pParse->nTableLock>0 && db->init.busy==0 ){
121562 if( db->auth.authLevel<UAUTH_User ){
121564 pParse->rc = SQLITE_AUTH_USER;
121576 assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
121578 assert( db->nDb>0 );
121582 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
121584 pSchema = db->aDb[iDb].pSchema;
121588 DbMaskTest(pParse->writeMask,iDb), /* P2 */
121589 pSchema->schema_cookie, /* P3 */
121590 pSchema->iGeneration /* P4 */
121592 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
121594 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
121595 }while( ++iDb<db->nDb );
121597 for(i=0; i<pParse->nVtabLock; i++){
121598 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
121601 pParse->nVtabLock = 0;
121606 ** obtain the required table-locks. This is a no-op unless the
121607 ** shared-cache feature is enabled.
121609 if( pParse->nTableLock ) codeTableLocks(pParse);
121614 if( pParse->pAinc ) sqlite3AutoincrementBegin(pParse);
121618 if( pParse->pConstExpr ){
121619 ExprList *pEL = pParse->pConstExpr;
121620 pParse->okConstFactor = 0;
121621 for(i=0; i<pEL->nExpr; i++){
121622 assert( pEL->a[i].u.iConstExprReg>0 );
121623 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
121627 if( pParse->bReturning ){
121628 Returning *pRet = pParse->u1.pReturning;
121629 if( pRet->nRetCol ){
121630 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol);
121640 assert( v!=0 || pParse->nErr );
121641 assert( db->mallocFailed==0 || pParse->nErr );
121642 if( pParse->nErr==0 ){
121645 assert( pParse->pAinc==0 || pParse->nTab>0 );
121647 pParse->rc = SQLITE_DONE;
121649 pParse->rc = SQLITE_ERROR;
121662 ** * Built-in SQL functions always take precedence over application-defined
121664 ** built-in function.
121669 sqlite3 *db = pParse->db;
121670 u32 savedDbFlags = db->mDbFlags;
121673 if( pParse->nErr ) return;
121674 if( pParse->eParseMode ) return;
121675 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
121683 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
121684 pParse->nErr++;
121687 pParse->nested++;
121690 db->mDbFlags |= DBFLAG_PreferBuiltin;
121692 db->mDbFlags = savedDbFlags;
121695 pParse->nested--;
121709 ** Locate the in-memory structure that describes a particular database
121729 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
121734 for(i=0; i<db->nDb; i++){
121735 if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
121737 if( i>=db->nDb ){
121746 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
121753 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
121758 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
121765 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
121768 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
121771 for(i=2; i<db->nDb; i++){
121773 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
121778 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE);
121780 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
121789 ** Locate the in-memory structure that describes a particular database
121792 ** error message in pParse->zErrMsg.
121795 ** routine leaves an error message in pParse->zErrMsg where
121805 sqlite3 *db = pParse->db;
121809 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
121821 if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){
121822 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
121827 testcase( pMod->pEpoTab==0 );
121828 return pMod->pEpoTab;
121833 pParse->checkSchema = 1;
121834 }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){
121846 assert( HasRowid(p) || p->iPKey<0 );
121857 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
121858 ** non-NULL if it is part of a view or trigger program definition. See
121867 assert( p->pSchema==0 || p->zDatabase==0 );
121868 if( p->pSchema ){
121869 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
121870 zDb = pParse->db->aDb[iDb].zDbSName;
121872 zDb = p->zDatabase;
121874 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
121894 ** Locate the in-memory structure that describes
121910 for(i=OMIT_TEMPDB; i<db->nDb; i++){
121912 Schema *pSchema = db->aDb[j].pSchema;
121916 p = sqlite3HashFind(&pSchema->idxHash, zName);
121929 sqlite3ExprDelete(db, p->pPartIdxWhere);
121930 sqlite3ExprListDelete(db, p->aColExpr);
121931 sqlite3DbFree(db, p->zColAff);
121932 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
121934 sqlite3_free(p->aiRowEst);
121950 pHash = &db->aDb[iDb].pSchema->idxHash;
121953 if( pIndex->pTable->pIndex==pIndex ){
121954 pIndex->pTable->pIndex = pIndex->pNext;
121959 p = pIndex->pTable->pIndex;
121960 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
121961 if( ALWAYS(p && p->pNext==pIndex) ){
121962 p->pNext = pIndex->pNext;
121967 db->mDbFlags |= DBFLAG_SchemaChange;
121971 ** Look through the list of open database files in db->aDb[] and if
121973 ** db->aDb[] structure to a smaller size, if possible.
121980 for(i=j=2; i<db->nDb; i++){
121981 struct Db *pDb = &db->aDb[i];
121982 if( pDb->pBt==0 ){
121983 sqlite3DbFree(db, pDb->zDbSName);
121984 pDb->zDbSName = 0;
121988 db->aDb[j] = db->aDb[i];
121992 db->nDb = j;
121993 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
121994 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
121995 sqlite3DbFree(db, db->aDb);
121996 db->aDb = db->aDbStatic;
122002 ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
122007 assert( iDb<db->nDb );
122013 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
122016 if( db->nSchemaLock==0 ){
122017 for(i=0; i<db->nDb; i++){
122019 sqlite3SchemaClear(db->aDb[i].pSchema);
122032 for(i=0; i<db->nDb; i++){
122033 Db *pDb = &db->aDb[i];
122034 if( pDb->pSchema ){
122035 if( db->nSchemaLock==0 ){
122036 sqlite3SchemaClear(pDb->pSchema);
122042 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
122045 if( db->nSchemaLock==0 ){
122054 db->mDbFlags &= ~DBFLAG_SchemaChange;
122070 pList = pTab->u.tab.pDfltList;
122071 if( pCol->iDflt==0
122073 || NEVER(pList->nExpr<pCol->iDflt)
122075 pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
122076 pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
122078 sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr);
122079 pList->a[pCol->iDflt-1].pExpr = pExpr;
122089 if( pCol->iDflt==0 ) return 0;
122091 if( NEVER(pTab->u.tab.pDfltList==0) ) return 0;
122092 if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
122093 return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
122108 n = sqlite3Strlen30(pCol->zCnName) + 1;
122109 if( pCol->colFlags & COLFLAG_HASTYPE ){
122110 n += sqlite3Strlen30(pCol->zCnName+n) + 1;
122113 zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n);
122115 pCol->zCnName = zNew;
122116 memcpy(pCol->zCnName + n, zColl, nColl);
122117 pCol->colFlags |= COLFLAG_HASCOLL;
122126 if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0;
122127 z = pCol->zCnName;
122129 if( pCol->colFlags & COLFLAG_HASTYPE ){
122144 if( (pCol = pTable->aCol)!=0 ){
122145 for(i=0; i<pTable->nCol; i++, pCol++){
122146 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
122147 sqlite3DbFree(db, pCol->zCnName);
122149 sqlite3DbNNFreeNN(db, pTable->aCol);
122151 sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
122153 if( db->pnBytesFreed==0 ){
122154 pTable->aCol = 0;
122155 pTable->nCol = 0;
122157 pTable->u.tab.pDfltList = 0;
122175 ** db parameter can be used with db->pnBytesFreed to measure the memory
122191 if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
122197 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
122198 pNext = pIndex->pNext;
122199 assert( pIndex->pSchema==pTable->pSchema
122200 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
122201 if( db->pnBytesFreed==0 && !IsVirtual(pTable) ){
122202 char *zName = pIndex->zName;
122204 &pIndex->pSchema->idxHash, zName, 0
122206 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
122222 sqlite3SelectDelete(db, pTable->u.view.pSelect);
122228 sqlite3DbFree(db, pTable->zName);
122229 sqlite3DbFree(db, pTable->zColAff);
122230 sqlite3ExprListDelete(db, pTable->pCheck);
122240 if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return;
122257 assert( iDb>=0 && iDb<db->nDb );
122260 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
122261 pDb = &db->aDb[iDb];
122262 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
122264 db->mDbFlags |= DBFLAG_SchemaChange;
122283 zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n);
122299 if( p->nTab==0 ){
122300 p->nTab = 1;
122305 ** Parameter zName points to a nul-terminated buffer containing the name
122307 ** function returns the index of the named database in db->aDb[], or
122308 ** -1 if the named db cannot be found.
122311 int i = -1; /* Database number */
122314 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
122315 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
122327 ** index of the named database in db->aDb[], or -1 if the named db
122362 sqlite3 *db = pParse->db;
122365 if( pName2->n>0 ){
122366 if( db->init.busy ) {
122368 return -1;
122374 return -1;
122377 assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE
122378 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
122379 iDb = db->init.iDb;
122389 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
122390 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
122392 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
122394 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
122396 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
122400 ** This routine is used to check if the UTF-8 string zName is a legal
122416 sqlite3 *db = pParse->db;
122418 || db->init.imposterTable
122424 if( db->init.busy ){
122425 if( sqlite3_stricmp(zType, db->init.azInit[0])
122426 || sqlite3_stricmp(zName, db->init.azInit[1])
122427 || sqlite3_stricmp(zTblName, db->init.azInit[2])
122433 if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7))
122450 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
122457 ** find the (first) offset of that column in index pIdx. Or return -1
122462 for(i=0; i<pIdx->nColumn; i++){
122463 if( iCol==pIdx->aiColumn[i] ) return i;
122465 return -1;
122478 ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
122481 if( pTab->tabFlags & TF_HasVirtual ){
122484 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++;
122496 ** the N-th virtual column (zero-based) then the storage number is
122497 ** the number of non-virtual columns in the table plus N.
122513 ** -- 0 1 2 3 4 5 6 7 8
122524 ** this routine is a no-op macro. If the pTab does not have any virtual
122525 ** columns, then this routine is no-op that always return iCol. If iCol
122531 assert( iCol<pTab->nCol );
122532 if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
122534 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
122536 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
122538 return pTab->nNVCol + i - n;
122552 ** is a read-only no-op.
122555 int iReg = ++pParse->nMem;
122573 ** The new table record is initialized and put in pParse->pNewTable.
122590 sqlite3 *db = pParse->db;
122595 if( db->init.busy && db->init.newTnum==1 ){
122597 iDb = db->init.iDb;
122604 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
122616 pParse->sNameToken = *pName;
122621 if( db->init.iDb==1 ) isTemp = 1;
122632 char *zDb = db->aDb[iDb].zDbSName;
122651 char *zDb = db->aDb[iDb].zDbSName;
122661 assert( !db->init.busy || CORRUPT_DB );
122675 assert( db->mallocFailed );
122676 pParse->rc = SQLITE_NOMEM_BKPT;
122677 pParse->nErr++;
122680 pTable->zName = zName;
122681 pTable->iPKey = -1;
122682 pTable->pSchema = db->aDb[iDb].pSchema;
122683 pTable->nTabRef = 1;
122685 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
122687 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
122689 assert( pParse->pNewTable==0 );
122690 pParse->pNewTable = pTable;
122700 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
122717 reg1 = pParse->regRowid = ++pParse->nMem;
122718 reg2 = pParse->regRoot = ++pParse->nMem;
122719 reg3 = ++pParse->nMem;
122723 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
122729 /* This just creates a place-holder record in the sqlite_schema table.
122733 ** The rowid for the new entry is left in register pParse->regRowid.
122734 ** The root page number of the new table is left in reg pParse->regRoot.
122744 assert( !pParse->bReturning );
122745 pParse->u1.addrCrTab =
122756 /* Normal (non-error) return. */
122761 pParse->checkSchema = 1;
122771 if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
122772 pCol->colFlags |= COLFLAG_HIDDEN;
122773 if( pTab ) pTab->tabFlags |= TF_HasHidden;
122774 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
122775 pTab->tabFlags |= TF_OOOHidden;
122786 pHash = &(db->aDb[1].pSchema->trigHash);
122787 sqlite3HashInsert(pHash, pRet->zName, 0);
122788 sqlite3ExprListDelete(db, pRet->pReturnEL);
122811 sqlite3 *db = pParse->db;
122812 if( pParse->pNewTrigger ){
122815 assert( pParse->bReturning==0 || pParse->ifNotExists );
122817 pParse->bReturning = 1;
122823 pParse->u1.pReturning = pRet;
122824 pRet->pParse = pParse;
122825 pRet->pReturnEL = pList;
122827 testcase( pParse->earlyCleanup );
122828 if( db->mallocFailed ) return;
122829 sqlite3_snprintf(sizeof(pRet->zName), pRet->zName,
122831 pRet->retTrig.zName = pRet->zName;
122832 pRet->retTrig.op = TK_RETURNING;
122833 pRet->retTrig.tr_tm = TRIGGER_AFTER;
122834 pRet->retTrig.bReturning = 1;
122835 pRet->retTrig.pSchema = db->aDb[1].pSchema;
122836 pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
122837 pRet->retTrig.step_list = &pRet->retTStep;
122838 pRet->retTStep.op = TK_RETURNING;
122839 pRet->retTStep.pTrig = &pRet->retTrig;
122840 pRet->retTStep.pExprList = pList;
122841 pHash = &(db->aDb[1].pSchema->trigHash);
122842 assert( sqlite3HashFind(pHash, pRet->zName)==0
122843 || pParse->nErr || pParse->ifNotExists );
122844 if( sqlite3HashInsert(pHash, pRet->zName, &pRet->retTrig)
122845 ==&pRet->retTrig ){
122864 sqlite3 *db = pParse->db;
122871 if( (p = pParse->pNewTable)==0 ) return;
122872 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
122873 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
122883 && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0
122885 sType.n -= 6;
122886 while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
122888 && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0
122890 sType.n -= 9;
122891 while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
122920 for(i=0; i<p->nCol; i++){
122921 if( p->aCol[i].hName==hName && sqlite3StrICmp(z, p->aCol[i].zCnName)==0 ){
122927 aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0]));
122932 p->aCol = aNew;
122933 pCol = &p->aCol[p->nCol];
122934 memset(pCol, 0, sizeof(p->aCol[0]));
122935 pCol->zCnName = z;
122936 pCol->hName = hName;
122942 pCol->affinity = affinity;
122943 pCol->eCType = eType;
122944 pCol->szEst = szEst;
122948 pCol->colFlags |= COLFLAG_SORTERREF;
122957 pCol->affinity = sqlite3AffinityType(zType, pCol);
122958 pCol->colFlags |= COLFLAG_HASTYPE;
122960 p->nCol++;
122961 p->nNVCol++;
122962 pParse->constraintName.n = 0;
122974 p = pParse->pNewTable;
122975 if( p==0 || NEVER(p->nCol<1) ) return;
122976 pCol = &p->aCol[p->nCol-1];
122977 pCol->notNull = (u8)onError;
122978 p->tabFlags |= TF_HasNotNull;
122982 if( pCol->colFlags & COLFLAG_UNIQUE ){
122984 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
122985 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
122986 if( pIdx->aiColumn[0]==p->nCol-1 ){
122987 pIdx->uniqNotNull = 1;
122997 ** This routine does a case-independent search of zType for the
123005 ** --------------------------------
123064 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
123071 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
123076 pCol->colFlags |= COLFLAG_SORTERREF;
123081 pCol->szEst = v;
123104 sqlite3 *db = pParse->db;
123105 p = pParse->pNewTable;
123107 int isInit = db->init.busy && db->init.iDb!=1;
123108 pCol = &(p->aCol[p->nCol-1]);
123111 pCol->zCnName);
123113 }else if( pCol->colFlags & COLFLAG_GENERATED ){
123114 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
123115 testcase( pCol->colFlags & COLFLAG_STORED );
123156 if( p->op==TK_STRING ){
123157 p->op = TK_ID;
123158 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
123159 p->pLeft->op = TK_ID;
123167 pCol->colFlags |= COLFLAG_PRIMKEY;
123169 if( pCol->colFlags & COLFLAG_GENERATED ){
123170 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
123171 testcase( pCol->colFlags & COLFLAG_STORED );
123190 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
123203 Table *pTab = pParse->pNewTable;
123205 int iCol = -1, i;
123208 if( pTab->tabFlags & TF_HasPrimaryKey ){
123210 "table \"%s\" has more than one primary key", pTab->zName);
123213 pTab->tabFlags |= TF_HasPrimaryKey;
123215 iCol = pTab->nCol - 1;
123216 pCol = &pTab->aCol[iCol];
123220 nTerm = pList->nExpr;
123222 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
123225 if( pCExpr->op==TK_ID ){
123228 zCName = pCExpr->u.zToken;
123229 for(iCol=0; iCol<pTab->nCol; iCol++){
123230 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zCnName)==0 ){
123231 pCol = &pTab->aCol[iCol];
123241 && pCol->eCType==COLTYPE_INTEGER
123245 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
123246 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
123248 pTab->iPKey = iCol;
123249 pTab->keyConf = (u8)onError;
123251 pTab->tabFlags |= autoInc*TF_Autoincrement;
123252 if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags;
123266 sqlite3ExprListDelete(pParse->db, pList);
123280 Table *pTab = pParse->pNewTable;
123281 sqlite3 *db = pParse->db;
123283 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
123285 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
123286 if( pParse->constraintName.n ){
123287 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
123291 while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; }
123293 t.n = (int)(zEnd - t.z);
123294 sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1);
123299 sqlite3ExprDelete(pParse->db, pCheckExpr);
123313 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return;
123314 i = p->nCol-1;
123315 db = pParse->db;
123321 sqlite3ColumnSetColl(db, &p->aCol[i], zColl);
123327 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
123328 assert( pIdx->nKeyCol==1 );
123329 if( pIdx->aiColumn[0]==i ){
123330 pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]);
123343 Table *pTab = pParse->pNewTable;
123349 pCol = &(pTab->aCol[pTab->nCol-1]);
123354 if( pCol->iDflt>0 ) goto generated_error;
123356 if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){
123357 /* no-op */
123358 }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){
123364 if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
123365 pCol->colFlags |= eType;
123368 pTab->tabFlags |= eType;
123369 if( pCol->colFlags & COLFLAG_PRIMKEY ){
123372 if( ALWAYS(pExpr) && pExpr->op==TK_ID ){
123379 if( pExpr && pExpr->op!=TK_RAISE ) pExpr->affExpr = pCol->affinity;
123386 pCol->zCnName);
123388 sqlite3ExprDelete(pParse->db, pExpr);
123391 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
123393 sqlite3ExprDelete(pParse->db, pExpr);
123407 ** This plan is not completely bullet-proof. It is possible for
123413 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
123414 ** the schema-version whenever the schema changes.
123417 sqlite3 *db = pParse->db;
123418 Vdbe *v = pParse->pVdbe;
123421 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
123444 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
123451 ** it is quoted using double-quotes.
123487 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
123488 n += identLength(pCol->zCnName) + 5;
123490 n += identLength(p->zName);
123500 n += 35 + 6*p->nCol;
123508 identPut(zStmt, &k, p->zName);
123510 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
123522 sqlite3_snprintf(n-k, &zStmt[k], zSep);
123525 identPut(zStmt, &k, pCol->zCnName);
123526 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
123527 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
123528 testcase( pCol->affinity==SQLITE_AFF_BLOB );
123529 testcase( pCol->affinity==SQLITE_AFF_TEXT );
123530 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
123531 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
123532 testcase( pCol->affinity==SQLITE_AFF_REAL );
123533 testcase( pCol->affinity==SQLITE_AFF_FLEXNUM );
123535 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
123537 assert( pCol->affinity==SQLITE_AFF_BLOB
123538 || pCol->affinity==SQLITE_AFF_FLEXNUM
123539 || pCol->affinity==sqlite3AffinityType(zType, 0) );
123544 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
123555 if( pIdx->nColumn>=N ) return SQLITE_OK;
123556 assert( pIdx->isResized==0 );
123560 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
123561 pIdx->azColl = (const char**)zExtra;
123563 memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1));
123564 pIdx->aiRowLogEst = (LogEst*)zExtra;
123566 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
123567 pIdx->aiColumn = (i16*)zExtra;
123569 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
123570 pIdx->aSortOrder = (u8*)zExtra;
123571 pIdx->nColumn = N;
123572 pIdx->isResized = 1;
123583 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
123584 wTable += pTabCol->szEst;
123586 if( pTab->iPKey<0 ) wTable++;
123587 pTab->szTabRow = sqlite3LogEst(wTable*4);
123596 const Column *aCol = pIdx->pTable->aCol;
123597 for(i=0; i<pIdx->nColumn; i++){
123598 i16 x = pIdx->aiColumn[i];
123599 assert( x<pIdx->pTable->nCol );
123602 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
123610 while( nCol-- > 0 ){
123620 ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
123633 assert( nKey<=pIdx->nColumn );
123634 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
123635 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
123636 assert( pPk->pTable->tabFlags & TF_WithoutRowid );
123637 assert( pPk->pTable==pIdx->pTable );
123639 j = pPk->aiColumn[iCol];
123642 assert( pIdx->aiColumn[i]>=0 || j>=0 );
123643 if( pIdx->aiColumn[i]==j
123644 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
123657 ** high-order bit of colNotIdxed is always 1. All unindexed columns
123660 ** 2019-10-24: For the purpose of this computation, virtual columns are
123668 ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
123674 Table *pTab = pIdx->pTable;
123675 for(j=pIdx->nColumn-1; j>=0; j--){
123676 int x = pIdx->aiColumn[j];
123677 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
123678 testcase( x==BMS-1 );
123679 testcase( x==BMS-2 );
123680 if( x<BMS-1 ) m |= MASKBIT(x);
123683 pIdx->colNotIdxed = ~m;
123684 assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */
123717 sqlite3 *db = pParse->db;
123718 Vdbe *v = pParse->pVdbe;
123722 if( !db->init.imposterTable ){
123723 for(i=0; i<pTab->nCol; i++){
123724 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
123725 && (pTab->aCol[i].notNull==OE_None)
123727 pTab->aCol[i].notNull = OE_Abort;
123730 pTab->tabFlags |= TF_HasNotNull;
123736 assert( !pParse->bReturning );
123737 if( pParse->u1.addrCrTab ){
123739 sqlite3VdbeChangeP3(v, pParse->u1.addrCrTab, BTREE_BLOBKEY);
123745 if( pTab->iPKey>=0 ){
123748 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName);
123752 pTab->tabFlags &= ~TF_WithoutRowid;
123756 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
123758 pList->a[0].fg.sortFlags = pParse->iPkSortOrder;
123759 assert( pParse->pNewTable==pTab );
123760 pTab->iPKey = -1;
123761 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
123763 if( pParse->nErr ){
123764 pTab->tabFlags &= ~TF_WithoutRowid;
123767 assert( db->mallocFailed==0 );
123769 assert( pPk->nKeyCol==1 );
123779 for(i=j=1; i<pPk->nKeyCol; i++){
123781 pPk->nColumn--;
123783 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
123784 pPk->azColl[j] = pPk->azColl[i];
123785 pPk->aSortOrder[j] = pPk->aSortOrder[i];
123786 pPk->aiColumn[j++] = pPk->aiColumn[i];
123789 pPk->nKeyCol = j;
123792 pPk->isCovering = 1;
123793 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
123794 nPk = pPk->nColumn = pPk->nKeyCol;
123800 if( v && pPk->tnum>0 ){
123801 assert( db->init.busy==0 );
123802 sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto);
123806 pPk->tnum = pTab->tnum;
123808 /* Update the in-memory representation of all UNIQUE indices by converting
123811 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
123815 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
123816 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
123822 pIdx->nColumn = pIdx->nKeyCol;
123825 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
123826 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
123827 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
123828 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
123829 pIdx->aiColumn[j] = pPk->aiColumn[i];
123830 pIdx->azColl[j] = pPk->azColl[i];
123831 if( pPk->aSortOrder[i] ){
123833 pIdx->bAscKeyBug = 1;
123838 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
123839 assert( pIdx->nColumn>=j );
123845 for(i=0; i<pTab->nCol; i++){
123846 if( !hasColumn(pPk->aiColumn, nPk, i)
123847 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
123850 for(i=0, j=nPk; i<pTab->nCol; i++){
123851 if( !hasColumn(pPk->aiColumn, j, i)
123852 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
123854 assert( j<pPk->nColumn );
123855 pPk->aiColumn[j] = i;
123856 pPk->azColl[j] = sqlite3StrBINARY;
123860 assert( pPk->nColumn==j );
123861 assert( pTab->nNVCol<=j );
123876 nName = sqlite3Strlen30(pTab->zName);
123877 if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
123879 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
123881 if( pMod->pModule->iVersion<3 ) return 0;
123882 if( pMod->pModule->xShadowName==0 ) return 0;
123883 return pMod->pModule->xShadowName(zName+nName+1);
123895 int nName; /* Length of pTab->zName */
123900 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
123902 if( NEVER(pMod->pModule==0) ) return;
123903 if( pMod->pModule->iVersion<3 ) return;
123904 if( pMod->pModule->xShadowName==0 ) return;
123905 assert( pTab->zName!=0 );
123906 nName = sqlite3Strlen30(pTab->zName);
123907 for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){
123909 assert( pOther->zName!=0 );
123911 if( pOther->tabFlags & TF_Shadow ) continue;
123912 if( sqlite3StrNICmp(pOther->zName, pTab->zName, nName)==0
123913 && pOther->zName[nName]=='_'
123914 && pMod->pModule->xShadowName(pOther->zName+nName+1)
123916 pOther->tabFlags |= TF_Shadow;
123968 #define markExprListImmutable(X) /* no-op */
123981 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
124000 sqlite3 *db = pParse->db; /* The database connection */
124007 p = pParse->pNewTable;
124010 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
124011 p->tabFlags |= TF_Shadow;
124014 /* If the db->init.busy is 1 it means we are reading the SQL off the
124017 ** for the table from the db->init.newTnum field. (The page number
124021 ** table itself. So mark it read-only.
124023 if( db->init.busy ){
124024 if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){
124028 p->tnum = db->init.newTnum;
124029 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
124043 p->tabFlags |= TF_Strict;
124044 for(ii=0; ii<p->nCol; ii++){
124045 Column *pCol = &p->aCol[ii];
124046 if( pCol->eCType==COLTYPE_CUSTOM ){
124047 if( pCol->colFlags & COLFLAG_HASTYPE ){
124050 p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "")
124054 p->zName, pCol->zCnName);
124057 }else if( pCol->eCType==COLTYPE_ANY ){
124058 pCol->affinity = SQLITE_AFF_BLOB;
124060 if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0
124061 && p->iPKey!=ii
124062 && pCol->notNull == OE_None
124064 pCol->notNull = OE_Abort;
124065 p->tabFlags |= TF_HasNotNull;
124070 assert( (p->tabFlags & TF_HasPrimaryKey)==0
124071 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
124072 assert( (p->tabFlags & TF_HasPrimaryKey)!=0
124073 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
124077 if( (p->tabFlags & TF_Autoincrement) ){
124082 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
124083 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
124086 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
124089 iDb = sqlite3SchemaToIndex(db, p->pSchema);
124094 if( p->pCheck ){
124095 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
124096 if( pParse->nErr ){
124099 sqlite3ExprListDelete(db, p->pCheck);
124100 p->pCheck = 0;
124102 markExprListImmutable(p->pCheck);
124107 if( p->tabFlags & TF_HasGenerated ){
124109 testcase( p->tabFlags & TF_HasVirtual );
124110 testcase( p->tabFlags & TF_HasStored );
124111 for(ii=0; ii<p->nCol; ii++){
124112 u32 colFlags = p->aCol[ii].colFlags;
124114 Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]);
124124 sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii],
124132 sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
124140 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
124150 if( !db->init.busy ){
124178 ** statement to populate the new table. The root-page number for the
124179 ** new table is in register pParse->regRoot.
124185 ** A shared-cache write-lock is not required to write to the new table,
124186 ** as a schema-lock must have already been obtained to create it. Since
124187 ** a schema-lock excludes all other database users, the write-lock would
124192 int regYield; /* Register holding co-routine entry-point */
124193 int addrTop; /* Top of the co-routine */
124201 pParse->rc = SQLITE_ERROR;
124202 pParse->nErr++;
124205 iCsr = pParse->nTab++;
124206 regYield = ++pParse->nMem;
124207 regRec = ++pParse->nMem;
124208 regRowid = ++pParse->nMem;
124210 sqlite3VdbeAddOp3(v, OP_OpenWrite, iCsr, pParse->regRoot, iDb);
124214 if( pParse->nErr ) return;
124217 assert( p->aCol==0 );
124218 p->nCol = p->nNVCol = pSelTab->nCol;
124219 p->aCol = pSelTab->aCol;
124220 pSelTab->nCol = 0;
124221 pSelTab->aCol = 0;
124225 if( pParse->nErr ) return;
124227 sqlite3VdbeJumpHere(v, addrTop - 1);
124243 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
124244 n = (int)(pEnd2->z - pParse->sNameToken.z);
124245 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
124247 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
124259 db->aDb[iDb].zDbSName,
124261 p->zName,
124262 p->zName,
124263 pParse->regRoot,
124265 pParse->regRowid
124274 if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){
124275 Db *pDb = &db->aDb[iDb];
124277 if( pDb->pSchema->pSeqTab==0 ){
124280 pDb->zDbSName
124288 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0);
124292 if( p->tabFlags & TF_HasGenerated ){
124295 db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC);
124299 /* Add the table to the in-memory representation of the database.
124301 if( db->init.busy ){
124303 Schema *pSchema = p->pSchema;
124305 assert( HasRowid(p) || p->iPKey<0 );
124306 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
124312 pParse->pNewTable = 0;
124313 db->mDbFlags |= DBFLAG_SchemaChange;
124318 assert( !pParse->nested );
124320 if( strcmp(p->zName, "sqlite_sequence")==0 ){
124322 p->pSchema->pSeqTab = p;
124330 if( pCons->z==0 ){
124333 p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
124359 sqlite3 *db = pParse->db;
124361 if( pParse->nVar>0 ){
124366 p = pParse->pNewTable;
124367 if( p==0 || pParse->nErr ) goto create_view_fail;
124372 ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
124376 p->tabFlags |= sqlite3Config.mNoVisibleRowid; /* Optional. Allow by default */
124378 p->tabFlags |= TF_NoVisibleRowid; /* Never allow rowid in view */
124382 iDb = sqlite3SchemaToIndex(db, p->pSchema);
124388 ** allocated rather than point to the input string - which means that
124391 pSelect->selFlags |= SF_View;
124393 p->u.view.pSelect = pSelect;
124396 p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
124398 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
124399 p->eTabType = TABTYP_VIEW;
124400 if( db->mallocFailed ) goto create_view_fail;
124405 sEnd = pParse->sLastToken;
124411 n = (int)(sEnd.z - pBegin->z);
124413 z = pBegin->z;
124414 while( sqlite3Isspace(z[n-1]) ){ n--; }
124415 sEnd.z = &z[n-1];
124434 ** the columns of the view in the pTable structure. Return non-zero if
124436 ** in pParse->zErrMsg.
124442 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
124454 db->nSchemaLock++;
124456 db->nSchemaLock--;
124466 assert( pTable->nCol<=0 );
124483 if( pTable->nCol<0 ){
124484 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
124487 assert( pTable->nCol>=0 );
124497 pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0);
124499 u8 eParseMode = pParse->eParseMode;
124500 int nTab = pParse->nTab;
124501 int nSelect = pParse->nSelect;
124502 pParse->eParseMode = PARSE_MODE_NORMAL;
124503 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
124504 pTable->nCol = -1;
124507 xAuth = db->xAuth;
124508 db->xAuth = 0;
124510 db->xAuth = xAuth;
124514 pParse->nTab = nTab;
124515 pParse->nSelect = nSelect;
124517 pTable->nCol = 0;
124519 }else if( pTable->pCheck ){
124522 ** arglist which is stored in pTable->pCheck. The pCheck field
124526 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
124527 &pTable->nCol, &pTable->aCol);
124528 if( pParse->nErr==0
124529 && pTable->nCol==pSel->pEList->nExpr
124531 assert( db->mallocFailed==0 );
124538 assert( pTable->aCol==0 );
124539 pTable->nCol = pSelTab->nCol;
124540 pTable->aCol = pSelTab->aCol;
124541 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT);
124542 pSelTab->nCol = 0;
124543 pSelTab->aCol = 0;
124544 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
124546 pTable->nNVCol = pTable->nCol;
124550 pParse->eParseMode = eParseMode;
124554 pTable->pSchema->schemaFlags |= DB_UnresetViews;
124555 if( db->mallocFailed ){
124559 return nErr + pParse->nErr;
124563 if( !IsVirtual(pTable) && pTable->nCol>0 ) return 0;
124576 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
124591 ** root-page of a table or index in database iDb has changed from iFrom
124612 pDb = &db->aDb[iDb];
124613 pHash = &pDb->pSchema->tblHash;
124616 if( pTab->tnum==iFrom ){
124617 pTab->tnum = iTo;
124620 pHash = &pDb->pSchema->idxHash;
124623 if( pIdx->tnum==iFrom ){
124624 pIdx->tnum = iTo;
124631 ** Write code to erase the table with root-page iTable from database iDb.
124633 ** if a root-page of another table is moved by the btree-layer whilst
124634 ** erasing iTable (this can happen with an auto-vacuum database).
124644 ** is non-zero, then it is the root page number of a table moved to
124655 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
124663 ** in case a root-page belonging to another table is moved by the btree layer
124664 ** is also added (this can happen with an auto-vacuum database).
124667 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
124669 ** table and index root-pages in order, starting with the numerically
124670 ** largest root-page number. This guarantees that none of the root-pages
124678 ** and root page 5 happened to be the largest root-page number in the
124681 ** a free-list page.
124683 Pgno iTab = pTab->tnum;
124693 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
124694 Pgno iIdx = pIdx->tnum;
124695 assert( pIdx->pSchema==pTab->pSchema );
124703 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
124704 assert( iDb>=0 && iDb<pParse->db->nDb );
124722 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
124726 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
124740 sqlite3 *db = pParse->db;
124742 Db *pDb = &db->aDb[iDb];
124760 assert( pTrigger->pSchema==pTab->pSchema ||
124761 pTrigger->pSchema==db->aDb[1].pSchema );
124763 pTrigger = pTrigger->pNext;
124770 ** move as a result of the drop (can happen in auto-vacuum mode).
124772 if( pTab->tabFlags & TF_Autoincrement ){
124775 pDb->zDbSName, pTab->zName
124790 pDb->zDbSName, pTab->zName);
124799 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
124802 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
124808 ** Return TRUE if shadow tables should be read-only in the current
124813 if( (db->flags & SQLITE_Defensive)!=0
124814 && db->pVtabCtx==0
124815 && db->nVdbeExec==0
124828 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
124829 if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0;
124830 if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
124833 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
124836 if( pTab->tabFlags & TF_Eponymous ){
124849 sqlite3 *db = pParse->db;
124852 if( db->mallocFailed ){
124855 assert( pParse->nErr==0 );
124856 assert( pName->nSrc==1 );
124858 if( noErr ) db->suppressErr++;
124860 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
124861 if( noErr ) db->suppressErr--;
124865 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
124870 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
124871 assert( iDb>=0 && iDb<db->nDb );
124883 const char *zDb = db->aDb[iDb].zDbSName;
124897 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
124906 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
124909 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
124915 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
124924 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
124928 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
124940 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
124961 ** under construction in the pParse->pNewTable field.
124973 sqlite3 *db = pParse->db;
124977 Table *p = pParse->pNewTable;
124986 int iCol = p->nCol-1;
124988 if( pToCol && pToCol->nExpr!=1 ){
124991 p->aCol[iCol].zCnName, pTo);
124995 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
125001 nCol = pFromCol->nExpr;
125003 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
125005 for(i=0; i<pToCol->nExpr; i++){
125006 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
125013 pFKey->pFrom = p;
125015 pFKey->pNextFrom = p->u.tab.pFKey;
125016 z = (char*)&pFKey->aCol[nCol];
125017 pFKey->zTo = z;
125021 memcpy(z, pTo->z, pTo->n);
125022 z[pTo->n] = 0;
125024 z += pTo->n+1;
125025 pFKey->nCol = nCol;
125027 pFKey->aCol[0].iFrom = p->nCol-1;
125031 for(j=0; j<p->nCol; j++){
125032 if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
125033 pFKey->aCol[i].iFrom = j;
125037 if( j>=p->nCol ){
125040 pFromCol->a[i].zEName);
125044 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
125050 int n = sqlite3Strlen30(pToCol->a[i].zEName);
125051 pFKey->aCol[i].zCol = z;
125053 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
125055 memcpy(z, pToCol->a[i].zEName, n);
125060 pFKey->isDeferred = 0;
125061 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
125062 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
125064 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
125065 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
125066 pFKey->zTo, (void *)pFKey
125073 assert( pNextTo->pPrevTo==0 );
125074 pFKey->pNextTo = pNextTo;
125075 pNextTo->pPrevTo = pFKey;
125081 p->u.tab.pFKey = pFKey;
125102 if( (pTab = pParse->pNewTable)==0 ) return;
125104 if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
125105 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
125106 pFKey->isDeferred = (u8)isDeferred;
125119 ** the root page number of the index is taken from pIndex->tnum.
125122 Table *pTab = pIndex->pTable; /* The table that is indexed */
125123 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
125124 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
125133 sqlite3 *db = pParse->db; /* The database connection */
125134 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
125137 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
125138 db->aDb[iDb].zDbSName ) ){
125143 /* Require a write-lock on the table to perform this operation */
125144 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
125151 tnum = pIndex->tnum;
125154 assert( pKey!=0 || pParse->nErr );
125157 iSorter = pParse->nTab++;
125158 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
125184 pIndex->nKeyCol); VdbeCoverage(v);
125199 if( !pIndex->bAscKeyBug ){
125224 ** of 8-byte aligned space after the Index object and return a
125244 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
125245 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
125246 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
125247 p->aSortOrder = (u8*)pExtra;
125248 p->nColumn = nCol;
125249 p->nKeyCol = nCol - 1;
125258 ** pParse and return non-zero. Otherwise, return zero.
125263 for(i=0; i<pList->nExpr; i++){
125264 if( pList->a[i].fg.bNulls ){
125265 u8 sf = pList->a[i].fg.sortFlags;
125280 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
125281 ** as the table to be indexed. pParse->pNewTable is a table that is
125285 ** is a primary key or unique-constraint on the most recent column added
125292 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
125308 sqlite3 *db = pParse->db;
125318 assert( db->pParse==pParse );
125319 if( pParse->nErr ){
125322 assert( db->mallocFailed==0 );
125338 /* Use the two-part index name to determine the database
125345 assert( pName && pName->z );
125352 if( !db->init.busy ){
125354 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
125366 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
125367 assert( db->mallocFailed==0 || pTab==0 );
125369 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
125371 "cannot create a TEMP index on non-TEMP table \"%s\"",
125372 pTab->zName);
125379 pTab = pParse->pNewTable;
125381 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
125383 pDb = &db->aDb[iDb];
125386 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
125387 && db->init.busy==0
125390 && sqlite3UserAuthTable(pTab->zName)==0
125393 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
125425 assert( pName->z!=0 );
125426 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
125430 if( !db->init.busy ){
125431 if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){
125436 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
125440 assert( !db->init.busy );
125450 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
125451 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
125468 const char *zDb = pDb->zDbSName;
125474 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
125486 Column *pCol = &pTab->aCol[pTab->nCol-1];
125487 pCol->colFlags |= COLFLAG_UNIQUE;
125488 sqlite3TokenInit(&prevCol, pCol->zCnName);
125492 assert( pList->nExpr==1 );
125496 if( pParse->nErr ) goto exit_create_index;
125502 for(i=0; i<pList->nExpr; i++){
125503 Expr *pExpr = pList->a[i].pExpr;
125505 if( pExpr->op==TK_COLLATE ){
125507 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
125515 nExtraCol = pPk ? pPk->nKeyCol : 1;
125516 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
125517 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
125519 if( db->mallocFailed ){
125522 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
125523 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
125524 pIndex->zName = zExtra;
125526 memcpy(pIndex->zName, zName, nName+1);
125527 pIndex->pTable = pTab;
125528 pIndex->onError = (u8)onError;
125529 pIndex->uniqNotNull = onError!=OE_None;
125530 pIndex->idxType = idxType;
125531 pIndex->pSchema = db->aDb[iDb].pSchema;
125532 pIndex->nKeyCol = pList->nExpr;
125535 pIndex->pPartIdxWhere = pPIWhere;
125542 if( pDb->pSchema->file_format>=4 ){
125543 sortOrderMask = -1; /* Honor DESC */
125551 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
125557 pListItem = pList->a;
125559 pIndex->aColExpr = pList;
125562 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
125563 Expr *pCExpr; /* The i-th index expression */
125564 int requestedSortOrder; /* ASC or DESC on the i-th expression */
125567 sqlite3StringToId(pListItem->pExpr);
125568 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
125569 if( pParse->nErr ) goto exit_create_index;
125570 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
125571 if( pCExpr->op!=TK_COLUMN ){
125572 if( pTab==pParse->pNewTable ){
125577 if( pIndex->aColExpr==0 ){
125578 pIndex->aColExpr = pList;
125582 pIndex->aiColumn[i] = XN_EXPR;
125583 pIndex->uniqNotNull = 0;
125584 pIndex->bHasExpr = 1;
125586 j = pCExpr->iColumn;
125589 j = pTab->iPKey;
125591 if( pTab->aCol[j].notNull==0 ){
125592 pIndex->uniqNotNull = 0;
125594 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
125595 pIndex->bHasVCol = 1;
125596 pIndex->bHasExpr = 1;
125599 pIndex->aiColumn[i] = (i16)j;
125602 if( pListItem->pExpr->op==TK_COLLATE ){
125604 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) );
125605 zColl = pListItem->pExpr->u.zToken;
125611 nExtra -= nColl;
125613 zColl = sqlite3ColumnColl(&pTab->aCol[j]);
125616 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
125619 pIndex->azColl[i] = zColl;
125620 requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask;
125621 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
125629 for(j=0; j<pPk->nKeyCol; j++){
125630 int x = pPk->aiColumn[j];
125632 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
125633 pIndex->nColumn--;
125635 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
125636 pIndex->aiColumn[i] = x;
125637 pIndex->azColl[i] = pPk->azColl[j];
125638 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
125642 assert( i==pIndex->nColumn );
125644 pIndex->aiColumn[i] = XN_ROWID;
125645 pIndex->azColl[i] = sqlite3StrBINARY;
125648 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
125653 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
125655 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
125656 pIndex->isCovering = 1;
125657 for(j=0; j<pTab->nCol; j++){
125658 if( j==pTab->iPKey ) continue;
125660 pIndex->isCovering = 0;
125665 if( pTab==pParse->pNewTable ){
125688 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
125691 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
125694 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
125695 for(k=0; k<pIdx->nKeyCol; k++){
125698 assert( pIdx->aiColumn[k]>=0 );
125699 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
125700 z1 = pIdx->azColl[k];
125701 z2 = pIndex->azColl[k];
125704 if( k==pIdx->nKeyCol ){
125705 if( pIdx->onError!=pIndex->onError ){
125713 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
125717 if( pIdx->onError==OE_Default ){
125718 pIdx->onError = pIndex->onError;
125721 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
125723 pIndex->pNext = pParse->pNewIndex;
125724 pParse->pNewIndex = pIndex;
125735 ** in-memory database structures.
125737 assert( pParse->nErr==0 );
125738 if( db->init.busy ){
125741 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
125743 pIndex->tnum = db->init.newTnum;
125746 pParse->rc = SQLITE_CORRUPT_BKPT;
125750 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
125751 pIndex->zName, pIndex);
125757 db->mDbFlags |= DBFLAG_SchemaChange;
125776 int iMem = ++pParse->nMem;
125789 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop);
125797 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
125798 if( pName->z[n-1]==';' ) n--;
125801 onError==OE_None ? "" : " UNIQUE", n, pName->z);
125812 db->aDb[iDb].zDbSName,
125813 pIndex->zName,
125814 pTab->zName,
125821 ** to invalidate all pre-compiled statements.
125827 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0);
125831 sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
125834 if( db->init.busy || pTblName==0 ){
125835 pIndex->pNext = pTab->pIndex;
125836 pTab->pIndex = pIndex;
125840 assert( pParse->pNewIndex==0 );
125841 pParse->pNewIndex = pIndex;
125855 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
125857 if( pThis->onError!=OE_Replace ) continue;
125858 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){
125860 pThis->pNext = pNext->pNext;
125861 pNext->pNext = pThis;
125862 ppFrom = &pNext->pNext;
125870 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
125871 assert( pThis->onError!=OE_Replace
125872 || pThis->pNext==0
125873 || pThis->pNext->onError==OE_Replace );
125884 ** Fill the Index.aiRowEst[] array with default information - information
125894 ** aiRowEst[N]<=aiRowEst[N-1]
125904 LogEst *a = pIdx->aiRowLogEst;
125906 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
125910 assert( !pIdx->hasStat1 );
125916 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
125922 x = pIdx->pTable->nRowLogEst;
125925 pIdx->pTable->nRowLogEst = x = 99;
125927 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); }
125933 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
125938 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
125948 sqlite3 *db = pParse->db;
125951 if( db->mallocFailed ){
125954 assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */
125955 assert( pName->nSrc==1 );
125959 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
125962 sqlite3ErrorMsg(pParse, "no such index: %S", pName->a);
125964 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
125967 pParse->checkSchema = 1;
125970 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
125975 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
125979 Table *pTab = pIndex->pTable;
125980 const char *zDb = db->aDb[iDb].zDbSName;
125986 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
125998 db->aDb[iDb].zDbSName, pIndex->zName
126000 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
126002 destroyRootPage(pParse, pIndex->tnum, iDb);
126003 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
126016 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
126024 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
126036 if( (n & (n-1))==0 ){
126040 *pIdx = -1;
126058 sqlite3 *db = pParse->db;
126066 sizeof(IdList) + pList->nId*sizeof(pList->a));
126073 i = pList->nId++;
126074 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
126075 if( IN_RENAME_OBJECT && pList->a[i].zName ){
126076 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
126088 assert( pList->eU4!=EU4_EXPR ); /* EU4_EXPR mode is not currently used */
126089 for(i=0; i<pList->nId; i++){
126090 sqlite3DbFree(db, pList->a[i].zName);
126096 ** Return the index in pList of the identifier named zId. Return -1
126102 for(i=0; i<pList->nId; i++){
126103 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
126105 return -1;
126114 ** appropriate for small and memory-limited applications.
126143 int nExtra, /* Number of new slots to add to pSrc->a[] */
126144 int iStart /* Index in pSrc->a[] of first new slot */
126152 assert( iStart<=pSrc->nSrc );
126155 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
126157 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
126158 sqlite3 *db = pParse->db;
126160 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
126167 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
126169 assert( db->mallocFailed );
126173 pSrc->nAlloc = nAlloc;
126178 for(i=pSrc->nSrc-1; i>=iStart; i--){
126179 pSrc->a[i+nExtra] = pSrc->a[i];
126181 pSrc->nSrc += nExtra;
126184 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
126186 pSrc->a[i].iCursor = -1;
126239 assert( pParse->db!=0 );
126240 db = pParse->db;
126242 pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
126244 pList->nAlloc = 1;
126245 pList->nSrc = 1;
126246 memset(&pList->a[0], 0, sizeof(pList->a[0]));
126247 pList->a[0].iCursor = -1;
126249 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
126257 pItem = &pList->a[pList->nSrc-1];
126258 if( pDatabase && pDatabase->z==0 ){
126262 pItem->zName = sqlite3NameFromToken(db, pDatabase);
126263 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
126265 pItem->zName = sqlite3NameFromToken(db, pTable);
126266 pItem->zDatabase = 0;
126277 assert( pList || pParse->db->mallocFailed );
126279 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
126280 if( pItem->iCursor>=0 ) continue;
126281 pItem->iCursor = pParse->nTab++;
126282 if( pItem->pSelect ){
126283 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
126297 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
126298 if( pItem->zDatabase ) sqlite3DbNNFreeNN(db, pItem->zDatabase);
126299 if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName);
126300 if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias);
126301 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
126302 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
126303 sqlite3DeleteTable(db, pItem->pTab);
126304 if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
126305 if( pItem->fg.isUsing ){
126306 sqlite3IdListDelete(db, pItem->u3.pUsing);
126307 }else if( pItem->u3.pOn ){
126308 sqlite3ExprDelete(db, pItem->u3.pOn);
126320 ** pDatabase is NULL if the database name qualifier is missing - the
126335 Token *pAlias, /* The right-hand side of the AS subexpression */
126340 sqlite3 *db = pParse->db;
126341 if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){
126343 (pOnUsing->pOn ? "ON" : "USING")
126351 assert( p->nSrc>0 );
126352 pItem = &p->a[p->nSrc-1];
126354 assert( pItem->zName==0 || pDatabase!=0 );
126355 if( IN_RENAME_OBJECT && pItem->zName ){
126356 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
126357 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
126360 if( pAlias->n ){
126361 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
126364 pItem->pSelect = pSubquery;
126365 if( pSubquery->selFlags & SF_NestedFrom ){
126366 pItem->fg.isNestedFrom = 1;
126369 assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 );
126370 assert( pItem->fg.isUsing==0 );
126372 pItem->u3.pOn = 0;
126373 }else if( pOnUsing->pUsing ){
126374 pItem->fg.isUsing = 1;
126375 pItem->u3.pUsing = pOnUsing->pUsing;
126377 pItem->u3.pOn = pOnUsing->pOn;
126390 ** element of the source-list passed as the second argument.
126394 if( p && pIndexedBy->n>0 ){
126396 assert( p->nSrc>0 );
126397 pItem = &p->a[p->nSrc-1];
126398 assert( pItem->fg.notIndexed==0 );
126399 assert( pItem->fg.isIndexedBy==0 );
126400 assert( pItem->fg.isTabFunc==0 );
126401 if( pIndexedBy->n==1 && !pIndexedBy->z ){
126404 pItem->fg.notIndexed = 1;
126406 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
126407 pItem->fg.isIndexedBy = 1;
126408 assert( pItem->fg.isCte==0 ); /* No collision on union u2 */
126419 assert( p1 && p1->nSrc==1 );
126421 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
126423 sqlite3SrcListDelete(pParse->db, p2);
126426 memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem));
126427 sqlite3DbFree(pParse->db, p2);
126428 p1->a[0].fg.jointype |= (JT_LTORJ & p1->a[1].fg.jointype);
126436 ** table-valued-function.
126440 SrcItem *pItem = &p->a[p->nSrc-1];
126441 assert( pItem->fg.notIndexed==0 );
126442 assert( pItem->fg.isIndexedBy==0 );
126443 assert( pItem->fg.isTabFunc==0 );
126444 pItem->u1.pFuncArg = pList;
126445 pItem->fg.isTabFunc = 1;
126447 sqlite3ExprListDelete(pParse->db, pList);
126463 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
126468 ** * All tables to the left of the right-most RIGHT JOIN are tagged with
126475 if( p && p->nSrc>1 ){
126476 int i = p->nSrc-1;
126479 allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype;
126480 }while( (--i)>0 );
126481 p->a[0].fg.jointype = 0;
126486 for(i=p->nSrc-1; ALWAYS(i>0) && (p->a[i].fg.jointype&JT_RIGHT)==0; i--){}
126487 i--;
126490 p->a[i].fg.jointype |= JT_LTORJ;
126491 }while( (--i)>=0 );
126505 db = pParse->db;
126513 for(i=0; i<db->nDb; i++){
126515 Btree *pBt = db->aDb[i].pBt;
126540 assert( pParse->db!=0 );
126558 char *zName = sqlite3NameFromToken(pParse->db, pName);
126566 sqlite3DbFree(pParse->db, zName);
126578 sqlite3 *db = pParse->db;
126579 if( db->aDb[1].pBt==0 && !pParse->explain ){
126589 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
126593 pParse->rc = rc;
126596 db->aDb[1].pBt = pBt;
126597 assert( db->aDb[1].pSchema );
126598 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
126609 ** will occur at the end of the top-level VDBE and will be generated
126613 assert( iDb>=0 && iDb<pToplevel->db->nDb );
126614 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 );
126616 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) );
126617 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
126618 DbMaskSet(pToplevel->cookieMask, iDb);
126634 sqlite3 *db = pParse->db;
126636 for(i=0; i<db->nDb; i++){
126637 Db *pDb = &db->aDb[i];
126638 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
126660 DbMaskSet(pToplevel->writeMask, iDb);
126661 pToplevel->isMultiWrite |= setStatement;
126673 pToplevel->isMultiWrite = 1;
126694 pToplevel->mayAbort = 1;
126711 assert( pParse->pVdbe!=0 );
126713 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
126732 Table *pTab = pIdx->pTable;
126734 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
126735 pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
126736 if( pIdx->aColExpr ){
126737 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
126739 for(j=0; j<pIdx->nKeyCol; j++){
126741 assert( pIdx->aiColumn[j]>=0 );
126742 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
126744 sqlite3_str_appendall(&errMsg, pTab->zName);
126758 ** Code an OP_Halt due to non-unique rowid.
126763 Table *pTab /* The table with the non-unique rowid */
126767 if( pTab->iPKey>=0 ){
126768 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
126769 pTab->aCol[pTab->iPKey].zCnName);
126772 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
126787 for(i=0; i<pIndex->nColumn; i++){
126788 const char *z = pIndex->azColl[i];
126789 assert( z!=0 || pIndex->aiColumn[i]<0 );
126790 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
126807 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
126809 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
126811 sqlite3RefillIndex(pParse, pIndex, -1);
126827 sqlite3 *db = pParse->db; /* The database connection */
126832 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
126834 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
126845 ** REINDEX -- 1
126846 ** REINDEX <collation> -- 2
126847 ** REINDEX ?<database>.?<tablename> -- 3
126848 ** REINDEX ?<database>.?<indexname> -- 4
126863 sqlite3 *db = pParse->db; /* The database connection */
126875 }else if( NEVER(pName2==0) || pName2->z==0 ){
126877 assert( pName1->z );
126878 zColl = sqlite3NameFromToken(pParse->db, pName1);
126892 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
126902 iDb = sqlite3SchemaToIndex(db, pIndex->pTable->pSchema);
126904 sqlite3RefillIndex(pParse, pIndex, -1);
126919 int nCol = pIdx->nColumn;
126920 int nKey = pIdx->nKeyCol;
126922 if( pParse->nErr ) return 0;
126923 if( pIdx->uniqNotNull ){
126924 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
126926 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
126931 const char *zColl = pIdx->azColl[i];
126932 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
126934 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
126935 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
126937 if( pParse->nErr ){
126938 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
126939 if( pIdx->bNoQuery==0 ){
126944 ** the missing index using the collation-needed callback. For
126947 pIdx->bNoQuery = 1;
126948 pParse->rc = SQLITE_ERROR_RETRY;
126963 Token *pName, /* Name of the common-table */
126969 sqlite3 *db = pParse->db;
126972 assert( pNew!=0 || db->mallocFailed );
126974 if( db->mallocFailed ){
126978 pNew->pSelect = pQuery;
126979 pNew->pCols = pArglist;
126980 pNew->zName = sqlite3NameFromToken(pParse->db, pName);
126981 pNew->eM10d = eM10d;
126992 sqlite3ExprListDelete(db, pCte->pCols);
126993 sqlite3SelectDelete(db, pCte->pSelect);
126994 sqlite3DbFree(db, pCte->zName);
127017 sqlite3 *db = pParse->db;
127027 zName = pCte->zName;
127030 for(i=0; i<pWith->nCte; i++){
127031 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
127038 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
127043 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
127045 if( db->mallocFailed ){
127049 pNew->a[pNew->nCte++] = *pCte;
127062 for(i=0; i<pWith->nCte; i++){
127063 cteClear(db, &pWith->a[i]);
127098 assert( !db->xCollNeeded || !db->xCollNeeded16 );
127099 if( db->xCollNeeded ){
127102 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
127106 if( db->xCollNeeded16 ){
127109 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
127112 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
127123 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
127128 char *z = pColl->zName;
127133 if( pColl2->xCmp!=0 ){
127135 pColl->xDel = 0; /* Do not copy the destructor */
127154 if( pColl && pColl->xCmp==0 ){
127155 const char *zName = pColl->zName;
127156 sqlite3 *db = pParse->db;
127175 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
127187 pColl = sqlite3HashFind(&db->aCollSeq, zName);
127201 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
127219 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
127244 if( pColl ) pColl += enc-1;
127246 pColl = db->pDfltColl;
127257 db->enc = enc;
127258 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
127261 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
127286 sqlite3 *db = pParse->db;
127292 if( !p || !p->xCmp ){
127299 if( p && !p->xCmp && synthCollSeq(db, p) ){
127302 assert( !p || p->xCmp );
127305 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
127331 sqlite3 *db = pParse->db;
127333 u8 initbusy = db->init.busy;
127337 if( !initbusy && (!pColl || !pColl->xCmp) ){
127350 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
127351 ** is also -1. In other words, we are searching for a function that
127354 ** If nArg is -2 that means that we are searching for any function
127364 ** 4: UTF8/16 conversion required - argument count matches exactly
127365 ** 5: UTF16 byte order conversion required - argument count matches exactly
127368 ** If nArg==(-2) then any function with a non-null xSFunc is
127370 ** a non-match.
127375 int nArg, /* Desired number of arguments. (-1)==any */
127379 assert( p->nArg>=-1 );
127382 if( p->nArg!=nArg ){
127383 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
127384 if( p->nArg>=0 ) return 0;
127389 if( p->nArg==nArg ){
127396 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
127398 }else if( (enc & p->funcFlags & 2)!=0 ){
127414 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
127415 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
127416 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
127439 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
127440 aDef[i].pNext = pOther->pNext;
127441 pOther->pNext = &aDef[i];
127454 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
127462 ** If nArg is -2, then the first valid function found is returned. A
127463 ** function is valid if xSFunc is non-zero. The nArg==(-2)
127465 ** of arguments. If nArg is -2, then createFlag must be 0.
127473 const char *zName, /* Name of the function. zero-terminated */
127474 int nArg, /* Number of arguments. -1 means any number */
127484 assert( nArg>=(-2) );
127485 assert( nArg>=(-1) || createFlag==0 );
127488 /* First search for a match amongst the application-defined functions.
127490 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
127497 p = p->pNext;
127500 /* If no match is found, search the built-in functions.
127502 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
127503 ** functions even if a prior app-defined function was found. And give
127504 ** priority to built-in functions.
127509 ** new function. But the FuncDefs for built-in functions are read-only.
127510 ** So we must not search for built-ins when creating a new function.
127512 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
127522 p = p->pNext;
127534 pBest->zName = (const char*)&pBest[1];
127535 pBest->nArg = (u16)nArg;
127536 pBest->funcFlags = enc;
127538 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
127539 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
127545 pBest->pNext = pOther;
127549 if( pBest && (pBest->xSFunc || createFlag) ){
127571 temp1 = pSchema->tblHash;
127572 temp2 = pSchema->trigHash;
127573 sqlite3HashInit(&pSchema->trigHash);
127574 sqlite3HashClear(&pSchema->idxHash);
127579 sqlite3HashInit(&pSchema->tblHash);
127585 sqlite3HashClear(&pSchema->fkeyHash);
127586 pSchema->pSeqTab = 0;
127587 if( pSchema->schemaFlags & DB_SchemaLoaded ){
127588 pSchema->iGeneration++;
127590 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
127606 }else if ( 0==p->file_format ){
127607 sqlite3HashInit(&p->tblHash);
127608 sqlite3HashInit(&p->idxHash);
127609 sqlite3HashInit(&p->trigHash);
127610 sqlite3HashInit(&p->fkeyHash);
127611 p->enc = SQLITE_UTF8;
127644 ** pSrc->a[0].pTab Pointer to the Table object
127645 ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
127649 SrcItem *pItem = pSrc->a;
127651 assert( pItem && pSrc->nSrc>=1 );
127653 if( pItem->pTab ) sqlite3DeleteTable(pParse->db, pItem->pTab);
127654 pItem->pTab = pTab;
127655 pItem->fg.notCte = 1;
127657 pTab->nTabRef++;
127658 if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
127665 /* Generate byte-code that will report the number of rows modified
127675 /* Return true if table pTab is read-only.
127677 ** A table is read-only if any of the following are true:
127692 ** is for a top-level SQL statement.
127695 if( sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ){
127702 ** * Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS
127705 if( pParse->pToplevel!=0
127706 && pTab->u.vtab.p->eVtabRisk >
127707 ((pParse->db->flags & SQLITE_TrustedSchema)!=0)
127710 pTab->zName);
127719 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
127720 db = pParse->db;
127721 if( (pTab->tabFlags & TF_Readonly)!=0 ){
127722 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
127724 assert( pTab->tabFlags & TF_Shadow );
127731 ** If pTab is not writable -> generate an error message and return 1.
127732 ** If pTab is writable but other errors have occurred -> return 1.
127733 ** If pTab is writable and no prior errors -> return 0;
127737 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
127742 && (pTrigger==0 || (pTrigger->bReturning && pTrigger->pNext==0))
127744 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
127769 sqlite3 *db = pParse->db;
127770 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
127774 assert( pFrom->nSrc==1 );
127775 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
127776 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
127777 assert( pFrom->a[0].fg.isUsing==0 );
127778 assert( pFrom->a[0].u3.pOn==0 );
127799 SrcList *pSrc, /* the FROM clause -- which tables to scan */
127805 sqlite3 *db = pParse->db;
127817 sqlite3ExprDelete(pParse->db, pWhere);
127818 sqlite3ExprListDelete(pParse->db, pOrderBy);
127838 pTab = pSrc->a[0].pTab;
127847 assert( pPk->nKeyCol>=1 );
127848 if( pPk->nKeyCol==1 ){
127850 assert( pPk->aiColumn[0]>=0 && pPk->aiColumn[0]<pTab->nCol );
127851 zName = pTab->aCol[pPk->aiColumn[0]].zCnName;
127856 for(i=0; i<pPk->nKeyCol; i++){
127858 assert( pPk->aiColumn[i]>=0 && pPk->aiColumn[i]<pTab->nCol );
127859 p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zCnName);
127864 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
127871 pSrc->a[0].pTab = 0;
127873 pSrc->a[0].pTab = pTab;
127874 if( pSrc->a[0].fg.isIndexedBy ){
127875 assert( pSrc->a[0].fg.isCte==0 );
127876 pSrc->a[0].u2.pIBIndex = 0;
127877 pSrc->a[0].fg.isIndexedBy = 0;
127878 sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
127879 }else if( pSrc->a[0].fg.isCte ){
127880 pSrc->a[0].u2.pCteUse->nUse++;
127947 db = pParse->db;
127948 assert( db->pParse==pParse );
127949 if( pParse->nErr ){
127952 assert( db->mallocFailed==0 );
127953 assert( pTabList->nSrc==1 );
127982 sqlite3TreeViewDelete(pParse->pWith, pTabList, pWhere,
128006 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
128007 assert( iDb<db->nDb );
128008 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
128009 db->aDb[iDb].zDbSName);
128018 assert( pTabList->nSrc==1 );
128019 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
128020 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
128021 pParse->nTab++;
128027 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
128036 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
128065 if( (db->flags & SQLITE_CountRows)!=0
128066 && !pParse->nested
128067 && !pParse->pTriggerTab
128068 && !pParse->bReturning
128070 memCnt = ++pParse->nMem;
128081 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
128091 && db->xPreUpdateCallback==0
128095 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
128097 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
128098 pTab->zName, P4_STATIC);
128100 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
128101 assert( pIdx->pSchema==pTab->pSchema );
128103 sqlite3VdbeAddOp3(v, OP_Clear, pIdx->tnum, iDb, memCnt ? memCnt : -1);
128105 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
128118 iRowSet = ++pParse->nMem;
128125 nPk = pPk->nKeyCol;
128126 iPk = pParse->nMem+1;
128127 pParse->nMem += nPk;
128128 iEphCur = pParse->nTab++;
128137 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
128138 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
128139 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
128160 assert( pPk->aiColumn[i]>=0 );
128162 pPk->aiColumn[i], iPk+i);
128166 iKey = ++pParse->nMem;
128167 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
128171 /* For ONEPASS, no need to store the rowid/primary-key. There is only
128182 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
128183 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
128189 iKey = ++pParse->nMem;
128192 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
128222 /* Set up a loop over the rowids/primary-keys that were found in the
128223 ** where-clause loop above.
128227 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
128256 pParse->isMultiWrite = 0;
128264 int count = (pParse->nested==0); /* True to count changes */
128269 /* End of the loop over all rowids/primary-keys. */
128280 } /* End non-truncate path */
128286 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
128332 ** cursor number iIdxCur+i for the i-th index.
128368 u8 count, /* If non-zero, increment the row change counter */
128373 Vdbe *v = pParse->pVdbe; /* Vdbe */
128407 iOld = pParse->nMem+1;
128408 pParse->nMem += (1 + pTab->nCol);
128410 /* Populate the OLD.* pseudo-table register array. These values will be
128413 for(iCol=0; iCol<pTab->nCol; iCol++){
128441 iIdxNoSeek = -1;
128454 ** If variable 'count' is non-zero, then this OP_Delete instruction should
128455 ** invoke the update-hook. The pre-update-hook, on the other hand should
128457 ** the update-hook is not invoked for rows removed by REPLACE, but the
128458 ** pre-update-hook is.
128464 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
128508 ** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
128509 ** index is the 0-th index.)
128523 int r1 = -1; /* Register holding an index key */
128530 v = pParse->pVdbe;
128532 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
128537 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
128541 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
128589 Vdbe *v = pParse->pVdbe;
128595 if( pIdx->pPartIdxWhere ){
128597 pParse->iSelfTab = iDataCur + 1;
128598 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
128600 pParse->iSelfTab = 0;
128601 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
128607 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
128609 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
128612 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
128613 && pPrior->aiColumn[j]!=XN_EXPR
128619 if( pIdx->aiColumn[j]>=0 ){
128637 ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
128643 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
128660 ** This file contains the C-language implementations for many of the SQL
128677 assert( context->pVdbe!=0 );
128678 pOp = &context->pVdbe->aOp[context->iOp-1];
128679 assert( pOp->opcode==OP_CollSeq );
128680 assert( pOp->p4type==P4_COLLSEQ );
128681 return pOp->p4.pColl;
128689 assert( context->isError<=0 );
128690 context->isError = -1;
128691 context->skipFlag = 1;
128695 ** Implementation of the non-aggregate min() and max() functions
128708 mask = sqlite3_user_data(context)==0 ? 0 : -1;
128711 assert( mask==-1 || mask==0 );
128733 int i = sqlite3_value_type(argv[0]) - 1;
128741 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
128745 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
128790 sqlite3_result_int(context, (int)(z-z0));
128817 i64 m = sqlite3_context_db_handle(context)->enc<=SQLITE_UTF8 ? 1 : 2;
128839 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
128850 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
128852 ** equivalent positive 64-bit two complement value. */
128853 sqlite3_result_error(context, "integer overflow", -1);
128856 iVal = -iVal;
128862 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
128869 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
128873 if( rVal<0 ) rVal = -rVal;
128940 nHaystack--;
128971 x.nArg = argc-1;
128974 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
128987 ** p1 is 1-indexed. So substr(x,1,1) returns the first character
128988 ** of x. If x is text, then we actually count UTF-8 characters.
129032 ** as substr(X,1,N) - it returns the first N characters of X. This
129033 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
129034 ** from 2009-02-02 for compatibility of applications that exploited the
129041 p2 = -p2;
129045 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
129055 p1--;
129057 p2--;
129060 p1 -= p2;
129070 p1--;
129072 for(z2=z; *z2 && p2; p2--){
129075 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
129079 p2 = len-p1;
129103 /* If Y==0 and X will fit in a 64-bit int,
129107 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
129110 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
129135 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
129136 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
129137 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
129199 #define noopFunc versionFunc /* Substitute function - never called */
129214 ** (or -9223372036854775808) since when you do abs() of that
129219 ** therefore be no less than -9223372036854775807.
129221 r = -(r & LARGEST_INT64);
129261 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
129270 ** IMP: R-32760-32347 The changes() SQL function is a wrapper
129295 /* IMP: R-11217-42568 This function is a wrapper around the
129301 ** A structure defining how to do GLOB-style comparisons.
129324 /* The correct SQL-92 behavior is for the LIKE operator to ignore
129339 ** Compare two UTF-8 strings for equality where the first string is
129359 ** range of characters can be specified using '-'. Example:
129360 ** "[a-z]" matches any single lower-case letter. To match a '-', make
129383 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
129384 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
129385 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
129402 if( pInfo->matchSet==0 ){
129408 assert( matchOther<0x80 ); /* '[' is a single-byte character */
129410 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
129423 ** For a case-insensitive search, set variable cx to be the same as
129456 if( pInfo->matchSet==0 ){
129476 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
129507 ** non-zero if there is no match.
129520 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
129521 ** a miss - like strcmp().
129545 ** the built-in LIKE operator. The first argument to the function is the
129583 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
129584 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
129585 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
129586 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
129590 /* The escape character string must consist of a single UTF-8 character.
129595 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
129597 "ESCAPE expression must be a single character", -1);
129601 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
129604 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
129605 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
129608 escape = pInfo->matchSet;
129648 /* IMP: R-48699-48617 This function is an SQL wrapper around the
129649 ** sqlite3_libversion() C-interface. */
129650 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
129664 /* IMP: R-24470-31136 This function is an SQL wrapper around the
129666 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
129672 ** its side-effects.
129698 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
129722 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
129726 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
129730 /* Array for converting from half-bytes (nybbles) into ASCII hex
129745 assert( pStr!=0 && pStr->nChar==0 );
129755 sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8);
129772 if( pStr->accError==0 ){
129773 char *zText = pStr->zText;
129783 pStr->nChar = nBlob*2 + 3;
129805 ** Strings are surrounded by single-quotes with escapes on interior quotes
129815 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
129826 ** The unicode() function. Return the integer unicode code-point value
129879 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
129907 sqlite3_result_text64(context, zHex, (u64)(z-zHex),
129913 ** Buffer zStr contains nStr bytes of utf-8 encoded text. Return 1 if zStr
129993 sqlite3_result_blob(pCtx, pBlob, (p - pBlob), sqlite3_free);
130003 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
130016 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
130055 || sqlite3_context_db_handle(context)->mallocFailed );
130075 loopLimit = nStr - nPattern;
130082 nOut += nRep - nPattern;
130083 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
130084 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
130085 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
130091 if( (cntExpand&(cntExpand-1))==0 ){
130096 zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
130106 i += nPattern-1;
130109 assert( j+nStr-i+1<=nOut );
130110 memcpy(&zOut[j], &zStr[i], nStr-i);
130111 j += nStr - i;
130166 aLen[nChar] = (unsigned)(z - azChar[nChar]);
130181 nIn -= len;
130189 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
130192 nIn -= len;
130205 ** Return a string value that is the concatenation of all non-null
130221 n += (argc-1)*nSep;
130249 ** concatentation of all non-null arguments.
130263 ** argument. Use the first argument (which must be non-NULL) as the
130274 concatFuncCore(context, argc-1, argv+1, nSep, zSep);
130282 ** when the SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION compile-time option is used.
130283 ** When the "sqlite3" command-line shell is built using this functionality,
130285 ** involving application-defined functions to be examined in a generic
130293 /* no-op */
130301 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
130302 ** is only available if the SQLITE_SOUNDEX compile-time option is used
130309 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
130354 /* IMP: R-64894-50321 The string "?000" is returned if the argument
130363 ** A function that loads a shared-library extension then returns NULL.
130374 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
130375 sqlite3_result_error(context, "not authorized", -1);
130385 sqlite3_result_error(context, zErrMsg, -1);
130399 double rErr; /* Error term for Kahan-Babushka-Neumaier summation */
130402 u8 approx; /* True if any non-integer value was input to the sum */
130407 ** Do one step of the Kahan-Babushka-Neumaier summation.
130418 volatile double s = pSum->rSum;
130421 pSum->rErr += (s - t) + r;
130423 pSum->rErr += (r - t) + s;
130425 pSum->rSum = t;
130432 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
130435 iBig = iVal - iSm;
130444 ** Initialize the Kahan-Babaska-Neumaier sum from a 64-bit integer
130450 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
130452 p->rSum = (double)(iVal - iSm);
130453 p->rErr = (double)iSm;
130455 p->rSum = (double)iVal;
130456 p->rErr = 0.0;
130478 p->cnt++;
130479 if( p->approx==0 ){
130481 kahanBabuskaNeumaierInit(p, p->iSum);
130482 p->approx = 1;
130485 i64 x = p->iSum;
130487 p->iSum = x;
130489 p->ovrfl = 1;
130490 kahanBabuskaNeumaierInit(p, p->iSum);
130491 p->approx = 1;
130499 p->ovrfl = 0;
130513 /* p is always non-NULL because sumStep() will have been called first
130516 assert( p->cnt>0 );
130517 p->cnt--;
130518 if( !p->approx ){
130519 p->iSum -= sqlite3_value_int64(argv[0]);
130523 kahanBabuskaNeumaierStepInt64(p, -iVal);
130529 kahanBabuskaNeumaierStep(p, -sqlite3_value_double(argv[0]));
130539 if( p && p->cnt>0 ){
130540 if( p->approx ){
130541 if( p->ovrfl ){
130542 sqlite3_result_error(context,"integer overflow",-1);
130543 }else if( !sqlite3IsOverflow(p->rErr) ){
130544 sqlite3_result_double(context, p->rSum+p->rErr);
130546 sqlite3_result_double(context, p->rSum);
130549 sqlite3_result_int64(context, p->iSum);
130556 if( p && p->cnt>0 ){
130558 if( p->approx ){
130559 r = p->rSum;
130560 if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr;
130562 r = (double)(p->iSum);
130564 sqlite3_result_double(context, r/(double)p->cnt);
130572 if( p->approx ){
130573 r = p->rSum;
130574 if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr;
130576 r = (double)(p->iSum);
130601 p->n++;
130608 ** expressed as a 32-bit integer. */
130609 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
130610 || p->n==sqlite3_aggregate_count(context) );
130616 sqlite3_result_int64(context, p ? p->n : 0);
130622 /* p is always non-NULL since countStep() will have been called first */
130624 p->n--;
130626 p->bInverse = 1;
130650 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
130651 }else if( pBest->flags ){
130658 ** sqlite3_user_data() function returns (void *)-1. For min() it
130671 pBest->db = sqlite3_context_db_handle(context);
130679 if( pRes->flags ){
130711 /* If pnSepLengths!=0, refs an array of inter-string separator lengths,
130713 ** (Hence, its slots in use number nAccum-1 between method calls.)
130734 int firstTerm = pGCC->str.mxAlloc==0;
130735 pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
130738 sqlite3_str_appendchar(&pGCC->str, 1, ',');
130742 pGCC->nFirstSepLength = 1;
130749 sqlite3_str_append(&pGCC->str, zSep, nSep);
130755 if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){
130756 int *pnsl = pGCC->pnSepLengths;
130759 pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int));
130761 int i = 0, nA = pGCC->nAccum-1;
130762 while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength;
130765 pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int));
130768 if( ALWAYS(pGCC->nAccum>0) ){
130769 pnsl[pGCC->nAccum-1] = nSep;
130771 pGCC->pnSepLengths = pnsl;
130773 sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM);
130780 pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]);
130782 pGCC->nAccum += 1;
130786 if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal);
130801 /* pGCC is always non-NULL since groupConcatStep() will have always
130809 pGCC->nAccum -= 1;
130810 if( pGCC->pnSepLengths!=0 ){
130811 assert(pGCC->nAccum >= 0);
130812 if( pGCC->nAccum>0 ){
130813 nVS += *pGCC->pnSepLengths;
130814 memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1,
130815 (pGCC->nAccum-1)*sizeof(int));
130818 /* If removing single accumulated string, harmlessly over-do. */
130819 nVS += pGCC->nFirstSepLength;
130821 if( nVS>=(int)pGCC->str.nChar ){
130822 pGCC->str.nChar = 0;
130824 pGCC->str.nChar -= nVS;
130825 memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar);
130827 if( pGCC->str.nChar==0 ){
130828 pGCC->str.mxAlloc = 0;
130829 sqlite3_free(pGCC->pnSepLengths);
130830 pGCC->pnSepLengths = 0;
130841 sqlite3ResultStrAccum(context, &pGCC->str);
130843 sqlite3_free(pGCC->pnSepLengths);
130852 StrAccum *pAccum = &pGCC->str;
130853 if( pAccum->accError==SQLITE_TOOBIG ){
130855 }else if( pAccum->accError==SQLITE_NOMEM ){
130857 }else if( pGCC->nAccum>0 && pAccum->nChar==0 ){
130861 sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT);
130870 ** This routine does per-connection function registration. Most
130871 ** of the built-in functions above are part of the global function set.
130883 ** Re-register the built-in LIKE functions. The caseSensitive
130903 pDef->funcFlags |= flags;
130904 pDef->funcFlags &= ~SQLITE_FUNC_UNSAFE;
130913 ** LIKE-style function then return FALSE.
130929 assert( pExpr->op==TK_FUNCTION );
130931 if( !pExpr->x.pList ){
130934 nExpr = pExpr->x.pList->nExpr;
130936 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
130940 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
130948 memcpy(aWc, pDef->pUserData, 3);
130956 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
130958 if( pEscape->op!=TK_STRING ) return 0;
130960 zEscape = pEscape->u.zToken;
130967 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
130983 /* Extra math functions that require linking with -lm
131041 ** ln(X) - natural logarithm
131042 ** log(X) - log X base 10
131043 ** log10(X) - log X base 10
131044 ** log(B,X) - log X base B
131098 ** Implementation of 1-argument SQL math functions:
131100 ** exp(X) - Compute e to the X-th power
131120 ** Implementation of 2-argument SQL math functions:
131122 ** power(X,Y) - Compute X to the Y-th power
131145 ** Implementation of 0-argument pi() function.
131174 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
131204 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
131210 ** to the global function hash table. This occurs at start-time (as
131221 ** FuncDef.pHash elements at start-time. The elements of this array
131222 ** are read-only after initialization is complete.
131261 FUNCTION(min, -1, 0, 1, minmaxFunc ),
131265 FUNCTION(max, -1, 1, 1, minmaxFunc ),
131274 FUNCTION(printf, -1, 0, 0, printfFunc ),
131275 FUNCTION(format, -1, 0, 0, printfFunc ),
131277 FUNCTION(char, -1, 0, 0, charFunc ),
131291 FUNCTION(concat, -1, 0, 0, concatFunc ),
131293 FUNCTION(concat_ws, -1, 0, 0, concatwsFunc ),
131337 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
131378 INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ),
131389 #if 0 /* Enable to print out how the built-in functions are hashed */
131394 printf("FUNC-HASH %02d:", i);
131395 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
131396 int n = sqlite3Strlen30(p->zName);
131397 int h = p->zName[0] + n;
131398 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
131399 printf(" %s(%d)", p->zName, h);
131429 ** --------------------------
131511 ** ---------------
131513 ** Before coding an UPDATE or DELETE row operation, the code-generator
131518 ** accessed). No information is required by the code-generator before
131522 ** sqlite3FkRequired() - Test to see if FK processing is required.
131523 ** sqlite3FkOldmask() - Query for the set of required old.* columns.
131527 ** --------------------------------------
131529 ** sqlite3FkCheck() - Check for foreign key violations.
131530 ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
131531 ** sqlite3FkDelete() - Delete an FKey structure.
131536 ** -----------------------
131566 ** constraint to the parent table column stored in the left-most column
131568 ** child table column that corresponds to the second left-most column of
131587 ** then non-zero is returned, and a "foreign key mismatch" error loaded
131588 ** into pParse. If an OOM error occurs, non-zero is returned and the
131589 ** pParse->db->mallocFailed flag is set.
131600 int nCol = pFKey->nCol; /* Number of columns in parent key */
131601 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
131608 /* If this is a non-composite (single column) foreign key, check if it
131614 ** Non-composite foreign keys do not require the aiCol array.
131624 if( pParent->iPKey>=0 ){
131626 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zCnName, zKey) ){
131632 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
131637 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
131638 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
131650 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
131655 /* If zKey is non-NULL, then this foreign key was declared to
131661 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
131670 zDfltColl = sqlite3ColumnColl(&pParent->aCol[iCol]);
131672 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
131674 zIdxCol = pParent->aCol[iCol].zCnName;
131676 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
131677 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
131689 if( !pParse->disableTriggers ){
131691 "foreign key mismatch - \"%w\" referencing \"%w\"",
131692 pFKey->pFrom->zName, pFKey->zTo);
131694 sqlite3DbFree(pParse->db, aiCol);
131706 ** affected - once to "delete" the old row, and then again to "insert" the
131716 ** --------------------------------------------------------------------------
131741 int iCur = pParse->nTab - 1; /* Cursor number to use */
131745 (!pFKey->isDeferred
131746 && !(pParse->db->flags & SQLITE_DeferFKs)
131747 && !pParse->pToplevel
131748 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
131758 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
131761 for(i=0; i<pFKey->nCol; i++){
131762 int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1;
131779 sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp);
131784 ** to increment the constraint-counter (i.e. this is an INSERT operation),
131786 ** increment the constraint-counter. */
131787 if( pTab==pFKey->pFrom && nIncr==1 ){
131795 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
131799 int nCol = pFKey->nCol;
131802 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
131806 sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData,
131811 ** to increment the constraint-counter (i.e. this is an INSERT operation),
131813 ** increment the constraint-counter.
131815 ** If any of the parent-key values are NULL, then the row cannot match
131817 ** of the parent-key values are NULL (at this point it is known that
131820 if( pTab==pFKey->pFrom && nIncr==1 ){
131823 int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i])
131826 iParent += sqlite3TableColumnToStorage(pIdx->pTable,
131827 pIdx->aiColumn[i]);
131828 assert( pIdx->aiColumn[i]>=0 );
131829 assert( aiCol[i]!=pTab->iPKey );
131830 if( pIdx->aiColumn[i]==pTab->iPKey ){
131841 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
131848 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
131849 && !pParse->pToplevel
131850 && !pParse->isMultiWrite
131860 if( nIncr>0 && pFKey->isDeferred==0 ){
131863 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
131888 sqlite3 *db = pParse->db;
131892 if( iCol>=0 && iCol!=pTab->iPKey ){
131893 pCol = &pTab->aCol[iCol];
131894 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
131895 pExpr->affExpr = pCol->affinity;
131897 if( zColl==0 ) zColl = db->pDfltColl->zName;
131900 pExpr->iTable = regBase;
131901 pExpr->affExpr = SQLITE_AFF_INTEGER;
131920 pExpr->y.pTab = pTab;
131921 pExpr->iTable = iCursor;
131922 pExpr->iColumn = iCol;
131931 ** code for an SQL UPDATE operation, this function may be called twice -
131934 ** Parameter nIncr is passed -1 when inserting a row (as this may decrease
131943 ** --------------------------------------------------------------------------
131965 sqlite3 *db = pParse->db; /* Database handle */
131973 assert( pIdx==0 || pIdx->pTable==pTab );
131974 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
131975 assert( pIdx!=0 || pFKey->nCol==1 );
131979 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
131985 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
131991 for(i=0; i<pFKey->nCol; i++){
131998 iCol = pIdx ? pIdx->aiColumn[i] : -1;
132000 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
132002 zCol = pFKey->pFrom->aCol[iCol].zCnName;
132022 if( pTab==pFKey->pFrom && nIncr>0 ){
132027 pLeft = exprTableRegister(pParse, pTab, regData, -1);
132028 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
132033 for(i=0; i<pIdx->nKeyCol; i++){
132034 i16 iCol = pIdx->aiColumn[i];
132037 pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zCnName);
132055 if( pParse->nErr==0 ){
132057 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
132085 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
132091 ** and all of its sub-components.
132093 ** The Trigger structure or any of its sub-components may be allocated from
132098 TriggerStep *pStep = p->step_list;
132099 sqlite3ExprDelete(dbMem, pStep->pWhere);
132100 sqlite3ExprListDelete(dbMem, pStep->pExprList);
132101 sqlite3SelectDelete(dbMem, pStep->pSelect);
132102 sqlite3ExprDelete(dbMem, p->pWhen);
132114 Hash *pHash = &db->aDb[iDb].pSchema->tblHash;
132119 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
132120 fkTriggerDelete(db, pFKey->apTrigger[0]); pFKey->apTrigger[0] = 0;
132121 fkTriggerDelete(db, pFKey->apTrigger[1]); pFKey->apTrigger[1] = 0;
132144 sqlite3 *db = pParse->db;
132145 if( (db->flags&SQLITE_ForeignKeys) && IsOrdinaryTable(pTab) ){
132158 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
132159 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
132166 pParse->disableTriggers = 1;
132168 pParse->disableTriggers = 0;
132179 if( (db->flags & SQLITE_DeferFKs)==0 ){
132200 ** is set to -1). If the rowid column is modified by the UPDATE statement
132201 ** the bChngRowid argument is non-zero.
132213 for(i=0; i<p->nCol; i++){
132214 int iChildKey = p->aCol[i].iFrom;
132216 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
132227 ** is set to -1). If the rowid column is modified by the UPDATE statement
132228 ** the bChngRowid argument is non-zero.
132240 for(i=0; i<p->nCol; i++){
132241 char *zKey = p->aCol[i].zCol;
132243 for(iKey=0; iKey<pTab->nCol; iKey++){
132244 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
132245 Column *pCol = &pTab->aCol[iKey];
132247 if( 0==sqlite3StrICmp(pCol->zCnName, zKey) ) return 1;
132248 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
132264 if( pTop->pTriggerPrg ){
132265 Trigger *p = pTop->pTriggerPrg->pTrigger;
132266 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
132267 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
132269 assert( (pTop->db->flags & SQLITE_FkNoAction)==0 );
132282 ** first register in an array of (pTab->nCol+1) registers containing the
132288 ** first register of an array of (pTab->nCol+1) registers containing the new
132304 sqlite3 *db = pParse->db; /* Database handle */
132308 int isIgnoreErrors = pParse->disableTriggers;
132310 /* Exactly one of regOld and regNew should be non-zero. */
132313 /* If foreign-keys are disabled, this function is a no-op. */
132314 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
132317 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
132318 zDb = db->aDb[iDb].zDbSName;
132322 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
132332 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
132342 if( pParse->disableTriggers ){
132343 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
132345 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
132349 if( !isIgnoreErrors || db->mallocFailed ) return;
132356 ** FK counter for each row of the current table with non-NULL keys.
132359 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
132360 for(i=0; i<pFKey->nCol; i++){
132362 iFromCol = pFKey->aCol[i].iFrom;
132363 iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
132366 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
132370 assert( pFKey->nCol==1 || (aiFree && pIdx) );
132375 iCol = pFKey->aCol[0].iFrom;
132378 for(i=0; i<pFKey->nCol; i++){
132379 if( aiCol[i]==pTab->iPKey ){
132380 aiCol[i] = -1;
132382 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
132387 if( db->xAuth ){
132389 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName;
132390 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
132396 /* Take a shared-cache advisory read-lock on the parent table. Allocate
132399 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
132400 pParse->nTab++;
132406 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
132425 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
132434 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
132435 && !pParse->pToplevel && !pParse->isMultiWrite
132444 if( !isIgnoreErrors || db->mallocFailed ) return;
132447 assert( aiCol || pFKey->nCol==1 );
132453 SrcItem *pItem = pSrc->a;
132454 pItem->pTab = pFKey->pFrom;
132455 pItem->zName = pFKey->pFrom->zName;
132456 pItem->pTab->nTabRef++;
132457 pItem->iCursor = pParse->nTab++;
132460 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
132463 int eAction = pFKey->aAction[aChange!=0];
132464 if( (db->flags & SQLITE_FkNoAction) ) eAction = OE_None;
132470 ** So do not set the "may-abort" flag in this case.
132473 ** may-abort flag will eventually be set on this statement anyway
132484 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
132488 pItem->zName = 0;
132506 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
132509 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
132510 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
132512 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
132516 for(i=0; i<pIdx->nKeyCol; i++){
132517 assert( pIdx->aiColumn[i]>=0 );
132518 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
132533 ** entry in the aChange[] array is set to -1. If the column is modified,
132538 ** non-zero. If there is no foreign key related processing, this function
132554 int *aChange, /* Non-NULL for UPDATE operations */
132559 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
132564 bHaveFK = (sqlite3FkReferences(pTab) || pTab->u.tab.pFKey);
132571 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
132573 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) eRet = 2;
132579 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
132581 if( (pParse->db->flags & SQLITE_FkNoAction)==0
132582 && p->aAction[1]!=OE_None
132596 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
132604 ** require no special handling by the triggers sub-system, code for them is
132627 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
132629 sqlite3 *db = pParse->db; /* Database handle */
132634 action = pFKey->aAction[iAction];
132635 if( (db->flags & SQLITE_FkNoAction) ) action = OE_None;
132636 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
132639 pTrigger = pFKey->apTrigger[iAction];
132645 int *aiCol = 0; /* child table cols -> parent key cols */
132654 assert( aiCol || pFKey->nCol==1 );
132656 for(i=0; i<pFKey->nCol; i++){
132664 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
132666 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
132667 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
132669 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
132670 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName);
132708 Column *pCol = pFKey->pFrom->aCol + iFromCol;
132710 if( pCol->colFlags & COLFLAG_GENERATED ){
132711 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
132712 testcase( pCol->colFlags & COLFLAG_STORED );
132715 pDflt = sqlite3ColumnExpr(pFKey->pFrom, pCol);
132731 zFrom = pFKey->pFrom->zName;
132735 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
132741 pRaise->affExpr = OE_Abort;
132745 assert( pSrc->nSrc==1 );
132746 pSrc->a[0].zName = sqlite3DbStrDup(db, zFrom);
132747 pSrc->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
132764 nFrom + 1 /* Space for pStep->zTarget */
132767 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
132768 pStep->zTarget = (char *)&pStep[1];
132769 memcpy((char *)pStep->zTarget, zFrom, nFrom);
132771 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
132772 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
132773 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
132776 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
132780 /* Re-enable the lookaside buffer, if it was disabled earlier. */
132787 if( db->mallocFailed==1 ){
132796 pStep->op = TK_SELECT;
132800 pStep->op = TK_DELETE;
132805 pStep->op = TK_UPDATE;
132807 pStep->pTrig = pTrigger;
132808 pTrigger->pSchema = pTab->pSchema;
132809 pTrigger->pTabSchema = pTab->pSchema;
132810 pFKey->apTrigger[iAction] = pTrigger;
132811 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
132824 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
132829 /* If foreign-key support is enabled, iterate through all FKs that
132832 ** trigger sub-program. */
132833 if( pParse->db->flags&SQLITE_ForeignKeys ){
132835 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
132855 FKey *pNext; /* Copy of pFKey->pNextFrom */
132859 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
132860 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
132863 if( db->pnBytesFreed==0 ){
132864 if( pFKey->pPrevTo ){
132865 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
132867 const char *z = (pFKey->pNextTo ? pFKey->pNextTo->zTo : pFKey->zTo);
132868 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, pFKey->pNextTo);
132870 if( pFKey->pNextTo ){
132871 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
132875 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
132878 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
132882 fkTriggerDelete(db, pFKey->apTrigger[0]);
132883 fkTriggerDelete(db, pFKey->apTrigger[1]);
132886 pNext = pFKey->pNextFrom;
132928 assert( pParse->pVdbe!=0 );
132929 v = pParse->pVdbe;
132931 if( !pParse->db->noSharedCache ){
132932 sqlite3TableLock(pParse, iDb, pTab->tnum,
132933 (opcode==OP_OpenWrite)?1:0, pTab->zName);
132936 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
132937 VdbeComment((v, "%s", pTab->zName));
132941 assert( pPk->tnum==pTab->tnum || CORRUPT_DB );
132942 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
132944 VdbeComment((v, "%s", pTab->zName));
132954 ** ------------------------------
132978 Table *pTab = pIdx->pTable;
132979 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
132980 if( !pIdx->zColAff ){
132984 for(n=0; n<pIdx->nColumn; n++){
132985 i16 x = pIdx->aiColumn[n];
132988 aff = pTab->aCol[x].affinity;
132993 assert( pIdx->bHasExpr );
132994 assert( pIdx->aColExpr!=0 );
132995 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
132999 pIdx->zColAff[n] = aff;
133001 pIdx->zColAff[n] = 0;
133002 return pIdx->zColAff;
133005 if( !pIdx->zColAff ) return computeIndexAffStr(db, pIdx);
133006 return pIdx->zColAff;
133017 zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1);
133020 for(i=j=0; i<pTab->nCol; i++){
133021 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
133022 zColAff[j++] = pTab->aCol[i].affinity;
133026 zColAff[j--] = 0;
133036 ** For ordinary (legacy, non-strict) tables:
133037 ** -----------------------------------------
133043 ** which were then optimized out) then this routine becomes a no-op.
133053 ** --------- ---------------
133061 ** ------------------
133075 if( pTab->tabFlags & TF_Strict ){
133084 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
133085 pPrev->opcode = OP_TypeCheck;
133086 sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3);
133089 sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol);
133094 zColAff = pTab->zColAff;
133101 pTab->zColAff = zColAff;
133109 assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord
133110 || sqlite3VdbeDb(v)->mallocFailed );
133111 sqlite3VdbeChangeP4(v, -1, zColAff, i);
133117 ** Return non-zero if the table pTab in database iDb or any of its indices
133127 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
133133 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
133135 Pgno tnum = pOp->p2;
133136 if( tnum==pTab->tnum ){
133139 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
133140 if( tnum==pIndex->tnum ){
133146 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
133147 assert( pOp->p4.pVtab!=0 );
133148 assert( pOp->p4type==P4_VTAB );
133160 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
133161 assert( pExpr->iColumn < pWalker->u.pTab->nCol );
133162 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
133186 assert( pTab->tabFlags & TF_HasGenerated );
133187 testcase( pTab->tabFlags & TF_HasVirtual );
133188 testcase( pTab->tabFlags & TF_HasStored );
133193 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
133194 if( (pTab->tabFlags & TF_HasStored)!=0 ){
133195 pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
133196 if( pOp->opcode==OP_Affinity ){
133198 ** columns. '@' is the no-op affinity and those columns have not
133201 char *zP4 = pOp->p4.z;
133203 assert( pOp->p4type==P4_DYNAMIC );
133205 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
133208 if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
133213 }else if( pOp->opcode==OP_TypeCheck ){
133217 pOp->p3 = 1;
133222 ** this is a two-pass algorithm. On the first pass, mark all generated
133225 for(i=0; i<pTab->nCol; i++){
133226 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
133227 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
133228 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
133229 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
133238 /* On the second pass, compute the value of each NOT-AVAILABLE column.
133243 pParse->iSelfTab = -iRegStore;
133247 for(i=0; i<pTab->nCol; i++){
133248 Column *pCol = pTab->aCol + i;
133249 if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
133251 pCol->colFlags |= COLFLAG_BUSY;
133254 pCol->colFlags &= ~COLFLAG_BUSY;
133260 assert( pCol->colFlags & COLFLAG_GENERATED );
133263 pCol->colFlags &= ~COLFLAG_NOTAVAIL;
133268 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName);
133270 pParse->iSelfTab = 0;
133305 assert( pParse->db->aDb[iDb].pSchema!=0 );
133306 if( (pTab->tabFlags & TF_Autoincrement)!=0
133307 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
133311 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
133315 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
133319 || pSeqTab->nCol!=2
133321 pParse->nErr++;
133322 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
133326 pInfo = pToplevel->pAinc;
133327 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
133329 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
133331 testcase( pParse->earlyCleanup );
133332 if( pParse->db->mallocFailed ) return 0;
133333 pInfo->pNext = pToplevel->pAinc;
133334 pToplevel->pAinc = pInfo;
133335 pInfo->pTab = pTab;
133336 pInfo->iDb = iDb;
133337 pToplevel->nMem++; /* Register to hold name of table */
133338 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
133339 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
133341 memId = pInfo->regCtr;
133352 sqlite3 *db = pParse->db; /* The database connection */
133355 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
133357 /* This routine is never called during trigger-generation. It is
133358 ** only called from the top-level */
133359 assert( pParse->pTriggerTab==0 );
133363 for(p = pParse->pAinc; p; p = p->pNext){
133380 pDb = &db->aDb[p->iDb];
133381 memId = p->regCtr;
133382 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
133383 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
133384 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
133390 aOp[3].p1 = memId-1;
133399 if( pParse->nTab==0 ) pParse->nTab = 1;
133413 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
133426 Vdbe *v = pParse->pVdbe;
133427 sqlite3 *db = pParse->db;
133430 for(p = pParse->pAinc; p; p = p->pNext){
133440 Db *pDb = &db->aDb[p->iDb];
133442 int memId = p->regCtr;
133445 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
133448 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
133453 aOp[2].p1 = memId-1;
133462 if( pParse->pAinc ) autoIncrementEnd(pParse);
133467 ** above are all no-ops
133475 ** that was able to use the co-routine optimization, finish coding the
133476 ** co-routine.
133479 if( ALWAYS(pVal) && pVal->pSrc->nSrc>0 ){
133480 SrcItem *pItem = &pVal->pSrc->a[0];
133481 sqlite3VdbeEndCoroutine(pParse->pVdbe, pItem->regReturn);
133482 sqlite3VdbeJumpHere(pParse->pVdbe, pItem->addrFillSub - 1);
133487 ** Return true if all expressions in the expression-list passed as the
133492 for(ii=0; ii<pRow->nExpr; ii++){
133493 if( 0==sqlite3ExprIsConstant(pParse, pRow->a[ii].pExpr) ) return 0;
133499 ** Return true if all expressions in the expression-list passed as the
133505 for(ii=0; ii<pRow->nExpr; ii++){
133506 Expr *pExpr = pRow->a[ii].pExpr;
133507 assert( pExpr->op!=TK_RAISE );
133508 assert( pExpr->affExpr==0 );
133517 ** rows of a multi-row VALUES clause. Argument pLeft is the part of
133522 ** There are two ways in which this may be achieved - by incremental
133523 ** coding of a co-routine (the "co-routine" method) or by returning a
133531 ** When the co-routine method is used, each row that will be returned
133532 ** by the VALUES clause is coded into part of a co-routine as it is
133536 ** Select object to read co-routine
133539 ** The co-routine method is used in most cases. Exceptions are:
133547 ** This will not work, as the co-routine uses a hard-coded register
133554 ** a co-routine is not possible.
133556 ** c) There are non-constant expressions in the VALUES clause (e.g.
133557 ** the VALUES clause is part of a correlated sub-query).
133568 if( pParse->bHasWith /* condition (a) above */
133569 || pParse->db->init.busy /* condition (b) above */
133571 || (pLeft->pSrc->nSrc==0 &&
133572 exprListIsNoAffinity(pParse,pLeft->pEList)==0) /* condition (d) above */
133575 /* The co-routine method cannot be used. Fall back to UNION ALL. */
133578 if( pLeft->pSrc->nSrc ){
133581 }else if( pLeft->pPrior ){
133583 f = (f & pLeft->selFlags);
133586 pLeft->selFlags &= ~SF_MultiValue;
133588 pSelect->op = TK_ALL;
133589 pSelect->pPrior = pLeft;
133593 SrcItem *p = 0; /* SrcItem that reads from co-routine */
133595 if( pLeft->pSrc->nSrc==0 ){
133596 /* Co-routine has not yet been started and the special Select object
133597 ** that accesses the co-routine has not yet been created. This block
133604 if( (pParse->db->mDbFlags & DBFLAG_SchemaKnownOk)==0 ){
133610 pRet->pSrc->nSrc = 1;
133611 pRet->pPrior = pLeft->pPrior;
133612 pRet->op = pLeft->op;
133613 if( pRet->pPrior ) pRet->selFlags |= SF_Values;
133614 pLeft->pPrior = 0;
133615 pLeft->op = TK_SELECT;
133616 assert( pLeft->pNext==0 );
133617 assert( pRet->pNext==0 );
133618 p = &pRet->pSrc->a[0];
133619 p->pSelect = pLeft;
133620 p->fg.viaCoroutine = 1;
133621 p->addrFillSub = sqlite3VdbeCurrentAddr(v) + 1;
133622 p->regReturn = ++pParse->nMem;
133623 p->iCursor = -1;
133624 p->u1.nRow = 2;
133625 sqlite3VdbeAddOp3(v,OP_InitCoroutine,p->regReturn,0,p->addrFillSub);
133626 sqlite3SelectDestInit(&dest, SRT_Coroutine, p->regReturn);
133628 /* Allocate registers for the output of the co-routine. Do so so
133630 ** used by the co-routine. This allows the code in sqlite3Insert()
133632 ** of the co-routine to a separate array for processing. */
133633 dest.iSdst = pParse->nMem + 3;
133634 dest.nSdst = pLeft->pEList->nExpr;
133635 pParse->nMem += 2 + dest.nSdst;
133637 pLeft->selFlags |= SF_MultiValue;
133639 p->regResult = dest.iSdst;
133640 assert( pParse->nErr || dest.iSdst>0 );
133644 p = &pLeft->pSrc->a[0];
133645 assert( !p->fg.isTabFunc && !p->fg.isIndexedBy );
133646 p->u1.nRow++;
133649 if( pParse->nErr==0 ){
133651 if( p->pSelect->pEList->nExpr!=pRow->nExpr ){
133652 sqlite3SelectWrongNumTermsError(pParse, p->pSelect);
133654 sqlite3ExprCodeExprList(pParse, pRow, p->regResult, 0, 0);
133655 sqlite3VdbeAddOp1(pParse->pVdbe, OP_Yield, p->regReturn);
133658 sqlite3ExprListDelete(pParse->db, pRow);
133681 ** then a list of all (non-hidden) columns for the table is substituted.
133686 ** first two forms shown above. A VALUES clause is really just short-hand
133692 ** insert with data coming from a single-row VALUES clause, the code executes
133693 ** once straight down through. Pseudo-code follows (we call this
133705 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
133729 ** X <- A
133737 ** end-coroutine X
133750 ** X <- A
133758 ** end co-routine R
133787 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
133816 db = pParse->db;
133817 assert( db->pParse==pParse );
133818 if( pParse->nErr ){
133821 assert( db->mallocFailed==0 );
133828 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
133829 pList = pSelect->pEList;
133830 pSelect->pEList = 0;
133837 assert( pTabList->nSrc==1 );
133842 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133843 assert( iDb<db->nDb );
133844 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
133845 db->aDb[iDb].zDbSName) ){
133870 sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList,
133876 ** ViewGetColumnNames() is a no-op if pTab is not a view.
133882 /* Cannot insert into a read-only table.
133892 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
133924 regRowid = regIns = pParse->nMem+1;
133925 pParse->nMem += pTab->nCol + 1;
133928 pParse->nMem++;
133941 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
133942 ** loop, if ipkColumn==(-1), that means that integer primary key
133951 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
133953 assert( pColumn->eU4!=EU4_EXPR );
133954 pColumn->eU4 = EU4_IDX;
133955 for(i=0; i<pColumn->nId; i++){
133956 pColumn->a[i].u4.idx = -1;
133958 for(i=0; i<pColumn->nId; i++){
133959 for(j=0; j<pTab->nCol; j++){
133960 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zCnName)==0 ){
133961 pColumn->a[i].u4.idx = j;
133963 if( j==pTab->iPKey ){
133967 if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
133970 pTab->aCol[j].zCnName);
133977 if( j>=pTab->nCol ){
133978 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
133983 pTabList->a, pColumn->a[i].zName);
133984 pParse->checkSchema = 1;
133992 ** is coming from a SELECT statement, then generate a co-routine that
133994 ** co-routine is the common header to the 3rd and 4th templates.
133997 /* Data is coming from a SELECT or from a multi-row VALUES clause.
133998 ** Generate a co-routine to run the SELECT. */
134001 if( pSelect->pSrc->nSrc==1
134002 && pSelect->pSrc->a[0].fg.viaCoroutine
134003 && pSelect->pPrior==0
134005 SrcItem *pItem = &pSelect->pSrc->a[0];
134006 dest.iSDParm = pItem->regReturn;
134007 regFromSelect = pItem->regResult;
134008 nColumn = pItem->pSelect->pEList->nExpr;
134010 if( bIdListInOrder && nColumn==pTab->nCol ){
134012 regRowid = regData - 1;
134013 regIns = regRowid - (IsVirtual(pTab) ? 1 : 0);
134016 int addrTop; /* Top of the co-routine */
134017 int regYield = ++pParse->nMem;
134022 dest.nSdst = pTab->nCol;
134025 assert( db->pParse==pParse );
134026 if( rc || pParse->nErr ) goto insert_cleanup;
134027 assert( db->mallocFailed==0 );
134029 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
134030 assert( pSelect->pEList );
134031 nColumn = pSelect->pEList->nExpr;
134062 srcTab = pParse->nTab++;
134077 ** single-row VALUES clause
134082 srcTab = -1;
134085 nColumn = pList->nExpr;
134099 ipkColumn = pTab->iPKey;
134101 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
134102 testcase( pTab->tabFlags & TF_HasVirtual );
134103 testcase( pTab->tabFlags & TF_HasStored );
134104 for(i=ipkColumn-1; i>=0; i--){
134105 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
134106 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
134107 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
134108 ipkColumn--;
134120 if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){
134121 for(i=0; i<pTab->nCol; i++){
134122 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
134125 if( nColumn!=(pTab->nCol-nHidden) ){
134128 pTabList->a, pTab->nCol-nHidden, nColumn);
134132 if( pColumn!=0 && nColumn!=pColumn->nId ){
134133 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
134139 if( (db->flags & SQLITE_CountRows)!=0
134140 && !pParse->nested
134141 && !pParse->pTriggerTab
134142 && !pParse->bReturning
134144 regRowCount = ++pParse->nMem;
134151 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
134157 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
134159 aRegIdx[i] = ++pParse->nMem;
134160 pParse->nMem += pIdx->nColumn;
134162 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
134169 pTab->zName);
134176 if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
134179 pTabList->a[0].iCursor = iDataCur;
134182 pNx->pUpsertSrc = pTabList;
134183 pNx->regData = regData;
134184 pNx->iDataCur = iDataCur;
134185 pNx->iIdxCur = iIdxCur;
134186 if( pNx->pUpsertTarget ){
134191 pNx = pNx->pNextUpsert;
134219 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
134223 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
134225 ** the value does not get overwritten by a NULL at tag-20191021-002. */
134239 for(i=0; i<pTab->nCol; i++, iRegStore++){
134243 if( i==pTab->iPKey ){
134244 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
134247 ** NULL - we cannot optimize further by skipping the column completely */
134251 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
134257 iRegStore--;
134261 ** triggers, the slots used for stored columns will be OP_Copy-ed
134272 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
134278 assert( pColumn->eU4==EU4_IDX );
134279 for(j=0; j<pColumn->nId && pColumn->a[j].u4.idx!=i; j++){}
134280 if( j>=pColumn->nId ){
134284 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
134292 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
134296 k = i - nHidden;
134306 Expr *pX = pList->a[k].pExpr;
134320 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
134326 ** not happened yet) so we substitute a rowid of -1
134329 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
134337 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
134340 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
134346 assert( pTab->nNVCol>0 || pParse->nErr>0 );
134347 sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
134354 if( pTab->tabFlags & TF_HasGenerated ){
134355 testcase( pTab->tabFlags & TF_HasVirtual );
134356 testcase( pTab->tabFlags & TF_HasStored );
134372 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
134374 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
134387 /* Rowid already initialized at tag-20191021-001 */
134389 Expr *pIpk = pList->a[ipkColumn].pExpr;
134390 if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
134394 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
134425 if( pTab->tabFlags & TF_HasGenerated ){
134437 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
134448 if( db->flags & SQLITE_ForeignKeys ){
134466 }else if( pParse->bReturning ){
134468 ** constant value -1, in case one or more of the returned expressions
134470 sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid);
134483 pTab, regData-2-pTab->nCol, onError, endOfLoop);
134500 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
134501 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
134515 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
134551 ** Meanings of bits in of pWalker->eCode for
134558 * Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
134563 if( pExpr->op==TK_COLUMN ){
134564 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
134565 if( pExpr->iColumn>=0 ){
134566 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
134567 pWalker->eCode |= CKCNSTRNT_COLUMN;
134570 pWalker->eCode |= CKCNSTRNT_ROWID;
134577 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
134586 ** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
134587 ** The operation of this routine is the same - return true if an only if
134615 ** the indexes of a table in the order provided in the Table->pIndex list.
134616 ** However, sometimes (rarely - when there is an upsert) it wants to visit
134650 assert( pIter->i==0 );
134651 if( pIter->eType ){
134652 *pIx = pIter->u.ax.aIdx[0].ix;
134653 return pIter->u.ax.aIdx[0].p;
134656 return pIter->u.lx.pIdx;
134662 if( pIter->eType ){
134663 int i = ++pIter->i;
134664 if( i>=pIter->u.ax.nIdx ){
134668 *pIx = pIter->u.ax.aIdx[i].ix;
134669 return pIter->u.ax.aIdx[i].p;
134672 pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
134673 return pIter->u.lx.pIdx;
134683 ** pTab->nCol+1 registers in this range. The first register (the one
134710 ** at pTab->pIndex.
134712 ** (2019-05-07) The generated code also creates a new record for the
134714 ** register identified by aRegIdx[nIdx] - in other words in the first
134725 ** for the first index in the pTab->pIndex list. Cursors for other indices
134726 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
134734 ** --------------- ---------- ----------------------------------------
134764 ** Or if overrideError==OE_Default, then the pParse->onError parameter
134765 ** is used. Or if pParse->onError==OE_Default then the onError value
134776 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
134810 db = pParse->db;
134811 v = pParse->pVdbe;
134814 nCol = pTab->nCol;
134825 nPkField = pPk->nKeyCol;
134834 if( pTab->tabFlags & TF_HasNotNull ){
134841 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
134842 int isGenerated; /* non-zero if column is generated */
134843 onError = pCol->notNull;
134845 if( i==pTab->iPKey ){
134848 isGenerated = pCol->colFlags & COLFLAG_GENERATED;
134864 || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */
134866 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
134867 testcase( pCol->colFlags & COLFLAG_STORED );
134868 testcase( pCol->colFlags & COLFLAG_GENERATED );
134884 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
134896 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
134897 pCol->zCnName);
134898 testcase( zMsg==0 && db->mallocFailed==0 );
134923 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
134931 } /* end of 2-pass loop */
134932 } /* end if( has-not-null-constraints ) */
134937 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
134938 ExprList *pCheck = pTab->pCheck;
134939 pParse->iSelfTab = -(regNewData+1);
134941 for(i=0; i<pCheck->nExpr; i++){
134944 Expr *pExpr = pCheck->a[i].pExpr;
134959 if( !db->mallocFailed ){
134966 char *zName = pCheck->a[i].zEName;
134967 assert( zName!=0 || pParse->db->mallocFailed );
134968 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
134975 pParse->iSelfTab = 0;
134992 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
135010 sIdxIter.u.lx.pIdx = pTab->pIndex;
135012 if( pUpsert->pUpsertTarget==0 ){
135013 /* There is just on ON CONFLICT clause and it has no constraint-target */
135014 assert( pUpsert->pNextUpsert==0 );
135015 if( pUpsert->isDoUpdate==0 ){
135016 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
135024 }else if( pTab->pIndex!=0 ){
135032 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
135041 pUpsert->pToFree = sIdxIter.u.ax.aIdx;
135042 for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
135043 if( pTerm->pUpsertTarget==0 ) break;
135044 if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */
135046 pIdx = pTab->pIndex;
135047 while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
135048 pIdx = pIdx->pNext;
135057 for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
135087 if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
135093 if( db->flags&SQLITE_RecTriggers ){
135103 regTrigCnt = ++pParse->nMem;
135118 onError = pTab->keyConf;
135129 if( pUpsertClause->isDoUpdate==0 ){
135150 && pTab->pIndex /* There exist other constraints */
135189 ** recursive-triggers flag is set, call GenerateRowDelete() to
135191 ** the triggers and remove both the table and index b-tree entries.
135193 ** Otherwise, if there are no triggers or the recursive-triggers
135195 ** GenerateRowIndexDelete(). This removes the index b-tree entries
135196 ** only. The table b-tree entry will be replaced by the new entry
135213 regNewData, 1, 0, OE_Replace, 1, -1);
135219 /* This OP_Delete opcode fires the pre-update-hook only. It does
135220 ** not modify the b-tree. It is more efficient to let the coming
135226 if( pTab->pIndex ){
135228 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
135251 sqlite3VdbeJumpHere(v, ipkTop-1);
135284 VdbeNoopComment((v, "prep index %s", pIdx->zName));
135289 if( pIdx->pPartIdxWhere ){
135291 pParse->iSelfTab = -(regNewData+1);
135292 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
135294 pParse->iSelfTab = 0;
135301 for(i=0; i<pIdx->nColumn; i++){
135302 int iField = pIdx->aiColumn[i];
135305 pParse->iSelfTab = -(regNewData+1);
135306 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
135307 pParse->iSelfTab = 0;
135308 VdbeComment((v, "%s column %d", pIdx->zName, i));
135309 }else if( iField==XN_ROWID || iField==pTab->iPKey ){
135317 VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
135320 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
135321 VdbeComment((v, "for %s", pIdx->zName));
135323 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
135324 sqlite3SetMakeRecordP5(v, pIdx->pTable);
135327 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
135339 onError = pIdx->onError;
135352 if( pUpsertClause->isDoUpdate==0 ){
135367 ** must be explicitly deleted in order to ensure any pre-update hook
135371 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
135374 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
135376 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
135377 (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
135388 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
135396 ** is different from old-rowid */
135405 ** store it in registers regR..regR+nPk-1 */
135407 for(i=0; i<pPk->nKeyCol; i++){
135408 assert( pPk->aiColumn[i]>=0 );
135409 x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
135411 VdbeComment((v, "%s.%s", pTab->zName,
135412 pTab->aCol[pPk->aiColumn[i]].zCnName));
135423 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
135427 for(i=0; i<pPk->nKeyCol; i++){
135428 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
135429 x = pPk->aiColumn[i];
135431 if( i==(pPk->nKeyCol-1) ){
135475 nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
135476 assert( nConflictCk>0 || db->mallocFailed );
135504 if( pIdx->pPartIdxWhere ){
135507 sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk);
135511 ** the constraint-ok jump destination to be the address of
135532 nConflictCk--;
135587 sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec);
135601 ** to be the number of columns in table pTab that must not be NULL-trimmed.
135603 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
135609 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
135610 if( pTab->pSchema->file_format<2 ) return;
135612 for(i=pTab->nCol-1; i>0; i--){
135613 if( pTab->aCol[i].iDflt!=0 ) break;
135614 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
135623 ** PK index. This function adds code to invoke the pre-update hook,
135633 Vdbe *v = pParse->pVdbe;
135636 assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB );
135676 v = pParse->pVdbe;
135679 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
135681 assert( pIdx->onError!=OE_Replace
135682 || pIdx->pNext==0
135683 || pIdx->pNext->onError==OE_Replace );
135685 if( pIdx->pPartIdxWhere ){
135699 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
135703 if( pParse->nested ){
135716 if( !pParse->nested ){
135732 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
135738 ** pTab->pIndex list.
135740 ** If pTab is a virtual table, then this routine is a no-op and the
135764 /* This routine is a no-op for virtual tables. Leave the output
135767 *piDataCur = *piIdxCur = -999;
135770 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
135771 v = pParse->pVdbe;
135773 if( iBase<0 ) iBase = pParse->nTab;
135778 }else if( pParse->db->noSharedCache==0 ){
135779 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
135782 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
135784 assert( pIdx->pSchema==pTab->pSchema );
135790 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
135793 VdbeComment((v, "%s", pIdx->zName));
135796 if( iBase>pParse->nTab ) pParse->nTab = iBase;
135805 ** purposes only - to make sure the transfer optimization really
135827 assert( pDest->pTable!=pSrc->pTable );
135828 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
135831 if( pDest->onError!=pSrc->onError ){
135834 for(i=0; i<pSrc->nKeyCol; i++){
135835 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
135838 if( pSrc->aiColumn[i]==XN_EXPR ){
135839 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
135840 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
135841 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
135845 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
135848 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
135852 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
135870 ** There are lots of rules for determining compatibility - see comments
135875 ** is empty - a factor that can only be determined at run-time. In that
135892 sqlite3 *db = pParse->db;
135896 SrcItem *pItem; /* An element of pSelect->pSrc */
135909 if( pParse->pWith || pSelect->pWith ){
135921 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
135924 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
135925 if( pSelect->pSrc->nSrc!=1 ){
135928 if( pSelect->pSrc->a[0].pSelect ){
135931 if( pSelect->pWhere ){
135934 if( pSelect->pOrderBy ){
135939 if( pSelect->pGroupBy ){
135942 if( pSelect->pLimit ){
135945 if( pSelect->pPrior ){
135948 if( pSelect->selFlags & SF_Distinct ){
135951 pEList = pSelect->pEList;
135953 if( pEList->nExpr!=1 ){
135956 assert( pEList->a[0].pExpr );
135957 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
135965 pItem = pSelect->pSrc->a;
135970 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
135980 if( pDest->nCol!=pSrc->nCol ){
135983 if( pDest->iPKey!=pSrc->iPKey ){
135986 if( (pDest->tabFlags & TF_Strict)!=0 && (pSrc->tabFlags & TF_Strict)==0 ){
135987 return 0; /* Cannot feed from a non-strict into a strict table */
135989 for(i=0; i<pDest->nCol; i++){
135990 Column *pDestCol = &pDest->aCol[i];
135991 Column *pSrcCol = &pSrc->aCol[i];
135993 if( (db->mDbFlags & DBFLAG_Vacuum)==0
135994 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
136016 if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
136017 (pSrcCol->colFlags & COLFLAG_GENERATED) ){
136018 return 0; /* Both columns have the same generated-column type */
136024 if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
136027 sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){
136028 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
136029 testcase( pDestCol->colFlags & COLFLAG_STORED );
136034 if( pDestCol->affinity!=pSrcCol->affinity ){
136041 if( pDestCol->notNull && !pSrcCol->notNull ){
136045 if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
136048 assert( pDestExpr==0 || pDestExpr->op==TK_SPAN );
136050 assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN );
136053 || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken,
136054 pSrcExpr->u.zToken)!=0)
136060 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
136064 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
136070 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
136075 return 0; /* Corrupt schema - two indexes on the same btree */
136079 if( pDest->pCheck
136080 && (db->mDbFlags & DBFLAG_Vacuum)==0
136081 && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1)
136095 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){
136099 if( (db->flags & SQLITE_CountRows)!=0 ){
136110 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
136113 iSrc = pParse->nTab++;
136114 iDest = pParse->nTab++;
136121 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
136122 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
136151 if( pDest->iPKey>=0 ){
136153 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
136161 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
136165 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
136168 if( db->mDbFlags & DBFLAG_Vacuum ){
136175 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
136184 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
136185 sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE);
136193 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
136194 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
136196 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
136198 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
136202 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
136204 VdbeComment((v, "%s", pSrcIdx->zName));
136205 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
136208 VdbeComment((v, "%s", pDestIdx->zName));
136210 if( db->mDbFlags & DBFLAG_Vacuum ){
136215 ** order. In this case, instead of seeking within the b-tree as part
136217 ** OP_IdxInsert to seek to the point within the b-tree where each key
136225 for(i=0; i<pSrcIdx->nColumn; i++){
136226 const char *zColl = pSrcIdx->azColl[i];
136229 if( i==pSrcIdx->nColumn ){
136234 }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
136239 if( (db->mDbFlags & DBFLAG_Vacuum)==0
136315 sqlite3_mutex_enter(db->mutex);
136322 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
136328 /* this happens for a comment or white-space */
136341 && db->flags&SQLITE_NullCallback)) ){
136368 /* EVIDENCE-OF: R-38229-40159 If the callback function to
136369 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
136407 assert( (rc&db->errMask)==rc );
136408 sqlite3_mutex_leave(db->mutex);
136819 ** (part of the main SQLite library - not an extension) so that
136826 #define sqlite3_aggregate_context sqlite3_api->aggregate_context
136828 #define sqlite3_aggregate_count sqlite3_api->aggregate_count
136830 #define sqlite3_bind_blob sqlite3_api->bind_blob
136831 #define sqlite3_bind_double sqlite3_api->bind_double
136832 #define sqlite3_bind_int sqlite3_api->bind_int
136833 #define sqlite3_bind_int64 sqlite3_api->bind_int64
136834 #define sqlite3_bind_null sqlite3_api->bind_null
136835 #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
136836 #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
136837 #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
136838 #define sqlite3_bind_text sqlite3_api->bind_text
136839 #define sqlite3_bind_text16 sqlite3_api->bind_text16
136840 #define sqlite3_bind_value sqlite3_api->bind_value
136841 #define sqlite3_busy_handler sqlite3_api->busy_handler
136842 #define sqlite3_busy_timeout sqlite3_api->busy_timeout
136843 #define sqlite3_changes sqlite3_api->changes
136844 #define sqlite3_close sqlite3_api->close
136845 #define sqlite3_collation_needed sqlite3_api->collation_needed
136846 #define sqlite3_collation_needed16 sqlite3_api->collation_needed16
136847 #define sqlite3_column_blob sqlite3_api->column_blob
136848 #define sqlite3_column_bytes sqlite3_api->column_bytes
136849 #define sqlite3_column_bytes16 sqlite3_api->column_bytes16
136850 #define sqlite3_column_count sqlite3_api->column_count
136851 #define sqlite3_column_database_name sqlite3_api->column_database_name
136852 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
136853 #define sqlite3_column_decltype sqlite3_api->column_decltype
136854 #define sqlite3_column_decltype16 sqlite3_api->column_decltype16
136855 #define sqlite3_column_double sqlite3_api->column_double
136856 #define sqlite3_column_int sqlite3_api->column_int
136857 #define sqlite3_column_int64 sqlite3_api->column_int64
136858 #define sqlite3_column_name sqlite3_api->column_name
136859 #define sqlite3_column_name16 sqlite3_api->column_name16
136860 #define sqlite3_column_origin_name sqlite3_api->column_origin_name
136861 #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
136862 #define sqlite3_column_table_name sqlite3_api->column_table_name
136863 #define sqlite3_column_table_name16 sqlite3_api->column_table_name16
136864 #define sqlite3_column_text sqlite3_api->column_text
136865 #define sqlite3_column_text16 sqlite3_api->column_text16
136866 #define sqlite3_column_type sqlite3_api->column_type
136867 #define sqlite3_column_value sqlite3_api->column_value
136868 #define sqlite3_commit_hook sqlite3_api->commit_hook
136869 #define sqlite3_complete sqlite3_api->complete
136870 #define sqlite3_complete16 sqlite3_api->complete16
136871 #define sqlite3_create_collation sqlite3_api->create_collation
136872 #define sqlite3_create_collation16 sqlite3_api->create_collation16
136873 #define sqlite3_create_function sqlite3_api->create_function
136874 #define sqlite3_create_function16 sqlite3_api->create_function16
136875 #define sqlite3_create_module sqlite3_api->create_module
136876 #define sqlite3_create_module_v2 sqlite3_api->create_module_v2
136877 #define sqlite3_data_count sqlite3_api->data_count
136878 #define sqlite3_db_handle sqlite3_api->db_handle
136879 #define sqlite3_declare_vtab sqlite3_api->declare_vtab
136880 #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
136881 #define sqlite3_errcode sqlite3_api->errcode
136882 #define sqlite3_errmsg sqlite3_api->errmsg
136883 #define sqlite3_errmsg16 sqlite3_api->errmsg16
136884 #define sqlite3_exec sqlite3_api->exec
136886 #define sqlite3_expired sqlite3_api->expired
136888 #define sqlite3_finalize sqlite3_api->finalize
136889 #define sqlite3_free sqlite3_api->free
136890 #define sqlite3_free_table sqlite3_api->free_table
136891 #define sqlite3_get_autocommit sqlite3_api->get_autocommit
136892 #define sqlite3_get_auxdata sqlite3_api->get_auxdata
136893 #define sqlite3_get_table sqlite3_api->get_table
136895 #define sqlite3_global_recover sqlite3_api->global_recover
136897 #define sqlite3_interrupt sqlite3_api->interruptx
136898 #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
136899 #define sqlite3_libversion sqlite3_api->libversion
136900 #define sqlite3_libversion_number sqlite3_api->libversion_number
136901 #define sqlite3_malloc sqlite3_api->malloc
136902 #define sqlite3_mprintf sqlite3_api->mprintf
136903 #define sqlite3_open sqlite3_api->open
136904 #define sqlite3_open16 sqlite3_api->open16
136905 #define sqlite3_prepare sqlite3_api->prepare
136906 #define sqlite3_prepare16 sqlite3_api->prepare16
136907 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
136908 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
136909 #define sqlite3_profile sqlite3_api->profile
136910 #define sqlite3_progress_handler sqlite3_api->progress_handler
136911 #define sqlite3_realloc sqlite3_api->realloc
136912 #define sqlite3_reset sqlite3_api->reset
136913 #define sqlite3_result_blob sqlite3_api->result_blob
136914 #define sqlite3_result_double sqlite3_api->result_double
136915 #define sqlite3_result_error sqlite3_api->result_error
136916 #define sqlite3_result_error16 sqlite3_api->result_error16
136917 #define sqlite3_result_int sqlite3_api->result_int
136918 #define sqlite3_result_int64 sqlite3_api->result_int64
136919 #define sqlite3_result_null sqlite3_api->result_null
136920 #define sqlite3_result_text sqlite3_api->result_text
136921 #define sqlite3_result_text16 sqlite3_api->result_text16
136922 #define sqlite3_result_text16be sqlite3_api->result_text16be
136923 #define sqlite3_result_text16le sqlite3_api->result_text16le
136924 #define sqlite3_result_value sqlite3_api->result_value
136925 #define sqlite3_rollback_hook sqlite3_api->rollback_hook
136926 #define sqlite3_set_authorizer sqlite3_api->set_authorizer
136927 #define sqlite3_set_auxdata sqlite3_api->set_auxdata
136928 #define sqlite3_snprintf sqlite3_api->xsnprintf
136929 #define sqlite3_step sqlite3_api->step
136930 #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
136931 #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
136932 #define sqlite3_total_changes sqlite3_api->total_changes
136933 #define sqlite3_trace sqlite3_api->trace
136935 #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
136937 #define sqlite3_update_hook sqlite3_api->update_hook
136938 #define sqlite3_user_data sqlite3_api->user_data
136939 #define sqlite3_value_blob sqlite3_api->value_blob
136940 #define sqlite3_value_bytes sqlite3_api->value_bytes
136941 #define sqlite3_value_bytes16 sqlite3_api->value_bytes16
136942 #define sqlite3_value_double sqlite3_api->value_double
136943 #define sqlite3_value_int sqlite3_api->value_int
136944 #define sqlite3_value_int64 sqlite3_api->value_int64
136945 #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
136946 #define sqlite3_value_text sqlite3_api->value_text
136947 #define sqlite3_value_text16 sqlite3_api->value_text16
136948 #define sqlite3_value_text16be sqlite3_api->value_text16be
136949 #define sqlite3_value_text16le sqlite3_api->value_text16le
136950 #define sqlite3_value_type sqlite3_api->value_type
136951 #define sqlite3_vmprintf sqlite3_api->vmprintf
136952 #define sqlite3_vsnprintf sqlite3_api->xvsnprintf
136953 #define sqlite3_overload_function sqlite3_api->overload_function
136954 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
136955 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
136956 #define sqlite3_clear_bindings sqlite3_api->clear_bindings
136957 #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
136958 #define sqlite3_blob_bytes sqlite3_api->blob_bytes
136959 #define sqlite3_blob_close sqlite3_api->blob_close
136960 #define sqlite3_blob_open sqlite3_api->blob_open
136961 #define sqlite3_blob_read sqlite3_api->blob_read
136962 #define sqlite3_blob_write sqlite3_api->blob_write
136963 #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
136964 #define sqlite3_file_control sqlite3_api->file_control
136965 #define sqlite3_memory_highwater sqlite3_api->memory_highwater
136966 #define sqlite3_memory_used sqlite3_api->memory_used
136967 #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
136968 #define sqlite3_mutex_enter sqlite3_api->mutex_enter
136969 #define sqlite3_mutex_free sqlite3_api->mutex_free
136970 #define sqlite3_mutex_leave sqlite3_api->mutex_leave
136971 #define sqlite3_mutex_try sqlite3_api->mutex_try
136972 #define sqlite3_open_v2 sqlite3_api->open_v2
136973 #define sqlite3_release_memory sqlite3_api->release_memory
136974 #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
136975 #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
136976 #define sqlite3_sleep sqlite3_api->sleep
136977 #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
136978 #define sqlite3_vfs_find sqlite3_api->vfs_find
136979 #define sqlite3_vfs_register sqlite3_api->vfs_register
136980 #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
136981 #define sqlite3_threadsafe sqlite3_api->xthreadsafe
136982 #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
136983 #define sqlite3_result_error_code sqlite3_api->result_error_code
136984 #define sqlite3_test_control sqlite3_api->test_control
136985 #define sqlite3_randomness sqlite3_api->randomness
136986 #define sqlite3_context_db_handle sqlite3_api->context_db_handle
136987 #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
136988 #define sqlite3_limit sqlite3_api->limit
136989 #define sqlite3_next_stmt sqlite3_api->next_stmt
136990 #define sqlite3_sql sqlite3_api->sql
136991 #define sqlite3_status sqlite3_api->status
136992 #define sqlite3_backup_finish sqlite3_api->backup_finish
136993 #define sqlite3_backup_init sqlite3_api->backup_init
136994 #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
136995 #define sqlite3_backup_remaining sqlite3_api->backup_remaining
136996 #define sqlite3_backup_step sqlite3_api->backup_step
136997 #define sqlite3_compileoption_get sqlite3_api->compileoption_get
136998 #define sqlite3_compileoption_used sqlite3_api->compileoption_used
136999 #define sqlite3_create_function_v2 sqlite3_api->create_function_v2
137000 #define sqlite3_db_config sqlite3_api->db_config
137001 #define sqlite3_db_mutex sqlite3_api->db_mutex
137002 #define sqlite3_db_status sqlite3_api->db_status
137003 #define sqlite3_extended_errcode sqlite3_api->extended_errcode
137004 #define sqlite3_log sqlite3_api->log
137005 #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
137006 #define sqlite3_sourceid sqlite3_api->sourceid
137007 #define sqlite3_stmt_status sqlite3_api->stmt_status
137008 #define sqlite3_strnicmp sqlite3_api->strnicmp
137009 #define sqlite3_unlock_notify sqlite3_api->unlock_notify
137010 #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
137011 #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
137012 #define sqlite3_wal_hook sqlite3_api->wal_hook
137013 #define sqlite3_blob_reopen sqlite3_api->blob_reopen
137014 #define sqlite3_vtab_config sqlite3_api->vtab_config
137015 #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
137017 #define sqlite3_close_v2 sqlite3_api->close_v2
137018 #define sqlite3_db_filename sqlite3_api->db_filename
137019 #define sqlite3_db_readonly sqlite3_api->db_readonly
137020 #define sqlite3_db_release_memory sqlite3_api->db_release_memory
137021 #define sqlite3_errstr sqlite3_api->errstr
137022 #define sqlite3_stmt_busy sqlite3_api->stmt_busy
137023 #define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
137024 #define sqlite3_stricmp sqlite3_api->stricmp
137025 #define sqlite3_uri_boolean sqlite3_api->uri_boolean
137026 #define sqlite3_uri_int64 sqlite3_api->uri_int64
137027 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
137028 #define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
137029 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
137031 #define sqlite3_auto_extension sqlite3_api->auto_extension
137032 #define sqlite3_bind_blob64 sqlite3_api->bind_blob64
137033 #define sqlite3_bind_text64 sqlite3_api->bind_text64
137034 #define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
137035 #define sqlite3_load_extension sqlite3_api->load_extension
137036 #define sqlite3_malloc64 sqlite3_api->malloc64
137037 #define sqlite3_msize sqlite3_api->msize
137038 #define sqlite3_realloc64 sqlite3_api->realloc64
137039 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
137040 #define sqlite3_result_blob64 sqlite3_api->result_blob64
137041 #define sqlite3_result_text64 sqlite3_api->result_text64
137042 #define sqlite3_strglob sqlite3_api->strglob
137044 #define sqlite3_value_dup sqlite3_api->value_dup
137045 #define sqlite3_value_free sqlite3_api->value_free
137046 #define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
137047 #define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
137049 #define sqlite3_value_subtype sqlite3_api->value_subtype
137050 #define sqlite3_result_subtype sqlite3_api->result_subtype
137052 #define sqlite3_status64 sqlite3_api->status64
137053 #define sqlite3_strlike sqlite3_api->strlike
137054 #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
137056 #define sqlite3_system_errno sqlite3_api->system_errno
137058 #define sqlite3_trace_v2 sqlite3_api->trace_v2
137059 #define sqlite3_expanded_sql sqlite3_api->expanded_sql
137061 #define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
137063 #define sqlite3_prepare_v3 sqlite3_api->prepare_v3
137064 #define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
137065 #define sqlite3_bind_pointer sqlite3_api->bind_pointer
137066 #define sqlite3_result_pointer sqlite3_api->result_pointer
137067 #define sqlite3_value_pointer sqlite3_api->value_pointer
137069 #define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
137070 #define sqlite3_value_nochange sqlite3_api->value_nochange
137071 #define sqlite3_vtab_collation sqlite3_api->vtab_collation
137073 #define sqlite3_keyword_count sqlite3_api->keyword_count
137074 #define sqlite3_keyword_name sqlite3_api->keyword_name
137075 #define sqlite3_keyword_check sqlite3_api->keyword_check
137076 #define sqlite3_str_new sqlite3_api->str_new
137077 #define sqlite3_str_finish sqlite3_api->str_finish
137078 #define sqlite3_str_appendf sqlite3_api->str_appendf
137079 #define sqlite3_str_vappendf sqlite3_api->str_vappendf
137080 #define sqlite3_str_append sqlite3_api->str_append
137081 #define sqlite3_str_appendall sqlite3_api->str_appendall
137082 #define sqlite3_str_appendchar sqlite3_api->str_appendchar
137083 #define sqlite3_str_reset sqlite3_api->str_reset
137084 #define sqlite3_str_errcode sqlite3_api->str_errcode
137085 #define sqlite3_str_length sqlite3_api->str_length
137086 #define sqlite3_str_value sqlite3_api->str_value
137088 #define sqlite3_create_window_function sqlite3_api->create_window_function
137090 #define sqlite3_normalized_sql sqlite3_api->normalized_sql
137092 #define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
137093 #define sqlite3_value_frombind sqlite3_api->value_frombind
137095 #define sqlite3_drop_modules sqlite3_api->drop_modules
137097 #define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
137098 #define sqlite3_uri_key sqlite3_api->uri_key
137099 #define sqlite3_filename_database sqlite3_api->filename_database
137100 #define sqlite3_filename_journal sqlite3_api->filename_journal
137101 #define sqlite3_filename_wal sqlite3_api->filename_wal
137103 #define sqlite3_create_filename sqlite3_api->create_filename
137104 #define sqlite3_free_filename sqlite3_api->free_filename
137105 #define sqlite3_database_file_object sqlite3_api->database_file_object
137107 #define sqlite3_txn_state sqlite3_api->txn_state
137109 #define sqlite3_changes64 sqlite3_api->changes64
137110 #define sqlite3_total_changes64 sqlite3_api->total_changes64
137112 #define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
137114 #define sqlite3_error_offset sqlite3_api->error_offset
137115 #define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
137116 #define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
137117 #define sqlite3_vtab_in sqlite3_api->vtab_in
137118 #define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
137119 #define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
137122 #define sqlite3_deserialize sqlite3_api->deserialize
137123 #define sqlite3_serialize sqlite3_api->serialize
137125 #define sqlite3_db_name sqlite3_api->db_name
137127 #define sqlite3_value_encoding sqlite3_api->value_encoding
137129 #define sqlite3_is_interrupted sqlite3_api->is_interrupted
137131 #define sqlite3_stmt_explain sqlite3_api->stmt_explain
137133 #define sqlite3_get_clientdata sqlite3_api->get_clientdata
137134 #define sqlite3_set_clientdata sqlite3_api->set_clientdata
137147 # define SQLITE_EXTENSION_INIT1 /*no-op*/
137149 # define SQLITE_EXTENSION_INIT3 /*no-op*/
137685 sqlite3_vfs *pVfs = db->pVfs;
137717 if( (db->flags & SQLITE_LoadExtension)==0 ){
137726 /* tag-20210611-1. Some dlopen() implementations will segfault if given
137731 ** Later (2023-03-25): Save an extra 6 bytes for the filename suffix.
137774 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
137794 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
137813 /* Append the new shared library handle to the db->aExtension array. */
137814 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
137818 if( db->nExtension>0 ){
137819 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
137821 sqlite3DbFree(db, db->aExtension);
137822 db->aExtension = aHandle;
137824 db->aExtension[db->nExtension++] = handle;
137835 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
137847 sqlite3_mutex_enter(db->mutex);
137850 sqlite3_mutex_leave(db->mutex);
137860 assert( sqlite3_mutex_held(db->mutex) );
137861 for(i=0; i<db->nExtension; i++){
137862 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
137864 sqlite3DbFree(db, db->aExtension);
137875 sqlite3_mutex_enter(db->mutex);
137877 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
137879 db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
137881 sqlite3_mutex_leave(db->mutex);
137902 ** we have to locate the state vector at run-time. In the more common
137965 ** routine is a no-op.
137983 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
137985 wsdAutoext.nExt--;
138151 #define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
138155 #define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
138157 /* Names of columns for pragmas that return multi-column result
138158 ** or that return single-column results where the name of the
138225 /* Definitions of all built-in pragmas */
138760 ** the following macro or to the actual analysis_limit if it is non-zero,
138763 ** The value of 2000 is chosen emperically so that the worst-case run-time
138765 ** of test databases on a RaspberryPI-4 compiled using -Os and without
138766 ** -DSQLITE_DEBUG. Of course, your mileage may vary. For the purpose of
138767 ** this paragraph, "worst-case" means that ANALYZE ends up being
138774 ** optimize on a PI-4 is more like 1 millisecond or less with the 0x10000
138844 ** Interpret the given string as an auto-vacuum mode value.
138862 ** backed temporary databases, 2 for the Red-Black tree in memory database
138863 ** and 0 to use the compile-time default.
138867 return z[0] - '0';
138884 sqlite3 *db = pParse->db;
138885 if( db->aDb[1].pBt!=0 ){
138886 if( !db->autoCommit
138887 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE
138893 sqlite3BtreeClose(db->aDb[1].pBt);
138894 db->aDb[1].pBt = 0;
138909 sqlite3 *db = pParse->db;
138910 if( db->temp_store==ts ) return SQLITE_OK;
138914 db->temp_store = (u8)ts;
138926 u8 n = pPragma->nPragCName;
138929 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
138932 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
138966 if( db->autoCommit ){
138967 Db *pDb = db->aDb;
138968 int n = db->nDb;
138974 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
138975 while( (n--) > 0 ){
138976 if( pDb->pBt ){
138977 sqlite3BtreeSetPagerFlags(pDb->pBt,
138978 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
138985 # define setAllPagerFlags(X) /* no-op */
138990 ** Return a human-readable name for a constraint resolution action.
139011 ** journal-mode name.
139038 upr = ArraySize(aPragmaName)-1;
139044 upr = mid - 1;
139059 int isBuiltin, /* True if this is a built-in function */
139070 for(; p; p=p->pNext){
139079 if( p->xSFunc==0 ) continue;
139080 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
139085 if( p->xValue!=0 ){
139087 }else if( p->xFinalize!=0 ){
139093 p->zName, isBuiltin,
139094 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
139095 p->nArg,
139096 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
139105 ** Generate code to output a single-column result row with a value of the
139138 int minusFlag /* True if a '-' sign preceded <value> */
139140 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
139141 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
139147 sqlite3 *db = pParse->db; /* The database connection */
139154 pParse->nMem = 2;
139160 pDb = &db->aDb[iDb];
139172 zRight = sqlite3MPrintf(db, "-%T", pValue);
139178 zDb = pId2->n>0 ? pDb->zDbSName : 0;
139183 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
139185 ** handled the pragma and generate a no-op prepared statement.
139187 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
139192 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
139202 db->busyHandler.nBusy = 0;
139216 pParse->nErr++;
139217 pParse->rc = rc;
139224 /* IMP: R-43042-22504 No error messages are generated if an
139230 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
139235 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
139236 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
139242 switch( pPragma->ePragTyp ){
139277 pParse->nMem += 2;
139289 pDb->pSchema->cache_size = size;
139290 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
139307 Btree *pBt = pDb->pBt;
139313 /* Malloc may fail when setting the page-size, as there is an internal
139316 db->nextPagesize = sqlite3Atoi(zRight);
139317 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
139333 Btree *pBt = pDb->pBt;
139334 int b = -1;
139343 if( pId2->n==0 && b>=0 ){
139345 for(ii=0; ii<db->nDb; ii++){
139346 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
139375 iReg = ++pParse->nMem;
139399 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
139402 ** the locking-mode of the main database).
139404 eMode = db->dfltLockMode;
139407 if( pId2->n==0 ){
139409 ** of the PRAGMA command. In this case the locking-mode must be
139417 assert(pDb==&db->aDb[0]);
139418 for(ii=2; ii<db->nDb; ii++){
139419 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
139422 db->dfltLockMode = (u8)eMode;
139424 pPager = sqlite3BtreePager(pDb->pBt);
139461 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
139462 /* Do not allow journal-mode "OFF" in defensive since the database
139467 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
139470 pId2->n = 1;
139472 for(ii=db->nDb-1; ii>=0; ii--){
139473 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
139489 Pager *pPager = sqlite3BtreePager(pDb->pBt);
139490 i64 iLimit = -2;
139493 if( iLimit<-1 ) iLimit = -1;
139506 ** Get or set the value of the database 'auto-vacuum' parameter.
139511 Btree *pBt = pDb->pBt;
139518 db->nextAutovac = (u8)eAuto;
139520 ** incr-vacuum flags. This is required in case this connection
139522 ** as an auto-vacuum capable db.
139529 ** that this really is an auto-vacuum capable database.
139548 aOp[4].p3 = eAuto - 1;
139571 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
139587 ** number of pages is adjusted so that the cache uses -N kibibytes
139593 returnSingleInt(v, pDb->pSchema->cache_size);
139596 pDb->pSchema->cache_size = size;
139597 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
139614 ** number of pages is adjusted so that the cache uses -N kibibytes
139628 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
139629 sqlite3BtreeSetSpillSize(pDb->pBt,0));
139633 sqlite3BtreeSetSpillSize(pDb->pBt, size);
139636 db->flags |= SQLITE_CacheSpill;
139638 db->flags &= ~(u64)SQLITE_CacheSpill;
139667 if( pId2->n==0 ) db->szMmap = sz;
139668 for(ii=db->nDb-1; ii>=0; ii--){
139669 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
139670 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
139674 sz = -1;
139683 pParse->nErr++;
139684 pParse->rc = rc;
139697 ** Note that it is possible for the library compile-time options to
139702 returnSingleInt(v, db->temp_store);
139727 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
139735 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
139736 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
139774 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
139805 Pager *pPager = sqlite3BtreePager(pDb->pBt);
139812 Pager *pPager = sqlite3BtreePager(pDb->pBt);
139842 returnSingleInt(v, pDb->safety_level-1);
139844 if( !db->autoCommit ){
139850 pDb->safety_level = iLevel;
139851 pDb->bSyncSet = 1;
139863 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
139865 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
139866 if( db->autoCommit==0 ){
139868 ** in auto-commit mode. */
139872 if( db->auth.authLevel==UAUTH_User ){
139873 /* Do not allow non-admin users to modify the schema arbitrarily */
139880 || (db->flags & SQLITE_Defensive)==0
139882 db->flags |= mask;
139885 db->flags &= ~mask;
139886 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
139890 /* IMP: R-60817-01178 If the argument is "RESET" then schema
139897 /* Many of the flag-pragmas modify the code generated by the SQL
139920 ** pk: Non-zero for PK fields.
139931 pParse->nMem = 7;
139933 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
139936 if( pCol->colFlags & COLFLAG_NOINSERT ){
139937 if( pPragma->iArg==0 ){
139941 if( pCol->colFlags & COLFLAG_VIRTUAL ){
139943 }else if( pCol->colFlags & COLFLAG_STORED ){
139945 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
139949 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
139954 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
139957 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
139960 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
139961 i-nHidden,
139962 pCol->zCnName,
139964 pCol->notNull ? 1 : 0,
139965 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
139988 pParse->nMem = 6;
139990 for(ii=0; ii<db->nDb; ii++){
139994 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
140001 pHash = &db->aDb[ii].pSchema->tblHash;
140003 while( initNCol-- ){
140008 if( pTab->nCol==0 ){
140009 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
140012 (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
140016 if( db->mallocFailed ){
140017 sqlite3ErrorMsg(db->pParse, "out of memory");
140018 db->pParse->rc = SQLITE_NOMEM_BKPT;
140020 pHash = &db->aDb[ii].pSchema->tblHash;
140029 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
140034 }else if( pTab->tabFlags & TF_Shadow ){
140040 db->aDb[ii].zDbSName,
140041 sqlite3PreferredTableName(pTab->zName),
140043 pTab->nCol,
140044 (pTab->tabFlags & TF_WithoutRowid)!=0,
140045 (pTab->tabFlags & TF_Strict)!=0
140056 pParse->nMem = 5;
140058 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
140061 sqlite3PreferredTableName(pTab->zName),
140063 pTab->szTabRow,
140064 pTab->nRowLogEst,
140065 pTab->tabFlags);
140066 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
140068 pIdx->zName,
140069 pIdx->szIdxRow,
140070 pIdx->aiRowLogEst[0],
140071 pIdx->hasStat1);
140093 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
140096 if( pPragma->iArg ){
140098 mx = pIdx->nColumn;
140099 pParse->nMem = 6;
140102 mx = pIdx->nKeyCol;
140103 pParse->nMem = 3;
140105 pTab = pIdx->pTable;
140107 assert( pParse->nMem<=pPragma->nPragCName );
140109 i16 cnum = pIdx->aiColumn[i];
140111 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
140112 if( pPragma->iArg ){
140114 pIdx->aSortOrder[i],
140115 pIdx->azColl[i],
140116 i<pIdx->nKeyCol);
140118 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
140130 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
140131 pParse->nMem = 5;
140133 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
140137 pIdx->zName,
140139 azOrigin[pIdx->idxType],
140140 pIdx->pPartIdxWhere!=0);
140148 pParse->nMem = 3;
140149 for(i=0; i<db->nDb; i++){
140150 if( db->aDb[i].pBt==0 ) continue;
140151 assert( db->aDb[i].zDbSName!=0 );
140154 db->aDb[i].zDbSName,
140155 sqlite3BtreeGetFilename(db->aDb[i].pBt));
140163 pParse->nMem = 2;
140164 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
140166 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
140176 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
140177 pParse->nMem = 6;
140179 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
140180 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
140184 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
140186 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
140195 pParse->nMem = 1;
140196 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
140198 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
140221 pFK = pTab->u.tab.pFKey;
140223 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
140225 pParse->nMem = 8;
140229 for(j=0; j<pFK->nCol; j++){
140233 pFK->zTo,
140234 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
140235 pFK->aCol[j].zCol,
140236 actionName(pFK->aAction[1]), /* ON UPDATE */
140237 actionName(pFK->aAction[0]), /* ON DELETE */
140241 pFK = pFK->pNextFrom;
140266 regResult = pParse->nMem+1;
140267 pParse->nMem += 4;
140268 regRow = ++pParse->nMem;
140269 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
140278 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
140279 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
140280 zDb = db->aDb[iDb].zDbSName;
140282 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
140283 sqlite3TouchRegister(pParse, pTab->nCol+regRow);
140285 sqlite3VdbeLoadString(v, regResult, pTab->zName);
140287 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
140288 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
140291 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
140297 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
140305 assert( pParse->nErr>0 || pFK==0 );
140307 if( pParse->nTab<i ) pParse->nTab = i;
140310 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
140311 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
140316 assert( x==0 || db->mallocFailed );
140324 sqlite3TouchRegister(pParse, regRow + pFK->nCol);
140325 for(j=0; j<pFK->nCol; j++){
140326 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
140334 sqlite3VdbeAddOp4(v, OP_Affinity, regRow, pFK->nCol, 0,
140335 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
140336 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regRow, pFK->nCol);
140342 assert( pFK->nCol==1 || db->mallocFailed );
140351 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
140390 ** without the overhead of cross-checking indexes. Quick_check
140418 ** to -1 here, to indicate that the VDBE should verify the integrity
140421 assert( iDb==0 || pId2->z );
140422 if( pId2->z==0 ) iDb = -1;
140425 pParse->nMem = 6;
140430 if( sqlite3GetInt32(pValue->z, &mxErr) ){
140436 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
140439 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
140442 for(i=0; i<db->nDb; i++){
140452 pParse->okConstFactor = 0; /* tag-20230327-1 */
140454 /* Do an integrity check of the B-Tree
140460 pTbls = &db->aDb[i].pSchema->tblHash;
140467 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
140479 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
140480 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
140481 aRoot[++cnt] = pIdx->tnum;
140490 /* Do the b-tree integrity checks */
140495 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
140513 for(pIdx=pTab->pIndex; ALWAYS(pIdx); pIdx=pIdx->pNext){
140518 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
140519 if( pIdx->pPartIdxWhere==0 ){
140522 sqlite3VdbeLoadString(v, 4, pIdx->zName);
140539 int r1 = -1;
140542 int mxCol; /* Maximum non-virtual column number */
140551 r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol);
140552 sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1);
140557 ** reg[8+i] counts the number of entries in the i-th index
140560 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
140563 assert( pParse->nMem>=8+j );
140568 /* Fetch the right-most column from the table. This will cause
140575 mxCol = -1;
140576 for(j=0; j<pTab->nCol; j++){
140577 if( (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)==0 ) mxCol++;
140579 if( mxCol==pTab->iPKey ) mxCol--;
140582 ** PK index column-count, so there is no need to account for them
140584 mxCol = sqlite3PrimaryKeyIndex(pTab)->nColumn-1;
140596 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
140601 pTab->zName);
140606 for(j=0; j<pPk->nKeyCol; j++){
140614 ** (2) Datatype must be exact for non-ANY columns in STRICT tables
140615 ** (3) Datatype for TEXT columns in non-STRICT tables must be
140617 ** (4) Datatype for numeric columns in non-STRICT tables must not
140620 bStrict = (pTab->tabFlags & TF_Strict)!=0;
140621 for(j=0; j<pTab->nCol; j++){
140623 Column *pCol = pTab->aCol + j; /* The column to be checked */
140629 if( j==pTab->iPKey ) continue;
140631 doTypeCheck = pCol->eCType>COLTYPE_ANY;
140633 doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB;
140635 if( pCol->notNull==0 && !doTypeCheck ) continue;
140639 if( pCol->colFlags & COLFLAG_VIRTUAL ){
140641 p1 = -1;
140644 if( pCol->iDflt ){
140647 pCol->affinity, &pDfltValue);
140665 if( pCol->notNull ){
140684 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
140685 pCol->zCnName);
140696 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
140706 assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) );
140707 sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
140709 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
140710 sqlite3StdType[pCol->eCType-1],
140711 pTab->zName, pTab->aCol[j].zCnName);
140713 }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){
140714 /* (3) Datatype for TEXT columns in non-STRICT tables must be
140720 pTab->zName, pTab->aCol[j].zCnName);
140722 }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){
140723 /* (4) Datatype for numeric columns in non-STRICT tables must not
140732 sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4);
140736 pTab->zName, pTab->aCol[j].zCnName);
140744 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
140745 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
140746 if( db->mallocFailed==0 ){
140751 pParse->iSelfTab = iDataCur + 1;
140752 for(k=pCheck->nExpr-1; k>0; k--){
140753 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
140755 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
140758 pParse->iSelfTab = 0;
140760 pTab->zName);
140769 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
140780 pIdx->nColumn); VdbeCoverage(v);
140785 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
140797 jmp7 = sqlite3VdbeAddOp3(v, OP_Eq, 3, 0, r1+pIdx->nColumn-1);
140800 "rowid not at end-of-record for row ");
140803 sqlite3VdbeGoto(v, jmp5-1);
140807 /* Any indexed columns with non-BINARY collations must still hold
140810 for(kk=0; kk<pIdx->nKeyCol; kk++){
140811 if( pIdx->azColl[kk]==sqlite3StrBINARY ) continue;
140822 sqlite3VdbeGoto(v, jmp5-1);
140832 for(kk=0; kk<pIdx->nKeyCol; kk++){
140833 int iCol = pIdx->aiColumn[kk];
140834 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
140835 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
140843 pIdx->nKeyCol); VdbeCoverage(v);
140844 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
140853 sqlite3VdbeJumpHere(v, loopTop-1);
140856 sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
140871 if( pTab->nCol<=0 ){
140872 const char *zMod = pTab->u.vtab.azArg[0];
140873 if( sqlite3HashFind(&db->aModule, zMod)==0 ) continue;
140876 if( pTab->u.vtab.p==0 ) continue;
140877 pVTab = pTab->u.vtab.p->pVtab;
140879 if( NEVER(pVTab->pModule==0) ) continue;
140880 if( pVTab->pModule->iVersion<4 ) continue;
140881 if( pVTab->pModule->xIntegrity==0 ) continue;
140883 pTab->nTabRef++;
140907 aOp[0].p2 = 1-mxErr;
140913 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
140922 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
140927 ** The second form of this pragma is a no-op if the main database file
140948 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
140949 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
140950 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
140953 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
140963 returnSingleText(v, encnames[ENC(pParse->db)].zName);
140970 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
140971 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
140972 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
140973 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
140979 if( !pEnc->zName ){
141004 ** the value of the schema-version and user-version, respectively. Both
141005 ** the schema-version and the user-version are 32-bit signed integers
141008 ** The schema-cookie is usually only manipulated internally by SQLite. It
141015 ** the schema-version is potentially dangerous and may lead to program
141018 ** The user-version is not used internally by SQLite. It may be used by
141022 int iCookie = pPragma->iArg; /* Which cookie to read or write */
141024 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
141039 if( iCookie==BTREE_SCHEMA_VERSION && (db->flags & SQLITE_Defensive)!=0 ){
141041 ** mode. Change the OP_SetCookie opcode into a no-op. */
141068 ** Return the names of all compile-time options used in this build,
141074 pParse->nMem = 1;
141091 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
141102 pParse->nMem = 3;
141121 db->xWalCallback==sqlite3WalDefaultHook ?
141122 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
141130 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
141163 ** SQLITE_DEFAULT_OPTIMIZE_LIMIT compile-time option.
141165 ** currently (2024-02-19) set to 2000, which is such that
141166 ** the worst case run-time for PRAGMA optimize on a 100MB
141168 ** a RaspberryPI-4 class machine. On by default.
141195 ** (4c) The query planner used sqlite_stat1-style statistics for one
141203 ** 10-fold. In other words, the current size of the table is
141235 }else if( db->nAnalysisLimit>0
141236 && db->nAnalysisLimit<SQLITE_DEFAULT_OPTIMIZE_LIMIT ){
141241 iTabCur = pParse->nTab++;
141242 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
141245 pSchema = db->aDb[iDb].pSchema;
141246 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
141253 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ) continue;
141256 ** If any index is unanalyzed, then the threshold is -1 to
141259 szThreshold = pTab->nRowLogEst;
141261 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
141263 if( !pIdx->hasStat1 ){
141264 szThreshold = -1; /* Always analyze if any index lacks statistics */
141271 if( (pTab->tabFlags & TF_MaybeReanalyze)!=0 ){
141275 }else if( pTab->pIndex!=0 && szThreshold<0 ){
141298 szThreshold>=iRange ? szThreshold-iRange : -1,
141307 db->aDb[iDb].zDbSName, pTab->zName);
141321 ** the analysis_limit to avoid excess run-time in the worst case.
141323 if( !db->mallocFailed && nLimit>0 && nBtree>100 ){
141347 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
141351 returnSingleInt(v, db->busyTimeout);
141359 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
141361 ** specified and is a non-negative integer.
141362 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
141364 ** sqlite3_soft_heap_limit64(-1) C-language function.
141371 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
141382 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
141389 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
141392 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
141411 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
141425 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */
141428 db->nAnalysisLimit = (int)(N&0x7fffffff);
141430 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
141443 pParse->nMem = 2;
141444 for(i=0; i<db->nDb; i++){
141448 if( db->aDb[i].zDbSName==0 ) continue;
141449 pBt = db->aDb[i].pBt;
141452 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
141456 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
141464 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
141473 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
141478 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
141529 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
141534 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
141538 if( pPragma->mPragFlg & PragFlg_Result1 ){
141542 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
141548 assert( strlen(zBuf) < sizeof(zBuf)-1 );
141556 pTab->pName = pPragma;
141557 pTab->db = db;
141558 pTab->iHidden = i;
141559 pTab->nHidden = j;
141591 pIdxInfo->estimatedCost = (double)1;
141592 if( pTab->nHidden==0 ){ return SQLITE_OK; }
141593 pConstraint = pIdxInfo->aConstraint;
141596 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
141597 if( pConstraint->iColumn < pTab->iHidden ) continue;
141598 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
141599 if( pConstraint->usable==0 ) return SQLITE_CONSTRAINT;
141600 j = pConstraint->iColumn - pTab->iHidden;
141605 pIdxInfo->estimatedCost = (double)2147483647;
141606 pIdxInfo->estimatedRows = 2147483647;
141609 j = seen[0]-1;
141610 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
141611 pIdxInfo->aConstraintUsage[j].omit = 1;
141612 pIdxInfo->estimatedCost = (double)20;
141613 pIdxInfo->estimatedRows = 20;
141615 j = seen[1]-1;
141616 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
141617 pIdxInfo->aConstraintUsage[j].omit = 1;
141628 pCsr->base.pVtab = pVtab;
141629 *ppCursor = &pCsr->base;
141636 sqlite3_finalize(pCsr->pPragma);
141637 pCsr->pPragma = 0;
141638 pCsr->iRowid = 0;
141639 for(i=0; i<ArraySize(pCsr->azArg); i++){
141640 sqlite3_free(pCsr->azArg[i]);
141641 pCsr->azArg[i] = 0;
141659 pCsr->iRowid++;
141660 assert( pCsr->pPragma );
141661 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
141662 rc = sqlite3_finalize(pCsr->pPragma);
141663 pCsr->pPragma = 0;
141678 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
141687 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
141690 assert( j<ArraySize(pCsr->azArg) );
141691 assert( pCsr->azArg[j]==0 );
141693 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
141694 if( pCsr->azArg[j]==0 ){
141699 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
141701 if( pCsr->azArg[1] ){
141702 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
141704 sqlite3_str_appendall(&acc, pTab->pName->zName);
141705 if( pCsr->azArg[0] ){
141706 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
141710 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
141713 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
141724 return (pCsr->pPragma==0);
141736 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
141737 if( i<pTab->iHidden ){
141738 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
141740 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
141750 *p = pCsr->iRowid;
141757 0, /* xCreate - create a table */
141758 pragmaVtabConnect, /* xConnect - connect to an existing table */
141759 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
141760 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
141761 0, /* xDestroy - Drop a table */
141762 pragmaVtabOpen, /* xOpen - open a cursor */
141763 pragmaVtabClose, /* xClose - close a cursor */
141764 pragmaVtabFilter, /* xFilter - configure scan constraints */
141765 pragmaVtabNext, /* xNext - advance a cursor */
141767 pragmaVtabColumn, /* xColumn - read data */
141768 pragmaVtabRowid, /* xRowid - read data */
141769 0, /* xUpdate - write data */
141770 0, /* xBegin - begin transaction */
141771 0, /* xSync - sync transaction */
141772 0, /* xCommit - commit transaction */
141773 0, /* xRollback - rollback transaction */
141774 0, /* xFindFunction - function overloading */
141775 0, /* xRename - rename the table */
141793 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
141794 assert( sqlite3HashFind(&db->aModule, zName)==0 );
141830 sqlite3 *db = pData->db;
141831 if( db->mallocFailed ){
141832 pData->rc = SQLITE_NOMEM_BKPT;
141833 }else if( pData->pzErrMsg[0]!=0 ){
141835 }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){
141841 *pData->pzErrMsg = sqlite3MPrintf(db,
141843 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1],
141846 pData->rc = SQLITE_ERROR;
141847 }else if( db->flags & SQLITE_WriteSchema ){
141848 pData->rc = SQLITE_CORRUPT_BKPT;
141853 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
141854 *pData->pzErrMsg = z;
141855 pData->rc = SQLITE_CORRUPT_BKPT;
141866 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
141867 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
141875 const char *zSql, /* UTF-8 encoded SQL statement. */
141900 sqlite3 *db = pData->db;
141901 int iDb = pData->iDb;
141905 assert( sqlite3_mutex_held(db->mutex) );
141906 db->mDbFlags |= DBFLAG_EncodingFixed;
141908 pData->nInitRow++;
141909 if( db->mallocFailed ){
141914 assert( iDb>=0 && iDb<db->nDb );
141921 ** But because db->init.busy is set to 1, no VDBE code is generated
141931 u8 saved_iDb = db->init.iDb;
141935 assert( db->init.busy );
141936 db->init.iDb = iDb;
141937 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
141938 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
141944 db->init.orphanTrigger = 0;
141945 db->init.azInit = (const char**)argv;
141947 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
141948 rc = db->errCode;
141950 db->init.iDb = saved_iDb;
141951 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
141953 if( db->init.orphanTrigger ){
141956 if( rc > pData->rc ) pData->rc = rc;
141964 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
141976 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
141980 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
141981 || pIndex->tnum<2
141982 || pIndex->tnum>pData->mxPage
142013 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
142015 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
142016 assert( iDb>=0 && iDb<db->nDb );
142017 assert( db->aDb[iDb].pSchema );
142018 assert( sqlite3_mutex_held(db->mutex) );
142019 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
142021 db->init.busy = 1;
142023 /* Construct the in-memory representation schema tables (sqlite_schema or
142027 ** the schema table as read-only. */
142043 db->mDbFlags &= mask;
142051 pDb = &db->aDb[iDb];
142052 if( pDb->pBt==0 ){
142059 /* If there is not already a read-only (or read-write) transaction opened
142060 ** on the b-tree database, open one now. If a transaction is opened, it
142062 sqlite3BtreeEnter(pDb->pBt);
142063 if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){
142064 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
142079 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
142090 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
142092 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
142095 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
142097 /* If opening a non-empty database, check the text encoding. For the
142102 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
142103 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
142107 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
142112 if( db->nVdbeActive>0 && encoding!=ENC(db)
142113 && (db->mDbFlags & DBFLAG_Vacuum)==0
142122 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
142130 pDb->pSchema->enc = ENC(db);
142132 if( pDb->pSchema->cache_size==0 ){
142134 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
142136 pDb->pSchema->cache_size = size;
142138 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
142140 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
142146 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
142149 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
142150 if( pDb->pSchema->file_format==0 ){
142151 pDb->pSchema->file_format = 1;
142153 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
142164 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
142165 db->flags &= ~(u64)SQLITE_LegacyFileFmt;
142170 assert( db->init.busy );
142171 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
142176 db->aDb[iDb].zDbSName, zSchemaTabName);
142180 xAuth = db->xAuth;
142181 db->xAuth = 0;
142185 db->xAuth = xAuth;
142196 assert( pDb == &(db->aDb[iDb]) );
142197 if( db->mallocFailed ){
142200 pDb = &db->aDb[iDb];
142202 if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){
142223 sqlite3BtreeCommit(pDb->pBt);
142225 sqlite3BtreeLeave(pDb->pBt);
142234 db->init.busy = 0;
142239 ** Initialize all database files - the main database file, the file
142249 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
142251 assert( sqlite3_mutex_held(db->mutex) );
142252 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
142253 assert( db->init.busy==0 );
142255 assert( db->nDb>0 );
142262 for(i=db->nDb-1; i>0; i--){
142263 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
142276 ** This routine is a no-op if the database schema is already initialized.
142281 sqlite3 *db = pParse->db;
142282 assert( sqlite3_mutex_held(db->mutex) );
142283 if( !db->init.busy ){
142284 rc = sqlite3Init(db, &pParse->zErrMsg);
142286 pParse->rc = rc;
142287 pParse->nErr++;
142288 }else if( db->noSharedCache ){
142289 db->mDbFlags |= DBFLAG_SchemaKnownOk;
142298 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
142299 ** make no changes to pParse->rc.
142302 sqlite3 *db = pParse->db;
142307 assert( pParse->checkSchema );
142308 assert( sqlite3_mutex_held(db->mutex) );
142309 for(iDb=0; iDb<db->nDb; iDb++){
142311 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
142314 /* If there is not already a read-only (or read-write) transaction opened
142315 ** on the b-tree database, open one now. If a transaction is opened, it
142316 ** will be closed immediately after reading the meta-value. */
142321 pParse->rc = SQLITE_NOMEM;
142328 ** value stored as part of the in-memory schema representation,
142332 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
142333 if( DbHasProperty(db, iDb, DB_SchemaLoaded) ) pParse->rc = SQLITE_SCHEMA;
142346 ** which database file in db->aDb[] the schema refers to.
142352 int i = -32768;
142354 /* If pSchema is NULL, then return -32768. This happens when code in
142356 ** created by a sub-select). In this case the return value of this
142359 ** We return -32768 instead of the more usual -1 simply because using
142360 ** -32768 as the incorrect index into db->aDb[] is much
142361 ** more likely to cause a segfault than -1 (of course there are assert()
142363 ** -32768 will still fit into a 16-bit signed integer.
142365 assert( sqlite3_mutex_held(db->mutex) );
142368 assert( i<db->nDb );
142369 if( db->aDb[i].pSchema==pSchema ){
142373 assert( i>=0 && i<db->nDb );
142382 sqlite3 *db = pParse->db;
142384 assert( db->pParse==pParse );
142385 assert( pParse->nested==0 );
142387 if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock);
142389 while( pParse->pCleanup ){
142390 ParseCleanup *pCleanup = pParse->pCleanup;
142391 pParse->pCleanup = pCleanup->pNext;
142392 pCleanup->xCleanup(db, pCleanup->pPtr);
142395 if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel);
142396 if( pParse->pConstExpr ){
142397 sqlite3ExprListDelete(db, pParse->pConstExpr);
142399 assert( db->lookaside.bDisable >= pParse->disableLookaside );
142400 db->lookaside.bDisable -= pParse->disableLookaside;
142401 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
142402 assert( pParse->db->pParse==pParse );
142403 db->pParse = pParse->pOuterParse;
142414 ** common cleanups, we save a single NULL-pointer comparison in
142419 ** pParse->earlyCleanup flag is set in that case. Calling code show verify
142421 ** use-after-free errors following an OOM. The preferred way to do this is
142424 ** testcase( pParse->earlyCleanup );
142442 sqlite3OomFault(pParse->db);
142444 pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
142447 pCleanup->pNext = pParse->pCleanup;
142448 pParse->pCleanup = pCleanup;
142449 pCleanup->pPtr = pPtr;
142450 pCleanup->xCleanup = xCleanup;
142452 xCleanup(pParse->db, pPtr);
142455 pParse->earlyCleanup = 1;
142473 assert( db->pParse!=pParse );
142474 pParse->pOuterParse = db->pParse;
142475 db->pParse = pParse;
142476 pParse->db = db;
142477 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
142489 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
142493 const char *zSql, /* UTF-8 encoded SQL statement. */
142507 sParse.pOuterParse = db->pParse;
142508 db->pParse = &sParse;
142517 if( db->mallocFailed ){
142519 db->errCode = rc = SQLITE_NOMEM;
142522 assert( sqlite3_mutex_held(db->mutex) );
142524 /* For a long-term use prepared statement avoid the use of
142535 ** some other database connection is holding a write-lock, which in
142556 if( !db->noSharedCache ){
142557 for(i=0; i<db->nDb; i++) {
142558 Btree *pBt = db->aDb[i].pBt;
142563 const char *zDb = db->aDb[i].zDbSName;
142565 testcase( db->flags & SQLITE_ReadUncommit );
142573 if( db->pDisconnect ) sqlite3VtabUnlockList(db);
142576 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
142578 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
142589 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
142603 if( db->init.busy==0 ){
142604 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
142606 if( db->mallocFailed ){
142611 if( sParse.checkSchema && db->init.busy==0 ){
142636 sParse.pTriggerPrg = pT->pNext;
142647 const char *zSql, /* UTF-8 encoded SQL statement. */
142664 sqlite3_mutex_enter(db->mutex);
142672 if( rc==SQLITE_OK || db->mallocFailed ) break;
142674 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
142677 assert( (rc&db->errMask)==rc );
142678 db->busyHandler.nBusy = 0;
142679 sqlite3_mutex_leave(db->mutex);
142700 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
142704 assert( sqlite3_mutex_held(db->mutex) );
142706 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
142734 const char *zSql, /* UTF-8 encoded SQL statement. */
142746 const char *zSql, /* UTF-8 encoded SQL statement. */
142752 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
142764 const char *zSql, /* UTF-8 encoded SQL statement. */
142771 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
142788 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
142792 const void *zSql, /* UTF-16 encoded SQL statement. */
142798 /* This function currently works by first transforming the UTF-16
142799 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
142819 sqlite3_mutex_enter(db->mutex);
142822 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
142827 ** equivalent pointer into the UTF-16 string by counting the unicode
142829 ** the same number of characters into the UTF-16 string.
142831 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
142836 sqlite3_mutex_leave(db->mutex);
142850 const void *zSql, /* UTF-16 encoded SQL statement. */
142862 const void *zSql, /* UTF-16 encoded SQL statement. */
142874 const void *zSql, /* UTF-16 encoded SQL statement. */
142925 ** The aDefer[] array is used by the sorter-references optimization. For
142935 ** When the sorter-reference optimization is used, there is one entry in the
142944 int regReturn; /* Register holding block-output return address */
142945 int labelBkOut; /* Start label for the block-output subroutine */
142976 Select *pPrior = p->pPrior;
142977 sqlite3ExprListDelete(db, p->pEList);
142978 sqlite3SrcListDelete(db, p->pSrc);
142979 sqlite3ExprDelete(db, p->pWhere);
142980 sqlite3ExprListDelete(db, p->pGroupBy);
142981 sqlite3ExprDelete(db, p->pHaving);
142982 sqlite3ExprListDelete(db, p->pOrderBy);
142983 sqlite3ExprDelete(db, p->pLimit);
142984 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
142986 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
142987 sqlite3WindowListDelete(db, p->pWinDefn);
142989 while( p->pWin ){
142990 assert( p->pWin->ppThis==&p->pWin );
142991 sqlite3WindowUnlinkFromSelect(p->pWin);
143004 pDest->eDest = (u8)eDest;
143005 pDest->iSDParm = iParm;
143006 pDest->iSDParm2 = 0;
143007 pDest->zAffSdst = 0;
143008 pDest->iSdst = 0;
143009 pDest->nSdst = 0;
143020 SrcList *pSrc, /* the FROM clause -- which tables to scan */
143030 pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
143032 assert( pParse->db->mallocFailed );
143037 sqlite3Expr(pParse->db,TK_ASTERISK,0));
143039 pNew->pEList = pEList;
143040 pNew->op = TK_SELECT;
143041 pNew->selFlags = selFlags;
143042 pNew->iLimit = 0;
143043 pNew->iOffset = 0;
143044 pNew->selId = ++pParse->nSelect;
143045 pNew->addrOpenEphm[0] = -1;
143046 pNew->addrOpenEphm[1] = -1;
143047 pNew->nSelectRow = 0;
143048 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
143049 pNew->pSrc = pSrc;
143050 pNew->pWhere = pWhere;
143051 pNew->pGroupBy = pGroupBy;
143052 pNew->pHaving = pHaving;
143053 pNew->pOrderBy = pOrderBy;
143054 pNew->pPrior = 0;
143055 pNew->pNext = 0;
143056 pNew->pLimit = pLimit;
143057 pNew->pWith = 0;
143059 pNew->pWin = 0;
143060 pNew->pWinDefn = 0;
143062 if( pParse->db->mallocFailed ) {
143063 clearSelect(pParse->db, pNew, pNew!=&standin);
143066 assert( pNew->pSrc!=0 || pParse->nErr>0 );
143083 ** Return a pointer to the right-most SELECT statement in a compound.
143086 while( p->pNext ) p = p->pNext;
143111 ** ------- ----- ----- ------------
143112 ** CROSS - - JT_CROSS
143113 ** INNER - - JT_INNER
143114 ** LEFT - - JT_LEFT|JT_OUTER
143115 ** LEFT OUTER - JT_LEFT|JT_OUTER
143116 ** RIGHT - - JT_RIGHT|JT_OUTER
143117 ** RIGHT OUTER - JT_RIGHT|JT_OUTER
143118 ** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER
143119 ** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER
143120 ** NATURAL INNER - JT_NATURAL|JT_INNER
143121 ** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER
143123 ** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER
143125 ** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT
143129 ** of other non-standard and in many cases nonsensical join types.
143134 ** INNER CROSS JOIN -> same as JOIN
143135 ** NATURAL CROSS JOIN -> same as NATURAL JOIN
143136 ** OUTER LEFT JOIN -> same as LEFT JOIN
143137 ** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN
143138 ** LEFT RIGHT JOIN -> same as FULL JOIN
143139 ** RIGHT OUTER FULL JOIN -> same as FULL JOIN
143140 ** CROSS CROSS CROSS JOIN -> same as JOIN
143179 if( p->n==aKeyword[j].nChar
143180 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
143208 ** Return the index of a column in a table. Return -1 if the column
143215 for(pCol=pTab->aCol, i=0; i<pTab->nCol; pCol++, i++){
143216 if( pCol->hName==h && sqlite3StrICmp(pCol->zCnName, zCol)==0 ) return i;
143218 return -1;
143226 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
143227 if( pItem->fg.isNestedFrom ){
143229 assert( pItem->pSelect!=0 );
143230 pResults = pItem->pSelect->pEList;
143232 assert( iCol>=0 && iCol<pResults->nExpr );
143233 pResults->a[iCol].fg.bUsed = 1;
143239 ** table that has a column named zCol. The search is left-to-right.
143249 int iStart, /* First member of pSrc->a[] to check */
143250 int iEnd, /* Last member of pSrc->a[] to check */
143252 int *piTab, /* Write index of pSrc->a[] here */
143253 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
143259 assert( iEnd<pSrc->nSrc );
143264 iCol = sqlite3ColumnIndex(pSrc->a[i].pTab, zCol);
143266 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0)
143269 sqlite3SrcItemColumnUsed(&pSrc->a[i], iCol);
143311 p->w.iJoin = iTable;
143312 if( p->op==TK_FUNCTION ){
143314 if( p->x.pList ){
143316 for(i=0; i<p->x.pList->nExpr; i++){
143317 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable, joinFlag);
143321 sqlite3SetJoinExpr(p->pLeft, iTable, joinFlag);
143322 p = p->pRight;
143342 if( iTable<0 || (ExprHasProperty(p, EP_OuterON) && p->w.iJoin==iTable) ){
143346 if( p->op==TK_COLUMN && p->iTable==iTable && !nullable ){
143349 if( p->op==TK_FUNCTION ){
143351 assert( p->pLeft==0 );
143352 if( p->x.pList ){
143354 for(i=0; i<p->x.pList->nExpr; i++){
143355 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable, nullable);
143359 unsetJoinExpr(p->pLeft, iTable, nullable);
143360 p = p->pRight;
143377 ** The left most table is the first entry in Select.pSrc. The right-most
143391 pSrc = p->pSrc;
143392 pLeft = &pSrc->a[0];
143394 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
143395 Table *pRightTab = pRight->pTab;
143398 if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
143399 joinType = (pRight->fg.jointype & JT_OUTER)!=0 ? EP_OuterON : EP_InnerON;
143404 if( pRight->fg.jointype & JT_NATURAL ){
143406 if( pRight->fg.isUsing || pRight->u3.pOn ){
143411 for(j=0; j<pRightTab->nCol; j++){
143414 if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
143415 zName = pRightTab->aCol[j].zCnName;
143419 assert( pUsing->nId>0 );
143420 assert( pUsing->a[pUsing->nId-1].zName==0 );
143421 pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(pParse->db, zName);
143426 pRight->fg.isUsing = 1;
143427 pRight->fg.isSynthUsing = 1;
143428 pRight->u3.pUsing = pUsing;
143430 if( pParse->nErr ) return 1;
143440 if( pRight->fg.isUsing ){
143441 IdList *pList = pRight->u3.pUsing;
143442 sqlite3 *db = pParse->db;
143444 for(j=0; j<pList->nId; j++){
143453 zName = pList->a[j].zName;
143457 pRight->fg.isSynthUsing)==0
143459 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
143464 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
143465 if( (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
143468 ** contains the zName column, then this branch is a no-op.
143475 ** non-USING references to zName on the left of an INNER or LEFT
143482 pRight->fg.isSynthUsing)!=0 ){
143483 if( pSrc->a[iLeft].fg.isUsing==0
143484 || sqlite3IdListIndex(pSrc->a[iLeft].u3.pUsing, zName)<0
143492 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
143507 pEq->w.iJoin = pE2->iTable;
143509 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pEq);
143516 else if( pRight->u3.pOn ){
143517 sqlite3SetJoinExpr(pRight->u3.pOn, pRight->iCursor, joinType);
143518 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn);
143519 pRight->u3.pOn = 0;
143520 pRight->fg.isOn = 1;
143549 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
143550 0, pInfo->ecelFlags);
143552 if( pInfo->pExtra ){
143553 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
143554 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
143572 int nOBSat = pSort->nOBSat;
143573 Vdbe *v = pParse->pVdbe;
143574 int regOut = ++pParse->nMem;
143575 if( pSort->pDeferredRowLoad ){
143576 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
143578 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
143584 ** through regData+nData-1 onto the sorter.
143595 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
143596 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
143597 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
143601 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
143624 pSort->addrPush = sqlite3VdbeCurrentAddr(v);
143629 regBase = regData - nPrefixReg;
143631 regBase = pParse->nMem + 1;
143632 pParse->nMem += nBase;
143634 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
143635 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
143636 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
143637 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
143640 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
143654 regPrevKey = pParse->nMem+1;
143655 pParse->nMem += pSort->nOBSat;
143656 nKey = nExpr - pSort->nOBSat + bSeq;
143660 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
143663 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
143664 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
143665 if( pParse->db->mallocFailed ) return;
143666 pOp->p2 = nKey + nData;
143667 pKI = pOp->p4.pKeyInfo;
143668 memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
143669 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
143670 testcase( pKI->nAllField > pKI->nKeyField+2 );
143671 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
143672 pKI->nAllField-pKI->nKeyField-1);
143676 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
143677 pSort->regReturn = ++pParse->nMem;
143678 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
143679 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
143681 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
143685 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
143699 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
143704 int iCsr = pSort->iECursor;
143709 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
143716 if( pSort->sortFlags & SORTFLAG_UseSorter ){
143721 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
143722 regBase+nOBSat, nBase-nOBSat);
143725 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
143728 pSort->addrPushEnd = sqlite3VdbeCurrentAddr(v)-1;
143798 int nResultCol = pEList->nExpr;
143799 Vdbe *v = pParse->pVdbe;
143808 iRet = regPrev = pParse->nMem+1;
143809 pParse->nMem += nResultCol;
143813 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
143814 if( i<nResultCol-1 ){
143821 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
143824 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
143825 sqlite3VdbeAddOp3(v, OP_Copy, regElem, regPrev, nResultCol-1);
143860 ** No adjustments necessary. This function is a no-op.
143879 if( pParse->nErr==0
143882 Vdbe *v = pParse->pVdbe;
143884 if( sqlite3VdbeGetOp(v, iOpenEphAddr+1)->opcode==OP_Explain ){
143893 pOp->opcode = OP_Null;
143894 pOp->p1 = 1;
143895 pOp->p2 = iVal;
143902 ** This function is called as part of inner-loop generation for a SELECT
143904 ** determines the expressions, if any, that the sorter-reference
143905 ** optimization should be used for. The sorter-reference optimization
143918 ** for which the sorter-reference optimization should be enabled.
143919 ** Additionally, the pSort->aDefer[] array is populated with entries
143934 for(i=0; i<pEList->nExpr; i++){
143935 struct ExprList_item *pItem = &pEList->a[i];
143936 if( pItem->u.x.iOrderByCol==0 ){
143937 Expr *pExpr = pItem->pExpr;
143939 if( pExpr->op==TK_COLUMN
143940 && pExpr->iColumn>=0
143942 && (pTab = pExpr->y.pTab)!=0
143944 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)!=0
143948 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
143951 if( nDefer==ArraySize(pSort->aDefer) ){
143959 nKey = pPk->nKeyCol;
143964 pNew->iTable = pExpr->iTable;
143966 pNew->y.pTab = pExpr->y.pTab;
143967 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
143971 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
143972 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
143973 pSort->aDefer[nDefer].nKey = nKey;
143977 pItem->fg.bSorterRef = 1;
143981 pSort->nDefer = (u8)nDefer;
143990 ** If srcTab is negative, then the p->pEList expressions
143992 ** zero or more, then data is pulled from srcTab and p->pEList is used only
143998 int srcTab, /* Pull data from this table if non-negative */
144005 Vdbe *v = pParse->pVdbe;
144008 int eDest = pDest->eDest; /* How to dispose of results */
144009 int iParm = pDest->iSDParm; /* First argument to disposal method */
144017 ** values for any expressions that are also part of the sort-key are omitted
144023 assert( p->pEList!=0 );
144024 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
144025 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
144028 codeOffset(v, p->iOffset, iContinue);
144033 nResultCol = p->pEList->nExpr;
144035 if( pDest->iSdst==0 ){
144037 nPrefixReg = pSort->pOrderBy->nExpr;
144038 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
144039 pParse->nMem += nPrefixReg;
144041 pDest->iSdst = pParse->nMem+1;
144042 pParse->nMem += nResultCol;
144043 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
144045 ** on the right-hand side of an INSERT contains more result columns than
144049 pParse->nMem += nResultCol;
144051 pDest->nSdst = nResultCol;
144052 regOrig = regResult = pDest->iSdst;
144056 VdbeComment((v, "%s", p->pEList->a[i].zEName));
144073 /* For each expression in p->pEList that is a copy of an expression in
144074 ** the ORDER BY clause (pSort->pOrderBy), set the associated
144076 ** expression within the sort-key that pushOntoSorter() will generate.
144077 ** This allows the p->pEList field to be omitted from the sorted record,
144081 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
144083 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
144084 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
144088 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
144089 if( pExtra && pParse->db->mallocFailed==0 ){
144095 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
144096 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
144097 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
144098 pParse->nMem += pExtra->nExpr;
144104 pEList = p->pEList;
144105 for(i=0; i<pEList->nExpr; i++){
144106 if( pEList->a[i].u.x.iOrderByCol>0
144108 || pEList->a[i].fg.bSorterRef
144111 nResultCol--;
144130 if( pExtra ) nResultCol += pExtra->nExpr;
144132 if( p->iLimit
144138 pSort->pDeferredRowLoad = &sRowLoadInfo;
144150 int eType = pDistinct->eTnctType;
144151 int iTab = pDistinct->tabTnct;
144152 assert( nResultCol==p->pEList->nExpr );
144153 iTab = codeDistinct(pParse, eType, iTab, iContinue, p->pEList, regResult);
144154 fixDistinctOpenEph(pParse, eType, iTab, pDistinct->addrTnct);
144156 codeOffset(v, p->iOffset, iContinue);
144197 /* A destination of SRT_Table and a non-zero iSDParm2 parameter means
144200 ** This does not affect operation in any way - it just allows MakeRecord
144202 if( eDest==SRT_Table && pDest->iSDParm2 ){
144239 int i2 = pDest->iSDParm2;
144248 regResult+(i2<0), nResultCol-(i2<0), r1);
144273 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
144275 r1, pDest->zAffSdst, nResultCol);
144297 assert( nResultCol<=pDest->nSdst );
144301 assert( nResultCol==pDest->nSdst );
144309 case SRT_Coroutine: /* Send data to a co-routine */
144317 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
144326 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
144327 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
144328 ** pSO->nExpr columns, then make sure all keys are unique by adding a
144337 pSO = pDest->pOrderBy;
144339 nKey = pSO->nExpr;
144358 regResult + pSO->a[i].u.x.iOrderByCol - 1,
144376 ** user-defined functions that have side effects. We do not care
144390 if( pSort==0 && p->iLimit ){
144391 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
144400 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
144403 p->aSortFlags = (u8*)&p->aColl[N+X];
144404 p->nKeyField = (u16)N;
144405 p->nAllField = (u16)(N+X);
144406 p->enc = ENC(db);
144407 p->db = db;
144408 p->nRef = 1;
144421 assert( p->db!=0 );
144422 assert( p->nRef>0 );
144423 p->nRef--;
144424 if( p->nRef==0 ) sqlite3DbNNFreeNN(p->db, p);
144433 assert( p->nRef>0 );
144434 p->nRef++;
144446 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
144472 sqlite3 *db = pParse->db;
144475 nExpr = pList->nExpr;
144476 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
144479 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
144480 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
144481 pInfo->aSortFlags[i-iStart] = pItem->fg.sortFlags;
144504 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
144507 ** "USE TEMP B-TREE FOR xxx"
144513 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
144517 ** Assign expression b to lvalue a. A second, no-op, version of this macro
144526 /* No-op versions of the explainXXX() functions and macros. */
144533 ** If the inner loop was generated using a non-null pOrderBy argument,
144545 Vdbe *v = pParse->pVdbe; /* The prepared statement */
144546 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
144551 ExprList *pOrderBy = pSort->pOrderBy;
144552 int eDest = pDest->eDest;
144553 int iParm = pDest->iSDParm;
144562 struct ExprList_item *aOutEx = p->pEList->a;
144567 nKey = pOrderBy->nExpr - pSort->nOBSat;
144568 if( pSort->nOBSat==0 || nKey==1 ){
144570 "USE TEMP B-TREE FOR %sORDER BY", pSort->nOBSat?"LAST TERM OF ":""
144574 "USE TEMP B-TREE FOR LAST %d TERMS OF ORDER BY", nKey
144577 sqlite3VdbeScanStatusRange(v, addrExplain,pSort->addrPush,pSort->addrPushEnd);
144578 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, pSort->addrPush);
144582 if( pSort->labelBkOut ){
144583 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
144585 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
144589 /* Open any cursors needed for sorter-reference expressions */
144590 for(i=0; i<pSort->nDefer; i++){
144591 Table *pTab = pSort->aDefer[i].pTab;
144592 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
144593 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
144594 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
144598 iTab = pSort->iECursor;
144600 if( eDest==SRT_Mem && p->iOffset ){
144601 sqlite3VdbeAddOp2(v, OP_Null, 0, pDest->iSdst);
144604 regRow = pDest->iSdst;
144614 if( pSort->sortFlags & SORTFLAG_UseSorter ){
144615 int regSortOut = ++pParse->nMem;
144616 iSortTab = pParse->nTab++;
144617 if( pSort->labelBkOut ){
144625 assert( p->iLimit==0 && p->iOffset==0 );
144630 codeOffset(v, p->iOffset, addrContinue);
144633 if( p->iOffset>0 ){
144634 sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1);
144637 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
144644 if( pSort->nDefer ){
144648 for(i=0; i<pSort->nDefer; i++){
144649 int iCsr = pSort->aDefer[i].iCsr;
144650 Table *pTab = pSort->aDefer[i].pTab;
144651 int nKey = pSort->aDefer[i].nKey;
144661 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
144674 for(i=nColumn-1; i>=0; i--){
144683 iRead = aOutEx[i].u.x.iOrderByCol-1;
144685 iRead = iCol--;
144691 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
144703 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
144705 pDest->zAffSdst, nColumn);
144715 int i2 = pDest->iSDParm2;
144717 sqlite3VdbeAddOp3(v, OP_MakeRecord,regRow+(i2<0),nColumn-(i2<0),r1);
144730 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
144732 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
144748 if( pSort->sortFlags & SORTFLAG_UseSorter ){
144753 sqlite3VdbeScanStatusRange(v, addrExplain, sqlite3VdbeCurrentAddr(v)-1, -1);
144754 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
144766 ** result-set expression in all of the following SELECT statements is
144777 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
144804 assert( pNC->pSrcList!=0 );
144805 switch( pExpr->op ){
144813 int iCol = pExpr->iColumn; /* Index of column in pTab */
144815 SrcList *pTabList = pNC->pSrcList;
144816 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
144817 if( j<pTabList->nSrc ){
144818 pTab = pTabList->a[j].pTab;
144819 pS = pTabList->a[j].pSelect;
144821 pNC = pNC->pNext;
144836 ** sub-select. In this case, set the column type to NULL, even
144846 assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab );
144848 /* The "table" is actually a sub-select or a view in the FROM clause
144850 ** data for the result-set column of the sub-select.
144852 if( iCol<pS->pEList->nExpr
144856 ** rowid of the sub-select or view. This expression is legal (see
144857 ** test case misc2.2.2) - it always evaluates to NULL.
144860 Expr *p = pS->pEList->a[iCol].pExpr;
144861 sNC.pSrcList = pS->pSrc;
144863 sNC.pParse = pNC->pParse;
144870 if( iCol<0 ) iCol = pTab->iPKey;
144871 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
144876 zOrigCol = pTab->aCol[iCol].zCnName;
144877 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
144879 zOrigTab = pTab->zName;
144880 if( pNC->pParse && pTab->pSchema ){
144881 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
144882 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
144885 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
144889 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
144897 /* The expression is a sub-select. Return the declaration type and
144905 pS = pExpr->x.pSelect;
144906 p = pS->pEList->a[0].pExpr;
144907 sNC.pSrcList = pS->pSrc;
144909 sNC.pParse = pNC->pParse;
144937 Vdbe *v = pParse->pVdbe;
144943 for(i=0; i<pEList->nExpr; i++){
144944 Expr *p = pEList->a[i].pExpr;
144952 /* The vdbe must make its own copy of the column-type and other
145002 Vdbe *v = pParse->pVdbe;
145007 sqlite3 *db = pParse->db;
145011 if( pParse->colNamesSet ) return;
145012 /* Column names are determined by the left-most term of a compound select */
145013 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
145015 pTabList = pSelect->pSrc;
145016 pEList = pSelect->pEList;
145019 pParse->colNamesSet = 1;
145020 fullName = (db->flags & SQLITE_FullColNames)!=0;
145021 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
145022 sqlite3VdbeSetNumCols(v, pEList->nExpr);
145023 for(i=0; i<pEList->nExpr; i++){
145024 Expr *p = pEList->a[i].pExpr;
145027 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
145028 assert( p->op!=TK_COLUMN
145029 || (ExprUseYTab(p) && p->y.pTab!=0) ); /* Covering idx not yet coded */
145030 if( pEList->a[i].zEName && pEList->a[i].fg.eEName==ENAME_NAME ){
145032 char *zName = pEList->a[i].zEName;
145034 }else if( srcName && p->op==TK_COLUMN ){
145036 int iCol = p->iColumn;
145037 pTab = p->y.pTab;
145039 if( iCol<0 ) iCol = pTab->iPKey;
145040 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
145044 zCol = pTab->aCol[iCol].zCnName;
145048 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
145054 const char *z = pEList->a[i].zEName;
145090 sqlite3 *db = pParse->db; /* Database connection */
145102 nCol = pEList->nExpr;
145114 for(i=0, pCol=aCol; i<nCol && !pParse->nErr; i++, pCol++){
145115 struct ExprList_item *pX = &pEList->a[i];
145119 if( (zName = pX->zEName)!=0 && pX->fg.eEName==ENAME_NAME ){
145122 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pX->pExpr);
145123 while( ALWAYS(pColExpr!=0) && pColExpr->op==TK_DOT ){
145124 pColExpr = pColExpr->pRight;
145127 if( pColExpr->op==TK_COLUMN
145129 && ALWAYS( pColExpr->y.pTab!=0 )
145132 int iCol = pColExpr->iColumn;
145133 pTab = pColExpr->y.pTab;
145134 if( iCol<0 ) iCol = pTab->iPKey;
145135 zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid";
145136 }else if( pColExpr->op==TK_ID ){
145138 zName = pColExpr->u.zToken;
145141 assert( zName==pX->zEName ); /* pointer comparison intended */
145155 if( pCollide->fg.bUsingTerm ){
145156 pCol->colFlags |= COLFLAG_NOEXPAND;
145160 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
145169 pCol->zCnName = zName;
145170 pCol->hName = sqlite3StrIHash(zName);
145171 if( pX->fg.bNoExpand ){
145172 pCol->colFlags |= COLFLAG_NOEXPAND;
145180 if( pParse->nErr ){
145187 return pParse->rc;
145209 sqlite3 *db = pParse->db;
145218 assert( (pSelect->selFlags & SF_Resolved)!=0 );
145219 assert( pTab->nCol==pSelect->pEList->nExpr || pParse->nErr>0 );
145221 if( db->mallocFailed || IN_RENAME_OBJECT ) return;
145222 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
145223 a = pSelect->pEList->a;
145225 sNC.pSrcList = pSelect->pSrc;
145226 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
145231 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
145233 /* pCol->szEst = ... // Column size est for SELECT tables never used */
145234 pCol->affinity = sqlite3ExprAffinity(p);
145235 while( pCol->affinity<=SQLITE_AFF_NONE && pS2->pNext!=0 ){
145236 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
145237 pS2 = pS2->pNext;
145238 pCol->affinity = sqlite3ExprAffinity(pS2->pEList->a[i].pExpr);
145240 if( pCol->affinity<=SQLITE_AFF_NONE ){
145241 pCol->affinity = aff;
145243 if( pCol->affinity>=SQLITE_AFF_TEXT && (pS2->pNext || pS2!=pSelect) ){
145244 for(pS2=pS2->pNext; pS2; pS2=pS2->pNext){
145245 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
145247 if( pCol->affinity==SQLITE_AFF_TEXT && (m&0x01)!=0 ){
145248 pCol->affinity = SQLITE_AFF_BLOB;
145250 if( pCol->affinity>=SQLITE_AFF_NUMERIC && (m&0x02)!=0 ){
145251 pCol->affinity = SQLITE_AFF_BLOB;
145253 if( pCol->affinity>=SQLITE_AFF_NUMERIC && p->op==TK_CAST ){
145254 pCol->affinity = SQLITE_AFF_FLEXNUM;
145258 if( zType==0 || pCol->affinity!=sqlite3AffinityType(zType, 0) ){
145259 if( pCol->affinity==SQLITE_AFF_NUMERIC
145260 || pCol->affinity==SQLITE_AFF_FLEXNUM
145266 if( sqlite3StdTypeAffinity[j]==pCol->affinity ){
145275 n = sqlite3Strlen30(pCol->zCnName);
145276 pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+k+2);
145277 pCol->colFlags &= ~(COLFLAG_HASTYPE|COLFLAG_HASCOLL);
145278 if( pCol->zCnName ){
145279 memcpy(&pCol->zCnName[n+1], zType, k+1);
145280 pCol->colFlags |= COLFLAG_HASTYPE;
145285 assert( pTab->pIndex==0 );
145286 sqlite3ColumnSetColl(db, pCol, pColl->zName);
145289 pTab->szTabRow = 1; /* Any non-zero value works */
145298 sqlite3 *db = pParse->db;
145301 savedFlags = db->flags;
145302 db->flags &= ~(u64)SQLITE_FullColNames;
145303 db->flags |= SQLITE_ShortColNames;
145305 db->flags = savedFlags;
145306 if( pParse->nErr ) return 0;
145307 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
145312 pTab->nTabRef = 1;
145313 pTab->zName = 0;
145314 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
145315 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
145317 pTab->iPKey = -1;
145318 if( db->mallocFailed ){
145330 if( pParse->pVdbe ){
145331 return pParse->pVdbe;
145333 if( pParse->pToplevel==0
145334 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
145336 pParse->okConstFactor = 1;
145344 ** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
145352 ** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
145360 ** Only if pLimit->pLeft!=0 do the limit registers get
145370 Expr *pLimit = p->pLimit;
145372 if( p->iLimit ) return;
145375 ** "LIMIT -1" always shows all rows. There is some
145381 assert( pLimit->op==TK_LIMIT );
145382 assert( pLimit->pLeft!=0 );
145383 p->iLimit = iLimit = ++pParse->nMem;
145386 if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
145391 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
145392 p->nSelectRow = sqlite3LogEst((u64)n);
145393 p->selFlags |= SF_FixedLimit;
145396 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
145401 if( pLimit->pRight ){
145402 p->iOffset = iOffset = ++pParse->nMem;
145403 pParse->nMem++; /* Allocate an extra register for limit+offset */
145404 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
145415 ** Return the appropriate collating sequence for the iCol-th column of
145416 ** the result set for the compound-select statement "p". Return NULL if
145420 ** left-most term of the select that has a collating sequence.
145424 if( p->pPrior ){
145425 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
145430 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
145433 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
145434 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
145449 ExprList *pOrderBy = p->pOrderBy;
145450 int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
145451 sqlite3 *db = pParse->db;
145456 struct ExprList_item *pItem = &pOrderBy->a[i];
145457 Expr *pTerm = pItem->pExpr;
145460 if( pTerm->flags & EP_Collate ){
145463 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
145464 if( pColl==0 ) pColl = db->pDfltColl;
145465 pOrderBy->a[i].pExpr =
145466 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
145469 pRet->aColl[i] = pColl;
145470 pRet->aSortFlags[i] = pOrderBy->a[i].fg.sortFlags;
145482 ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
145484 ** p->pPrior p
145487 ** There is exactly one reference to the recursive-table in the FROM clause
145488 ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
145490 ** The setup-query runs once to generate an initial set of rows that go
145494 ** recursive-table for a recursive-query run. The output of the recursive-query
145519 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
145520 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
145521 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
145523 Select *pFirstRec; /* Left-most recursive term */
145539 if( p->pWin ){
145550 p->nSelectRow = 320; /* 4 billion rows */
145552 pLimit = p->pLimit;
145553 regLimit = p->iLimit;
145554 regOffset = p->iOffset;
145555 p->pLimit = 0;
145556 p->iLimit = p->iOffset = 0;
145557 pOrderBy = p->pOrderBy;
145560 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
145561 if( pSrc->a[i].fg.isRecursive ){
145562 iCurrent = pSrc->a[i].iCursor;
145570 iQueue = pParse->nTab++;
145571 if( p->op==TK_UNION ){
145573 iDistinct = pParse->nTab++;
145580 regCurrent = ++pParse->nMem;
145584 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
145592 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
145593 p->selFlags |= SF_UsesEphemeral;
145597 p->pOrderBy = 0;
145603 ** iDistinct table. pFirstRec is left pointing to the left-most
145606 for(pFirstRec=p; ALWAYS(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){
145607 if( pFirstRec->selFlags & SF_Aggregate ){
145611 pFirstRec->op = TK_ALL;
145612 if( (pFirstRec->pPrior->selFlags & SF_Recursive)==0 ) break;
145615 /* Store the results of the setup-query in Queue. */
145616 pSetup = pFirstRec->pPrior;
145617 pSetup->pNext = 0;
145620 pSetup->pNext = p;
145629 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
145647 ** the value for the recursive-table. Store the results in the Queue.
145649 pFirstRec->pPrior = 0;
145652 assert( pFirstRec->pPrior==0 );
145653 pFirstRec->pPrior = pSetup;
145660 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
145661 p->pOrderBy = pOrderBy;
145662 p->pLimit = pLimit;
145670 Select *p, /* The right-most of SELECTs to be coded */
145675 ** Handle the special case of a compound-select that originates from a
145688 ** Since the limit is exactly 1, we only need to evaluate the left-most VALUES.
145692 Select *p, /* The right-most of SELECTs to be coded */
145697 int bShowAll = p->pLimit==0;
145698 assert( p->selFlags & SF_MultiValue );
145700 assert( p->selFlags & SF_Values );
145701 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
145702 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
145704 if( p->pWin ) return -1;
145706 if( p->pPrior==0 ) break;
145707 assert( p->pPrior->pNext==p );
145708 p = p->pPrior;
145714 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
145716 p->nSelectRow = nRow;
145717 p = p->pNext;
145728 while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; }
145737 ** "p" points to the right-most of the two queries. the query on the
145738 ** left is p->pPrior. The left query could also be a compound query
145744 ** Example 1: Consider a three-way compound SQL statement.
145752 ** `-----> SELECT b FROM t2
145754 ** `------> SELECT a FROM t1
145758 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
145765 Select *p, /* The right-most of SELECTs to be coded */
145776 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
145778 assert( p && p->pPrior ); /* Calling function guarantees this much */
145779 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
145780 assert( p->selFlags & SF_Compound );
145781 db = pParse->db;
145782 pPrior = p->pPrior;
145784 assert( pPrior->pOrderBy==0 );
145785 assert( pPrior->pLimit==0 );
145793 assert( p->pEList );
145794 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
145798 /* Special handling for a compound-select that originates as a VALUES clause.
145800 if( p->selFlags & SF_MultiValue ){
145809 assert( p->pEList && pPrior->pEList );
145810 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
145813 if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){
145820 if( p->pOrderBy ){
145825 if( pPrior->pPrior==0 ){
145827 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
145833 switch( p->op ){
145837 assert( !pPrior->pLimit );
145838 pPrior->iLimit = p->iLimit;
145839 pPrior->iOffset = p->iOffset;
145840 pPrior->pLimit = p->pLimit;
145843 pPrior->pLimit = 0;
145847 p->pPrior = 0;
145848 p->iLimit = pPrior->iLimit;
145849 p->iOffset = pPrior->iOffset;
145850 if( p->iLimit ){
145851 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
145853 if( p->iOffset ){
145855 p->iLimit, p->iOffset+1, p->iOffset);
145862 pDelete = p->pPrior;
145863 p->pPrior = pPrior;
145864 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
145865 if( p->pLimit
145866 && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit)
145867 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
145869 p->nSelectRow = sqlite3LogEst((u64)nLimit);
145881 Expr *pLimit; /* Saved values of p->nLimit */
145885 testcase( p->op==TK_EXCEPT );
145886 testcase( p->op==TK_UNION );
145892 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
145898 unionTab = pParse->nTab++;
145899 assert( p->pOrderBy==0 );
145901 assert( p->addrOpenEphm[0] == -1 );
145902 p->addrOpenEphm[0] = addr;
145903 findRightmost(p)->selFlags |= SF_UsesEphemeral;
145904 assert( p->pEList );
145910 assert( !pPrior->pOrderBy );
145920 if( p->op==TK_EXCEPT ){
145923 assert( p->op==TK_UNION );
145926 p->pPrior = 0;
145927 pLimit = p->pLimit;
145928 p->pLimit = 0;
145930 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
145931 sqlite3SelectOpName(p->op)));
145935 assert( p->pOrderBy==0 );
145936 pDelete = p->pPrior;
145937 p->pPrior = pPrior;
145938 p->pOrderBy = 0;
145939 if( p->op==TK_UNION ){
145940 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
145942 sqlite3ExprDelete(db, p->pLimit);
145943 p->pLimit = pLimit;
145944 p->iLimit = 0;
145945 p->iOffset = 0;
145951 assert( p->pEList || db->mallocFailed );
145952 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
145968 default: assert( p->op==TK_INTERSECT ); {
145980 tab1 = pParse->nTab++;
145981 tab2 = pParse->nTab++;
145982 assert( p->pOrderBy==0 );
145985 assert( p->addrOpenEphm[0] == -1 );
145986 p->addrOpenEphm[0] = addr;
145987 findRightmost(p)->selFlags |= SF_UsesEphemeral;
145988 assert( p->pEList );
146002 assert( p->addrOpenEphm[1] == -1 );
146003 p->addrOpenEphm[1] = addr;
146004 p->pPrior = 0;
146005 pLimit = p->pLimit;
146006 p->pLimit = 0;
146008 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
146009 sqlite3SelectOpName(p->op)));
146013 pDelete = p->pPrior;
146014 p->pPrior = pPrior;
146015 if( p->nSelectRow>pPrior->nSelectRow ){
146016 p->nSelectRow = pPrior->nSelectRow;
146018 sqlite3ExprDelete(db, p->pLimit);
146019 p->pLimit = pLimit;
146025 assert( p->pEList );
146047 if( p->pNext==0 ){
146052 if( pParse->nErr ) goto multi_select_end;
146058 ** This section is run by the right-most SELECT statement only.
146059 ** SELECT statements to the left always skip this part. The right-most
146063 if( p->selFlags & SF_UsesEphemeral ){
146067 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
146070 assert( p->pNext==0 );
146071 assert( p->pEList!=0 );
146072 nCol = p->pEList->nExpr;
146078 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
146081 *apColl = db->pDfltColl;
146085 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
146087 int addr = pLoop->addrOpenEphm[i];
146091 assert( pLoop->addrOpenEphm[1]<0 );
146097 pLoop->addrOpenEphm[i] = -1;
146104 pDest->iSdst = dest.iSdst;
146105 pDest->nSdst = dest.nSdst;
146118 if( p->selFlags & SF_Values ){
146123 sqlite3SelectOpName(p->op));
146131 ** The data to be output is contained in pIn->iSdst. There are
146132 ** pIn->nSdst columns to be output. pDest is where the output should
146144 ** If the LIMIT found in p->iLimit is reached, jump immediately to
146157 Vdbe *v = pParse->pVdbe;
146169 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
146173 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
146176 if( pParse->db->mallocFailed ) return 0;
146180 codeOffset(v, p->iOffset, iContinue);
146182 assert( pDest->eDest!=SRT_Exists );
146183 assert( pDest->eDest!=SRT_Table );
146184 switch( pDest->eDest ){
146190 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
146191 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
146192 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
146204 testcase( pIn->nSdst>1 );
146206 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
146207 r1, pDest->zAffSdst, pIn->nSdst);
146208 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
146209 pIn->iSdst, pIn->nSdst);
146217 ** if it is the RHS of a row-value IN operator.
146220 testcase( pIn->nSdst>1 );
146221 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
146228 ** starting at pDest->iSdst. Then the co-routine yields.
146231 if( pDest->iSdst==0 ){
146232 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
146233 pDest->nSdst = pIn->nSdst;
146235 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
146236 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
146249 assert( pDest->eDest==SRT_Output );
146250 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
146257 if( p->iLimit ){
146258 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
146279 ** co-routines. Then run the co-routines in parallel and merge the results
146306 ** ------------- ----------------- -------------- -----------------
146357 Select *p, /* The right-most of SELECTs to be coded */
146362 Select *pSplit; /* Left-most SELECT in the right-hand group */
146367 int regAddrA; /* Address register for select-A coroutine */
146368 int regAddrB; /* Address register for select-B coroutine */
146369 int addrSelectA; /* Address of the select-A coroutine */
146370 int addrSelectB; /* Address of the select-B coroutine */
146371 int regOutA; /* Address register for the output-A subroutine */
146372 int regOutB; /* Address register for the output-B subroutine */
146373 int addrOutA; /* Address of the output-A subroutine */
146374 int addrOutB = 0; /* Address of the output-B subroutine */
146375 int addrEofA; /* Address of the select-A-exhausted subroutine */
146377 int addrEofB; /* Address of the select-B-exhausted subroutine */
146381 int regLimitA; /* Limit register for select-A */
146382 int regLimitB; /* Limit register for select-A */
146384 int savedLimit; /* Saved value of p->iLimit */
146385 int savedOffset; /* Saved value of p->iOffset */
146397 assert( p->pOrderBy!=0 );
146399 db = pParse->db;
146400 v = pParse->pVdbe;
146408 op = p->op;
146409 assert( p->pPrior->pOrderBy==0 );
146410 pOrderBy = p->pOrderBy;
146412 nOrderBy = pOrderBy->nExpr;
146419 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
146421 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
146423 assert( pItem->u.x.iOrderByCol>0 );
146424 if( pItem->u.x.iOrderByCol==i ) break;
146429 pNew->flags |= EP_IntValue;
146430 pNew->u.iValue = i;
146431 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
146432 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
146448 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
146450 assert( pItem->u.x.iOrderByCol>0 );
146451 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
146452 aPermute[i] = pItem->u.x.iOrderByCol - 1;
146466 int nExpr = p->pEList->nExpr;
146467 assert( nOrderBy>=nExpr || db->mallocFailed );
146468 regPrev = pParse->nMem+1;
146469 pParse->nMem += nExpr+1;
146475 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
146476 pKeyDup->aSortFlags[i] = 0;
146487 for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){
146489 assert( pSplit->pPrior->pNext==pSplit );
146496 for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; }
146498 pPrior = pSplit->pPrior;
146500 pSplit->pPrior = 0;
146501 pPrior->pNext = 0;
146502 assert( p->pOrderBy == pOrderBy );
146503 assert( pOrderBy!=0 || db->mallocFailed );
146504 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
146505 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
146506 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
146510 if( p->iLimit && op==TK_ALL ){
146511 regLimitA = ++pParse->nMem;
146512 regLimitB = ++pParse->nMem;
146513 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
146519 sqlite3ExprDelete(db, p->pLimit);
146520 p->pLimit = 0;
146522 regAddrA = ++pParse->nMem;
146523 regAddrB = ++pParse->nMem;
146524 regOutA = ++pParse->nMem;
146525 regOutB = ++pParse->nMem;
146529 ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)));
146532 ** left of the compound operator - the "A" select.
146537 pPrior->iLimit = regLimitA;
146544 ** the right - the "B" select
146549 savedLimit = p->iLimit;
146550 savedOffset = p->iOffset;
146551 p->iLimit = regLimitB;
146552 p->iOffset = 0;
146555 p->iLimit = savedLimit;
146556 p->iOffset = savedOffset;
146584 VdbeNoopComment((v, "eof-A subroutine"));
146589 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
146597 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
146599 VdbeNoopComment((v, "eof-B subroutine"));
146607 VdbeNoopComment((v, "A-lt-B subroutine"));
146620 VdbeNoopComment((v, "A-eq-B subroutine"));
146628 VdbeNoopComment((v, "A-gt-B subroutine"));
146657 if( pSplit->pPrior ){
146658 sqlite3ParserAddCleanup(pParse, sqlite3SelectDeleteGeneric, pSplit->pPrior);
146660 pSplit->pPrior = pPrior;
146661 pPrior->pNext = pSplit;
146662 sqlite3ExprListDelete(db, pPrior->pOrderBy);
146663 pPrior->pOrderBy = 0;
146668 return pParse->nErr!=0;
146683 ** position in the parent that NULL-able due to an OUTER JOIN. Either the
146706 ** when processing a non-matched row of the left.
146723 ** a column in table number iTable with a copy of the iColumn-th
146740 && pExpr->w.iJoin==pSubst->iTable
146743 pExpr->w.iJoin = pSubst->iNewTable;
146745 if( pExpr->op==TK_COLUMN
146746 && pExpr->iTable==pSubst->iTable
146750 if( pExpr->iColumn<0 ){
146751 pExpr->op = TK_NULL;
146759 iColumn = pExpr->iColumn;
146761 assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr );
146762 assert( pExpr->pRight==0 );
146763 pCopy = pSubst->pEList->a[iColumn].pExpr;
146765 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
146767 sqlite3 *db = pSubst->pParse->db;
146768 if( pSubst->isOuterJoin
146769 && (pCopy->op!=TK_COLUMN || pCopy->iTable!=pSubst->iNewTable)
146774 ifNullRow.iTable = pSubst->iNewTable;
146775 ifNullRow.iColumn = -99;
146781 if( db->mallocFailed ){
146785 if( pSubst->isOuterJoin ){
146789 sqlite3SetJoinExpr(pNew, pExpr->w.iJoin,
146790 pExpr->flags & (EP_OuterON|EP_InnerON));
146794 if( pExpr->op==TK_TRUEFALSE ){
146795 pExpr->u.iValue = sqlite3ExprTruthValue(pExpr);
146796 pExpr->op = TK_INTEGER;
146801 ** just as it did when it was a column of a view or sub-query. */
146803 CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
146804 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
146805 pSubst->pCList->a[iColumn].pExpr
146807 if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){
146808 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
146809 (pColl ? pColl->zName : "BINARY")
146817 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
146818 pExpr->iTable = pSubst->iNewTable;
146820 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
146821 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
146823 substSelect(pSubst, pExpr->x.pSelect, 1);
146825 substExprList(pSubst, pExpr->x.pList);
146829 Window *pWin = pExpr->y.pWin;
146830 pWin->pFilter = substExpr(pSubst, pWin->pFilter);
146831 substExprList(pSubst, pWin->pPartition);
146832 substExprList(pSubst, pWin->pOrderBy);
146844 for(i=0; i<pList->nExpr; i++){
146845 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
146851 int doPrior /* Do substitutes on p->pPrior too */
146858 substExprList(pSubst, p->pEList);
146859 substExprList(pSubst, p->pGroupBy);
146860 substExprList(pSubst, p->pOrderBy);
146861 p->pHaving = substExpr(pSubst, p->pHaving);
146862 p->pWhere = substExpr(pSubst, p->pWhere);
146863 pSrc = p->pSrc;
146865 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
146866 substSelect(pSubst, pItem->pSelect, 1);
146867 if( pItem->fg.isTabFunc ){
146868 substExprList(pSubst, pItem->u1.pFuncArg);
146871 }while( doPrior && (p = p->pPrior)!=0 );
146881 ** pSrcItem->colUsed mask.
146885 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
146886 pItem = pWalker->u.pSrcItem;
146887 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
146888 if( pExpr->iColumn<0 ) return WRC_Continue;
146889 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
146897 if( NEVER(pSrcItem->pTab==0) ) return;
146902 pSrcItem->colUsed = 0;
146918 ** If pSrc contains any sub-selects, call this routine recursively
146919 ** on the FROM clause of each such sub-select, with iExcept set to -1.
146929 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
146932 assert( pItem->iCursor < aCsrMap[0] );
146933 if( !pItem->fg.isRecursive || aCsrMap[pItem->iCursor+1]==0 ){
146934 aCsrMap[pItem->iCursor+1] = pParse->nTab++;
146936 pItem->iCursor = aCsrMap[pItem->iCursor+1];
146937 for(p=pItem->pSelect; p; p=p->pPrior){
146938 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1);
146948 int *aCsrMap = pWalker->u.aiCol;
146960 int op = pExpr->op;
146962 renumberCursorDoMapping(pWalker, &pExpr->iTable);
146965 renumberCursorDoMapping(pWalker, &pExpr->w.iJoin);
146973 ** cursor in the FROM clause of any FROM clause sub-selects, recursively.
146995 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept);
147010 while( pSel->pPrior ){
147011 pSel = pSel->pPrior;
147013 return pSel->pEList;
147017 ** Return true if any of the result-set columns in the compound query
147024 assert( p->pEList!=0 );
147025 assert( p->pPrior!=0 );
147026 pList = p->pEList;
147027 for(ii=0; ii<pList->nExpr; ii++){
147030 assert( pList->a[ii].pExpr!=0 );
147031 aff = sqlite3ExprAffinity(pList->a[ii].pExpr);
147032 for(pSub1=p->pPrior; pSub1; pSub1=pSub1->pPrior){
147033 assert( pSub1->pEList!=0 );
147034 assert( pSub1->pEList->nExpr>ii );
147035 assert( pSub1->pEList->a[ii].pExpr!=0 );
147036 if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
147080 ** other than the one FROM-clause subquery that is a candidate
147082 ** from 2015-02-09.)
147096 ** sub-queries that were excluded from this optimization. Restriction
147111 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
147112 ** accidentally carried the comment forward until 2014-09-15. Original
147149 ** The parent and sub-query may contain WHERE clauses. Subject to
147156 ** Also, each component of the sub-query must return the same number
147159 ** such (illegal) sub-query is flattened. The caller will detect the
147162 ** (18) If the sub-query is a compound select, then all terms of the
147169 ** (20) If the sub-query is a compound select, then it must not use
147180 ** (23) If the outer query is a recursive CTE, then the sub-query may not be
147186 ** The subquery may not be an aggregate that uses the built-in min() or
147209 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
147212 ** If flattening is not attempted, this routine is a no-op and returns 0.
147221 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
147224 const char *zSavedAuthContext = pParse->zAuthContext;
147227 Select *pSub1; /* Pointer to the rightmost select in sub-query */
147231 int iNewParent = -1;/* Replacement table for iParent */
147236 sqlite3 *db = pParse->db;
147243 assert( p->pPrior==0 );
147245 pSrc = p->pSrc;
147246 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
147247 pSubitem = &pSrc->a[iFrom];
147248 iParent = pSubitem->iCursor;
147249 pSub = pSubitem->pSelect;
147253 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
147256 pSubSrc = pSub->pSrc;
147260 ** because they could be computed at compile-time. But when LIMIT and OFFSET
147263 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
147264 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
147265 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
147268 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
147269 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
147270 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
147273 if( p->pOrderBy && pSub->pOrderBy ){
147276 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
147277 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
147278 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
147281 if( pSub->selFlags & (SF_Recursive) ){
147300 if( (pSubitem->fg.jointype & (JT_OUTER|JT_LTORJ))!=0 ){
147301 if( pSubSrc->nSrc>1 /* (3a) */
147302 || IsVirtual(pSubSrc->a[0].pTab) /* (3b) */
147303 || (p->selFlags & SF_Distinct)!=0 /* (3d) */
147304 || (pSubitem->fg.jointype & JT_RIGHT)!=0 /* (26) */
147311 assert( pSubSrc->nSrc>0 ); /* True by restriction (7) */
147312 if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
147317 assert( !pSubitem->fg.isCte || pSubitem->u2.pCteUse->eM10d!=M10d_Yes );
147319 /* Restriction (17): If the sub-query is a compound SELECT, then it must
147324 if( pSub->pPrior ){
147326 if( pSub->pOrderBy ){
147329 if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){
147332 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
147333 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
147334 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
147335 assert( pSub->pSrc!=0 );
147336 assert( (pSub->selFlags & SF_Recursive)==0 );
147337 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
147338 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
147339 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
147340 || pSub1->pSrc->nSrc<1 /* (17c) */
147342 || pSub1->pWin /* (17e) */
147347 if( iFrom>0 && (pSub1->pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
147349 ** omitted on left-hand tables of the right join that is being
147353 testcase( pSub1->pSrc->nSrc>1 );
147357 if( p->pOrderBy ){
147358 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
147359 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
147364 if( (p->selFlags & SF_Recursive) ) return 0;
147369 if( pSrc->nSrc>1 ){
147370 if( pParse->nSelect>500 ) return 0;
147372 aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
147373 if( aCsrMap ) aCsrMap[0] = pParse->nTab;
147379 pSub->selId, pSub, iFrom));
147382 pParse->zAuthContext = pSubitem->zName;
147385 pParse->zAuthContext = zSavedAuthContext;
147388 pSub1 = pSubitem->pSelect;
147389 sqlite3DbFree(db, pSubitem->zDatabase);
147390 sqlite3DbFree(db, pSubitem->zName);
147391 sqlite3DbFree(db, pSubitem->zAlias);
147392 pSubitem->zDatabase = 0;
147393 pSubitem->zName = 0;
147394 pSubitem->zAlias = 0;
147395 pSubitem->pSelect = 0;
147396 assert( pSubitem->fg.isUsing!=0 || pSubitem->u3.pOn==0 );
147398 /* If the sub-query is a compound SELECT statement, then (by restrictions
147402 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
147405 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
147406 ** OFFSET clauses and joins them to the left-hand-side of the original
147408 ** select statements in the compound sub-query.
147429 ** We call this the "compound-subquery flattening".
147431 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
147433 ExprList *pOrderBy = p->pOrderBy;
147434 Expr *pLimit = p->pLimit;
147435 Select *pPrior = p->pPrior;
147436 Table *pItemTab = pSubitem->pTab;
147437 pSubitem->pTab = 0;
147438 p->pOrderBy = 0;
147439 p->pPrior = 0;
147440 p->pLimit = 0;
147442 p->pLimit = pLimit;
147443 p->pOrderBy = pOrderBy;
147444 p->op = TK_ALL;
147445 pSubitem->pTab = pItemTab;
147447 p->pPrior = pPrior;
147449 pNew->selId = ++pParse->nSelect;
147450 if( aCsrMap && ALWAYS(db->mallocFailed==0) ){
147453 pNew->pPrior = pPrior;
147454 if( pPrior ) pPrior->pNext = pNew;
147455 pNew->pNext = p;
147456 p->pPrior = pNew;
147457 TREETRACE(0x4,pParse,p,("compound-subquery flattener"
147458 " creates %u as peer\n",pNew->selId));
147460 assert( pSubitem->pSelect==0 );
147463 if( db->mallocFailed ){
147464 pSubitem->pSelect = pSub1;
147473 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
147475 if( ALWAYS(pSubitem->pTab!=0) ){
147476 Table *pTabToDel = pSubitem->pTab;
147477 if( pTabToDel->nTabRef==1 ){
147480 testcase( pToplevel->earlyCleanup );
147482 pTabToDel->nTabRef--;
147484 pSubitem->pTab = 0;
147487 /* The following loop runs once for each term in a compound-subquery
147489 ** of flattening - a flattening other than a compound-subquery flattening -
147501 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
147504 u8 ltorj = pSrc->a[iFrom].fg.jointype & JT_LTORJ;
147506 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
147507 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
147508 pSrc = pParent->pSrc; /* FROM clause of the outer query */
147511 jointype = pSubitem->fg.jointype; /* First time through the loop */
147530 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
147532 pParent->pSrc = pSrc;
147539 SrcItem *pItem = &pSrc->a[i+iFrom];
147540 if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing);
147541 assert( pItem->fg.isTabFunc==0 );
147542 *pItem = pSubSrc->a[i];
147543 pItem->fg.jointype |= ltorj;
147544 iNewParent = pSubSrc->a[i].iCursor;
147545 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
147547 pSrc->a[iFrom].fg.jointype &= JT_LTORJ;
147548 pSrc->a[iFrom].fg.jointype |= jointype | ltorj;
147562 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
147563 /* At this point, any non-zero iOrderByCol values indicate that the
147570 ** function attempts to flatten a compound sub-query into pParent
147571 ** (the only way this can happen is if the compound sub-query is
147572 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
147573 ExprList *pOrderBy = pSub->pOrderBy;
147574 for(i=0; i<pOrderBy->nExpr; i++){
147575 pOrderBy->a[i].u.x.iOrderByCol = 0;
147577 assert( pParent->pOrderBy==0 );
147578 pParent->pOrderBy = pOrderBy;
147579 pSub->pOrderBy = 0;
147581 pWhere = pSub->pWhere;
147582 pSub->pWhere = 0;
147587 if( pParent->pWhere ){
147588 pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere);
147590 pParent->pWhere = pWhere;
147593 if( db->mallocFailed==0 ){
147599 x.pEList = pSub->pEList;
147606 pParent->selFlags |= pSub->selFlags & SF_Compound;
147607 assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */
147615 if( pSub->pLimit ){
147616 pParent->pLimit = pSub->pLimit;
147617 pSub->pLimit = 0;
147623 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
147651 u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */
147675 assert( pColumn->op==TK_COLUMN );
147676 assert( sqlite3ExprIsConstant(pConst->pParse, pValue) );
147680 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
147684 /* 2018-10-25 ticket [cf5ed20f]
147686 for(i=0; i<pConst->nConst; i++){
147687 const Expr *pE2 = pConst->apExpr[i*2];
147688 assert( pE2->op==TK_COLUMN );
147689 if( pE2->iTable==pColumn->iTable
147690 && pE2->iColumn==pColumn->iColumn
147696 pConst->bHasAffBlob = 1;
147699 pConst->nConst++;
147700 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
147701 pConst->nConst*2*sizeof(Expr*));
147702 if( pConst->apExpr==0 ){
147703 pConst->nConst = 0;
147705 pConst->apExpr[pConst->nConst*2-2] = pColumn;
147706 pConst->apExpr[pConst->nConst*2-1] = pValue;
147713 ** is part of the AND-connected terms of the expression. For each term
147719 if( ExprHasProperty(pExpr, pConst->mExcludeOn) ){
147724 if( pExpr->op==TK_AND ){
147725 findConstInWhere(pConst, pExpr->pRight);
147726 findConstInWhere(pConst, pExpr->pLeft);
147729 if( pExpr->op!=TK_EQ ) return;
147730 pRight = pExpr->pRight;
147731 pLeft = pExpr->pLeft;
147734 if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pConst->pParse, pLeft) ){
147737 if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pConst->pParse, pRight) ){
147746 ** pExpr is equivalent to one of the columns named in pWalker->u.pConst,
147748 ** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr
147757 if( pConst->pOomFault[0] ) return WRC_Prune;
147758 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
147759 if( ExprHasProperty(pExpr, EP_FixedCol|pConst->mExcludeOn) ){
147765 for(i=0; i<pConst->nConst; i++){
147766 Expr *pColumn = pConst->apExpr[i*2];
147768 if( pColumn->iTable!=pExpr->iTable ) continue;
147769 if( pColumn->iColumn!=pExpr->iColumn ) continue;
147774 pConst->nChng++;
147777 assert( pExpr->pLeft==0 );
147778 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
147779 if( pConst->pParse->db->mallocFailed ) return WRC_Prune;
147788 ** any substitutions based on the contents of pWalker->u.pConst should
147794 ** one of the columns in pWalker->u.pConst, or
147799 ** pWalker->u.pConst.
147802 WhereConst *pConst = pWalker->u.pConst;
147807 if( pConst->bHasAffBlob ){
147808 if( (pExpr->op>=TK_EQ && pExpr->op<=TK_GE)
147809 || pExpr->op==TK_IS
147811 propagateConstantExprRewriteOne(pConst, pExpr->pLeft, 0);
147812 if( pConst->pOomFault[0] ) return WRC_Prune;
147813 if( sqlite3ExprAffinity(pExpr->pLeft)!=SQLITE_AFF_TEXT ){
147814 propagateConstantExprRewriteOne(pConst, pExpr->pRight, 0);
147818 return propagateConstantExprRewriteOne(pConst, pExpr, pConst->bHasAffBlob);
147822 ** The WHERE-clause constant propagation optimization.
147825 ** CONSTANT=COLUMN that are top-level AND-connected terms that are not
147857 ** 2021-05-25 forum post 6a06202608: Another troublesome case is...
147880 x.pOomFault = &pParse->db->mallocFailed;
147886 if( ALWAYS(p->pSrc!=0)
147887 && p->pSrc->nSrc>0
147888 && (p->pSrc->a[0].fg.jointype & JT_LTORJ)!=0
147897 findConstInWhere(&x, p->pWhere);
147906 sqlite3WalkExpr(&w, p->pWhere);
147907 sqlite3DbFree(x.pParse->db, x.apExpr);
147918 ** push WHERE clause expression pExpr down to FROM clause sub-query
147925 ** BY clause of all window function used by the sub-query. It is safe
147931 ** * the sub-query uses only one distinct window frame, and
147935 assert( pSubq->pWin->pPartition );
147936 assert( (pSubq->selFlags & SF_MultiPart)==0 );
147937 assert( pSubq->pPrior==0 );
147938 return sqlite3ExprIsConstantOrGroupBy(pParse, pExpr, pSubq->pWin->pPartition);
147948 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
147952 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
147960 ** This optimization is called the "WHERE-clause push-down optimization".
147963 ** with a similar name: The "MySQL push-down optimization" causes WHERE
147972 ** (1) (** This restriction was removed on 2017-09-29. We used to
147990 ** of a LEFT JOIN where iCursor is not the right-hand table of that
148009 ** (6b) The inner query is a compound and uses window-functions.
148013 ** all window-functions used by the sub-query. It is safe to
148015 ** window over which any window-function is calculated.
148035 ** Without this restriction, the WHERE-clause push-down optimization
148040 ** (10) The inner query is not the right-hand table of a RIGHT JOIN.
148048 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
148061 pSrc = &pSrcList->a[iSrc];
148063 if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ){
148066 if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ){
148070 if( pSubq->pPrior ){
148073 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
148074 u8 op = pSel->op;
148081 if( pSel->pWin ) return 0; /* restriction (6b) */
148087 ** non-BINARY collating sequence. */
148088 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
148090 const ExprList *pList = pSel->pEList;
148092 for(ii=0; ii<pList->nExpr; ii++){
148093 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[ii].pExpr);
148102 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
148113 for(pX=pSubq; pX; pX=pX->pPrior){
148114 assert( (pX->selFlags & (SF_Recursive))==0 );
148119 if( pSubq->pLimit!=0 ){
148122 while( pWhere->op==TK_AND ){
148123 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, pSrcList, iSrc);
148124 pWhere = pWhere->pLeft;
148129 && (pSrcList->a[0].fg.jointype & JT_LTORJ)!=0 /* Fast pre-test of (9c) */
148133 if( pWhere->w.iJoin==pSrcList->a[jj].iCursor ){
148138 if( (pSrcList->a[jj].fg.jointype & JT_RIGHT)!=0 ){
148147 || pWhere->w.iJoin!=iCursor)
148152 && pWhere->w.iJoin!=iCursor
148159 if( ViewCanHaveRowid && (pWhere->op==TK_ISNULL || pWhere->op==TK_NOTNULL) ){
148160 Expr *pLeft = pWhere->pLeft;
148162 && pLeft->op==TK_COLUMN
148163 && pLeft->iColumn < 0
148172 pSubq->selFlags |= SF_PushDown;
148175 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
148176 unsetJoinExpr(pNew, -1, 1);
148178 x.iTable = pSrc->iCursor;
148179 x.iNewTable = pSrc->iCursor;
148181 x.pEList = pSubq->pEList;
148185 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
148186 /* Restriction 6c has prevented push-down in this case */
148187 sqlite3ExprDelete(pParse->db, pNew);
148188 nChng--;
148192 if( pSubq->selFlags & SF_Aggregate ){
148193 pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
148195 pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
148197 pSubq = pSubq->pPrior;
148205 ** Check to see if a subquery contains result-set columns that are
148206 ** never used. If it does, change the value of those result-set columns
148221 if( pItem->fg.isCorrelated || pItem->fg.isCte ){
148224 assert( pItem->pTab!=0 );
148225 pTab = pItem->pTab;
148226 assert( pItem->pSelect!=0 );
148227 pSub = pItem->pSelect;
148228 assert( pSub->pEList->nExpr==pTab->nCol );
148229 for(pX=pSub; pX; pX=pX->pPrior){
148230 if( (pX->selFlags & (SF_Distinct|SF_Aggregate))!=0 ){
148231 testcase( pX->selFlags & SF_Distinct );
148232 testcase( pX->selFlags & SF_Aggregate );
148235 if( pX->pPrior && pX->op!=TK_ALL ){
148241 if( pX->pWin ){
148248 colUsed = pItem->colUsed;
148249 if( pSub->pOrderBy ){
148250 ExprList *pList = pSub->pOrderBy;
148251 for(j=0; j<pList->nExpr; j++){
148252 u16 iCol = pList->a[j].u.x.iOrderByCol;
148254 iCol--;
148255 colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
148259 nCol = pTab->nCol;
148261 Bitmask m = j<BMS-1 ? MASKBIT(j) : TOPBIT;
148263 for(pX=pSub; pX; pX=pX->pPrior) {
148264 Expr *pY = pX->pEList->a[j].pExpr;
148265 if( pY->op==TK_NULL ) continue;
148266 pY->op = TK_NULL;
148268 pX->selFlags |= SF_PushDown;
148300 assert( pFunc->op==TK_AGG_FUNCTION );
148303 pEList = pFunc->x.pList;
148305 || pEList->nExpr!=1
148312 zFunc = pFunc->u.zToken;
148315 if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
148325 assert( pOrderBy!=0 || db->mallocFailed );
148326 if( pOrderBy ) pOrderBy->a[0].fg.sortFlags = sortFlags;
148332 ** The second argument is the associated aggregate-info object. This
148337 ** where table is a database table, not a sub-select or view. If the query
148351 assert( !p->pGroupBy );
148353 if( p->pWhere
148354 || p->pEList->nExpr!=1
148355 || p->pSrc->nSrc!=1
148356 || p->pSrc->a[0].pSelect
148357 || pAggInfo->nFunc!=1
148358 || p->pHaving
148362 pTab = p->pSrc->a[0].pTab;
148366 pExpr = p->pEList->a[0].pExpr;
148368 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
148369 if( pExpr->pAggInfo!=pAggInfo ) return 0;
148370 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
148371 assert( pAggInfo->aFunc[0].pFExpr==pExpr );
148380 ** If the source-list item passed as an argument was augmented with an
148384 ** pFrom->pIndex and return SQLITE_OK.
148387 Table *pTab = pFrom->pTab;
148388 char *zIndexedBy = pFrom->u1.zIndexedBy;
148391 assert( pFrom->fg.isIndexedBy!=0 );
148393 for(pIdx=pTab->pIndex;
148394 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
148395 pIdx=pIdx->pNext
148399 pParse->checkSchema = 1;
148402 assert( pFrom->fg.isCte==0 );
148403 pFrom->u2.pIBIndex = pIdx;
148438 if( p->pPrior==0 ) return WRC_Continue;
148439 if( p->pOrderBy==0 ) return WRC_Continue;
148440 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
148442 a = p->pOrderBy->a;
148444 /* If iOrderByCol is already non-zero, then it has already been matched
148446 ** SELECT is rewritten for window-functions processing and then passed
148451 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
148452 if( a[i].pExpr->flags & EP_Collate ) break;
148458 pParse = pWalker->pParse;
148459 db = pParse->db;
148466 p->pSrc = pNewSrc;
148467 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
148468 p->op = TK_SELECT;
148469 p->pWhere = 0;
148470 pNew->pGroupBy = 0;
148471 pNew->pHaving = 0;
148472 pNew->pOrderBy = 0;
148473 p->pPrior = 0;
148474 p->pNext = 0;
148475 p->pWith = 0;
148477 p->pWinDefn = 0;
148479 p->selFlags &= ~SF_Compound;
148480 assert( (p->selFlags & SF_Converted)==0 );
148481 p->selFlags |= SF_Converted;
148482 assert( pNew->pPrior!=0 );
148483 pNew->pPrior->pNext = pNew;
148484 pNew->pLimit = 0;
148489 ** Check to see if the FROM clause term pFrom has table-valued function
148491 ** non-zero, since pFrom is not allowed to be a table-valued function.
148494 if( pFrom->fg.isTabFunc ){
148495 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
148505 ** FROM clause element pItem is really a common-table-expression (CTE)
148509 ** If a non-NULL value is returned, set *ppContext to point to the With
148517 const char *zName = pItem->zName;
148519 assert( pItem->zDatabase==0 );
148521 for(p=pWith; p; p=p->pOuter){
148523 for(i=0; i<p->nCte; i++){
148524 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
148526 return &p->a[i];
148529 if( p->bView ) break;
148535 ** with the inner-most WITH clause being at the top of the stack.
148558 if( pParse->nErr==0 ){
148559 assert( pParse->pWith!=pWith );
148560 pWith->pOuter = pParse->pWith;
148561 pParse->pWith = pWith;
148570 ** pParse->pWith linked list). And if currently processing a CTE
148574 ** If pFrom matches a CTE according to either of these two above, pFrom->pTab
148589 assert( pFrom->pTab==0 );
148590 if( pParse->pWith==0 ){
148594 if( pParse->nErr ){
148595 /* Prior errors might have left pParse->pWith in a goofy state, so
148599 if( pFrom->zDatabase!=0 ){
148604 if( pFrom->fg.notCte ){
148612 pCte = searchWith(pParse->pWith, pFrom, &pWith);
148614 sqlite3 *db = pParse->db;
148618 Select *pLeft; /* Left-most SELECT statement */
148619 Select *pRecTerm; /* Left-most recursive term */
148621 With *pSavedWith; /* Initial value of pParse->pWith */
148622 int iRecTab = -1; /* Cursor for recursive table */
148625 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
148627 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
148629 if( pCte->zCteErr ){
148630 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
148635 assert( pFrom->pTab==0 );
148638 pCteUse = pCte->pUse;
148640 pCte->pUse = pCteUse = sqlite3DbMallocZero(db, sizeof(pCteUse[0]));
148647 pCteUse->eM10d = pCte->eM10d;
148649 pFrom->pTab = pTab;
148650 pTab->nTabRef = 1;
148651 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
148652 pTab->iPKey = -1;
148653 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
148654 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
148655 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
148656 if( db->mallocFailed ) return 2;
148657 pFrom->pSelect->selFlags |= SF_CopyCte;
148658 assert( pFrom->pSelect );
148659 if( pFrom->fg.isIndexedBy ){
148660 sqlite3ErrorMsg(pParse, "no such index: \"%s\"", pFrom->u1.zIndexedBy);
148663 pFrom->fg.isCte = 1;
148664 pFrom->u2.pCteUse = pCteUse;
148665 pCteUse->nUse++;
148668 pRecTerm = pSel = pFrom->pSelect;
148669 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
148670 while( bMayRecursive && pRecTerm->op==pSel->op ){
148672 SrcList *pSrc = pRecTerm->pSrc;
148673 assert( pRecTerm->pPrior!=0 );
148674 for(i=0; i<pSrc->nSrc; i++){
148675 SrcItem *pItem = &pSrc->a[i];
148676 if( pItem->zDatabase==0
148677 && pItem->zName!=0
148678 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
148680 pItem->pTab = pTab;
148681 pTab->nTabRef++;
148682 pItem->fg.isRecursive = 1;
148683 if( pRecTerm->selFlags & SF_Recursive ){
148685 "multiple references to recursive table: %s", pCte->zName
148689 pRecTerm->selFlags |= SF_Recursive;
148690 if( iRecTab<0 ) iRecTab = pParse->nTab++;
148691 pItem->iCursor = iRecTab;
148694 if( (pRecTerm->selFlags & SF_Recursive)==0 ) break;
148695 pRecTerm = pRecTerm->pPrior;
148698 pCte->zCteErr = "circular reference: %s";
148699 pSavedWith = pParse->pWith;
148700 pParse->pWith = pWith;
148701 if( pSel->selFlags & SF_Recursive ){
148704 assert( (pRecTerm->selFlags & SF_Recursive)==0 );
148705 assert( pRecTerm->pNext!=0 );
148706 assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 );
148707 assert( pRecTerm->pWith==0 );
148708 pRecTerm->pWith = pSel->pWith;
148710 pRecTerm->pWith = 0;
148712 pParse->pWith = pSavedWith;
148717 pParse->pWith = pSavedWith;
148721 pParse->pWith = pWith;
148723 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
148724 pEList = pLeft->pEList;
148725 if( pCte->pCols ){
148726 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
148728 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
148730 pParse->pWith = pSavedWith;
148733 pEList = pCte->pCols;
148736 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
148738 if( pSel->selFlags & SF_Recursive ){
148739 pCte->zCteErr = "multiple recursive references: %s";
148741 pCte->zCteErr = "recursive reference in a subquery: %s";
148745 pCte->zCteErr = 0;
148746 pParse->pWith = pSavedWith;
148763 Parse *pParse = pWalker->pParse;
148764 if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
148765 With *pWith = findRightmost(p)->pWith;
148767 assert( pParse->pWith==pWith || pParse->nErr );
148768 pParse->pWith = pWith->pOuter;
148776 ** sub-query in the FROM clause of a SELECT statement. This function
148782 Select *pSel = pFrom->pSelect;
148786 pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
148788 pTab->nTabRef = 1;
148789 if( pFrom->zAlias ){
148790 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
148792 pTab->zName = sqlite3MPrintf(pParse->db, "%!S", pFrom);
148794 while( pSel->pPrior ){ pSel = pSel->pPrior; }
148795 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
148796 pTab->iPKey = -1;
148797 pTab->eTabType = TABTYP_VIEW;
148798 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
148800 /* The usual case - do not allow ROWID on a subquery */
148801 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
148804 pTab->tabFlags |= TF_Ephemeral | sqlite3Config.mNoVisibleRowid;
148806 return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
148825 N--;
148827 if( pBase->fg.isUsing==0 ) continue;
148828 if( NEVER(pBase->u3.pUsing==0) ) continue;
148829 if( sqlite3IdListIndex(pBase->u3.pUsing, zName)>=0 ) return 1;
148842 ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
148844 ** fill pTabList->a[].pSelect with a copy of the SELECT statement
148860 Parse *pParse = pWalker->pParse;
148865 sqlite3 *db = pParse->db;
148867 u16 selFlags = p->selFlags;
148870 p->selFlags |= SF_Expanded;
148871 if( db->mallocFailed ){
148874 assert( p->pSrc!=0 );
148878 if( pWalker->eCode ){
148880 p->selId = ++pParse->nSelect;
148882 pTabList = p->pSrc;
148883 pEList = p->pEList;
148884 if( pParse->pWith && (p->selFlags & SF_View) ){
148885 if( p->pWith==0 ){
148886 p->pWith = (With*)sqlite3DbMallocZero(db, sizeof(With));
148887 if( p->pWith==0 ){
148891 p->pWith->bView = 1;
148893 sqlite3WithPush(pParse, p->pWith, 0);
148904 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
148906 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
148907 if( pFrom->pTab ) continue;
148908 assert( pFrom->fg.isRecursive==0 );
148909 if( pFrom->zName==0 ){
148911 Select *pSel = pFrom->pSelect;
148912 /* A sub-query in the FROM clause of a SELECT */
148914 assert( pFrom->pTab==0 );
148921 pTab = pFrom->pTab;
148926 assert( pFrom->pTab==0 );
148927 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
148929 if( pTab->nTabRef>=0xffff ){
148931 pTab->zName);
148932 pFrom->pTab = 0;
148935 pTab->nTabRef++;
148942 u8 eCodeOrig = pWalker->eCode;
148944 assert( pFrom->pSelect==0 );
148946 if( (db->flags & SQLITE_EnableView)==0
148947 && pTab->pSchema!=db->aDb[1].pSchema
148950 pTab->zName);
148952 pFrom->pSelect = sqlite3SelectDup(db, pTab->u.view.pSelect, 0);
148956 && pFrom->fg.fromDDL
148957 && ALWAYS(pTab->u.vtab.p!=0)
148958 && pTab->u.vtab.p->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0)
148961 pTab->zName);
148965 nCol = pTab->nCol;
148966 pTab->nCol = -1;
148967 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
148968 sqlite3WalkSelect(pWalker, pFrom->pSelect);
148969 pWalker->eCode = eCodeOrig;
148970 pTab->nCol = nCol;
148976 if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){
148983 assert( db->mallocFailed==0 || pParse->nErr!=0 );
148984 if( pParse->nErr || sqlite3ProcessJoin(pParse, p) ){
148999 for(k=0; k<pEList->nExpr; k++){
149000 pE = pEList->a[k].pExpr;
149001 if( pE->op==TK_ASTERISK ) break;
149002 assert( pE->op!=TK_DOT || pE->pRight!=0 );
149003 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
149004 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
149005 elistFlags |= pE->flags;
149007 if( k<pEList->nExpr ){
149013 struct ExprList_item *a = pEList->a;
149015 int flags = pParse->db->flags;
149019 for(k=0; k<pEList->nExpr; k++){
149021 elistFlags |= pE->flags;
149022 pRight = pE->pRight;
149023 assert( pE->op!=TK_DOT || pRight!=0 );
149024 if( pE->op!=TK_ASTERISK
149025 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
149031 pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
149032 pNew->a[pNew->nExpr-1].fg.eEName = a[k].fg.eEName;
149042 if( pE->op==TK_DOT ){
149044 assert( pE->pLeft!=0 );
149045 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
149046 zTName = pE->pLeft->u.zToken;
149047 assert( ExprUseWOfst(pE->pLeft) );
149048 iErrOfst = pE->pRight->w.iOfst;
149051 iErrOfst = pE->w.iOfst;
149053 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
149055 Table *pTab = pFrom->pTab; /* Table for this data source */
149056 ExprList *pNestedFrom; /* Result-set of a nested FROM clause */
149062 if( (zTabName = pFrom->zAlias)==0 ){
149063 zTabName = pTab->zName;
149065 if( db->mallocFailed ) break;
149066 assert( (int)pFrom->fg.isNestedFrom == IsNestedFrom(pFrom->pSelect) );
149067 if( pFrom->fg.isNestedFrom ){
149068 assert( pFrom->pSelect!=0 );
149069 pNestedFrom = pFrom->pSelect->pEList;
149071 assert( pNestedFrom->nExpr==pTab->nCol );
149078 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
149079 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
149081 if( i+1<pTabList->nSrc
149087 for(ii=0; ii<pUsing->nId; ii++){
149088 const char *zUName = pUsing->a[ii].zName;
149093 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
149094 assert( pX->zEName==0 );
149095 pX->zEName = sqlite3MPrintf(db,"..%s", zUName);
149096 pX->fg.eEName = ENAME_TAB;
149097 pX->fg.bUsingTerm = 1;
149104 nAdd = pTab->nCol;
149110 if( j==pTab->nCol ){
149114 zName = pTab->aCol[j].zCnName;
149116 /* If pTab is actually an SF_NestedFrom sub-select, do not
149118 if( pNestedFrom && pNestedFrom->a[j].fg.eEName==ENAME_ROWID ){
149124 && sqlite3MatchEName(&pNestedFrom->a[j], 0, zTName, 0, 0)==0
149130 ** result-set list unless the SELECT has the SF_IncludeHidden
149133 if( (p->selFlags & SF_IncludeHidden)==0
149134 && IsHiddenColumn(&pTab->aCol[j])
149138 if( (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0
149149 if( pFrom->fg.isUsing
149150 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0
149158 if( (pTabList->nSrc>1
149159 && ( (pFrom->fg.jointype & JT_LTORJ)==0
149161 || !inAnyUsingClause(zName,pFrom,pTabList->nSrc-i-1)
149169 if( IN_RENAME_OBJECT && pE->pLeft ){
149170 sqlite3RenameTokenRemap(pParse, pLeft, pE->pLeft);
149184 pX = &pNew->a[pNew->nExpr-1];
149185 assert( pX->zEName==0 );
149187 if( pNestedFrom && (!ViewCanHaveRowid || j<pNestedFrom->nExpr) ){
149188 assert( j<pNestedFrom->nExpr );
149189 pX->zEName = sqlite3DbStrDup(db, pNestedFrom->a[j].zEName);
149190 testcase( pX->zEName==0 );
149192 pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
149194 testcase( pX->zEName==0 );
149196 pX->fg.eEName = (j==pTab->nCol ? ENAME_ROWID : ENAME_TAB);
149197 if( (pFrom->fg.isUsing
149198 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0)
149200 || (j<pTab->nCol && (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND))
149202 pX->fg.bNoExpand = 1;
149205 pX->zEName = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
149206 pX->fg.eEName = ENAME_NAME;
149208 pX->zEName = sqlite3DbStrDup(db, zName);
149209 pX->fg.eEName = ENAME_NAME;
149223 p->pEList = pNew;
149225 if( p->pEList ){
149226 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
149231 p->selFlags |= SF_ComplexResult;
149236 TREETRACE(0x8,pParse,p,("After result-set wildcard expansion:\n"));
149263 ** The calling function can detect the problem by looking at pParse->nErr
149264 ** and/or pParse->db->mallocFailed.
149270 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
149287 ** For each FROM-clause subquery, add Column.zType, Column.zColl, and
149302 if( p->selFlags & SF_HasTypeInfo ) return;
149303 p->selFlags |= SF_HasTypeInfo;
149304 pParse = pWalker->pParse;
149305 assert( (p->selFlags & SF_Resolved) );
149306 pTabList = p->pSrc;
149307 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
149308 Table *pTab = pFrom->pTab;
149310 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
149311 /* A sub-query in the FROM clause of a SELECT */
149312 Select *pSel = pFrom->pSelect;
149324 ** the Table structures of all FROM-clause subqueries in a
149345 ** * VDBE Cursor numbers are assigned to all FROM-clause terms.
149346 ** * Ephemeral Table objects are created for all FROM-clause subqueries.
149358 assert( p!=0 || pParse->db->mallocFailed );
149359 assert( pParse->db->pParse==pParse );
149360 if( pParse->db->mallocFailed ) return;
149361 if( p->selFlags & SF_HasTypeInfo ) return;
149363 if( pParse->nErr ) return;
149365 if( pParse->nErr ) return;
149376 pAggInfo->selId, pAggInfo);
149377 for(ii=0; ii<pAggInfo->nColumn; ii++){
149378 struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
149380 "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
149382 ii, pCol->pTab ? pCol->pTab->zName : "NULL",
149383 pCol->iTable, pCol->iColumn, pAggInfo->iFirstReg+ii,
149384 pCol->iSorterColumn,
149385 ii>=pAggInfo->nAccumulator ? "" : " Accumulator");
149386 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
149388 for(ii=0; ii<pAggInfo->nFunc; ii++){
149389 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
149390 ii, pAggInfo->iFirstReg+pAggInfo->nColumn+ii);
149391 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
149397 ** Analyze the arguments to aggregate functions. Create new pAggInfo->aCol[]
149419 assert( pAggInfo->iFirstReg==0 );
149420 pNC->ncFlags |= NC_InAggFunc;
149421 for(i=0; i<pAggInfo->nFunc; i++){
149422 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
149423 assert( pExpr->op==TK_FUNCTION || pExpr->op==TK_AGG_FUNCTION );
149425 sqlite3ExprAnalyzeAggList(pNC, pExpr->x.pList);
149426 if( pExpr->pLeft ){
149427 assert( pExpr->pLeft->op==TK_ORDER );
149428 assert( ExprUseXList(pExpr->pLeft) );
149429 sqlite3ExprAnalyzeAggList(pNC, pExpr->pLeft->x.pList);
149434 sqlite3ExprAnalyzeAggregates(pNC, pExpr->y.pWin->pFilter);
149438 pNC->ncFlags &= ~NC_InAggFunc;
149452 NameContext *pNC /* Name context used to resolve agg-func args */
149454 assert( pAggInfo->iFirstReg==0 );
149456 assert( pSelect->pGroupBy!=0 );
149457 pAggInfo->nColumn = pAggInfo->nAccumulator;
149458 if( ALWAYS(pAggInfo->nSortingColumn>0) ){
149459 int mx = pSelect->pGroupBy->nExpr - 1;
149461 for(j=0; j<pAggInfo->nColumn; j++){
149462 k = pAggInfo->aCol[j].iSorterColumn;
149465 pAggInfo->nSortingColumn = mx+1;
149474 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
149475 printf("data-cursor=%d index={%d,%d}\n",
149476 pIEpr->iDataCur, pIEpr->iIdxCur, pIEpr->iIdxCol);
149477 sqlite3TreeViewExpr(0, pIEpr->pExpr, 0);
149494 if( pExpr->pAggInfo==0 ) return WRC_Continue;
149495 if( pExpr->op==TK_AGG_COLUMN ) return WRC_Continue;
149496 if( pExpr->op==TK_AGG_FUNCTION ) return WRC_Continue;
149497 if( pExpr->op==TK_IF_NULL_ROW ) return WRC_Continue;
149498 pAggInfo = pExpr->pAggInfo;
149499 if( NEVER(pExpr->iAgg>=pAggInfo->nColumn) ) return WRC_Continue;
149500 assert( pExpr->iAgg>=0 );
149501 pCol = &pAggInfo->aCol[pExpr->iAgg];
149502 pExpr->op = TK_AGG_COLUMN;
149503 pExpr->iTable = pCol->iTable;
149504 pExpr->iColumn = pCol->iColumn;
149510 ** Convert every pAggInfo->aFunc[].pExpr such that any node within
149519 for(i=0; i<pAggInfo->nFunc; i++){
149520 sqlite3WalkExpr(&w, pAggInfo->aFunc[i].pFExpr);
149527 ** pAggInfo->aCol[] and pAggInfo->aFunc[] entry in pAggInfo. The first
149528 ** register in this block is stored in pAggInfo->iFirstReg.
149544 assert( pAggInfo->iFirstReg==0 );
149545 pAggInfo->iFirstReg = pParse->nMem + 1;
149546 pParse->nMem += pAggInfo->nColumn + pAggInfo->nFunc;
149558 Vdbe *v = pParse->pVdbe;
149561 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
149562 assert( pAggInfo->iFirstReg>0 );
149563 assert( pParse->db->pParse==pParse );
149564 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
149566 if( pParse->nErr ) return;
149567 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->iFirstReg,
149568 pAggInfo->iFirstReg+nReg-1);
149569 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
149570 if( pFunc->iDistinct>=0 ){
149571 Expr *pE = pFunc->pFExpr;
149573 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
149576 pFunc->iDistinct = -1;
149578 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
149579 pFunc->iDistAddr = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
149580 pFunc->iDistinct, 0, 0, (char*)pKeyInfo, P4_KEYINFO);
149581 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)",
149582 pFunc->pFunc->zName));
149585 if( pFunc->iOBTab>=0 ){
149589 assert( pFunc->pFExpr->pLeft!=0 );
149590 assert( pFunc->pFExpr->pLeft->op==TK_ORDER );
149591 assert( ExprUseXList(pFunc->pFExpr->pLeft) );
149592 assert( pFunc->pFunc!=0 );
149593 pOBList = pFunc->pFExpr->pLeft->x.pList;
149594 if( !pFunc->bOBUnique ){
149597 if( pFunc->bOBPayload ){
149599 assert( ExprUseXList(pFunc->pFExpr) );
149600 nExtra += pFunc->pFExpr->x.pList->nExpr;
149602 if( pFunc->bUseSubtype ){
149603 nExtra += pFunc->pFExpr->x.pList->nExpr;
149606 if( !pFunc->bOBUnique && pParse->nErr==0 ){
149607 pKeyInfo->nKeyField++;
149610 pFunc->iOBTab, pOBList->nExpr+nExtra, 0,
149612 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(ORDER BY)",
149613 pFunc->pFunc->zName));
149623 Vdbe *v = pParse->pVdbe;
149626 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
149628 assert( ExprUseXList(pF->pFExpr) );
149629 pList = pF->pFExpr->x.pList;
149630 if( pF->iOBTab>=0 ){
149632 ** were stored in emphermal table pF->iOBTab. Here, we extract those
149641 assert( pF->pFunc!=0 );
149642 nArg = pList->nExpr;
149645 if( pF->bOBPayload==0 ){
149648 assert( pF->pFExpr->pLeft!=0 );
149649 assert( ExprUseXList(pF->pFExpr->pLeft) );
149650 assert( pF->pFExpr->pLeft->x.pList!=0 );
149651 nKey = pF->pFExpr->pLeft->x.pList->nExpr;
149652 if( ALWAYS(!pF->bOBUnique) ) nKey++;
149654 iTop = sqlite3VdbeAddOp1(v, OP_Rewind, pF->iOBTab); VdbeCoverage(v);
149655 for(j=nArg-1; j>=0; j--){
149656 sqlite3VdbeAddOp3(v, OP_Column, pF->iOBTab, nKey+j, regAgg+j);
149658 if( pF->bUseSubtype ){
149660 int iBaseCol = nKey + nArg + (pF->bOBPayload==0 && pF->bOBUnique==0);
149661 for(j=nArg-1; j>=0; j--){
149662 sqlite3VdbeAddOp3(v, OP_Column, pF->iOBTab, iBaseCol+j, regSubtype);
149668 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
149670 sqlite3VdbeAddOp2(v, OP_Next, pF->iOBTab, iTop+1); VdbeCoverage(v);
149675 pList ? pList->nExpr : 0);
149676 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
149684 ** If regAcc is non-zero and there are no min() or max() aggregates
149685 ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
149702 Vdbe *v = pParse->pVdbe;
149709 assert( pAggInfo->iFirstReg>0 );
149710 if( pParse->nErr ) return;
149711 pAggInfo->directMode = 1;
149712 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
149719 assert( ExprUseXList(pF->pFExpr) );
149720 assert( !IsWindowFunc(pF->pFExpr) );
149721 assert( pF->pFunc!=0 );
149722 pList = pF->pFExpr->x.pList;
149723 if( ExprHasProperty(pF->pFExpr, EP_WinFunc) ){
149724 Expr *pFilter = pF->pFExpr->y.pWin->pFilter;
149725 if( pAggInfo->nAccumulator
149726 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
149732 if( regHit==0 ) regHit = ++pParse->nMem;
149745 if( pF->iOBTab>=0 ){
149751 nArg = pList->nExpr;
149753 assert( pF->pFExpr->pLeft!=0 );
149754 assert( pF->pFExpr->pLeft->op==TK_ORDER );
149755 assert( ExprUseXList(pF->pFExpr->pLeft) );
149756 pOBList = pF->pFExpr->pLeft->x.pList;
149758 assert( pOBList->nExpr>0 );
149759 regAggSz = pOBList->nExpr;
149760 if( !pF->bOBUnique ){
149763 if( pF->bOBPayload ){
149766 if( pF->bUseSubtype ){
149773 jj = pOBList->nExpr;
149774 if( !pF->bOBUnique ){
149775 sqlite3VdbeAddOp2(v, OP_Sequence, pF->iOBTab, regAgg+jj);
149778 if( pF->bOBPayload ){
149783 if( pF->bUseSubtype ){
149785 int regBase = pF->bOBPayload ? regDistinct : regAgg;
149791 nArg = pList->nExpr;
149799 if( pF->iDistinct>=0 && pList ){
149803 pF->iDistinct = codeDistinct(pParse, eDistinctType,
149804 pF->iDistinct, addrNext, pList, regDistinct);
149806 if( pF->iOBTab>=0 ){
149808 sqlite3VdbeAddOp3(v, OP_MakeRecord, regAgg, regAggSz-1,
149809 regAgg+regAggSz-1);
149810 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pF->iOBTab, regAgg+regAggSz-1,
149811 regAgg, regAggSz-1);
149815 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
149819 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
149820 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
149821 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
149824 pColl = pParse->db->pDfltColl;
149826 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
149831 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
149839 if( regHit==0 && pAggInfo->nAccumulator ){
149845 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
149846 sqlite3ExprCode(pParse, pC->pCExpr, AggInfoColumnReg(pAggInfo,i));
149849 pAggInfo->directMode = 0;
149865 if( pParse->explain==2 ){
149868 pTab->zName,
149870 bCover ? pIdx->zName : ""
149885 ** sub-expression matches the criteria for being moved to the WHERE
149886 ** clause. If so, add it to the WHERE clause and replace the sub-expression
149890 if( pExpr->op!=TK_AND ){
149891 Select *pS = pWalker->u.pSelect;
149893 ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set
149899 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
149901 && pExpr->pAggInfo==0
149903 sqlite3 *db = pWalker->pParse->db;
149906 Expr *pWhere = pS->pWhere;
149908 pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
149909 pS->pWhere = pNew;
149910 pWalker->eCode = 1;
149939 sqlite3WalkExpr(&sWalker, p->pHaving);
149949 ** Check to see if the pThis entry of pTabList is a self-join of another view.
149950 ** Search FROM-clause entries in the range of iFirst..iEnd, including iFirst
149953 ** If pThis is a self-join, then return the SrcItem for the first other
149954 ** instance of that view found. If pThis is not a self-join then return 0.
149957 SrcList *pTabList, /* Search for self-joins in this FROM clause */
149959 int iFirst, int iEnd /* Range of FROM-clause entries to search. */
149962 assert( pThis->pSelect!=0 );
149963 if( pThis->pSelect->selFlags & SF_PushDown ) return 0;
149966 pItem = &pTabList->a[iFirst++];
149967 if( pItem->pSelect==0 ) continue;
149968 if( pItem->fg.viaCoroutine ) continue;
149969 if( pItem->zName==0 ) continue;
149970 assert( pItem->pTab!=0 );
149971 assert( pThis->pTab!=0 );
149972 if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
149973 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
149974 pS1 = pItem->pSelect;
149975 if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
149980 if( pItem->pSelect->selFlags & SF_PushDown ){
149995 sqlite3DbFree(db, p->aCol);
149996 sqlite3DbFree(db, p->aFunc);
150024 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
150025 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
150026 if( p->pWhere ) return 0;
150027 if( p->pHaving ) return 0;
150028 if( p->pGroupBy ) return 0;
150029 if( p->pOrderBy ) return 0;
150030 pExpr = p->pEList->a[0].pExpr;
150031 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
150033 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
150035 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
150036 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
150038 pSub = p->pSrc->a[0].pSelect;
150040 if( pSub->pPrior==0 ) return 0; /* Must be a compound */
150041 if( pSub->selFlags & SF_CopyCte ) return 0; /* Not a CTE */
150043 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
150044 if( pSub->pWhere ) return 0; /* No WHERE clause */
150045 if( pSub->pLimit ) return 0; /* No LIMIT clause */
150046 if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
150047 assert( pSub->pHaving==0 ); /* Due to the previous */
150048 pSub = pSub->pPrior; /* Repeat over compound */
150053 db = pParse->db;
150056 pSub = p->pSrc->a[0].pSelect;
150057 p->pSrc->a[0].pSelect = 0;
150058 sqlite3SrcListDelete(db, p->pSrc);
150059 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
150062 pPrior = pSub->pPrior;
150063 pSub->pPrior = 0;
150064 pSub->pNext = 0;
150065 pSub->selFlags |= SF_Aggregate;
150066 pSub->selFlags &= ~SF_Compound;
150067 pSub->nSelectRow = 0;
150068 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pSub->pEList);
150070 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
150080 p->pEList->a[0].pExpr = pExpr;
150081 p->selFlags &= ~SF_Aggregate;
150085 TREETRACE(0x200,pParse,p,("After count-of-view optimization:\n"));
150093 ** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same
150099 for(i=0; i<pSrc->nSrc; i++){
150100 SrcItem *p1 = &pSrc->a[i];
150102 if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
150105 if( p1->pSelect
150106 && (p1->pSelect->selFlags & SF_NestedFrom)!=0
150107 && sameSrcAlias(p0, p1->pSelect->pSrc)
150116 ** Return TRUE (non-zero) if the i-th entry in the pTabList SrcList can
150117 ** be implemented as a co-routine. The i-th entry is guaranteed to be
150120 ** The subquery is implemented as a co-routine if all of the following are
150127 ** (b) The subquery is the left-most term and a CROSS JOIN or similar
150130 ** (i) The subquery is the left-most subquery in the FROM clause
150141 ** (5) The subquery is not self-joined
150149 SrcItem *pItem = &pTabList->a[i];
150150 if( pItem->fg.isCte ){
150151 const CteUse *pCteUse = pItem->u2.pCteUse;
150152 if( pCteUse->eM10d==M10d_Yes ) return 0; /* (2a) */
150153 if( pCteUse->nUse>=2 && pCteUse->eM10d!=M10d_No ) return 0; /* (2b) */
150155 if( pTabList->a[0].fg.jointype & JT_LTORJ ) return 0; /* (3) */
150156 if( OptimizationDisabled(pParse->db, SQLITE_Coroutines) ) return 0; /* (4) */
150157 if( isSelfJoinView(pTabList, pItem, i+1, pTabList->nSrc)!=0 ){
150161 if( pTabList->nSrc==1 ) return 1; /* (1a) */
150162 if( pTabList->a[1].fg.jointype & JT_CROSS ) return 1; /* (1b) */
150163 if( selFlags & SF_UpdateFrom ) return 0; /* (1c-iii) */
150166 if( selFlags & SF_UpdateFrom ) return 0; /* (1c-iii) */
150167 while( 1 /*exit-by-break*/ ){
150168 if( pItem->fg.jointype & (JT_OUTER|JT_CROSS) ) return 0; /* (1c-ii) */
150170 i--;
150171 pItem--;
150172 if( pItem->pSelect!=0 ) return 0; /* (1c-i) */
150185 ** pParse->zErrMsg.
150213 db = pParse->db;
150214 assert( pParse==db->pParse );
150216 if( p==0 || pParse->nErr ){
150219 assert( db->mallocFailed==0 );
150222 TREETRACE(0x1,pParse,p, ("begin processing:\n", pParse->addrExplain));
150232 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
150233 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
150234 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
150235 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
150237 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
150238 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
150239 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
150241 if( p->pOrderBy ){
150245 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
150249 p->pOrderBy);
150250 testcase( pParse->earlyCleanup );
150251 p->pOrderBy = 0;
150253 p->selFlags &= ~SF_Distinct;
150254 p->selFlags |= SF_NoopOrderBy;
150257 if( pParse->nErr ){
150260 assert( db->mallocFailed==0 );
150261 assert( p->pEList!=0 );
150271 ** In this case, it is an error if the target object (pSrc->a[0]) name
150272 ** or alias is duplicated within FROM clause (pSrc->a[1..n]).
150278 if( p->selFlags & SF_UFSrcCheck ){
150279 SrcItem *p0 = &p->pSrc->a[0];
150280 if( sameSrcAlias(p0, p->pSrc) ){
150283 p0->zAlias ? p0->zAlias : p0->pTab->zName
150289 ** and leaving this flag set can cause errors if a compound sub-query
150290 ** in p->pSrc is flattened into this query and this function called
150292 p->selFlags &= ~SF_UFSrcCheck;
150295 if( pDest->eDest==SRT_Output ){
150301 assert( pParse->nErr );
150305 if( p->pWin && (sqlite3TreeTrace & 0x40)!=0 ){
150311 pTabList = p->pSrc;
150312 isAgg = (p->selFlags & SF_Aggregate)!=0;
150314 sSort.pOrderBy = p->pOrderBy;
150320 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
150321 SrcItem *pItem = &pTabList->a[i];
150322 Select *pSub = pItem->pSelect;
150323 Table *pTab = pItem->pTab;
150332 ** LEFT JOIN -> JOIN
150333 ** RIGHT JOIN -> JOIN
150334 ** FULL JOIN -> RIGHT JOIN
150336 ** If terms of the i-th table are used in the WHERE clause in such a
150337 ** way that the i-th table cannot be the NULL row of a join, then
150341 if( (pItem->fg.jointype & (JT_LEFT|JT_LTORJ))!=0
150342 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor,
150343 pItem->fg.jointype & JT_LTORJ)
150346 if( pItem->fg.jointype & JT_LEFT ){
150347 if( pItem->fg.jointype & JT_RIGHT ){
150349 ("FULL-JOIN simplifies to RIGHT-JOIN on term %d\n",i));
150350 pItem->fg.jointype &= ~JT_LEFT;
150353 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
150354 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
150355 unsetJoinExpr(p->pWhere, pItem->iCursor, 0);
150358 if( pItem->fg.jointype & JT_LTORJ ){
150359 for(j=i+1; j<pTabList->nSrc; j++){
150360 SrcItem *pI2 = &pTabList->a[j];
150361 if( pI2->fg.jointype & JT_RIGHT ){
150362 if( pI2->fg.jointype & JT_LEFT ){
150364 ("FULL-JOIN simplifies to LEFT-JOIN on term %d\n",j));
150365 pI2->fg.jointype &= ~JT_RIGHT;
150368 ("RIGHT-JOIN simplifies to JOIN on term %d\n",j));
150369 pI2->fg.jointype &= ~(JT_RIGHT|JT_OUTER);
150370 unsetJoinExpr(p->pWhere, pI2->iCursor, 1);
150374 for(j=pTabList->nSrc-1; j>=0; j--){
150375 pTabList->a[j].fg.jointype &= ~JT_LTORJ;
150376 if( pTabList->a[j].fg.jointype & JT_RIGHT ) break;
150386 if( pTab->nCol!=pSub->pEList->nExpr ){
150388 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
150396 if( pItem->fg.isCte && pItem->u2.pCteUse->eM10d==M10d_Yes ){
150404 ** will be implemented as a co-routine and there is no advantage to
150407 if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
150408 assert( pSub->pGroupBy==0 );
150410 /* If a FROM-clause subquery has an ORDER BY clause that is not
150419 ** (2) The subquery was added to help with window-function
150423 ** the built-in count(), min(), or max().
150432 if( pSub->pOrderBy!=0
150433 && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */
150434 && pSub->pLimit==0 /* Condition (1) */
150435 && (pSub->selFlags & SF_OrderByReqd)==0 /* Condition (2) */
150436 && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */
150440 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
150442 pSub->pOrderBy);
150443 pSub->pOrderBy = 0;
150449 ** it will be implemented as a co-routine, then do not flatten. This
150464 if( pSub->pOrderBy!=0
150466 && (p->selFlags & SF_ComplexResult)!=0
150467 && (pTabList->nSrc==1
150468 || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0)
150474 if( pParse->nErr ) goto select_end;
150476 i = -1;
150478 pTabList = p->pSrc;
150479 if( db->mallocFailed ) goto select_end;
150481 sSort.pOrderBy = p->pOrderBy;
150490 if( p->pPrior ){
150493 TREETRACE(0x400,pParse,p,("end compound-select processing\n"));
150498 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
150503 /* Do the WHERE-clause constant propagation optimization if this is
150504 ** a join. No need to speed time on this operation for non-join queries
150508 if( p->pWhere!=0
150509 && p->pWhere->op==TK_AND
150526 if( db->mallocFailed ) goto select_end;
150527 pTabList = p->pSrc;
150532 ** (2) Generate code for all sub-queries
150534 for(i=0; i<pTabList->nSrc; i++){
150535 SrcItem *pItem = &pTabList->a[i];
150548 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
150549 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
150556 ** assume the column name is non-NULL and segfault. The use of an empty
150559 if( pItem->colUsed==0 && pItem->zName!=0 ){
150560 sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
150564 /* Generate code for all sub-queries in the FROM clause
150566 pSub = pItem->pSelect;
150567 if( pSub==0 || pItem->addrFillSub!=0 ) continue;
150570 assert( pItem->addrFillSub==0 );
150575 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
150579 pParse->nHeight += sqlite3SelectExprHeight(p);
150581 /* Make copies of constant WHERE-clause terms in the outer query down
150585 && (pItem->fg.isCte==0
150586 || (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2))
150587 && pushDownWhereTerms(pParse, pSub, p->pWhere, pTabList, i)
150592 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
150596 assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 );
150598 TREETRACE(0x4000,pParse,p,("WHERE-lcause push-down not possible\n"));
150611 pSub->selId));
150617 zSavedAuthContext = pParse->zAuthContext;
150618 pParse->zAuthContext = pItem->zName;
150622 if( fromClauseTermCanBeCoroutine(pParse, pTabList, i, p->selFlags) ){
150623 /* Implement a co-routine that will return a single row of the result
150628 pItem->regReturn = ++pParse->nMem;
150629 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
150631 pItem->addrFillSub = addrTop;
150632 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
150633 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %!S", pItem));
150635 pItem->pTab->nRowLogEst = pSub->nSelectRow;
150636 pItem->fg.viaCoroutine = 1;
150637 pItem->regResult = dest.iSdst;
150638 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
150639 sqlite3VdbeJumpHere(v, addrTop-1);
150641 }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){
150644 ** the make the pItem->iCursor be a copy of the ephemeral table that
150646 CteUse *pCteUse = pItem->u2.pCteUse;
150647 sqlite3VdbeAddOp2(v, OP_Gosub, pCteUse->regRtn, pCteUse->addrM9e);
150648 if( pItem->iCursor!=pCteUse->iCur ){
150649 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pCteUse->iCur);
150652 pSub->nSelectRow = pCteUse->nRowEst;
150656 if( pPrior->addrFillSub ){
150657 sqlite3VdbeAddOp2(v, OP_Gosub, pPrior->regReturn, pPrior->addrFillSub);
150659 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
150660 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
150671 pItem->regReturn = ++pParse->nMem;
150673 pItem->addrFillSub = topAddr+1;
150674 pItem->fg.isMaterialized = 1;
150675 if( pItem->fg.isCorrelated==0 ){
150684 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
150688 pItem->pTab->nRowLogEst = pSub->nSelectRow;
150690 sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1);
150692 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
150695 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
150696 CteUse *pCteUse = pItem->u2.pCteUse;
150697 pCteUse->addrM9e = pItem->addrFillSub;
150698 pCteUse->regRtn = pItem->regReturn;
150699 pCteUse->iCur = pItem->iCursor;
150700 pCteUse->nRowEst = pSub->nSelectRow;
150703 if( db->mallocFailed ) goto select_end;
150704 pParse->nHeight -= sqlite3SelectExprHeight(p);
150705 pParse->zAuthContext = zSavedAuthContext;
150711 pEList = p->pEList;
150712 pWhere = p->pWhere;
150713 pGroupBy = p->pGroupBy;
150714 pHaving = p->pHaving;
150715 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
150719 TREETRACE(0x8000,pParse,p,("After all FROM-clause analysis:\n"));
150725 ** if the select-list is the same as the ORDER BY list, then this query
150734 ** The second form is preferred as a single index (or temp-table) may be
150736 ** written the query must use a temp-table for at least one of the ORDER
150737 ** BY and DISTINCT, and an index or separate temp-table for the other.
150739 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
150740 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
150742 && p->pWin==0
150745 p->selFlags &= ~SF_Distinct;
150746 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
150747 p->selFlags |= SF_Aggregate;
150748 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
150764 ** being unused if the data can be extracted in pre-sorted order.
150773 pParse, sSort.pOrderBy, 0, pEList->nExpr);
150774 sSort.iECursor = pParse->nTab++;
150777 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
150781 sSort.addrSortIndex = -1;
150786 if( pDest->eDest==SRT_EphemTab ){
150787 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
150788 if( p->selFlags & SF_NestedFrom ){
150789 /* Delete or NULL-out result columns that will never be used */
150791 for(ii=pEList->nExpr-1; ii>0 && pEList->a[ii].fg.bUsed==0; ii--){
150792 sqlite3ExprDelete(db, pEList->a[ii].pExpr);
150793 sqlite3DbFree(db, pEList->a[ii].zEName);
150794 pEList->nExpr--;
150796 for(ii=0; ii<pEList->nExpr; ii++){
150797 if( pEList->a[ii].fg.bUsed==0 ) pEList->a[ii].pExpr->op = TK_NULL;
150805 if( (p->selFlags & SF_FixedLimit)==0 ){
150806 p->nSelectRow = 320; /* 4 billion rows */
150808 if( p->pLimit ) computeLimitRegisters(pParse, p, iEnd);
150809 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
150816 if( p->selFlags & SF_Distinct ){
150817 sDistinct.tabTnct = pParse->nTab++;
150820 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
150831 | (p->selFlags & SF_FixedLimit);
150833 Window *pWin = p->pWin; /* Main window object (or NULL) */
150844 p->pEList, p, wctrlFlags, p->nSelectRow);
150846 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
150847 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
150855 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
150869 assert( p->pEList==pEList );
150875 int regGosub = ++pParse->nMem;
150881 VdbeNoopComment((v, "inner-loop subroutine"));
150883 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
150886 VdbeComment((v, "end inner-loop subroutine"));
150892 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
150924 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
150925 pItem->u.x.iAlias = 0;
150927 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
150928 pItem->u.x.iAlias = 0;
150931 if( p->nSelectRow>66 ) p->nSelectRow = 66;
150936 ** in the correct order. It also may not - the GROUP BY might use a
150941 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
150944 ** ASC or DESC order - only that each group is returned contiguously.
150948 for(ii=0; ii<pGroupBy->nExpr; ii++){
150950 sortFlags = sSort.pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_DESC;
150951 pGroupBy->a[ii].fg.sortFlags = sortFlags;
150953 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
150959 p->nSelectRow = 0;
150972 testcase( pParse->earlyCleanup );
150974 if( db->mallocFailed ){
150977 pAggInfo->selId = p->selId;
150979 pAggInfo->pSelect = p;
150986 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
150987 pAggInfo->pGroupBy = pGroupBy;
150992 assert( pWhere==p->pWhere );
150993 assert( pHaving==p->pHaving );
150994 assert( pGroupBy==p->pGroupBy );
150996 pWhere = p->pWhere;
151000 pAggInfo->nAccumulator = pAggInfo->nColumn;
151001 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
151002 minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
151007 if( db->mallocFailed ) goto select_end;
151026 int addr1; /* A-vs-B comparison jump */
151038 if( pAggInfo->nFunc==1
151039 && pAggInfo->aFunc[0].iDistinct>=0
151040 && ALWAYS(pAggInfo->aFunc[0].pFExpr!=0)
151041 && ALWAYS(ExprUseXList(pAggInfo->aFunc[0].pFExpr))
151042 && pAggInfo->aFunc[0].pFExpr->x.pList!=0
151044 Expr *pExpr = pAggInfo->aFunc[0].pFExpr->x.pList->a[0].pExpr;
151056 pAggInfo->sortingIdx = pParse->nTab++;
151058 0, pAggInfo->nColumn);
151060 pAggInfo->sortingIdx, pAggInfo->nSortingColumn,
151065 iUseFlag = ++pParse->nMem;
151066 iAbortFlag = ++pParse->nMem;
151067 regOutputRow = ++pParse->nMem;
151069 regReset = ++pParse->nMem;
151071 iAMem = pParse->nMem + 1;
151072 pParse->nMem += pGroupBy->nExpr;
151073 iBMem = pParse->nMem + 1;
151074 pParse->nMem += pGroupBy->nExpr;
151077 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
151094 if( pParse->pIdxEpr ){
151100 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
151120 ExplainQueryPlan2(addrExp, (pParse, 0, "USE TEMP B-TREE FOR %s",
151121 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
151126 nGroupBy = pGroupBy->nExpr;
151129 for(i=0; i<pAggInfo->nColumn; i++){
151130 if( pAggInfo->aCol[i].iSorterColumn>=j ){
151138 pAggInfo->directMode = 1;
151139 for(i=0; i<pAggInfo->nColumn; i++){
151140 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
151141 if( pCol->iSorterColumn>=j ){
151142 sqlite3ExprCode(pParse, pCol->pCExpr, j + regBase);
151146 pAggInfo->directMode = 0;
151150 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
151151 sqlite3VdbeScanStatusRange(v, addrExp, sqlite3VdbeCurrentAddr(v)-2, -1);
151156 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
151160 sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
151162 pAggInfo->useSortingIdx = 1;
151163 sqlite3VdbeScanStatusRange(v, addrExp, -1, sortPTab);
151164 sqlite3VdbeScanStatusRange(v, addrExp, -1, pAggInfo->sortingIdx);
151167 /* If there are entries in pAgggInfo->aFunc[] that contain subexpressions
151172 if( pParse->pIdxEpr ){
151188 ** This is an optimization - the correct answer should result regardless.
151205 sqlite3VdbeAddOp3(v, OP_SorterData, pAggInfo->sortingIdx,
151208 for(j=0; j<pGroupBy->nExpr; j++){
151212 pAggInfo->directMode = 1;
151213 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
151216 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
151230 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
151249 sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop);
151269 ** is less than or equal to zero, the subroutine is a no-op. If
151286 selectInnerLoop(pParse, p, -1, &sSort,
151292 /* Generate a subroutine that will reset the group-by accumulator
151301 struct AggInfo_func *pF = &pAggInfo->aFunc[0];
151302 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
151321 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
151322 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
151326 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
151329 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
151333 ** (2011-04-15) Do not do a full scan of an unordered index.
151335 ** (2013-10-03) Do not count the entries in a partial index.
151341 if( !p->pSrc->a[0].fg.notIndexed ){
151342 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
151343 if( pIdx->bUnordered==0
151344 && pIdx->szIdxRow<pTab->szTabRow
151345 && pIdx->pPartIdxWhere==0
151346 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
151353 iRoot = pBest->tnum;
151357 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
151360 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
151381 if( pAggInfo->nAccumulator ){
151382 for(i=0; i<pAggInfo->nFunc; i++){
151383 if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc) ){
151386 if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ){
151390 if( i==pAggInfo->nFunc ){
151391 regAcc = ++pParse->nMem;
151394 }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
151395 assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) );
151396 pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
151405 assert( p->pGroupBy==0 );
151414 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
151426 struct AggInfo_func *pF = pAggInfo->aFunc;
151428 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
151443 selectInnerLoop(pParse, p, -1, 0, 0,
151458 assert( p->pEList==pEList );
151459 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
151468 rc = (pParse->nErr>0);
151474 assert( db->mallocFailed==0 || db->mallocFailed==1 );
151475 assert( db->mallocFailed==0 || pParse->nErr!=0 );
151478 if( pAggInfo && !db->mallocFailed ){
151485 for(i=0; i<pAggInfo->nColumn; i++){
151486 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
151488 assert( pExpr->pAggInfo==pAggInfo );
151489 assert( pExpr->iAgg==i );
151491 for(i=0; i<pAggInfo->nFunc; i++){
151492 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
151494 assert( pExpr->pAggInfo==pAggInfo );
151495 assert( pExpr->iAgg==i );
151555 int need; /* Slots needed in p->azResult[] */
151559 /* Make sure there is enough space in p->azResult to hold everything
151562 if( p->nRow==0 && argv!=0 ){
151567 if( p->nData + need > p->nAlloc ){
151569 p->nAlloc = p->nAlloc*2 + need;
151570 azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
151572 p->azResult = azNew;
151578 if( p->nRow==0 ){
151579 p->nColumn = nCol;
151583 p->azResult[p->nData++] = z;
151585 }else if( (int)p->nColumn!=nCol ){
151586 sqlite3_free(p->zErrMsg);
151587 p->zErrMsg = sqlite3_mprintf(
151590 p->rc = SQLITE_ERROR;
151606 p->azResult[p->nData++] = z;
151608 p->nRow++;
151613 p->rc = SQLITE_NOMEM_BKPT;
151653 db->errCode = SQLITE_NOMEM;
151669 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
151682 db->errCode = SQLITE_NOMEM;
151701 azResult--;
151734 pTriggerStep = pTriggerStep->pNext;
151736 sqlite3ExprDelete(db, pTmp->pWhere);
151737 sqlite3ExprListDelete(db, pTmp->pExprList);
151738 sqlite3SelectDelete(db, pTmp->pSelect);
151739 sqlite3IdListDelete(db, pTmp->pIdList);
151740 sqlite3UpsertDelete(db, pTmp->pUpsert);
151741 sqlite3SrcListDelete(db, pTmp->pFrom);
151742 sqlite3DbFree(db, pTmp->zSpan);
151753 ** are already attached to pTab->pTrigger. But there might be additional
151755 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
151760 ** pTab as well as the triggers lised in pTab->pTrigger.
151767 assert( pParse->disableTriggers==0 );
151768 pTmpSchema = pParse->db->aDb[1].pSchema;
151769 p = sqliteHashFirst(&pTmpSchema->trigHash);
151770 pList = pTab->pTrigger;
151773 if( pTrig->pTabSchema==pTab->pSchema
151774 && pTrig->table
151775 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
151776 && (pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning)
151778 pTrig->pNext = pList;
151780 }else if( pTrig->op==TK_RETURNING ){
151782 assert( pParse->db->pVtabCtx==0 );
151784 assert( pParse->bReturning );
151785 assert( &(pParse->u1.pReturning->retTrig) == pTrig );
151786 pTrig->table = pTab->zName;
151787 pTrig->pTabSchema = pTab->pSchema;
151788 pTrig->pNext = pList;
151796 printf("Triggers for %s:", pTab->zName);
151797 for(pX=pList; pX; pX=pX->pNext){
151798 printf(" %s", pX->zName);
151811 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
151830 sqlite3 *db = pParse->db; /* The database connection */
151835 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
151841 if( pName2->n>0 ){
151854 if( !pTableName || db->mallocFailed ){
151858 /* A long-standing parser bug is that this syntax was allowed:
151866 if( db->init.busy && iDb!=1 ){
151867 sqlite3DbFree(db, pTableName->a[0].zDatabase);
151868 pTableName->a[0].zDatabase = 0;
151877 if( db->init.busy==0 && pName2->n==0 && pTab
151878 && pTab->pSchema==db->aDb[1].pSchema ){
151883 if( db->mallocFailed ) goto trigger_cleanup;
151884 assert( pTableName->nSrc==1 );
151898 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
151907 assert( db->mallocFailed );
151910 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
151915 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
151919 assert( !db->init.busy );
151921 VVA_ONLY( pParse->ifNotExists = 1; )
151928 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
151938 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
151943 " trigger on table: %S", pTableName->a);
151949 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
151951 const char *zDb = db->aDb[iTabDb].zDbSName;
151952 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
151954 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
151975 pTrigger->zName = zName;
151977 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
151978 pTrigger->pSchema = db->aDb[iDb].pSchema;
151979 pTrigger->pTabSchema = pTab->pSchema;
151980 pTrigger->op = (u8)op;
151981 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
151983 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
151984 pTrigger->pWhen = pWhen;
151987 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
151989 pTrigger->pColumns = pColumns;
151991 assert( pParse->pNewTrigger==0 );
151992 pParse->pNewTrigger = pTrigger;
151999 if( !pParse->pNewTrigger ){
152002 assert( pParse->pNewTrigger==pTrigger );
152007 if( db->init.iDb==1 ){
152010 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
152014 ** "orphaned trigger" - a trigger whose associated table is missing.
152016 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
152018 db->init.orphanTrigger = 1;
152032 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
152034 sqlite3 *db = pParse->db; /* The database */
152039 pParse->pNewTrigger = 0;
152040 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
152041 zName = pTrig->zName;
152042 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
152043 pTrig->step_list = pStepList;
152045 pStepList->pTrig = pTrig;
152046 pStepList = pStepList->pNext;
152048 sqlite3TokenInit(&nameToken, pTrig->zName);
152050 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
152051 || sqlite3FixExpr(&sFix, pTrig->pWhen)
152058 assert( !db->init.busy );
152059 pParse->pNewTrigger = pTrig;
152067 if( !db->init.busy ){
152072 ** are read-only, and the trigger makes a change to a shadow table,
152073 ** then raise an error - do not allow the trigger to be created. */
152076 for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){
152077 if( pStep->zTarget!=0
152078 && sqlite3ShadowTableName(db, pStep->zTarget)
152082 pTrig->zName, pStep->zTarget);
152092 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
152097 db->aDb[iDb].zDbSName, zName,
152098 pTrig->table, z);
152105 if( db->init.busy ){
152107 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
152113 }else if( pLink->pSchema==pLink->pTabSchema ){
152115 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
152117 pLink->pNext = pTab->pTrigger;
152118 pTab->pTrigger = pLink;
152124 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
152157 pTriggerStep->op = TK_SELECT;
152158 pTriggerStep->pSelect = pSelect;
152159 pTriggerStep->orconf = OE_Default;
152160 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
152168 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
152177 sqlite3 *db = pParse->db;
152180 if( pParse->nErr ) return 0;
152181 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
152184 memcpy(z, pName->z, pName->n);
152186 pTriggerStep->zTarget = z;
152187 pTriggerStep->op = op;
152188 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
152190 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
152213 sqlite3 *db = pParse->db;
152216 assert(pSelect != 0 || db->mallocFailed);
152221 pTriggerStep->pSelect = pSelect;
152224 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
152226 pTriggerStep->pIdList = pColumn;
152227 pTriggerStep->pUpsert = pUpsert;
152228 pTriggerStep->orconf = orconf;
152230 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
152251 SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */
152258 sqlite3 *db = pParse->db;
152264 pTriggerStep->pExprList = pEList;
152265 pTriggerStep->pWhere = pWhere;
152266 pTriggerStep->pFrom = pFrom;
152271 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
152272 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
152273 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
152275 pTriggerStep->orconf = orconf;
152295 sqlite3 *db = pParse->db;
152301 pTriggerStep->pWhere = pWhere;
152304 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
152306 pTriggerStep->orconf = OE_Default;
152316 if( pTrigger==0 || pTrigger->bReturning ) return;
152317 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
152318 sqlite3DbFree(db, pTrigger->zName);
152319 sqlite3DbFree(db, pTrigger->table);
152320 sqlite3ExprDelete(db, pTrigger->pWhen);
152321 sqlite3IdListDelete(db, pTrigger->pColumns);
152338 sqlite3 *db = pParse->db;
152340 if( db->mallocFailed ) goto drop_trigger_cleanup;
152345 assert( pName->nSrc==1 );
152346 zDb = pName->a[0].zDatabase;
152347 zName = pName->a[0].zName;
152349 for(i=OMIT_TEMPDB; i<db->nDb; i++){
152353 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
152358 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
152362 pParse->checkSchema = 1;
152376 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
152386 sqlite3 *db = pParse->db;
152389 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
152390 assert( iDb>=0 && iDb<db->nDb );
152392 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
152396 const char *zDb = db->aDb[iDb].zDbSName;
152399 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
152411 db->aDb[iDb].zDbSName, pTrigger->zName
152414 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
152426 pHash = &(db->aDb[iDb].pSchema->trigHash);
152429 if( pTrigger->pSchema==pTrigger->pTabSchema ){
152433 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
152435 *pp = (*pp)->pNext;
152442 db->mDbFlags |= DBFLAG_SchemaChange;
152458 for(e=0; e<pEList->nExpr; e++){
152459 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
152468 if( NEVER(db->aDb[1].pSchema==0) ) return 0;
152469 if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)==0 ) return 0;
152492 || (pList->bReturning && pList->pNext==0) );
152495 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
152496 && pTab->pTrigger!=0
152501 if( pList==pTab->pTrigger ){
152505 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
152506 p->pNext = 0;
152510 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
152511 mask |= p->tr_tm;
152512 }else if( p->op==TK_RETURNING ){
152516 p->op = op;
152523 p->tr_tm = TRIGGER_BEFORE;
152525 p->tr_tm = TRIGGER_AFTER;
152527 mask |= p->tr_tm;
152528 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
152531 mask |= p->tr_tm;
152533 p = p->pNext;
152550 if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db))
152551 || pParse->disableTriggers
152560 ** Convert the pStep->zTarget string into a SrcList and return a pointer
152573 sqlite3 *db = pParse->db;
152575 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
152577 assert( pSrc==0 || pSrc->nSrc==1 );
152580 Schema *pSchema = pStep->pTrig->pSchema;
152581 pSrc->a[0].zName = zName;
152582 if( pSchema!=db->aDb[1].pSchema ){
152583 pSrc->a[0].pSchema = pSchema;
152585 if( pStep->pFrom ){
152586 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
152587 if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT ){
152613 if( pTerm->op==TK_ASTERISK ) return 1;
152614 if( pTerm->op!=TK_DOT ) return 0;
152615 assert( pTerm->pRight!=0 );
152616 assert( pTerm->pLeft!=0 );
152617 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
152634 sqlite3 *db = pParse->db;
152637 for(i=0; i<pList->nExpr; i++){
152638 Expr *pOldExpr = pList->a[i].pExpr;
152642 for(jj=0; jj<pTab->nCol; jj++){
152644 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
152645 pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zCnName);
152647 if( !db->mallocFailed ){
152648 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
152649 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName);
152650 pItem->fg.eEName = ENAME_NAME;
152656 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
152657 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
152658 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
152659 pItem->fg.eEName = pList->a[i].fg.eEName;
152673 && (pExpr->x.pSelect->selFlags & SF_Correlated)!=0
152683 ** If the SELECT references the table pWalker->u.pTab, then do two things:
152686 ** (2) Set pWalker->eCode to non-zero so that the caller will know
152693 pSrc = pSelect->pSrc;
152695 for(i=0; i<pSrc->nSrc; i++){
152696 if( pSrc->a[i].pTab==pWalker->u.pTab ){
152697 testcase( pSelect->selFlags & SF_Correlated );
152698 pSelect->selFlags |= SF_Correlated;
152699 pWalker->eCode = 1;
152735 ** is generated in-line.
152743 Vdbe *v = pParse->pVdbe;
152744 sqlite3 *db = pParse->db;
152751 if( !pParse->bReturning ){
152756 assert( db->pParse==pParse );
152757 pReturning = pParse->u1.pReturning;
152758 if( pTrigger != &(pReturning->retTrig) ){
152764 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
152768 sFrom.a[0].zName = pTab->zName; /* tag-20240424-1 */
152769 sFrom.a[0].iCursor = -1;
152771 if( pParse->nErr==0 ){
152772 assert( db->mallocFailed==0 );
152776 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
152777 if( pParse->nErr==0 ){
152780 if( pReturning->nRetCol==0 ){
152781 pReturning->nRetCol = pNew->nExpr;
152782 pReturning->iRetCur = pParse->nTab++;
152787 pParse->eTriggerOp = pTrigger->op;
152788 pParse->pTriggerTab = pTab;
152790 && ALWAYS(!db->mallocFailed)
152793 int nCol = pNew->nExpr;
152794 int reg = pParse->nMem+1;
152796 pParse->nMem += nCol+2;
152797 pReturning->iRetReg = reg;
152799 Expr *pCol = pNew->a[i].pExpr;
152800 assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
152807 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
152808 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
152812 pParse->eTriggerOp = 0;
152813 pParse->pTriggerTab = 0;
152828 Vdbe *v = pParse->pVdbe;
152829 sqlite3 *db = pParse->db;
152831 assert( pParse->pTriggerTab && pParse->pToplevel );
152834 for(pStep=pStepList; pStep; pStep=pStep->pNext){
152845 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
152846 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
152848 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
152849 assert( pParse->okConstFactor==0 );
152852 if( pStep->zSpan ){
152854 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
152859 switch( pStep->op ){
152863 sqlite3ExprListDup(db, pStep->pExprList, 0),
152864 sqlite3ExprDup(db, pStep->pWhere, 0),
152865 pParse->eOrconf, 0, 0, 0
152873 sqlite3SelectDup(db, pStep->pSelect, 0),
152874 sqlite3IdListDup(db, pStep->pIdList),
152875 pParse->eOrconf,
152876 sqlite3UpsertDup(db, pStep->pUpsert)
152884 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
152889 default: assert( pStep->op==TK_SELECT ); {
152891 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
152922 ** Parse context structure pFrom has just been used to create a sub-vdbe
152927 assert( pFrom->zErrMsg==0 || pFrom->nErr );
152928 assert( pTo->zErrMsg==0 || pTo->nErr );
152929 if( pTo->nErr==0 ){
152930 pTo->zErrMsg = pFrom->zErrMsg;
152931 pTo->nErr = pFrom->nErr;
152932 pTo->rc = pFrom->rc;
152934 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
152939 ** Create and populate a new TriggerPrg object with a sub-program
152949 sqlite3 *db = pParse->db; /* Database handle */
152953 NameContext sNC; /* Name context for sub-vdbe */
152954 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
152956 Parse sSubParse; /* Parse context for sub-vdbe */
152958 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
152959 assert( pTop->pVdbe );
152963 ** list of the top-level Parse object sooner rather than later. */
152966 pPrg->pNext = pTop->pTriggerPrg;
152967 pTop->pTriggerPrg = pPrg;
152968 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
152970 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
152971 pPrg->pTrigger = pTrigger;
152972 pPrg->orconf = orconf;
152973 pPrg->aColmask[0] = 0xffffffff;
152974 pPrg->aColmask[1] = 0xffffffff;
152977 ** trigger sub-program. */
152983 sSubParse.zAuthContext = pTrigger->zName;
152984 sSubParse.eTriggerOp = pTrigger->op;
152985 sSubParse.nQueryLoop = pParse->nQueryLoop;
152986 sSubParse.prepFlags = pParse->prepFlags;
152991 pTrigger->zName, onErrorText(orconf),
152992 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
152993 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
152994 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
152995 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
152996 pTab->zName
152999 if( pTrigger->zName ){
153000 sqlite3VdbeChangeP4(v, -1,
153001 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
153007 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
153009 if( pTrigger->pWhen ){
153010 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
153011 if( db->mallocFailed==0
153020 /* Code the trigger program into the sub-vdbe. */
153021 codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf);
153023 /* Insert an OP_Halt at the end of the sub-program. */
153028 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
153031 if( pParse->nErr==0 ){
153032 assert( db->mallocFailed==0 );
153033 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
153035 pProgram->nMem = sSubParse.nMem;
153036 pProgram->nCsr = sSubParse.nTab;
153037 pProgram->token = (void *)pTrigger;
153038 pPrg->aColmask[0] = sSubParse.oldmask;
153039 pPrg->aColmask[1] = sSubParse.newmask;
153051 ** Return a pointer to a TriggerPrg object containing the sub-program for
153065 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
153071 for(pPrg=pRoot->pTriggerPrg;
153072 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
153073 pPrg=pPrg->pNext
153079 pParse->db->errByteOffset = -1;
153102 assert( pPrg || pParse->nErr );
153105 ** is a pointer to the sub-vdbe containing the trigger program. */
153107 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
153109 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
153110 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
153112 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
153114 /* Set the P5 operand of the OP_Program instruction to non-zero if
153116 ** invocation is disallowed if (a) the sub-program is really a trigger,
153131 ** operation on pTab, this function is a no-op.
153136 ** (a copy of pTab->nCol), then registers are populated as follows:
153139 ** ------------------------------------------------------
153141 ** reg+1 OLD.* value of left-most column of pTab
153143 ** reg+N OLD.* value of right-most column of pTab
153145 ** reg+N+2 NEW.* value of left-most column of pTab
153147 ** reg+N+N+1 NEW.* value of right-most column of pTab
153180 for(p=pTrigger; p; p=p->pNext){
153185 assert( p->pSchema!=0 );
153186 assert( p->pTabSchema!=0 );
153187 assert( p->pSchema==p->pTabSchema
153188 || p->pSchema==pParse->db->aDb[1].pSchema );
153195 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
153196 && p->tr_tm==tr_tm
153197 && checkColumnOverlap(p->pColumns, pChanges)
153199 if( !p->bReturning ){
153209 ** Triggers may access values stored in the old.* or new.* pseudo-table.
153210 ** This function returns a 32-bit bitmask indicating which columns of the
153215 ** Bit 0 of the returned mask is set if the left-most column of the
153250 for(p=pTrigger; p; p=p->pNext){
153251 if( p->op==op
153252 && (tr_tm&p->tr_tm)
153253 && checkColumnOverlap(p->pColumns,pChanges)
153255 if( p->bReturning ){
153261 mask |= pPrg->aColmask[isNew];
153306 ** i-th column of table pTab. This routine sets the P4 parameter of the
153312 ** command. If the latter, then the row-records in the table btree on disk
153315 ** If the former, then all row-records are guaranteed to include a value
153329 ** If column as REAL affinity and the table is an ordinary b-tree table
153337 assert( pTab->nCol>i );
153338 pCol = &pTab->aCol[i];
153339 if( pCol->iDflt ){
153343 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
153344 assert( i<pTab->nCol );
153347 pCol->affinity, &pValue);
153353 if( pCol->affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
153362 ** and false if not. This is an optimization. False-positives are a
153363 ** performance degradation, but false-negatives can result in a corrupt
153366 ** aXRef[j] will be non-negative if column j of the original table is
153376 i16 iIdxCol = pIdx->aiColumn[iCol];
153382 assert( pIdx->aColExpr!=0 );
153383 assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
153384 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
153392 ** to be unchanged. This is an optimization. False-positives are a
153393 ** performance degradation, but false-negatives can result in a corrupt
153396 ** aXRef[j] will be non-negative if column j of the original table is
153405 if( pIdx->pPartIdxWhere==0 ) return 0;
153406 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
153414 ** table in the source-list (pSrc->a[0]).
153418 if( pRet ) pRet->iColumn = iCol+1;
153426 ** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
153434 ** SELECT <other-columns>, pChanges FROM pTabList
153436 ** GROUP BY <other-columns>
153442 ** the <other-columns> in the query above are is determined by the type
153443 ** of table pTabList->a[0].pTab.
153446 ** PRIMARY KEY. In this case <other-columns> are the primary key columns
153450 ** If the table is actually a view, then <other-columns> are all columns of
153454 ** If the table is a virtual or ordinary intkey table, then <other-columns>
153457 ** rowid value in <other-columns> is used as the integer key, and the
153477 sqlite3 *db = pParse->db;
153478 Table *pTab = pTabList->a[0].pTab;
153498 assert( pTabList->nSrc>1 );
153500 assert( pSrc->a[0].fg.notCte );
153501 pSrc->a[0].iCursor = -1;
153502 pSrc->a[0].pTab->nTabRef--;
153503 pSrc->a[0].pTab = 0;
153506 for(i=0; i<pPk->nKeyCol; i++){
153507 Expr *pNew = exprRowColumn(pParse, pPk->aiColumn[i]);
153517 for(i=0; i<pTab->nCol; i++){
153530 assert( pChanges!=0 || pParse->db->mallocFailed );
153532 for(i=0; i<pChanges->nExpr; i++){
153534 sqlite3ExprDup(db, pChanges->a[i].pExpr, 0)
153542 if( pSelect ) pSelect->selFlags |= SF_OrderByReqd;
153544 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
153582 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
153583 ** an expression for the i-th column of the table.
153584 ** aXRef[i]==-1 if the i-th column is not changed. */
153590 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
153592 NameContext sNC; /* The name-context to resolve expressions in */
153614 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */
153626 db = pParse->db;
153627 assert( db->pParse==pParse );
153628 if( pParse->nErr ){
153631 assert( db->mallocFailed==0 );
153637 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
153659 sqlite3TreeViewUpdate(pParse->pWith, pTabList, pChanges, pWhere,
153665 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
153668 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;
153693 iBaseCur = iDataCur = pParse->nTab++;
153696 testcase( pPk!=0 && pPk!=pTab->pIndex );
153697 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
153699 iDataCur = pParse->nTab;
153701 pParse->nTab++;
153705 iDataCur = pUpsert->iDataCur;
153706 iIdxCur = pUpsert->iIdxCur;
153707 pParse->nTab = iBaseCur;
153709 pTabList->a[0].iCursor = iDataCur;
153714 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
153716 aRegIdx = aXRef+pTab->nCol;
153720 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
153722 /* Initialize the name-context */
153740 for(i=0; i<pChanges->nExpr; i++){
153741 u8 hCol = sqlite3StrIHash(pChanges->a[i].zEName);
153744 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
153747 for(j=0; j<pTab->nCol; j++){
153748 if( pTab->aCol[j].hName==hCol
153749 && sqlite3StrICmp(pTab->aCol[j].zCnName, pChanges->a[i].zEName)==0
153751 if( j==pTab->iPKey ){
153753 pRowidExpr = pChanges->a[i].pExpr;
153755 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
153759 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED ){
153760 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
153761 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
153764 pTab->aCol[j].zCnName);
153772 if( j>=pTab->nCol ){
153773 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){
153774 j = -1;
153776 pRowidExpr = pChanges->a[i].pExpr;
153779 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName);
153780 pParse->checkSchema = 1;
153787 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
153788 j<0 ? "ROWID" : pTab->aCol[j].zCnName,
153789 db->aDb[iDb].zDbSName);
153793 aXRef[j] = -1;
153807 ** is non-negative, so the value of aXRef[] for generated columns can be
153808 ** set to any non-negative number. We use 99999 so that the value is
153811 if( pTab->tabFlags & TF_HasGenerated ){
153813 testcase( pTab->tabFlags & TF_HasVirtual );
153814 testcase( pTab->tabFlags & TF_HasStored );
153817 for(i=0; i<pTab->nCol; i++){
153819 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ) continue;
153821 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
153837 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
153846 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
153851 reg = ++pParse->nMem;
153852 pParse->nMem += pIdx->nColumn;
153855 for(i=0; i<pIdx->nKeyCol; i++){
153857 reg = ++pParse->nMem;
153858 pParse->nMem += pIdx->nColumn;
153859 if( onError==OE_Default && pIdx->onError==OE_Replace ){
153869 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
153876 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
153885 ** two-pass update algorithm. */
153886 assert( aRegIdx[nAllIdx]==pParse->nMem );
153888 regOldRowid = regNewRowid = ++pParse->nMem;
153890 regOld = pParse->nMem + 1;
153891 pParse->nMem += pTab->nCol;
153894 regNewRowid = ++pParse->nMem;
153896 regNew = pParse->nMem + 1;
153897 pParse->nMem += pTab->nCol;
153902 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
153939 if( (db->flags&SQLITE_CountRows)!=0
153940 && !pParse->pTriggerTab
153941 && !pParse->nested
153942 && !pParse->bReturning
153945 regRowCount = ++pParse->nMem;
153951 iEph = pParse->nTab++;
153955 nPk = pPk ? pPk->nKeyCol : 0;
153956 iPk = pParse->nMem+1;
153957 pParse->nMem += nPk;
153958 pParse->nMem += nChangeFrom;
153959 regKey = ++pParse->nMem;
153961 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);
153962 iEph = pParse->nTab++;
153963 if( pPk ) sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
153968 pKeyInfo->nAllField = nEphCol;
154002 ** Do not consider a single-pass strategy for a multi-row update if
154012 if( !pParse->nested
154024 /* A one-pass strategy that might update more than one row may not
154040 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
154050 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
154054 aRegIdx[nAllIdx] = ++pParse->nMem;
154066 assert( pPk->aiColumn[i]>=0 );
154068 pPk->aiColumn[i], iPk+i);
154094 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
154095 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
154098 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
154176 /* Compute the old pre-UPDATE content of the row being changed, if that
154183 for(i=0; i<pTab->nCol; i++){
154184 u32 colFlags = pTab->aCol[i].colFlags;
154220 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
154221 if( i==pTab->iPKey ){
154223 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)!=0 ){
154224 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
154229 int nOff = (isView ? pTab->nCol : nPk);
154233 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k);
154251 if( pTab->tabFlags & TF_HasGenerated ){
154252 testcase( pTab->tabFlags & TF_HasVirtual );
154253 testcase( pTab->tabFlags & TF_HasStored );
154267 /* The row-trigger may have deleted the row being updated. In this
154269 ** required. This behavior - what happens when the row being updated
154270 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
154281 /* After-BEFORE-trigger-reload-loop:
154287 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
154290 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
154291 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
154292 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
154293 }else if( aXRef[i]<0 && i!=pTab->iPKey ){
154298 if( pTab->tabFlags & TF_HasGenerated ){
154299 testcase( pTab->tabFlags & TF_HasVirtual );
154300 testcase( pTab->tabFlags & TF_HasStored );
154332 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
154345 ** to invoke the pre-update hook.
154348 ** pre-update hook. If the caller invokes preupdate_new(), the returned
154362 if( !pParse->nested ){
154405 /* Nothing to do at end-of-loop for a single-pass */
154419 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
154457 ** There are two possible strategies - the default and the special
154485 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
154488 sqlite3 *db = pParse->db; /* Database connection */
154491 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
154495 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
154504 ephemTab = pParse->nTab++;
154506 regArg = pParse->nMem + 1;
154507 pParse->nMem += nArg;
154508 if( pSrc->nSrc>1 ){
154522 assert( pPk->nKeyCol==1 );
154523 iPk = pPk->aiColumn[0];
154525 pRow = sqlite3ExprDup(db, pChanges->a[aXRef[iPk]].pExpr, 0);
154532 for(i=0; i<pTab->nCol; i++){
154535 sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0)
154539 if( pRowExpr ) pRowExpr->op2 = OPFLAG_NOCHNG;
154548 regRec = ++pParse->nMem;
154549 regRowid = ++pParse->nMem;
154558 for(i=0; i<pTab->nCol; i++){
154559 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
154561 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
154579 assert( pPk->nKeyCol==1 );
154580 iPk = pPk->aiColumn[0];
154591 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
154602 ** accept no-change records with serial_type 10 */
154613 if( pSrc->nSrc==1 ){
154646 ** 2018-04-12
154667 Upsert *pNext = p->pNextUpsert;
154668 sqlite3ExprListDelete(db, p->pUpsertTarget);
154669 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
154670 sqlite3ExprListDelete(db, p->pUpsertSet);
154671 sqlite3ExprDelete(db, p->pUpsertWhere);
154672 sqlite3DbFree(db, p->pToFree);
154688 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
154689 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
154690 sqlite3ExprListDup(db, p->pUpsertSet, 0),
154691 sqlite3ExprDup(db, p->pUpsertWhere, 0),
154692 sqlite3UpsertDup(db, p->pNextUpsert)
154717 pNew->pUpsertTarget = pTarget;
154718 pNew->pUpsertTargetWhere = pTargetWhere;
154719 pNew->pUpsertSet = pSet;
154720 pNew->pUpsertWhere = pWhere;
154721 pNew->isDoUpdate = pSet!=0;
154722 pNew->pNextUpsert = pNext;
154729 ** symbols in the conflict-target.
154744 ExprList *pTarget; /* The conflict-target clause */
154745 Expr *pTerm; /* One term of the conflict-target clause */
154750 assert( pTabList->nSrc==1 );
154751 assert( pTabList->a[0].pTab!=0 );
154753 assert( pUpsert->pUpsertTarget!=0 );
154755 /* Resolve all symbolic names in the conflict-target clause, which
154756 ** includes both the list of columns and the optional partial-index
154762 for(; pUpsert && pUpsert->pUpsertTarget;
154763 pUpsert=pUpsert->pNextUpsert, nClause++){
154764 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
154766 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
154770 pTab = pTabList->a[0].pTab;
154771 pTarget = pUpsert->pUpsertTarget;
154772 iCursor = pTabList->a[0].iCursor;
154774 && pTarget->nExpr==1
154775 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
154776 && pTerm->iColumn==XN_ROWID
154778 /* The conflict-target is the rowid of the primary table */
154779 assert( pUpsert->pUpsertIdx==0 );
154787 ** prior to comparing against the conflict-target expression.
154793 sCol[1].iTable = pTabList->a[0].iCursor;
154796 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
154799 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
154800 if( pIdx->pPartIdxWhere ){
154801 if( pUpsert->pUpsertTargetWhere==0 ) continue;
154802 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
154803 pIdx->pPartIdxWhere, iCursor)!=0 ){
154807 nn = pIdx->nKeyCol;
154810 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
154811 if( pIdx->aiColumn[ii]==XN_EXPR ){
154812 assert( pIdx->aColExpr!=0 );
154813 assert( pIdx->aColExpr->nExpr>ii );
154814 assert( pIdx->bHasExpr );
154815 pExpr = pIdx->aColExpr->a[ii].pExpr;
154816 if( pExpr->op!=TK_COLLATE ){
154822 sCol[1].iColumn = pIdx->aiColumn[ii];
154826 if( sqlite3ExprCompare(0,pTarget->a[jj].pExpr,pExpr,iCursor)<2 ){
154840 pUpsert->pUpsertIdx = pIdx;
154844 ** after multi-CONFLICT upsert was added, and so we silently ignore
154847 pUpsert->isDup = 1;
154851 if( pUpsert->pUpsertIdx==0 ){
154853 if( nClause==0 && pUpsert->pNextUpsert==0 ){
154874 pNext = pUpsert->pNextUpsert;
154875 while( 1 /*exit-by-return*/ ){
154877 if( pNext->pUpsertTarget==0 ) return 1;
154878 if( pNext->pUpsertIdx==0 ) return 1;
154879 if( !pNext->isDup ) return 0;
154880 pNext = pNext->pNextUpsert;
154894 && pUpsert->pUpsertTarget!=0
154895 && pUpsert->pUpsertIdx!=pIdx
154897 pUpsert = pUpsert->pNextUpsert;
154906 ** In this case parameter iCur is a cursor open on the table b-tree that
154912 Parse *pParse, /* The parsing and code-generating context */
154918 Vdbe *v = pParse->pVdbe;
154919 sqlite3 *db = pParse->db;
154927 iDataCur = pUpsert->iDataCur;
154939 int nPk = pPk->nKeyCol;
154940 int iPk = pParse->nMem+1;
154941 pParse->nMem += nPk;
154944 assert( pPk->aiColumn[i]>=0 );
154945 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
154947 VdbeComment((v, "%s.%s", pIdx->zName,
154948 pTab->aCol[pPk->aiColumn[i]].zCnName));
154959 /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
154961 pSrc = sqlite3SrcListDup(db, pTop->pUpsertSrc, 0);
154963 for(i=0; i<pTab->nCol; i++){
154964 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
154965 sqlite3VdbeAddOp1(v, OP_RealAffinity, pTop->regData+i);
154968 sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db,pUpsert->pUpsertSet,0),
154969 sqlite3ExprDup(db,pUpsert->pUpsertWhere,0), OE_Abort, 0, 0, pUpsert);
155013 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
155085 if( pParse->nErr ) goto build_vacuum_end;
155094 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
155095 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
155098 iDb = sqlite3FindDb(pParse->db, pNm);
155105 iIntoReg = ++pParse->nMem;
155112 sqlite3ExprDelete(pParse->db, pInto);
155128 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
155129 u64 saved_flags; /* Saved value of db->flags */
155130 i64 saved_nChange; /* Saved value of db->nChange */
155131 i64 saved_nTotalChange; /* Saved value of db->nTotalChange */
155132 u32 saved_openFlags; /* Saved value of db->openFlags */
155142 if( !db->autoCommit ){
155144 return SQLITE_ERROR; /* IMP: R-12218-18073 */
155146 if( db->nVdbeActive>1 ){
155147 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
155148 return SQLITE_ERROR; /* IMP: R-15610-35227 */
155150 saved_openFlags = db->openFlags;
155153 sqlite3SetString(pzErrMsg, db, "non-text filename");
155157 db->openFlags &= ~SQLITE_OPEN_READONLY;
155158 db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
155164 ** restored before returning. Then set the writable-schema flag, and
155166 saved_flags = db->flags;
155167 saved_mDbFlags = db->mDbFlags;
155168 saved_nChange = db->nChange;
155169 saved_nTotalChange = db->nTotalChange;
155170 saved_mTrace = db->mTrace;
155171 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
155172 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
155173 db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
155175 db->mTrace = 0;
155177 zDbMain = db->aDb[iDb].zDbSName;
155178 pMain = db->aDb[iDb].pBt;
155187 ** An optimization would be to use a non-journaled pager.
155195 nDb = db->nDb;
155197 db->openFlags = saved_openFlags;
155199 assert( (db->nDb-1)==nDb );
155200 pDb = &db->aDb[nDb];
155201 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
155202 pTemp = pDb->pBt;
155206 if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
155211 db->mDbFlags |= DBFLAG_VacuumInto;
155213 /* For a VACUUM INTO, the pager-flags are set to the same values as
155216 pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK);
155220 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
155226 ** to ensure that we do not try to change the page-size on a WAL database.
155238 db->nextPagesize = 0;
155242 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
155243 || NEVER(db->mallocFailed)
155250 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
155257 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
155271 db->init.iDb = 0;
155284 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
155285 db->mDbFlags &= ~DBFLAG_Vacuum;
155304 ** both transactions are closed by this block - the main database
155358 /* Restore the original value of db->flags */
155359 db->init.iDb = 0;
155360 db->mDbFlags = saved_mDbFlags;
155361 db->flags = saved_flags;
155362 db->nChange = saved_nChange;
155363 db->nTotalChange = saved_nTotalChange;
155364 db->mTrace = saved_mTrace;
155365 sqlite3BtreeSetPageSize(pMain, -1, 0, 1);
155374 db->autoCommit = 1;
155377 sqlite3BtreeClose(pDb->pBt);
155378 pDb->pBt = 0;
155379 pDb->pSchema = 0;
155382 /* This both clears the schemas and reduces the size of the db->aDb[]
155453 pMod->zName = zCopy;
155454 pMod->pModule = pModule;
155455 pMod->pAux = pAux;
155456 pMod->xDestroy = xDestroy;
155457 pMod->pEpoTab = 0;
155458 pMod->nRefModule = 1;
155460 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
155488 sqlite3_mutex_enter(db->mutex);
155492 sqlite3_mutex_leave(db->mutex);
155498 ** External API function used to create a new virtual-table module.
155513 ** External API function used to create a new virtual-table module.
155529 ** External API to drop all virtual-table modules, except those named
155537 for(pThis=sqliteHashFirst(&db->aModule); pThis; pThis=pNext){
155542 for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){}
155545 createModule(db, pMod->zName, 0, 0, 0);
155555 assert( pMod->nRefModule>0 );
155556 pMod->nRefModule--;
155557 if( pMod->nRefModule==0 ){
155558 if( pMod->xDestroy ){
155559 pMod->xDestroy(pMod->pAux);
155561 assert( pMod->pEpoTab==0 );
155575 pVTab->nRef++;
155580 ** pTab is a pointer to a Table structure representing a virtual-table.
155582 ** this virtual-table, if one has been created, or NULL otherwise.
155587 for(pVtab=pTab->u.vtab.p; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
155592 ** Decrement the ref-count on a virtual table object. When the ref-count
155596 sqlite3 *db = pVTab->db;
155599 assert( pVTab->nRef>0 );
155600 assert( db->eOpenState==SQLITE_STATE_OPEN
155601 || db->eOpenState==SQLITE_STATE_ZOMBIE );
155603 pVTab->nRef--;
155604 if( pVTab->nRef==0 ){
155605 sqlite3_vtab *p = pVTab->pVtab;
155607 p->pModule->xDisconnect(p);
155609 sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod);
155616 ** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated
155619 ** connection db is left in the p->u.vtab.p list.
155626 pVTable = p->u.vtab.p;
155627 p->u.vtab.p = 0;
155633 ** database connection that may have an entry in the p->u.vtab.p list.
155635 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
155638 sqlite3 *db2 = pVTable->db;
155639 VTable *pNext = pVTable->pNext;
155643 p->u.vtab.p = pRet;
155644 pRet->pNext = 0;
155646 pVTable->pNext = db2->pDisconnect;
155647 db2->pDisconnect = pVTable;
155659 ** list in p->pVTab. It also decrements the VTable ref count. This is
155662 ** be being used by other shared-cache connections).
155669 assert( sqlite3_mutex_held(db->mutex) );
155671 for(ppVTab=&p->u.vtab.p; *ppVTab; ppVTab=&(*ppVTab)->pNext){
155672 if( (*ppVTab)->db==db ){
155674 *ppVTab = pVTab->pNext;
155686 ** shared b-tree databases opened using connection db are held by the
155696 ** or, if the virtual table is stored in a non-sharable database, then
155700 ** by multiple threads. It is thread-safe.
155703 VTable *p = db->pDisconnect;
155706 assert( sqlite3_mutex_held(db->mutex) );
155709 db->pDisconnect = 0;
155711 VTable *pNext = p->pNext;
155719 ** Clear any and all virtual-table information from the Table record.
155723 ** Since it is a virtual-table, the Table structure contains a pointer
155735 if( db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
155736 if( p->u.vtab.azArg ){
155738 for(i=0; i<p->u.vtab.nArg; i++){
155739 if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]);
155741 sqlite3DbFree(db, p->u.vtab.azArg);
155746 ** Add a new module argument to pTable->u.vtab.azArg[].
155747 ** The string is not copied - the pointer is stored. The
155754 sqlite3 *db = pParse->db;
155757 nBytes = sizeof(char *)*(2+pTable->u.vtab.nArg);
155758 if( pTable->u.vtab.nArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
155759 sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
155761 azModuleArg = sqlite3DbRealloc(db, pTable->u.vtab.azArg, nBytes);
155765 int i = pTable->u.vtab.nArg++;
155768 pTable->u.vtab.azArg = azModuleArg;
155788 pTable = pParse->pNewTable;
155790 assert( 0==pTable->pIndex );
155791 pTable->eTabType = TABTYP_VTAB;
155793 db = pParse->db;
155795 assert( pTable->u.vtab.nArg==0 );
155798 addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
155799 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
155800 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
155802 pParse->sNameToken.n = (int)(
155803 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
155812 if( pTable->u.vtab.azArg ){
155813 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
155815 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
155816 pTable->u.vtab.azArg[0], pParse->db->aDb[iDb].zDbSName);
155823 ** in pParse->zArg[] and appends it to the list of arguments on the
155824 ** virtual table currently under construction in pParse->pTable.
155827 if( pParse->sArg.z && pParse->pNewTable ){
155828 const char *z = (const char*)pParse->sArg.z;
155829 int n = pParse->sArg.n;
155830 sqlite3 *db = pParse->db;
155831 addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
155840 Table *pTab = pParse->pNewTable; /* The table being constructed */
155841 sqlite3 *db = pParse->db; /* The database connection */
155846 pParse->sArg.z = 0;
155847 if( pTab->u.vtab.nArg<1 ) return;
155855 if( !db->init.busy ){
155866 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
155868 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
155874 ** The VM register number pParse->regRowid holds the rowid of an
155878 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
155883 db->aDb[iDb].zDbSName,
155884 pTab->zName,
155885 pTab->zName,
155887 pParse->regRowid
155893 zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt);
155897 iReg = ++pParse->nMem;
155898 sqlite3VdbeLoadString(v, iReg, pTab->zName);
155901 /* If we are rereading the sqlite_schema table create the in-memory
155904 Schema *pSchema = pTab->pSchema;
155905 const char *zName = pTab->zName;
155908 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
155914 pParse->pNewTable = 0;
155924 pParse->sArg.z = 0;
155925 pParse->sArg.n = 0;
155933 Token *pArg = &pParse->sArg;
155934 if( pArg->z==0 ){
155935 pArg->z = p->z;
155936 pArg->n = p->n;
155938 assert(pArg->z <= p->z);
155939 pArg->n = (int)(&p->z[p->n] - pArg->z);
155959 int nArg = pTab->u.vtab.nArg;
155966 azArg = (const char *const*)pTab->u.vtab.azArg;
155968 /* Check that the virtual-table is not already being initialized */
155969 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
155970 if( pCtx->pTab==pTab ){
155972 "vtable constructor called recursively: %s", pTab->zName
155978 zModuleName = sqlite3DbStrDup(db, pTab->zName);
155989 pVTable->db = db;
155990 pVTable->pMod = pMod;
155991 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal;
155993 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
155994 pTab->u.vtab.azArg[1] = db->aDb[iDb].zDbSName;
155997 assert( &db->pVtabCtx );
156001 sCtx.pPrior = db->pVtabCtx;
156003 db->pVtabCtx = &sCtx;
156004 pTab->nTabRef++;
156005 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
156007 assert( pTab->nTabRef>1 || rc!=SQLITE_OK );
156009 db->pVtabCtx = sCtx.pPrior;
156021 }else if( ALWAYS(pVTable->pVtab) ){
156024 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
156025 pVTable->pVtab->pModule = pMod->pModule;
156026 pMod->nRefModule++;
156027 pVTable->nRef = 1;
156037 ** into the linked list headed by pTab->u.vtab.p. Then loop through the
156041 pVTable->pNext = pTab->u.vtab.p;
156042 pTab->u.vtab.p = pVTable;
156044 for(iCol=0; iCol<pTab->nCol; iCol++){
156045 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
156051 && (i==0 || zType[i-1]==' ')
156064 assert(zType[i-1]==' ');
156065 zType[i-1] = '\0';
156067 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
156068 pTab->tabFlags |= TF_HasHidden;
156071 pTab->tabFlags |= oooHidden;
156086 ** This call is a no-op if table pTab is not a virtual table.
156089 sqlite3 *db = pParse->db;
156101 zMod = pTab->u.vtab.azArg[0];
156102 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
156105 const char *zModule = pTab->u.vtab.azArg[0];
156110 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
156113 pParse->rc = rc;
156121 ** Grow the db->aVTrans[] array so that there is room for at least one
156122 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
156128 if( (db->nVTrans%ARRAY_INCR)==0 ){
156131 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
156132 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
156136 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
156137 db->aVTrans = aVTrans;
156149 db->aVTrans[db->nVTrans++] = pVTab;
156167 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
156168 assert( pTab && IsVirtual(pTab) && !pTab->u.vtab.p );
156171 zMod = pTab->u.vtab.azArg[0];
156172 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
156178 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
156182 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
156232 sqlite3_mutex_enter(db->mutex);
156233 pCtx = db->pVtabCtx;
156234 if( !pCtx || pCtx->bDeclared ){
156236 sqlite3_mutex_leave(db->mutex);
156240 pTab = pCtx->pTab;
156247 ** schema. Nevertheless, defend against that (turn off db->init.busy)
156249 assert( db->init.busy==0 );
156250 initBusy = db->init.busy;
156251 db->init.busy = 0;
156255 assert( !db->mallocFailed );
156258 if( !pTab->aCol ){
156261 pTab->aCol = pNew->aCol;
156262 sqlite3ExprListDelete(db, pNew->u.tab.pDfltList);
156263 pTab->nNVCol = pTab->nCol = pNew->nCol;
156264 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
156265 pNew->nCol = 0;
156266 pNew->aCol = 0;
156267 assert( pTab->pIndex==0 );
156270 && pCtx->pVTable->pMod->pModule->xUpdate!=0
156271 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
156273 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
156274 ** or else must have a single-column PRIMARY KEY */
156277 pIdx = pNew->pIndex;
156279 assert( pIdx->pNext==0 );
156280 pTab->pIndex = pIdx;
156281 pNew->pIndex = 0;
156282 pIdx->pTable = pTab;
156285 pCtx->bDeclared = 1;
156299 db->init.busy = initBusy;
156303 sqlite3_mutex_leave(db->mutex);
156312 ** This call is a no-op if zTab is not a virtual table.
156318 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
156321 && ALWAYS(pTab->u.vtab.p!=0)
156325 for(p=pTab->u.vtab.p; p; p=p->pNext){
156326 assert( p->pVtab );
156327 if( p->pVtab->nRef>0 ){
156332 xDestroy = p->pMod->pModule->xDestroy;
156333 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
156335 pTab->nTabRef++;
156336 rc = xDestroy(p->pVtab);
156339 assert( pTab->u.vtab.p==p && p->pNext==0 );
156340 p->pVtab = 0;
156341 pTab->u.vtab.p = 0;
156360 if( db->aVTrans ){
156361 VTable **aVTrans = db->aVTrans;
156362 db->aVTrans = 0;
156363 for(i=0; i<db->nVTrans; i++){
156365 sqlite3_vtab *p = pVTab->pVtab;
156368 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
156371 pVTab->iSavepoint = 0;
156375 db->nVTrans = 0;
156384 ** If an error message is available, leave it in p->zErrMsg.
156389 VTable **aVTrans = db->aVTrans;
156391 db->aVTrans = 0;
156392 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
156394 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
156395 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
156400 db->aVTrans = aVTrans;
156434 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
156445 pModule = pVTab->pVtab->pModule;
156447 if( pModule->xBegin ){
156451 for(i=0; i<db->nVTrans; i++){
156452 if( db->aVTrans[i]==pVTab ){
156461 rc = pModule->xBegin(pVTab->pVtab);
156463 int iSvpt = db->nStatement + db->nSavepoint;
156465 if( iSvpt && pModule->xSavepoint ){
156466 pVTab->iSavepoint = iSvpt;
156467 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
156494 assert( iSavepoint>=-1 );
156495 if( db->aVTrans ){
156497 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
156498 VTable *pVTab = db->aVTrans[i];
156499 const sqlite3_module *pMod = pVTab->pMod->pModule;
156500 if( pVTab->pVtab && pMod->iVersion>=2 ){
156505 xMethod = pMod->xSavepoint;
156506 pVTab->iSavepoint = iSavepoint+1;
156509 xMethod = pMod->xRollbackTo;
156512 xMethod = pMod->xRelease;
156515 if( xMethod && pVTab->iSavepoint>iSavepoint ){
156516 u64 savedFlags = (db->flags & SQLITE_Defensive);
156517 db->flags &= ~(u64)SQLITE_Defensive;
156518 rc = xMethod(pVTab->pVtab, iSavepoint);
156519 db->flags |= savedFlags;
156557 if( pExpr->op!=TK_COLUMN ) return pDef;
156559 pTab = pExpr->y.pTab;
156562 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
156564 assert( pVtab->pModule!=0 );
156565 pMod = (sqlite3_module *)pVtab->pModule;
156566 if( pMod->xFindFunction==0 ) return pDef;
156572 ** with an all lower-case function name. Continue in this tradition to
156578 for(i=0; pDef->zName[i]; i++){
156579 unsigned char x = (unsigned char)pDef->zName[i];
156584 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
156592 + sqlite3Strlen30(pDef->zName) + 1);
156597 pNew->zName = (const char*)&pNew[1];
156598 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
156599 pNew->xSFunc = xSFunc;
156600 pNew->pUserData = pArg;
156601 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
156606 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
156609 ** is a no-op.
156617 for(i=0; i<pToplevel->nVtabLock; i++){
156618 if( pTab==pToplevel->apVtabLock[i] ) return;
156620 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
156621 apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n);
156623 pToplevel->apVtabLock = apVtabLock;
156624 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
156626 sqlite3OomFault(pToplevel->db);
156633 ** exist. Return non-zero if either the eponymous virtual table instance
156640 ** instances always exist. They cannot be DROP-ed.
156646 const sqlite3_module *pModule = pMod->pModule;
156650 sqlite3 *db = pParse->db;
156651 if( pMod->pEpoTab ) return 1;
156652 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
156655 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
156656 if( pTab->zName==0 ){
156660 pMod->pEpoTab = pTab;
156661 pTab->nTabRef = 1;
156662 pTab->eTabType = TABTYP_VTAB;
156663 pTab->pSchema = db->aDb[0].pSchema;
156664 assert( pTab->u.vtab.nArg==0 );
156665 pTab->iPKey = -1;
156666 pTab->tabFlags |= TF_Eponymous;
156667 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
156669 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
156670 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
156684 Table *pTab = pMod->pEpoTab;
156689 pTab->tabFlags |= TF_Ephemeral;
156691 pMod->pEpoTab = 0;
156711 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
156712 return (int)aMap[db->vtabOnConflict-1];
156728 sqlite3_mutex_enter(db->mutex);
156729 p = db->pVtabCtx;
156733 assert( p->pTab==0 || IsVirtual(p->pTab) );
156737 p->pVTable->bConstraint = (u8)va_arg(ap, int);
156741 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low;
156745 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High;
156749 p->pVTable->bAllSchemas = 1;
156761 sqlite3_mutex_leave(db->mutex);
156770 ** 2015-06-06
156783 ** This file was split off from where.c on 2015-06-06 in order to reduce the
156792 ** 2013-11-12
156861 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
156869 int addrSkip; /* Jump here for next iteration of skip-scan */
156873 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
156874 int addrBignull; /* Jump here for next part of big-null scan */
156884 union { /* Information that depends on pWLoop->wsFlags */
156890 int iBase; /* Base register of multi-key index record */
156894 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
156908 ** prevent a query solution - which is an error) and many terms of the
156910 ** potential way of implementing that FROM-clause term, together with
156926 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
156969 ** correspond to the subquery(s) of OR-clause processing. Only the
156985 ** vector, not a scalar, and because dependencies are many-to-one, not
156986 ** one-to-one as are graph nodes. But it is a useful visualization aid.)
157002 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
157015 ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
157046 ** spread out over the non-negative integers. For example, the cursor
157065 int iParent; /* Disable pWC->a[iParent] when this term disabled */
157075 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
157088 #define TERM_OK 0x0040 /* Used during OR-clause processing */
157094 #define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
157101 #define TERM_SLICE 0x8000 /* One slice of a row-value/vector comparison */
157112 int k; /* Resume scanning at this->pWC->a[this->k] */
157118 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
157140 int nBase; /* Number of terms through the last non-Virtual */
157178 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
157179 ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
157186 ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
157187 ** 57->5, 73->4. Or one of 719 other combinations might be used. It
157226 ** routine. The limit is high enough that is should not impact real-world
157264 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
157273 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
157287 ** Private interfaces - callable only by other where.c routines.
157339 int iLevel, /* Which level of pWInfo->a[] should be coded */
157367 ** OR-ed combination of these values can be used when searching for
157379 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
157380 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
157381 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
157382 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
157386 #define WO_OR 0x0200 /* Two or more OR-connected terms */
157387 #define WO_AND 0x0400 /* Two or more AND-connected terms */
157390 #define WO_ROWVAL 0x2000 /* A row-value term */
157393 #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
157408 #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
157416 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
157421 #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
157423 #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
157426 /* 0x02000000 -- available for reuse */
157427 #define WHERE_EXPRIDX 0x04000000 /* Uses an index-on-expressions */
157437 ** Return the name of the i-th column of the pIdx index.
157440 i = pIdx->aiColumn[i];
157443 return pIdx->pTable->aCol[i].zCnName;
157458 int iTerm, /* Zero-based index of first term. */
157459 int bAnd, /* Non-zero to append " AND " */
157499 Index *pIndex = pLoop->u.btree.pIndex;
157500 u16 nEq = pLoop->u.btree.nEq;
157501 u16 nSkip = pLoop->nSkip;
157504 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
157513 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
157514 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
157517 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
157518 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
157524 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
157526 ** was defined at compile-time. If it is not a no-op, a single OP_Explain
157540 if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
157543 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
157544 Vdbe *v = pParse->pVdbe; /* VM being constructed */
157545 sqlite3 *db = pParse->db; /* Database handle */
157553 pLoop = pLevel->pWLoop;
157554 flags = pLoop->wsFlags;
157558 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
157568 assert( pLoop->u.btree.pIndex!=0 );
157569 pIdx = pLoop->u.btree.pIndex;
157571 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
157586 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
157592 const Table *pTab = pItem->pTab;
157593 const char *zRowid = pTab->iPKey>=0 ? pTab->aCol[pTab->iPKey].zCnName:
157615 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
157618 if( pItem->fg.jointype & JT_LEFT ){
157619 sqlite3_str_appendf(&str, " LEFT-JOIN");
157622 if( pLoop->nOut>=10 ){
157624 sqlite3LogEstToInt(pLoop->nOut));
157632 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
157642 ** required and this routine is a no-op.
157653 SrcItem *pItem = &pWInfo->pTabList->a[pLevel->iFrom];
157654 Vdbe *v = pParse->pVdbe; /* VM being constructed */
157655 sqlite3 *db = pParse->db; /* Database handle */
157665 pLoop = pLevel->pWLoop;
157666 if( pLoop->wsFlags & WHERE_IPK ){
157667 const Table *pTab = pItem->pTab;
157668 if( pTab->iPKey>=0 ){
157669 sqlite3_str_appendf(&str, "%s=?", pTab->aCol[pTab->iPKey].zCnName);
157674 for(i=pLoop->nSkip; i<pLoop->u.btree.nEq; i++){
157675 const char *z = explainIndexColumnName(pLoop->u.btree.pIndex, i);
157676 if( i>pLoop->nSkip ) sqlite3_str_append(&str, " AND ", 5);
157683 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
157685 sqlite3VdbeScanStatus(v, sqlite3VdbeCurrentAddr(v)-1, 0, 0, 0, 0);
157708 WhereLoop *pLoop = pLvl->pWLoop;
157709 int wsFlags = pLoop->wsFlags;
157712 if( (wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
157713 zObj = pLoop->u.btree.pIndex->zName;
157715 zObj = pSrclist->a[pLvl->iFrom].zName;
157716 viaCoroutine = pSrclist->a[pLvl->iFrom].fg.viaCoroutine;
157719 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
157724 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iTabCur);
157727 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur);
157730 int addr = pSrclist->a[pLvl->iFrom].addrFillSub;
157731 VdbeOp *pOp = sqlite3VdbeGetOp(v, addr-1);
157732 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->opcode==OP_InitCoroutine );
157733 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->p2>addr );
157734 sqlite3VdbeScanStatusRange(v, addrExplain, addr, pOp->p2-1);
157781 ** LIKE-optimization loop, when scanning BLOBs instead of strings.
157786 while( (pTerm->wtFlags & TERM_CODED)==0
157787 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_OuterON))
157788 && (pLevel->notReady & pTerm->prereqAll)==0
157790 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
157791 pTerm->wtFlags |= TERM_LIKECOND;
157793 pTerm->wtFlags |= TERM_CODED;
157797 sqlite3DebugPrintf("DISABLE-");
157798 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
157801 if( pTerm->iParent<0 ) break;
157802 pTerm = &pTerm->pWC->a[pTerm->iParent];
157804 pTerm->nChild--;
157805 if( pTerm->nChild!=0 ) break;
157815 ** are no-ops) at the beginning and end of zAff are ignored. If all entries
157822 Vdbe *v = pParse->pVdbe;
157824 assert( pParse->db->mallocFailed );
157834 n--;
157838 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){
157839 n--;
157910 sqlite3 *db = pParse->db;
157914 if( db->mallocFailed==0 ){
157915 for(pSelect=pNew->x.pSelect; pSelect; pSelect=pSelect->pPrior){
157923 pOrigRhs = pSelect->pEList;
157924 assert( pNew->pLeft!=0 );
157925 assert( ExprUseXList(pNew->pLeft) );
157926 if( pSelect==pNew->x.pSelect ){
157927 pOrigLhs = pNew->pLeft->x.pList;
157929 for(i=iEq; i<pLoop->nLTerm; i++){
157930 if( pLoop->aLTerm[i]->pExpr==pX ){
157932 assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 );
157933 iField = pLoop->aLTerm[i]->u.x.iField - 1;
157934 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
157935 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
157936 pOrigRhs->a[iField].pExpr = 0;
157938 assert( pOrigLhs->a[iField].pExpr!=0 );
157939 pLhs = sqlite3ExprListAppend(pParse,pLhs,pOrigLhs->a[iField].pExpr);
157940 pOrigLhs->a[iField].pExpr = 0;
157947 pNew->pLeft->x.pList = pLhs;
157949 pSelect->pEList = pRhs;
157950 if( pLhs && pLhs->nExpr==1 ){
157954 Expr *p = pLhs->a[0].pExpr;
157955 pLhs->a[0].pExpr = 0;
157956 sqlite3ExprDelete(db, pNew->pLeft);
157957 pNew->pLeft = p;
157959 if( pSelect->pOrderBy ){
157961 ** iOrderByCol variables. These are set to non-zero when an
157963 ** result-set. Since the result-set of the SELECT statement may
157967 ExprList *pOrderBy = pSelect->pOrderBy;
157968 for(i=0; i<pOrderBy->nExpr; i++){
157969 pOrderBy->a[i].u.x.iOrderByCol = 0;
157997 ** straight-line code. For constraints of the form X IN (...)
158005 int bRev, /* True for reverse-order IN operations */
158008 Expr *pX = pTerm->pExpr;
158009 Vdbe *v = pParse->pVdbe;
158012 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
158014 if( pX->op==TK_EQ || pX->op==TK_IS ){
158015 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
158016 }else if( pX->op==TK_ISNULL ){
158024 WhereLoop *pLoop = pLevel->pWLoop;
158029 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
158030 && pLoop->u.btree.pIndex!=0
158031 && pLoop->u.btree.pIndex->aSortOrder[iEq]
158037 assert( pX->op==TK_IN );
158041 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
158046 for(i=iEq;i<pLoop->nLTerm; i++){
158047 assert( pLoop->aLTerm[i]!=0 );
158048 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
158052 if( !ExprUseXSelect(pX) || pX->x.pSelect->pEList->nExpr==1 ){
158055 Expr *pExpr = pTerm->pExpr;
158056 if( pExpr->iTable==0 || !ExprHasProperty(pExpr, EP_Subrtn) ){
158057 sqlite3 *db = pParse->db;
158059 if( !db->mallocFailed ){
158060 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
158062 pExpr->iTable = iTab;
158066 int n = sqlite3ExprVectorSize(pX->pLeft);
158067 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*MAX(nEq,n));
158081 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
158082 pLoop->wsFlags |= WHERE_IN_ABLE;
158083 if( pLevel->u.in.nIn==0 ){
158084 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
158086 if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 ){
158087 pLoop->wsFlags |= WHERE_IN_EARLYOUT;
158090 i = pLevel->u.in.nIn;
158091 pLevel->u.in.nIn += nEq;
158092 pLevel->u.in.aInLoop =
158093 sqlite3WhereRealloc(pTerm->pWC->pWInfo,
158094 pLevel->u.in.aInLoop,
158095 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
158096 pIn = pLevel->u.in.aInLoop;
158100 for(i=iEq;i<pLoop->nLTerm; i++){
158101 if( pLoop->aLTerm[i]->pExpr==pX ){
158102 int iOut = iReg + i - iEq;
158104 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
158107 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
158111 pIn->iCur = iTab;
158112 pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
158114 pIn->iBase = iReg - i;
158115 pIn->nPrefix = i;
158117 pIn->nPrefix = 0;
158120 pIn->eEndLoopOp = OP_Noop;
158126 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
158127 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 );
158129 && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN|WHERE_VIRTUALTABLE))==0
158131 sqlite3VdbeAddOp3(v, OP_SeekHit, pLevel->iIdxCur, 0, iEq);
158134 pLevel->u.in.nIn = 0;
158136 sqlite3DbFree(pParse->db, aiMap);
158147 ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04)
158149 if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS)==0
158150 || (pTerm->eOperator & WO_EQUIV)==0
158171 ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
158172 ** The only thing it does is allocate the pLevel->iMem memory cell and
158211 u16 nSkip; /* Number of left-most columns to skip */
158212 Vdbe *v = pParse->pVdbe; /* The vm under construction */
158222 pLoop = pLevel->pWLoop;
158223 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
158224 nEq = pLoop->u.btree.nEq;
158225 nSkip = pLoop->nSkip;
158226 pIdx = pLoop->u.btree.pIndex;
158231 regBase = pParse->nMem + 1;
158233 pParse->nMem += nReg;
158235 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
158236 assert( zAff!=0 || pParse->db->mallocFailed );
158239 int iIdxCur = pLevel->iIdxCur;
158240 sqlite3VdbeAddOp3(v, OP_Null, 0, regBase, regBase+nSkip-1);
158244 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
158246 assert( pLevel->addrSkip==0 );
158247 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
158254 testcase( pIdx->aiColumn[j]==XN_EXPR );
158264 pTerm = pLoop->aLTerm[j];
158268 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
158269 testcase( pTerm->wtFlags & TERM_VIRTUAL );
158279 if( pTerm->eOperator & WO_IN ){
158280 if( pTerm->pExpr->flags & EP_xIsSelect ){
158287 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
158288 Expr *pRight = pTerm->pExpr->pRight;
158289 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
158290 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
158293 if( pParse->nErr==0 ){
158294 assert( pParse->db->mallocFailed==0 );
158324 ** becomes a no-op.
158331 if( pTerm->wtFlags & TERM_LIKEOPT ){
158333 assert( pLevel->iLikeRepCntr>0 );
158336 assert( pOp->opcode==OP_String8
158337 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
158338 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
158339 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
158362 ** accessed through the index. If it cannot, then set pWalker->eCode to 1.
158365 struct CCurHint *pHint = pWalker->u.pCCurHint;
158366 assert( pHint->pIdx!=0 );
158367 if( pExpr->op==TK_COLUMN
158368 && pExpr->iTable==pHint->iTabCur
158369 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
158371 pWalker->eCode = 1;
158378 ** should be included in the cursor-hint for a table that is on the rhs
158379 ** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
158392 if( pExpr->op==TK_IS
158393 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
158394 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
158396 pWalker->eCode = 1;
158397 }else if( pExpr->op==TK_FUNCTION ){
158400 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
158401 pWalker->eCode = 1;
158429 struct CCurHint *pHint = pWalker->u.pCCurHint;
158430 if( pExpr->op==TK_COLUMN ){
158431 if( pExpr->iTable!=pHint->iTabCur ){
158432 reg = ++pWalker->pParse->nMem; /* Register for column value */
158433 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
158434 pExpr->op = TK_REGISTER;
158435 pExpr->iTable = reg;
158436 }else if( pHint->pIdx!=0 ){
158437 pExpr->iTable = pHint->iIdxCur;
158438 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
158439 assert( pExpr->iColumn>=0 );
158441 }else if( pExpr->pAggInfo ){
158443 reg = ++pWalker->pParse->nMem; /* Register for column value */
158444 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
158445 pExpr->op = TK_REGISTER;
158446 pExpr->iTable = reg;
158447 }else if( pExpr->op==TK_TRUEFALSE ){
158448 /* Do not walk disabled expressions. tag-20230504-1 */
158461 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
158463 Parse *pParse = pWInfo->pParse;
158464 sqlite3 *db = pParse->db;
158465 Vdbe *v = pParse->pVdbe;
158467 WhereLoop *pLoop = pLevel->pWLoop;
158476 iCur = pLevel->iTabCur;
158477 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
158479 sHint.iIdxCur = pLevel->iIdxCur;
158480 sHint.pIdx = pLoop->u.btree.pIndex;
158484 pWC = &pWInfo->sWC;
158485 for(i=0; i<pWC->nBase; i++){
158486 pTerm = &pWC->a[i];
158487 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
158488 if( pTerm->prereqAll & pLevel->notReady ) continue;
158492 ** from the cursor-hint.
158512 if( pTabItem->fg.jointype & JT_LEFT ){
158513 Expr *pExpr = pTerm->pExpr;
158515 || pExpr->w.iJoin!=pTabItem->iCursor
158519 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
158523 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) continue;
158526 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
158529 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
158530 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
158531 if( j<pLoop->nLTerm ) continue;
158534 /* No subqueries or non-deterministic functions allowed */
158535 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
158542 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
158547 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
158551 if( pParse->nErr==0 ) sqlite3WalkExpr(&sWalker, pExpr);
158558 # define codeCursorHint(A,B,C,D) /* No-op */
158562 ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
158573 ** However, if the scan currently being coded is a branch of an OR-loop and
158590 int iCur, /* Cursor for IPK b-tree */
158593 Parse *pParse = pWInfo->pParse; /* Parse context */
158594 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
158597 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
158599 pWInfo->bDeferredSeek = 1;
158601 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
158602 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
158605 Table *pTab = pIdx->pTable;
158606 u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1));
158608 ai[0] = pTab->nCol;
158609 for(i=0; i<pIdx->nColumn-1; i++){
158611 assert( pIdx->aiColumn[i]<pTab->nCol );
158612 x1 = pIdx->aiColumn[i];
158617 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
158636 Vdbe *v = pParse->pVdbe;
158638 assert( p->op==TK_SELECT );
158640 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
158647 pList = p->x.pList;
158648 assert( nReg<=pList->nExpr );
158650 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
158654 assert( nReg==1 || pParse->nErr );
158673 while( pTruth->op==TK_AND ){
158674 whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
158675 pTruth = pTruth->pRight;
158677 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
158679 if( pTerm->wtFlags & TERM_CODED ) continue;
158680 pExpr = pTerm->pExpr;
158682 pTerm->wtFlags |= TERM_CODED;
158692 ** inner-loop Bloom filters, then evaluate those filters now, before the
158698 ** WhereLevel.regFilter set. If an inner-loop Bloom filter is checked,
158705 int iLevel, /* Which level of pWInfo->a[] should be coded */
158709 while( ++iLevel < pWInfo->nLevel ){
158710 WhereLevel *pLevel = &pWInfo->a[iLevel];
158711 WhereLoop *pLoop = pLevel->pWLoop;
158712 if( pLevel->regFilter==0 ) continue;
158713 if( pLevel->pWLoop->nSkip ) continue;
158714 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
158715 ** vvvvv--' pLevel->regFilter if this were true. */
158716 if( NEVER(pLoop->prereq & notReady) ) continue;
158717 assert( pLevel->addrBrk==0 );
158718 pLevel->addrBrk = addrNxt;
158719 if( pLoop->wsFlags & WHERE_IPK ){
158720 WhereTerm *pTerm = pLoop->aLTerm[0];
158723 assert( pTerm->pExpr!=0 );
158724 testcase( pTerm->wtFlags & TERM_VIRTUAL );
158727 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt, regRowid, addrNxt);
158728 VdbeCoverage(pParse->pVdbe);
158729 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
158731 VdbeCoverage(pParse->pVdbe);
158733 u16 nEq = pLoop->u.btree.nEq;
158737 assert( pLoop->wsFlags & WHERE_INDEXED );
158738 assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 );
158741 sqlite3DbFree(pParse->db, zStartAff);
158742 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
158744 VdbeCoverage(pParse->pVdbe);
158746 pLevel->regFilter = 0;
158747 pLevel->addrBrk = 0;
158757 if( pLoop->u.btree.pIndex->onError
158758 && pLoop->nSkip==0
158759 && pLoop->u.btree.nEq==pLoop->u.btree.pIndex->nKeyCol
158762 for(ii=0; ii<pLoop->u.btree.nEq; ii++){
158763 if( pLoop->aLTerm[ii]->eOperator & (WO_IS|WO_ISNULL) ){
158773 ** Generate code for the start of the iLevel-th loop in the WHERE clause
158780 int iLevel, /* Which level of pWInfo->a[] should be coded */
158801 pWC = &pWInfo->sWC;
158802 db = pParse->db;
158803 pLoop = pLevel->pWLoop;
158804 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
158805 iCur = pTabItem->iCursor;
158806 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
158807 bRev = (pWInfo->revMask>>iLevel)&1;
158808 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
158812 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
158820 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
158822 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
158837 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
158838 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
158844 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
158845 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
158847 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
158848 pLevel->iLeftJoin = ++pParse->nMem;
158849 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
158855 for(j=iLevel; j>0; j--){
158856 if( pWInfo->a[j].iLeftJoin ) break;
158857 if( pWInfo->a[j].pRJ ) break;
158859 addrHalt = pWInfo->a[j].addrBrk;
158861 /* Special case of a FROM clause subquery implemented as a co-routine */
158862 if( pTabItem->fg.viaCoroutine ){
158863 int regYield = pTabItem->regReturn;
158864 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
158865 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
158867 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
158868 pLevel->op = OP_Goto;
158872 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
158873 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
158878 int nConstraint = pLoop->nLTerm;
158881 addrNotFound = pLevel->addrBrk;
158884 pTerm = pLoop->aLTerm[j];
158886 if( pTerm->eOperator & WO_IN ){
158887 if( SMASKBIT32(j) & pLoop->u.vtab.mHandleIn ){
158888 int iTab = pParse->nTab++;
158889 int iCache = ++pParse->nMem;
158890 sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab);
158894 addrNotFound = pLevel->addrNxt;
158897 Expr *pRight = pTerm->pExpr->pRight;
158899 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
158900 && pLoop->u.vtab.bOmitOffset
158902 assert( pTerm->eOperator==WO_AUX );
158903 assert( pWInfo->pSelect!=0 );
158904 assert( pWInfo->pSelect->iOffset>0 );
158905 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pSelect->iOffset);
158910 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
158913 pLoop->u.vtab.idxStr,
158914 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
158916 pLoop->u.vtab.needFree = 0;
158918 ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */
158919 if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0;
158920 pLevel->p1 = iCur;
158921 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
158922 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
158923 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
158926 pTerm = pLoop->aLTerm[j];
158927 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
158931 if( (pTerm->eOperator & WO_IN)!=0
158932 && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0
158933 && !db->mallocFailed
158938 int iIn; /* IN loop corresponding to the j-th constraint */
158945 for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn); iIn++){
158946 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
158947 if( (pOp->opcode==OP_Column && pOp->p3==iReg+j+2)
158948 || (pOp->opcode==OP_Rowid && pOp->p2==iReg+j+2)
158950 testcase( pOp->opcode==OP_Rowid );
158951 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
158960 if( !db->mallocFailed ){
158961 int iFld = pTerm->u.x.iField;
158962 Expr *pLeft = pTerm->pExpr->pLeft;
158965 assert( pLeft->op==TK_VECTOR );
158967 assert( iFld<=pLeft->x.pList->nExpr );
158968 pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr;
158970 pCompare->pLeft = pLeft;
158972 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
158974 pRight->iTable = iReg+j+2;
158976 pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL
158979 pCompare->pLeft = 0;
158987 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
158995 if( (pLoop->wsFlags & WHERE_IPK)!=0
158996 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
159003 assert( pLoop->u.btree.nEq==1 );
159004 pTerm = pLoop->aLTerm[0];
159006 assert( pTerm->pExpr!=0 );
159007 testcase( pTerm->wtFlags & TERM_VIRTUAL );
159008 iReleaseReg = ++pParse->nMem;
159011 addrNxt = pLevel->addrNxt;
159012 if( pLevel->regFilter ){
159015 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
159022 pLevel->op = OP_Noop;
159023 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
159024 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
159035 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
159036 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
159062 assert( (pStart->wtFlags & TERM_VNULL)==0 );
159063 testcase( pStart->wtFlags & TERM_VIRTUAL );
159064 pX = pStart->pExpr;
159066 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
159067 if( sqlite3ExprIsVector(pX->pRight) ){
159069 codeExprOrVector(pParse, pX->pRight, r1, 1);
159070 testcase( pX->op==TK_GT );
159071 testcase( pX->op==TK_GE );
159072 testcase( pX->op==TK_LT );
159073 testcase( pX->op==TK_LE );
159074 op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
159075 assert( pX->op!=TK_GT || op==OP_SeekGE );
159076 assert( pX->op!=TK_GE || op==OP_SeekGE );
159077 assert( pX->op!=TK_LT || op==OP_SeekLE );
159078 assert( pX->op!=TK_LE || op==OP_SeekLE );
159080 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
159082 op = aMoveOp[(pX->op - TK_GT)];
159086 VdbeCoverageIf(v, pX->op==TK_GT);
159087 VdbeCoverageIf(v, pX->op==TK_LE);
159088 VdbeCoverageIf(v, pX->op==TK_LT);
159089 VdbeCoverageIf(v, pX->op==TK_GE);
159098 pX = pEnd->pExpr;
159100 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
159101 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
159102 testcase( pEnd->wtFlags & TERM_VIRTUAL );
159103 memEndValue = ++pParse->nMem;
159104 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
159105 if( 0==sqlite3ExprIsVector(pX->pRight)
159106 && (pX->op==TK_LT || pX->op==TK_GT)
159112 if( 0==sqlite3ExprIsVector(pX->pRight) ){
159117 pLevel->op = bRev ? OP_Prev : OP_Next;
159118 pLevel->p1 = iCur;
159119 pLevel->p2 = start;
159120 assert( pLevel->p5==0 );
159122 iRowidReg = ++pParse->nMem;
159131 }else if( pLoop->wsFlags & WHERE_INDEXED ){
159136 ** left-most columns of the index. It may also contain
159139 ** the right-most column can be an inequality - the rest must
159179 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
159180 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
159181 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
159197 int regBignull = 0; /* big-null flag register */
159200 pIdx = pLoop->u.btree.pIndex;
159201 iIdxCur = pLevel->iIdxCur;
159202 assert( nEq>=pLoop->nSkip );
159208 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
159209 pRangeStart = pLoop->aLTerm[j++];
159210 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
159212 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
159213 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
159215 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
159216 pRangeEnd = pLoop->aLTerm[j++];
159217 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
159219 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
159221 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
159222 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
159223 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
159225 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
159229 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
159231 pLevel->iLikeRepCntr <<=1;
159232 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
159236 j = pIdx->aiColumn[nEq];
159237 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
159242 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
159245 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
159248 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
159251 if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0
159252 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0
159256 testcase( pLoop->nSkip>0 );
159259 pLevel->regBignull = regBignull = ++pParse->nMem;
159260 if( pLevel->iLeftJoin ){
159263 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
159270 if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) ){
159276 if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){
159292 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
159294 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
159295 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
159296 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
159297 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
159298 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
159299 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
159305 Expr *pRight = pRangeStart->pExpr->pRight;
159308 if( (pRangeStart->wtFlags & TERM_VNULL)==0
159318 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
159335 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
159336 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
159337 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
159343 VdbeComment((v, "NULL-scan pass ctr"));
159345 if( pLevel->regFilter ){
159346 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
159354 if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 && op==OP_SeekGE ){
159365 (pIdx->aiRowLogEst[0]+9)/10);
159390 nConstraint-startEq);
159404 assert( pLevel->p2==0 );
159406 Expr *pRight = pRangeEnd->pExpr->pRight;
159410 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
159420 assert( pParse->db->mallocFailed );
159423 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
159441 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
159446 /* Except, skip the end-of-range check while doing the NULL-scan */
159448 VdbeComment((v, "If NULL-scan 2nd pass"));
159460 /* During a NULL-scan, check to see if we have reached the end of
159466 VdbeComment((v, "If NULL-scan 1st pass"));
159477 if( (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0 ){
159482 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
159483 && (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0;
159486 }else if( HasRowid(pIdx->pTable) ){
159489 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
159490 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
159491 for(j=0; j<pPk->nKeyCol; j++){
159492 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
159496 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
159499 if( pLevel->iLeftJoin==0 ){
159504 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
159507 if( pIdx->pPartIdxWhere ){
159508 whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
159511 testcase( pIdx->pPartIdxWhere );
159513 ** The OR-optimization doesn't work for the right hand table of
159515 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 );
159519 if( (pLoop->wsFlags & WHERE_ONEROW)
159520 || (pLevel->u.in.nIn && regBignull==0 && whereLoopIsOneRow(pLoop))
159522 pLevel->op = OP_Noop;
159524 pLevel->op = OP_Prev;
159526 pLevel->op = OP_Next;
159528 pLevel->p1 = iIdxCur;
159529 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
159530 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
159531 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
159533 assert( pLevel->p5==0 );
159539 if( pLoop->wsFlags & WHERE_MULTI_OR ){
159578 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
159583 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
159584 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
159586 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
159588 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
159596 Table *pTab = pTabItem->pTab;
159598 pTerm = pLoop->aLTerm[0];
159600 assert( pTerm->eOperator & WO_OR );
159601 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
159602 pOrWc = &pTerm->u.pOrInfo->wc;
159603 pLevel->op = OP_Return;
159604 pLevel->p1 = regReturn;
159610 if( pWInfo->nLevel>1 ){
159613 nNotReady = pWInfo->nLevel - iLevel - 1;
159615 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
159617 pOrTab->nAlloc = (u8)(nNotReady + 1);
159618 pOrTab->nSrc = pOrTab->nAlloc;
159619 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
159620 origSrc = pWInfo->pTabList->a;
159622 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
159625 pOrTab = pWInfo->pTabList;
159636 ** correct response for the end-of-loop code (the OP_Return) is to
159640 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
159642 regRowset = ++pParse->nMem;
159646 regRowset = pParse->nTab++;
159647 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
159650 regRowid = ++pParse->nMem;
159660 ** the "interesting" terms of z - terms that did not originate in the
159668 ** 2022-02-04: Do not push down slices of a row-value comparison.
159670 ** the initialization of the right-hand operand of the vector comparison
159674 ** 2022-03-03: Do not push down expressions that involve subqueries.
159675 ** The subquery might get coded as a subroutine. Any table-references
159676 ** in the subquery might be resolved to index-references for the index on
159679 ** index-references will not work. tag-20220303a
159682 if( pWC->nTerm>1 ){
159684 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
159685 Expr *pExpr = pWC->a[iTerm].pExpr;
159686 if( &pWC->a[iTerm] == pTerm ) continue;
159687 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
159688 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
159689 testcase( pWC->a[iTerm].wtFlags & TERM_SLICE );
159690 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED|TERM_SLICE))!=0 ){
159693 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
159694 if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */
159702 ** prevents sqlite3PExpr() from applying the AND short-circuit
159710 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
159712 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
159713 for(ii=0; ii<pOrWc->nTerm; ii++){
159714 WhereTerm *pOrTerm = &pOrWc->a[ii];
159715 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
159716 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
159717 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
159724 if( db->mallocFailed ){
159729 pAndExpr->pLeft = pOrExpr;
159734 WHERETRACE(0xffffffff, ("Subplan for OR-clause:\n"));
159737 assert( pSubWInfo || pParse->nErr );
159741 pParse, pOrTab, &pSubWInfo->a[0], 0
159743 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
159745 /* This is the sub-WHERE clause body. First skip over
159746 ** duplicate rows from prior sub-WHERE clauses, and record the
159748 ** row will be skipped in subsequent sub-WHERE clauses.
159750 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
159751 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
159753 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
159759 int nPk = pPk->nKeyCol;
159766 int iCol = pPk->aiColumn[iPk];
159778 ** the temp table. And if iSet is -1, assume that there is no
159801 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
159804 /* The pSubWInfo->untestedTerms flag means that this OR term
159809 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
159811 /* If all of the OR-connected terms are optimized using the same
159817 ** uses an index, and this is either the first OR-connected term
159823 pSubLoop = pSubWInfo->a[0].pWLoop;
159824 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
159825 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
159826 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
159827 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
159829 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
159830 pCov = pSubLoop->u.btree.pIndex;
159835 pWInfo->bDeferredSeek = 1;
159846 assert( pLevel->pWLoop==pLoop );
159847 assert( (pLoop->wsFlags & WHERE_MULTI_OR)!=0 );
159848 assert( (pLoop->wsFlags & WHERE_IN_ABLE)==0 );
159849 pLevel->u.pCoveringIdx = pCov;
159850 if( pCov ) pLevel->iIdxCur = iCovCur;
159852 pAndExpr->pLeft = 0;
159856 sqlite3VdbeGoto(v, pLevel->addrBrk);
159861 ** loop. The byte-code formatter will use that P2 value as a hint to
159863 ** See tag-20220407a in vdbe.c and shell.c */
159864 assert( pLevel->op==OP_Return );
159865 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
159867 if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); }
159879 if( pTabItem->fg.isRecursive ){
159881 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
159882 pLevel->op = OP_Noop;
159885 pLevel->op = aStep[bRev];
159886 pLevel->p1 = iCur;
159887 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
159890 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
159895 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
159907 ** sub-queries.
159913 ** more complex one is call the "push-down" optimization in MySQL. Here
159914 ** in SQLite, the name is "MySQL push-down", since there is also another
159915 ** totally unrelated optimization called "WHERE-clause push-down".
159921 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
159924 testcase( pTerm->wtFlags & TERM_VIRTUAL );
159925 testcase( pTerm->wtFlags & TERM_CODED );
159926 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
159927 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
159928 testcase( pWInfo->untestedTerms==0
159929 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
159930 pWInfo->untestedTerms = 1;
159933 pE = pTerm->pExpr;
159935 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ){
159938 ** join processing. tag-20220513a */
159940 }else if( (pTabItem->fg.jointype & JT_LEFT)==JT_LEFT
159944 Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin);
159945 if( m & pLevel->notReady ){
159951 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
159955 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
159960 if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
159969 u32 x = pLevel->iLikeRepCntr;
159980 pWC->nTerm-j, pTerm, iLoop));
159984 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
159989 pTerm->wtFlags |= TERM_CODED;
160002 for(pTerm=pWC->a, j=pWC->nBase; j>0; j--, pTerm++){
160005 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
160006 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
160007 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
160008 if( pTerm->leftCursor!=iCur ) continue;
160009 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ) continue;
160010 pE = pTerm->pExpr;
160014 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
160018 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
160019 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
160020 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady,
160023 if( pAlt->wtFlags & (TERM_CODED) ) continue;
160024 if( (pAlt->eOperator & WO_IN)
160025 && ExprUseXSelect(pAlt->pExpr)
160026 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
160030 testcase( pAlt->eOperator & WO_EQ );
160031 testcase( pAlt->eOperator & WO_IS );
160032 testcase( pAlt->eOperator & WO_IN );
160034 sEAlt = *pAlt->pExpr;
160035 sEAlt.pLeft = pE->pLeft;
160037 pAlt->wtFlags |= TERM_CODED;
160043 if( pLevel->pRJ ){
160048 WhereRightJoin *pRJ = pLevel->pRJ;
160050 /* pTab is the right-hand table of the RIGHT JOIN. Generate code that
160055 pTab = pWInfo->pTabList->a[pLevel->iFrom].pTab;
160058 sqlite3ExprCodeGetColumnOfTable(v, pTab, pLevel->iTabCur, -1, r+1);
160063 nPk = pPk->nKeyCol;
160066 int iCol = pPk->aiColumn[iPk];
160070 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, 0, r+1, nPk);
160072 VdbeComment((v, "match against %s", pTab->zName));
160074 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pRJ->iMatch, r, r+1, nPk);
160075 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pRJ->regBloom, 0, r+1, nPk);
160084 if( pLevel->iLeftJoin ){
160085 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
160086 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
160088 if( pLevel->pRJ==0 ){
160093 if( pLevel->pRJ ){
160096 ** be in-line with the rest of the code. But at the end, a separate
160100 WhereRightJoin *pRJ = pLevel->pRJ;
160101 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pRJ->regReturn);
160102 pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v);
160103 assert( pParse->withinRJSubrtn < 255 );
160104 pParse->withinRJSubrtn++;
160109 ** appropriate WHERE clause constraint checks. tag-20220513a.
160112 for(pTerm=pWC->a, j=0; j<pWC->nBase; j++, pTerm++){
160113 testcase( pTerm->wtFlags & TERM_VIRTUAL );
160114 testcase( pTerm->wtFlags & TERM_CODED );
160115 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
160116 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
160117 assert( pWInfo->untestedTerms );
160120 if( pTabItem->fg.jointype & JT_LTORJ ) continue;
160121 assert( pTerm->pExpr );
160122 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
160123 pTerm->wtFlags |= TERM_CODED;
160129 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
160135 iLevel, (u64)pLevel->notReady);
160138 return pLevel->notReady;
160142 ** Generate the code for the loop that finds all non-matched terms
160150 Parse *pParse = pWInfo->pParse;
160151 Vdbe *v = pParse->pVdbe;
160152 WhereRightJoin *pRJ = pLevel->pRJ;
160154 WhereClause *pWC = &pWInfo->sWC;
160156 WhereLoop *pLoop = pLevel->pWLoop;
160157 SrcItem *pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
160162 ExplainQueryPlan((pParse, 1, "RIGHT-JOIN %s", pTabItem->pTab->zName));
160163 sqlite3VdbeNoJumpsOutsideSubrtn(v, pRJ->addrSubrtn, pRJ->endSubrtn,
160164 pRJ->regReturn);
160168 assert( pWInfo->a[k].pWLoop->iTab == pWInfo->a[k].iFrom );
160169 pRight = &pWInfo->pTabList->a[pWInfo->a[k].iFrom];
160170 mAll |= pWInfo->a[k].pWLoop->maskSelf;
160171 if( pRight->fg.viaCoroutine ){
160173 v, OP_Null, 0, pRight->regResult,
160174 pRight->regResult + pRight->pSelect->pEList->nExpr-1
160177 sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur);
160178 iIdxCur = pWInfo->a[k].iIdxCur;
160183 if( (pTabItem->fg.jointype & JT_LTORJ)==0 ){
160184 mAll |= pLoop->maskSelf;
160185 for(k=0; k<pWC->nTerm; k++){
160186 WhereTerm *pTerm = &pWC->a[k];
160187 if( (pTerm->wtFlags & (TERM_VIRTUAL|TERM_SLICE))!=0
160188 && pTerm->eOperator!=WO_ROWVAL
160192 if( pTerm->prereqAll & ~mAll ) continue;
160193 if( ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON) ) continue;
160195 sqlite3ExprDup(pParse->db, pTerm->pExpr, 0));
160202 assert( pParse->withinRJSubrtn < 100 );
160203 pParse->withinRJSubrtn++;
160207 int iCur = pLevel->iTabCur;
160208 int r = ++pParse->nMem;
160212 Table *pTab = pTabItem->pTab;
160214 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, r);
160219 nPk = pPk->nKeyCol;
160220 pParse->nMem += nPk - 1;
160222 int iCol = pPk->aiColumn[iPk];
160226 jmp = sqlite3VdbeAddOp4Int(v, OP_Filter, pRJ->regBloom, 0, r, nPk);
160228 sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, addrCont, r, nPk);
160231 sqlite3VdbeAddOp2(v, OP_Gosub, pRJ->regReturn, pRJ->addrSubrtn);
160234 sqlite3ExprDelete(pParse->db, pSubWhere);
160236 assert( pParse->withinRJSubrtn>0 );
160237 pParse->withinRJSubrtn--;
160243 ** 2015-06-08
160270 sqlite3WhereClauseClear(&p->wc);
160278 sqlite3WhereClauseClear(&p->wc);
160285 ** The index in pWC->a[] of the new WhereTerm is returned on success.
160288 ** the db->mallocFailed flag so that higher-level functions can detect it.
160290 ** This routine will increase the size of the pWC->a[] array as necessary.
160299 ** the pWC->a[] array.
160305 if( pWC->nTerm>=pWC->nSlot ){
160306 WhereTerm *pOld = pWC->a;
160307 sqlite3 *db = pWC->pWInfo->pParse->db;
160308 pWC->a = sqlite3WhereMalloc(pWC->pWInfo, sizeof(pWC->a[0])*pWC->nSlot*2 );
160309 if( pWC->a==0 ){
160313 pWC->a = pOld;
160316 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
160317 pWC->nSlot = pWC->nSlot*2;
160319 pTerm = &pWC->a[idx = pWC->nTerm++];
160320 if( (wtFlags & TERM_VIRTUAL)==0 ) pWC->nBase = pWC->nTerm;
160322 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
160324 pTerm->truthProb = 1;
160326 pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p);
160327 pTerm->wtFlags = wtFlags;
160328 pTerm->pWC = pWC;
160329 pTerm->iParent = -1;
160330 memset(&pTerm->eOperator, 0,
160331 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
160353 if( pExpr->pLeft->op==TK_VECTOR
160354 || pExpr->pRight->op==TK_VECTOR
160355 || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) !=
160356 sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft)
160358 pExpr->flags ^= EP_Commuted;
160360 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
160361 if( pExpr->op>=TK_GT ){
160366 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
160367 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
160385 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
160386 c = (u16)(WO_EQ<<(op-TK_EQ));
160424 int cnt; /* Number of non-wildcard prefix characters */
160426 sqlite3 *db = pParse->db; /* Database connection */
160438 pList = pExpr->x.pList;
160439 pLeft = pList->a[1].pExpr;
160441 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
160442 op = pRight->op;
160443 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
160444 Vdbe *pReprepare = pParse->pReprepare;
160445 int iCol = pRight->iColumn;
160450 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
160451 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
160454 z = (u8*)pRight->u.zToken;
160466 ** with a wildcard and if (2) the non-wildcard prefix does not end with
160473 if( (cnt>1 || (cnt>0 && z[0]!=wc[3])) && 255!=(u8)z[cnt-1] ){
160485 zNew = pPrefix->u.zToken;
160501 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
160502 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
160503 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
160504 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
160505 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
160507 if( pLeft->op!=TK_COLUMN
160510 && ALWAYS(pLeft->y.pTab)
160511 && IsVirtual(pLeft->y.pTab)) /* Might be numeric */
160517 if( iTo==1 && zNew[0]=='-' ){
160520 zNew[iTo-1]++;
160522 zNew[iTo-1]--;
160537 Vdbe *v = pParse->pVdbe;
160538 sqlite3VdbeSetVarmask(v, pRight->iColumn);
160540 if( *pisComplete && pRight->u.zToken[1] ){
160549 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
160571 ** ----------------------- ---------------------------------
160599 if( pExpr->op==TK_FUNCTION ){
160614 pList = pExpr->x.pList;
160615 if( pList==0 || pList->nExpr!=2 ){
160619 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
160621 ** the left-hand side operand in their in-fix form.
160626 pCol = pList->a[1].pExpr;
160627 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
160631 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
160633 *ppRight = pList->a[0].pExpr;
160646 ** Historically, xFindFunction expected to see lower-case function
160650 pCol = pList->a[0].pExpr;
160651 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
160652 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
160658 pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
160660 assert( pVtab->pModule!=0 );
160662 pMod = (sqlite3_module *)pVtab->pModule;
160663 if( pMod->xFindFunction!=0 ){
160664 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
160667 *ppRight = pList->a[1].pExpr;
160673 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
160675 Expr *pLeft = pExpr->pLeft;
160676 Expr *pRight = pExpr->pRight;
160677 assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) );
160681 assert( pRight==0 || pRight->op!=TK_COLUMN
160682 || (ExprUseYTab(pRight) && pRight->y.pTab!=0) );
160689 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
160690 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
160691 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
160704 pDerived->flags |= pBase->flags & (EP_OuterON|EP_InnerON);
160705 pDerived->w.iJoin = pBase->w.iJoin;
160713 pWC->a[iChild].iParent = iParent;
160714 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
160715 pWC->a[iParent].nChild++;
160719 ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
160724 if( pTerm->eOperator!=WO_AND ){
160727 if( N<pTerm->u.pAndInfo->wc.nTerm ){
160728 return &pTerm->u.pAndInfo->wc.a[N];
160735 ** two subterms are in disjunction - they are OR-ed together.
160745 ** x<y OR x=y --> x<=y
160746 ** x=y OR x=y --> x=y
160747 ** x<=y OR x<y --> x<=y
160751 ** x<y OR x>y --> x!=y
160759 u16 eOp = pOne->eOperator | pTwo->eOperator;
160765 if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return;
160766 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
160767 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
160770 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
160771 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
160772 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
160773 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
160775 if( (eOp & (eOp-1))!=0 ){
160783 db = pWC->pWInfo->pParse->db;
160784 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
160786 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
160787 pNew->op = op;
160794 ** Analyze a term that consists of two or more OR-connected
160807 ** The term being analyzed must have two or more of OR-connected subterms.
160808 ** A single subterm might be a set of AND-connected sub-subterms.
160846 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
160883 int idxTerm /* Index of the OR-term to be analyzed */
160885 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
160886 Parse *pParse = pWInfo->pParse; /* Parser context */
160887 sqlite3 *db = pParse->db; /* Database connection */
160888 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
160889 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
160892 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
160902 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
160903 assert( pExpr->op==TK_OR );
160904 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
160906 pTerm->wtFlags |= TERM_ORINFO;
160907 pOrWc = &pOrInfo->wc;
160908 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
160912 if( db->mallocFailed ) return;
160913 assert( pOrWc->nTerm>=2 );
160920 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
160921 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
160923 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
160931 pOrTerm->u.pAndInfo = pAndInfo;
160932 pOrTerm->wtFlags |= TERM_ANDINFO;
160933 pOrTerm->eOperator = WO_AND;
160934 pOrTerm->leftCursor = -1;
160935 pAndWC = &pAndInfo->wc;
160936 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
160937 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
160938 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
160940 pAndWC->pOuter = pWC;
160941 if( !db->mallocFailed ){
160942 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
160943 assert( pAndTerm->pExpr );
160944 if( allowedOp(pAndTerm->pExpr->op)
160945 || pAndTerm->eOperator==WO_AUX
160947 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
160953 }else if( pOrTerm->wtFlags & TERM_COPIED ){
160958 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
160959 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
160960 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
160961 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
160964 if( (pOrTerm->eOperator & WO_EQ)==0 ){
160976 pOrInfo->indexable = indexable;
160977 pTerm->eOperator = WO_OR;
160978 pTerm->leftCursor = -1;
160980 pWC->hasOr = 1;
160983 /* For a two-way OR, attempt to implementation case 2.
160985 if( indexable && pOrWc->nTerm==2 ){
160988 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
160991 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
161002 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
161005 ** something other than == on a column in the single table. The 1-bit
161009 ** sure the same column is used on all terms. The 2-bit case is when
161020 int iColumn = -1; /* Column index on lhs of IN operator */
161021 int iCursor = -1; /* Table cursor common to all terms */
161032 pOrTerm = pOrWc->a;
161033 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
161034 assert( pOrTerm->eOperator & WO_EQ );
161035 pOrTerm->wtFlags &= ~TERM_OK;
161036 if( pOrTerm->leftCursor==iCursor ){
161037 /* This is the 2-bit case and we are on the second iteration and
161042 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
161043 pOrTerm->leftCursor))==0 ){
161048 testcase( pOrTerm->wtFlags & TERM_COPIED );
161049 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
161050 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
161053 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
161054 iColumn = pOrTerm->u.x.leftColumn;
161055 iCursor = pOrTerm->leftCursor;
161056 pLeft = pOrTerm->pExpr->pLeft;
161064 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
161072 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
161073 assert( pOrTerm->eOperator & WO_EQ );
161074 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
161075 if( pOrTerm->leftCursor!=iCursor ){
161076 pOrTerm->wtFlags &= ~TERM_OK;
161077 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR
161078 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
161083 /* If the right-hand side is also a column, then the affinities
161087 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
161088 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
161092 pOrTerm->wtFlags |= TERM_OK;
161108 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
161109 if( (pOrTerm->wtFlags & TERM_OK)==0 ) continue;
161110 assert( pOrTerm->eOperator & WO_EQ );
161111 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
161112 assert( pOrTerm->leftCursor==iCursor );
161113 assert( pOrTerm->u.x.leftColumn==iColumn );
161114 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
161115 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
161116 pLeft = pOrTerm->pExpr->pLeft;
161125 pNew->x.pList = pList;
161129 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */
161157 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
161158 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
161160 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
161161 aff2 = sqlite3ExprAffinity(pExpr->pRight);
161169 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
161180 SrcList *pSrc = pS->pSrc;
161181 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
161182 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
161183 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
161184 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
161185 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
161188 for(i=0; i<pSrc->nSrc; i++){
161189 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
161190 if( pSrc->a[i].fg.isUsing==0 ){
161191 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].u3.pOn);
161193 if( pSrc->a[i].fg.isTabFunc ){
161194 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
161198 pS = pS->pPrior;
161209 ** of the column that is indexed, or XN_EXPR (-2) if an expression is being
161220 int j /* Start looking with the j-th pFrom entry */
161226 iCur = pFrom->a[j].iCursor;
161227 for(pIdx=pFrom->a[j].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
161228 if( pIdx->aColExpr==0 ) continue;
161229 for(i=0; i<pIdx->nKeyCol; i++){
161230 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
161231 assert( pIdx->bHasExpr );
161232 if( sqlite3ExprCompareSkip(pExpr,pIdx->aColExpr->a[i].pExpr,iCur)==0
161233 && !sqlite3ExprIsConstant(0,pIdx->aColExpr->a[i].pExpr)
161241 }while( ++j < pFrom->nSrc );
161258 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
161260 pExpr = pExpr->x.pList->a[0].pExpr;
161263 if( pExpr->op==TK_COLUMN ){
161264 aiCurCol[0] = pExpr->iTable;
161265 aiCurCol[1] = pExpr->iColumn;
161269 for(i=0; i<pFrom->nSrc; i++){
161271 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
161272 if( pIdx->aColExpr ){
161304 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
161308 Bitmask prereqLeft; /* Prerequisites of the pExpr->pLeft */
161314 int op; /* Top-level operator. pExpr->op */
161315 Parse *pParse = pWInfo->pParse; /* Parsing context */
161316 sqlite3 *db = pParse->db; /* Database connection */
161320 if( db->mallocFailed ){
161323 assert( pWC->nTerm > idxTerm );
161324 pTerm = &pWC->a[idxTerm];
161325 pMaskSet = &pWInfo->sMaskSet;
161326 pExpr = pTerm->pExpr;
161328 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
161329 pMaskSet->bVarSelect = 0;
161330 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
161331 op = pExpr->op;
161333 assert( pExpr->pRight==0 );
161336 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
161338 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
161340 prereqAll = prereqLeft | pTerm->prereqRight;
161342 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
161343 if( pExpr->pLeft==0
161345 || pExpr->x.pList!=0
161349 prereqAll = prereqLeft | pTerm->prereqRight;
161352 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
161363 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
161366 extraRight = x-1; /* ON clause terms may not be used with an index
161379 if( ALWAYS(pSrc->nSrc>0) && (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
161386 pTerm->prereqAll = prereqAll;
161387 pTerm->leftCursor = -1;
161388 pTerm->iParent = -1;
161389 pTerm->eOperator = 0;
161392 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
161393 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
161394 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
161396 if( pTerm->u.x.iField>0 ){
161398 assert( pLeft->op==TK_VECTOR );
161400 pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr;
161404 pTerm->leftCursor = aiCurCol[0];
161405 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
161406 pTerm->u.x.leftColumn = aiCurCol[1];
161407 pTerm->eOperator = operatorMask(op) & opMask;
161409 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
161416 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
161417 assert( pTerm->u.x.iField==0 );
161418 if( pTerm->leftCursor>=0 ){
161421 if( db->mallocFailed ){
161427 pNew = &pWC->a[idxNew];
161429 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
161430 pTerm = &pWC->a[idxTerm];
161431 pTerm->wtFlags |= TERM_COPIED;
161434 pTerm->eOperator |= WO_EQUIV;
161441 pNew->wtFlags |= exprCommute(pParse, pDup);
161442 pNew->leftCursor = aiCurCol[0];
161443 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
161444 pNew->u.x.leftColumn = aiCurCol[1];
161446 pNew->prereqRight = prereqLeft | extraRight;
161447 pNew->prereqAll = prereqAll;
161448 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
161455 pExpr->op = TK_TRUEFALSE; /* See tag-20230504-1 */
161456 pExpr->u.zToken = "false";
161458 pTerm->prereqAll = 0;
161459 pTerm->eOperator = 0;
161479 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
161484 pList = pExpr->x.pList;
161486 assert( pList->nExpr==2 );
161491 sqlite3ExprDup(db, pExpr->pLeft, 0),
161492 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
161497 pTerm = &pWC->a[idxTerm];
161507 else if( pExpr->op==TK_OR ){
161508 assert( pWC->op==TK_AND );
161510 pTerm = &pWC->a[idxTerm];
161519 else if( pExpr->op==TK_NOTNULL ){
161520 if( pExpr->pLeft->op==TK_COLUMN
161521 && pExpr->pLeft->iColumn>=0
161525 Expr *pLeft = pExpr->pLeft;
161536 pNewTerm = &pWC->a[idxNew];
161537 pNewTerm->prereqRight = 0;
161538 pNewTerm->leftCursor = pLeft->iTable;
161539 pNewTerm->u.x.leftColumn = pLeft->iColumn;
161540 pNewTerm->eOperator = WO_GT;
161542 pTerm = &pWC->a[idxTerm];
161543 pTerm->wtFlags |= TERM_COPIED;
161544 pNewTerm->prereqAll = pTerm->prereqAll;
161560 ** for LIKE) then the lower-bound is made all uppercase and the upper-
161564 else if( pExpr->op==TK_FUNCTION
161565 && pWC->op==TK_AND
161569 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
161578 pLeft = pExpr->x.pList->a[1].pExpr;
161584 /* Convert the lower bound to upper-case and the upper bound to
161585 ** lower-case (upper-case is less than lower-case in ASCII) so that
161588 if( noCase && !pParse->db->mallocFailed ){
161591 pTerm->wtFlags |= TERM_LIKE;
161592 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
161593 pStr1->u.zToken[i] = sqlite3Toupper(c);
161594 pStr2->u.zToken[i] = sqlite3Tolower(c);
161598 if( !db->mallocFailed ){
161600 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
161609 if( c=='A'-1 ) isComplete = 0;
161631 pTerm = &pWC->a[idxTerm];
161639 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
161640 ** new terms for each component comparison - "a = ?" and "b = ?". The
161645 ** is not a sub-select.
161647 ** tag-20220128a
161649 if( (pExpr->op==TK_EQ || pExpr->op==TK_IS)
161650 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
161651 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
161652 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
161653 || (pExpr->pRight->flags & EP_xIsSelect)==0)
161654 && pWC->op==TK_AND
161660 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft);
161661 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft);
161663 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
161668 pTerm = &pWC->a[idxTerm];
161669 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
161670 pTerm->eOperator = WO_ROWVAL;
161673 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
161682 else if( pExpr->op==TK_IN
161683 && pTerm->u.x.iField==0
161684 && pExpr->pLeft->op==TK_VECTOR
161686 && (pExpr->x.pSelect->pPrior==0 || (pExpr->x.pSelect->selFlags & SF_Values))
161688 && pExpr->x.pSelect->pWin==0
161690 && pWC->op==TK_AND
161693 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
161696 pWC->a[idxNew].u.x.iField = i+1;
161712 else if( pWC->op==TK_AND ){
161715 while( res-- > 0 ){
161728 pNewExpr->w.iJoin = pExpr->w.iJoin;
161732 pNewTerm = &pWC->a[idxNew];
161733 pNewTerm->prereqRight = prereqExpr;
161734 pNewTerm->leftCursor = pLeft->iTable;
161735 pNewTerm->u.x.leftColumn = pLeft->iColumn;
161736 pNewTerm->eOperator = WO_AUX;
161737 pNewTerm->eMatchOp = eOp2;
161739 pTerm = &pWC->a[idxTerm];
161740 pTerm->wtFlags |= TERM_COPIED;
161741 pNewTerm->prereqAll = pTerm->prereqAll;
161751 testcase( pTerm!=&pWC->a[idxTerm] );
161752 pTerm = &pWC->a[idxTerm];
161753 pTerm->prereqRight |= extraRight;
161780 pWC->op = op;
161783 if( pE2->op!=op ){
161786 sqlite3WhereSplit(pWC, pE2->pLeft, op);
161787 sqlite3WhereSplit(pWC, pE2->pRight, op);
161794 ** where-clause passed as the first argument. The value for the term
161809 Parse *pParse = pWC->pWInfo->pParse;
161810 sqlite3 *db = pParse->db;
161818 pVal->u.iValue = iVal;
161823 pVal->iTable = iReg;
161830 pTerm = &pWC->a[idx];
161831 pTerm->leftCursor = iCsr;
161832 pTerm->eOperator = WO_AUX;
161833 pTerm->eMatchOp = eMatchOp;
161856 assert( p!=0 && p->pLimit!=0 ); /* 1 -- checked by caller */
161857 if( p->pGroupBy==0
161858 && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
161859 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
161861 ExprList *pOrderBy = p->pOrderBy;
161862 int iCsr = p->pSrc->a[0].iCursor;
161866 for(ii=0; ii<pWC->nTerm; ii++){
161867 if( pWC->a[ii].wtFlags & TERM_CODED ){
161869 ** other, subsequent terms. It can be ignored. See tag-20220128a */
161870 assert( pWC->a[ii].wtFlags & TERM_VIRTUAL );
161871 assert( pWC->a[ii].eOperator==WO_ROWVAL );
161874 if( pWC->a[ii].nChild ){
161876 ** pWC->a[] array. So this term can be ignored, as a LIMIT clause
161881 if( pWC->a[ii].leftCursor!=iCsr ) return;
161882 if( pWC->a[ii].prereqRight!=0 ) return;
161887 for(ii=0; ii<pOrderBy->nExpr; ii++){
161888 Expr *pExpr = pOrderBy->a[ii].pExpr;
161889 if( pExpr->op!=TK_COLUMN ) return;
161890 if( pExpr->iTable!=iCsr ) return;
161891 if( pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) return;
161895 /* All conditions are met. Add the terms to the where-clause object. */
161896 assert( p->pLimit->op==TK_LIMIT );
161897 if( p->iOffset!=0 && (p->selFlags & SF_Compound)==0 ){
161898 whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
161901 if( p->iOffset==0 || (p->selFlags & SF_Compound)==0 ){
161902 whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
161915 pWC->pWInfo = pWInfo;
161916 pWC->hasOr = 0;
161917 pWC->pOuter = 0;
161918 pWC->nTerm = 0;
161919 pWC->nBase = 0;
161920 pWC->nSlot = ArraySize(pWC->aStatic);
161921 pWC->a = pWC->aStatic;
161930 sqlite3 *db = pWC->pWInfo->pParse->db;
161931 assert( pWC->nTerm>=pWC->nBase );
161932 if( pWC->nTerm>0 ){
161933 WhereTerm *a = pWC->a;
161934 WhereTerm *aLast = &pWC->a[pWC->nTerm-1];
161937 /* Verify that every term past pWC->nBase is virtual */
161938 for(i=pWC->nBase; i<pWC->nTerm; i++){
161939 assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 );
161943 assert( a->eMatchOp==0 || a->eOperator==WO_AUX );
161944 if( a->wtFlags & TERM_DYNAMIC ){
161945 sqlite3ExprDelete(db, a->pExpr);
161947 if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){
161948 if( a->wtFlags & TERM_ORINFO ){
161949 assert( (a->wtFlags & TERM_ANDINFO)==0 );
161950 whereOrInfoDelete(db, a->u.pOrInfo);
161952 assert( (a->wtFlags & TERM_ANDINFO)!=0 );
161953 whereAndInfoDelete(db, a->u.pAndInfo);
161968 ** sqlite3WhereExprUsage(MaskSet, Expr) ->
161973 ** sqlite3WhereExprUsageNN(MaskSet, Expr) ->
161978 ** sqlite3WhereExprListUsage(MaskSet, ExprList) ->
161984 ** sqlite3WhereExprUsageFull(MaskSet, ExprList) ->
161991 ** "no-inline" function to avoid the stack push overhead in the
161999 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
162000 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
162001 if( p->pRight ){
162002 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
162003 assert( p->x.pList==0 );
162005 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
162006 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
162007 }else if( p->x.pList ){
162008 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
162011 if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && ExprUseYWin(p) ){
162012 assert( p->y.pWin!=0 );
162013 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
162014 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
162015 mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
162021 if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
162022 return sqlite3WhereGetMask(pMaskSet, p->iTable);
162024 assert( p->op!=TK_IF_NULL_ROW );
162036 for(i=0; i<pList->nExpr; i++){
162037 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
162057 for(i=pWC->nTerm-1; i>=0; i--){
162063 ** For table-valued-functions, transform the function arguments into
162079 if( pItem->fg.isTabFunc==0 ) return;
162080 pTab = pItem->pTab;
162082 pArgs = pItem->u1.pFuncArg;
162084 for(j=k=0; j<pArgs->nExpr; j++){
162087 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
162088 if( k>=pTab->nCol ){
162089 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
162090 pTab->zName, j);
162093 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
162095 pColRef->iTable = pItem->iCursor;
162096 pColRef->iColumn = k++;
162098 pColRef->y.pTab = pTab;
162099 pItem->colUsed |= sqlite3ExprColUsed(pColRef);
162101 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
162103 if( pItem->fg.jointype & (JT_LEFT|JT_RIGHT) ){
162104 testcase( pItem->fg.jointype & JT_LEFT ); /* testtag-20230227a */
162105 testcase( pItem->fg.jointype & JT_RIGHT ); /* testtag-20230227b */
162108 testcase( pItem->fg.jointype & JT_LTORJ ); /* testtag-20230227c */
162111 sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType);
162167 return pWInfo->nRowOut;
162175 return pWInfo->eDistinct;
162187 return pWInfo->nOBSat<0 ? 0 : pWInfo->nOBSat;
162191 ** In the ORDER BY LIMIT optimization, if the inner-most loop is known
162193 ** inner-most loop did not fit within the sorter, then we can skip all
162196 ** loop - the loop immediately outside the inner-most.
162203 ** be the continuation for the second-inner-most loop. If the ORDER BY
162205 ** be the continuation for the inner-most loop.
162208 ** inner-most loop, in the sense that a correct answer will result.
162215 if( !pWInfo->bOrderedInnerLoop ){
162217 ** continuation of the inner-most loop. */
162218 return pWInfo->iContinue;
162220 pInner = &pWInfo->a[pWInfo->nLevel-1];
162221 assert( pInner->addrNxt!=0 );
162222 return pInner->pRJ ? pWInfo->iContinue : pInner->addrNxt;
162227 ** the aggregate-step call to min() or max(), check to see if any
162239 if( !pWInfo->bOrderedInnerLoop ) return;
162240 if( pWInfo->nOBSat==0 ) return;
162241 for(i=pWInfo->nLevel-1; i>=0; i--){
162242 pInner = &pWInfo->a[i];
162243 if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN)!=0 ){
162244 sqlite3VdbeGoto(v, pInner->addrNxt);
162248 sqlite3VdbeGoto(v, pWInfo->iBreak);
162256 assert( pWInfo->iContinue!=0 );
162257 return pWInfo->iContinue;
162265 return pWInfo->iBreak;
162272 ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
162279 ** Either value may be -1, indicating that cursor is not used.
162282 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
162286 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
162288 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
162290 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
162294 return pWInfo->eOnePass;
162302 return pWInfo->bDeferredSeek;
162309 pDest->n = pSrc->n;
162310 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
162323 LogEst rRun, /* Run-cost of the new entry */
162328 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
162329 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
162332 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
162336 if( pSet->n<N_OR_COST ){
162337 p = &pSet->a[pSet->n++];
162338 p->nOut = nOut;
162340 p = pSet->a;
162341 for(i=1; i<pSet->n; i++){
162342 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
162344 if( p->rRun<=rRun ) return 0;
162347 p->prereq = prereq;
162348 p->rRun = rRun;
162349 if( p->nOut>nOut ) p->nOut = nOut;
162359 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
162360 assert( pMaskSet->n>0 || pMaskSet->ix[0]<0 );
162361 assert( iCursor>=-1 );
162362 if( pMaskSet->ix[0]==iCursor ){
162365 for(i=1; i<pMaskSet->n; i++){
162366 if( pMaskSet->ix[i]==iCursor ){
162377 pBlock = sqlite3DbMallocRawNN(pWInfo->pParse->db, nByte+sizeof(*pBlock));
162379 pBlock->pNext = pWInfo->pMemToFree;
162380 pBlock->sz = nByte;
162381 pWInfo->pMemToFree = pBlock;
162390 pOldBlk--;
162391 assert( pOldBlk->sz<nByte );
162392 memcpy(pNew, pOld, pOldBlk->sz);
162402 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
162406 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
162407 pMaskSet->ix[pMaskSet->n++] = iCursor;
162411 ** If the right-hand branch of the expression is a TK_COLUMN, then return
162412 ** a pointer to the right-hand branch. Otherwise, return NULL.
162415 p = sqlite3ExprSkipCollateAndLikely(p->pRight);
162416 if( ALWAYS(p!=0) && p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
162436 Expr *pX = pTerm->pExpr;
162439 assert( pTerm->eOperator & WO_IN );
162441 if( sqlite3ExprIsVector(pX->pLeft) ){
162442 int iField = pTerm->u.x.iField - 1;
162445 inexpr.pLeft = pX->pLeft->x.pList->a[iField].pExpr;
162447 inexpr.pRight = pX->x.pSelect->pEList->a[iField].pExpr;
162453 return pRet ? pRet->zName : sqlite3StrBINARY;
162465 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
162467 WhereClause *pWC; /* Shorthand for pScan->pWC */
162469 int k = pScan->k; /* Where to start scanning */
162471 assert( pScan->iEquiv<=pScan->nEquiv );
162472 pWC = pScan->pWC;
162474 iColumn = pScan->aiColumn[pScan->iEquiv-1];
162475 iCur = pScan->aiCur[pScan->iEquiv-1];
162479 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
162480 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 || pTerm->leftCursor<0 );
162481 if( pTerm->leftCursor==iCur
162482 && pTerm->u.x.leftColumn==iColumn
162484 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
162485 pScan->pIdxExpr,iCur)==0)
162486 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_OuterON))
162488 if( (pTerm->eOperator & WO_EQUIV)!=0
162489 && pScan->nEquiv<ArraySize(pScan->aiCur)
162490 && (pX = whereRightSubexprIsColumn(pTerm->pExpr))!=0
162493 for(j=0; j<pScan->nEquiv; j++){
162494 if( pScan->aiCur[j]==pX->iTable
162495 && pScan->aiColumn[j]==pX->iColumn ){
162499 if( j==pScan->nEquiv ){
162500 pScan->aiCur[j] = pX->iTable;
162501 pScan->aiColumn[j] = pX->iColumn;
162502 pScan->nEquiv++;
162505 if( (pTerm->eOperator & pScan->opMask)!=0 ){
162507 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
162509 Parse *pParse = pWC->pWInfo->pParse;
162510 pX = pTerm->pExpr;
162512 if( (pTerm->eOperator & WO_IN) ){
162513 zCollName = indexInAffinityOk(pParse, pTerm, pScan->idxaff);
162517 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
162520 assert(pX->pLeft);
162522 zCollName = pColl ? pColl->zName : sqlite3StrBINARY;
162525 if( sqlite3StrICmp(zCollName, pScan->zCollName) ){
162529 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
162530 && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0))
162531 && pX->op==TK_COLUMN
162532 && pX->iTable==pScan->aiCur[0]
162533 && pX->iColumn==pScan->aiColumn[0]
162535 testcase( pTerm->eOperator & WO_IS );
162538 pScan->pWC = pWC;
162539 pScan->k = k+1;
162543 sqlite3DebugPrintf("SCAN-TERM %p: nEquiv=%d",
162544 pTerm, pScan->nEquiv);
162545 for(ii=0; ii<pScan->nEquiv; ii++){
162547 pScan->aiCur[ii], pScan->aiColumn[ii]);
162556 pWC = pWC->pOuter;
162559 if( pScan->iEquiv>=pScan->nEquiv ) break;
162560 pWC = pScan->pOrigWC;
162562 pScan->iEquiv++;
162569 ** It is factored out into a separate tail-recursion subroutine so that
162570 ** the normal whereScanInit() routine, which is a high-runner, does not
162574 pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
162605 pScan->pOrigWC = pWC;
162606 pScan->pWC = pWC;
162607 pScan->pIdxExpr = 0;
162608 pScan->idxaff = 0;
162609 pScan->zCollName = 0;
162610 pScan->opMask = opMask;
162611 pScan->k = 0;
162612 pScan->aiCur[0] = iCur;
162613 pScan->nEquiv = 1;
162614 pScan->iEquiv = 1;
162617 iColumn = pIdx->aiColumn[j];
162618 if( iColumn==pIdx->pTable->iPKey ){
162621 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
162622 pScan->zCollName = pIdx->azColl[j];
162624 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
162625 pScan->zCollName = pIdx->azColl[j];
162626 pScan->aiColumn[0] = XN_EXPR;
162632 pScan->aiColumn[0] = iColumn;
162643 ** Search for terms matching the iColumn-th column of pIdx
162644 ** rather than the iColumn-th column of table iCur.
162648 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
162655 ** then try for the one with no dependencies on <expr> - in other words where
162658 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
162676 if( (p->prereqRight & notReady)==0 ){
162677 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
162678 testcase( p->eOperator & WO_IS );
162689 ** This function searches pList for an entry that matches the iCol-th column
162692 ** If such an expression is found, its index in pList->a[] is returned. If
162693 ** no expression is found, -1 is returned.
162703 const char *zColl = pIdx->azColl[iCol];
162705 for(i=0; i<pList->nExpr; i++){
162706 Expr *p = sqlite3ExprSkipCollateAndLikely(pList->a[i].pExpr);
162708 && (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN)
162709 && p->iColumn==pIdx->aiColumn[iCol]
162710 && p->iTable==iBase
162712 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
162713 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
162719 return -1;
162723 ** Return TRUE if the iCol-th column of index pIdx is NOT NULL
162728 assert( iCol>=0 && iCol<pIdx->nColumn );
162729 j = pIdx->aiColumn[iCol];
162731 return pIdx->pTable->aCol[j].notNull;
162732 }else if( j==(-1) ){
162735 assert( j==(-2) );
162742 ** Return true if the DISTINCT expression-list passed as the third argument
162746 ** DISTINCT list are collectively unique and individually non-null.
162759 /* If there is more than one table or sub-select in the FROM clause of
162762 if( pTabList->nSrc!=1 ) return 0;
162763 iBase = pTabList->a[0].iCursor;
162764 pTab = pTabList->a[0].pTab;
162767 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
162768 ** current SELECT is a correlated sub-query.
162770 for(i=0; i<pDistinct->nExpr; i++){
162771 Expr *p = sqlite3ExprSkipCollateAndLikely(pDistinct->a[i].pExpr);
162773 if( p->op!=TK_COLUMN && p->op!=TK_AGG_COLUMN ) continue;
162774 if( p->iTable==iBase && p->iColumn<0 ) return 1;
162785 ** comparison and select-list expressions must match those of the index.
162790 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
162792 if( pIdx->pPartIdxWhere ) continue;
162793 for(i=0; i<pIdx->nKeyCol; i++){
162799 if( i==pIdx->nKeyCol ){
162813 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
162820 ** opcodes into OP_Copy when the table is being accessed via co-routine
162833 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
162835 Vdbe *v = pParse->pVdbe;
162838 if( pParse->db->mallocFailed ) return;
162840 if( pOp->p1!=iTabCur ) continue;
162841 if( pOp->opcode==OP_Column ){
162843 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
162847 pOp->opcode = OP_Copy;
162848 pOp->p1 = pOp->p2 + iRegister;
162849 pOp->p2 = pOp->p3;
162850 pOp->p3 = 0;
162851 pOp->p5 = 2; /* Cause the MEM_Subtype flag to be cleared */
162852 }else if( pOp->opcode==OP_Rowid ){
162854 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
162858 pOp->opcode = OP_Sequence;
162859 pOp->p1 = iAutoidxCur;
162862 pOp->opcode = OP_Null;
162863 pOp->p3 = 0;
162874 ** are no-ops.
162883 sqlite3DebugPrintf("sqlite3_index_info inputs for %s:\n", pTab->zName);
162884 for(i=0; i<p->nConstraint; i++){
162888 p->aConstraint[i].iColumn,
162889 p->aConstraint[i].iTermOffset,
162890 p->aConstraint[i].op,
162891 p->aConstraint[i].usable,
162894 for(i=0; i<p->nOrderBy; i++){
162897 p->aOrderBy[i].iColumn,
162898 p->aOrderBy[i].desc);
162907 sqlite3DebugPrintf("sqlite3_index_info outputs for %s:\n", pTab->zName);
162908 for(i=0; i<p->nConstraint; i++){
162911 p->aConstraintUsage[i].argvIndex,
162912 p->aConstraintUsage[i].omit);
162914 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
162915 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
162916 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
162917 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
162918 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
162936 ** not-NULL condition on the left table of the RIGHT JOIN.
162942 assert( (pSrc->fg.jointype&(JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 ); /* By caller */
162943 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
162944 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
162945 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) )
162946 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
162947 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)
162948 || pTerm->pExpr->w.iJoin != pSrc->iCursor
162952 if( (pSrc->fg.jointype & (JT_LEFT|JT_RIGHT))!=0
162953 && ExprHasProperty(pTerm->pExpr, EP_InnerON)
162974 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
162975 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
162976 assert( (pSrc->fg.jointype & JT_RIGHT)==0 );
162977 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
162982 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
162983 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
162984 if( pTerm->u.x.leftColumn<0 ) return 0;
162985 aff = pSrc->pTab->aCol[pTerm->u.x.leftColumn].affinity;
162986 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
162987 testcase( pTerm->pExpr->op==TK_IS );
163013 if( IS_STMT_SCANSTATUS(pParse->db) && pParse->explain!=2 ){
163014 Table *pTab = pIdx->pTable;
163018 sqlite3_str *pStr = sqlite3_str_new(pParse->db);
163019 sqlite3_str_appendf(pStr,"CREATE AUTOMATIC INDEX ON %s(", pTab->zName);
163020 assert( pIdx->nColumn>1 );
163021 assert( pIdx->aiColumn[pIdx->nColumn-1]==XN_ROWID );
163022 for(ii=0; ii<(pIdx->nColumn-1); ii++){
163024 int iCol = pIdx->aiColumn[ii];
163026 zName = pTab->aCol[iCol].zCnName;
163032 sqlite3OomFault(pParse->db);
163058 WhereTerm *pWCEnd; /* End of pWC->a[] */
163067 int mxBitCol; /* Maximum column in pSrc->colUsed */
163087 v = pParse->pVdbe;
163094 pTabList = pWC->pWInfo->pTabList;
163095 pSrc = &pTabList->a[pLevel->iFrom];
163096 pTable = pSrc->pTab;
163097 pWCEnd = &pWC->a[pWC->nTerm];
163098 pLoop = pLevel->pWLoop;
163100 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
163101 Expr *pExpr = pTerm->pExpr;
163105 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
163106 && sqlite3ExprIsSingleTableConstraint(pExpr, pTabList, pLevel->iFrom, 0)
163109 sqlite3ExprDup(pParse->db, pExpr, 0));
163114 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
163115 iCol = pTerm->u.x.leftColumn;
163116 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
163118 testcase( iCol==BMS-1 );
163121 "automatic index on %s(%s)", pTable->zName,
163122 pTable->aCol[iCol].zCnName);
163126 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
163129 pLoop->aLTerm[nKeyCol++] = pTerm;
163134 assert( nKeyCol>0 || pParse->db->mallocFailed );
163135 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
163136 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
163150 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
163152 mxBitCol = MIN(BMS-1,pTable->nCol);
163153 testcase( pTable->nCol==BMS-1 );
163154 testcase( pTable->nCol==BMS-2 );
163158 if( pSrc->colUsed & MASKBIT(BMS-1) ){
163159 nKeyCol += pTable->nCol - BMS + 1;
163163 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
163165 pLoop->u.btree.pIndex = pIdx;
163166 pIdx->zName = "auto-index";
163167 pIdx->pTable = pTable;
163170 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
163174 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
163175 iCol = pTerm->u.x.leftColumn;
163176 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
163177 testcase( iCol==BMS-1 );
163180 Expr *pX = pTerm->pExpr;
163182 pIdx->aiColumn[n] = pTerm->u.x.leftColumn;
163184 assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */
163185 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
163187 if( ALWAYS(pX->pLeft!=0)
163188 && sqlite3ExprAffinity(pX->pLeft)!=SQLITE_AFF_TEXT
163200 assert( (u32)n==pLoop->u.btree.nEq );
163206 pIdx->aiColumn[n] = i;
163207 pIdx->azColl[n] = sqlite3StrBINARY;
163211 if( pSrc->colUsed & MASKBIT(BMS-1) ){
163212 for(i=BMS-1; i<pTable->nCol; i++){
163213 pIdx->aiColumn[n] = i;
163214 pIdx->azColl[n] = sqlite3StrBINARY;
163219 pIdx->aiColumn[n] = XN_ROWID;
163220 pIdx->azColl[n] = sqlite3StrBINARY;
163224 assert( pLevel->iIdxCur>=0 );
163225 pLevel->iIdxCur = pParse->nTab++;
163226 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
163228 VdbeComment((v, "for %s", pTable->zName));
163229 if( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) && useBloomFilter ){
163230 sqlite3WhereExplainBloomFilter(pParse, pWC->pWInfo, pLevel);
163231 pLevel->regFilter = ++pParse->nMem;
163232 sqlite3VdbeAddOp2(v, OP_Blob, 10000, pLevel->regFilter);
163236 assert( pSrc == &pWC->pWInfo->pTabList->a[pLevel->iFrom] );
163237 if( pSrc->fg.viaCoroutine ){
163238 int regYield = pSrc->regReturn;
163240 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pSrc->addrFillSub);
163243 VdbeComment((v, "next row of %s", pSrc->pTab->zName));
163245 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
163250 pLoop->wsFlags |= WHERE_PARTIALIDX;
163254 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
163256 if( pLevel->regFilter ){
163257 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0,
163258 regBase, pLoop->u.btree.nEq);
163261 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
163264 if( pSrc->fg.viaCoroutine ){
163266 testcase( pParse->db->mallocFailed );
163267 assert( pLevel->iIdxCur>0 );
163268 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
163269 pSrc->regResult, pLevel->iIdxCur);
163271 pSrc->fg.viaCoroutine = 0;
163273 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
163281 sqlite3VdbeScanStatusRange(v, addrExp, addrExp, -1);
163284 sqlite3ExprDelete(pParse->db, pPartial);
163301 ** and skip the subsequence B-Tree seek if the Bloom filter indicates that
163310 int iLevel, /* Index in pWInfo->a[] that is pLevel */
163319 Parse *pParse = pWInfo->pParse; /* Parsing context */
163320 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
163321 WhereLoop *pLoop = pLevel->pWLoop; /* The loop being coded */
163326 saved_pIdxEpr = pParse->pIdxEpr;
163327 saved_pIdxPartExpr = pParse->pIdxPartExpr;
163328 pParse->pIdxEpr = 0;
163329 pParse->pIdxPartExpr = 0;
163333 assert( pLoop->wsFlags & WHERE_BLOOMFILTER );
163334 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 );
163345 iCur = pLevel->iTabCur;
163346 pLevel->regFilter = ++pParse->nMem;
163349 ** to zero-filled blob of at least 80K bits, but maybe more if the
163351 ** measure the size of the table at run-time using OP_Count with
163356 pTabList = pWInfo->pTabList;
163357 iSrc = pLevel->iFrom;
163358 pItem = &pTabList->a[iSrc];
163360 pTab = pItem->pTab;
163362 sz = sqlite3LogEstToInt(pTab->nRowLogEst);
163368 sqlite3VdbeAddOp2(v, OP_Blob, (int)sz, pLevel->regFilter);
163371 pWCEnd = &pWInfo->sWC.a[pWInfo->sWC.nTerm];
163372 for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){
163373 Expr *pExpr = pTerm->pExpr;
163374 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
163377 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
163380 if( pLoop->wsFlags & WHERE_IPK ){
163383 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, 1);
163386 Index *pIdx = pLoop->u.btree.pIndex;
163387 int n = pLoop->u.btree.nEq;
163391 assert( pIdx->pTable==pItem->pTab );
163394 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, n);
163398 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
163401 pLoop->wsFlags &= ~WHERE_BLOOMFILTER;
163402 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break;
163403 while( ++iLevel < pWInfo->nLevel ){
163405 pLevel = &pWInfo->a[iLevel];
163406 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
163407 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ) ) continue;
163408 pLoop = pLevel->pWLoop;
163410 if( pLoop->prereq & notReady ) continue;
163411 if( (pLoop->wsFlags & (WHERE_BLOOMFILTER|WHERE_COLUMN_IN))
163414 /* This is a candidate for bloom-filter pull-down (early evaluation).
163421 }while( iLevel < pWInfo->nLevel );
163423 pParse->pIdxEpr = saved_pIdxEpr;
163424 pParse->pIdxPartExpr = saved_pIdxPartExpr;
163443 Parse *pParse = pWInfo->pParse;
163454 ExprList *pOrderBy = pWInfo->pOrderBy;
163457 pTab = pSrc->pTab;
163465 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
163466 pTerm->wtFlags &= ~TERM_OK;
163467 if( pTerm->leftCursor != pSrc->iCursor ) continue;
163468 if( pTerm->prereqRight & mUnusable ) continue;
163469 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
163470 testcase( pTerm->eOperator & WO_IN );
163471 testcase( pTerm->eOperator & WO_ISNULL );
163472 testcase( pTerm->eOperator & WO_IS );
163473 testcase( pTerm->eOperator & WO_ALL );
163474 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
163475 if( pTerm->wtFlags & TERM_VNULL ) continue;
163477 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
163478 assert( pTerm->u.x.leftColumn>=XN_ROWID );
163479 assert( pTerm->u.x.leftColumn<pTab->nCol );
163480 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
163486 pTerm->wtFlags |= TERM_OK;
163495 int n = pOrderBy->nExpr;
163497 Expr *pExpr = pOrderBy->a[i].pExpr;
163506 if( pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) break;
163508 /* First case - a direct column references without a COLLATE operator */
163509 if( pExpr->op==TK_COLUMN && pExpr->iTable==pSrc->iCursor ){
163510 assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol );
163514 /* 2nd case - a column reference with a COLLATE operator. Only match
163516 if( pExpr->op==TK_COLLATE
163517 && (pE2 = pExpr->pLeft)->op==TK_COLUMN
163518 && pE2->iTable==pSrc->iCursor
163522 assert( pExpr->u.zToken!=0 );
163523 assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol );
163524 pExpr->iColumn = pE2->iColumn;
163525 if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */
163526 zColl = sqlite3ColumnColl(&pTab->aCol[pE2->iColumn]);
163528 if( sqlite3_stricmp(pExpr->u.zToken, zColl)==0 ) continue;
163536 if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY) && !pSrc->fg.rowidUsed ){
163537 eDistinct = 2 + ((pWInfo->wctrlFlags & WHERE_SORTBYGROUP)!=0);
163538 }else if( pWInfo->wctrlFlags & WHERE_GROUPBY ){
163546 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
163555 pIdxCons = (struct sqlite3_index_constraint*)&pHidden->aRhs[nTerm];
163558 pIdxInfo->aConstraint = pIdxCons;
163559 pIdxInfo->aOrderBy = pIdxOrderBy;
163560 pIdxInfo->aConstraintUsage = pUsage;
163561 pHidden->pWC = pWC;
163562 pHidden->pParse = pParse;
163563 pHidden->eDistinct = eDistinct;
163564 pHidden->mIn = 0;
163565 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
163567 if( (pTerm->wtFlags & TERM_OK)==0 ) continue;
163568 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
163570 op = pTerm->eOperator & WO_ALL;
163572 if( (pTerm->wtFlags & TERM_SLICE)==0 ){
163573 pHidden->mIn |= SMASKBIT32(j);
163578 pIdxCons[j].op = pTerm->eMatchOp;
163595 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
163598 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
163610 pIdxInfo->nConstraint = j;
163612 Expr *pExpr = pOrderBy->a[i].pExpr;
163614 assert( pExpr->op==TK_COLUMN
163615 || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN
163616 && pExpr->iColumn==pExpr->pLeft->iColumn) );
163617 pIdxOrderBy[j].iColumn = pExpr->iColumn;
163618 pIdxOrderBy[j].desc = pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC;
163621 pIdxInfo->nOrderBy = j;
163636 assert( pHidden->pParse!=0 );
163637 assert( pHidden->pParse->db==db );
163638 for(i=0; i<pIdxInfo->nConstraint; i++){
163639 sqlite3ValueFree(pHidden->aRhs[i]); /* IMP: R-14553-25174 */
163640 pHidden->aRhs[i] = 0;
163658 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
163662 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
163666 pParse->db->nSchemaLock++;
163667 rc = pVtab->pModule->xBestIndex(pVtab, p);
163668 pParse->db->nSchemaLock--;
163673 sqlite3OomFault(pParse->db);
163674 }else if( !pVtab->zErrMsg ){
163677 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
163680 if( pTab->u.vtab.p->bAllSchemas ){
163683 sqlite3_free(pVtab->zErrMsg);
163684 pVtab->zErrMsg = 0;
163699 ** into the aSample[] array - it is an index into a virtual set of samples
163710 IndexSample *aSample = pIdx->aSample;
163724 assert( pIdx->nSample>0 );
163725 assert( pRec->nField>0 );
163772 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
163773 nField = pIdx->nKeyCol;
163775 nField = pIdx->nColumn;
163777 nField = MIN(pRec->nField, nField);
163779 iSample = pIdx->nSample * nField;
163791 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
163797 pRec->nField = n;
163800 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
163803 iLower = aSample[iSamp].anLt[n-1];
163805 res = -1;
163808 iCol = n-1;
163817 if( pParse->db->mallocFailed==0 ){
163820 assert( i<pIdx->nSample );
163821 assert( iCol==nField-1 );
163822 pRec->nField = nField;
163824 || pParse->db->mallocFailed
163827 /* Unless i==pIdx->nSample, indicating that pRec is larger than
163830 assert( i<=pIdx->nSample && i>=0 );
163831 pRec->nField = iCol+1;
163832 assert( i==pIdx->nSample
163834 || pParse->db->mallocFailed );
163839 ** If (i>0), then pRec must also be greater than sample (i-1). */
163841 pRec->nField = iCol;
163843 || pParse->db->mallocFailed || CORRUPT_DB );
163846 pRec->nField = nField;
163847 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
163848 || pParse->db->mallocFailed || CORRUPT_DB );
163856 assert( iCol==nField-1 );
163861 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
163864 if( i>=pIdx->nSample ){
163865 iUpper = pIdx->nRowEst0;
163873 iGap = iUpper - iLower;
163881 aStat[1] = pIdx->aAvgEq[nField-1];
163884 /* Restore the pRec->nField value before returning. */
163885 pRec->nField = nField;
163904 if( pTerm->truthProb<=0 ){
163905 nRet += pTerm->truthProb;
163906 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
163907 nRet -= 20; assert( 20==sqlite3LogEst(4) );
163919 assert( iCol>=0 && iCol<pIdx->nColumn );
163920 if( !pIdx->zColAff ){
163923 assert( pIdx->zColAff[iCol]!=0 );
163924 return pIdx->zColAff[iCol];
163932 ** range-scan on a skip-scan index. For example:
163937 ** Value pLoop->nOut is currently set to the estimated number of rows
163948 ** N is the total number of samples, the pLoop->nOut value is adjusted
163951 ** nOut = nOut * ( min(U - L, 1) / N )
163972 Index *p = pLoop->u.btree.pIndex;
163973 int nEq = pLoop->u.btree.nEq;
163974 sqlite3 *db = pParse->db;
163975 int nLower = -1;
163976 int nUpper = p->nSample+1;
163985 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
163987 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
163991 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
163992 nUpper = p2 ? 0 : p->nSample;
163998 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
163999 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
164009 nDiff = (nUpper - nLower);
164018 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
164019 pLoop->nOut -= nAdjust;
164021 WHERETRACE(0x20, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
164022 nLower, nUpper, nAdjust*-1, pLoop->nOut));
164052 ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
164060 ** left-most column of the index). Or, if the query is:
164085 int nOut = pLoop->nOut;
164089 Index *p = pLoop->u.btree.pIndex;
164090 int nEq = pLoop->u.btree.nEq;
164092 if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)
164093 && OptimizationEnabled(pParse->db, SQLITE_Stat4)
164095 if( nEq==pBuilder->nRecValid ){
164096 UnpackedRecord *pRec = pBuilder->pRec;
164098 int nBtm = pLoop->u.btree.nBtm;
164099 int nTop = pLoop->u.btree.nTop;
164104 ** key-prefix formed by the nEq values matched against the nEq left-most
164118 ** The number of rows between the two bounds is then just iUpper-iLower.
164122 int iLwrIdx = -2; /* aSample[] for the lower bound */
164123 int iUprIdx = -1; /* aSample[] for the upper bound */
164126 testcase( pRec->nField!=pBuilder->nRecValid );
164127 pRec->nField = pBuilder->nRecValid;
164132 iUpper = p->nRowEst0;
164134 /* Note: this call could be optimized away - since the same values must
164141 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
164142 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
164143 assert( p->aSortOrder!=0 );
164144 if( p->aSortOrder[nEq] ){
164153 Expr *pExpr = pLower->pExpr->pRight;
164160 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
164162 nOut--;
164170 Expr *pExpr = pUpper->pExpr->pRight;
164177 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
164179 nOut--;
164184 pBuilder->pRec = pRec;
164187 nNew = sqlite3LogEst(iUpper - iLower);
164192 if( iLwrIdx==iUprIdx ){ nNew -= 20; }
164214 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 || pParse->nErr>0 );
164219 ** has an application-defined likelihood(), assume the range is
164220 ** reduced by an additional 75%. This means that, by default, an open-ended
164224 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
164225 nNew -= 20;
164228 nOut -= (pLower!=0) + (pUpper!=0);
164232 if( pLoop->nOut>nOut ){
164234 pLoop->nOut, nOut));
164237 pLoop->nOut = (LogEst)nOut;
164245 ** the histogram data. This only works when x is the left-most
164252 ** non-zero.
164265 Index *p = pBuilder->pNew->u.btree.pIndex;
164266 int nEq = pBuilder->pNew->u.btree.nEq;
164267 UnpackedRecord *pRec = pBuilder->pRec;
164273 assert( nEq<=p->nColumn );
164274 assert( p->aSample!=0 );
164275 assert( p->nSample>0 );
164276 assert( pBuilder->nRecValid<nEq );
164280 if( pBuilder->nRecValid<(nEq-1) ){
164286 if( nEq>=p->nColumn ){
164291 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
164292 pBuilder->pRec = pRec;
164295 pBuilder->nRecValid = nEq;
164299 p->zName, nEq-1, (int)a[1]));
164309 ** an IN constraint where the right-hand side of the IN operator
164316 ** non-zero.
164329 Index *p = pBuilder->pNew->u.btree.pIndex;
164330 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
164331 int nRecValid = pBuilder->nRecValid;
164337 assert( p->aSample!=0 );
164338 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
164340 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
164342 pBuilder->nRecValid = nRecValid;
164350 assert( pBuilder->nRecValid==nRecValid );
164362 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
164367 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
164368 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
164369 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) zType[2] = 'L';
164370 if( pTerm->wtFlags & TERM_CODED ) zType[3] = 'C';
164371 if( pTerm->eOperator & WO_SINGLE ){
164372 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
164374 pTerm->leftCursor, pTerm->u.x.leftColumn);
164375 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
164377 pTerm->u.pOrInfo->indexable);
164379 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
164382 "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
164383 iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
164387 sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
164388 pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
164390 if( (pTerm->eOperator & (WO_OR|WO_AND))==0 && pTerm->u.x.iField ){
164391 sqlite3DebugPrintf(" iField=%d", pTerm->u.x.iField);
164393 if( pTerm->iParent>=0 ){
164394 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
164397 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
164408 for(i=0; i<pWC->nTerm; i++){
164409 sqlite3WhereTermPrint(&pWC->a[i], i);
164420 ** .--- Position in WHERE clause rSetup, rRun, nOut ---.
164422 ** | .--- selfMask nTerm ------. |
164424 ** | | .-- prereq Idx wsFlags----. | |
164426 ** | | | __|__ nEq ---. ___|__ | __|__
164432 WhereInfo *pWInfo = pWC->pWInfo;
164433 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
164434 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
164435 Table *pTab = pItem->pTab;
164436 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
164437 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
164438 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
164440 pItem->zAlias ? pItem->zAlias : pTab->zName);
164443 p->cId, p->iTab, p->maskSelf, p->prereq & 0xfff, p->cId, p->iTab);
164445 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
164447 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
164449 int i = sqlite3Strlen30(zName) - 1;
164450 while( zName[i]!='_' ) i--;
164453 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
164459 if( p->u.vtab.idxStr ){
164461 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
164463 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
164465 sqlite3DebugPrintf(" %-19s", z);
164468 if( p->wsFlags & WHERE_SKIPSCAN ){
164469 sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
164471 sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm);
164473 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
164474 if( p->nLTerm && (sqlite3WhereTrace & 0x4000)!=0 ){
164476 for(i=0; i<p->nLTerm; i++){
164477 sqlite3WhereTermPrint(p->aLTerm[i], i);
164487 p = p->pNextLoop;
164497 p->aLTerm = p->aLTermSpace;
164498 p->nLTerm = 0;
164499 p->nLSlot = ArraySize(p->aLTermSpace);
164500 p->wsFlags = 0;
164507 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
164508 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
164509 sqlite3_free(p->u.vtab.idxStr);
164510 p->u.vtab.needFree = 0;
164511 p->u.vtab.idxStr = 0;
164512 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
164513 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
164514 sqlite3DbFreeNN(db, p->u.btree.pIndex);
164515 p->u.btree.pIndex = 0;
164525 if( p->aLTerm!=p->aLTermSpace ){
164526 sqlite3DbFreeNN(db, p->aLTerm);
164527 p->aLTerm = p->aLTermSpace;
164528 p->nLSlot = ArraySize(p->aLTermSpace);
164531 p->nLTerm = 0;
164532 p->wsFlags = 0;
164536 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
164540 if( p->nLSlot>=n ) return SQLITE_OK;
164542 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
164544 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
164545 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
164546 p->aLTerm = paNew;
164547 p->nLSlot = n;
164556 if( pFrom->nLTerm > pTo->nLSlot
164557 && whereLoopResize(db, pTo, pFrom->nLTerm)
164563 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
164564 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
164565 pFrom->u.vtab.needFree = 0;
164566 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
164567 pFrom->u.btree.pIndex = 0;
164587 sqlite3WhereClauseClear(&pWInfo->sWC);
164588 while( pWInfo->pLoops ){
164589 WhereLoop *p = pWInfo->pLoops;
164590 pWInfo->pLoops = p->pNextLoop;
164593 while( pWInfo->pMemToFree ){
164594 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
164595 sqlite3DbNNFreeNN(db, pWInfo->pMemToFree);
164596 pWInfo->pMemToFree = pNext;
164614 ** (1c) Neither X nor Y use skip-scan
164631 if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0; /* (1d) and (2a) */
164632 assert( (pX->wsFlags & WHERE_VIRTUALTABLE)==0 );
164633 assert( (pY->wsFlags & WHERE_VIRTUALTABLE)==0 );
164634 if( pX->u.btree.nEq < pY->u.btree.nEq /* (1b) */
164635 && pX->u.btree.pIndex==pY->u.btree.pIndex /* (1a) */
164636 && pX->nSkip==0 && pY->nSkip==0 /* (1c) */
164640 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
164643 if( pY->nSkip > pX->nSkip ) return 0; /* (2d) */
164644 for(i=pX->nLTerm-1; i>=0; i--){
164645 if( pX->aLTerm[i]==0 ) continue;
164646 for(j=pY->nLTerm-1; j>=0; j--){
164647 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
164651 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
164652 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
164673 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
164674 for(; p; p=p->pNextLoop){
164675 if( p->iTab!=pTemplate->iTab ) continue;
164676 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
164681 pTemplate->rRun, pTemplate->nOut,
164682 MIN(p->rRun, pTemplate->rRun),
164683 MIN(p->nOut - 1, pTemplate->nOut)));
164684 pTemplate->rRun = MIN(p->rRun, pTemplate->rRun);
164685 pTemplate->nOut = MIN(p->nOut - 1, pTemplate->nOut);
164690 pTemplate->rRun, pTemplate->nOut,
164691 MAX(p->rRun, pTemplate->rRun),
164692 MAX(p->nOut + 1, pTemplate->nOut)));
164693 pTemplate->rRun = MAX(p->rRun, pTemplate->rRun);
164694 pTemplate->nOut = MAX(p->nOut + 1, pTemplate->nOut);
164718 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
164719 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
164728 assert( p->rSetup==0 || pTemplate->rSetup==0
164729 || p->rSetup==pTemplate->rSetup );
164733 ** rSetup. Call this SETUP-INVARIANT */
164734 assert( p->rSetup>=pTemplate->rSetup );
164736 /* Any loop using an application-defined index (or PRIMARY KEY or
164738 ** than an automatic index. Unless it is a skip-scan. */
164739 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
164740 && (pTemplate->nSkip)==0
164741 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
164742 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
164743 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
164753 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
164754 && p->rSetup<=pTemplate->rSetup /* (2a) */
164755 && p->rRun<=pTemplate->rRun /* (2b) */
164756 && p->nOut<=pTemplate->nOut /* (2c) */
164766 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
164767 && p->rRun>=pTemplate->rRun /* (2a) */
164768 && p->nOut>=pTemplate->nOut /* (2b) */
164770 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
164786 ** If pBuilder->pOrSet is not NULL then we care about only the
164788 ** information is gathered in the pBuilder->pOrSet object. This special
164791 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
164803 WhereInfo *pWInfo = pBuilder->pWInfo;
164804 sqlite3 *db = pWInfo->pParse->db;
164808 if( pBuilder->iPlanLimit==0 ){
164810 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
164813 pBuilder->iPlanLimit--;
164815 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
164817 /* If pBuilder->pOrSet is defined, then only keep track of the costs
164820 if( pBuilder->pOrSet!=0 ){
164821 if( pTemplate->nLTerm ){
164823 u16 n = pBuilder->pOrSet->n;
164826 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
164827 pTemplate->nOut);
164830 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
164831 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
164840 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
164848 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
164864 sqlite3WhereLoopPrint(p, pBuilder->pWC);
164869 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
164877 p->pNextLoop = 0;
164882 WhereLoop **ppTail = &p->pNextLoop;
164889 *ppTail = pToDel->pNextLoop;
164893 sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
164900 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
164901 Index *pIndex = p->u.btree.pIndex;
164902 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK ){
164903 p->u.btree.pIndex = 0;
164922 ** TODO --> Perhaps this is something that could be improved by better
164926 ** value corresponds to -1 in LogEst notation, so this means decrement
164933 ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
164934 ** "x" column is boolean or else -1 or 0 or 1 is a common default value
164944 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
164946 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
164948 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
164949 for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
164951 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
164952 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
164953 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) continue;
164954 for(j=pLoop->nLTerm-1; j>=0; j--){
164955 pX = pLoop->aLTerm[j];
164958 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
164961 sqlite3ProgressCheck(pWC->pWInfo->pParse);
164962 if( pLoop->maskSelf==pTerm->prereqAll ){
164966 ** "self-culling".
164968 ** 2022-03-24: Self-culling only applies if either the extra terms
164969 ** are straight comparison operators that are non-true with NULL
164972 if( (pTerm->eOperator & 0x3f)!=0
164973 || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype
164976 pLoop->wsFlags |= WHERE_SELFCULL;
164979 if( pTerm->truthProb<=0 ){
164982 pLoop->nOut += pTerm->truthProb;
164986 pLoop->nOut--;
164987 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
164988 && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */
164990 Expr *pRight = pTerm->pExpr->pRight;
164992 testcase( pTerm->pExpr->op==TK_IS );
164993 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
164999 pTerm->wtFlags |= TERM_HEURTRUTH;
165006 if( pLoop->nOut > nRow-iReduce ){
165007 pLoop->nOut = nRow - iReduce;
165035 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
165038 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
165047 assert( ExprUseXList(pTerm->pExpr->pLeft) );
165048 pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
165049 pRhs = pTerm->pExpr->pRight;
165051 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
165053 pRhs = pRhs->x.pList->a[i].pExpr;
165060 if( pLhs->op!=TK_COLUMN
165061 || pLhs->iTable!=iCur
165062 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
165063 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
165068 testcase( pLhs->iColumn==XN_ROWID );
165070 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
165075 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
165082 ** compiled with -DSQLITE_ENABLE_COSTMULT
165091 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
165094 ** When this function is called, pBuilder->pNew->nOut contains the
165099 ** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
165108 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyze context */
165109 Parse *pParse = pWInfo->pParse; /* Parsing context */
165110 sqlite3 *db = pParse->db; /* Database connection malloc context */
165115 Bitmask saved_prereq; /* Original value of pNew->prereq */
165116 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
165117 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
165118 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
165119 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
165120 u16 saved_nSkip; /* Original value of pNew->nSkip */
165121 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
165122 LogEst saved_nOut; /* Original value of pNew->nOut */
165128 pNew = pBuilder->pNew;
165129 assert( db->mallocFailed==0 || pParse->nErr>0 );
165130 if( pParse->nErr ){
165131 return pParse->rc;
165134 pProbe->pTable->zName,pProbe->zName,
165135 pNew->u.btree.nEq, pNew->nSkip, pNew->rRun));
165137 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
165138 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
165139 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
165142 assert( pNew->u.btree.nBtm==0 );
165145 if( pProbe->bUnordered || pProbe->bLowQual ){
165146 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
165147 if( pProbe->bLowQual && pSrc->fg.isIndexedBy==0 ){
165152 assert( pNew->u.btree.nEq<pProbe->nColumn );
165153 assert( pNew->u.btree.nEq<pProbe->nKeyCol
165154 || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY );
165156 saved_nEq = pNew->u.btree.nEq;
165157 saved_nBtm = pNew->u.btree.nBtm;
165158 saved_nTop = pNew->u.btree.nTop;
165159 saved_nSkip = pNew->nSkip;
165160 saved_nLTerm = pNew->nLTerm;
165161 saved_wsFlags = pNew->wsFlags;
165162 saved_prereq = pNew->prereq;
165163 saved_nOut = pNew->nOut;
165164 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
165166 pNew->rSetup = 0;
165167 rSize = pProbe->aiRowLogEst[0];
165170 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
165175 int nRecValid = pBuilder->nRecValid;
165177 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
165182 if( pTerm->prereqRight & pNew->maskSelf ) continue;
165186 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
165188 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
165193 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
165194 pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE;
165196 pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED;
165198 pNew->wsFlags = saved_wsFlags;
165199 pNew->u.btree.nEq = saved_nEq;
165200 pNew->u.btree.nBtm = saved_nBtm;
165201 pNew->u.btree.nTop = saved_nTop;
165202 pNew->nLTerm = saved_nLTerm;
165203 if( pNew->nLTerm>=pNew->nLSlot
165204 && whereLoopResize(db, pNew, pNew->nLTerm+1)
165206 break; /* OOM while trying to enlarge the pNew->aLTerm array */
165208 pNew->aLTerm[pNew->nLTerm++] = pTerm;
165209 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
165212 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
165213 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
165214 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
165218 Expr *pExpr = pTerm->pExpr;
165229 for(i=0; i<pNew->nLTerm-1; i++){
165230 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
165232 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
165234 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
165236 if( pProbe->hasStat1 && rLogSize>=10 ){
165243 ** the left-most index column, M==N.
165253 ** with the index, as using an index has better worst-case behavior.
165258 M = pProbe->aiRowLogEst[saved_nEq];
165260 /* TUNING v----- 10 to bias toward indexed IN */
165261 x = M + logK + 10 - (nIn + rLogSize);
165270 " nInMul=%d) prefers skip-scan\n",
165272 pNew->wsFlags |= WHERE_IN_SEEKSCAN;
165281 pNew->wsFlags |= WHERE_COLUMN_IN;
165283 int iCol = pProbe->aiColumn[saved_nEq];
165284 pNew->wsFlags |= WHERE_COLUMN_EQ;
165285 assert( saved_nEq==pNew->u.btree.nEq );
165287 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
165289 if( iCol==XN_ROWID || pProbe->uniqNotNull
165290 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
165292 pNew->wsFlags |= WHERE_ONEROW;
165294 pNew->wsFlags |= WHERE_UNQ_WANTED;
165297 if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS;
165299 pNew->wsFlags |= WHERE_COLUMN_NULL;
165302 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
165307 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
165308 pNew->u.btree.nBtm = nVecLen;
165311 if( pTerm->wtFlags & TERM_LIKEOPT ){
165315 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
165316 assert( pTop->wtFlags & TERM_LIKEOPT );
165317 assert( pTop->eOperator==WO_LT );
165318 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
165319 pNew->aLTerm[pNew->nLTerm++] = pTop;
165320 pNew->wsFlags |= WHERE_TOP_LIMIT;
165321 pNew->u.btree.nTop = 1;
165327 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
165328 pNew->u.btree.nTop = nVecLen;
165330 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
165331 pNew->aLTerm[pNew->nLTerm-2] : 0;
165335 /* At this point pNew->nOut is set to the number of rows expected to
165339 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
165340 assert( pNew->nOut==saved_nOut );
165341 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
165346 int nEq = ++pNew->u.btree.nEq;
165349 assert( pNew->nOut==saved_nOut );
165350 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
165353 pNew->nOut += pTerm->truthProb;
165354 pNew->nOut -= nIn;
165359 && pProbe->nSample
165360 && ALWAYS(pNew->u.btree.nEq<=pProbe->nSampleCol)
165361 && ((eOp & WO_IN)==0 || ExprUseXList(pTerm->pExpr))
165364 Expr *pExpr = pTerm->pExpr;
165369 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
165371 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
165376 pNew->nOut = sqlite3LogEst(nOut);
165380 ** See tag-202002240-1 */
165381 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
165390 pTerm->wtFlags |= TERM_HIGHTRUTH;
165391 if( pTerm->wtFlags & TERM_HEURTRUTH ){
165395 pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS;
165398 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
165399 pNew->nOut -= nIn;
165405 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
165410 pNew->nOut += 10;
165418 ** 1. The cost of doing one search-by-key to find the first matching
165420 ** 2. Stepping forward in the index pNew->nOut times to find all
165423 assert( pSrc->pTab->szTabRow>0 );
165424 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
165425 /* The pProbe->szIdxRow is low for an IPK table since the interior
165427 ** But the leaf pages are full-size, so pProbe->szIdxRow would badly
165428 ** under-estimate the scanning cost. */
165429 rCostIdx = pNew->nOut + 16;
165431 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
165438 ** we also have to add in the cost of doing pNew->nOut searches to
165441 pNew->rRun = rCostIdx;
165442 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK|WHERE_EXPRIDX))==0 ){
165443 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
165445 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
165447 nOutUnadjusted = pNew->nOut;
165448 pNew->rRun += nInMul + nIn;
165449 pNew->nOut += nInMul + nIn;
165450 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
165453 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
165454 pNew->nOut = saved_nOut;
165456 pNew->nOut = nOutUnadjusted;
165459 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
165460 && pNew->u.btree.nEq<pProbe->nColumn
165461 && (pNew->u.btree.nEq<pProbe->nKeyCol ||
165462 pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY)
165464 if( pNew->u.btree.nEq>3 ){
165469 pNew->nOut = saved_nOut;
165471 pBuilder->nRecValid = nRecValid;
165474 pNew->prereq = saved_prereq;
165475 pNew->u.btree.nEq = saved_nEq;
165476 pNew->u.btree.nBtm = saved_nBtm;
165477 pNew->u.btree.nTop = saved_nTop;
165478 pNew->nSkip = saved_nSkip;
165479 pNew->wsFlags = saved_wsFlags;
165480 pNew->nOut = saved_nOut;
165481 pNew->nLTerm = saved_nLTerm;
165483 /* Consider using a skip-scan if there are no WHERE clause constraints
165484 ** available for the left-most terms of the index, and if the average
165485 ** number of repeats in the left-most terms is at least 18.
165495 && saved_nEq+1<pProbe->nKeyCol
165496 && saved_nEq==pNew->nLTerm
165497 && pProbe->noSkipScan==0
165498 && pProbe->hasStat1!=0
165500 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
165501 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
165504 pNew->u.btree.nEq++;
165505 pNew->nSkip++;
165506 pNew->aLTerm[pNew->nLTerm++] = 0;
165507 pNew->wsFlags |= WHERE_SKIPSCAN;
165508 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
165509 pNew->nOut -= nIter;
165510 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
165511 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
165514 pNew->nOut = saved_nOut;
165515 pNew->u.btree.nEq = saved_nEq;
165516 pNew->nSkip = saved_nSkip;
165517 pNew->wsFlags = saved_wsFlags;
165521 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
165542 if( pIndex->bUnordered ) return 0;
165543 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
165544 for(ii=0; ii<pOB->nExpr; ii++){
165545 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr);
165547 if( (pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN)
165548 && pExpr->iTable==iCursor
165550 if( pExpr->iColumn<0 ) return 1;
165551 for(jj=0; jj<pIndex->nKeyCol; jj++){
165552 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
165554 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
165555 for(jj=0; jj<pIndex->nKeyCol; jj++){
165556 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
165557 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
165580 pParse = pWC->pWInfo->pParse;
165581 while( pWhere->op==TK_AND ){
165582 if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere->pLeft) ) return 0;
165583 pWhere = pWhere->pRight;
165585 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
165586 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
165588 pExpr = pTerm->pExpr;
165589 if( (!ExprHasProperty(pExpr, EP_OuterON) || pExpr->w.iJoin==iTab)
165592 && (pTerm->wtFlags & TERM_VNULL)==0
165610 for(i=0; i<pIdx->nColumn; i++){
165611 if( pIdx->aiColumn[i]==XN_EXPR
165612 && sqlite3ExprCompare(0, pExpr, pIdx->aColExpr->a[i].pExpr, iTabCur)==0
165632 ** Information passed in is pWalk->u.pCovIdxCk. Call it pCk.
165634 ** If the Expr node references the table with cursor pCk->iTabCur, then
165635 ** make sure that column is covered by the index pCk->pIdx. We know that
165636 ** all columns less than 63 (really BMS-1) are covered, so we don't need
165639 ** If the index does not cover the column, then set pWalk->eCode to
165640 ** non-zero and return WRC_Abort to stop the search.
165645 ** If pCk->pIdx contains indexed expressions and one of those expressions
165655 pCk = pWalk->u.pCovIdxCk;
165656 pIdx = pCk->pIdx;
165657 if( (pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN) ){
165658 /* if( pExpr->iColumn<(BMS-1) && pIdx->bHasExpr==0 ) return WRC_Continue;*/
165659 if( pExpr->iTable!=pCk->iTabCur ) return WRC_Continue;
165660 pIdx = pWalk->u.pCovIdxCk->pIdx;
165661 aiColumn = pIdx->aiColumn;
165662 nColumn = pIdx->nColumn;
165664 if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue;
165666 pCk->bUnidx = 1;
165668 }else if( pIdx->bHasExpr
165669 && exprIsCoveredByIndex(pExpr, pIdx, pWalk->u.pCovIdxCk->iTabCur) ){
165670 pCk->bExpr = 1;
165678 ** pIdx is an index that covers all of the low-number columns used by
165679 ** pWInfo->pSelect (columns from 0 through 62) or an index that has
165708 if( pWInfo->pSelect==0 ){
165713 if( pIdx->bHasExpr==0 ){
165714 for(i=0; i<pIdx->nColumn; i++){
165715 if( pIdx->aiColumn[i]>=BMS-1 ) break;
165717 if( i>=pIdx->nColumn ){
165732 sqlite3WalkSelect(&w, pWInfo->pSelect);
165745 ** free the Parse->pIdxEpr list when the Parse object is destroyed.
165751 *pp = p->pIENext;
165752 sqlite3ExprDelete(db, p->pExpr);
165758 ** This function is called for a partial index - one with a WHERE clause - in
165767 ** The "a" in the select-list may be replaced by <expr>, iff:
165792 assert( pItem==0 || (pItem->fg.jointype & JT_RIGHT)==0 );
165795 if( pPart->op==TK_AND ){
165796 wherePartIdxExpr(pParse, pIdx, pPart->pRight, pMask, iIdxCur, pItem);
165797 pPart = pPart->pLeft;
165800 if( (pPart->op==TK_EQ || pPart->op==TK_IS) ){
165801 Expr *pLeft = pPart->pLeft;
165802 Expr *pRight = pPart->pRight;
165805 if( pLeft->op!=TK_COLUMN ) return;
165808 if( pLeft->iColumn<0 ) return;
165809 aff = pIdx->pTable->aCol[pLeft->iColumn].affinity;
165812 sqlite3 *db = pParse->db;
165815 int bNullRow = (pItem->fg.jointype&(JT_LEFT|JT_LTORJ))!=0;
165816 p->pExpr = sqlite3ExprDup(db, pRight, 0);
165817 p->iDataCur = pItem->iCursor;
165818 p->iIdxCur = iIdxCur;
165819 p->iIdxCol = pLeft->iColumn;
165820 p->bMaybeNullRow = bNullRow;
165821 p->pIENext = pParse->pIdxPartExpr;
165822 p->aff = aff;
165823 pParse->pIdxPartExpr = p;
165824 if( p->pIENext==0 ){
165825 void *pArg = (void*)&pParse->pIdxPartExpr;
165829 }else if( pLeft->iColumn<(BMS-1) ){
165830 *pMask &= ~((Bitmask)1 << pLeft->iColumn);
165839 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
165840 ** a b-tree table, not a virtual table.
165842 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function
165847 ** cost = nRow * 3.0 // full-table scan
165849 ** cost = nRow * (K+3.0) // scan of non-covering index
165856 ** the index b-tree:
165859 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
165863 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
165868 ** log(nRow) factor is omitted from a non-covering index scan in order to
165869 ** bias the scoring in favor of using an index, since the worst-case
165870 ** performance of using an index is far better than the worst-case performance
165881 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
165892 pNew = pBuilder->pNew;
165893 pWInfo = pBuilder->pWInfo;
165894 pTabList = pWInfo->pTabList;
165895 pSrc = pTabList->a + pNew->iTab;
165896 pTab = pSrc->pTab;
165897 pWC = pBuilder->pWC;
165898 assert( !IsVirtual(pSrc->pTab) );
165900 if( pSrc->fg.isIndexedBy ){
165901 assert( pSrc->fg.isCte==0 );
165903 pProbe = pSrc->u2.pIBIndex;
165905 pProbe = pTab->pIndex;
165921 aiRowEstPk[0] = pTab->nRowLogEst;
165923 pFirst = pSrc->pTab->pIndex;
165924 if( pSrc->fg.notIndexed==0 ){
165931 rSize = pTab->nRowLogEst;
165935 if( !pBuilder->pOrSet /* Not part of an OR optimization */
165936 && (pWInfo->wctrlFlags & (WHERE_RIGHT_JOIN|WHERE_OR_SUBCLAUSE))==0
165937 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
165938 && !pSrc->fg.isIndexedBy /* Has no INDEXED BY clause */
165939 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
165941 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
165942 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
165943 && (pSrc->fg.jointype & JT_RIGHT)==0 /* Not the right tab of a RIGHT JOIN */
165945 /* Generate auto-index WhereLoops */
165948 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
165950 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
165951 if( pTerm->prereqRight & pNew->maskSelf ) continue;
165953 pNew->u.btree.nEq = 1;
165954 pNew->nSkip = 0;
165955 pNew->u.btree.pIndex = 0;
165956 pNew->nLTerm = 1;
165957 pNew->aLTerm[0] = pTerm;
165958 /* TUNING: One-time cost for computing the automatic index is
165961 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
165966 pNew->rSetup = rLogSize + rSize;
165967 if( !IsView(pTab) && (pTab->tabFlags & TF_Ephemeral)==0 ){
165968 pNew->rSetup += 28;
165970 pNew->rSetup -= 25; /* Greatly reduced setup cost for auto indexes
165973 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
165974 if( pNew->rSetup<0 ) pNew->rSetup = 0;
165979 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
165980 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
165981 pNew->wsFlags = WHERE_AUTO_INDEX;
165982 pNew->prereq = mPrereq | pTerm->prereqRight;
165992 pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
165994 if( pProbe->pPartIdxWhere!=0
165995 && !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC,
165996 pProbe->pPartIdxWhere)
165998 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
166001 if( pProbe->bNoQuery ) continue;
166002 rSize = pProbe->aiRowLogEst[0];
166003 pNew->u.btree.nEq = 0;
166004 pNew->u.btree.nBtm = 0;
166005 pNew->u.btree.nTop = 0;
166006 pNew->nSkip = 0;
166007 pNew->nLTerm = 0;
166008 pNew->iSortIdx = 0;
166009 pNew->rSetup = 0;
166010 pNew->prereq = mPrereq;
166011 pNew->nOut = rSize;
166012 pNew->u.btree.pIndex = pProbe;
166013 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
166016 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
166017 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
166019 pNew->wsFlags = WHERE_IPK;
166022 pNew->iSortIdx = b ? iSortIdx : 0;
166025 ** since index lookups have better worst-case performance if our
166036 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0);
166038 pNew->rRun = rSize + 16;
166040 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
166043 pNew->nOut = rSize;
166047 if( pProbe->isCovering ){
166049 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
166051 m = pSrc->colUsed & pProbe->colNotIdxed;
166052 if( pProbe->pPartIdxWhere ){
166054 pWInfo->pParse, pProbe, pProbe->pPartIdxWhere, &m, 0, 0
166057 pNew->wsFlags = WHERE_INDEXED;
166058 if( m==TOPBIT || (pProbe->bHasExpr && !pProbe->bHasVCol && m!=0) ){
166059 u32 isCov = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
166062 ("-> %s is not a covering index"
166063 " according to whereIsCoveringIndex()\n", pProbe->zName));
166067 pNew->wsFlags |= isCov;
166070 ("-> %s is a covering expression index"
166071 " according to whereIsCoveringIndex()\n", pProbe->zName));
166075 ("-> %s might be a covering expression index"
166076 " according to whereIsCoveringIndex()\n", pProbe->zName));
166080 && (HasRowid(pTab) || pWInfo->pSelect!=0 || sqlite3FaultSim(700))
166083 ("-> %s a covering index according to bitmasks\n",
166084 pProbe->zName, m==0 ? "is" : "is not"));
166085 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
166092 || pProbe->pPartIdxWhere!=0
166093 || pSrc->fg.isIndexedBy
166095 && pProbe->bUnordered==0
166096 && (pProbe->szIdxRow<pTab->szTabRow)
166097 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
166099 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
166102 pNew->iSortIdx = b ? iSortIdx : 0;
166107 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
166109 /* If this is a non-covering index scan, add in the cost of
166116 int iCur = pSrc->iCursor;
166117 WhereClause *pWC2 = &pWInfo->sWC;
166118 for(ii=0; ii<pWC2->nTerm; ii++){
166119 WhereTerm *pTerm = &pWC2->a[ii];
166120 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
166125 if( pTerm->truthProb<=0 ){
166126 nLookup += pTerm->truthProb;
166128 nLookup--;
166129 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
166133 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
166135 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
166137 if( (pSrc->fg.jointype & JT_RIGHT)!=0 && pProbe->aColExpr ){
166138 /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
166140 ** positioned to the correct row during the right-join no-match
166145 pNew->nOut = rSize;
166150 pBuilder->bldFlags1 = 0;
166152 if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED ){
166153 /* If a non-unique index is used, or if a prefix of the key for
166154 ** unique index is used (making the index functionally non-unique)
166157 pTab->tabFlags |= TF_MaybeReanalyze;
166160 sqlite3Stat4ProbeFree(pBuilder->pRec);
166161 pBuilder->nRecValid = 0;
166162 pBuilder->pRec = 0;
166174 assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 );
166175 return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT
166176 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET;
166181 ** marked as in-use (have argvIndex>0). False otherwise.
166196 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
166224 WhereClause *pWC = pBuilder->pWC;
166227 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
166231 WhereLoop *pNew = pBuilder->pNew;
166232 Parse *pParse = pBuilder->pWInfo->pParse;
166233 SrcItem *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
166234 int nConstraint = pIdxInfo->nConstraint;
166238 pNew->prereq = mPrereq;
166242 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
166244 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
166245 pIdxCons->usable = 0;
166246 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
166247 && (pTerm->eOperator & mExclude)==0
166250 pIdxCons->usable = 1;
166256 assert( pIdxInfo->needToFreeIdxStr==0 );
166257 pIdxInfo->idxStr = 0;
166258 pIdxInfo->idxNum = 0;
166259 pIdxInfo->orderByConsumed = 0;
166260 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
166261 pIdxInfo->estimatedRows = 25;
166262 pIdxInfo->idxFlags = 0;
166263 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
166264 pHidden->mHandleIn = 0;
166267 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
166274 WHERETRACE(0xffffffff, (" ^^^^--- non-viable plan rejected!\n"));
166280 mxTerm = -1;
166281 assert( pNew->nLSlot>=nConstraint );
166282 memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint );
166283 memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab));
166284 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
166287 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
166289 int j = pIdxCons->iTermOffset;
166292 || j>=pWC->nTerm
166293 || pNew->aLTerm[iTerm]!=0
166294 || pIdxCons->usable==0
166296 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
166297 testcase( pIdxInfo->needToFreeIdxStr );
166300 testcase( iTerm==nConstraint-1 );
166302 testcase( j==pWC->nTerm-1 );
166303 pTerm = &pWC->a[j];
166304 pNew->prereq |= pTerm->prereqRight;
166305 assert( iTerm<pNew->nLSlot );
166306 pNew->aLTerm[iTerm] = pTerm;
166313 pNew->u.vtab.omitMask |= 1<<iTerm;
166317 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET ){
166318 pNew->u.vtab.bOmitOffset = 1;
166321 if( SMASKBIT32(i) & pHidden->mHandleIn ){
166322 pNew->u.vtab.mHandleIn |= MASKBIT32(iTerm);
166323 }else if( (pTerm->eOperator & WO_IN)!=0 ){
166329 pIdxInfo->orderByConsumed = 0;
166330 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
166334 /* Unless pbRetryLimit is non-NULL, there should be no LIMIT/OFFSET
166337 assert( !isLimitTerm(pTerm) || i>=nConstraint-2 );
166338 assert( !isLimitTerm(pTerm) || i==nConstraint-1 || isLimitTerm(pTerm+1) );
166348 if( pIdxInfo->needToFreeIdxStr ){
166349 sqlite3_free(pIdxInfo->idxStr);
166350 pIdxInfo->idxStr = 0;
166351 pIdxInfo->needToFreeIdxStr = 0;
166359 pNew->nLTerm = mxTerm+1;
166361 if( pNew->aLTerm[i]==0 ){
166362 /* The non-zero argvIdx values must be contiguous. Raise an
166364 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
166365 testcase( pIdxInfo->needToFreeIdxStr );
166369 assert( pNew->nLTerm<=pNew->nLSlot );
166370 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
166371 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
166372 pIdxInfo->needToFreeIdxStr = 0;
166373 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
166374 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
166375 pIdxInfo->nOrderBy : 0);
166376 pNew->rSetup = 0;
166377 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
166378 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
166382 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
166383 pNew->wsFlags |= WHERE_ONEROW;
166385 pNew->wsFlags &= ~WHERE_ONEROW;
166388 if( pNew->u.vtab.needFree ){
166389 sqlite3_free(pNew->u.vtab.idxStr);
166390 pNew->u.vtab.needFree = 0;
166394 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
166417 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
166419 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
166420 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
166421 if( pX->pLeft ){
166422 pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX);
166424 zRet = (pC ? pC->zName : sqlite3StrBINARY);
166437 if( m & pHidden->mIn ){
166439 pHidden->mHandleIn &= ~m;
166441 pHidden->mHandleIn |= m;
166452 ** on the right-hand-side of constraint iCons.
166462 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
166463 rc = SQLITE_MISUSE_BKPT; /* EV: R-30545-25046 */
166465 if( pH->aRhs[iCons]==0 ){
166466 WhereTerm *pTerm = &pH->pWC->a[pIdxInfo->aConstraint[iCons].iTermOffset];
166468 pH->pParse->db, pTerm->pExpr->pRight, ENC(pH->pParse->db),
166469 SQLITE_AFF_BLOB, &pH->aRhs[iCons]
166473 pVal = pH->aRhs[iCons];
166477 if( rc==SQLITE_OK && pVal==0 ){ /* IMP: R-19933-32160 */
166478 rc = SQLITE_NOTFOUND; /* IMP: R-36424-56542 */
166489 assert( pHidden->eDistinct>=0 && pHidden->eDistinct<=3 );
166490 return pHidden->eDistinct;
166496 ** prepared is read-only, then just start read transactions on all
166500 ** This is used by the (built-in) sqlite_dbpage virtual table.
166503 int nDb = pParse->db->nDb;
166508 if( DbMaskNonZero(pParse->writeMask) ){
166517 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
166538 ** mUnusable should always be configured as "not-usable" for xBestIndex.
166559 pWInfo = pBuilder->pWInfo;
166560 pParse = pWInfo->pParse;
166561 pWC = pBuilder->pWC;
166562 pNew = pBuilder->pNew;
166563 pSrc = &pWInfo->pTabList->a[pNew->iTab];
166564 assert( IsVirtual(pSrc->pTab) );
166567 pNew->rSetup = 0;
166568 pNew->wsFlags = WHERE_VIRTUALTABLE;
166569 pNew->nLTerm = 0;
166570 pNew->u.vtab.needFree = 0;
166571 nConstraint = p->nConstraint;
166572 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
166573 freeIndexInfo(pParse->db, p);
166578 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
166595 if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
166608 mBestNoIn = pNew->prereq & ~mPrereq;
166623 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
166634 if( pNew->prereq==mPrereq ){
166660 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
166661 freeIndexInfo(pParse->db, p);
166662 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
166676 WhereInfo *pWInfo = pBuilder->pWInfo;
166687 pWC = pBuilder->pWC;
166688 pWCEnd = pWC->a + pWC->nTerm;
166689 pNew = pBuilder->pNew;
166691 pItem = pWInfo->pTabList->a + pNew->iTab;
166692 iCur = pItem->iCursor;
166694 /* The multi-index OR optimization does not work for RIGHT and FULL JOIN */
166695 if( pItem->fg.jointype & JT_RIGHT ) return SQLITE_OK;
166697 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
166698 if( (pTerm->eOperator & WO_OR)!=0
166699 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
166701 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
166702 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
166710 WHERETRACE(0x400, ("Begin processing OR-clause %p\n", pTerm));
166711 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
166712 if( (pOrTerm->eOperator & WO_AND)!=0 ){
166713 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
166714 }else if( pOrTerm->leftCursor==iCur ){
166715 tempWC.pWInfo = pWC->pWInfo;
166727 WHERETRACE(0x400, ("OR-term %d of %p has %d subterms:\n",
166728 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
166734 if( IsVirtual(pItem->pTab) ){
166765 pNew->nLTerm = 1;
166766 pNew->aLTerm[0] = pTerm;
166767 pNew->wsFlags = WHERE_MULTI_OR;
166768 pNew->rSetup = 0;
166769 pNew->iSortIdx = 0;
166770 memset(&pNew->u, 0, sizeof(pNew->u));
166773 ** of all sub-scans required by the OR-scan. However, due to rounding
166774 ** errors, it may be that the cost of the OR-scan is equal to its
166775 ** most expensive sub-scan. Add the smallest possible penalty
166782 ** the planner may elect to "OR" together a full-table scan and an
166784 pNew->rRun = sSum.a[i].rRun + 1;
166785 pNew->nOut = sSum.a[i].nOut;
166786 pNew->prereq = sSum.a[i].prereq;
166789 WHERETRACE(0x400, ("End processing OR-clause %p\n", pTerm));
166799 WhereInfo *pWInfo = pBuilder->pWInfo;
166803 SrcList *pTabList = pWInfo->pTabList;
166805 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
166806 sqlite3 *db = pWInfo->pParse->db;
166814 pNew = pBuilder->pNew;
166817 assert( pNew->nLTerm==0 );
166818 assert( pNew->wsFlags==0 );
166819 assert( pNew->nLSlot>=ArraySize(pNew->aLTermSpace) );
166820 assert( pNew->aLTerm!=0 );
166822 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
166823 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
166825 pNew->iTab = iTab;
166826 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
166827 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
166829 || (pItem->fg.jointype & (JT_OUTER|JT_CROSS|JT_LTORJ))!=0
166837 ** prevent FROM-clause terms from moving from the right side of
166841 if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1;
166843 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
166848 if( IsVirtual(pItem->pTab) ){
166851 if( mUnusable || (p->fg.jointype & (JT_OUTER|JT_CROSS)) ){
166852 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
166861 if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
166864 mPrior |= pNew->maskSelf;
166865 if( rc || db->mallocFailed ){
166895 ** pOrderBy terms must be matched in strict left-to-right order.
166902 u16 nLoop, /* Number of entries in pPath->aLoop[] */
166903 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
166909 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
166925 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
166928 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
166932 ** We say the WhereLoop is "one-row" if it generates no more than one
166933 ** row of output. A WhereLoop is one-row if all of the following are true:
166936 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
166937 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
166939 ** We say the WhereLoop is "order-distinct" if the set of columns from
166941 ** row of the WhereLoop. Every one-row WhereLoop is automatically
166942 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
166943 ** is not order-distinct. To be order-distinct is not quite the same as being
166945 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
166946 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
166950 ** automatically order-distinct.
166956 nOrderBy = pOrderBy->nExpr;
166957 testcase( nOrderBy==BMS-1 );
166958 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
166960 obDone = MASKBIT(nOrderBy)-1;
166968 if( iLoop>0 ) ready |= pLoop->maskSelf;
166970 pLoop = pPath->aLoop[iLoop];
166975 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
166976 if( pLoop->u.vtab.isOrdered
166983 pLoop->u.btree.nDistinctCol = 0;
166985 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
166994 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
166996 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
166997 if( pOBExpr->iTable!=iCur ) continue;
166998 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
167001 if( pTerm->eOperator==WO_IN ){
167007 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
167008 if( j>=pLoop->nLTerm ) continue;
167010 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
167011 Parse *pParse = pWInfo->pParse;
167012 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr);
167013 CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr);
167015 if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){
167018 testcase( pTerm->pExpr->op==TK_IS );
167023 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
167024 if( pLoop->wsFlags & WHERE_IPK ){
167028 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
167031 nKeyCol = pIndex->nKeyCol;
167032 nColumn = pIndex->nColumn;
167033 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
167034 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
167035 || !HasRowid(pIndex->pTable));
167036 /* All relevant terms of the index must also be non-NULL in order
167039 ** made at tag-20210426-1 below */
167041 && (pLoop->wsFlags & WHERE_SKIPSCAN)==0;
167052 assert( j>=pLoop->u.btree.nEq
167053 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
167055 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
167056 u16 eOp = pLoop->aLTerm[j]->eOperator;
167061 ** the loop need to be marked as not order-distinct because it can
167080 ** j<pLoop->u.btree.nEq constraint above. Any equality other
167083 Expr *pX = pLoop->aLTerm[j]->pExpr;
167084 for(i=j+1; i<pLoop->u.btree.nEq; i++){
167085 if( pLoop->aLTerm[i]->pExpr==pX ){
167086 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
167095 ** (revIdx) for the j-th column of the index.
167098 iColumn = pIndex->aiColumn[j];
167099 revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC;
167100 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
167107 ** WhereLoop is not well-ordered. tag-20210426-1
167111 && j>=pLoop->u.btree.nEq
167112 && pIndex->pTable->aCol[iColumn].notNull==0
167121 /* Find the ORDER BY term that corresponds to the j-th column
167127 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
167133 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
167134 if( pOBExpr->iTable!=iCur ) continue;
167135 if( pOBExpr->iColumn!=iColumn ) continue;
167137 Expr *pIxExpr = pIndex->aColExpr->a[j].pExpr;
167143 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
167144 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
167147 pLoop->u.btree.nDistinctCol = j+1;
167157 != (pOrderBy->a[i].fg.sortFlags&KEYINFO_ORDER_DESC)
167162 rev = revIdx ^ (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC);
167167 if( isMatch && (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL) ){
167168 if( j==pLoop->u.btree.nEq ){
167169 pLoop->wsFlags |= WHERE_BIGNULL_SORT;
167193 } /* end-if not one-row */
167197 orderDistinctMask |= pLoop->maskSelf;
167202 p = pOrderBy->a[i].pExpr;
167203 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
167210 } /* End the loop over all WhereLoops from outer-most down to inner-most */
167213 for(i=nOrderBy-1; i>0; i--){
167214 Bitmask m = ALWAYS(i<BMS) ? MASKBIT(i) - 1 : 0;
167219 return -1;
167226 ** BY clause - and so any order that groups rows as required satisfies the
167243 ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
167244 ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
167247 assert( pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY) );
167248 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
167249 return pWInfo->sorted;
167257 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
167258 if( pLast ) zName[i++] = pLast->cId;
167280 ** Or, if the order-by clause has X terms but only the last Y
167281 ** terms are out of order, then block-sorting will reduce the
167297 assert( pWInfo->pSelect!=0 );
167298 assert( pWInfo->pSelect->pEList!=0 );
167300 nCol = sqlite3LogEst((pWInfo->pSelect->pEList->nExpr+59)/30);
167304 rSortCost += sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
167312 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 ){
167317 if( pWInfo->iLimit<nRow ){
167318 nRow = pWInfo->iLimit;
167320 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){
167323 if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) ); }
167330 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
167332 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
167362 pParse = pWInfo->pParse;
167363 nLoop = pWInfo->nLevel;
167365 ** For 2-way joins, the 5 best paths are followed.
167368 assert( nLoop<=pWInfo->pTabList->nSrc );
167369 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d, nQueryLoop=%d)\n",
167370 nRowEst, pParse->nQueryLoop));
167377 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
167380 nOrderBy = pWInfo->pOrderBy->nExpr;
167386 pSpace = sqlite3StackAllocRawNN(pParse->db, nSpace);
167392 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
167393 pFrom->aLoop = pX;
167398 ** is either zero - meaning it has not yet been initialized - or the
167413 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
167421 ** -1, indicating that the result set may or may not be ordered,
167423 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
167432 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
167438 Bitmask revMask; /* Mask of rev-order loops for (..) */
167440 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
167441 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
167442 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
167453 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
167454 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
167455 nOut = pFrom->nRow + pWLoop->nOut;
167456 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
167457 isOrdered = pFrom->isOrdered;
167461 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
167464 revMask = pFrom->revLoop;
167479 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
167480 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
167484 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
167488 ** mxChoice best-so-far paths.
167490 ** First look for an existing path among best-so-far paths
167494 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
167495 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
167496 ** of legal values for isOrdered, -1..64.
167499 if( pTo->maskLoop==maskNew
167500 && ((pTo->isOrdered^isOrdered)&0x80)==0
167502 testcase( jj==nTo-1 );
167507 /* None of the existing best-so-far paths match the candidate. */
167512 ** paths currently in the best-so-far buffer. So discard
167516 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
167524 ** needs to be added to the set of best-so-far paths. */
167535 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
167541 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
167547 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
167549 if( pTo->rCost<rCost
167550 || (pTo->rCost==rCost
167551 && (pTo->nRow<nOut
167552 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
167559 "Skip %s cost=%-3d,%3d,%3d order=%c",
167562 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
167563 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
167564 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
167568 testcase( pTo->rCost==rCost );
167571 testcase( pTo->rCost==rCost+1 );
167577 "Update %s cost=%-3d,%3d,%3d order=%c",
167580 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
167581 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
167582 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
167587 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
167588 pTo->revLoop = revMask;
167589 pTo->nRow = nOut;
167590 pTo->rCost = rCost;
167591 pTo->rUnsorted = rUnsorted;
167592 pTo->isOrdered = isOrdered;
167593 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
167594 pTo->aLoop[iLoop] = pWLoop;
167600 if( pTo->rCost>mxCost
167601 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
167603 mxCost = pTo->rCost;
167604 mxUnsorted = pTo->rUnsorted;
167614 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
167616 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
167617 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
167618 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
167619 if( pTo->isOrdered>0 ){
167620 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
167637 sqlite3StackFreeNN(pParse->db, pSpace);
167644 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
167646 assert( pWInfo->nLevel==nLoop );
167649 WhereLevel *pLevel = pWInfo->a + iLoop;
167650 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
167651 pLevel->iFrom = pWLoop->iTab;
167652 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
167654 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
167655 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
167656 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
167660 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
167661 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used);
167662 if( rc==pWInfo->pResultSet->nExpr ){
167663 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
167666 pWInfo->bOrderedInnerLoop = 0;
167667 if( pWInfo->pOrderBy ){
167668 pWInfo->nOBSat = pFrom->isOrdered;
167669 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
167670 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
167671 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
167673 /* vvv--- See check-in [12ad822d9b827777] on 2023-03-16 ---vvv */
167674 assert( pWInfo->pSelect->pOrderBy==0
167675 || pWInfo->nOBSat <= pWInfo->pSelect->pOrderBy->nExpr );
167677 pWInfo->revMask = pFrom->revLoop;
167678 if( pWInfo->nOBSat<=0 ){
167679 pWInfo->nOBSat = 0;
167681 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
167686 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
167687 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
167690 if( rc==pWInfo->pOrderBy->nExpr ){
167691 pWInfo->bOrderedInnerLoop = 1;
167692 pWInfo->revMask = m;
167697 && pWInfo->nOBSat==1
167698 && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX))!=0
167700 pWInfo->bOrderedInnerLoop = 1;
167703 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
167704 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
167707 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
167708 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
167710 assert( pWInfo->sorted==0 );
167711 if( nOrder==pWInfo->pOrderBy->nExpr ){
167712 pWInfo->sorted = 1;
167713 pWInfo->revMask = revMask;
167718 pWInfo->nRowOut = pFrom->nRow;
167721 sqlite3StackFreeNN(pParse->db, pSpace);
167738 ** FROM clause term that would try to do a full-table scan. This prevents
167739 ** an index search from being converted into a full-table scan in order to
167741 ** performance using the full-scan without sorting if the output size
167743 ** degradation using the full-scan if the output size estimate is too large.
167746 ** Except, if the first solver() call generated a full-table scan in an outer
167747 ** loop then stop this analysis at the first full-scan, since the second
167748 ** solver() run might try to swap that full-scan for another in order to
167753 ** |-- SCAN t1 |-- SCAN t2
167754 ** |-- SEARCH t2 `-- SEARCH t1
167755 ** `-- SORT USING B-TREE
167760 ** |-- SEARCH t1 |-- SCAN t2 <--- bad!
167761 ** |-- SEARCH t2 `-- SEARCH t1
167762 ** `-- SORT USING B-TREE
167764 ** See test cases in test/whereN.test for the real-world query that
167772 for(i=0; i<pWInfo->nLevel; i++){
167773 WhereLoop *p = pWInfo->a[i].pWLoop;
167775 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 ) continue;
167776 if( (p->wsFlags & (WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0 ){
167777 u8 iTab = p->iTab;
167779 for(pLoop=pWInfo->pLoops; pLoop; pLoop=pLoop->pNextLoop){
167780 if( pLoop->iTab!=iTab ) continue;
167781 if( (pLoop->wsFlags & (WHERE_CONSTRAINT|WHERE_AUTO_INDEX))!=0 ){
167782 /* Auto-index and index-constrained loops allowed to remain */
167791 sqlite3WhereLoopPrint(pLoop, &pWInfo->sWC);
167794 pLoop->prereq = ALLBITS; /* Prevent 2nd solver() from using this one */
167806 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
167809 ** Return non-zero on success, if this query can be handled by this
167810 ** no-frills query planner. Return zero if this query needs the
167811 ** general-purpose query planner.
167825 pWInfo = pBuilder->pWInfo;
167826 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
167827 assert( pWInfo->pTabList->nSrc>=1 );
167828 pItem = pWInfo->pTabList->a;
167829 pTab = pItem->pTab;
167831 if( pItem->fg.isIndexedBy || pItem->fg.notIndexed ){
167832 testcase( pItem->fg.isIndexedBy );
167833 testcase( pItem->fg.notIndexed );
167836 iCur = pItem->iCursor;
167837 pWC = &pWInfo->sWC;
167838 pLoop = pBuilder->pNew;
167839 pLoop->wsFlags = 0;
167840 pLoop->nSkip = 0;
167841 pTerm = whereScanInit(&scan, pWC, iCur, -1, WO_EQ|WO_IS, 0);
167842 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
167844 testcase( pTerm->eOperator & WO_IS );
167845 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
167846 pLoop->aLTerm[0] = pTerm;
167847 pLoop->nLTerm = 1;
167848 pLoop->u.btree.nEq = 1;
167850 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
167852 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
167854 assert( pLoop->aLTermSpace==pLoop->aLTerm );
167856 || pIdx->pPartIdxWhere!=0
167857 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
167859 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
167860 for(j=0; j<pIdx->nKeyCol; j++){
167862 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
167864 testcase( pTerm->eOperator & WO_IS );
167865 pLoop->aLTerm[j] = pTerm;
167867 if( j!=pIdx->nKeyCol ) continue;
167868 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
167869 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
167870 pLoop->wsFlags |= WHERE_IDX_ONLY;
167872 pLoop->nLTerm = j;
167873 pLoop->u.btree.nEq = j;
167874 pLoop->u.btree.pIndex = pIdx;
167876 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
167880 if( pLoop->wsFlags ){
167881 pLoop->nOut = (LogEst)1;
167882 pWInfo->a[0].pWLoop = pLoop;
167883 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
167884 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
167885 pWInfo->a[0].iTabCur = iCur;
167886 pWInfo->nRowOut = 1;
167887 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
167888 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
167889 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
167891 if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS;
167893 pLoop->cId = '0';
167909 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
167910 pWalker->eCode = 0;
167917 ** Return true if the expression contains no non-deterministic SQL
167918 ** functions. Do not consider non-deterministic SQL functions that are
167919 ** part of sub-select statements.
167942 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
167943 p->cId = zLabel[i%(sizeof(zLabel)-1)];
167963 ** 5) The table must not have an inner-join ON or USING clause if there is
167967 ** the right-most table of a subquery that was flattened into the
167968 ** main query and that subquery was the right-hand operand of an
167971 ** 7) The omit-noop-join optimization is enabled.
168002 assert( pWInfo->nLevel>=2 );
168003 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_OmitNoopJoin) );
168007 assert( pWInfo->pResultSet!=0 );
168008 assert( 0==(pWInfo->wctrlFlags & WHERE_AGG_DISTINCT) );
168010 tabUsed = sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pResultSet);
168011 if( pWInfo->pOrderBy ){
168012 tabUsed |= sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pOrderBy);
168014 hasRightJoin = (pWInfo->pTabList->a[0].fg.jointype & JT_LTORJ)!=0;
168015 for(i=pWInfo->nLevel-1; i>=1; i--){
168019 pLoop = pWInfo->a[i].pWLoop;
168020 pItem = &pWInfo->pTabList->a[pLoop->iTab];
168021 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ) continue;
168022 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)==0
168023 && (pLoop->wsFlags & WHERE_ONEROW)==0
168027 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
168028 pEnd = pWInfo->sWC.a + pWInfo->sWC.nTerm;
168029 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
168030 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
168031 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON)
168032 || pTerm->pExpr->w.iJoin!=pItem->iCursor
168038 && ExprHasProperty(pTerm->pExpr, EP_InnerON)
168039 && pTerm->pExpr->w.iJoin==pItem->iCursor
168045 WHERETRACE(0xffffffff, ("-> drop loop %c not used\n", pLoop->cId));
168046 notReady &= ~pLoop->maskSelf;
168047 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
168048 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
168049 pTerm->wtFlags |= TERM_CODED;
168052 if( i!=pWInfo->nLevel-1 ){
168053 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
168054 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
168056 pWInfo->nLevel--;
168057 assert( pWInfo->nLevel>0 );
168071 ** (3) Bloom-filter processing is not disabled. (Checked by the
168086 assert( pWInfo->nLevel>=2 );
168087 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) );
168088 for(i=0; i<pWInfo->nLevel; i++){
168089 WhereLoop *pLoop = pWInfo->a[i].pWLoop;
168091 SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
168092 Table *pTab = pItem->pTab;
168093 if( (pTab->tabFlags & TF_HasStat1)==0 ) break;
168094 pTab->tabFlags |= TF_MaybeReanalyze;
168096 && (pLoop->wsFlags & reqFlags)==reqFlags
168097 /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
168098 && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0)
168100 if( nSearch > pTab->nRowLogEst ){
168101 testcase( pItem->fg.jointype & JT_LEFT );
168102 pLoop->wsFlags |= WHERE_BLOOMFILTER;
168103 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
168105 "-> use Bloom-filter on loop %c because there are ~%.1e "
168107 pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName,
168108 (double)sqlite3LogEstToInt(pTab->nRowLogEst)));
168111 nSearch += pLoop->nOut;
168124 ** Assume that every function is able to pass-through a subtype from
168136 if( pExpr->op!=TK_FUNCTION ){
168140 db = pWalker->pParse->db;
168141 n = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
168142 pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
168143 if( pDef==0 || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){
168144 pWalker->eCode = 1;
168173 ** This routine adds IndexedExpr entries to the Parse->pIdxEpr field for
168179 Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxEpr */
168180 Index *pIdx, /* The index-on-expression that contains the expressions */
168187 assert( pIdx->bHasExpr );
168188 pTab = pIdx->pTable;
168189 for(i=0; i<pIdx->nColumn; i++){
168191 int j = pIdx->aiColumn[i];
168193 pExpr = pIdx->aColExpr->a[i].pExpr;
168194 }else if( j>=0 && (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)!=0 ){
168195 pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]);
168200 if( pExpr->op==TK_FUNCTION && sqlite3ExprCanReturnSubtype(pParse,pExpr) ){
168206 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
168208 p->pIENext = pParse->pIdxEpr;
168211 sqlite3DebugPrintf("New pParse->pIdxEpr term {%d,%d}\n", iIdxCur, i);
168215 p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
168216 p->iDataCur = pTabItem->iCursor;
168217 p->iIdxCur = iIdxCur;
168218 p->iIdxCol = i;
168219 p->bMaybeNullRow = (pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0;
168220 if( sqlite3IndexAffinityStr(pParse->db, pIdx) ){
168221 p->aff = pIdx->zColAff[i];
168224 p->zIdxName = pIdx->zName;
168226 pParse->pIdxEpr = p;
168227 if( p->pIENext==0 ){
168228 void *pArg = (void*)&pParse->pIdxEpr;
168235 ** Set the reverse-scan order mask to one for all tables in the query
168244 for(ii=0; ii<pWInfo->pTabList->nSrc; ii++){
168245 SrcItem *pItem = &pWInfo->pTabList->a[ii];
168246 if( !pItem->fg.isCte
168247 || pItem->u2.pCteUse->eM10d!=M10d_Yes
168248 || NEVER(pItem->pSelect==0)
168249 || pItem->pSelect->pOrderBy==0
168251 pWInfo->revMask |= MASKBIT(ii);
168275 ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
168279 ** end |-- by sqlite3WhereEnd()
168286 ** scanning through all values on the right-hand side of the IN.
168289 ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
168299 ** entire tables. Thus a three-way join is an O(N^3) operation. But if
168309 ** inner loops (or around the "..." if the test occurs within the inner-
168358 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
168362 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
168379 db = pParse->db;
168383 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
168384 if( pOrderBy && pOrderBy->nExpr>=BMS ){
168387 wctrlFlags |= WHERE_KEEP_ALL_JOINS; /* Disable omit-noop-join opt */
168393 testcase( pTabList->nSrc==BMS );
168394 if( pTabList->nSrc>BMS ){
168404 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
168409 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
168410 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
168415 nByteWInfo = ROUND8P(nByteWInfo + (nTabList-1)*sizeof(WhereLevel));
168418 if( db->mallocFailed ){
168423 pWInfo->pParse = pParse;
168424 pWInfo->pTabList = pTabList;
168425 pWInfo->pOrderBy = pOrderBy;
168427 pWInfo->pWhere = pWhere;
168429 pWInfo->pResultSet = pResultSet;
168430 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
168431 pWInfo->nLevel = nTabList;
168432 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
168433 pWInfo->wctrlFlags = wctrlFlags;
168434 pWInfo->iLimit = iAuxArg;
168435 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
168436 pWInfo->pSelect = pSelect;
168437 memset(&pWInfo->nOBSat, 0,
168438 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
168439 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
168440 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
168441 pMaskSet = &pWInfo->sMaskSet;
168442 pMaskSet->n = 0;
168443 pMaskSet->ix[0] = -99; /* Initialize ix[0] to a value that can never be
168445 ** test for pMaskSet->n==0 in sqlite3WhereGetMask() */
168447 sWLB.pWC = &pWInfo->sWC;
168452 sWLB.pNew->cId = '*';
168458 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
168459 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
168464 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
168468 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
168470 if( ALWAYS(pWInfo->pSelect)
168471 && (pWInfo->pSelect->selFlags & SF_MultiValue)==0
168478 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
168481 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
168485 ** Note that bitmasks are created for all pTabList->nSrc tables in
168487 ** equal to pTabList->nSrc but might be shortened to 1 if the
168492 createMask(pMaskSet, pTabList->a[ii].iCursor);
168493 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
168494 }while( (++ii)<pTabList->nSrc );
168498 for(ii=0; ii<pTabList->nSrc; ii++){
168499 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
168508 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
168509 if( pSelect && pSelect->pLimit ){
168510 sqlite3WhereAddLimit(&pWInfo->sWC, pSelect);
168512 if( pParse->nErr ) goto whereBeginError;
168514 /* The False-WHERE-Term-Bypass optimization:
168523 ** right-hand side of a LEFT or FULL JOIN.
168526 ** (4) If the expression contains non-deterministic functions
168527 ** that are not within a sub-select. This is not required
168531 ** WHERE random()>0; -- eval random() once per row
168532 ** WHERE (SELECT random())>0; -- eval random() just once overall
168540 for(ii=0; ii<sWLB.pWC->nBase; ii++){
168541 WhereTerm *pT = &sWLB.pWC->a[ii]; /* A term of the WHERE clause */
168543 if( pT->wtFlags & TERM_VIRTUAL ) continue;
168544 pX = pT->pExpr;
168546 assert( pT->prereqAll!=0 || !ExprHasProperty(pX, EP_OuterON) );
168547 if( pT->prereqAll==0 /* Conditions (1) and (2) */
168550 && (pTabList->a[0].fg.jointype & JT_LTORJ)!=0 )
168552 sqlite3ExprIfFalse(pParse, pX, pWInfo->iBreak, SQLITE_JUMPIFNULL);
168553 pT->wtFlags |= TERM_CODED;
168562 pWInfo->wctrlFlags &= ~WHERE_WANT_DISTINCT;
168563 }else if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
168565 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
168568 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
168569 pWInfo->pOrderBy = pResultSet;
168592 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
168613 while( pWInfo->pLoops ){
168614 WhereLoop *p = pWInfo->pLoops;
168615 pWInfo->pLoops = p->pNextLoop;
168625 if( db->mallocFailed ) goto whereBeginError;
168626 if( pWInfo->pOrderBy ){
168628 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
168629 if( db->mallocFailed ) goto whereBeginError;
168633 ** the output size by a factor of 8 (LogEst -30).
168635 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0 ){
168637 pWInfo->nRowOut, pWInfo->nRowOut-30));
168638 pWInfo->nRowOut -= 30;
168642 assert( pWInfo->pTabList!=0 );
168643 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
168646 if( pParse->nErr ){
168649 assert( db->mallocFailed==0 );
168652 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
168653 if( pWInfo->nOBSat>0 ){
168654 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
168656 switch( pWInfo->eDistinct ){
168671 for(ii=0; ii<pWInfo->nLevel; ii++){
168672 sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
168680 ** This query optimization is factored out into a separate "no-inline"
168683 ** some C-compiler optimizers from in-lining the
168685 ** in-line sqlite3WhereCodeOneLoopStart() for performance reasons.
168688 if( pWInfo->nLevel>=2 /* Must be a join, or this opt8n is pointless */
168694 nTabList = pWInfo->nLevel;
168701 if( pWInfo->nLevel>=2
168709 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
168714 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
168717 ** to use a one-pass algorithm, determine if this is appropriate.
168719 ** A one-pass approach can be used if the caller has requested one
168723 ** * the caller has indicated that a one-pass approach can be used
168732 ** use a one-pass approach, and this is not set accurately for scans
168735 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
168737 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
168739 assert( !(wsFlags & WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pTab) );
168742 && !IsVirtual(pTabList->a[0].pTab)
168746 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
168747 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
168751 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
168759 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
168764 pTabItem = &pTabList->a[pLevel->iFrom];
168765 pTab = pTabItem->pTab;
168766 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
168767 pLoop = pLevel->pWLoop;
168768 if( (pTab->tabFlags & TF_Ephemeral)!=0 || IsView(pTab) ){
168772 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
168774 int iCur = pTabItem->iCursor;
168780 if( ((pLoop->wsFlags & WHERE_IDX_ONLY)==0
168782 || (pTabItem->fg.jointype & (JT_LTORJ|JT_RIGHT))!=0
168785 if( pWInfo->eOnePass!=ONEPASS_OFF ){
168787 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
168789 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
168790 assert( pTabItem->iCursor==pLevel->iTabCur );
168791 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
168792 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
168793 if( pWInfo->eOnePass==ONEPASS_OFF
168794 && pTab->nCol<BMS
168795 && (pTab->tabFlags & (TF_HasGenerated|TF_WithoutRowid))==0
168796 && (pLoop->wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))==0
168801 Bitmask b = pTabItem->colUsed;
168804 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32);
168805 assert( n<=pTab->nCol );
168808 if( pLoop->u.btree.pIndex!=0 && (pTab->tabFlags & TF_WithoutRowid)==0 ){
168816 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
168817 (const u8*)&pTabItem->colUsed, P4_INT64);
168820 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
168822 if( pLoop->wsFlags & WHERE_INDEXED ){
168823 Index *pIx = pLoop->u.btree.pIndex;
168827 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
168831 /* This is one term of an OR-optimization using the PRIMARY KEY of a
168833 iIndexCur = pLevel->iTabCur;
168835 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
168836 Index *pJ = pTabItem->pTab->pIndex;
168841 pJ = pJ->pNext;
168844 pWInfo->aiCurOnePass[1] = iIndexCur;
168849 iIndexCur = pParse->nTab++;
168850 if( pIx->bHasExpr && OptimizationEnabled(db, SQLITE_IndexedExpr) ){
168853 if( pIx->pPartIdxWhere && (pTabItem->fg.jointype & JT_RIGHT)==0 ){
168855 pParse, pIx, pIx->pPartIdxWhere, 0, iIndexCur, pTabItem
168859 pLevel->iIdxCur = iIndexCur;
168861 assert( pIx->pSchema==pTab->pSchema );
168864 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
168866 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
168867 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
168868 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0
168869 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
168870 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
168871 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
168875 VdbeComment((v, "%s", pIx->zName));
168880 for(ii=0; ii<pIx->nColumn; ii++){
168881 jj = pIx->aiColumn[ii];
168884 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
168894 if( (pTabItem->fg.jointype & JT_RIGHT)!=0
168895 && (pLevel->pRJ = sqlite3WhereMalloc(pWInfo, sizeof(WhereRightJoin)))!=0
168897 WhereRightJoin *pRJ = pLevel->pRJ;
168898 pRJ->iMatch = pParse->nTab++;
168899 pRJ->regBloom = ++pParse->nMem;
168900 sqlite3VdbeAddOp2(v, OP_Blob, 65536, pRJ->regBloom);
168901 pRJ->regReturn = ++pParse->nMem;
168902 sqlite3VdbeAddOp2(v, OP_Null, 0, pRJ->regReturn);
168903 assert( pTab==pTabItem->pTab );
168906 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, 1);
168907 pInfo = sqlite3KeyInfoAlloc(pParse->db, 1, 0);
168909 pInfo->aColl[0] = 0;
168910 pInfo->aSortFlags[0] = 0;
168915 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, pPk->nKeyCol);
168918 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
168922 pWInfo->nOBSat = 0;
168923 pWInfo->eDistinct = WHERE_DISTINCT_UNORDERED;
168926 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
168927 if( db->mallocFailed ) goto whereBeginError;
168937 if( pParse->nErr ) goto whereBeginError;
168938 pLevel = &pWInfo->a[ii];
168939 wsFlags = pLevel->pWLoop->wsFlags;
168940 pSrc = &pTabList->a[pLevel->iFrom];
168941 if( pSrc->fg.isMaterialized ){
168942 if( pSrc->fg.isCorrelated ){
168943 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
168946 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
168950 assert( pTabList == pWInfo->pTabList );
168954 constructAutomaticIndex(pParse, &pWInfo->sWC, notReady, pLevel);
168959 if( db->mallocFailed ) goto whereBeginError;
168964 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
168966 pWInfo->iContinue = pLevel->addrCont;
168973 VdbeModuleComment((v, "Begin WHERE-core"));
168974 pWInfo->iEndWhere = sqlite3VdbeCurrentAddr(v);
168980 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
168998 # define OpcodeRewriteTrace(D,K,P) /* no-op */
169006 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
169016 Parse *pParse = pWInfo->pParse;
169017 Vdbe *v = pParse->pVdbe;
169021 SrcList *pTabList = pWInfo->pTabList;
169022 sqlite3 *db = pParse->db;
169028 VdbeModuleComment((v, "End WHERE-core"));
169029 for(i=pWInfo->nLevel-1; i>=0; i--){
169031 pLevel = &pWInfo->a[i];
169032 if( pLevel->pRJ ){
169035 WhereRightJoin *pRJ = pLevel->pRJ;
169036 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
169037 pLevel->addrCont = 0;
169038 pRJ->endSubrtn = sqlite3VdbeCurrentAddr(v);
169039 sqlite3VdbeAddOp3(v, OP_Return, pRJ->regReturn, pRJ->addrSubrtn, 1);
169043 pLoop = pLevel->pWLoop;
169044 if( pLevel->op!=OP_Noop ){
169049 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
169050 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
169051 && (pLoop->wsFlags & WHERE_INDEXED)!=0
169052 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
169053 && (n = pLoop->u.btree.nDistinctCol)>0
169054 && pIdx->aiRowLogEst[n]>=36
169056 int r1 = pParse->nMem+1;
169059 sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
169061 pParse->nMem += n+1;
169062 op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
169063 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
169066 sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
169070 if( pLevel->addrCont ) sqlite3VdbeResolveLabel(v, pLevel->addrCont);
169071 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
169072 sqlite3VdbeChangeP5(v, pLevel->p5);
169074 VdbeCoverageIf(v, pLevel->op==OP_Next);
169075 VdbeCoverageIf(v, pLevel->op==OP_Prev);
169076 VdbeCoverageIf(v, pLevel->op==OP_VNext);
169077 if( pLevel->regBignull ){
169078 sqlite3VdbeResolveLabel(v, pLevel->addrBignull);
169079 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, pLevel->regBignull, pLevel->p2-1);
169085 }else if( pLevel->addrCont ){
169086 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
169088 if( (pLoop->wsFlags & WHERE_IN_ABLE)!=0 && pLevel->u.in.nIn>0 ){
169091 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
169092 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
169093 assert( sqlite3VdbeGetOp(v, pIn->addrInTop+1)->opcode==OP_IsNull
169094 || pParse->db->mallocFailed );
169095 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
169096 if( pIn->eEndLoopOp!=OP_Noop ){
169097 if( pIn->nPrefix ){
169099 (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
169100 && (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0;
169101 if( pLevel->iLeftJoin ){
169102 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
169107 ** return the null-row. So, if the cursor is not open yet,
169110 sqlite3VdbeAddOp2(v, OP_IfNotOpen, pIn->iCur,
169115 sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
169117 pIn->iBase, pIn->nPrefix);
169123 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
169126 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
169128 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
169129 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
169131 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
169134 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
169135 if( pLevel->pRJ ){
169136 sqlite3VdbeAddOp3(v, OP_Return, pLevel->pRJ->regReturn, 0, 1);
169139 if( pLevel->addrSkip ){
169140 sqlite3VdbeGoto(v, pLevel->addrSkip);
169141 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
169142 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
169143 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
169146 if( pLevel->addrLikeRep ){
169147 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1),
169148 pLevel->addrLikeRep);
169152 if( pLevel->iLeftJoin ){
169153 int ws = pLoop->wsFlags;
169154 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
169157 SrcItem *pSrc = &pTabList->a[pLevel->iFrom];
169158 assert( pLevel->iTabCur==pSrc->iCursor );
169159 if( pSrc->fg.viaCoroutine ){
169161 n = pSrc->regResult;
169162 assert( pSrc->pTab!=0 );
169163 m = pSrc->pTab->nCol;
169164 sqlite3VdbeAddOp3(v, OP_Null, 0, n, n+m-1);
169166 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
169169 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCoveringIdx)
169172 Index *pIx = pLevel->u.pCoveringIdx;
169173 int iDb = sqlite3SchemaToIndex(db, pIx->pSchema);
169174 sqlite3VdbeAddOp3(v, OP_ReopenIdx, pLevel->iIdxCur, pIx->tnum, iDb);
169177 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
169179 if( pLevel->op==OP_Return ){
169180 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
169182 sqlite3VdbeGoto(v, pLevel->addrFirst);
169186 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
169187 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
169190 assert( pWInfo->nLevel<=pTabList->nSrc );
169191 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
169195 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
169196 Table *pTab = pTabItem->pTab;
169198 pLoop = pLevel->pWLoop;
169204 if( pLevel->pRJ ){
169209 /* For a co-routine, change all OP_Column references to the table of
169210 ** the co-routine into OP_Copy of result contained in a register.
169213 if( pTabItem->fg.viaCoroutine ){
169214 testcase( pParse->db->mallocFailed );
169215 assert( pTabItem->regResult>=0 );
169216 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
169217 pTabItem->regResult, 0);
169232 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
169233 pIdx = pLoop->u.btree.pIndex;
169234 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
169235 pIdx = pLevel->u.pCoveringIdx;
169238 && !db->mallocFailed
169240 if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){
169243 last = pWInfo->iEndWhere;
169245 if( pIdx->bHasExpr ){
169246 IndexedExpr *p = pParse->pIdxEpr;
169248 if( p->iIdxCur==pLevel->iIdxCur ){
169251 sqlite3DebugPrintf("Disable pParse->pIdxEpr term {%d,%d}\n",
169252 p->iIdxCur, p->iIdxCol);
169253 if( sqlite3WhereTrace & 0x5000 ) sqlite3ShowExpr(p->pExpr);
169256 p->iDataCur = -1;
169257 p->iIdxCur = -1;
169259 p = p->pIENext;
169262 k = pLevel->addrBody + 1;
169264 if( db->flags & SQLITE_VdbeAddopTrace ){
169265 printf("TRANSLATE cursor %d->%d in opcode range %d..%d\n",
169266 pLevel->iTabCur, pLevel->iIdxCur, k, last-1);
169269 pOp = sqlite3VdbeGetOp(v, k - 1);
169270 assert( pOp->opcode!=OP_Column || pOp->p1!=pLevel->iTabCur );
169271 assert( pOp->opcode!=OP_Rowid || pOp->p1!=pLevel->iTabCur );
169272 assert( pOp->opcode!=OP_IfNullRow || pOp->p1!=pLevel->iTabCur );
169275 pLastOp = pOp + (last - k);
169278 if( pOp->p1!=pLevel->iTabCur ){
169279 /* no-op */
169280 }else if( pOp->opcode==OP_Column
169282 || pOp->opcode==OP_Offset
169285 int x = pOp->p2;
169286 assert( pIdx->pTable==pTab );
169288 if( pOp->opcode==OP_Offset ){
169294 x = pPk->aiColumn[x];
169302 pOp->p2 = x;
169303 pOp->p1 = pLevel->iIdxCur;
169307 ** reference. Verify that this is harmless - that the
169310 if( pLoop->wsFlags & WHERE_IDX_ONLY ){
169312 pParse->rc = SQLITE_INTERNAL;
169315 }else if( pOp->opcode==OP_Rowid ){
169316 pOp->p1 = pLevel->iIdxCur;
169317 pOp->opcode = OP_IdxRowid;
169319 }else if( pOp->opcode==OP_IfNullRow ){
169320 pOp->p1 = pLevel->iIdxCur;
169328 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
169336 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
169340 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
169342 pParse->withinRJSubrtn -= nRJ;
169391 ** the sub-query.
169396 ** select-list and ORDER BY expressions in the parent query are
169397 ** selected by the sub-query. For the purposes of the transformation,
169429 ** sub-query, which is always implemented as a co-routine. It then calls
169434 ** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
169435 ** When the sub-routine is invoked:
169437 ** * The results of all window-functions for the row are stored
169449 ** BUILT-IN WINDOW FUNCTIONS
169451 ** This implementation features the following built-in window functions:
169465 ** These are the same built-in window functions supported by Postgres.
169468 ** be implemented using an API, built-in window functions are much more
169471 ** As such, some built-in window functions use the same API as aggregate
169482 ** As well as some of the built-in window functions, aggregate window
169489 ** Implementation of built-in window function row_number(). Assumes that the
169520 ** Implementation of built-in window function dense_rank(). Assumes that
169532 if( p ) p->nStep = 1;
169540 if( p->nStep ){
169541 p->nValue++;
169542 p->nStep = 0;
169544 sqlite3_result_int64(pCtx, p->nValue);
169549 ** Implementation of built-in window function nth_value(). This
169550 ** implementation is used in "slow mode" only - when the EXCLUDE clause
169581 p->nStep++;
169582 if( iVal==p->nStep ){
169583 p->pValue = sqlite3_value_dup(apArg[0]);
169584 if( !p->pValue ){
169595 pCtx, "second argument to nth_value must be a positive integer", -1
169601 if( p && p->pValue ){
169602 sqlite3_result_value(pCtx, p->pValue);
169603 sqlite3_value_free(p->pValue);
169604 p->pValue = 0;
169617 if( p && p->pValue==0 ){
169618 p->pValue = sqlite3_value_dup(apArg[0]);
169619 if( !p->pValue ){
169629 if( p && p->pValue ){
169630 sqlite3_result_value(pCtx, p->pValue);
169631 sqlite3_value_free(p->pValue);
169632 p->pValue = 0;
169639 ** Implementation of built-in window function rank(). Assumes that
169652 p->nStep++;
169653 if( p->nValue==0 ){
169654 p->nValue = p->nStep;
169664 sqlite3_result_int64(pCtx, p->nValue);
169665 p->nValue = 0;
169670 ** Implementation of built-in window function percent_rank(). Assumes that
169685 p->nTotal++;
169697 p->nStep++;
169703 p->nValue = p->nStep;
169704 if( p->nTotal>1 ){
169705 double r = (double)p->nValue / (double)(p->nTotal-1);
169715 ** Implementation of built-in window function cume_dist(). Assumes that
169730 p->nTotal++;
169742 p->nStep++;
169748 double r = (double)(p->nStep) / (double)(p->nTotal);
169778 if( p->nTotal==0 ){
169779 p->nParam = sqlite3_value_int64(apArg[0]);
169780 if( p->nParam<=0 ){
169782 pCtx, "argument of ntile must be a positive integer", -1
169786 p->nTotal++;
169798 p->iRow++;
169803 if( p && p->nParam>0 ){
169804 int nSize = (p->nTotal / p->nParam);
169806 sqlite3_result_int64(pCtx, p->iRow+1);
169808 i64 nLarge = p->nTotal - p->nParam*nSize;
169810 i64 iRow = p->iRow;
169812 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
169817 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
169844 sqlite3_value_free(p->pVal);
169845 p->pVal = sqlite3_value_dup(apArg[0]);
169846 if( p->pVal==0 ){
169849 p->nVal++;
169863 p->nVal--;
169864 if( p->nVal==0 ){
169865 sqlite3_value_free(p->pVal);
169866 p->pVal = 0;
169873 if( p && p->pVal ){
169874 sqlite3_result_value(pCtx, p->pVal);
169880 if( p && p->pVal ){
169881 sqlite3_result_value(pCtx, p->pVal);
169882 sqlite3_value_free(p->pVal);
169883 p->pVal = 0;
169888 ** Static names for the built-in window function names. These static
169893 ** if( pFuncDef->zName==row_valueName ){ ... }
169908 ** No-op implementations of xStep() and xFinalize(). Used as place-holders
169909 ** for built-in window functions that never call those interfaces.
169926 static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ }
169937 ** no-op routines for their methods */
169955 ** Register those built-in window functions that are not also aggregates.
169980 for(p=pList; p; p=p->pNextWin){
169981 if( sqlite3StrICmp(p->zName, zName)==0 ) break;
170002 ** * If the function is a built-in window function that requires the
170003 ** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
170012 if( pWin->zName && pWin->eFrmType==0 ){
170013 Window *p = windowFind(pParse, pList, pWin->zName);
170015 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
170016 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
170017 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
170018 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
170019 pWin->eStart = p->eStart;
170020 pWin->eEnd = p->eEnd;
170021 pWin->eFrmType = p->eFrmType;
170022 pWin->eExclude = p->eExclude;
170026 if( (pWin->eFrmType==TK_RANGE)
170027 && (pWin->pStart || pWin->pEnd)
170028 && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
170034 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
170035 sqlite3 *db = pParse->db;
170036 if( pWin->pFilter ){
170058 if( pFunc->zName==aUp[i].zFunc ){
170059 sqlite3ExprDelete(db, pWin->pStart);
170060 sqlite3ExprDelete(db, pWin->pEnd);
170061 pWin->pEnd = pWin->pStart = 0;
170062 pWin->eFrmType = aUp[i].eFrmType;
170063 pWin->eStart = aUp[i].eStart;
170064 pWin->eEnd = aUp[i].eEnd;
170065 pWin->eExclude = 0;
170066 if( pWin->eStart==TK_FOLLOWING ){
170067 pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1");
170074 pWin->pWFunc = pFunc;
170087 Select *pSubSelect; /* Current sub-select, if any */
170092 ** this function appends to the output expression-list and updates
170096 struct WindowRewrite *p = pWalker->u.pRewrite;
170097 Parse *pParse = pWalker->pParse;
170099 assert( p->pWin!=0 );
170101 /* If this function is being called from within a scalar sub-select
170105 ** to the scalar sub-select. */
170106 if( p->pSubSelect ){
170107 if( pExpr->op!=TK_COLUMN ){
170110 int nSrc = p->pSrc->nSrc;
170113 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
170119 switch( pExpr->op ){
170126 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
170127 if( pExpr->y.pWin==pWin ){
170128 assert( pWin->pOwner==pExpr );
170138 int iCol = -1;
170139 if( pParse->db->mallocFailed ) return WRC_Abort;
170140 if( p->pSub ){
170142 for(i=0; i<p->pSub->nExpr; i++){
170143 if( 0==sqlite3ExprCompare(0, p->pSub->a[i].pExpr, pExpr, -1) ){
170150 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
170151 if( pDup && pDup->op==TK_AGG_FUNCTION ) pDup->op = TK_FUNCTION;
170152 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
170154 if( p->pSub ){
170155 int f = pExpr->flags & EP_Collate;
170158 sqlite3ExprDelete(pParse->db, pExpr);
170162 pExpr->op = TK_COLUMN;
170163 pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol);
170164 pExpr->iTable = p->pWin->iEphCsr;
170165 pExpr->y.pTab = p->pTab;
170166 pExpr->flags = f;
170168 if( pParse->db->mallocFailed ) return WRC_Abort;
170172 default: /* no-op */
170179 struct WindowRewrite *p = pWalker->u.pRewrite;
170180 Select *pSave = p->pSubSelect;
170184 p->pSubSelect = pSelect;
170186 p->pSubSelect = pSave;
170193 ** Iterate through each expression in expression-list pEList. For each:
170200 ** Append the node to output expression-list (*ppSub). And replace it
170201 ** with a TK_COLUMN that reads the (N-1)th element of table
170202 ** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
170211 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
170236 ** Append a copy of each expression in expression-list pAppend to
170247 int nInit = pList ? pList->nExpr : 0;
170248 for(i=0; i<pAppend->nExpr; i++){
170249 sqlite3 *db = pParse->db;
170250 Expr *pDup = sqlite3ExprDup(db, pAppend->a[i].pExpr, 0);
170251 if( db->mallocFailed ){
170260 pSub->op = TK_NULL;
170261 pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse);
170262 pSub->u.zToken = 0;
170266 if( pList ) pList->a[nInit+i].fg.sortFlags = pAppend->a[i].fg.sortFlags;
170275 ** then we have to increase the Expr->op2 values of those nodes
170281 if( pExpr->op==TK_AGG_FUNCTION
170282 && pExpr->op2>=pWalker->walkerDepth
170284 pExpr->op2++;
170290 if( pExpr->op==TK_AGG_FUNCTION && pExpr->pAggInfo==0 ){
170292 sqlite3ErrorMsg(pWalker->pParse,
170293 "misuse of aggregate: %s()", pExpr->u.zToken);
170300 ** any SQL window functions, this function is a no-op. Otherwise, it
170307 if( p->pWin
170308 && p->pPrior==0
170309 && ALWAYS((p->selFlags & SF_WinRewrite)==0)
170313 sqlite3 *db = pParse->db;
170315 SrcList *pSrc = p->pSrc;
170316 Expr *pWhere = p->pWhere;
170317 ExprList *pGroupBy = p->pGroupBy;
170318 Expr *pHaving = p->pHaving;
170321 ExprList *pSublist = 0; /* Expression list for sub-query */
170322 Window *pMWin = p->pWin; /* Main window object */
170327 u32 selFlags = p->selFlags;
170335 if( (p->selFlags & SF_Aggregate)==0 ){
170338 sqlite3WalkExprList(&w, p->pOrderBy);
170341 p->pSrc = 0;
170342 p->pWhere = 0;
170343 p->pGroupBy = 0;
170344 p->pHaving = 0;
170345 p->selFlags &= ~SF_Aggregate;
170346 p->selFlags |= SF_WinRewrite;
170348 /* Create the ORDER BY clause for the sub-select. This is the concatenation
170351 pSort = exprListAppendList(pParse, 0, pMWin->pPartition, 1);
170352 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
170353 if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
170354 int nSave = pSort->nExpr;
170355 pSort->nExpr = p->pOrderBy->nExpr;
170356 if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
170357 sqlite3ExprListDelete(db, p->pOrderBy);
170358 p->pOrderBy = 0;
170360 pSort->nExpr = nSave;
170366 pMWin->iEphCsr = pParse->nTab++;
170367 pParse->nTab += 3;
170369 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist);
170370 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist);
170371 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
170374 ** sub-select expression list. They are required to figure out where
170376 pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
170377 pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);
170380 ** sub-select expression list. Also allocate two registers for each
170381 ** window function - one for the accumulator, another for interim
170383 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
170385 assert( ExprUseXList(pWin->pOwner) );
170386 assert( pWin->pWFunc!=0 );
170387 pArgs = pWin->pOwner->x.pList;
170388 if( pWin->pWFunc->funcFlags & SQLITE_SUBTYPE ){
170390 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
170391 pWin->bExprArgs = 1;
170393 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
170396 if( pWin->pFilter ){
170397 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
170400 pWin->regAccum = ++pParse->nMem;
170401 pWin->regResult = ++pParse->nMem;
170402 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
170421 ("New window-function subquery in FROM clause of (%u/%p)\n",
170422 p->selId, p));
170423 p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
170424 assert( pSub!=0 || p->pSrc==0 ); /* Due to db->mallocFailed test inside
170427 if( p->pSrc ){
170429 p->pSrc->a[0].pSelect = pSub;
170430 p->pSrc->a[0].fg.isCorrelated = 1;
170431 sqlite3SrcListAssignCursors(pParse, p->pSrc);
170432 pSub->selFlags |= SF_Expanded|SF_OrderByReqd;
170434 pSub->selFlags |= (selFlags & SF_Aggregate);
170437 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
170442 pTab->tabFlags |= TF_Ephemeral;
170443 p->pSrc->a[0].pTab = pTab;
170454 if( db->mallocFailed ) rc = SQLITE_NOMEM;
170458 ** result-set or ORDER BY clause of the SELECT statement p. */
170462 assert( rc==SQLITE_OK || pParse->nErr!=0 );
170471 if( p->ppThis ){
170472 *p->ppThis = p->pNextWin;
170473 if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
170474 p->ppThis = 0;
170484 sqlite3ExprDelete(db, p->pFilter);
170485 sqlite3ExprListDelete(db, p->pPartition);
170486 sqlite3ExprListDelete(db, p->pOrderBy);
170487 sqlite3ExprDelete(db, p->pEnd);
170488 sqlite3ExprDelete(db, p->pStart);
170489 sqlite3DbFree(db, p->zName);
170490 sqlite3DbFree(db, p->zBase);
170500 Window *pNext = p->pNextWin;
170508 ** value should be a non-negative integer. If the value is not a
170509 ** constant, change it to NULL. The fact that it is then a non-negative
170516 sqlite3ExprDelete(pParse->db, pExpr);
170517 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
170572 pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
170574 pWin->eFrmType = eType;
170575 pWin->eStart = eStart;
170576 pWin->eEnd = eEnd;
170577 if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
170580 pWin->eExclude = eExclude;
170581 pWin->bImplicitFrame = bImplicitFrame;
170582 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
170583 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
170587 sqlite3ExprDelete(pParse->db, pEnd);
170588 sqlite3ExprDelete(pParse->db, pStart);
170594 ** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
170595 ** equivalent nul-terminated string.
170605 pWin->pPartition = pPartition;
170606 pWin->pOrderBy = pOrderBy;
170608 pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
170611 sqlite3ExprListDelete(pParse->db, pPartition);
170612 sqlite3ExprListDelete(pParse->db, pOrderBy);
170620 ** stored in the linked list starting at pWin->pNextWin. This function
170625 if( pWin->zBase ){
170626 sqlite3 *db = pParse->db;
170627 Window *pExist = windowFind(pParse, pList, pWin->zBase);
170631 if( pWin->pPartition ){
170633 }else if( pExist->pOrderBy && pWin->pOrderBy ){
170635 }else if( pExist->bImplicitFrame==0 ){
170640 "cannot override %s of window: %s", zErr, pWin->zBase
170643 pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
170644 if( pExist->pOrderBy ){
170645 assert( pWin->pOrderBy==0 );
170646 pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
170648 sqlite3DbFree(db, pWin->zBase);
170649 pWin->zBase = 0;
170660 assert( p->op==TK_FUNCTION );
170663 p->y.pWin = pWin;
170665 pWin->pOwner = p;
170666 if( (p->flags & EP_Distinct) && pWin->eFrmType!=TK_FILTER ){
170672 sqlite3WindowDelete(pParse->db, pWin);
170677 ** Possibly link window pWin into the list at pSel->pWin (window functions
170684 if( 0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0) ){
170685 pWin->pNextWin = pSel->pWin;
170686 if( pSel->pWin ){
170687 pSel->pWin->ppThis = &pWin->pNextWin;
170689 pSel->pWin = pWin;
170690 pWin->ppThis = &pSel->pWin;
170692 if( sqlite3ExprListCompare(pWin->pPartition, pSel->pWin->pPartition,-1) ){
170693 pSel->selFlags |= SF_MultiPart;
170712 if( p1->eFrmType!=p2->eFrmType ) return 1;
170713 if( p1->eStart!=p2->eStart ) return 1;
170714 if( p1->eEnd!=p2->eEnd ) return 1;
170715 if( p1->eExclude!=p2->eExclude ) return 1;
170716 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
170717 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
170718 if( (res = sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1)) ){
170721 if( (res = sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1)) ){
170725 if( (res = sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1)) ){
170735 ** to begin iterating through the sub-query results. It is used to allocate
170739 int nEphExpr = pSelect->pSrc->a[0].pSelect->pEList->nExpr;
170740 Window *pMWin = pSelect->pWin;
170744 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, nEphExpr);
170745 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
170746 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
170747 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
170751 if( pMWin->pPartition ){
170752 int nExpr = pMWin->pPartition->nExpr;
170753 pMWin->regPart = pParse->nMem+1;
170754 pParse->nMem += nExpr;
170755 sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
170758 pMWin->regOne = ++pParse->nMem;
170759 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne);
170761 if( pMWin->eExclude ){
170762 pMWin->regStartRowid = ++pParse->nMem;
170763 pMWin->regEndRowid = ++pParse->nMem;
170764 pMWin->csrApp = pParse->nTab++;
170765 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
170766 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
170767 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr);
170771 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
170772 FuncDef *p = pWin->pWFunc;
170773 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
170783 assert( ExprUseXList(pWin->pOwner) );
170784 pList = pWin->pOwner->x.pList;
170786 pWin->csrApp = pParse->nTab++;
170787 pWin->regApp = pParse->nMem+1;
170788 pParse->nMem += 3;
170789 if( pKeyInfo && pWin->pWFunc->zName[1]=='i' ){
170790 assert( pKeyInfo->aSortFlags[0]==0 );
170791 pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC;
170793 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
170795 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
170797 else if( p->zName==nth_valueName || p->zName==first_valueName ){
170798 /* Allocate two registers at pWin->regApp. These will be used to
170800 pWin->regApp = pParse->nMem+1;
170801 pWin->csrApp = pParse->nTab++;
170802 pParse->nMem += 2;
170803 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
170805 else if( p->zName==leadName || p->zName==lagName ){
170806 pWin->csrApp = pParse->nTab++;
170807 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
170822 ** code to check that the value is a non-negative integer and throws an
170827 "frame starting offset must be a non-negative integer",
170828 "frame ending offset must be a non-negative integer",
170830 "frame starting offset must be a non-negative number",
170831 "frame ending offset must be a non-negative number",
170869 ** Return the number of arguments passed to the window-function associated
170874 assert( ExprUseXList(pWin->pOwner) );
170875 pList = pWin->pOwner->x.pList;
170876 return (pList ? pList->nExpr : 0);
170899 ** in an array of accumulator registers - one for each window function
170906 ** order to reduce memory requirements - it would always be safe just
170921 ** Consider a window-frame similar to the following:
170941 ** Depending on the window-frame in question, all three cursors may not
170969 Window *pMWin = p->pMWin;
170970 ExprList *pOrderBy = pMWin->pOrderBy;
170972 Vdbe *v = sqlite3GetVdbe(p->pParse);
170973 ExprList *pPart = pMWin->pPartition;
170974 int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
170976 for(i=0; i<pOrderBy->nExpr; i++){
170984 ** xInverse (if bInverse is non-zero) for each window function in the
170985 ** linked list starting at pMWin. Or, for built-in window functions
170996 ** already populated with all columns from the current row of the sub-query.
170998 ** If argument regPartSize is non-zero, then it is a register containing the
171008 Parse *pParse = p->pParse;
171011 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
171012 FuncDef *pFunc = pWin->pWFunc;
171014 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
171017 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
171024 if( i!=1 || pFunc->zName!=nth_valueName ){
171025 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
171027 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
171032 if( pMWin->regStartRowid==0
171033 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX)
171034 && (pWin->eStart!=TK_UNBOUNDED)
171039 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
171040 sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
171041 sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
171042 sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
171044 sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
171046 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
171047 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
171050 }else if( pWin->regApp ){
171051 assert( pFunc->zName==nth_valueName
171052 || pFunc->zName==first_valueName
171055 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
171056 }else if( pFunc->xSFunc!=noopStepFunc ){
171058 if( pWin->pFilter ){
171060 assert( ExprUseXList(pWin->pOwner) );
171061 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
171062 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
171064 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
171070 if( pWin->bExprArgs ){
171074 assert( ExprUseXList(pWin->pOwner) );
171075 nArg = pWin->pOwner->x.pList->nExpr;
171077 sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0);
171081 if( pOp->opcode==OP_Column && pOp->p1==pMWin->iEphCsr ){
171082 pOp->p1 = csr;
171086 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
171089 assert( ExprUseXList(pWin->pOwner) );
171090 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
171094 bInverse, regArg, pWin->regAccum);
171097 if( pWin->bExprArgs ){
171115 ** pMWin. Or, for built-in window-functions that do not use the standard
171119 Parse *pParse = p->pParse;
171120 Window *pMWin = p->pMWin;
171124 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
171125 if( pMWin->regStartRowid==0
171126 && (pWin->pWFunc->funcFlags & SQLITE_FUNC_MINMAX)
171127 && (pWin->eStart!=TK_UNBOUNDED)
171129 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
171130 sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
171132 sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
171133 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
171134 }else if( pWin->regApp ){
171135 assert( pMWin->regStartRowid==0 );
171139 sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg);
171140 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF);
171141 sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
171142 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
171144 sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult);
171145 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF);
171153 ** p->pMWin list by doing a full scan of the current window frame. Store the
171159 Parse *pParse = p->pParse;
171160 Window *pMWin = p->pMWin;
171161 Vdbe *v = p->pVdbe;
171177 csr = pMWin->csrApp;
171178 nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
171190 sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid);
171191 windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
171193 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
171194 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
171197 sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid);
171201 sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid);
171204 if( pMWin->eExclude==TK_CURRENT ){
171207 }else if( pMWin->eExclude!=TK_NO ){
171212 if( pMWin->pOrderBy ){
171213 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
171215 if( pMWin->eExclude==TK_TIES ){
171232 windowAggStep(p, pMWin, csr, 0, p->regArg);
171237 sqlite3VdbeJumpHere(v, addrNext-1);
171251 ** Invoke the sub-routine at regGosub (generated by code in select.c) to
171255 ** for per-row processing is only generated for the following built-in window
171264 Window *pMWin = p->pMWin;
171265 Vdbe *v = p->pVdbe;
171267 if( pMWin->regStartRowid ){
171270 Parse *pParse = p->pParse;
171273 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
171274 FuncDef *pFunc = pWin->pWFunc;
171275 assert( ExprUseXList(pWin->pOwner) );
171276 if( pFunc->zName==nth_valueName
171277 || pFunc->zName==first_valueName
171279 int csr = pWin->csrApp;
171282 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
171284 if( pFunc->zName==nth_valueName ){
171285 sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
171290 sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
171291 sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
171295 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
171299 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
171300 int nArg = pWin->pOwner->x.pList->nExpr;
171301 int csr = pWin->csrApp;
171304 int iEph = pMWin->iEphCsr;
171307 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
171309 sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult);
171313 int val = (pFunc->zName==leadName ? 1 : -1);
171316 int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
171318 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
171325 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
171331 sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub);
171337 ** any equivalent initialization required by any built-in window functions
171345 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
171346 FuncDef *pFunc = pWin->pWFunc;
171347 assert( pWin->regAccum );
171348 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
171350 if( pMWin->regStartRowid==0 ){
171351 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
171352 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
171353 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
171356 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
171357 assert( pWin->eStart!=TK_UNBOUNDED );
171358 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
171359 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
171363 regArg = pParse->nMem+1;
171364 pParse->nMem += nArg;
171374 if( pMWin->regStartRowid ) return 1;
171375 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
171376 FuncDef *pFunc = pWin->pWFunc;
171377 if( (pFunc->zName==nth_valueName)
171378 || (pFunc->zName==first_valueName)
171379 || (pFunc->zName==leadName)
171380 || (pFunc->zName==lagName)
171390 ** pOrderBy->nExpr. This function generates code to compare the two
171407 int nVal = pOrderBy->nExpr;
171415 sqlite3VdbeAddOp3(v, OP_Copy, regNew, regOld, nVal-1);
171430 ** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
171432 ** If the sort-order for the ORDER BY term in the window is DESC, then the
171434 ** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
171438 ** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
171448 int regVal, /* Register containing non-negative number */
171452 Parse *pParse = p->pParse;
171454 ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */
171457 int regString = ++pParse->nMem; /* Reg. for constant value '' */
171463 /* Read the peer-value from each cursor into a register */
171468 assert( pOrderBy && pOrderBy->nExpr==1 );
171469 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_DESC ){
171479 reg1, (arith==OP_Add ? "+" : "-"), regVal,
171502 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_BIGNULL ){
171517 default: assert( op==OP_Lt ); /* no-op */ break;
171528 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
171531 ** then leave reg1 as it is. In pseudo-code, this is implemented as:
171534 ** reg1 = reg1 +/- regVal
171537 ** Since all strings and blobs are greater-than-or-equal-to an empty string,
171554 pColl = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[0].pExpr);
171583 Parse *pParse = p->pParse;
171584 Window *pMWin = p->pMWin;
171586 Vdbe *v = p->pVdbe;
171588 int bPeer = (pMWin->eFrmType!=TK_ROWS);
171593 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
171595 if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
171601 if( pMWin->eFrmType==TK_RANGE ){
171605 if( pMWin->eStart==TK_FOLLOWING ){
171607 p, OP_Le, p->current.csr, regCountdown, p->start.csr, lblDone
171611 p, OP_Ge, p->start.csr, regCountdown, p->current.csr, lblDone
171616 p, OP_Gt, p->end.csr, regCountdown, p->current.csr, lblDone
171625 if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
171636 if( pMWin->eStart==pMWin->eEnd && regCountdown
171637 && pMWin->eFrmType==TK_RANGE
171642 sqlite3VdbeAddOp2(v, OP_Rowid, p->start.csr, regRowid1);
171643 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid2);
171646 }else if( p->regRowid ){
171647 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid1);
171648 sqlite3VdbeAddOp3(v, OP_Ge, p->regRowid, lblDone, regRowid1);
171653 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING );
171658 csr = p->current.csr;
171659 reg = p->current.reg;
171664 csr = p->start.csr;
171665 reg = p->start.reg;
171666 if( pMWin->regStartRowid ){
171667 assert( pMWin->regEndRowid );
171668 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regStartRowid, 1);
171670 windowAggStep(p, pMWin, csr, 1, p->regArg);
171676 csr = p->end.csr;
171677 reg = p->end.reg;
171678 if( pMWin->regStartRowid ){
171679 assert( pMWin->regEndRowid );
171680 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regEndRowid, 1);
171682 windowAggStep(p, pMWin, csr, 0, p->regArg);
171687 if( op==p->eDelete ){
171705 int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
171708 windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
171730 pNew->zName = sqlite3DbStrDup(db, p->zName);
171731 pNew->zBase = sqlite3DbStrDup(db, p->zBase);
171732 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
171733 pNew->pWFunc = p->pWFunc;
171734 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
171735 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
171736 pNew->eFrmType = p->eFrmType;
171737 pNew->eEnd = p->eEnd;
171738 pNew->eStart = p->eStart;
171739 pNew->eExclude = p->eExclude;
171740 pNew->regResult = p->regResult;
171741 pNew->regAccum = p->regAccum;
171742 pNew->iArgCol = p->iArgCol;
171743 pNew->iEphCsr = p->iEphCsr;
171744 pNew->bExprArgs = p->bExprArgs;
171745 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
171746 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
171747 pNew->pOwner = pOwner;
171748 pNew->bImplicitFrame = p->bImplicitFrame;
171763 for(pWin=p; pWin; pWin=pWin->pNextWin){
171766 pp = &((*pp)->pNextWin);
171782 sqlite3 *db = pParse->db;
171784 sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC, &pVal);
171796 ** and invoke the sub-routine at instruction addrGosub once for each row.
171826 ** if( (regEnd--)<=0 ){
171828 ** if( (regStart--)<=0 ){
171839 ** if( (regStart--)<=0 ){
171845 ** The pseudo-code above uses the following shorthand:
171860 ** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
171879 ** if( (regEnd--)<=0 ){
171883 ** if( (regStart--)<=0 ){
171889 ** if( (regEnd--)<=0 ){
171905 ** regStart = regEnd - <expr1>
171908 ** if( (regEnd--)<=0 ){
171911 ** if( (regStart--)<=0 ){
171919 ** if( (regEnd--)<=0 ){
171923 ** if( (regStart--)<=0 ){
171935 ** This is optimized of course - branches that will never be taken and
171956 ** if( (regStart--)<=0 ){
171972 ** To handle this case, the pseudo-code programs depicted above are modified
171992 ** of the outer loop - the one started by sqlite3WhereBegin().
172134 Window *pMWin = p->pWin;
172135 ExprList *pOrderBy = pMWin->pOrderBy;
172138 int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */
172139 int nInput = p->pSrc->a[0].pTab->nCol; /* Number of cols returned by sub */
172150 WindowCodeArg s; /* Context object for sub-routines */
172155 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT
172156 || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED
172158 assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT
172159 || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING
172161 assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
172162 || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
172163 || pMWin->eExclude==TK_NO
172175 s.current.csr = pMWin->iEphCsr;
172181 ** are four options - they may never be deleted (eDelete==0), they may
172186 switch( pMWin->eStart ){
172188 if( pMWin->eFrmType!=TK_RANGE
172189 && windowExprGtZero(pParse, pMWin->pStart)
172196 if( pMWin->eEnd==TK_PRECEDING ){
172197 if( pMWin->eFrmType!=TK_RANGE
172198 && windowExprGtZero(pParse, pMWin->pEnd)
172212 /* Allocate registers for the array of values from the sub-query, the
172215 regNew = pParse->nMem+1;
172216 pParse->nMem += nInput;
172217 regRecord = ++pParse->nMem;
172218 s.regRowid = ++pParse->nMem;
172223 if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
172224 regStart = ++pParse->nMem;
172226 if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
172227 regEnd = ++pParse->nMem;
172233 if( pMWin->eFrmType!=TK_ROWS ){
172234 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
172235 regNewPeer = regNew + pMWin->nBufferCol;
172236 if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
172237 regPeer = pParse->nMem+1; pParse->nMem += nPeer;
172238 s.start.reg = pParse->nMem+1; pParse->nMem += nPeer;
172239 s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
172240 s.end.reg = pParse->nMem+1; pParse->nMem += nPeer;
172243 /* Load the column values for the row returned by the sub-select
172256 if( pMWin->pPartition ){
172258 ExprList *pPart = pMWin->pPartition;
172259 int nPart = pPart->nExpr;
172260 int regNewPart = regNew + pMWin->nBufferCol;
172263 regFlushPart = ++pParse->nMem;
172264 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart);
172270 sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
172276 addrNe = sqlite3VdbeAddOp3(v, OP_Ne, pMWin->regOne, 0, s.regRowid);
172283 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
172284 windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE?3:0));
172287 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
172288 windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE?3:0));
172291 if( pMWin->eFrmType!=TK_RANGE && pMWin->eStart==pMWin->eEnd && regStart ){
172292 int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
172303 if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
172304 assert( pMWin->eEnd==TK_FOLLOWING );
172308 if( pMWin->eStart!=TK_UNBOUNDED ){
172314 sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, pOrderBy->nExpr-1);
172315 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.start.reg, pOrderBy->nExpr-1);
172316 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.current.reg, pOrderBy->nExpr-1);
172317 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.end.reg, pOrderBy->nExpr-1);
172328 if( pMWin->eStart==TK_FOLLOWING ){
172330 if( pMWin->eEnd!=TK_UNBOUNDED ){